-
Notifications
You must be signed in to change notification settings - Fork 809
Sparse tensor data structures #1186
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
Draft
aestriplex
wants to merge
2
commits into
ARM-software:main
Choose a base branch
from
aestriplex:sparse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,898
−8
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright (c) 2025 Arm Limited. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef ACL_ARM_COMPUTE_CORE_IREDUCIBLETENSOR_H | ||
| #define ACL_ARM_COMPUTE_CORE_IREDUCIBLETENSOR_H | ||
|
|
||
| #include "arm_compute/core/SparseTensor.h" | ||
| #include "arm_compute/runtime/COOTensor.h" | ||
| #include "arm_compute/runtime/CSRTensor.h" | ||
|
|
||
| namespace arm_compute | ||
| { | ||
| /** */ | ||
| class IReducibleTensor | ||
| { | ||
| public: | ||
| virtual ~IReducibleTensor() = default; | ||
| /** Convert a dense tensor to sparse tensor with specified sparse dimensions using the default | ||
| * sparse tensor representation: COO format. | ||
| * | ||
| * @param[in] dim sparse dimension | ||
| * | ||
| * @return A unique pointer to a SparseTensor object. | ||
| */ | ||
| virtual std::unique_ptr<SparseTensor> to_sparse(size_t dim) const = 0; | ||
| /** Convert a dense tensor to COO sparse tensor with specified sparse dimensions. | ||
| * | ||
| * @param[in] dim sparse dimension | ||
| * | ||
| * @return A unique pointer to a COOTensor object. | ||
| */ | ||
| virtual std::unique_ptr<COOTensor> to_coo_sparse(size_t dim) const = 0; | ||
| /** Convert a dense tensor to COO sparse tensor with the default sparse dimension. | ||
| * For COO format, the number of sparse dimensions is equal to the total number of dimensions. | ||
| * | ||
| * @return A unique pointer to a COOTensor object. | ||
| */ | ||
| virtual std::unique_ptr<COOTensor> to_coo_sparse() const = 0; | ||
| /** Convert a dense tensor to CSR sparse tensor with specified sparse dimensions. | ||
| * | ||
| * @param[in] dim sparse dimension | ||
| * | ||
| * @return A unique pointer to a CSRTensor object. | ||
| */ | ||
| virtual std::unique_ptr<CSRTensor> to_csr_sparse(size_t dim) const = 0; | ||
| /** Convert a dense tensor to CSR sparse tensor with the default sparse dimension. | ||
| * For CSR format, the number of sparse dimensions is equal to 2. | ||
| * | ||
| * @return A unique pointer to a CSRTensor object. | ||
| */ | ||
| virtual std::unique_ptr<CSRTensor> to_csr_sparse() const = 0; | ||
| }; | ||
| } | ||
|
|
||
| #endif // ACL_ARM_COMPUTE_CORE_IREDUCIBLETENSOR_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright (c) 2025 Arm Limited. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H | ||
| #define ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H | ||
|
|
||
| #include "arm_compute/core/ITensor.h" | ||
| #include "arm_compute/core/Types.h" | ||
|
|
||
| #include <functional> | ||
| #include <stdexcept> | ||
|
|
||
| typedef std::function<bool(const void *)> predicate_t; | ||
|
|
||
| namespace arm_compute | ||
| { | ||
| /** Common base class for all sparse tensors */ | ||
| class SparseTensor : public ITensor | ||
| { | ||
| public: | ||
| /** Prevent instances of this class to be constructed by the default constructor */ | ||
| SparseTensor() = delete; | ||
| /** Prevent instances of this class to be copy constructed */ | ||
| SparseTensor(const SparseTensor&) = delete; | ||
| /** Prevent instances of this class to be copied */ | ||
| SparseTensor& operator=(const SparseTensor&) = delete; | ||
| ~SparseTensor() = default; | ||
|
|
||
| /** Returns the number of sparse dimensions */ | ||
| size_t sparse_dim() const; | ||
| /** Returns the number of dense dimensions */ | ||
| size_t dense_dim() const; | ||
| /** Returns the (total) number of dimensions */ | ||
| size_t dim() const; | ||
| /** Returns true if the tensor is hybrid (contains both sparse and dense dimensions) | ||
| * | ||
| * @note A sparse tensor is hybrid if it has at least one dense dimension. | ||
| */ | ||
| bool is_hybrid() const; | ||
| /** Returns the ratio of zero-valued elements to the total number of elements */ | ||
| float sparsity() const; | ||
| /** Returns the ratio of non-zero elements to the total number of elements */ | ||
| float density() const; | ||
| /** Returns the dense volume */ | ||
| uint32_t dense_volume(size_t sparse_dim) const; | ||
| /** Returns the number of non zero elements */ | ||
| virtual size_t nnz() const = 0; | ||
| /** Converts the sparse tensor to a dense tensor */ | ||
| virtual std::unique_ptr<ITensor> to_dense() = 0; | ||
| /** Returns the coordinates of the n-th (non-zero) element. | ||
| * | ||
| * @param nth The *zero-base* index of the element | ||
| * | ||
| * @return The coordinates of the element | ||
| */ | ||
| virtual Coordinates get_coordinates(size_t nth) const = 0; | ||
| /** Returns a pointer to the n-th (non-zero) element. If the element specified by | ||
| * the coordinates is zero, nullptr is returned. | ||
| * | ||
| * @param nth The *zero-base* index of the element | ||
| * | ||
| * @return The value of the element | ||
| * | ||
| * @note The value has size dense_volume(sparse_dim()). | ||
| */ | ||
| virtual const uint8_t *get_value(Coordinates coords) const = 0; | ||
|
|
||
| protected: | ||
| SparseTensor(size_t dim, size_t sparse_dim); | ||
|
|
||
| std::function<bool(const void *)> make_is_nonzero_predicate(DataType dt) const; | ||
| bool has_non_zero_elements(uint8_t *arr, size_t len, size_t element_size, predicate_t is_non_zero) const; | ||
| void print_values(std::ostream &os, const uint8_t *data, size_t offset, size_t count) const; | ||
|
|
||
| private: | ||
| size_t _total_dim; | ||
| size_t _sparse_dim; | ||
| }; | ||
| } | ||
|
|
||
| #endif // ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.