Skip to content

Commit 19d22ab

Browse files
Revert exposed_as_util functionality and refactor stack utils
Differential Revision: D81199586 Pull Request resolved: #13822
1 parent f4ec01a commit 19d22ab

File tree

7 files changed

+148
-320
lines changed

7 files changed

+148
-320
lines changed

kernels/portable/cpu/op_add.cpp

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
namespace torch {
1616
namespace executor {
1717
namespace native {
18-
namespace impl {
1918

2019
Tensor& add_out(
2120
KernelRuntimeContext& ctx,
@@ -152,77 +151,6 @@ Tensor& add_scalar_out(
152151
return out;
153152
}
154153

155-
} // namespace impl
156-
157-
Tensor& add_out(
158-
KernelRuntimeContext& ctx,
159-
const Tensor& a,
160-
const Tensor& b,
161-
const Scalar& alpha,
162-
Tensor& out) {
163-
return impl::add_out(ctx, a, b, alpha, out);
164-
}
165-
166-
Tensor& add_scalar_out(
167-
KernelRuntimeContext& ctx,
168-
const Tensor& a,
169-
const Scalar& b,
170-
const Scalar& alpha,
171-
Tensor& out) {
172-
return impl::add_scalar_out(ctx, a, b, alpha, out);
173-
}
174-
175-
namespace utils {
176-
177-
Tensor& add_out(
178-
KernelRuntimeContext& ctx,
179-
const Tensor& a,
180-
const Tensor& b,
181-
const Scalar& alpha,
182-
Tensor& out) {
183-
return impl::add_out(ctx, a, b, alpha, out);
184-
}
185-
186-
Tensor& add_scalar_out(
187-
KernelRuntimeContext& ctx,
188-
const Tensor& a,
189-
const Scalar& b,
190-
const Scalar& alpha,
191-
Tensor& out) {
192-
return impl::add_scalar_out(ctx, a, b, alpha, out);
193-
}
194-
195-
std::tuple<
196-
Error,
197-
std::array<executorch::aten::SizesType, kTensorDimensionLimit>,
198-
size_t>
199-
add_out_shape(const Tensor& a, const Tensor& b, ET_UNUSED const Scalar& alpha) {
200-
std::array<executorch::aten::SizesType, kTensorDimensionLimit> out_sizes{};
201-
size_t out_dim = 0;
202-
203-
Error err = get_broadcast_target_size(
204-
a, b, out_sizes.data(), kTensorDimensionLimit, &out_dim);
205-
206-
return std::make_tuple(err, out_sizes, out_dim);
207-
}
208-
209-
std::tuple<
210-
Error,
211-
std::array<executorch::aten::SizesType, kTensorDimensionLimit>,
212-
size_t>
213-
add_scalar_out_shape(
214-
const Tensor& a,
215-
ET_UNUSED const Scalar& b,
216-
ET_UNUSED const Scalar& alpha) {
217-
std::array<executorch::aten::SizesType, kTensorDimensionLimit> out_sizes{};
218-
size_t out_dim = a.dim();
219-
220-
std::copy(a.sizes().begin(), a.sizes().end(), out_sizes.begin());
221-
222-
return std::make_tuple(Error::Ok, out_sizes, out_dim);
223-
}
224-
225-
} // namespace utils
226154
} // namespace native
227155
} // namespace executor
228156
} // namespace torch

kernels/portable/cpu/op_add.h

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

kernels/portable/cpu/op_stack.cpp

Lines changed: 2 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -6,142 +6,21 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <cstring>
10-
11-
#include <c10/util/irange.h>
12-
#include <executorch/kernels/portable/cpu/util/copy_ops_util.h>
9+
#include <executorch/kernels/portable/cpu/util/stack_util.h>
1310
#include <executorch/runtime/kernel/kernel_includes.h>
1411

1512
namespace torch {
1613
namespace executor {
1714
namespace native {
18-
namespace impl {
19-
20-
using Tensor = executorch::aten::Tensor;
2115

2216
Tensor& stack_out(
2317
KernelRuntimeContext& ctx,
2418
executorch::aten::ArrayRef<Tensor> tensors,
2519
int64_t dim,
2620
Tensor& out) {
27-
(void)ctx;
28-
29-
if (dim < 0) {
30-
dim += out.dim();
31-
}
32-
33-
ET_KERNEL_CHECK(
34-
ctx, check_stack_args(tensors, dim, out), InvalidArgument, out);
35-
36-
for (size_t i = 0; i < tensors.size(); ++i) {
37-
ET_KERNEL_CHECK(
38-
ctx,
39-
tensors_have_same_dim_order(tensors[i], out),
40-
InvalidArgument,
41-
out);
42-
}
43-
44-
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out);
45-
46-
Tensor::SizesType expected_out_size[kTensorDimensionLimit];
47-
size_t expected_out_dim = 0;
48-
get_stack_out_target_size(tensors, dim, expected_out_size, &expected_out_dim);
49-
ET_KERNEL_CHECK(
50-
ctx,
51-
resize_tensor(out, {expected_out_size, expected_out_dim}) == Error::Ok,
52-
InvalidArgument,
53-
out);
54-
55-
const size_t outer = getLeadingDims(out, dim);
56-
const size_t inner = getTrailingDims(out, dim);
57-
const size_t ninputs = tensors.size();
58-
59-
const auto out_type = out.scalar_type();
60-
ET_SWITCH_REALHBBF16_TYPES(out_type, ctx, "stack.out", CTYPE_OUT, [&] {
61-
CTYPE_OUT* out_ptr = out.mutable_data_ptr<CTYPE_OUT>();
62-
for (size_t i = 0; i < outer; ++i) {
63-
for (size_t j = 0; j < ninputs; ++j) {
64-
const auto in_type = tensors[j].scalar_type();
65-
ET_SWITCH_REALHBBF16_TYPES(in_type, ctx, "stack.out", CTYPE_IN, [&] {
66-
const CTYPE_IN* const in_ptr =
67-
tensors[j].const_data_ptr<CTYPE_IN>() + i * inner;
68-
69-
for (size_t k = 0; k < inner; ++k) {
70-
out_ptr[k] = static_cast<CTYPE_OUT>(in_ptr[k]);
71-
}
72-
out_ptr += inner;
73-
});
74-
}
75-
}
76-
});
77-
78-
return out;
79-
}
80-
81-
} // namespace impl
82-
83-
Tensor& stack_out(
84-
KernelRuntimeContext& ctx,
85-
executorch::aten::ArrayRef<Tensor> tensors,
86-
int64_t dim,
87-
Tensor& out) {
88-
return impl::stack_out(ctx, tensors, dim, out);
89-
}
90-
91-
namespace utils {
92-
93-
Tensor& stack_out(
94-
KernelRuntimeContext& ctx,
95-
executorch::aten::ArrayRef<Tensor> tensors,
96-
int64_t dim,
97-
Tensor& out) {
98-
return impl::stack_out(ctx, tensors, dim, out);
99-
}
100-
101-
std::tuple<
102-
Error,
103-
std::array<executorch::aten::SizesType, kTensorDimensionLimit>,
104-
size_t>
105-
stack_out_shape(executorch::aten::ArrayRef<Tensor> tensors, int64_t dim) {
106-
std::array<executorch::aten::SizesType, kTensorDimensionLimit> out_sizes{};
107-
size_t out_dim = 0;
108-
109-
// Check if tensors array is empty
110-
if (tensors.size() == 0) {
111-
return std::make_tuple(Error::InvalidArgument, out_sizes, out_dim);
112-
}
113-
114-
// Normalize negative dimension
115-
int64_t normalized_dim = dim;
116-
if (normalized_dim < 0) {
117-
normalized_dim += tensors[0].dim() + 1;
118-
}
119-
120-
// Check if dimension is valid
121-
if (normalized_dim < 0 || normalized_dim > tensors[0].dim()) {
122-
return std::make_tuple(Error::InvalidArgument, out_sizes, out_dim);
123-
}
124-
125-
// Check that all tensors have the same shape
126-
for (size_t i = 1; i < tensors.size(); ++i) {
127-
if (tensors[i].dim() != tensors[0].dim()) {
128-
return std::make_tuple(Error::InvalidArgument, out_sizes, out_dim);
129-
}
130-
for (const auto d : c10::irange(tensors[0].dim())) {
131-
if (tensors[i].size(d) != tensors[0].size(d)) {
132-
return std::make_tuple(Error::InvalidArgument, out_sizes, out_dim);
133-
}
134-
}
135-
}
136-
137-
// Compute output shape using the existing utility
138-
::torch::executor::get_stack_out_target_size(
139-
tensors, normalized_dim, out_sizes.data(), &out_dim);
140-
141-
return std::make_tuple(Error::Ok, out_sizes, out_dim);
21+
return utils::stack_out_impl(ctx, tensors, dim, out);
14222
}
14323

144-
} // namespace utils
14524
} // namespace native
14625
} // namespace executor
14726
} // namespace torch

0 commit comments

Comments
 (0)