mirror of
https://github.com/huggingface/candle.git
synced 2025-06-17 02:58:50 +00:00
Handle the contiguous case in an optimized way when copying cpu memory.
This commit is contained in:
@ -115,19 +115,27 @@ impl CpuStorage {
|
|||||||
src_stride: &[usize],
|
src_stride: &[usize],
|
||||||
dst_offset: usize,
|
dst_offset: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// TODO: Optimize the contiguous case.
|
|
||||||
let src_indexes = StridedIndex::new(src_shape.dims(), src_stride);
|
|
||||||
match (self, dst) {
|
match (self, dst) {
|
||||||
(Self::F32(src), Self::F32(dst)) => {
|
(Self::F32(src), Self::F32(dst)) => {
|
||||||
|
if src_shape.is_contiguous(src_stride) {
|
||||||
|
dst[dst_offset..].copy_from_slice(src)
|
||||||
|
} else {
|
||||||
|
let src_indexes = StridedIndex::new(src_shape.dims(), src_stride);
|
||||||
for (dst_index, src_index) in src_indexes.enumerate() {
|
for (dst_index, src_index) in src_indexes.enumerate() {
|
||||||
dst[dst_index + dst_offset] = src[src_index]
|
dst[dst_index + dst_offset] = src[src_index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
(Self::F64(src), Self::F64(dst)) => {
|
(Self::F64(src), Self::F64(dst)) => {
|
||||||
|
if src_shape.is_contiguous(src_stride) {
|
||||||
|
dst[dst_offset..].copy_from_slice(src)
|
||||||
|
} else {
|
||||||
|
let src_indexes = StridedIndex::new(src_shape.dims(), src_stride);
|
||||||
for (dst_index, src_index) in src_indexes.enumerate() {
|
for (dst_index, src_index) in src_indexes.enumerate() {
|
||||||
dst[dst_index + dst_offset] = src[src_index]
|
dst[dst_index + dst_offset] = src[src_index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
(_, dst) => {
|
(_, dst) => {
|
||||||
// This should be covered by the dtype check above.
|
// This should be covered by the dtype check above.
|
||||||
return Err(Error::DTypeMismatchBinaryOp {
|
return Err(Error::DTypeMismatchBinaryOp {
|
||||||
|
Reference in New Issue
Block a user