Skip to content

Use Memcpy in copy_utils #11430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions kernels/portable/cpu/util/copy_ops_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ void _as_strided_copy(
ArrayRef<int64_t> stride,
int64_t dim) {
// the last dimension, copy data
const int64_t stride_dim = stride.at(dim);
if (dim == static_cast<int64_t>(size.size()) - 1) {
for (const auto i : c10::irange(size.at(dim))) {
output_data[i] = *input_data;
input_data += stride.at(dim);
const size_t num_elements = size.at(dim);
// use memcpy for contiguous memory
if (stride_dim == 1) {
memcpy(output_data, input_data, num_elements * sizeof(CTYPE));
} else {
for (const auto i : c10::irange(num_elements)) {
output_data[i] = *input_data;
input_data += stride_dim;
}
}
return;
}
Expand All @@ -39,7 +46,7 @@ void _as_strided_copy(
for ([[maybe_unused]] const auto i : c10::irange(size.at(dim))) {
_as_strided_copy<CTYPE>(
input_data, output_data, out, size, stride, dim + 1);
input_data += stride.at(dim);
input_data += stride_dim;
output_data += trailing_dims;
}
}
Expand Down
Loading