|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Arm Limited. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#ifndef ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H |
| 26 | +#define ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H |
| 27 | + |
| 28 | +#include "arm_compute/core/ITensor.h" |
| 29 | +#include "arm_compute/core/Types.h" |
| 30 | + |
| 31 | +#include <functional> |
| 32 | +#include <stdexcept> |
| 33 | + |
| 34 | +typedef std::function<bool(const void *)> predicate_t; |
| 35 | + |
| 36 | +namespace arm_compute |
| 37 | +{ |
| 38 | +/** Common base class for all sparse tensors */ |
| 39 | +class SparseTensor : public ITensor |
| 40 | +{ |
| 41 | +public: |
| 42 | + /** Prevent instances of this class to be constructed by the default constructor */ |
| 43 | + SparseTensor() = delete; |
| 44 | + /** Prevent instances of this class to be copy constructed */ |
| 45 | + SparseTensor(const SparseTensor&) = delete; |
| 46 | + /** Prevent instances of this class to be copied */ |
| 47 | + SparseTensor& operator=(const SparseTensor&) = delete; |
| 48 | + ~SparseTensor() = default; |
| 49 | + |
| 50 | + /** Returns the number of sparse dimensions */ |
| 51 | + size_t sparse_dim() const; |
| 52 | + /** Returns the number of dense dimensions */ |
| 53 | + size_t dense_dim() const; |
| 54 | + /** Returns the (total) number of dimensions */ |
| 55 | + size_t dim() const; |
| 56 | + /** Returns true if the tensor is hybrid (contains both sparse and dense dimensions) |
| 57 | + * |
| 58 | + * @note A sparse tensor is hybrid if it has at least one dense dimension. |
| 59 | + */ |
| 60 | + bool is_hybrid() const; |
| 61 | + /** Returns the ratio of zero-valued elements to the total number of elements */ |
| 62 | + float sparsity() const; |
| 63 | + /** Returns the ratio of non-zero elements to the total number of elements */ |
| 64 | + float density() const; |
| 65 | + /** Returns the dense volume */ |
| 66 | + uint32_t dense_volume(size_t sparse_dim) const; |
| 67 | + /** Returns the number of non zero elements */ |
| 68 | + virtual size_t nnz() const = 0; |
| 69 | + /** Converts the sparse tensor to a dense tensor */ |
| 70 | + virtual std::unique_ptr<ITensor> to_dense() = 0; |
| 71 | + /** Returns the coordinates of the n-th (non-zero) element. |
| 72 | + * |
| 73 | + * @param nth The *zero-base* index of the element |
| 74 | + * |
| 75 | + * @return The coordinates of the element |
| 76 | + */ |
| 77 | + virtual Coordinates get_coordinates(size_t nth) const = 0; |
| 78 | + /** Returns a pointer to the n-th (non-zero) element. If the element specified by |
| 79 | + * the coordinates is zero, nullptr is returned. |
| 80 | + * |
| 81 | + * @param nth The *zero-base* index of the element |
| 82 | + * |
| 83 | + * @return The value of the element |
| 84 | + * |
| 85 | + * @note The value has size dense_volume(sparse_dim()). |
| 86 | + */ |
| 87 | + virtual const uint8_t *get_value(Coordinates coords) const = 0; |
| 88 | + |
| 89 | +protected: |
| 90 | + SparseTensor(size_t dim, size_t sparse_dim); |
| 91 | + |
| 92 | + std::function<bool(const void *)> make_is_nonzero_predicate(DataType dt) const; |
| 93 | + bool has_non_zero_elements(uint8_t *arr, size_t len, size_t element_size, predicate_t is_non_zero) const; |
| 94 | + void print_values(std::ostream &os, const uint8_t *data, size_t offset, size_t count) const; |
| 95 | + |
| 96 | +private: |
| 97 | + size_t _total_dim; |
| 98 | + size_t _sparse_dim; |
| 99 | +}; |
| 100 | +} |
| 101 | + |
| 102 | +#endif // ACL_ARM_COMPUTE_RUNTIME_SPARSETENSOR_H |
0 commit comments