CUDA `copy_strided_src` 会在偏移源视图的末尾复制数据,从而无声地损坏 `slice_scatter`(CPU 会出现不一致的情况)。
作者: PCfVW创建于 2026年9月2日更新于 2026年9月2日
// candle-core/src/cpu_backend/mod.rs:902 -- copies exactly the view's length
StridedBlocks::SingleBlock { start_offset, len } =>
dst[dst_offset..dst_offset + len]
.copy_from_slice(&src[start_offset..start_offset + len])// candle-core/src/cuda_backend/mod.rs:1208 -- derives the length from storage
fn slice_src_and_dst<'a, T>(
src: &'a CudaSlice<T>,
src_l: &Layout,
dst: &'a mut CudaSlice<T>,
dst_offset: usize,
) -> (cudarc::driver::CudaView<'a, T>, cudarc::driver::CudaViewMut<'a, T>) {
let src_offset = src_l.start_offset();
let to_copy = dst
.len()
.saturating_sub(dst_offset)
.min(src.len().saturating_sub(src_offset));
let src = src.slice(src_offset..src_offset + to_copy);
let dst = dst.slice_mut(dst_offset..dst_offset + to_copy);
(src, dst)
}内容来源: huggingface/candle