Skip to content

Commit 288e758

Browse files
authored
[executorch] Migrate most of extension/... to new namespace
Differential Revision: D60938936 Pull Request resolved: #4617
1 parent 3323164 commit 288e758

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+715
-429
lines changed

extension/aten_util/aten_bridge.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include <executorch/runtime/platform/assert.h>
1212
#include <cstring>
1313

14-
namespace torch {
15-
namespace util {
14+
namespace executorch {
15+
namespace extension {
1616

1717
namespace {
1818
void check_tensor_meta(const at::Tensor& a, const exec_aten::Tensor& b) {
@@ -55,14 +55,15 @@ ET_CHECK_MSG(
5555
}
5656
// check dtype
5757
ET_CHECK_MSG(
58-
b.scalar_type() == torchToExecuTorchScalarType(a.options().dtype()),
58+
b.scalar_type() == torch_to_executorch_scalar_type(a.options().dtype()),
5959
"dtypes dont match a %hhd vs. b %hhd",
60-
torchToExecuTorchScalarType(a.options().dtype()),
60+
torch_to_executorch_scalar_type(a.options().dtype()),
6161
b.scalar_type());
6262
}
6363
} // namespace
6464

65-
torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type) {
65+
torch::executor::ScalarType torch_to_executorch_scalar_type(
66+
caffe2::TypeMeta type) {
6667
switch (c10::typeMetaToScalarType(type)) {
6768
case c10::ScalarType::Byte:
6869
return torch::executor::ScalarType::Byte;
@@ -91,7 +92,8 @@ torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type) {
9192
}
9293
}
9394

94-
c10::ScalarType execuTorchtoTorchScalarType(torch::executor::ScalarType type) {
95+
c10::ScalarType executorch_to_torch_scalar_type(
96+
torch::executor::ScalarType type) {
9597
switch (type) {
9698
case torch::executor::ScalarType::Byte:
9799
return c10::ScalarType::Byte;
@@ -147,7 +149,8 @@ void alias_etensor_to_attensor(
147149
}
148150

149151
at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& etensor) {
150-
c10::ScalarType dtype = execuTorchtoTorchScalarType(etensor.scalar_type());
152+
c10::ScalarType dtype =
153+
executorch_to_torch_scalar_type(etensor.scalar_type());
151154
std::vector<int64_t> at_tensor_sizes(
152155
etensor.sizes().begin(), etensor.sizes().end());
153156
std::vector<int64_t> at_tensor_strides(
@@ -162,5 +165,6 @@ at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& etensor) {
162165
check_tensor_meta(t, etensor);
163166
return t;
164167
}
165-
} // namespace util
166-
} // namespace torch
168+
169+
} // namespace extension
170+
} // namespace executorch

extension/aten_util/aten_bridge.h

+38-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
#include <memory>
1919
#include <vector>
2020

21-
namespace torch {
22-
namespace util {
21+
namespace executorch {
22+
namespace extension {
2323

24-
torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type);
24+
torch::executor::ScalarType torch_to_executorch_scalar_type(
25+
caffe2::TypeMeta type);
2526

26-
c10::ScalarType execuTorchtoTorchScalarType(torch::executor::ScalarType type);
27+
c10::ScalarType executorch_to_torch_scalar_type(
28+
torch::executor::ScalarType type);
2729

2830
/*
2931
* @param[in] aten_tensor Input at::Tensor
@@ -45,5 +47,37 @@ void alias_etensor_to_attensor(at::Tensor& at, torch::executor::Tensor& et);
4547
* cloned.
4648
*/
4749
at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& et);
50+
51+
} // namespace extension
52+
} // namespace executorch
53+
54+
namespace torch {
55+
namespace executor {
56+
namespace util {
57+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
58+
// to the new `::executorch` namespaces.
59+
using ::executorch::extension::alias_attensor_to_etensor;
60+
using ::executorch::extension::alias_etensor_to_attensor;
61+
inline torch::executor::ScalarType torchToExecuTorchScalarType(
62+
caffe2::TypeMeta type) {
63+
return ::executorch::extension::torch_to_executorch_scalar_type(type);
64+
}
65+
inline c10::ScalarType execuTorchtoTorchScalarType(
66+
torch::executor::ScalarType type) {
67+
return ::executorch::extension::executorch_to_torch_scalar_type(type);
68+
}
69+
} // namespace util
70+
} // namespace executor
71+
} // namespace torch
72+
73+
// Some users refer to these as `torch::util::`.
74+
namespace torch {
75+
namespace util {
76+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
77+
// to the new `::executorch` namespaces.
78+
using ::torch::executor::util::alias_attensor_to_etensor;
79+
using ::torch::executor::util::alias_etensor_to_attensor;
80+
using ::torch::executor::util::execuTorchtoTorchScalarType;
81+
using ::torch::executor::util::torchToExecuTorchScalarType;
4882
} // namespace util
4983
} // namespace torch

extension/aten_util/make_aten_functor_from_et_functor.h

+39-22
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
2525
#include <torch/torch.h>
2626

27-
namespace torch {
28-
namespace executor {
27+
namespace executorch {
28+
namespace extension {
29+
namespace internal {
2930

3031
// Map types from ETen to ATen.
3132
// This is used to convert ETen arguments into ATen.
@@ -105,29 +106,35 @@ struct type_convert<
105106
torch::executor::Tensor>>>
106107
final {
107108
explicit type_convert(ATensor value) : value_(value) {
108-
auto sizes = std::make_shared<std::vector<Tensor::SizesType>>(
109-
value_.sizes().begin(), value_.sizes().end());
109+
auto sizes =
110+
std::make_shared<std::vector<torch::executor::Tensor::SizesType>>(
111+
value_.sizes().begin(), value_.sizes().end());
110112
const ssize_t dim = sizes->size();
111-
auto dim_order = std::make_shared<std::vector<Tensor::DimOrderType>>(dim);
112-
auto strides = std::make_shared<std::vector<Tensor::StridesType>>(dim);
113+
auto dim_order =
114+
std::make_shared<std::vector<torch::executor::Tensor::DimOrderType>>(
115+
dim);
116+
auto strides =
117+
std::make_shared<std::vector<torch::executor::Tensor::StridesType>>(
118+
dim);
113119

114120
std::iota(dim_order->begin(), dim_order->end(), 0);
115-
dim_order_to_stride_nocheck(
121+
::executorch::runtime::dim_order_to_stride_nocheck(
116122
sizes->data(), dim_order->data(), dim, strides->data());
117123

118-
auto tensor_impl = std::make_shared<TensorImpl>(
124+
auto tensor_impl = std::make_shared<torch::executor::TensorImpl>(
119125
static_cast<torch::executor::ScalarType>(value_.scalar_type()),
120126
sizes->size(),
121127
sizes->data(),
122128
value_.mutable_data_ptr(),
123129
dim_order->data(),
124130
strides->data());
125131

126-
converted_ = std::unique_ptr<Tensor, std::function<void(Tensor*)>>(
127-
new Tensor(tensor_impl.get()),
128-
[sizes, dim_order, strides, tensor_impl](Tensor* pointer) {
129-
delete pointer;
130-
});
132+
converted_ = std::unique_ptr<
133+
torch::executor::Tensor,
134+
std::function<void(torch::executor::Tensor*)>>(
135+
new torch::executor::Tensor(tensor_impl.get()),
136+
[sizes, dim_order, strides, tensor_impl](
137+
torch::executor::Tensor* pointer) { delete pointer; });
131138
}
132139

133140
ETensor call() {
@@ -136,7 +143,10 @@ struct type_convert<
136143

137144
private:
138145
ATensor value_;
139-
std::unique_ptr<Tensor, std::function<void(Tensor*)>> converted_;
146+
std::unique_ptr<
147+
torch::executor::Tensor,
148+
std::function<void(torch::executor::Tensor*)>>
149+
converted_;
140150
};
141151

142152
// Tensors: ETen to ATen.
@@ -258,7 +268,12 @@ struct wrapper_impl<R (*)(Args...), f, int, N> {
258268
using TupleArgsType = std::tuple<typename type_map<Args>::type...>;
259269
static constexpr size_t num_args = sizeof...(Args);
260270
static_assert(
261-
(N < num_args && std::is_same_v<element_t<N, typelist<Args...>>, R>) ||
271+
(N < num_args &&
272+
std::is_same_v<
273+
executorch::extension::kernel_util_internal::element_t<
274+
N,
275+
executorch::extension::kernel_util_internal::typelist<Args...>>,
276+
R>) ||
262277
N == -1,
263278
"The index of the out tensor can't be greater or equal to num_args and "
264279
"the Nth argument type has to be the same as the return type.");
@@ -298,16 +313,18 @@ struct wrapper_impl<R (*)(Args...), f, int, N> {
298313
}
299314
};
300315

301-
} // namespace executor
302-
} // namespace torch
316+
} // namespace internal
317+
} // namespace extension
318+
} // namespace executorch
303319

304320
// Wrapper macro for out variant function. N is the index of the out tensor.
305321
// We need N to know how to preserve the semantics of modifying out tensor and
306322
// return the reference without allocating a new memory buffer for out tensor.
307-
#define _WRAP_2(func, N) \
308-
::torch::executor::wrapper_impl<decltype(&func), func, decltype(N), N>::wrap
323+
#define _WRAP_2(func, N) \
324+
::executorch::extension::internal:: \
325+
wrapper_impl<decltype(&func), func, decltype(N), N>::wrap
309326
#define _WRAP_1(func) \
310-
::torch::executor::wrapper_impl<decltype(&func), func>::wrap
327+
::executorch::extension::internal::wrapper_impl<decltype(&func), func>::wrap
311328

312-
#define GET_MACRO(_1, _2, NAME, ...) NAME
313-
#define WRAP_TO_ATEN(...) GET_MACRO(__VA_ARGS__, _WRAP_2, _WRAP_1)(__VA_ARGS__)
329+
#define _GET_MACRO(_1, _2, NAME, ...) NAME
330+
#define WRAP_TO_ATEN(...) _GET_MACRO(__VA_ARGS__, _WRAP_2, _WRAP_1)(__VA_ARGS__)

extension/aten_util/test/aten_bridge_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <gtest/gtest.h>
1717

1818
using namespace ::testing;
19-
using namespace torch::util;
2019
using namespace torch::executor;
20+
using namespace torch::executor::util;
2121

2222
namespace {
2323
at::Tensor generate_at_tensor() {

extension/aten_util/test/make_aten_functor_from_et_functor_test.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
#include <gtest/gtest.h>
1515
#include <torch/library.h>
1616

17-
namespace torch {
18-
namespace executor {
19-
2017
using namespace ::testing;
18+
using ::executorch::extension::internal::type_convert;
19+
using ::executorch::extension::internal::type_map;
20+
using ::torch::executor::ScalarType;
21+
using ::torch::executor::Tensor;
2122

2223
Tensor& my_op_out(const Tensor& a, Tensor& out) {
2324
(void)a;
@@ -420,6 +421,3 @@ TEST_F(MakeATenFunctorFromETFunctorTest, TestWrap_ArrayRefOptional) {
420421
EXPECT_EQ(stack.size(), 1);
421422
EXPECT_EQ(stack[0].toTensor().const_data_ptr<int64_t>()[0], 4);
422423
}
423-
424-
} // namespace executor
425-
} // namespace torch

extension/data_loader/buffer_data_loader.h

+19-9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
#include <executorch/runtime/platform/log.h>
1515
#include <cstring>
1616

17-
namespace torch {
18-
namespace executor {
19-
namespace util {
17+
namespace executorch {
18+
namespace extension {
2019

2120
/**
2221
* A DataLoader that wraps a pre-allocated buffer. The FreeableBuffers
@@ -25,12 +24,13 @@ namespace util {
2524
* This can be used to wrap data that is directly embedded into the firmware
2625
* image, or to wrap data that was allocated elsewhere.
2726
*/
28-
class BufferDataLoader final : public DataLoader {
27+
class BufferDataLoader final : public executorch::runtime::DataLoader {
2928
public:
3029
BufferDataLoader(const void* data, size_t size)
3130
: data_(reinterpret_cast<const uint8_t*>(data)), size_(size) {}
3231

33-
ET_NODISCARD Result<FreeableBuffer> load(
32+
ET_NODISCARD
33+
executorch::runtime::Result<executorch::runtime::FreeableBuffer> load(
3434
size_t offset,
3535
size_t size,
3636
ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
@@ -41,14 +41,15 @@ class BufferDataLoader final : public DataLoader {
4141
offset,
4242
size,
4343
size_);
44-
return FreeableBuffer(data_ + offset, size, /*free_fn=*/nullptr);
44+
return executorch::runtime::FreeableBuffer(
45+
data_ + offset, size, /*free_fn=*/nullptr);
4546
}
4647

47-
ET_NODISCARD Result<size_t> size() const override {
48+
ET_NODISCARD executorch::runtime::Result<size_t> size() const override {
4849
return size_;
4950
}
5051

51-
ET_NODISCARD Error load_into(
52+
ET_NODISCARD executorch::runtime::Error load_into(
5253
size_t offset,
5354
size_t size,
5455
ET_UNUSED const SegmentInfo& segment_info,
@@ -63,14 +64,23 @@ class BufferDataLoader final : public DataLoader {
6364
return result.error();
6465
}
6566
std::memcpy(buffer, result->data(), size);
66-
return Error::Ok;
67+
return executorch::runtime::Error::Ok;
6768
}
6869

6970
private:
7071
const uint8_t* const data_; // uint8 is easier to index into.
7172
const size_t size_;
7273
};
7374

75+
} // namespace extension
76+
} // namespace executorch
77+
78+
namespace torch {
79+
namespace executor {
80+
namespace util {
81+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
82+
// to the new `::executorch` namespaces.
83+
using ::executorch::extension::BufferDataLoader;
7484
} // namespace util
7585
} // namespace executor
7686
} // namespace torch

extension/data_loader/file_data_loader.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
#define ET_HAVE_PREAD 1
3535
#endif // !ET_HAVE_PREAD
3636

37-
namespace torch {
38-
namespace executor {
39-
namespace util {
37+
using executorch::runtime::Error;
38+
using executorch::runtime::FreeableBuffer;
39+
using executorch::runtime::Result;
40+
41+
namespace executorch {
42+
namespace extension {
43+
4044
namespace {
4145

4246
/**
@@ -287,6 +291,5 @@ ET_NODISCARD Error FileDataLoader::load_into(
287291
return Error::Ok;
288292
}
289293

290-
} // namespace util
291-
} // namespace executor
292-
} // namespace torch
294+
} // namespace extension
295+
} // namespace executorch

0 commit comments

Comments
 (0)