From 99827e4e47358c1c963c84e3d924ae365ea166eb Mon Sep 17 00:00:00 2001 From: pytorchbot Date: Thu, 10 Oct 2024 23:15:57 -0700 Subject: [PATCH] s/exec_aten::/executorch::aten::/ for extension/**/*.h (#6106) s/exec_aten::/executorch::aten::/ for extension/**/*.h (#6032) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/6032 Migrate all extension headers to use the new aten namespace, so that they act as good examples for users. The .cpp code can migrate later. Reviewed By: lucylq Differential Revision: D64079593 fbshipit-source-id: 62164cd6ec3238e21e896813f185d0c1446ef527 (cherry picked from commit 69c2c766c7ed5ef61581c352ca7016f38aeff771) Co-authored-by: Dave Bort --- extension/android/jni/jni_layer_constants.h | 2 +- .../make_boxed_from_unboxed_functor.h | 25 +- extension/llm/runner/image_prefiller.h | 2 +- extension/llm/runner/text_decoder_runner.h | 5 +- extension/llm/runner/text_token_generator.h | 11 +- extension/tensor/tensor_impl_ptr.h | 98 ++++---- extension/tensor/tensor_ptr.h | 116 +++++---- extension/tensor/tensor_ptr_maker.h | 237 +++++++++--------- extension/training/module/training_module.h | 8 +- extension/training/optimizer/sgd.h | 21 +- 10 files changed, 272 insertions(+), 253 deletions(-) diff --git a/extension/android/jni/jni_layer_constants.h b/extension/android/jni/jni_layer_constants.h index b710dbe8e0..0f98f7b4ee 100644 --- a/extension/android/jni/jni_layer_constants.h +++ b/extension/android/jni/jni_layer_constants.h @@ -37,7 +37,7 @@ constexpr static int kTensorDTypeBits4x2 = 20; constexpr static int kTensorDTypeBits8 = 21; constexpr static int kTensorDTypeBits16 = 22; -using exec_aten::ScalarType; +using executorch::aten::ScalarType; const std::unordered_map scalar_type_to_java_dtype = { {ScalarType::Byte, kTensorDTypeUInt8}, diff --git a/extension/kernel_util/make_boxed_from_unboxed_functor.h b/extension/kernel_util/make_boxed_from_unboxed_functor.h index 409c981cbb..56c4c02a9d 100644 --- a/extension/kernel_util/make_boxed_from_unboxed_functor.h +++ b/extension/kernel_util/make_boxed_from_unboxed_functor.h @@ -66,12 +66,12 @@ struct decay_if_not_tensor final { using type = std::decay_t; }; template <> -struct decay_if_not_tensor final { - using type = exec_aten::Tensor&; +struct decay_if_not_tensor final { + using type = executorch::aten::Tensor&; }; template <> -struct decay_if_not_tensor final { - using type = const exec_aten::Tensor&; +struct decay_if_not_tensor final { + using type = const executorch::aten::Tensor&; }; template @@ -82,29 +82,30 @@ struct evalue_to_arg final { }; template <> -struct evalue_to_arg final { - static exec_aten::Tensor& call(executorch::runtime::EValue& v) { +struct evalue_to_arg final { + static executorch::aten::Tensor& call(executorch::runtime::EValue& v) { return v.toTensor(); } }; template <> -struct evalue_to_arg final { - static const exec_aten::Tensor& call(executorch::runtime::EValue& v) { +struct evalue_to_arg final { + static const executorch::aten::Tensor& call(executorch::runtime::EValue& v) { return v.toTensor(); } }; template -struct evalue_to_arg> final { - static exec_aten::optional call(executorch::runtime::EValue& v) { +struct evalue_to_arg> final { + static executorch::aten::optional call(executorch::runtime::EValue& v) { return v.toOptional(); } }; template -struct evalue_to_arg>> final { - static exec_aten::ArrayRef> call( +struct evalue_to_arg>> + final { + static executorch::aten::ArrayRef> call( executorch::runtime::EValue& v) { return v.toListOptionalTensor(); } diff --git a/extension/llm/runner/image_prefiller.h b/extension/llm/runner/image_prefiller.h index 93bb9a030b..89ed026357 100644 --- a/extension/llm/runner/image_prefiller.h +++ b/extension/llm/runner/image_prefiller.h @@ -30,7 +30,7 @@ class ImagePrefiller { * It's passed as reference and will be updated inside this function. * @return The next token of the LLM Module after prefill. */ - virtual ::executorch::runtime::Result prefill( + virtual ::executorch::runtime::Result prefill( Image& image, int64_t& start_pos) = 0; diff --git a/extension/llm/runner/text_decoder_runner.h b/extension/llm/runner/text_decoder_runner.h index 14614775f3..9d5a8112e5 100644 --- a/extension/llm/runner/text_decoder_runner.h +++ b/extension/llm/runner/text_decoder_runner.h @@ -37,7 +37,7 @@ class TextDecoderRunner { * Module. * @return The output of the LLM Module. This will be a tensor of logits. */ - virtual ::executorch::runtime::Result step( + virtual ::executorch::runtime::Result step( TensorPtr& input, TensorPtr& start_pos); @@ -66,7 +66,8 @@ class TextDecoderRunner { * @param logits_tensor The logits tensor. * @return The next token. */ - inline int32_t logits_to_token(const exec_aten::Tensor& logits_tensor) { + inline int32_t logits_to_token( + const executorch::aten::Tensor& logits_tensor) { int32_t result = 0; ET_SWITCH_THREE_TYPES( Float, diff --git a/extension/llm/runner/text_token_generator.h b/extension/llm/runner/text_token_generator.h index 1726750ece..0392e340c6 100644 --- a/extension/llm/runner/text_token_generator.h +++ b/extension/llm/runner/text_token_generator.h @@ -53,7 +53,7 @@ class TextTokenGenerator { int64_t pos = start_pos; // position in the sequence std::vector token_data; // allocate space for the tokens - std::vector token_shape; + std::vector token_shape; // Token after prefill uint64_t cur_token = tokens.back(); @@ -70,10 +70,11 @@ class TextTokenGenerator { } // initialize tensor wrappers - auto tokens_managed = - from_blob(token_data.data(), token_shape, exec_aten::ScalarType::Long); + auto tokens_managed = from_blob( + token_data.data(), token_shape, executorch::aten::ScalarType::Long); - auto start_pos_managed = from_blob(&pos, {1}, exec_aten::ScalarType::Long); + auto start_pos_managed = + from_blob(&pos, {1}, executorch::aten::ScalarType::Long); // Generate our tokens while (pos < seq_len - 1) { @@ -82,7 +83,7 @@ class TextTokenGenerator { text_decoder_runner_->step(tokens_managed, start_pos_managed); ET_CHECK_OK_OR_RETURN_ERROR(logits_res.error()); - exec_aten::Tensor& logits_tensor = logits_res.get(); + executorch::aten::Tensor& logits_tensor = logits_res.get(); prev_token = cur_token; diff --git a/extension/tensor/tensor_impl_ptr.h b/extension/tensor/tensor_impl_ptr.h index 89fc7ff1eb..38032b89d1 100644 --- a/extension/tensor/tensor_impl_ptr.h +++ b/extension/tensor/tensor_impl_ptr.h @@ -30,7 +30,7 @@ namespace extension { * It serves as a safer, more convenient alternative to the original TensorImpl, * which does not manage its metadata by design. */ -using TensorImplPtr = std::shared_ptr; +using TensorImplPtr = std::shared_ptr; #else /** * A smart pointer type for managing the lifecycle of a TensorImpl. @@ -40,7 +40,7 @@ using TensorImplPtr = std::shared_ptr; * metadata. */ using TensorImplPtr = - c10::intrusive_ptr; + c10::intrusive_ptr; #endif // USE_ATEN_LIB /** @@ -59,13 +59,13 @@ using TensorImplPtr = * @return A TensorImplPtr managing the newly created TensorImpl. */ TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, void* data, - std::vector dim_order, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, + std::vector dim_order, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND, std::function deleter = nullptr); /** @@ -82,11 +82,11 @@ TensorImplPtr make_tensor_impl_ptr( * @return A TensorImplPtr managing the newly created TensorImpl. */ inline TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, void* data, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND, std::function deleter = nullptr) { return make_tensor_impl_ptr( std::move(sizes), data, {}, {}, type, dynamism, std::move(deleter)); @@ -114,15 +114,16 @@ inline TensorImplPtr make_tensor_impl_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - std::vector dim_order = {}, - std::vector strides = {}, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector dim_order = {}, + std::vector strides = {}, + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { if (type != deduced_type) { ET_CHECK_MSG( runtime::canCast(deduced_type, type), @@ -179,13 +180,15 @@ TensorImplPtr make_tensor_impl_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorImplPtr make_tensor_impl_ptr( std::vector data, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - std::vector sizes{exec_aten::SizesType(data.size())}; + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes{ + executorch::aten::SizesType(data.size())}; return make_tensor_impl_ptr( std::move(sizes), std::move(data), {0}, {1}, type, dynamism); } @@ -213,15 +216,16 @@ inline TensorImplPtr make_tensor_impl_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, std::initializer_list list, - std::vector dim_order = {}, - std::vector strides = {}, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector dim_order = {}, + std::vector strides = {}, + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_impl_ptr( std::move(sizes), std::vector(std::move(list)), @@ -251,13 +255,15 @@ inline TensorImplPtr make_tensor_impl_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorImplPtr make_tensor_impl_ptr( std::initializer_list list, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - std::vector sizes{exec_aten::SizesType(list.size())}; + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes{ + executorch::aten::SizesType(list.size())}; return make_tensor_impl_ptr( std::move(sizes), std::move(list), {0}, {1}, type, dynamism); } @@ -291,13 +297,13 @@ inline TensorImplPtr make_tensor_impl_ptr(T value) { * @return A TensorImplPtr managing the newly created TensorImpl. */ TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - std::vector dim_order, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector dim_order, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates a TensorImplPtr that manages a newly created TensorImpl with the @@ -314,11 +320,11 @@ TensorImplPtr make_tensor_impl_ptr( * @return A TensorImplPtr managing the newly created TensorImpl. */ inline TensorImplPtr make_tensor_impl_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_impl_ptr( std::move(sizes), std::move(data), {}, {}, type, dynamism); } diff --git a/extension/tensor/tensor_ptr.h b/extension/tensor/tensor_ptr.h index 1ec7388257..7e2e3bd579 100644 --- a/extension/tensor/tensor_ptr.h +++ b/extension/tensor/tensor_ptr.h @@ -27,7 +27,7 @@ namespace internal { struct TensorPtrDeleter final { TensorImplPtr tensor_impl; - void operator()(exec_aten::Tensor* pointer) { + void operator()(executorch::aten::Tensor* pointer) { // Release all resources immediately since the data held by the // TensorPtrDeleter is tied to the managed object, not the smart pointer // itself. We need to free this memory when the object is destroyed, not @@ -48,16 +48,16 @@ struct TensorPtrDeleter final { * among tensors as needed. */ using TensorPtr = - std::unique_ptr; + std::unique_ptr; #else /** * A smart pointer type for managing the lifecycle of a Tensor. * - * When using ATen, this is a standard unique_ptr for exec_aten::Tensor. + * When using ATen, this is a standard unique_ptr for executorch::aten::Tensor. * In ATen, the Tensor class owns its TensorImpl and associated metadata, * so no custom deleter is required. */ -using TensorPtr = std::unique_ptr; +using TensorPtr = std::unique_ptr; #endif // USE_ATEN_LIB /** @@ -74,11 +74,11 @@ using TensorPtr = std::unique_ptr; */ inline TensorPtr make_tensor_ptr(TensorImplPtr tensor_impl) { #ifndef USE_ATEN_LIB - auto tensor = std::make_unique(tensor_impl.get()); + auto tensor = std::make_unique(tensor_impl.get()); return TensorPtr( tensor.release(), internal::TensorPtrDeleter{std::move(tensor_impl)}); #else - return std::make_unique(std::move(tensor_impl)); + return std::make_unique(std::move(tensor_impl)); #endif // USE_ATEN_LIB } @@ -110,21 +110,21 @@ inline TensorPtr make_tensor_ptr(const TensorPtr& tensor) { * @return A new TensorPtr managing a Tensor with the same properties as the * original. */ -inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { +inline TensorPtr make_tensor_ptr(const executorch::aten::Tensor& tensor) { return make_tensor_ptr(make_tensor_impl_ptr( - std::vector( + std::vector( tensor.sizes().begin(), tensor.sizes().end()), tensor.mutable_data_ptr(), #ifndef USE_ATEN_LIB - std::vector( + std::vector( tensor.dim_order().begin(), tensor.dim_order().end()), - std::vector( + std::vector( tensor.strides().begin(), tensor.strides().end()), tensor.scalar_type(), tensor.shape_dynamism() #else // USE_ATEN_LIB {}, - std::vector( + std::vector( tensor.strides().begin(), tensor.strides().end()), tensor.scalar_type() #endif // USE_ATEN_LIB @@ -146,13 +146,14 @@ inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { * @return A TensorPtr that manages the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, void* data, - std::vector dim_order, - std::vector strides, - const exec_aten::ScalarType type = exec_aten::ScalarType::Float, - const exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, + std::vector dim_order, + std::vector strides, + const executorch::aten::ScalarType type = + executorch::aten::ScalarType::Float, + const executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND, std::function deleter = nullptr) { return make_tensor_ptr(make_tensor_impl_ptr( std::move(sizes), @@ -177,11 +178,12 @@ inline TensorPtr make_tensor_ptr( * @return A TensorPtr that manages the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, void* data, - const exec_aten::ScalarType type = exec_aten::ScalarType::Float, - const exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, + const executorch::aten::ScalarType type = + executorch::aten::ScalarType::Float, + const executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND, std::function deleter = nullptr) { return make_tensor_ptr(make_tensor_impl_ptr( std::move(sizes), data, {}, {}, type, dynamism, std::move(deleter))); @@ -209,15 +211,16 @@ inline TensorPtr make_tensor_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - std::vector dim_order = {}, - std::vector strides = {}, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector dim_order = {}, + std::vector strides = {}, + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr( std::move(sizes), std::move(data), @@ -246,12 +249,13 @@ inline TensorPtr make_tensor_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorPtr make_tensor_ptr( std::vector data, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr(std::move(data), type, dynamism)); } @@ -279,15 +283,16 @@ inline TensorPtr make_tensor_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, std::initializer_list list, - std::vector dim_order = {}, - std::vector strides = {}, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector dim_order = {}, + std::vector strides = {}, + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr( std::move(sizes), std::move(list), @@ -318,12 +323,13 @@ inline TensorPtr make_tensor_ptr( */ template < typename T = float, - exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> + executorch::aten::ScalarType deduced_type = + runtime::CppTypeToScalarType::value> inline TensorPtr make_tensor_ptr( std::initializer_list list, - exec_aten::ScalarType type = deduced_type, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = deduced_type, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr(std::move(list), type, dynamism)); } @@ -355,13 +361,13 @@ inline TensorPtr make_tensor_ptr(T value) { * @return A TensorPtr managing the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - std::vector dim_order, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector dim_order, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr( std::move(sizes), std::move(data), @@ -385,11 +391,11 @@ inline TensorPtr make_tensor_ptr( * @return A TensorPtr managing the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - std::vector sizes, + std::vector sizes, std::vector data, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr( make_tensor_impl_ptr(std::move(sizes), std::move(data), type, dynamism)); } @@ -403,7 +409,7 @@ inline TensorPtr make_tensor_ptr( * @return A new TensorPtr that manages a Tensor with the same properties as the * original but with copied data. */ -TensorPtr clone_tensor_ptr(const exec_aten::Tensor& tensor); +TensorPtr clone_tensor_ptr(const executorch::aten::Tensor& tensor); /** * Creates a new TensorPtr by cloning the given TensorPtr, copying the @@ -427,7 +433,7 @@ inline TensorPtr clone_tensor_ptr(const TensorPtr& tensor) { ET_NODISCARD runtime::Error resize_tensor_ptr( TensorPtr& tensor, - const std::vector& sizes); + const std::vector& sizes); } // namespace extension } // namespace executorch diff --git a/extension/tensor/tensor_ptr_maker.h b/extension/tensor/tensor_ptr_maker.h index 3f2d267a4e..e3351fe37e 100644 --- a/extension/tensor/tensor_ptr_maker.h +++ b/extension/tensor/tensor_ptr_maker.h @@ -38,7 +38,7 @@ class TensorPtrMaker final { * @param type The scalar type (e.g., float, int, bool). * @return Rvalue to this TensorPtrMaker for method chaining. */ - TensorPtrMaker&& type(exec_aten::ScalarType type) { + TensorPtrMaker&& type(executorch::aten::ScalarType type) { type_ = type; return std::move(*this); } @@ -49,7 +49,8 @@ class TensorPtrMaker final { * @param dim_order A vector specifying the dimension order. * @return Rvalue to this TensorPtrMaker for method chaining. */ - TensorPtrMaker&& dim_order(std::vector dim_order) { + TensorPtrMaker&& dim_order( + std::vector dim_order) { dim_order_ = std::move(dim_order); return std::move(*this); } @@ -60,7 +61,7 @@ class TensorPtrMaker final { * @param strides A vector specifying the stride for each dimension. * @return Rvalue to this TensorPtrMaker for method chaining. */ - TensorPtrMaker&& strides(std::vector strides) { + TensorPtrMaker&& strides(std::vector strides) { strides_ = std::move(strides); return std::move(*this); } @@ -72,7 +73,7 @@ class TensorPtrMaker final { * bounded. * @return Rvalue to this TensorPtrMaker for method chaining. */ - TensorPtrMaker&& dynamism(exec_aten::TensorShapeDynamism dynamism) { + TensorPtrMaker&& dynamism(executorch::aten::TensorShapeDynamism dynamism) { dynamism_ = dynamism; return std::move(*this); } @@ -120,26 +121,26 @@ class TensorPtrMaker final { private: TensorPtrMaker( void* data, - std::vector sizes, - exec_aten::ScalarType type) + std::vector sizes, + executorch::aten::ScalarType type) : sizes_(std::move(sizes)), data_(data), type_(type) {} private: // The following properties are required to create a Tensor. friend TensorPtrMaker for_blob( void* data, - std::vector sizes, - exec_aten::ScalarType type); + std::vector sizes, + executorch::aten::ScalarType type); private: - std::vector sizes_; - std::vector strides_; - std::vector dim_order_; + std::vector sizes_; + std::vector strides_; + std::vector dim_order_; std::function deleter_ = nullptr; void* data_ = nullptr; - exec_aten::ScalarType type_ = exec_aten::ScalarType::Float; - exec_aten::TensorShapeDynamism dynamism_ = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND; + executorch::aten::ScalarType type_ = executorch::aten::ScalarType::Float; + executorch::aten::TensorShapeDynamism dynamism_ = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND; }; /** @@ -158,8 +159,8 @@ class TensorPtrMaker final { */ inline TensorPtrMaker for_blob( void* data, - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float) { return TensorPtrMaker(data, std::move(sizes), type); } @@ -180,10 +181,10 @@ inline TensorPtrMaker for_blob( */ inline TensorPtr from_blob( void* data, - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return for_blob(data, std::move(sizes), type) .dynamism(dynamism) .make_tensor_ptr(); @@ -208,11 +209,11 @@ inline TensorPtr from_blob( */ inline TensorPtr from_blob( void* data, - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return for_blob(data, std::move(sizes), type) .strides(std::move(strides)) .dynamism(dynamism) @@ -237,11 +238,11 @@ inline TensorPtr from_blob( */ inline TensorPtr from_blob( void* data, - std::vector sizes, - exec_aten::ScalarType type, + std::vector sizes, + executorch::aten::ScalarType type, std::function&& deleter, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return for_blob(data, std::move(sizes), type) .deleter(std::move(deleter)) .dynamism(dynamism) @@ -267,12 +268,12 @@ inline TensorPtr from_blob( */ inline TensorPtr from_blob( void* data, - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type, + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type, std::function&& deleter, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return for_blob(data, std::move(sizes), type) .strides(std::move(strides)) .deleter(std::move(deleter)) @@ -294,11 +295,11 @@ inline TensorPtr from_blob( * @return A TensorPtr instance managing the newly created Tensor. */ TensorPtr empty_strided( - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates an empty TensorPtr with the same size and properties as the given @@ -316,10 +317,10 @@ TensorPtr empty_strided( */ inline TensorPtr empty_like( const TensorPtr& other, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - if (type == exec_aten::ScalarType::Undefined) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + if (type == executorch::aten::ScalarType::Undefined) { type = other->scalar_type(); } return empty_strided( @@ -341,10 +342,10 @@ inline TensorPtr empty_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr empty( - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return empty_strided(std::move(sizes), {}, type, dynamism); } @@ -359,12 +360,12 @@ inline TensorPtr empty( * @return A TensorPtr instance managing the newly created Tensor. */ TensorPtr full_strided( - std::vector sizes, - std::vector strides, - exec_aten::Scalar fill_value, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector sizes, + std::vector strides, + executorch::aten::Scalar fill_value, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates a TensorPtr filled with the specified value, with the same size and @@ -380,11 +381,11 @@ TensorPtr full_strided( */ inline TensorPtr full_like( const TensorPtr& other, - exec_aten::Scalar fill_value, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - if (type == exec_aten::ScalarType::Undefined) { + executorch::aten::Scalar fill_value, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + if (type == executorch::aten::ScalarType::Undefined) { type = other->scalar_type(); } return full_strided( @@ -405,11 +406,11 @@ inline TensorPtr full_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr full( - std::vector sizes, - exec_aten::Scalar fill_value, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::Scalar fill_value, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return full_strided(std::move(sizes), {}, fill_value, type, dynamism); } @@ -421,8 +422,8 @@ inline TensorPtr full( * @return A TensorPtr instance managing the newly created scalar Tensor. */ inline TensorPtr scalar_tensor( - exec_aten::Scalar value, - exec_aten::ScalarType type = exec_aten::ScalarType::Float) { + executorch::aten::Scalar value, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float) { return full({}, value, type); } @@ -439,9 +440,9 @@ inline TensorPtr scalar_tensor( */ inline TensorPtr ones_like( const TensorPtr& other, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return full_like(other, 1, type, dynamism); } @@ -454,10 +455,10 @@ inline TensorPtr ones_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr ones( - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return full(std::move(sizes), 1, type, dynamism); } @@ -474,9 +475,9 @@ inline TensorPtr ones( */ inline TensorPtr zeros_like( const TensorPtr& other, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return full_like(other, 0, type, dynamism); } @@ -489,10 +490,10 @@ inline TensorPtr zeros_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr zeros( - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return full(std::move(sizes), 0, type, dynamism); } @@ -506,11 +507,11 @@ inline TensorPtr zeros( * @return A TensorPtr instance managing the newly created Tensor. **/ TensorPtr rand_strided( - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates a TensorPtr filled with random values between 0 and 1. @@ -524,10 +525,10 @@ TensorPtr rand_strided( */ inline TensorPtr rand_like( const TensorPtr& other, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - if (type == exec_aten::ScalarType::Undefined) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + if (type == executorch::aten::ScalarType::Undefined) { type = other->scalar_type(); } return rand_strided( @@ -546,10 +547,10 @@ inline TensorPtr rand_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr rand( - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return rand_strided(std::move(sizes), {}, type, dynamism); } @@ -564,11 +565,11 @@ inline TensorPtr rand( * @return A TensorPtr instance managing the newly created Tensor. */ TensorPtr randn_strided( - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates a TensorPtr filled with random values from a normal distribution. @@ -582,10 +583,10 @@ TensorPtr randn_strided( */ inline TensorPtr randn_like( const TensorPtr& other, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - if (type == exec_aten::ScalarType::Undefined) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + if (type == executorch::aten::ScalarType::Undefined) { type = other->scalar_type(); } return randn_strided( @@ -605,10 +606,10 @@ inline TensorPtr randn_like( * @return A TensorPtr instance managing the newly created Tensor. */ inline TensorPtr randn( - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Float, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Float, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return randn_strided(std::move(sizes), {}, type, dynamism); } @@ -626,11 +627,11 @@ inline TensorPtr randn( TensorPtr randint_strided( int64_t low, int64_t high, - std::vector sizes, - std::vector strides, - exec_aten::ScalarType type = exec_aten::ScalarType::Int, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); + std::vector sizes, + std::vector strides, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Int, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND); /** * Creates a TensorPtr filled with random integer values in the given range. @@ -648,10 +649,10 @@ inline TensorPtr randint_like( const TensorPtr& other, int64_t low, int64_t high, - exec_aten::ScalarType type = exec_aten::ScalarType::Undefined, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { - if (type == exec_aten::ScalarType::Undefined) { + executorch::aten::ScalarType type = executorch::aten::ScalarType::Undefined, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { + if (type == executorch::aten::ScalarType::Undefined) { type = other->scalar_type(); } return randint_strided( @@ -677,10 +678,10 @@ inline TensorPtr randint_like( inline TensorPtr randint( int64_t low, int64_t high, - std::vector sizes, - exec_aten::ScalarType type = exec_aten::ScalarType::Int, - exec_aten::TensorShapeDynamism dynamism = - exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + std::vector sizes, + executorch::aten::ScalarType type = executorch::aten::ScalarType::Int, + executorch::aten::TensorShapeDynamism dynamism = + executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) { return randint_strided(low, high, std::move(sizes), {}, type, dynamism); } diff --git a/extension/training/module/training_module.h b/extension/training/module/training_module.h index ade3f6f2f4..b31463a68f 100644 --- a/extension/training/module/training_module.h +++ b/extension/training/module/training_module.h @@ -71,7 +71,8 @@ class ET_EXPERIMENTAL TrainingModule final : executorch::extension::Module { * parameter tensor, or an error if the method is not a joint graph. */ ET_EXPERIMENTAL - runtime::Result> + runtime::Result< + const std::map> named_parameters(const std::string& method_name); /** @@ -86,13 +87,14 @@ class ET_EXPERIMENTAL TrainingModule final : executorch::extension::Module { * or has not been executed yet. */ ET_EXPERIMENTAL - runtime::Result> + runtime::Result< + const std::map> named_gradients(const std::string& method_name); private: std::unordered_map< std::string, - std::map> + std::map> method_named_gradients_; }; diff --git a/extension/training/optimizer/sgd.h b/extension/training/optimizer/sgd.h index 00e84b1348..055d561287 100644 --- a/extension/training/optimizer/sgd.h +++ b/extension/training/optimizer/sgd.h @@ -40,15 +40,15 @@ class ET_EXPERIMENTAL SGDParamState { * @param[in] momentum_buffer A tensor that stores the momentum at the last * epoch. */ - explicit SGDParamState(exec_aten::Tensor& momentum_buffer) + explicit SGDParamState(executorch::aten::Tensor& momentum_buffer) : momentum_buffer_(momentum_buffer) {} - exec_aten::Tensor& momentum_buffer() { + executorch::aten::Tensor& momentum_buffer() { return momentum_buffer_; } private: - exec_aten::Tensor momentum_buffer_; + executorch::aten::Tensor momentum_buffer_; }; /** @@ -151,11 +151,11 @@ class ET_EXPERIMENTAL SGDParamGroup { * qualified names. */ /* implicit */ SGDParamGroup( - const std::map& + const std::map& named_parameters) : named_parameters_(named_parameters) {} SGDParamGroup( - const std::map& + const std::map& named_parameters, std::unique_ptr options) : named_parameters_(named_parameters), options_(std::move(options)) {} @@ -164,11 +164,12 @@ class ET_EXPERIMENTAL SGDParamGroup { SGDOptions& options(); const SGDOptions& options() const; void set_options(std::unique_ptr options); - const std::map& named_parameters() - const; + const std::map& + named_parameters() const; private: - std::map named_parameters_; + std::map + named_parameters_; std::unique_ptr options_; }; @@ -188,7 +189,7 @@ class ET_EXPERIMENTAL SGD { } explicit SGD( - const std::map& + const std::map& named_parameters, SGDOptions defaults) : SGD({SGDParamGroup(named_parameters)}, defaults) {} @@ -205,7 +206,7 @@ class ET_EXPERIMENTAL SGD { * fully qualified name. */ ::executorch::runtime::Error step( - const std::map& + const std::map& named_gradients); private: