-
Notifications
You must be signed in to change notification settings - Fork 4.1k
GH-34796: [C++] Add FromTensor, ToTensor and strides methods to FixedShapeTensorArray #34797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,25 @@ namespace extension { | |
| class ARROW_EXPORT FixedShapeTensorArray : public ExtensionArray { | ||
| public: | ||
| using ExtensionArray::ExtensionArray; | ||
|
|
||
| /// \brief Create a FixedShapeTensorArray from a Tensor | ||
| /// | ||
| /// This method will create a FixedShapeTensorArray from a Tensor, taking its first | ||
| /// dimension as the number of elements in the resulting array and the remaining | ||
| /// dimensions as the shape of the individual tensors. If Tensor provides strides, | ||
| /// they will be used to determine dimension permutation. Otherwise, row-major layout | ||
| /// (i.e. no permutation) will be assumed. | ||
| /// | ||
| /// \param[in] tensor The Tensor to convert to a FixedShapeTensorArray | ||
| static Result<std::shared_ptr<FixedShapeTensorArray>> FromTensor( | ||
| const std::shared_ptr<Tensor>& tensor); | ||
|
|
||
| /// \brief Create a Tensor from FixedShapeTensorArray | ||
| /// | ||
| /// This method will create a Tensor from a FixedShapeTensorArray, setting its | ||
| /// first dimension as length equal to the FixedShapeTensorArray's length and the | ||
| /// remaining dimensions as the FixedShapeTensorType's shape. | ||
| const Result<std::shared_ptr<Tensor>> ToTensor() const; | ||
| }; | ||
|
|
||
| /// \brief Concrete type class for constant-size Tensor data. | ||
|
|
@@ -51,6 +70,11 @@ class ARROW_EXPORT FixedShapeTensorType : public ExtensionType { | |
| /// Value type of tensor elements | ||
| const std::shared_ptr<DataType> value_type() const { return value_type_; } | ||
|
|
||
| /// Strides of tensor elements. Strides state offset in bytes between adjacent | ||
| /// elements along each dimension. In case permutation is non-empty strides are | ||
| /// computed from permuted tensor element's shape. | ||
| const std::vector<int64_t>& strides(); | ||
|
|
||
| /// Permutation mapping from logical to physical memory layout of tensor elements | ||
| const std::vector<int64_t>& permutation() const { return permutation_; } | ||
|
|
||
|
|
@@ -74,10 +98,17 @@ class ARROW_EXPORT FixedShapeTensorType : public ExtensionType { | |
| const std::vector<int64_t>& permutation = {}, | ||
| const std::vector<std::string>& dim_names = {}); | ||
|
|
||
| /// \brief Compute strides of FixedShapeTensorType | ||
| static Status ComputeStrides(const FixedWidthType& type, | ||
|
||
| const std::vector<int64_t>& shape, | ||
| const std::vector<int64_t>& permutation, | ||
| std::vector<int64_t>* strides); | ||
|
|
||
| private: | ||
| std::shared_ptr<DataType> storage_type_; | ||
| std::shared_ptr<DataType> value_type_; | ||
| std::vector<int64_t> shape_; | ||
| std::vector<int64_t> strides_; | ||
| std::vector<int64_t> permutation_; | ||
| std::vector<std::string> dim_names_; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add here to the docstring that this will automatically reshape the resulting Tensor according to the
permutationmetadata of the FixedShapeTensorArray?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.