Skip to content

Commit

Permalink
Move type arg to the end to match Aten constructors. (#5379)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #5379

.

Differential Revision: D62701089
  • Loading branch information
shoumikhin authored and facebook-github-bot committed Sep 15, 2024
1 parent 18f97b2 commit f36ea11
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 104 deletions.
8 changes: 4 additions & 4 deletions extension/tensor/tensor_impl_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ struct TensorImplPtrDeleter final {
} // namespace

TensorImplPtr make_tensor_impl_ptr(
exec_aten::ScalarType type,
std::vector<exec_aten::SizesType> sizes,
void* data,
std::vector<exec_aten::DimOrderType> dim_order,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type,
exec_aten::TensorShapeDynamism dynamism,
std::function<void(void*)> deleter) {
const auto dim = sizes.size();
Expand Down Expand Up @@ -129,24 +129,24 @@ TensorImplPtr make_tensor_impl_ptr(
}

TensorImplPtr make_tensor_impl_ptr(
exec_aten::ScalarType scalar_type,
std::vector<exec_aten::SizesType> sizes,
std::vector<uint8_t> data,
std::vector<exec_aten::DimOrderType> dim_order,
std::vector<exec_aten::StridesType> 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::vector<uint8_t>>(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*) {});
}
Expand Down
171 changes: 157 additions & 14 deletions extension/tensor/tensor_impl_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,51 @@ 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
* TensorImpl object is destroyed.
* @return A TensorImplPtr managing the newly created TensorImpl.
*/
TensorImplPtr make_tensor_impl_ptr(
exec_aten::ScalarType type,
std::vector<exec_aten::SizesType> sizes,
void* data,
std::vector<exec_aten::DimOrderType> dim_order = {},
std::vector<exec_aten::StridesType> strides = {},
std::vector<exec_aten::DimOrderType> dim_order,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND,
std::function<void(void*)> 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<exec_aten::SizesType> sizes,
void* data,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND,
std::function<void(void*)> 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.
Expand All @@ -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 <typename T = float>
template <
typename T = float,
exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType<T>::value>
inline TensorImplPtr make_tensor_impl_ptr(
std::vector<exec_aten::SizesType> sizes,
std::vector<T> data,
std::vector<exec_aten::DimOrderType> dim_order = {},
std::vector<exec_aten::StridesType> strides = {},
exec_aten::ScalarType type = deduced_type,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
constexpr exec_aten::ScalarType scalar_type =
runtime::CppTypeToScalarType<T>::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::vector<T>>(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*) {});
}
Expand All @@ -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 <typename T = float>
template <
typename T = float,
exec_aten::ScalarType deduced_type = runtime::CppTypeToScalarType<T>::value>
inline TensorImplPtr make_tensor_impl_ptr(
std::vector<T> 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<exec_aten::SizesType> 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<T>::value>
inline TensorImplPtr make_tensor_impl_ptr(
std::vector<exec_aten::SizesType> sizes,
std::initializer_list<T> list,
std::vector<exec_aten::DimOrderType> dim_order = {},
std::vector<exec_aten::StridesType> 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<T>(std::move(list));
const auto raw_data_ptr = data.data();
auto data_ptr = std::make_shared<std::vector<T>>(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<T>::value>
inline TensorImplPtr make_tensor_impl_ptr(
std::initializer_list<T> 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<exec_aten::SizesType> 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 <typename T>
inline TensorImplPtr make_tensor_impl_ptr(T value) {
return make_tensor_impl_ptr({}, std::vector<T>{value});
}

/**
Expand All @@ -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<exec_aten::SizesType> sizes,
std::vector<uint8_t> data,
std::vector<exec_aten::DimOrderType> dim_order = {},
std::vector<exec_aten::StridesType> strides = {},
std::vector<exec_aten::DimOrderType> dim_order,
std::vector<exec_aten::StridesType> 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<uint8_t>
* 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<exec_aten::SizesType> sizes,
std::vector<uint8_t> 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
Loading

0 comments on commit f36ea11

Please sign in to comment.