Skip to content

Commit f8d18ec

Browse files
committed
Revert ATen _clone_dim_order implementation and refactoring
1 parent beeeebd commit f8d18ec

File tree

5 files changed

+65
-93
lines changed

5 files changed

+65
-93
lines changed

kernels/aten/cpu/op__clone_dim_order.cpp

Lines changed: 0 additions & 62 deletions
This file was deleted.

kernels/aten/cpu/op__to_dim_order_copy.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/kernels/aten/cpu/util/copy_ops_util.h>
109
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
1110
#include <executorch/runtime/kernel/kernel_includes.h>
1211

@@ -26,6 +25,71 @@ using OptionalArrayRef = executorch::aten::OptionalArrayRef<T>;
2625
template <typename T>
2726
using Optional = std::optional<T>;
2827

28+
namespace {
29+
Optional<MemoryFormat> get_memory_format(OptionalArrayRef<int64_t> dim_order) {
30+
if (!dim_order.has_value()) {
31+
return executorch::aten::nullopt;
32+
}
33+
if (is_contiguous_dim_order(
34+
dim_order.value().data(), dim_order.value().size())) {
35+
return MemoryFormat::Contiguous;
36+
} else if (is_channels_last_dim_order(
37+
dim_order.value().data(), dim_order.value().size())) {
38+
return MemoryFormat::ChannelsLast;
39+
} else {
40+
ET_ASSERT_UNREACHABLE();
41+
}
42+
}
43+
44+
bool check__to_dim_order_copy_args(
45+
const Tensor& input,
46+
bool non_blocking,
47+
executorch::aten::OptionalArrayRef<int64_t> dim_order,
48+
Tensor& out) {
49+
// Right now we only support blocking data transfer
50+
ET_LOG_AND_RETURN_IF_FALSE(non_blocking == false);
51+
52+
// dim_order is set, the target dim_order will be either contiguous or
53+
// channels_last memory format
54+
if (dim_order.has_value()) {
55+
executorch::aten::ArrayRef<int64_t> dim_order_ref = dim_order.value();
56+
57+
// dim order size shall equal to input dim
58+
ET_LOG_AND_RETURN_IF_FALSE(dim_order_ref.size() == input.dim());
59+
60+
ET_LOG_AND_RETURN_IF_FALSE(
61+
is_channels_last_dim_order(
62+
dim_order.value().data(), dim_order.value().size()) ||
63+
is_contiguous_dim_order(
64+
dim_order.value().data(), dim_order.value().size()));
65+
66+
// Out Aten tensor shall have same memory format stride as dim_order
67+
const size_t kMaxNumOfDimensions = 16;
68+
ET_LOG_AND_RETURN_IF_FALSE(kMaxNumOfDimensions >= out.dim());
69+
executorch::aten::StridesType target_strides[kMaxNumOfDimensions];
70+
dim_order_to_stride_nocheck(
71+
out.sizes().data(),
72+
dim_order_ref.data(),
73+
dim_order_ref.size(),
74+
target_strides);
75+
ET_LOG_AND_RETURN_IF_FALSE(out.dim() == dim_order_ref.size());
76+
for (size_t i = 0; i < dim_order_ref.size(); i++) {
77+
ET_LOG_AND_RETURN_IF_FALSE(target_strides[i] == out.strides()[i]);
78+
}
79+
80+
} else { // dim_order is not set, preserve the dim order of input
81+
82+
auto out_strides = out.strides();
83+
auto input_strides = input.strides();
84+
ET_LOG_AND_RETURN_IF_FALSE(input_strides.size() == out_strides.size());
85+
for (size_t i = 0; i < input_strides.size(); i++) {
86+
ET_LOG_AND_RETURN_IF_FALSE(input_strides[i] == out_strides[i]);
87+
}
88+
}
89+
return true;
90+
}
91+
} // namespace
92+
2993
// _to_dim_order_copy.out(Tensor self, *, bool non_blocking=False, int[]?
3094
// dim_order=None, Tensor(a!) out) -> Tensor(a!)
3195
Tensor& _to_dim_order_copy_out(

kernels/aten/cpu/util/copy_ops_util.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,6 @@ namespace torch {
1515
namespace executor {
1616

1717
using Tensor = executorch::aten::Tensor;
18-
using MemoryFormat = executorch::aten::MemoryFormat;
19-
20-
/**
21-
* Determines the memory format (Contiguous or ChannelsLast) corresponding to
22-
* the dim_order. Provides support for bridging torch.memory_format with
23-
* ExecuTorch's dim_order.
24-
*/
25-
std::optional<MemoryFormat> get_memory_format(
26-
executorch::aten::OptionalArrayRef<int64_t> dim_order) {
27-
if (!dim_order.has_value()) {
28-
return executorch::aten::nullopt;
29-
}
30-
if (is_contiguous_dim_order(
31-
dim_order.value().data(), dim_order.value().size())) {
32-
return MemoryFormat::Contiguous;
33-
} else if (is_channels_last_dim_order(
34-
dim_order.value().data(), dim_order.value().size())) {
35-
return MemoryFormat::ChannelsLast;
36-
} else {
37-
ET_ASSERT_UNREACHABLE();
38-
}
39-
}
4018

4119
bool check__to_dim_order_copy_args(
4220
const Tensor& input,

kernels/aten/cpu/util/copy_ops_util.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
namespace torch {
1414
namespace executor {
1515

16-
std::optional<MemoryFormat> get_memory_format(
17-
executorch::aten::OptionalArrayRef<int64_t> dim_order);
18-
1916
bool check__to_dim_order_copy_args(
2017
const Tensor& input,
2118
bool non_blocking,

kernels/aten/edge_dialect_aten_op.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,3 @@
1111
kernels:
1212
- arg_meta: null
1313
kernel_name: torch::executor::_to_dim_order_copy_out
14-
15-
- func: dim_order_ops::_clone_dim_order.out(Tensor self, *, bool non_blocking=False, int[]? dim_order=None, Tensor(a!) out) -> Tensor(a!)
16-
kernels:
17-
- arg_meta: null
18-
kernel_name: torch::executor::_clone_dim_order_out

0 commit comments

Comments
 (0)