From 48e2d57494d2934eb24613bb4d131d56a8c0a9dc Mon Sep 17 00:00:00 2001 From: Dave Bort Date: Wed, 21 Aug 2024 11:32:42 -0700 Subject: [PATCH] [executorch] Migrate most of extension/... to new namespace Pull Request resolved: https://github.com/pytorch/executorch/pull/4617 Migrate these headers to the new `::executorch::extension` namespace. Add temporary aliases from the old `::torch::executor` namespace so we can migrate users incrementally. ghstack-source-id: 239152036 @exported-using-ghexport Differential Revision: [D60938936](https://our.internmc.facebook.com/intern/diff/D60938936/) --- extension/aten_util/aten_bridge.cpp | 22 ++-- extension/aten_util/aten_bridge.h | 42 +++++++- .../make_aten_functor_from_et_functor.h | 61 +++++++---- extension/aten_util/test/aten_bridge_test.cpp | 2 +- ...make_aten_functor_from_et_functor_test.cpp | 10 +- extension/data_loader/buffer_data_loader.h | 28 +++-- extension/data_loader/file_data_loader.cpp | 15 +-- extension/data_loader/file_data_loader.h | 27 +++-- extension/data_loader/mmap_data_loader.cpp | 14 +-- extension/data_loader/mmap_data_loader.h | 29 +++-- .../data_loader/shared_ptr_data_loader.h | 23 ++-- .../test/buffer_data_loader_test.cpp | 12 +-- .../test/file_data_loader_test.cpp | 14 +-- .../test/mmap_data_loader_test.cpp | 14 +-- .../test/shared_ptr_data_loader_test.cpp | 12 +-- .../make_boxed_from_unboxed_functor.h | 74 ++++++++----- extension/kernel_util/meta_programming.h | 18 ++-- .../make_boxed_from_unboxed_functor_test.cpp | 33 +++--- extension/kernel_util/type_list.h | 13 ++- .../malloc_memory_allocator.h | 17 ++- .../test/malloc_memory_allocator_test.cpp | 7 +- extension/module/module.cpp | 52 ++++++--- extension/module/module.h | 102 +++++++++++------- .../parallel/test/thread_parallel_test.cpp | 5 +- extension/parallel/thread_parallel.cpp | 6 +- extension/parallel/thread_parallel.h | 16 ++- extension/pybindings/pybindings.cpp | 91 ++++++++++------ extension/pytree/aten_util/ivalue_util.cpp | 12 +-- extension/pytree/aten_util/ivalue_util.h | 24 +++-- .../aten_util/test/ivalue_util_test.cpp | 12 +-- extension/pytree/function_ref.h | 20 +++- extension/pytree/pybindings.cpp | 8 +- extension/pytree/pytree.h | 16 ++- extension/pytree/test/TARGETS | 4 +- extension/pytree/test/function_ref_test.cpp | 12 +-- extension/pytree/test/test_pytree.cpp | 20 ++-- extension/runner_util/inputs.cpp | 17 +-- extension/runner_util/inputs.h | 31 ++++-- extension/runner_util/inputs_aten.cpp | 14 +-- extension/runner_util/inputs_portable.cpp | 16 +-- extension/runner_util/managed_tensor.h | 45 ++++---- extension/runner_util/test/inputs_test.cpp | 27 +++-- .../runner_util/test/managed_tensor_test.cpp | 6 +- extension/testing_util/temp_file.h | 16 ++- .../testing_util/test/temp_file_test.cpp | 2 +- extension/training/optimizer/sgd.cpp | 17 ++- extension/training/optimizer/sgd.h | 46 ++++---- .../training/optimizer/test/sgd_test.cpp | 10 +- runtime/executor/test/executor_test.cpp | 10 +- 49 files changed, 715 insertions(+), 429 deletions(-) diff --git a/extension/aten_util/aten_bridge.cpp b/extension/aten_util/aten_bridge.cpp index 3916f7ed42..362dc57c37 100644 --- a/extension/aten_util/aten_bridge.cpp +++ b/extension/aten_util/aten_bridge.cpp @@ -11,8 +11,8 @@ #include #include -namespace torch { -namespace util { +namespace executorch { +namespace extension { namespace { void check_tensor_meta(const at::Tensor& a, const exec_aten::Tensor& b) { @@ -55,14 +55,15 @@ ET_CHECK_MSG( } // check dtype ET_CHECK_MSG( - b.scalar_type() == torchToExecuTorchScalarType(a.options().dtype()), + b.scalar_type() == torch_to_executorch_scalar_type(a.options().dtype()), "dtypes dont match a %hhd vs. b %hhd", - torchToExecuTorchScalarType(a.options().dtype()), + torch_to_executorch_scalar_type(a.options().dtype()), b.scalar_type()); } } // namespace -torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type) { +torch::executor::ScalarType torch_to_executorch_scalar_type( + caffe2::TypeMeta type) { switch (c10::typeMetaToScalarType(type)) { case c10::ScalarType::Byte: return torch::executor::ScalarType::Byte; @@ -91,7 +92,8 @@ torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type) { } } -c10::ScalarType execuTorchtoTorchScalarType(torch::executor::ScalarType type) { +c10::ScalarType executorch_to_torch_scalar_type( + torch::executor::ScalarType type) { switch (type) { case torch::executor::ScalarType::Byte: return c10::ScalarType::Byte; @@ -147,7 +149,8 @@ void alias_etensor_to_attensor( } at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& etensor) { - c10::ScalarType dtype = execuTorchtoTorchScalarType(etensor.scalar_type()); + c10::ScalarType dtype = + executorch_to_torch_scalar_type(etensor.scalar_type()); std::vector at_tensor_sizes( etensor.sizes().begin(), etensor.sizes().end()); std::vector at_tensor_strides( @@ -162,5 +165,6 @@ at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& etensor) { check_tensor_meta(t, etensor); return t; } -} // namespace util -} // namespace torch + +} // namespace extension +} // namespace executorch diff --git a/extension/aten_util/aten_bridge.h b/extension/aten_util/aten_bridge.h index a01d9bc26c..0d6b697463 100644 --- a/extension/aten_util/aten_bridge.h +++ b/extension/aten_util/aten_bridge.h @@ -18,12 +18,14 @@ #include #include -namespace torch { -namespace util { +namespace executorch { +namespace extension { -torch::executor::ScalarType torchToExecuTorchScalarType(caffe2::TypeMeta type); +torch::executor::ScalarType torch_to_executorch_scalar_type( + caffe2::TypeMeta type); -c10::ScalarType execuTorchtoTorchScalarType(torch::executor::ScalarType type); +c10::ScalarType executorch_to_torch_scalar_type( + torch::executor::ScalarType type); /* * @param[in] aten_tensor Input at::Tensor @@ -45,5 +47,37 @@ void alias_etensor_to_attensor(at::Tensor& at, torch::executor::Tensor& et); * cloned. */ at::Tensor alias_attensor_to_etensor(const torch::executor::Tensor& et); + +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::alias_attensor_to_etensor; +using ::executorch::extension::alias_etensor_to_attensor; +inline torch::executor::ScalarType torchToExecuTorchScalarType( + caffe2::TypeMeta type) { + return ::executorch::extension::torch_to_executorch_scalar_type(type); +} +inline c10::ScalarType execuTorchtoTorchScalarType( + torch::executor::ScalarType type) { + return ::executorch::extension::executorch_to_torch_scalar_type(type); +} +} // namespace util +} // namespace executor +} // namespace torch + +// Some users refer to these as `torch::util::`. +namespace torch { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::torch::executor::util::alias_attensor_to_etensor; +using ::torch::executor::util::alias_etensor_to_attensor; +using ::torch::executor::util::execuTorchtoTorchScalarType; +using ::torch::executor::util::torchToExecuTorchScalarType; } // namespace util } // namespace torch diff --git a/extension/aten_util/make_aten_functor_from_et_functor.h b/extension/aten_util/make_aten_functor_from_et_functor.h index 1e6ab069ef..3b54254e8e 100644 --- a/extension/aten_util/make_aten_functor_from_et_functor.h +++ b/extension/aten_util/make_aten_functor_from_et_functor.h @@ -24,8 +24,9 @@ #include #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { +namespace internal { // Map types from ETen to ATen. // This is used to convert ETen arguments into ATen. @@ -105,17 +106,22 @@ struct type_convert< torch::executor::Tensor>>> final { explicit type_convert(ATensor value) : value_(value) { - auto sizes = std::make_shared>( - value_.sizes().begin(), value_.sizes().end()); + auto sizes = + std::make_shared>( + value_.sizes().begin(), value_.sizes().end()); const ssize_t dim = sizes->size(); - auto dim_order = std::make_shared>(dim); - auto strides = std::make_shared>(dim); + auto dim_order = + std::make_shared>( + dim); + auto strides = + std::make_shared>( + dim); std::iota(dim_order->begin(), dim_order->end(), 0); - dim_order_to_stride_nocheck( + ::executorch::runtime::dim_order_to_stride_nocheck( sizes->data(), dim_order->data(), dim, strides->data()); - auto tensor_impl = std::make_shared( + auto tensor_impl = std::make_shared( static_cast(value_.scalar_type()), sizes->size(), sizes->data(), @@ -123,11 +129,12 @@ struct type_convert< dim_order->data(), strides->data()); - converted_ = std::unique_ptr>( - new Tensor(tensor_impl.get()), - [sizes, dim_order, strides, tensor_impl](Tensor* pointer) { - delete pointer; - }); + converted_ = std::unique_ptr< + torch::executor::Tensor, + std::function>( + new torch::executor::Tensor(tensor_impl.get()), + [sizes, dim_order, strides, tensor_impl]( + torch::executor::Tensor* pointer) { delete pointer; }); } ETensor call() { @@ -136,7 +143,10 @@ struct type_convert< private: ATensor value_; - std::unique_ptr> converted_; + std::unique_ptr< + torch::executor::Tensor, + std::function> + converted_; }; // Tensors: ETen to ATen. @@ -258,7 +268,12 @@ struct wrapper_impl { using TupleArgsType = std::tuple::type...>; static constexpr size_t num_args = sizeof...(Args); static_assert( - (N < num_args && std::is_same_v>, R>) || + (N < num_args && + std::is_same_v< + executorch::extension::kernel_util_internal::element_t< + N, + executorch::extension::kernel_util_internal::typelist>, + R>) || N == -1, "The index of the out tensor can't be greater or equal to num_args and " "the Nth argument type has to be the same as the return type."); @@ -298,16 +313,18 @@ struct wrapper_impl { } }; -} // namespace executor -} // namespace torch +} // namespace internal +} // namespace extension +} // namespace executorch // Wrapper macro for out variant function. N is the index of the out tensor. // We need N to know how to preserve the semantics of modifying out tensor and // return the reference without allocating a new memory buffer for out tensor. -#define _WRAP_2(func, N) \ - ::torch::executor::wrapper_impl::wrap +#define _WRAP_2(func, N) \ + ::executorch::extension::internal:: \ + wrapper_impl::wrap #define _WRAP_1(func) \ - ::torch::executor::wrapper_impl::wrap + ::executorch::extension::internal::wrapper_impl::wrap -#define GET_MACRO(_1, _2, NAME, ...) NAME -#define WRAP_TO_ATEN(...) GET_MACRO(__VA_ARGS__, _WRAP_2, _WRAP_1)(__VA_ARGS__) +#define _GET_MACRO(_1, _2, NAME, ...) NAME +#define WRAP_TO_ATEN(...) _GET_MACRO(__VA_ARGS__, _WRAP_2, _WRAP_1)(__VA_ARGS__) diff --git a/extension/aten_util/test/aten_bridge_test.cpp b/extension/aten_util/test/aten_bridge_test.cpp index 5f52a06309..cf6d2b8597 100644 --- a/extension/aten_util/test/aten_bridge_test.cpp +++ b/extension/aten_util/test/aten_bridge_test.cpp @@ -16,8 +16,8 @@ #include using namespace ::testing; -using namespace torch::util; using namespace torch::executor; +using namespace torch::executor::util; namespace { at::Tensor generate_at_tensor() { diff --git a/extension/aten_util/test/make_aten_functor_from_et_functor_test.cpp b/extension/aten_util/test/make_aten_functor_from_et_functor_test.cpp index db99d9b49d..26fe845a9e 100644 --- a/extension/aten_util/test/make_aten_functor_from_et_functor_test.cpp +++ b/extension/aten_util/test/make_aten_functor_from_et_functor_test.cpp @@ -14,10 +14,11 @@ #include #include -namespace torch { -namespace executor { - using namespace ::testing; +using ::executorch::extension::internal::type_convert; +using ::executorch::extension::internal::type_map; +using ::torch::executor::ScalarType; +using ::torch::executor::Tensor; Tensor& my_op_out(const Tensor& a, Tensor& out) { (void)a; @@ -420,6 +421,3 @@ TEST_F(MakeATenFunctorFromETFunctorTest, TestWrap_ArrayRefOptional) { EXPECT_EQ(stack.size(), 1); EXPECT_EQ(stack[0].toTensor().const_data_ptr()[0], 4); } - -} // namespace executor -} // namespace torch diff --git a/extension/data_loader/buffer_data_loader.h b/extension/data_loader/buffer_data_loader.h index 17ca36386d..ee25d86526 100644 --- a/extension/data_loader/buffer_data_loader.h +++ b/extension/data_loader/buffer_data_loader.h @@ -14,9 +14,8 @@ #include #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * A DataLoader that wraps a pre-allocated buffer. The FreeableBuffers @@ -25,12 +24,13 @@ namespace util { * This can be used to wrap data that is directly embedded into the firmware * image, or to wrap data that was allocated elsewhere. */ -class BufferDataLoader final : public DataLoader { +class BufferDataLoader final : public executorch::runtime::DataLoader { public: BufferDataLoader(const void* data, size_t size) : data_(reinterpret_cast(data)), size_(size) {} - ET_NODISCARD Result load( + ET_NODISCARD + executorch::runtime::Result load( size_t offset, size_t size, ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override { @@ -41,14 +41,15 @@ class BufferDataLoader final : public DataLoader { offset, size, size_); - return FreeableBuffer(data_ + offset, size, /*free_fn=*/nullptr); + return executorch::runtime::FreeableBuffer( + data_ + offset, size, /*free_fn=*/nullptr); } - ET_NODISCARD Result size() const override { + ET_NODISCARD executorch::runtime::Result size() const override { return size_; } - ET_NODISCARD Error load_into( + ET_NODISCARD executorch::runtime::Error load_into( size_t offset, size_t size, ET_UNUSED const SegmentInfo& segment_info, @@ -63,7 +64,7 @@ class BufferDataLoader final : public DataLoader { return result.error(); } std::memcpy(buffer, result->data(), size); - return Error::Ok; + return executorch::runtime::Error::Ok; } private: @@ -71,6 +72,15 @@ class BufferDataLoader final : public DataLoader { const size_t size_; }; +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::BufferDataLoader; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/data_loader/file_data_loader.cpp b/extension/data_loader/file_data_loader.cpp index b58edaa2de..1d097cfd98 100644 --- a/extension/data_loader/file_data_loader.cpp +++ b/extension/data_loader/file_data_loader.cpp @@ -34,9 +34,13 @@ #define ET_HAVE_PREAD 1 #endif // !ET_HAVE_PREAD -namespace torch { -namespace executor { -namespace util { +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; + +namespace executorch { +namespace extension { + namespace { /** @@ -287,6 +291,5 @@ ET_NODISCARD Error FileDataLoader::load_into( return Error::Ok; } -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/data_loader/file_data_loader.h b/extension/data_loader/file_data_loader.h index 12e0fcae49..7cf2a92c4a 100644 --- a/extension/data_loader/file_data_loader.h +++ b/extension/data_loader/file_data_loader.h @@ -14,9 +14,8 @@ #include #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * A DataLoader that loads segments from a file, allocating the memory @@ -25,7 +24,7 @@ namespace util { * Note that this will keep the file open for the duration of its lifetime, to * avoid the overhead of opening it again for every load() call. */ -class FileDataLoader final : public DataLoader { +class FileDataLoader final : public executorch::runtime::DataLoader { public: /** * Creates a new FileDataLoader that wraps the named file. @@ -40,12 +39,12 @@ class FileDataLoader final : public DataLoader { * could not be found. * @retval Error::MemoryAllocationFailed Internal memory allocation failure. */ - static Result from( + static executorch::runtime::Result from( const char* file_name, size_t alignment = alignof(std::max_align_t)); /// DEPRECATED: Use the lowercase `from()` instead. - ET_DEPRECATED static Result From( + ET_DEPRECATED static executorch::runtime::Result From( const char* file_name, size_t alignment = alignof(std::max_align_t)) { return from(file_name, alignment); @@ -65,14 +64,15 @@ class FileDataLoader final : public DataLoader { ~FileDataLoader() override; - ET_NODISCARD Result load( + ET_NODISCARD + executorch::runtime::Result load( size_t offset, size_t size, const DataLoader::SegmentInfo& segment_info) const override; - ET_NODISCARD Result size() const override; + ET_NODISCARD executorch::runtime::Result size() const override; - ET_NODISCARD Error load_into( + ET_NODISCARD executorch::runtime::Error load_into( size_t offset, size_t size, ET_UNUSED const SegmentInfo& segment_info, @@ -100,6 +100,15 @@ class FileDataLoader final : public DataLoader { const int fd_; // Owned by the instance. }; +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::FileDataLoader; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/data_loader/mmap_data_loader.cpp b/extension/data_loader/mmap_data_loader.cpp index ce523f6e9b..ebe74f9526 100644 --- a/extension/data_loader/mmap_data_loader.cpp +++ b/extension/data_loader/mmap_data_loader.cpp @@ -22,9 +22,12 @@ #include #include -namespace torch { -namespace executor { -namespace util { +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; + +namespace executorch { +namespace extension { namespace { @@ -254,6 +257,5 @@ Result MmapDataLoader::size() const { return file_size_; } -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/data_loader/mmap_data_loader.h b/extension/data_loader/mmap_data_loader.h index 04a2514c77..c55f81a490 100644 --- a/extension/data_loader/mmap_data_loader.h +++ b/extension/data_loader/mmap_data_loader.h @@ -12,9 +12,8 @@ #include #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * A DataLoader that loads segments from a file, allocating the memory @@ -23,7 +22,7 @@ namespace util { * Note that this will keep the file open for the duration of its lifetime, to * avoid the overhead of opening it again for every load() call. */ -class MmapDataLoader final : public DataLoader { +class MmapDataLoader final : public executorch::runtime::DataLoader { public: /** * Describes how and whether to lock loaded pages with `mlock()`. @@ -51,12 +50,12 @@ class MmapDataLoader final : public DataLoader { * @param[in] mlock_config How and whether to lock loaded pages with * `mlock()`. */ - static Result from( + static executorch::runtime::Result from( const char* file_name, MlockConfig mlock_config = MlockConfig::UseMlock); /// DEPRECATED: Use the lowercase `from()` instead. - ET_DEPRECATED static Result From( + ET_DEPRECATED static executorch::runtime::Result From( const char* file_name, MlockConfig mlock_config = MlockConfig::UseMlock) { return from(file_name, mlock_config); @@ -64,7 +63,9 @@ class MmapDataLoader final : public DataLoader { /// DEPRECATED: Use the version of `from()` that takes an MlockConfig. ET_DEPRECATED - static Result From(const char* file_name, bool use_mlock) { + static executorch::runtime::Result From( + const char* file_name, + bool use_mlock) { MlockConfig mlock_config = use_mlock ? MlockConfig::UseMlock : MlockConfig::NoMlock; return from(file_name, mlock_config); @@ -86,12 +87,13 @@ class MmapDataLoader final : public DataLoader { ~MmapDataLoader() override; - ET_NODISCARD Result load( + ET_NODISCARD + executorch::runtime::Result load( size_t offset, size_t size, const DataLoader::SegmentInfo& segment_info) const override; - ET_NODISCARD Result size() const override; + ET_NODISCARD executorch::runtime::Result size() const override; private: MmapDataLoader( @@ -118,6 +120,15 @@ class MmapDataLoader final : public DataLoader { const MlockConfig mlock_config_; }; +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::MmapDataLoader; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/data_loader/shared_ptr_data_loader.h b/extension/data_loader/shared_ptr_data_loader.h index 79a329084f..551ab4d498 100644 --- a/extension/data_loader/shared_ptr_data_loader.h +++ b/extension/data_loader/shared_ptr_data_loader.h @@ -14,9 +14,8 @@ #include #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * A DataLoader that wraps a pre-allocated buffer and shares ownership to it. @@ -24,12 +23,13 @@ namespace util { * * This can be used to wrap data that was allocated elsewhere. */ -class SharedPtrDataLoader final : public DataLoader { +class SharedPtrDataLoader final : public executorch::runtime::DataLoader { public: SharedPtrDataLoader(std::shared_ptr data, size_t size) : data_(data), size_(size) {} - ET_NODISCARD Result load( + ET_NODISCARD + executorch::runtime::Result load( size_t offset, size_t size, ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override { @@ -40,11 +40,11 @@ class SharedPtrDataLoader final : public DataLoader { offset, size, size_); - return FreeableBuffer( + return executorch::runtime::FreeableBuffer( static_cast(data_.get()) + offset, size, /*free_fn=*/nullptr); } - ET_NODISCARD Result size() const override { + ET_NODISCARD executorch::runtime::Result size() const override { return size_; } @@ -53,6 +53,15 @@ class SharedPtrDataLoader final : public DataLoader { const size_t size_; }; +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::SharedPtrDataLoader; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/data_loader/test/buffer_data_loader_test.cpp b/extension/data_loader/test/buffer_data_loader_test.cpp index e5facfc3ba..83d053ee46 100644 --- a/extension/data_loader/test/buffer_data_loader_test.cpp +++ b/extension/data_loader/test/buffer_data_loader_test.cpp @@ -16,18 +16,18 @@ #include using namespace ::testing; -using torch::executor::DataLoader; -using torch::executor::Error; -using torch::executor::FreeableBuffer; -using torch::executor::Result; -using torch::executor::util::BufferDataLoader; +using executorch::extension::BufferDataLoader; +using executorch::runtime::DataLoader; +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; class BufferDataLoaderTest : public ::testing::Test { protected: void SetUp() override { // Since these tests cause ET_LOG to be called, the PAL must be initialized // first. - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } }; diff --git a/extension/data_loader/test/file_data_loader_test.cpp b/extension/data_loader/test/file_data_loader_test.cpp index f7081565fd..1d4f4c1619 100644 --- a/extension/data_loader/test/file_data_loader_test.cpp +++ b/extension/data_loader/test/file_data_loader_test.cpp @@ -18,19 +18,19 @@ #include using namespace ::testing; -using torch::executor::DataLoader; -using torch::executor::Error; -using torch::executor::FreeableBuffer; -using torch::executor::Result; -using torch::executor::testing::TempFile; -using torch::executor::util::FileDataLoader; +using executorch::extension::FileDataLoader; +using executorch::extension::testing::TempFile; +using executorch::runtime::DataLoader; +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; class FileDataLoaderTest : public ::testing::TestWithParam { protected: void SetUp() override { // Since these tests cause ET_LOG to be called, the PAL must be initialized // first. - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } // The alignment in bytes that tests should use. The values are set by the diff --git a/extension/data_loader/test/mmap_data_loader_test.cpp b/extension/data_loader/test/mmap_data_loader_test.cpp index b6781bc448..a76121109a 100644 --- a/extension/data_loader/test/mmap_data_loader_test.cpp +++ b/extension/data_loader/test/mmap_data_loader_test.cpp @@ -19,19 +19,19 @@ #include using namespace ::testing; -using torch::executor::DataLoader; -using torch::executor::Error; -using torch::executor::FreeableBuffer; -using torch::executor::Result; -using torch::executor::testing::TempFile; -using torch::executor::util::MmapDataLoader; +using executorch::extension::MmapDataLoader; +using executorch::extension::testing::TempFile; +using executorch::runtime::DataLoader; +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; class MmapDataLoaderTest : public ::testing::Test { protected: void SetUp() override { // Since these tests cause ET_LOG to be called, the PAL must be initialized // first. - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); // Get the page size and ensure it's a power of 2. long page_size = sysconf(_SC_PAGESIZE); diff --git a/extension/data_loader/test/shared_ptr_data_loader_test.cpp b/extension/data_loader/test/shared_ptr_data_loader_test.cpp index b4fc153cab..62d71ae056 100644 --- a/extension/data_loader/test/shared_ptr_data_loader_test.cpp +++ b/extension/data_loader/test/shared_ptr_data_loader_test.cpp @@ -17,18 +17,18 @@ #include using namespace ::testing; -using torch::executor::DataLoader; -using torch::executor::Error; -using torch::executor::FreeableBuffer; -using torch::executor::Result; -using torch::executor::util::SharedPtrDataLoader; +using executorch::extension::SharedPtrDataLoader; +using executorch::runtime::DataLoader; +using executorch::runtime::Error; +using executorch::runtime::FreeableBuffer; +using executorch::runtime::Result; class SharedPtrDataLoaderTest : public ::testing::Test { protected: void SetUp() override { // Since these tests cause ET_LOG to be called, the PAL must be initialized // first. - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } }; diff --git a/extension/kernel_util/make_boxed_from_unboxed_functor.h b/extension/kernel_util/make_boxed_from_unboxed_functor.h index 0202b4f51c..2b21914f49 100644 --- a/extension/kernel_util/make_boxed_from_unboxed_functor.h +++ b/extension/kernel_util/make_boxed_from_unboxed_functor.h @@ -54,10 +54,13 @@ class KernelRuntimeContext; // Forward declaration } // namespace runtime } // namespace executorch -namespace torch { -namespace executor { +namespace executorch { +namespace extension { + +// This extension has a lot of generic internal names like "size"; use a unique +// internal namespace to avoid conflicts with other extensions. +namespace kernel_util_internal { -// evalue_to_arg template struct decay_if_not_tensor final { using type = std::decay_t; @@ -73,45 +76,44 @@ struct decay_if_not_tensor final { template struct evalue_to_arg final { - static T call(EValue& v) { + static T call(executorch::runtime::EValue& v) { return std::move(v).to(); } }; template <> struct evalue_to_arg final { - static exec_aten::Tensor& call(EValue& v) { + static exec_aten::Tensor& call(executorch::runtime::EValue& v) { return v.toTensor(); } }; template <> struct evalue_to_arg final { - static const exec_aten::Tensor& call(EValue& v) { + static const exec_aten::Tensor& call(executorch::runtime::EValue& v) { return v.toTensor(); } }; template struct evalue_to_arg> final { - static exec_aten::optional call(EValue& v) { + static exec_aten::optional call(executorch::runtime::EValue& v) { return v.toOptional(); } }; template struct evalue_to_arg>> final { - static exec_aten::ArrayRef> call(EValue& v) { + static exec_aten::ArrayRef> call( + executorch::runtime::EValue& v) { return v.toListOptionalTensor(); } }; -// Call functor with args from stack - template -void call_functor_with_args_from_stack_( +void call_functor_with_args_from_stack( ::executorch::runtime::KernelRuntimeContext& ctx, - EValue** stack, + executorch::runtime::EValue** stack, std::index_sequence, typelist*) { (*Functor::func_ptr())( @@ -120,6 +122,8 @@ void call_functor_with_args_from_stack_( *stack[evalue_arg_indices])...); } +} // namespace kernel_util_internal + /** * WrapUnboxedIntoFunctor: Given a function pointer, wrap it into a functor that * takes EValues as input and returns void. The wrapped functor will unbox all @@ -128,25 +132,29 @@ void call_functor_with_args_from_stack_( template struct WrapUnboxedIntoFunctor { static_assert( - is_compile_time_function_pointer::value, + kernel_util_internal::is_compile_time_function_pointer::value, "Can't handle function other than EXECUTORCH_FN"); using TrueType = typename FuncType::FuncType; - using ReturnType = typename infer_function_traits_t::return_type; - using ArgsType = typename infer_function_traits_t::parameter_types; + using ReturnType = typename kernel_util_internal::infer_function_traits_t< + TrueType>::return_type; + using ArgsType = typename kernel_util_internal::infer_function_traits_t< + TrueType>::parameter_types; // check if the first argument is KernelRuntimeContext, if so, remove it static constexpr bool first_arg_is_context = std::is_same< ::executorch::runtime::KernelRuntimeContext, - std::remove_reference_t>>::value; + std::remove_reference_t< + kernel_util_internal::head_with_default_t>>::value; using ContextRemovedArgsType = std::conditional_t< first_arg_is_context, - drop_if_nonempty_t, + kernel_util_internal::drop_if_nonempty_t, ArgsType>; static void call( ::executorch::runtime::KernelRuntimeContext& ctx, - EValue** stack) { - constexpr size_t num_inputs = size::value; - return call_functor_with_args_from_stack_( + executorch::runtime::EValue** stack) { + constexpr size_t num_inputs = + kernel_util_internal::size::value; + return kernel_util_internal::call_functor_with_args_from_stack( ctx, stack, std::make_index_sequence(), @@ -155,14 +163,26 @@ struct WrapUnboxedIntoFunctor { }; template -static Kernel make_boxed_kernel(const char* name, FuncType) { - return Kernel(name, WrapUnboxedIntoFunctor::call); +static executorch::runtime::Kernel make_boxed_kernel( + const char* name, + FuncType) { + return executorch::runtime::Kernel( + name, WrapUnboxedIntoFunctor::call); } -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch -#define EXECUTORCH_LIBRARY(ns, op_name, func) \ - static auto res_##ns = ::torch::executor::register_kernels( \ - ::torch::executor::make_boxed_kernel( \ +#define EXECUTORCH_LIBRARY(ns, op_name, func) \ + static auto res_##ns = ::executorch::runtime::register_kernels( \ + ::executorch::extension::make_boxed_kernel( \ #ns "::" op_name, EXECUTORCH_FN(func))) + +namespace torch { +namespace executor { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::make_boxed_kernel; +using ::executorch::extension::WrapUnboxedIntoFunctor; +} // namespace executor +} // namespace torch diff --git a/extension/kernel_util/meta_programming.h b/extension/kernel_util/meta_programming.h index c412e907ea..027568fe68 100644 --- a/extension/kernel_util/meta_programming.h +++ b/extension/kernel_util/meta_programming.h @@ -17,8 +17,11 @@ #include #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { +// This extension has a lot of generic internal names like "size"; use a unique +// internal namespace to avoid conflicts with other extensions. +namespace kernel_util_internal { // Check if a given type is a function template @@ -48,9 +51,9 @@ template struct is_compile_time_function_pointer< CompileTimeFunctionPointer> : std::true_type {}; -#define EXECUTORCH_FN_TYPE(func) \ - ::torch::executor::CompileTimeFunctionPointer< \ - std::remove_pointer_t>, \ +#define EXECUTORCH_FN_TYPE(func) \ + ::executorch::extension::kernel_util_internal::CompileTimeFunctionPointer< \ + std::remove_pointer_t>, \ func> #define EXECUTORCH_FN(func) EXECUTORCH_FN_TYPE(func)() @@ -111,5 +114,6 @@ struct infer_function_traits { template using infer_function_traits_t = typename infer_function_traits::type; -} // namespace executor -} // namespace torch +} // namespace kernel_util_internal +} // namespace extension +} // namespace executorch diff --git a/extension/kernel_util/test/make_boxed_from_unboxed_functor_test.cpp b/extension/kernel_util/test/make_boxed_from_unboxed_functor_test.cpp index b75a8f160a..da9596def7 100644 --- a/extension/kernel_util/test/make_boxed_from_unboxed_functor_test.cpp +++ b/extension/kernel_util/test/make_boxed_from_unboxed_functor_test.cpp @@ -15,22 +15,31 @@ #include using namespace ::testing; -using RuntimeContext = torch::executor::KernelRuntimeContext; -using namespace torch::executor; - -Tensor& my_op_out(RuntimeContext& ctx, const Tensor& a, Tensor& out) { +using exec_aten::ArrayRef; +using exec_aten::optional; +using exec_aten::ScalarType; +using exec_aten::Tensor; +using exec_aten::TensorImpl; +using executorch::runtime::BoxedEvalueList; +using executorch::runtime::EValue; +using executorch::runtime::getOpsFn; +using executorch::runtime::hasOpsFn; +using executorch::runtime::KernelRuntimeContext; + +Tensor& my_op_out(KernelRuntimeContext& ctx, const Tensor& a, Tensor& out) { (void)ctx; (void)a; return out; } -Tensor& set_1_out(RuntimeContext& ctx, Tensor& out) { +Tensor& set_1_out(KernelRuntimeContext& ctx, Tensor& out) { (void)ctx; out.mutable_data_ptr()[0] = 1; return out; } -Tensor& add_tensor_out(RuntimeContext& ctx, ArrayRef a, Tensor& out) { +Tensor& +add_tensor_out(KernelRuntimeContext& ctx, ArrayRef a, Tensor& out) { (void)ctx; for (int i = 0; i < out.numel(); i++) { int sum = 0; @@ -43,7 +52,7 @@ Tensor& add_tensor_out(RuntimeContext& ctx, ArrayRef a, Tensor& out) { } Tensor& add_optional_scalar_out( - RuntimeContext& ctx, + KernelRuntimeContext& ctx, optional s1, optional s2, Tensor& out) { @@ -58,7 +67,7 @@ Tensor& add_optional_scalar_out( } Tensor& add_optional_tensor_out( - RuntimeContext& ctx, + KernelRuntimeContext& ctx, ArrayRef> a, Tensor& out) { (void)ctx; @@ -100,7 +109,7 @@ TEST_F(MakeBoxedFromUnboxedFunctorTest, UnboxLogicWorks) { auto fn = getOpsFn("my_ns::set_1.out"); // run it - RuntimeContext context; + KernelRuntimeContext context; EValue values[1]; values[0] = a; EValue* stack[1]; @@ -129,7 +138,7 @@ TEST_F(MakeBoxedFromUnboxedFunctorTest, UnboxArrayRef) { auto fn = getOpsFn("my_ns::add_tensor.out"); // run it. - RuntimeContext context; + KernelRuntimeContext context; EValue values[2] = {boxed_array_ref, out}; EValue* stack[2] = {&values[0], &values[1]}; fn(context, stack); @@ -154,7 +163,7 @@ TEST_F(MakeBoxedFromUnboxedFunctorTest, UnboxOptional) { auto fn = getOpsFn("my_ns::add_optional_scalar.out"); // run it. - RuntimeContext context; + KernelRuntimeContext context; EValue values[3] = {scalar, scalar_none, out}; EValue* stack[3] = {&values[0], &values[1], &values[2]}; fn(context, stack); @@ -180,7 +189,7 @@ TEST_F(MakeBoxedFromUnboxedFunctorTest, UnboxOptionalArrayRef) { auto fn = getOpsFn("my_ns::add_optional_tensor.out"); // run it. - RuntimeContext context; + KernelRuntimeContext context; EValue values[2] = {boxed_array_ref, out}; EValue* stack[2] = {&values[0], &values[1]}; fn(context, stack); diff --git a/extension/kernel_util/type_list.h b/extension/kernel_util/type_list.h index f832ab9f26..300cbfcb7c 100644 --- a/extension/kernel_util/type_list.h +++ b/extension/kernel_util/type_list.h @@ -20,8 +20,12 @@ #include #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { +// This extension has a lot of generic internal names like "size"; use a unique +// internal namespace to avoid conflicts with other extensions. +namespace kernel_util_internal { + /** * Type holding a list of types for compile time type computations * constexpr size_t num = size>::value; @@ -139,5 +143,6 @@ struct drop_if_nonempty final { template using drop_if_nonempty_t = typename drop_if_nonempty::type; -} // namespace executor -} // namespace torch +} // namespace kernel_util_internal +} // namespace extension +} // namespace executorch diff --git a/extension/memory_allocator/malloc_memory_allocator.h b/extension/memory_allocator/malloc_memory_allocator.h index 6625f587aa..7e1cf8b2ab 100644 --- a/extension/memory_allocator/malloc_memory_allocator.h +++ b/extension/memory_allocator/malloc_memory_allocator.h @@ -14,9 +14,8 @@ #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * Dynamically allocates memory using malloc() and frees all pointers at @@ -25,7 +24,7 @@ namespace util { * For systems with malloc(), this can be easier than using a fixed-sized * MemoryAllocator. */ -class MallocMemoryAllocator : public MemoryAllocator { +class MallocMemoryAllocator : public executorch::runtime::MemoryAllocator { public: /** * Construct a new Malloc memory allocator via an optional alignment size @@ -76,6 +75,16 @@ class MallocMemoryAllocator : public MemoryAllocator { private: std::vector mem_ptrs_; }; + +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::MallocMemoryAllocator; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/memory_allocator/test/malloc_memory_allocator_test.cpp b/extension/memory_allocator/test/malloc_memory_allocator_test.cpp index 05dfafa206..fc2db04bc8 100644 --- a/extension/memory_allocator/test/malloc_memory_allocator_test.cpp +++ b/extension/memory_allocator/test/malloc_memory_allocator_test.cpp @@ -12,17 +12,16 @@ #include using namespace ::testing; -using torch::executor::util::MallocMemoryAllocator; +using executorch::extension::MallocMemoryAllocator; -constexpr auto kDefaultAlignment = - torch::executor::util::MallocMemoryAllocator::kDefaultAlignment; +constexpr auto kDefaultAlignment = MallocMemoryAllocator::kDefaultAlignment; class MallocMemoryAllocatorTest : public ::testing::Test { protected: void SetUp() override { // Since these tests cause ET_LOG to be called, the PAL must be initialized // first. - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); } }; diff --git a/extension/module/module.cpp b/extension/module/module.cpp index 6cee0185c7..e59d4b45db 100644 --- a/extension/module/module.cpp +++ b/extension/module/module.cpp @@ -33,7 +33,24 @@ std::move(*et_result__)); \ }) -namespace torch::executor { +using ::exec_aten::Tensor; +using ::executorch::extension::FileDataLoader; +using ::executorch::extension::MallocMemoryAllocator; +using ::executorch::extension::MmapDataLoader; +using ::executorch::runtime::DataLoader; +using ::executorch::runtime::Error; +using ::executorch::runtime::EValue; +using ::executorch::runtime::EventTracer; +using ::executorch::runtime::HierarchicalAllocator; +using ::executorch::runtime::MemoryAllocator; +using ::executorch::runtime::MemoryManager; +using ::executorch::runtime::MethodMeta; +using ::executorch::runtime::Program; +using ::executorch::runtime::Result; +using ::executorch::runtime::Span; + +namespace executorch { +namespace extension { Module::Module( const std::string& file_path, @@ -41,10 +58,10 @@ Module::Module( std::unique_ptr event_tracer) : file_path_(file_path), load_mode_(load_mode), - memory_allocator_(std::make_unique()), - temp_allocator_(std::make_unique()), + memory_allocator_(std::make_unique()), + temp_allocator_(std::make_unique()), event_tracer_(std::move(event_tracer)) { - runtime_init(); + ::executorch::runtime::runtime_init(); } Module::Module( @@ -55,12 +72,12 @@ Module::Module( : data_loader_(std::move(data_loader)), memory_allocator_( memory_allocator ? std::move(memory_allocator) - : std::make_unique()), + : std::make_unique()), temp_allocator_( temp_allocator ? std::move(temp_allocator) - : std::make_unique()), + : std::make_unique()), event_tracer_(std::move(event_tracer)) { - runtime_init(); + ::executorch::runtime::runtime_init(); } Module::Module( @@ -71,12 +88,12 @@ Module::Module( : program_(std::move(program)), memory_allocator_( memory_allocator ? std::move(memory_allocator) - : std::make_unique()), + : std::make_unique()), temp_allocator_( temp_allocator ? std::move(temp_allocator) - : std::make_unique()), + : std::make_unique()), event_tracer_(std::move(event_tracer)) { - runtime_init(); + ::executorch::runtime::runtime_init(); } Error Module::load(const Program::Verification verification) { @@ -85,20 +102,20 @@ Error Module::load(const Program::Verification verification) { switch (load_mode_) { case LoadMode::File: data_loader_ = - ET_UNWRAP_UNIQUE(util::FileDataLoader::from(file_path_.c_str())); + ET_UNWRAP_UNIQUE(FileDataLoader::from(file_path_.c_str())); break; case LoadMode::Mmap: - data_loader_ = ET_UNWRAP_UNIQUE(util::MmapDataLoader::from( - file_path_.c_str(), util::MmapDataLoader::MlockConfig::NoMlock)); + data_loader_ = ET_UNWRAP_UNIQUE(MmapDataLoader::from( + file_path_.c_str(), MmapDataLoader::MlockConfig::NoMlock)); break; case LoadMode::MmapUseMlock: data_loader_ = - ET_UNWRAP_UNIQUE(util::MmapDataLoader::from(file_path_.c_str())); + ET_UNWRAP_UNIQUE(MmapDataLoader::from(file_path_.c_str())); break; case LoadMode::MmapUseMlockIgnoreErrors: - data_loader_ = ET_UNWRAP_UNIQUE(util::MmapDataLoader::from( + data_loader_ = ET_UNWRAP_UNIQUE(MmapDataLoader::from( file_path_.c_str(), - util::MmapDataLoader::MlockConfig::UseMlockIgnoreErrors)); + MmapDataLoader::MlockConfig::UseMlockIgnoreErrors)); break; } }; @@ -199,4 +216,5 @@ Error Module::set_output_data_ptr(Tensor& output_tensor, size_t output_index) { output_tensor.mutable_data_ptr(), output_tensor.nbytes(), output_index); } -} // namespace torch::executor +} // namespace extension +} // namespace executorch diff --git a/extension/module/module.h b/extension/module/module.h index da09141659..a0b575d5bf 100644 --- a/extension/module/module.h +++ b/extension/module/module.h @@ -16,7 +16,8 @@ #include -namespace torch::executor { +namespace executorch { +namespace extension { /** * A facade class for loading programs and executing methods within them. @@ -47,7 +48,8 @@ class Module final { explicit Module( const std::string& file_path, const LoadMode load_mode = LoadMode::MmapUseMlock, - std::unique_ptr event_tracer = nullptr); + std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = + nullptr); /** * Constructs an instance with the provided data loader and memory allocator. @@ -59,10 +61,13 @@ class Module final { * @param[in] event_tracer A EventTracer used for tracking and logging events. */ explicit Module( - std::unique_ptr data_loader, - std::unique_ptr memory_allocator = nullptr, - std::unique_ptr temp_allocator = nullptr, - std::unique_ptr event_tracer = nullptr); + std::unique_ptr<::executorch::runtime::DataLoader> data_loader, + std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator = + nullptr, + std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator = + nullptr, + std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = + nullptr); /** * Constructs an instance using an existing shared program. @@ -75,10 +80,13 @@ class Module final { * @param[in] event_tracer A EventTracer used for tracking and logging events. */ explicit Module( - std::shared_ptr program, - std::unique_ptr memory_allocator = nullptr, - std::unique_ptr temp_allocator = nullptr, - std::unique_ptr event_tracer = nullptr); + std::shared_ptr<::executorch::runtime::Program> program, + std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator = + nullptr, + std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator = + nullptr, + std::unique_ptr<::executorch::runtime::EventTracer> event_tracer = + nullptr); Module(const Module&) = delete; Module& operator=(const Module&) = delete; @@ -94,9 +102,9 @@ class Module final { * @returns An Error to indicate success or failure of the loading process. */ ET_NODISCARD - Error load( - const Program::Verification verification = - Program::Verification::Minimal); + ::executorch::runtime::Error load( + const ::executorch::runtime::Program::Verification verification = + ::executorch::runtime::Program::Verification::Minimal); /** * Checks if the program is loaded. @@ -111,7 +119,7 @@ class Module final { * * @returns Shared pointer to the program or nullptr if it's not yet loaded. */ - std::shared_ptr program() const { + std::shared_ptr<::executorch::runtime::Program> program() const { return program_; } @@ -122,7 +130,7 @@ class Module final { * @returns A set of strings containing the names of the methods, or an error * if the program or method failed to load. */ - Result> method_names(); + ::executorch::runtime::Result> method_names(); /** * Load a specific method from the program and set up memory management if @@ -133,7 +141,7 @@ class Module final { * @returns An Error to indicate success or failure. */ ET_NODISCARD - Error load_method(const std::string& method_name); + ::executorch::runtime::Error load_method(const std::string& method_name); /** * Checks if a specific method is loaded. @@ -154,7 +162,8 @@ class Module final { * @returns A method metadata, or an error if the program or method failed to * load. */ - Result method_meta(const std::string& method_name); + ::executorch::runtime::Result<::executorch::runtime::MethodMeta> method_meta( + const std::string& method_name); /** * Execute a specific method with the given input and retrieve output. @@ -167,9 +176,10 @@ class Module final { * from the method or an error to indicate failure. */ ET_NODISCARD - Result> execute( + ::executorch::runtime::Result> + execute( const std::string& method_name, - const std::vector& input); + const std::vector<::executorch::runtime::EValue>& input); /** * Execute a specific method without any input values. @@ -181,7 +191,8 @@ class Module final { * from the method or an error to indicate failure. */ ET_NODISCARD - Result> execute(const std::string& method_name) { + ::executorch::runtime::Result> + execute(const std::string& method_name) { return execute(method_name, {}); } @@ -196,12 +207,12 @@ class Module final { * method or an error to indicate failure. */ ET_NODISCARD - Result get( + ::executorch::runtime::Result<::executorch::runtime::EValue> get( const std::string& method_name, - const std::vector& input) { + const std::vector<::executorch::runtime::EValue>& input) { auto result = ET_UNWRAP(execute(method_name, input)); if (result.empty()) { - return Error::InvalidArgument; + return ::executorch::runtime::Error::InvalidArgument; } return result[0]; } @@ -216,7 +227,8 @@ class Module final { * method or an error to indicate failure. */ ET_NODISCARD - Result get(const std::string& method_name) { + ::executorch::runtime::Result<::executorch::runtime::EValue> get( + const std::string& method_name) { return get(method_name, {}); } @@ -230,7 +242,8 @@ class Module final { * from the 'forward' method or an error to indicate failure. */ ET_NODISCARD - Result> forward(const std::vector& input) { + ::executorch::runtime::Result> + forward(const std::vector<::executorch::runtime::EValue>& input) { return execute("forward", input); } @@ -242,7 +255,8 @@ class Module final { * from the 'forward' method or an error to indicate failure. */ ET_NODISCARD - Result> forward() { + ::executorch::runtime::Result> + forward() { return forward({}); } @@ -254,7 +268,7 @@ class Module final { * @returns A pointer to the EventTracer instance. Returns nullptr if no * EventTracer is set. */ - EventTracer* event_tracer() const { + ::executorch::runtime::EventTracer* event_tracer() const { return event_tracer_.get(); } @@ -266,26 +280,38 @@ class Module final { * * @returns An Error to indicate success or failure of the loading process. */ - Error set_output_data_ptr(Tensor& output_tensor, size_t output_index); + ::executorch::runtime::Error set_output_data_ptr( + exec_aten::Tensor& output_tensor, + size_t output_index); private: struct MethodHolder { std::vector> planned_buffers; - std::vector> planned_spans; - std::unique_ptr planned_memory; - std::unique_ptr memory_manager; - std::unique_ptr method; + std::vector<::executorch::runtime::Span> planned_spans; + std::unique_ptr<::executorch::runtime::HierarchicalAllocator> + planned_memory; + std::unique_ptr<::executorch::runtime::MemoryManager> memory_manager; + std::unique_ptr<::executorch::runtime::Method> method; }; private: std::string file_path_; LoadMode load_mode_{LoadMode::MmapUseMlock}; - std::shared_ptr program_; - std::unique_ptr data_loader_; - std::unique_ptr memory_allocator_; - std::unique_ptr temp_allocator_; - std::unique_ptr event_tracer_; + std::shared_ptr<::executorch::runtime::Program> program_; + std::unique_ptr<::executorch::runtime::DataLoader> data_loader_; + std::unique_ptr<::executorch::runtime::MemoryAllocator> memory_allocator_; + std::unique_ptr<::executorch::runtime::MemoryAllocator> temp_allocator_; + std::unique_ptr<::executorch::runtime::EventTracer> event_tracer_; std::unordered_map methods_; }; -} // namespace torch::executor +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::Module; +} // namespace executor +} // namespace torch diff --git a/extension/parallel/test/thread_parallel_test.cpp b/extension/parallel/test/thread_parallel_test.cpp index 1eea87beb0..d386429100 100644 --- a/extension/parallel/test/thread_parallel_test.cpp +++ b/extension/parallel/test/thread_parallel_test.cpp @@ -15,8 +15,7 @@ #include using namespace ::testing; - -namespace torch::executor { +using ::executorch::extension::parallel_for; class ParallelTest : public ::testing::Test { protected: @@ -192,5 +191,3 @@ TEST_F(ParallelTest, TestChunkSizeTooLarge) { EXPECT_EQ(data_[i], i); } } - -} // namespace torch::executor diff --git a/extension/parallel/thread_parallel.cpp b/extension/parallel/thread_parallel.cpp index cdd1d21a83..aac47cca2e 100644 --- a/extension/parallel/thread_parallel.cpp +++ b/extension/parallel/thread_parallel.cpp @@ -13,7 +13,8 @@ #include #include -namespace torch::executor { +namespace executorch { +namespace extension { namespace { thread_local int64_t thread_num_ = 0; @@ -74,4 +75,5 @@ bool parallel_for( return true; } -} // namespace torch::executor +} // namespace extension +} // namespace executorch diff --git a/extension/parallel/thread_parallel.h b/extension/parallel/thread_parallel.h index 7b58236e8a..bbce211597 100644 --- a/extension/parallel/thread_parallel.h +++ b/extension/parallel/thread_parallel.h @@ -12,7 +12,8 @@ // @nolint PATTERNLINT Ok to use stdlib for this optional library #include -namespace torch::executor { +namespace executorch { +namespace extension { /** * A helper to run function in parallel. @@ -39,4 +40,15 @@ int64_t get_thread_num(); void set_thread_num(int64_t thread_num); -} // namespace torch::executor +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::get_thread_num; +using ::executorch::extension::parallel_for; +using ::executorch::extension::set_thread_num; +} // namespace executor +} // namespace torch diff --git a/extension/pybindings/pybindings.cpp b/extension/pybindings/pybindings.cpp index 9a49e3878e..83cec280b8 100644 --- a/extension/pybindings/pybindings.cpp +++ b/extension/pybindings/pybindings.cpp @@ -72,8 +72,39 @@ void et_pal_emit_log_message( } namespace py = pybind11; -namespace torch { -namespace executor { +using ::executorch::extension::BufferDataLoader; +using ::executorch::extension::MallocMemoryAllocator; +using ::executorch::extension::MmapDataLoader; +using ::executorch::runtime::ArrayRef; +using ::executorch::runtime::DataLoader; +using ::executorch::runtime::Error; +using ::executorch::runtime::EValue; +using ::executorch::runtime::EventTracerDebugLogLevel; +using ::executorch::runtime::get_kernels; +using ::executorch::runtime::HierarchicalAllocator; +using ::executorch::runtime::Kernel; +using ::executorch::runtime::MemoryAllocator; +using ::executorch::runtime::MemoryManager; +using ::executorch::runtime::Method; +using ::executorch::runtime::prof_result_t; +using ::executorch::runtime::Program; +using ::executorch::runtime::Result; +using ::executorch::runtime::Span; +using ::executorch::runtime::Tag; +using torch::executor::etdump_result; +using torch::executor::ETDumpGen; +using torch::executor::bundled_program::LoadBundledInput; +using torch::executor::bundled_program::VerifyResultWithBundledExpectedOutput; + +#ifndef USE_ATEN_LIB +using ::executorch::extension::alias_attensor_to_etensor; +using ::executorch::extension::alias_etensor_to_attensor; +using ::executorch::extension::torch_to_executorch_scalar_type; +#endif // !USE_ATEN_LIB + +namespace executorch { +namespace extension { +namespace pybindings { namespace { @@ -96,7 +127,7 @@ void write_data_to_file(const std::string& path, void* buf, size_t size) { } void setup_output_storage( - executor::Method& method, + Method& method, const std::vector>& output_storages) { if (output_storages.size() != method.outputs_size()) { THROW_IF_ERROR( @@ -123,10 +154,6 @@ void setup_output_storage( } } -using util::BufferDataLoader; -using util::MallocMemoryAllocator; -using util::MmapDataLoader; - class Module final { public: explicit Module( @@ -136,7 +163,7 @@ class Module final { : loader_(std::move(loader)), event_tracer_(std::move(tracer)), debug_buffer_size_(debug_buffer_size) { - runtime_init(); + ::executorch::runtime::runtime_init(); Result program = Program::load( loader_.get(), Program::Verification::InternalConsistency); THROW_IF_ERROR( @@ -346,12 +373,12 @@ class Module final { size_t debug_buffer_size_; }; -inline std::unique_ptr load_from_buffer( +inline std::unique_ptr load_module_from_buffer( const void* ptr, size_t ptr_len, bool enable_etdump, size_t debug_buffer_size) { - EXECUTORCH_SCOPE_PROF("load_from_buffer"); + EXECUTORCH_SCOPE_PROF("load_module_from_buffer"); auto loader = std::make_unique(ptr, ptr_len); return std::make_unique( std::move(loader), @@ -359,11 +386,11 @@ inline std::unique_ptr load_from_buffer( debug_buffer_size); } -inline std::unique_ptr load_from_file( +inline std::unique_ptr load_module_from_file( const std::string& path, bool enable_etdump, size_t debug_buffer_size) { - EXECUTORCH_SCOPE_PROF("load_from_file"); + EXECUTORCH_SCOPE_PROF("load_module_from_file"); Result res = MmapDataLoader::from( path.c_str(), MmapDataLoader::MlockConfig::UseMlockIgnoreErrors); @@ -428,7 +455,7 @@ struct PyModule final { const py::bytes& buffer, bool enable_etdump, size_t debug_buffer_size = 0) - : module_(torch::executor::load_from_buffer( + : module_(load_module_from_buffer( buffer.cast().data(), py::len(buffer), enable_etdump, @@ -439,7 +466,7 @@ struct PyModule final { size_t ptr_len, bool enable_etdump, size_t debug_buffer_size = 0) - : module_(torch::executor::load_from_buffer( + : module_(load_module_from_buffer( ptr, ptr_len, enable_etdump, @@ -449,10 +476,8 @@ struct PyModule final { const std::string& path, bool enable_etdump, size_t debug_buffer_size = 0) - : module_(torch::executor::load_from_file( - path, - enable_etdump, - debug_buffer_size)) {} + : module_(load_module_from_file(path, enable_etdump, debug_buffer_size)) { + } PyModule(const PyModule&) = delete; PyModule& operator=(const PyModule&) = delete; @@ -525,8 +550,8 @@ struct PyModule final { EValue evalue(at_tensor); #else // convert at::Tensor to torch::executor::Tensor - auto type = torch::util::torchToExecuTorchScalarType( - at_tensor.options().dtype()); + auto type = + torch_to_executorch_scalar_type(at_tensor.options().dtype()); size_t dim = at_tensor.dim(); // cant directly alias at::Tensor sizes and strides due to int64 vs // int32 typing conflict @@ -551,7 +576,7 @@ struct PyModule final { torch::executor::Tensor temp = torch::executor::Tensor(&input_tensors.back()); - torch::util::alias_etensor_to_attensor(at_tensor, temp); + alias_etensor_to_attensor(at_tensor, temp); EValue evalue(temp); #endif @@ -628,10 +653,10 @@ struct PyModule final { void load_bundled_input( PyBundledModule& m, - const string method_name, + const std::string method_name, size_t testset_idx) { const void* bundled_program_ptr = m.get_bundled_program_ptr(); - Error status = bundled_program::LoadBundledInput( + Error status = LoadBundledInput( module_->get_method(method_name), bundled_program_ptr, testset_idx); THROW_IF_ERROR( status, @@ -641,20 +666,19 @@ struct PyModule final { py::list verify_result_with_bundled_expected_output( PyBundledModule& m, - const string method_name, + const std::string method_name, size_t testset_idx, double rtol = 1e-5, double atol = 1e-8) { const void* bundled_program_ptr = m.get_bundled_program_ptr(); auto& method = module_->get_method(method_name); - Error status = bundled_program::LoadBundledInput( - method, bundled_program_ptr, testset_idx); + Error status = LoadBundledInput(method, bundled_program_ptr, testset_idx); THROW_IF_ERROR( status, "LoadBundledInput failed with status %" PRIu32, static_cast(status)); py::list outputs = plan_execute(method_name); - status = bundled_program::VerifyResultWithBundledExpectedOutput( + status = VerifyResultWithBundledExpectedOutput( method, bundled_program_ptr, testset_idx, rtol, atol); THROW_IF_ERROR( status, @@ -663,7 +687,7 @@ struct PyModule final { return outputs; } - py::list plan_execute(const string method_name) { + py::list plan_execute(const std::string method_name) { auto& method = module_->get_method(method_name); // Need to pre-allocate space for outputs just like in run_method. const auto num_outputs = method.outputs_size(); @@ -704,8 +728,7 @@ struct PyModule final { // module object list[i] = py::cast(v.toTensor().clone()); #else - list[i] = py::cast( - torch::util::alias_attensor_to_etensor(v.toTensor()).clone()); + list[i] = py::cast(alias_attensor_to_etensor(v.toTensor()).clone()); #endif } else { ET_ASSERT_UNREACHABLE_MSG("Invalid model output type"); @@ -720,8 +743,7 @@ struct PyModule final { // bundled programs. std::vector> output_storages_; - std::vector> make_output_storages( - const executor::Method& method) { + std::vector> make_output_storages(const Method& method) { const auto num_outputs = method.outputs_size(); // These output storages will not be used if the ExecuTorch program already // pre-allocated output space. That is represented by an error from @@ -845,5 +867,6 @@ PYBIND11_MODULE(EXECUTORCH_PYTHON_MODULE_NAME, m) { py::class_(m, "BundledModule"); } -} // namespace executor -} // namespace torch +} // namespace pybindings +} // namespace extension +} // namespace executorch diff --git a/extension/pytree/aten_util/ivalue_util.cpp b/extension/pytree/aten_util/ivalue_util.cpp index 6935d45e92..c4d11c13ee 100644 --- a/extension/pytree/aten_util/ivalue_util.cpp +++ b/extension/pytree/aten_util/ivalue_util.cpp @@ -10,13 +10,12 @@ #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { using namespace c10; using namespace at; -using namespace torch::executor::pytree; +using namespace executorch::extension::pytree; ContainerHandle getContainerHandle(const IValue& data) { if (data.isList()) { @@ -214,6 +213,5 @@ bool is_same(const IValue& lhs, const IValue& rhs) { return at::all(l == r).item(); } -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/pytree/aten_util/ivalue_util.h b/extension/pytree/aten_util/ivalue_util.h index 7798a3a4b3..b797f13e8f 100644 --- a/extension/pytree/aten_util/ivalue_util.h +++ b/extension/pytree/aten_util/ivalue_util.h @@ -19,20 +19,19 @@ #include -namespace torch { -namespace executor { -namespace util { - -using Empty = torch::executor::pytree::Empty; +namespace executorch { +namespace extension { std::pair< std::vector, - std::unique_ptr>> + std::unique_ptr<::executorch::extension::pytree::TreeSpec< + ::executorch::extension::pytree::Empty>>> flatten(const c10::IValue& data); c10::IValue unflatten( const std::vector& tensors, - const std::unique_ptr>& tree_spec); + const std::unique_ptr<::executorch::extension::pytree::TreeSpec< + ::executorch::extension::pytree::Empty>>& tree_spec); bool is_same( const std::vector& a, @@ -40,6 +39,17 @@ bool is_same( bool is_same(const c10::IValue& lhs, const c10::IValue& rhs); +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::flatten; +using ::executorch::extension::is_same; +using ::executorch::extension::unflatten; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/pytree/aten_util/test/ivalue_util_test.cpp b/extension/pytree/aten_util/test/ivalue_util_test.cpp index 92954f2af8..8b35f173e3 100644 --- a/extension/pytree/aten_util/test/ivalue_util_test.cpp +++ b/extension/pytree/aten_util/test/ivalue_util_test.cpp @@ -9,9 +9,9 @@ #include #include -using namespace c10; -using namespace torch::executor::pytree; -using namespace torch::executor::util; +using executorch::extension::flatten; +using executorch::extension::is_same; +using executorch::extension::unflatten; std::vector makeExampleTensors(size_t N) { std::vector tensors; @@ -22,7 +22,7 @@ std::vector makeExampleTensors(size_t N) { } struct TestCase { - IValue ivalue; + c10::IValue ivalue; std::vector tensors; }; @@ -54,7 +54,7 @@ TestCase makeExampleDictOfTensors() { TestCase makeExampleComposite() { auto tensors = makeExampleTensors(8); - IValue list = c10::List{ + c10::IValue list = c10::List{ tensors[1], tensors[2], }; @@ -100,7 +100,7 @@ void testUnflatten(const TestCase& testcase) { auto ret = flatten(testcase.ivalue); // then we unflatten it - IValue unflattened = unflatten(ret.first, ret.second); + c10::IValue unflattened = unflatten(ret.first, ret.second); // and see if we got the same IValue back ASSERT_TRUE(is_same(unflattened, testcase.ivalue)); diff --git a/extension/pytree/function_ref.h b/extension/pytree/function_ref.h index 01d2988597..0458610c4d 100644 --- a/extension/pytree/function_ref.h +++ b/extension/pytree/function_ref.h @@ -38,14 +38,16 @@ #include #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { namespace pytree { //===----------------------------------------------------------------------===// // Features from C++20 //===----------------------------------------------------------------------===// +namespace internal { + template struct remove_cvref { using type = @@ -55,6 +57,8 @@ struct remove_cvref { template using remove_cvref_t = typename remove_cvref::type; +} // namespace internal + template class FunctionRef; @@ -79,7 +83,7 @@ class FunctionRef { typename Callable, // This is not the copy-constructor. typename std::enable_if< - !std::is_same, FunctionRef>::value, + !std::is_same, FunctionRef>::value, int32_t>::type = 0, // Avoid lvalue reference to non-capturing lambda. typename std::enable_if< @@ -153,6 +157,16 @@ class FunctionRef { } }; +} // namespace pytree +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace pytree { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::pytree::FunctionRef; } // namespace pytree } // namespace executor } // namespace torch diff --git a/extension/pytree/pybindings.cpp b/extension/pytree/pybindings.cpp index 9bcf6043b7..931943e489 100644 --- a/extension/pytree/pybindings.cpp +++ b/extension/pytree/pybindings.cpp @@ -15,8 +15,8 @@ namespace py = pybind11; -namespace torch { -namespace executor { +namespace executorch { +namespace extension { namespace pytree { namespace { @@ -395,5 +395,5 @@ PYBIND11_MODULE(pybindings, m) { } } // namespace pytree -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/pytree/pytree.h b/extension/pytree/pytree.h index 127310254d..78e2305fe3 100644 --- a/extension/pytree/pytree.h +++ b/extension/pytree/pytree.h @@ -19,8 +19,8 @@ // NB: This is a local, pytree FunctionRef and not from the ExecuTorch runtime. #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { namespace pytree { inline void pytree_assert(bool must_be_true) { @@ -738,6 +738,18 @@ std::pair, std::unique_ptr>> flatten( std::make_unique>(clone(tree, spec_leaves.get()))}; } +} // namespace pytree +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace pytree { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::pytree::Empty; +using ::executorch::extension::pytree::from_str; +using ::executorch::extension::pytree::TreeSpec; } // namespace pytree } // namespace executor } // namespace torch diff --git a/extension/pytree/test/TARGETS b/extension/pytree/test/TARGETS index c281994cea..190bdb0bc6 100644 --- a/extension/pytree/test/TARGETS +++ b/extension/pytree/test/TARGETS @@ -5,7 +5,7 @@ load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest") oncall("executorch") cpp_unittest( - name = "cpptest", + name = "pytree_test", srcs = ["test_pytree.cpp"], deps = ["//executorch/extension/pytree:pytree"], ) @@ -17,7 +17,7 @@ cpp_unittest( ) python_unittest( - name = "test", + name = "pybindings_test", srcs = [ "test.py", ], diff --git a/extension/pytree/test/function_ref_test.cpp b/extension/pytree/test/function_ref_test.cpp index f847c8ebd7..a3cdbd824b 100644 --- a/extension/pytree/test/function_ref_test.cpp +++ b/extension/pytree/test/function_ref_test.cpp @@ -6,15 +6,13 @@ * LICENSE file in the root directory of this source tree. */ -#include - #include +#include + using namespace ::testing; -namespace torch { -namespace executor { -namespace pytree { +using ::executorch::extension::pytree::FunctionRef; namespace { class Item { @@ -84,7 +82,3 @@ TEST(FunctionRefTest, FunctionPointer) { Item item1(0, &one); EXPECT_EQ(item1.get(), 1); } - -} // namespace pytree -} // namespace executor -} // namespace torch diff --git a/extension/pytree/test/test_pytree.cpp b/extension/pytree/test/test_pytree.cpp index 5f8ab72acf..0101bca3f5 100644 --- a/extension/pytree/test/test_pytree.cpp +++ b/extension/pytree/test/test_pytree.cpp @@ -6,19 +6,15 @@ * LICENSE file in the root directory of this source tree. */ -#include -#include - #include -int main(int argc, char* argv[]) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} +#include +#include -namespace torch { -namespace executor { -namespace pytree { +using ::executorch::extension::pytree::ContainerHandle; +using ::executorch::extension::pytree::Key; +using ::executorch::extension::pytree::Kind; +using ::executorch::extension::pytree::unflatten; using Leaf = int32_t; @@ -187,7 +183,3 @@ TEST(pytree, FlattenNestedDict) { ASSERT_EQ(*leaves[i], items[i]); } } - -} // namespace pytree -} // namespace executor -} // namespace torch diff --git a/extension/runner_util/inputs.cpp b/extension/runner_util/inputs.cpp index 103d3461f0..f4c77cae19 100644 --- a/extension/runner_util/inputs.cpp +++ b/extension/runner_util/inputs.cpp @@ -12,9 +12,15 @@ #include #include -namespace torch { -namespace executor { -namespace util { +using executorch::runtime::Error; +using executorch::runtime::Method; +using executorch::runtime::MethodMeta; +using executorch::runtime::Result; +using executorch::runtime::Tag; +using executorch::runtime::TensorInfo; + +namespace executorch { +namespace extension { Result prepare_input_tensors(Method& method) { MethodMeta method_meta = method.method_meta(); @@ -53,6 +59,5 @@ Result prepare_input_tensors(Method& method) { return BufferCleanup({inputs, num_allocated}); } -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/runner_util/inputs.h b/extension/runner_util/inputs.h index 316ae53cb5..b933bca807 100644 --- a/extension/runner_util/inputs.h +++ b/extension/runner_util/inputs.h @@ -12,9 +12,8 @@ #include #include -namespace torch { -namespace executor { -namespace util { +namespace executorch { +namespace extension { /** * RAII helper that frees a set of buffers when destroyed. Movable. @@ -25,14 +24,15 @@ class BufferCleanup final { * Takes ownership of `buffers.data()` and the elements of `buffers`, which * each will be passed to `free()` when the object is destroyed. */ - explicit BufferCleanup(Span buffers) : buffers_(buffers) {} + explicit BufferCleanup(executorch::runtime::Span buffers) + : buffers_(buffers) {} /** * Move ctor. Takes ownership of the data previously owned by `rhs`, leaving * `rhs` with an empty list of buffers. */ BufferCleanup(BufferCleanup&& rhs) noexcept : buffers_(rhs.buffers_) { - rhs.buffers_ = Span(); + rhs.buffers_ = executorch::runtime::Span(); } ~BufferCleanup() { @@ -48,7 +48,7 @@ class BufferCleanup final { BufferCleanup& operator=(const BufferCleanup&) = delete; BufferCleanup& operator=(BufferCleanup&&) noexcept = delete; - Span buffers_; + executorch::runtime::Span buffers_; }; /** @@ -61,20 +61,31 @@ class BufferCleanup final { * remain alive when calling `method->execute()`. * @returns An error on failure. */ -Result prepare_input_tensors(Method& method); +executorch::runtime::Result prepare_input_tensors( + executorch::runtime::Method& method); namespace internal { /** * INTERNAL-ONLY: Creates a Tensor using the provided shape and buffer, * fills it with ones, and sets the input at `input_index`. */ -Error fill_and_set_input( - Method& method, - TensorInfo& tensor_meta, +executorch::runtime::Error fill_and_set_input( + executorch::runtime::Method& method, + executorch::runtime::TensorInfo& tensor_meta, size_t input_index, void* data_ptr); } // namespace internal +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace util { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::BufferCleanup; +using ::executorch::extension::prepare_input_tensors; } // namespace util } // namespace executor } // namespace torch diff --git a/extension/runner_util/inputs_aten.cpp b/extension/runner_util/inputs_aten.cpp index d9ccb63fd5..83d12dac42 100644 --- a/extension/runner_util/inputs_aten.cpp +++ b/extension/runner_util/inputs_aten.cpp @@ -14,10 +14,12 @@ #include #include -namespace torch { -namespace executor { -namespace util { +using executorch::runtime::Error; +using executorch::runtime::Method; +using executorch::runtime::TensorInfo; +namespace executorch { +namespace extension { namespace internal { Error fill_and_set_input( @@ -38,7 +40,5 @@ Error fill_and_set_input( } } // namespace internal - -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/runner_util/inputs_portable.cpp b/extension/runner_util/inputs_portable.cpp index f799a968cf..f9db03bcd1 100644 --- a/extension/runner_util/inputs_portable.cpp +++ b/extension/runner_util/inputs_portable.cpp @@ -16,9 +16,14 @@ #include #include -namespace torch { -namespace executor { -namespace util { +using exec_aten::Tensor; +using exec_aten::TensorImpl; +using executorch::runtime::Error; +using executorch::runtime::Method; +using executorch::runtime::TensorInfo; + +namespace executorch { +namespace extension { namespace internal { namespace { @@ -68,6 +73,5 @@ Error fill_and_set_input( } } // namespace internal -} // namespace util -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/runner_util/managed_tensor.h b/extension/runner_util/managed_tensor.h index 16a84a13df..5c74f7550a 100644 --- a/extension/runner_util/managed_tensor.h +++ b/extension/runner_util/managed_tensor.h @@ -6,28 +6,27 @@ * LICENSE file in the root directory of this source tree. */ +#pragma once + +#include +// @nolint PATTERNLINT Ok to use stdlib for this optional library +#include + #include #include #include #include -#include -// NOTE: required by torchchat install_et.sh script. -// @nolint PATTERNLINT Ok to use stdlib for this optional library -#include #ifdef USE_ATEN_LIB #include -#else -#include #endif -#pragma once -namespace torch { -namespace executor { +namespace executorch { +namespace extension { /** * A tensor wrapper takes ownership of all the memory of the necessary metadata - * for torch::executor::Tensor. Note that it doesn't own the data memory. + * for exec_aten::Tensor. Note that it doesn't own the data memory. */ class ManagedTensor { public: @@ -43,7 +42,7 @@ class ManagedTensor { explicit ManagedTensor( void* data, const std::vector& sizes, - ScalarType dtype) + exec_aten::ScalarType dtype) : sizes_(sizes) { #ifdef USE_ATEN_LIB tensor_ = torch::from_blob(data, sizes, dtype); @@ -58,43 +57,51 @@ class ManagedTensor { } // Allocate TensorImpl. - tensor_impl_ = std::make_unique( + tensor_impl_ = std::make_unique( dtype, sizes_.size(), sizes_.data(), data, /*dim_order=*/nullptr, strides_.data(), - TensorShapeDynamism::DYNAMIC_BOUND); + executorch::runtime::TensorShapeDynamism::DYNAMIC_BOUND); #endif } void resize(const std::vector& new_sizes) { - auto err = resize_tensor( + auto err = executorch::runtime::resize_tensor( this->get_aliasing_tensor(), exec_aten::ArrayRef(new_sizes.data(), new_sizes.size())); - ET_CHECK(err == Error::Ok); + ET_CHECK(err == executorch::runtime::Error::Ok); } /** * Get the underlying Tensor object. This is assuming the copying is cheap. */ - Tensor get_aliasing_tensor() { + exec_aten::Tensor get_aliasing_tensor() { #ifdef USE_ATEN_LIB return tensor_; #else - return Tensor(tensor_impl_.get()); + return exec_aten::Tensor(tensor_impl_.get()); #endif } private: - std::unique_ptr tensor_impl_; + std::unique_ptr tensor_impl_; std::vector sizes_; std::vector strides_; #ifdef USE_ATEN_LIB - Tensor tensor_; + exec_aten::Tensor tensor_; #endif }; +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::ManagedTensor; } // namespace executor } // namespace torch diff --git a/extension/runner_util/test/inputs_test.cpp b/extension/runner_util/test/inputs_test.cpp index 94e99bcc09..c916da488e 100644 --- a/extension/runner_util/test/inputs_test.cpp +++ b/extension/runner_util/test/inputs_test.cpp @@ -20,20 +20,19 @@ using namespace ::testing; using exec_aten::ScalarType; using exec_aten::Tensor; -using torch::executor::Error; -using torch::executor::EValue; -using torch::executor::MemoryAllocator; -using torch::executor::MemoryManager; -using torch::executor::Method; -using torch::executor::Program; -using torch::executor::Result; -using torch::executor::Span; -using torch::executor::Tag; -using torch::executor::Tensor; -using torch::executor::testing::ManagedMemoryManager; -using torch::executor::util::BufferCleanup; -using torch::executor::util::FileDataLoader; -using torch::executor::util::prepare_input_tensors; +using executorch::extension::BufferCleanup; +using executorch::extension::FileDataLoader; +using executorch::extension::prepare_input_tensors; +using executorch::runtime::Error; +using executorch::runtime::EValue; +using executorch::runtime::MemoryAllocator; +using executorch::runtime::MemoryManager; +using executorch::runtime::Method; +using executorch::runtime::Program; +using executorch::runtime::Result; +using executorch::runtime::Span; +using executorch::runtime::Tag; +using executorch::runtime::testing::ManagedMemoryManager; class InputsTest : public ::testing::Test { protected: diff --git a/extension/runner_util/test/managed_tensor_test.cpp b/extension/runner_util/test/managed_tensor_test.cpp index b511cdbcf1..8ac1285f2b 100644 --- a/extension/runner_util/test/managed_tensor_test.cpp +++ b/extension/runner_util/test/managed_tensor_test.cpp @@ -17,13 +17,13 @@ using exec_aten::DimOrderType; using exec_aten::ScalarType; using exec_aten::SizesType; using exec_aten::StridesType; -using torch::executor::ArrayRef; -using torch::executor::ManagedTensor; +using executorch::extension::ManagedTensor; +using executorch::runtime::ArrayRef; class ManagedTensorTest : public ::testing::Test { protected: void SetUp() override { - torch::executor::runtime_init(); + executorch::runtime::runtime_init(); data_ = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; sizes_ = {2, 3, 4}; diff --git a/extension/testing_util/temp_file.h b/extension/testing_util/temp_file.h index a710e130e7..aa8f5bcc82 100644 --- a/extension/testing_util/temp_file.h +++ b/extension/testing_util/temp_file.h @@ -18,9 +18,9 @@ #include -namespace torch { -namespace executor { -namespace testing { +namespace executorch { +namespace extension { +namespace testing { // Test-only helpers belong in a "testing" sub-namespace. /** * Creates and manages a named temporary file in the file system. Deletes the @@ -98,6 +98,16 @@ class TempFile { std::string path_; }; +} // namespace testing +} // namespace extension +} // namespace executorch + +namespace torch { +namespace executor { +namespace testing { +// TODO(T197294990): Remove these deprecated aliases once all users have moved +// to the new `::executorch` namespaces. +using ::executorch::extension::testing::TempFile; } // namespace testing } // namespace executor } // namespace torch diff --git a/extension/testing_util/test/temp_file_test.cpp b/extension/testing_util/test/temp_file_test.cpp index 072630897b..2a666d24d5 100644 --- a/extension/testing_util/test/temp_file_test.cpp +++ b/extension/testing_util/test/temp_file_test.cpp @@ -18,7 +18,7 @@ #include using namespace ::testing; -using torch::executor::testing::TempFile; +using executorch::extension::testing::TempFile; TEST(TempFileTest, Smoke) { std::string path; diff --git a/extension/training/optimizer/sgd.cpp b/extension/training/optimizer/sgd.cpp index f2f63523b4..ad6130183e 100644 --- a/extension/training/optimizer/sgd.cpp +++ b/extension/training/optimizer/sgd.cpp @@ -12,8 +12,14 @@ #include #include -namespace torch { -namespace executor { +using exec_aten::Tensor; +using exec_aten::TensorImpl; +using ::executorch::runtime::Error; +using ::executorch::runtime::KernelRuntimeContext; +using ::executorch::runtime::Span; + +namespace executorch { +namespace extension { namespace training { namespace optimizer { @@ -67,7 +73,7 @@ Error SGD::step(Span gradient_names, Span gradient_data) { InvalidState, "Gradient names and gradients must have the same length."); - RuntimeContext context; + KernelRuntimeContext context; for (auto& group : param_groups_) { auto& options = static_cast(group.options()); auto weight_decay = options.weight_decay(); @@ -170,7 +176,8 @@ SGD::~SGD() { #endif } } + } // namespace optimizer } // namespace training -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/training/optimizer/sgd.h b/extension/training/optimizer/sgd.h index 308e3471d9..fb797e4d5d 100644 --- a/extension/training/optimizer/sgd.h +++ b/extension/training/optimizer/sgd.h @@ -23,15 +23,11 @@ #include #include -namespace torch { -namespace executor { +namespace executorch { +namespace extension { namespace training { namespace optimizer { -using Tensor = exec_aten::Tensor; -using TensorImpl = exec_aten::TensorImpl; -using ScalarType = exec_aten::ScalarType; - /** * SGD optimizer state. This keeps track of the state of a given parameter to * be used in later epochs. @@ -44,15 +40,15 @@ class SGDParamState { * @param[in] momentum_buffer A tensor that stores the momentum at the last * epoch. */ - explicit SGDParamState(Tensor& momentum_buffer) + explicit SGDParamState(exec_aten::Tensor& momentum_buffer) : momentum_buffer_(momentum_buffer) {} - Tensor& momentum_buffer() { + exec_aten::Tensor& momentum_buffer() { return momentum_buffer_; } private: - Tensor momentum_buffer_; + exec_aten::Tensor momentum_buffer_; }; /** @@ -159,13 +155,13 @@ class SGDParamGroup { * @param[in] param_data The tensors representing the param data. */ /* implicit */ SGDParamGroup( - Span param_names, - Span param_data) + ::executorch::runtime::Span param_names, + ::executorch::runtime::Span param_data) : param_data_(std::move(param_data)), param_names_(std::move(param_names)) {} SGDParamGroup( - Span param_names, - Span param_data, + ::executorch::runtime::Span param_names, + ::executorch::runtime::Span param_data, std::unique_ptr options) : param_data_(std::move(param_data)), param_names_(std::move(param_names)), @@ -175,14 +171,14 @@ class SGDParamGroup { SGDOptions& options(); const SGDOptions& options() const; void set_options(std::unique_ptr options); - Span param_names(); - const Span param_names() const; - Span param_data(); - const Span param_data() const; + ::executorch::runtime::Span param_names(); + const ::executorch::runtime::Span param_names() const; + ::executorch::runtime::Span param_data(); + const ::executorch::runtime::Span param_data() const; private: - Span param_data_; - Span param_names_; + ::executorch::runtime::Span param_data_; + ::executorch::runtime::Span param_names_; std::unique_ptr options_; }; @@ -202,8 +198,8 @@ class SGD { } explicit SGD( - Span param_names, - Span param_data, + ::executorch::runtime::Span param_names, + ::executorch::runtime::Span param_data, SGDOptions defaults) : SGD({SGDParamGroup(std::move(param_names), std::move(param_data))}, defaults) {} @@ -225,7 +221,9 @@ class SGD { * @param[in] gradient_data The gradient tensors to be used for optimization * step. */ - Error step(Span gradient_names, Span gradient_data); + ::executorch::runtime::Error step( + ::executorch::runtime::Span gradient_names, + ::executorch::runtime::Span gradient_data); private: std::vector param_groups_; @@ -235,5 +233,5 @@ class SGD { } // namespace optimizer } // namespace training -} // namespace executor -} // namespace torch +} // namespace extension +} // namespace executorch diff --git a/extension/training/optimizer/test/sgd_test.cpp b/extension/training/optimizer/test/sgd_test.cpp index 1dd1a3e55d..33a70b4fe9 100644 --- a/extension/training/optimizer/test/sgd_test.cpp +++ b/extension/training/optimizer/test/sgd_test.cpp @@ -17,12 +17,14 @@ // @lint-ignore-every CLANGTIDY facebook-hte-CArray using namespace ::testing; -using namespace torch::executor::training::optimizer; using exec_aten::ScalarType; using exec_aten::Tensor; -using torch::executor::Error; -using torch::executor::Span; -using torch::executor::testing::TensorFactory; +using ::executorch::extension::training::optimizer::SGD; +using ::executorch::extension::training::optimizer::SGDOptions; +using ::executorch::extension::training::optimizer::SGDParamState; +using ::executorch::runtime::Error; +using ::executorch::runtime::Span; +using ::executorch::runtime::testing::TensorFactory; class SGDOptimizerTest : public ::testing::Test { protected: diff --git a/runtime/executor/test/executor_test.cpp b/runtime/executor/test/executor_test.cpp index 74da48d732..da0d53374f 100644 --- a/runtime/executor/test/executor_test.cpp +++ b/runtime/executor/test/executor_test.cpp @@ -31,6 +31,8 @@ using executorch::runtime::KernelRuntimeContext; using executorch::runtime::register_kernels; using executorch::runtime::testing::TensorFactory; +namespace pytree = ::executorch::extension::pytree; + class ExecutorTest : public ::testing::Test { protected: void SetUp() override { @@ -210,7 +212,7 @@ TEST(PyTreeEValue, List) { Scalar d((double)3.0); EValue items[2] = {i, d}; - auto c = torch::executor::pytree::unflatten(spec, items); + auto c = pytree::unflatten(spec, items); ASSERT_TRUE(c.isList()); ASSERT_EQ(c.size(), 2); @@ -232,7 +234,7 @@ TEST(PyTreeEValue, List) { auto unflatten(EValue* items) { std::string spec = "D4#1#1#1#1('key0':$,1:$,23:$,123:$)"; - return torch::executor::pytree::unflatten(spec, items); + return pytree::unflatten(spec, items); } TEST(PyTreeEValue, DestructedSpec) { @@ -249,8 +251,8 @@ TEST(PyTreeEValue, DestructedSpec) { auto& key0 = c.key(0); auto& key1 = c.key(1); - ASSERT_TRUE(key0 == torch::executor::pytree::Key("key0")); - ASSERT_TRUE(key1 == torch::executor::pytree::Key(1)); + ASSERT_TRUE(key0 == pytree::Key("key0")); + ASSERT_TRUE(key1 == pytree::Key(1)); const auto& child0 = c[0]; const auto& child1 = c[1];