Skip to content

Add direct copy fast path for portable copy op #10487

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
Apr 27, 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
58 changes: 36 additions & 22 deletions kernels/portable/cpu/op_copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ Tensor& copy_out(
// @lint-ignore CLANGTIDY facebook-hte-CArray
static constexpr const char op_name[] = "copy.out";

ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, "copy.out", CTYPE, [&]() {
utils::apply_bitensor_elementwise_fn<CTYPE, op_name>(
[](ET_UNUSED const CTYPE _, const CTYPE val_src) { return val_src; },
ctx,
in,
utils::SupportedTensorDtypes::REALHBBF16,
src,
utils::SupportedTensorDtypes::REALHBBF16,
out,
utils::SupportedTensorDtypes::REALHBBF16);
});
// Use direct copy fast path if broadcast is not needed and tensors are
// non-empty
if (internal::sizes_match_ignoring_leading_1s(out.sizes(), src.sizes()) &&
src.numel() > 0) {
std::memcpy(out.mutable_data_ptr(), src.const_data_ptr(), src.nbytes());
} else {
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, "copy.out", CTYPE, [&]() {
utils::apply_bitensor_elementwise_fn<CTYPE, op_name>(
[](ET_UNUSED const CTYPE _, const CTYPE val_src) { return val_src; },
ctx,
in,
utils::SupportedTensorDtypes::REALHBBF16,
src,
utils::SupportedTensorDtypes::REALHBBF16,
out,
utils::SupportedTensorDtypes::REALHBBF16);
});
}

return out;
}
Expand All @@ -79,17 +86,24 @@ Tensor& copy_(
// @lint-ignore CLANGTIDY facebook-hte-CArray
static constexpr const char op_name[] = "copy_";

ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, "copy_", CTYPE, [&]() {
utils::apply_bitensor_elementwise_fn<CTYPE, op_name>(
[](ET_UNUSED const CTYPE _, const CTYPE val_src) { return val_src; },
ctx,
in,
utils::SupportedTensorDtypes::REALHBBF16,
src,
utils::SupportedTensorDtypes::REALHBBF16,
in,
utils::SupportedTensorDtypes::REALHBBF16);
});
// Use direct copy fast path if broadcast is not needed and tensors are
// non-empty
if (internal::sizes_match_ignoring_leading_1s(in.sizes(), src.sizes()) &&
src.numel() > 0) {
std::memcpy(in.mutable_data_ptr(), src.const_data_ptr(), in.nbytes());
} else {
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, "copy_", CTYPE, [&]() {
utils::apply_bitensor_elementwise_fn<CTYPE, op_name>(
[](ET_UNUSED const CTYPE _, const CTYPE val_src) { return val_src; },
ctx,
in,
utils::SupportedTensorDtypes::REALHBBF16,
src,
utils::SupportedTensorDtypes::REALHBBF16,
in,
utils::SupportedTensorDtypes::REALHBBF16);
});
}

return in;
}
Expand Down
Loading