From f36ea11075efe4c30f67c6614c914da6c6ed33e6 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Sun, 15 Sep 2024 11:15:38 -0700 Subject: [PATCH] Move type arg to the end to match Aten constructors. (#5379) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/5379 . Differential Revision: D62701089 --- extension/tensor/tensor_impl_ptr.cpp | 8 +- extension/tensor/tensor_impl_ptr.h | 171 ++++++++++++++++-- extension/tensor/tensor_ptr.h | 148 ++++++++++++--- extension/tensor/tensor_ptr_maker.cpp | 2 +- extension/tensor/tensor_ptr_maker.h | 2 +- .../tensor/test/tensor_impl_ptr_test.cpp | 63 ++----- extension/tensor/test/tensor_ptr_test.cpp | 24 ++- 7 files changed, 314 insertions(+), 104 deletions(-) diff --git a/extension/tensor/tensor_impl_ptr.cpp b/extension/tensor/tensor_impl_ptr.cpp index cbc49299b9f..358acfd1850 100644 --- a/extension/tensor/tensor_impl_ptr.cpp +++ b/extension/tensor/tensor_impl_ptr.cpp @@ -54,11 +54,11 @@ struct TensorImplPtrDeleter final { } // namespace TensorImplPtr make_tensor_impl_ptr( - exec_aten::ScalarType type, std::vector sizes, void* data, std::vector dim_order, std::vector strides, + exec_aten::ScalarType type, exec_aten::TensorShapeDynamism dynamism, std::function deleter) { const auto dim = sizes.size(); @@ -129,24 +129,24 @@ TensorImplPtr make_tensor_impl_ptr( } TensorImplPtr make_tensor_impl_ptr( - exec_aten::ScalarType scalar_type, std::vector sizes, std::vector data, std::vector dim_order, std::vector strides, + exec_aten::ScalarType type, exec_aten::TensorShapeDynamism dynamism) { ET_CHECK_MSG( data.size() >= exec_aten::compute_numel(sizes.data(), sizes.size()) * - exec_aten::elementSize(scalar_type), + exec_aten::elementSize(type), "Data size is smaller than required by sizes and scalar type."); auto raw_data_ptr = data.data(); auto data_ptr = std::make_shared>(std::move(data)); return make_tensor_impl_ptr( - scalar_type, std::move(sizes), raw_data_ptr, std::move(dim_order), std::move(strides), + type, dynamism, [data_ptr = std::move(data_ptr)](void*) {}); } diff --git a/extension/tensor/tensor_impl_ptr.h b/extension/tensor/tensor_impl_ptr.h index 83bf534d73d..0f523a19eac 100644 --- a/extension/tensor/tensor_impl_ptr.h +++ b/extension/tensor/tensor_impl_ptr.h @@ -48,11 +48,11 @@ using TensorImplPtr = * Creates a TensorImplPtr that manages a newly created TensorImpl with the * specified properties. * - * @param type The scalar type of the tensor elements. * @param sizes A vector specifying the size of each dimension. * @param data A pointer to the data buffer. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @param deleter A custom deleter function for managing the lifetime of the * data buffer. If provided, this deleter will be called when the managed @@ -60,15 +60,39 @@ using TensorImplPtr = * @return A TensorImplPtr managing the newly created TensorImpl. */ TensorImplPtr make_tensor_impl_ptr( - exec_aten::ScalarType type, std::vector sizes, void* data, - std::vector dim_order = {}, - std::vector strides = {}, + 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::function deleter = nullptr); +/** + * Creates a TensorImplPtr that manages a newly created TensorImpl with the + * specified properties. + * + * @param sizes A vector specifying the size of each dimension. + * @param data A pointer to the data buffer. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @param deleter A custom deleter function for managing the lifetime of the + * data buffer. If provided, this deleter will be called when the managed + * TensorImpl object is destroyed. + * @return A TensorImplPtr managing the newly created TensorImpl. + */ +inline TensorImplPtr make_tensor_impl_ptr( + std::vector sizes, + void* data, + exec_aten::ScalarType type = exec_aten::ScalarType::Float, + exec_aten::TensorShapeDynamism dynamism = + exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, + std::function deleter = nullptr) { + return make_tensor_impl_ptr( + std::move(sizes), data, {}, {}, type, dynamism, std::move(deleter)); +} + /** * Creates a TensorImplPtr that manages a newly created TensorImpl with the * specified properties. @@ -83,27 +107,30 @@ TensorImplPtr make_tensor_impl_ptr( * @param data A vector containing the tensor's data. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorImplPtr that manages the newly created TensorImpl. */ -template +template < + typename T = float, + exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> inline TensorImplPtr make_tensor_impl_ptr( 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) { - constexpr exec_aten::ScalarType scalar_type = - runtime::CppTypeToScalarType::value; + ET_CHECK_MSG(type == deduced_type, "Type does not match the deduced type."); const auto raw_data_ptr = data.data(); auto data_ptr = std::make_shared>(std::move(data)); return make_tensor_impl_ptr( - scalar_type, std::move(sizes), raw_data_ptr, std::move(dim_order), std::move(strides), + type, dynamism, [data_ptr = std::move(data_ptr)](void*) {}); } @@ -119,17 +146,109 @@ inline TensorImplPtr make_tensor_impl_ptr( * * @tparam T The C++ type of the tensor elements, deduced from the vector. * @param data A vector containing the tensor's data. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorImplPtr that manages the newly created TensorImpl. */ -template +template < + typename T = float, + exec_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) { + ET_CHECK_MSG(type == deduced_type, "Type does not match the deduced type."); std::vector sizes{exec_aten::SizesType(data.size())}; return make_tensor_impl_ptr( - std::move(sizes), std::move(data), {0}, {1}, dynamism); + std::move(sizes), std::move(data), {0}, {1}, type, dynamism); +} + +/** + * Creates a TensorImplPtr that manages a newly created TensorImpl with the + * specified properties. + * + * This template overload is specialized for cases where the tensor data is + * provided as an initializer list. The scalar type is automatically deduced + * from the initializer list's data type. The deleter ensures that the data is + * properly managed and its lifetime is tied to the TensorImpl. + * + * @tparam T The C++ type of the tensor elements, deduced from the initializer + * list. + * @param sizes A vector specifying the size of each dimension. + * @param list An initializer list containing the tensor's data. + * @param dim_order A vector specifying the order of dimensions. + * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @return A TensorImplPtr that manages the newly created TensorImpl. + */ +template < + typename T = float, + exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> +inline TensorImplPtr make_tensor_impl_ptr( + 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) { + ET_CHECK_MSG(type == deduced_type, "Type does not match the deduced type."); + auto data = std::vector(std::move(list)); + const auto raw_data_ptr = data.data(); + auto data_ptr = std::make_shared>(std::move(data)); + return make_tensor_impl_ptr( + std::move(sizes), + raw_data_ptr, + std::move(dim_order), + std::move(strides), + type, + dynamism, + [data_ptr = std::move(data_ptr)](void*) {}); +} + +/** + * Creates a TensorImplPtr that manages a newly created TensorImpl with the + * specified properties. + * + * This template overload is specialized for cases where the tensor data is + * provided as an initializer list. The scalar type is automatically deduced + * from the initializer list's data type. The deleter ensures that the data is + * properly managed and its lifetime is tied to the TensorImpl. + * + * @tparam T The C++ type of the tensor elements, deduced from the initializer + * list. + * @param sizes A vector specifying the size of each dimension. + * @param list An initializer list containing the tensor's data. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @return A TensorImplPtr that manages the newly created TensorImpl. + */ +template < + typename T = float, + exec_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) { + ET_CHECK_MSG(type == deduced_type, "Type does not match the deduced type."); + std::vector sizes{exec_aten::SizesType(list.size())}; + return make_tensor_impl_ptr( + std::move(sizes), std::move(list), {0}, {1}, type, dynamism); +} + +/** + * Creates a TensorPtr that manages a Tensor with a single scalar value. + * + * @tparam T The C++ type of the scalar value. + * @param value The scalar value to be used for the Tensor. + * @return A TensorPtr that manages the newly created TensorImpl. + */ +template +inline TensorImplPtr make_tensor_impl_ptr(T value) { + return make_tensor_impl_ptr({}, std::vector{value}); } /** @@ -140,22 +259,46 @@ inline TensorImplPtr make_tensor_impl_ptr( * and a scalar type to interpret the data. The vector is managed, and the * memory's lifetime is tied to the TensorImpl. * - * @param scalar_type The scalar type of the tensor elements. * @param sizes A vector specifying the size of each dimension. * @param data A vector containing the raw memory for the tensor's data. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorImplPtr managing the newly created TensorImpl. */ TensorImplPtr make_tensor_impl_ptr( - exec_aten::ScalarType scalar_type, std::vector sizes, std::vector data, - std::vector dim_order = {}, - std::vector strides = {}, + std::vector dim_order, + std::vector strides, + exec_aten::ScalarType type = exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism dynamism = exec_aten::TensorShapeDynamism::DYNAMIC_BOUND); +/** + * Creates a TensorImplPtr that manages a newly created TensorImpl with the + * specified properties. + * + * This overload accepts a raw memory buffer stored in a std::vector + * and a scalar type to interpret the data. The vector is managed, and the + * memory's lifetime is tied to the TensorImpl. + * + * @param sizes A vector specifying the size of each dimension. + * @param data A vector containing the raw memory for the tensor's data. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @return A TensorImplPtr managing the newly created TensorImpl. + */ +inline TensorImplPtr make_tensor_impl_ptr( + std::vector sizes, + std::vector data, + exec_aten::ScalarType type = exec_aten::ScalarType::Float, + exec_aten::TensorShapeDynamism dynamism = + exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + return make_tensor_impl_ptr( + std::move(sizes), std::move(data), {}, {}, type, dynamism); +} + } // namespace extension } // namespace executorch diff --git a/extension/tensor/tensor_ptr.h b/extension/tensor/tensor_ptr.h index 17e18742bec..626c3579870 100644 --- a/extension/tensor/tensor_ptr.h +++ b/extension/tensor/tensor_ptr.h @@ -115,7 +115,6 @@ inline TensorPtr make_tensor_ptr(const TensorPtr& tensor) { */ inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { return make_tensor_ptr(make_tensor_impl_ptr( - tensor.scalar_type(), std::vector( tensor.sizes().begin(), tensor.sizes().end()), tensor.mutable_data_ptr(), @@ -124,11 +123,13 @@ inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { tensor.dim_order().begin(), tensor.dim_order().end()), std::vector( tensor.strides().begin(), tensor.strides().end()), + tensor.scalar_type(), tensor.shape_dynamism() #else // USE_ATEN_LIB {}, std::vector( - tensor.strides().begin(), tensor.strides().end()) + tensor.strides().begin(), tensor.strides().end()), + tensor.scalar_type() #endif // USE_ATEN_LIB )); } @@ -136,11 +137,11 @@ inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { /** * Creates a TensorPtr that manages a Tensor with the specified properties. * - * @param type The scalar type of the tensor elements. * @param sizes A vector specifying the size of each dimension. * @param data A pointer to the data buffer. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of the tensor. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @param deleter A custom deleter function for managing the lifetime of the * data buffer. If provided, this deleter will be called when the managed Tensor @@ -148,24 +149,47 @@ inline TensorPtr make_tensor_ptr(const exec_aten::Tensor& tensor) { * @return A TensorPtr that manages the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - const exec_aten::ScalarType type, std::vector sizes, void* data, - std::vector dim_order = {}, - std::vector strides = {}, + 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::function deleter = nullptr) { return make_tensor_ptr(make_tensor_impl_ptr( - type, std::move(sizes), data, std::move(dim_order), std::move(strides), + type, dynamism, std::move(deleter))); } +/** + * Creates a TensorPtr that manages a Tensor with the specified properties. + * + * @param sizes A vector specifying the size of each dimension. + * @param data A pointer to the data buffer. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @param deleter A custom deleter function for managing the lifetime of the + * data buffer. If provided, this deleter will be called when the managed Tensor + * object is destroyed. + * @return A TensorPtr that manages the newly created Tensor. + */ +inline TensorPtr make_tensor_ptr( + std::vector sizes, + void* data, + const exec_aten::ScalarType type = exec_aten::ScalarType::Float, + const exec_aten::TensorShapeDynamism dynamism = + exec_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))); +} + /** * Creates a TensorPtr that manages a Tensor with the specified properties. * @@ -178,15 +202,19 @@ inline TensorPtr make_tensor_ptr( * @param data A vector containing the tensor's data. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorPtr that manages the newly created TensorImpl. */ -template -TensorPtr make_tensor_ptr( +template < + typename T = float, + exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> +inline TensorPtr make_tensor_ptr( 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) { return make_tensor_ptr(make_tensor_impl_ptr( @@ -194,6 +222,7 @@ TensorPtr make_tensor_ptr( std::move(data), std::move(dim_order), std::move(strides), + type, dynamism)); } @@ -209,12 +238,52 @@ TensorPtr make_tensor_ptr( * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorPtr that manages the newly created TensorImpl. */ -template -TensorPtr make_tensor_ptr( +template < + typename T = float, + exec_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) { + return make_tensor_ptr(make_tensor_impl_ptr(std::move(data), type, dynamism)); +} + +/** + * Creates a TensorPtr that manages a Tensor with the specified properties. + * + * This template overload is specialized for cases where the tensor data is + * provided as an initializer list. The scalar type is automatically deduced + * from the initializer list's data type. + * + * @tparam T The C++ type of the tensor elements, deduced from the initializer + * list. + * @param sizes A vector specifying the size of each dimension. + * @param list An initializer list containing the tensor's data. + * @param dim_order A vector specifying the order of dimensions. + * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @return A TensorPtr that manages the newly created TensorImpl. + */ +template < + typename T = float, + exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType::value> +inline TensorPtr make_tensor_ptr( + 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) { - return make_tensor_ptr(make_tensor_impl_ptr(std::move(data), dynamism)); + return make_tensor_ptr(make_tensor_impl_ptr( + std::move(sizes), + std::move(list), + std::move(dim_order), + std::move(strides), + type, + dynamism)); } /** @@ -226,16 +295,19 @@ TensorPtr make_tensor_ptr( * * @tparam T The C++ type of the tensor elements, deduced from the initializer * list. - * @param data An initializer list containing the tensor's data. + * @param list An initializer list containing the tensor's data. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorPtr that manages the newly created TensorImpl. */ -template -TensorPtr make_tensor_ptr( - std::initializer_list data, +template < + typename T = float, + exec_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) { - return make_tensor_ptr(make_tensor_impl_ptr(std::vector(data), dynamism)); + return make_tensor_ptr(make_tensor_impl_ptr(std::move(list), type, dynamism)); } /** @@ -246,8 +318,8 @@ TensorPtr make_tensor_ptr( * @return A TensorPtr that manages the newly created TensorImpl. */ template -TensorPtr make_tensor_ptr(T value) { - return make_tensor_ptr(make_tensor_impl_ptr({}, std::vector{value})); +inline TensorPtr make_tensor_ptr(T value) { + return make_tensor_ptr(make_tensor_impl_ptr(value)); } /** @@ -257,31 +329,54 @@ TensorPtr make_tensor_ptr(T value) { * and a scalar type to interpret the data. The vector is managed, and the * memory's lifetime is tied to the TensorImpl. * - * @param scalar_type The scalar type of the tensor elements. * @param sizes A vector specifying the size of each dimension. * @param data A vector containing the raw memory for the tensor's data. * @param dim_order A vector specifying the order of dimensions. * @param strides A vector specifying the strides of each dimension. + * @param type The scalar type of the tensor elements. * @param dynamism Specifies the mutability of the tensor's shape. * @return A TensorPtr managing the newly created Tensor. */ inline TensorPtr make_tensor_ptr( - exec_aten::ScalarType scalar_type, std::vector sizes, std::vector data, - std::vector dim_order = {}, - std::vector strides = {}, + std::vector dim_order, + std::vector strides, + exec_aten::ScalarType type = exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism dynamism = exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { return make_tensor_ptr(make_tensor_impl_ptr( - scalar_type, std::move(sizes), std::move(data), std::move(dim_order), std::move(strides), + type, dynamism)); } +/** + * Creates a TensorPtr that manages a Tensor with the specified properties. + * + * This overload accepts a raw memory buffer stored in a std::vector + * and a scalar type to interpret the data. The vector is managed, and the + * memory's lifetime is tied to the TensorImpl. + * + * @param sizes A vector specifying the size of each dimension. + * @param data A vector containing the raw memory for the tensor's data. + * @param type The scalar type of the tensor elements. + * @param dynamism Specifies the mutability of the tensor's shape. + * @return A TensorPtr managing the newly created Tensor. + */ +inline TensorPtr make_tensor_ptr( + std::vector sizes, + std::vector data, + exec_aten::ScalarType type = exec_aten::ScalarType::Float, + exec_aten::TensorShapeDynamism dynamism = + exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) { + return make_tensor_ptr( + make_tensor_impl_ptr(std::move(sizes), std::move(data), type, dynamism)); +} + /** * Creates a TensorPtr that manages a new Tensor with the same properties * as the given Tensor, but with a copy of the data owned by the returned @@ -293,7 +388,6 @@ inline TensorPtr make_tensor_ptr( */ inline TensorPtr clone_tensor_ptr(const exec_aten::Tensor& tensor) { return make_tensor_ptr(make_tensor_impl_ptr( - tensor.scalar_type(), std::vector( tensor.sizes().begin(), tensor.sizes().end()), std::vector( @@ -304,11 +398,13 @@ inline TensorPtr clone_tensor_ptr(const exec_aten::Tensor& tensor) { tensor.dim_order().begin(), tensor.dim_order().end()), std::vector( tensor.strides().begin(), tensor.strides().end()), + tensor.scalar_type(), tensor.shape_dynamism() #else // USE_ATEN_LIB {}, std::vector( - tensor.strides().begin(), tensor.strides().end()) + tensor.strides().begin(), tensor.strides().end()), + tensor.scalar_type() #endif // USE_ATEN_LIB )); } diff --git a/extension/tensor/tensor_ptr_maker.cpp b/extension/tensor/tensor_ptr_maker.cpp index 1a09fea4cac..cbea6da1e74 100644 --- a/extension/tensor/tensor_ptr_maker.cpp +++ b/extension/tensor/tensor_ptr_maker.cpp @@ -105,11 +105,11 @@ TensorPtr empty_strided( exec_aten::compute_numel(sizes.data(), sizes.size()) * exec_aten::elementSize(type)); return make_tensor_ptr( - type, std::move(sizes), std::move(data), {}, std::move(strides), + type, dynamism); } diff --git a/extension/tensor/tensor_ptr_maker.h b/extension/tensor/tensor_ptr_maker.h index 8146135b153..538072fce5d 100644 --- a/extension/tensor/tensor_ptr_maker.h +++ b/extension/tensor/tensor_ptr_maker.h @@ -99,11 +99,11 @@ class TensorPtrMaker final { */ TensorPtr make_tensor_ptr() && { return ::executorch::extension::make_tensor_ptr( - type_, std::move(sizes_), data_, std::move(dim_order_), std::move(strides_), + type_, dynamism_, std::move(deleter_)); } diff --git a/extension/tensor/test/tensor_impl_ptr_test.cpp b/extension/tensor/test/tensor_impl_ptr_test.cpp index 642e8738705..b345258c2c6 100644 --- a/extension/tensor/test/tensor_impl_ptr_test.cpp +++ b/extension/tensor/test/tensor_impl_ptr_test.cpp @@ -25,8 +25,7 @@ class TensorImplPtrTest : public ::testing::Test { TEST_F(TensorImplPtrTest, ScalarTensorCreation) { float scalar_data = 3.14f; - auto tensor_impl = - make_tensor_impl_ptr(exec_aten::ScalarType::Float, {}, &scalar_data); + auto tensor_impl = make_tensor_impl_ptr({}, &scalar_data); EXPECT_EQ(tensor_impl->numel(), 1); EXPECT_EQ(tensor_impl->dim(), 0); @@ -48,8 +47,7 @@ TEST_F(TensorImplPtrTest, ScalarTensorOwningData) { TEST_F(TensorImplPtrTest, TensorImplCreation) { float data[20] = {2}; - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {4, 5}, data, {0, 1}, {5, 1}); + auto tensor_impl = make_tensor_impl_ptr({4, 5}, data, {0, 1}, {5, 1}); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 4); @@ -63,8 +61,7 @@ TEST_F(TensorImplPtrTest, TensorImplCreation) { TEST_F(TensorImplPtrTest, TensorImplSharedOwnership) { float data[20] = {2}; - auto tensor_impl1 = - make_tensor_impl_ptr(exec_aten::ScalarType::Float, {4, 5}, data); + auto tensor_impl1 = make_tensor_impl_ptr({4, 5}, data); auto tensor_impl2 = tensor_impl1; EXPECT_EQ(tensor_impl1.get(), tensor_impl2.get()); @@ -77,8 +74,7 @@ TEST_F(TensorImplPtrTest, TensorImplSharedOwnership) { TEST_F(TensorImplPtrTest, TensorImplInferredDimOrderAndStrides) { float data[12] = {0}; - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {}, {4, 1}); + auto tensor_impl = make_tensor_impl_ptr({3, 4}, data, {}, {4, 1}); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 3); @@ -90,8 +86,7 @@ TEST_F(TensorImplPtrTest, TensorImplInferredDimOrderAndStrides) { TEST_F(TensorImplPtrTest, TensorImplInferredDimOrderCustomStrides) { float data[12] = {0}; - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {}, {1, 3}); + auto tensor_impl = make_tensor_impl_ptr({3, 4}, data, {}, {1, 3}); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 3); @@ -102,8 +97,7 @@ TEST_F(TensorImplPtrTest, TensorImplInferredDimOrderCustomStrides) { TEST_F(TensorImplPtrTest, TensorImplDefaultDimOrderAndStrides) { float data[24] = {0}; - auto tensor_impl = - make_tensor_impl_ptr(exec_aten::ScalarType::Float, {2, 3, 4}, data); + auto tensor_impl = make_tensor_impl_ptr({2, 3, 4}, data); EXPECT_EQ(tensor_impl->dim(), 3); EXPECT_EQ(tensor_impl->size(0), 2); @@ -117,17 +111,12 @@ TEST_F(TensorImplPtrTest, TensorImplDefaultDimOrderAndStrides) { TEST_F(TensorImplPtrTest, TensorImplMismatchStridesAndDimOrder) { float data[12] = {0}; ET_EXPECT_DEATH( - { - auto _ = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {1, 0}, {1, 4}); - }, - ""); + { auto _ = make_tensor_impl_ptr({3, 4}, data, {1, 0}, {1, 4}); }, ""); } TEST_F(TensorImplPtrTest, TensorImplCustomDimOrderAndStrides) { float data[12] = {0}; - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {1, 0}, {1, 3}); + auto tensor_impl = make_tensor_impl_ptr({3, 4}, data, {1, 0}, {1, 3}); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 3); @@ -140,16 +129,14 @@ TEST_F(TensorImplPtrTest, TensorImplInvalidDimOrder) { ET_EXPECT_DEATH( { float data[20] = {2}; - auto _ = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {4, 5}, data, {2, 1}); + auto _ = make_tensor_impl_ptr({4, 5}, data, {2, 1}, {1, 4}); }, ""); } TEST_F(TensorImplPtrTest, TensorImplCustomDeleter) { float data[20] = {4}; - auto tensor_impl = - make_tensor_impl_ptr(exec_aten::ScalarType::Float, {4, 5}, data); + auto tensor_impl = make_tensor_impl_ptr({4, 5}, data); TensorImplPtr copied_tensor_impl = tensor_impl; EXPECT_EQ(tensor_impl.use_count(), copied_tensor_impl.use_count()); @@ -163,11 +150,11 @@ TEST_F(TensorImplPtrTest, TensorImplDataDeleterReleasesCapturedSharedPtr) { std::shared_ptr data_ptr( new float[10], [](float* ptr) { delete[] ptr; }); auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {4, 5}, data_ptr.get(), {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, [data_ptr, &deleter_called](void*) mutable { deleter_called = true; }); @@ -275,8 +262,7 @@ TEST_F(TensorImplPtrTest, TensorImplAmbiguityWithMixedVectors) { TEST_F(TensorImplPtrTest, SharedDataManagement) { auto data = std::make_shared>(100, 1.0f); - auto tensor_impl1 = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {10, 10}, data->data()); + auto tensor_impl1 = make_tensor_impl_ptr({10, 10}, data->data()); auto tensor_impl2 = tensor_impl1; EXPECT_EQ(tensor_impl1.get(), tensor_impl2.get()); @@ -298,11 +284,11 @@ TEST_F(TensorImplPtrTest, CustomDeleterWithSharedData) { bool deleter_called = false; { auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {10, 10}, data->data(), {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, [data, &deleter_called](void*) mutable { deleter_called = true; @@ -339,8 +325,7 @@ TEST_F(TensorImplPtrTest, TensorImplUint8BufferWithFloatScalarType) { float_data[2] = 3.0f; float_data[3] = 4.0f; - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {2, 2}, std::move(data)); + auto tensor_impl = make_tensor_impl_ptr({2, 2}, std::move(data)); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 2); @@ -358,18 +343,14 @@ TEST_F(TensorImplPtrTest, TensorImplUint8BufferTooSmallExpectDeath) { std::vector data( 2 * exec_aten::elementSize(exec_aten::ScalarType::Float)); ET_EXPECT_DEATH( - { - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {2, 2}, std::move(data)); - }, + { auto tensor_impl = make_tensor_impl_ptr({2, 2}, std::move(data)); }, ""); } TEST_F(TensorImplPtrTest, TensorImplUint8BufferTooLarge) { std::vector data( 4 * exec_aten::elementSize(exec_aten::ScalarType::Float)); - auto tensor_impl = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {2, 2}, std::move(data)); + auto tensor_impl = make_tensor_impl_ptr({2, 2}, std::move(data)); EXPECT_EQ(tensor_impl->dim(), 2); EXPECT_EQ(tensor_impl->size(0), 2); @@ -381,15 +362,7 @@ TEST_F(TensorImplPtrTest, TensorImplUint8BufferTooLarge) { TEST_F(TensorImplPtrTest, StridesAndDimOrderMustMatchSizes) { float data[12] = {0}; ET_EXPECT_DEATH( - { - auto _ = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {}, {1}); - }, - ""); + { auto _ = make_tensor_impl_ptr({3, 4}, data, {}, {1}); }, ""); ET_EXPECT_DEATH( - { - auto _ = make_tensor_impl_ptr( - exec_aten::ScalarType::Float, {3, 4}, data, {0}, {4, 1}); - }, - ""); + { auto _ = make_tensor_impl_ptr({3, 4}, data, {0}, {4, 1}); }, ""); } diff --git a/extension/tensor/test/tensor_ptr_test.cpp b/extension/tensor/test/tensor_ptr_test.cpp index 7fabf9ab8f9..00614f24eb7 100644 --- a/extension/tensor/test/tensor_ptr_test.cpp +++ b/extension/tensor/test/tensor_ptr_test.cpp @@ -24,7 +24,7 @@ class TensorPtrTest : public ::testing::Test { TEST_F(TensorPtrTest, ScalarTensorCreation) { float scalar_data = 3.14f; - auto tensor = make_tensor_ptr(exec_aten::ScalarType::Float, {}, &scalar_data); + auto tensor = make_tensor_ptr({}, &scalar_data); EXPECT_EQ(tensor->numel(), 1); EXPECT_EQ(tensor->dim(), 0); @@ -80,8 +80,7 @@ TEST_F(TensorPtrTest, ScalarTensorSingleValueCreation) { TEST_F(TensorPtrTest, CreateTensorWithStridesAndDimOrder) { float data[20] = {2}; - auto tensor = make_tensor_ptr( - exec_aten::ScalarType::Float, {4, 5}, data, {0, 1}, {5, 1}); + auto tensor = make_tensor_ptr({4, 5}, data, {0, 1}, {5, 1}); EXPECT_EQ(tensor->dim(), 2); EXPECT_EQ(tensor->size(0), 4); EXPECT_EQ(tensor->size(1), 5); @@ -93,7 +92,7 @@ TEST_F(TensorPtrTest, CreateTensorWithStridesAndDimOrder) { TEST_F(TensorPtrTest, TensorSharingImpl) { float data[20] = {2}; - auto tensor1 = make_tensor_ptr(exec_aten::ScalarType::Float, {4, 5}, data); + auto tensor1 = make_tensor_ptr({4, 5}, data); auto tensor2 = make_tensor_ptr(tensor1); EXPECT_EQ(tensor1->unsafeGetTensorImpl(), tensor2->unsafeGetTensorImpl()); } @@ -103,8 +102,7 @@ TEST_F(TensorPtrTest, TensorImplLifetime) { EXPECT_EQ(tensor, nullptr); { float data[20] = {2}; - auto tensor_impl = - make_tensor_impl_ptr(exec_aten::ScalarType::Float, {4, 5}, data); + auto tensor_impl = make_tensor_impl_ptr({4, 5}, data); tensor = make_tensor_ptr(tensor_impl); } EXPECT_EQ(tensor->dim(), 2); @@ -114,10 +112,10 @@ TEST_F(TensorPtrTest, TensorImplLifetime) { TEST_F(TensorPtrTest, TensorWithZeroDimensionAndElements) { float data[20] = {2}; - auto tensor = make_tensor_ptr(exec_aten::ScalarType::Float, {}, data); + auto tensor = make_tensor_ptr({}, data); EXPECT_EQ(tensor->dim(), 0); EXPECT_EQ(tensor->numel(), 1); - tensor = make_tensor_ptr(exec_aten::ScalarType::Float, {0, 5}, data); + tensor = make_tensor_ptr({0, 5}, data); EXPECT_EQ(tensor->dim(), 2); EXPECT_EQ(tensor->numel(), 0); } @@ -125,11 +123,11 @@ TEST_F(TensorPtrTest, TensorWithZeroDimensionAndElements) { TEST_F(TensorPtrTest, TensorResize) { float data[20] = {2}; auto tensor = make_tensor_ptr( - exec_aten::ScalarType::Float, {4, 5}, data, {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_UNBOUND); EXPECT_EQ(resize_tensor_ptr(tensor, {5, 4}), Error::Ok); EXPECT_EQ(tensor->size(0), 5); @@ -138,7 +136,7 @@ TEST_F(TensorPtrTest, TensorResize) { TEST_F(TensorPtrTest, TensorDataAccess) { float data[6] = {1, 2, 3, 4, 5, 6}; - auto tensor = make_tensor_ptr(exec_aten::ScalarType::Float, {2, 3}, data); + auto tensor = make_tensor_ptr({2, 3}, data); EXPECT_EQ(tensor->const_data_ptr()[0], 1); EXPECT_EQ(tensor->const_data_ptr()[5], 6); tensor->mutable_data_ptr()[0] = 10; @@ -149,11 +147,11 @@ TEST_F(TensorPtrTest, TensorWithCustomDataDeleter) { auto deleter_called = false; float* data = new float[20](); auto tensor = make_tensor_ptr( - exec_aten::ScalarType::Float, {4, 5}, data, {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, [&deleter_called](void* ptr) { deleter_called = true; @@ -169,11 +167,11 @@ TEST_F(TensorPtrTest, TensorManagesMovedVector) { std::vector data(20, 3.0f); auto* data_ptr = data.data(); auto tensor = make_tensor_ptr( - exec_aten::ScalarType::Float, {4, 5}, data_ptr, {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, [moved_data = std::move(data), &deleter_called](void*) mutable { deleter_called = true; @@ -191,11 +189,11 @@ TEST_F(TensorPtrTest, TensorDeleterReleasesCapturedSharedPtr) { std::shared_ptr data_ptr( new float[10], [](float* ptr) { delete[] ptr; }); auto tensor = make_tensor_ptr( - exec_aten::ScalarType::Float, {4, 5}, data_ptr.get(), {}, {}, + exec_aten::ScalarType::Float, exec_aten::TensorShapeDynamism::DYNAMIC_BOUND, [data_ptr, &deleter_called](void*) mutable { deleter_called = true; });