1010#include < executorch/kernels/portable/cpu/util/copy_ops_util.h>
1111#include < executorch/runtime/kernel/kernel_includes.h>
1212
13+ #include < algorithm>
14+ #include < cstring>
15+
1316namespace torch {
1417namespace executor {
1518namespace native {
@@ -19,6 +22,30 @@ using Tensor = executorch::aten::Tensor;
1922template <typename T>
2023using OptionalArrayRef = executorch::aten::OptionalArrayRef<T>;
2124
25+ namespace {
26+
27+ /* *
28+ * Checks the conditions for fast path direct memcpy. This can be used
29+ * when the output dim order is unchanged.
30+ */
31+ bool check_fast_path_conditions (
32+ const Tensor& in,
33+ OptionalArrayRef<int64_t > dim_order) {
34+ if (!dim_order.has_value ()) {
35+ // No dim order means preserve input dim order.
36+ return true ;
37+ }
38+
39+ auto input_dim_order = in.dim_order ();
40+ return std::equal (
41+ dim_order.value ().begin (),
42+ dim_order.value ().end (),
43+ input_dim_order.begin (),
44+ input_dim_order.end ());
45+ }
46+
47+ } // namespace
48+
2249/* *
2350 * _clone_dim_order.out(Tensor self, *, bool non_blocking=False, int[]?
2451 * dim_order=None, Tensor(a!) out) -> Tensor(a!)
@@ -55,13 +82,18 @@ Tensor& _clone_dim_order_out(
5582 return out;
5683 }
5784
58- // Select the correct input dtype and copy the tensors.
59- ET_SWITCH_REALHBBF16_TYPES (
60- self.scalar_type (),
61- ctx,
62- " dim_order_ops::_clone_dim_order.out" ,
63- CTYPE ,
64- [&] { _to_dim_order_copy_impl<CTYPE , CTYPE >(self, out); });
85+ // Dispatch to the fast path if we can use direct memcpy.
86+ if (check_fast_path_conditions (in, dim_order)) {
87+ std::memcpy (out.mutable_data_ptr (), src.const_data_ptr (), src.nbytes ());
88+ } else {
89+ // Select the correct input dtype and copy the tensors.
90+ ET_SWITCH_REALHBBF16_TYPES (
91+ self.scalar_type (),
92+ ctx,
93+ " dim_order_ops::_clone_dim_order.out" ,
94+ CTYPE ,
95+ [&] { _to_dim_order_copy_impl<CTYPE , CTYPE >(self, out); });
96+ }
6597
6698 return out;
6799}
@@ -77,4 +109,4 @@ Tensor& _clone_dim_order_out(
77109
78110} // namespace native
79111} // namespace executor
80- } // namespace torch
112+ } // namespace torch
0 commit comments