Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 4f76a00 | 2023-08-02 11:59:07 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TENSORSHAPE_H |
| 25 | #define ARM_COMPUTE_TENSORSHAPE_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Dimensions.h" |
| 28 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/Utility.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <algorithm> |
| 32 | #include <array> |
| 33 | #include <functional> |
| 34 | #include <numeric> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | /** Shape of a tensor */ |
Sheri Zhang | d80792a | 2020-11-05 10:43:37 +0000 | [diff] [blame] | 39 | class TensorShape : public Dimensions<size_t> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | /** Constructor to initialize the tensor shape. |
| 43 | * |
| 44 | * @param[in] dims Values to initialize the dimensions. |
| 45 | */ |
| 46 | template <typename... Ts> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | TensorShape(Ts... dims) : Dimensions{dims...} |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
| 49 | // Initialize unspecified dimensions to 1 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | if (_num_dimensions > 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | { |
| 52 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 53 | } |
| 54 | |
| 55 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 56 | apply_dimension_correction(); |
| 57 | } |
| 58 | /** Allow instances of this class to be copy constructed */ |
| 59 | TensorShape(const TensorShape &) = default; |
| 60 | /** Allow instances of this class to be copied */ |
| 61 | TensorShape &operator=(const TensorShape &) = default; |
| 62 | /** Allow instances of this class to be move constructed */ |
| 63 | TensorShape(TensorShape &&) = default; |
| 64 | /** Allow instances of this class to be moved */ |
| 65 | TensorShape &operator=(TensorShape &&) = default; |
| 66 | /** Default destructor */ |
| 67 | ~TensorShape() = default; |
| 68 | |
| 69 | /** Accessor to set the value of one of the dimensions. |
| 70 | * |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 71 | * @param[in] dimension Dimension for which the value is set. |
| 72 | * @param[in] value Value to be set for the dimension. |
Giorgio Arena | ec241b4 | 2020-12-11 13:39:02 +0000 | [diff] [blame] | 73 | * @param[in] apply_dim_correction (Optional) Flag to state whether apply dimension correction after setting one dimension. E.g. when permuting NCHW -> NHWC, 1x1x2 would become 2x1x1, but _num_dimensions should be 3 rather than 1. |
| 74 | * @param[in] increase_dim_unit (Optional) Set to true if new unit dimensions increase the number of dimensions of the shape. |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 75 | * |
| 76 | * @return *this. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | */ |
Giorgio Arena | ec241b4 | 2020-12-11 13:39:02 +0000 | [diff] [blame] | 78 | TensorShape &set(size_t dimension, size_t value, bool apply_dim_correction = true, bool increase_dim_unit = true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | { |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 80 | // Clear entire shape if one dimension is zero |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (value == 0) |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 82 | { |
| 83 | _num_dimensions = 0; |
| 84 | std::fill(_id.begin(), _id.end(), 0); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 85 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 86 | else |
| 87 | { |
| 88 | // Make sure all empty dimensions are filled with 1 |
| 89 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 91 | // Set the specified dimension and increase the number of dimensions if |
| 92 | // necessary |
Giorgio Arena | ec241b4 | 2020-12-11 13:39:02 +0000 | [diff] [blame] | 93 | Dimensions::set(dimension, value, increase_dim_unit); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 95 | // Correct number dimensions to ignore trailing dimensions of size 1 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (apply_dim_correction) |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 97 | { |
| 98 | apply_dimension_correction(); |
| 99 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 100 | } |
| 101 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Gian Marco Iodice | 42ab899 | 2017-08-04 10:54:55 +0100 | [diff] [blame] | 104 | /** Accessor to remove the dimension n from the tensor shape. |
| 105 | * |
| 106 | * @note The upper dimensions of the tensor shape will be shifted down by 1 |
| 107 | * |
Viet-Hoa Do | 4f76a00 | 2023-08-02 11:59:07 +0100 | [diff] [blame] | 108 | * @param[in] n Dimension to remove |
| 109 | * @param[in] apply_dim_correction (Optional) Flag to state whether apply dimension correction (removing trailing dimensions with size of 1) after removing a dimension. |
Gian Marco Iodice | 42ab899 | 2017-08-04 10:54:55 +0100 | [diff] [blame] | 110 | */ |
Viet-Hoa Do | 4f76a00 | 2023-08-02 11:59:07 +0100 | [diff] [blame] | 111 | void remove_dimension(size_t n, bool apply_dim_correction = true) |
Gian Marco Iodice | 42ab899 | 2017-08-04 10:54:55 +0100 | [diff] [blame] | 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(_num_dimensions < 1); |
| 114 | ARM_COMPUTE_ERROR_ON(n >= _num_dimensions); |
| 115 | |
| 116 | std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n); |
| 117 | |
| 118 | // Reduce number of dimensions |
| 119 | _num_dimensions--; |
| 120 | |
| 121 | // Make sure all empty dimensions are filled with 1 |
| 122 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 123 | |
| 124 | // Correct number dimensions to ignore trailing dimensions of size 1 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | if (apply_dim_correction) |
Viet-Hoa Do | 4f76a00 | 2023-08-02 11:59:07 +0100 | [diff] [blame] | 126 | { |
| 127 | apply_dimension_correction(); |
| 128 | } |
Gian Marco Iodice | 42ab899 | 2017-08-04 10:54:55 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | /** Collapse the first n dimensions. |
| 132 | * |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 133 | * @param[in] n Number of dimensions to collapse into @p first |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | * @param[in] first Dimensions into which the following @p n are collapsed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | */ |
| 136 | void collapse(size_t n, size_t first = 0) |
| 137 | { |
| 138 | Dimensions::collapse(n, first); |
| 139 | |
| 140 | // Make sure all empty dimensions are filled with 1 |
| 141 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 142 | } |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 143 | /** Shifts right the tensor shape increasing its dimensions |
| 144 | * |
| 145 | * @param[in] step Rotation step |
| 146 | */ |
| 147 | void shift_right(size_t step) |
| 148 | { |
| 149 | ARM_COMPUTE_ERROR_ON(step > TensorShape::num_max_dimensions - num_dimensions()); |
| 150 | |
| 151 | std::rotate(begin(), begin() + TensorShape::num_max_dimensions - step, end()); |
| 152 | _num_dimensions += step; |
| 153 | |
| 154 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 155 | apply_dimension_correction(); |
| 156 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 158 | /** Return a copy with collapsed dimensions starting from a given point. |
| 159 | * |
| 160 | * @param[in] start Starting point of collapsing dimensions. |
| 161 | * |
| 162 | * @return A copy with collapse dimensions starting from start. |
| 163 | */ |
| 164 | TensorShape collapsed_from(size_t start) const |
| 165 | { |
| 166 | TensorShape copy(*this); |
Isabella Gottardi | 5f29d4a | 2018-07-16 19:02:47 +0100 | [diff] [blame] | 167 | copy.collapse(num_dimensions() - start, start); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 168 | return copy; |
| 169 | } |
| 170 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | /** Collapses all dimensions to a single linear total size. |
| 172 | * |
| 173 | * @return The total tensor size in terms of elements. |
| 174 | */ |
| 175 | size_t total_size() const |
| 176 | { |
| 177 | return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>()); |
| 178 | } |
| 179 | /** Collapses given dimension and above. |
| 180 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | * @param[in] dimension Size of the wanted dimension |
| 182 | * |
| 183 | * @return The linear size of the collapsed dimensions |
| 184 | */ |
| 185 | size_t total_size_upper(size_t dimension) const |
| 186 | { |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 187 | ARM_COMPUTE_ERROR_ON(dimension >= TensorShape::num_max_dimensions); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>()); |
| 189 | } |
| 190 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 191 | /** Compute size of dimensions lower than the given one. |
| 192 | * |
| 193 | * @param[in] dimension Upper boundary. |
| 194 | * |
| 195 | * @return The linear size of the collapsed dimensions. |
| 196 | */ |
| 197 | size_t total_size_lower(size_t dimension) const |
| 198 | { |
| 199 | ARM_COMPUTE_ERROR_ON(dimension > TensorShape::num_max_dimensions); |
| 200 | return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>()); |
| 201 | } |
| 202 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 203 | /** If shapes are broadcast compatible, return the broadcasted shape. |
| 204 | * |
| 205 | * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1. |
| 206 | * |
| 207 | * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions. |
| 208 | * |
| 209 | * @param[in] shapes Tensor shapes. |
| 210 | * |
| 211 | * @return The broadcasted shape or an empty shape if the shapes are not broadcast compatible. |
| 212 | */ |
| 213 | template <typename... Shapes> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 214 | static TensorShape broadcast_shape(const Shapes &...shapes) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 215 | { |
| 216 | TensorShape bc_shape; |
| 217 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 218 | auto broadcast = [&bc_shape](const TensorShape &other) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 219 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 220 | if (bc_shape.num_dimensions() == 0) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 221 | { |
| 222 | bc_shape = other; |
| 223 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 224 | else if (other.num_dimensions() != 0) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 225 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 226 | for (size_t d = 0; d < TensorShape::num_max_dimensions; ++d) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 227 | { |
| 228 | const size_t dim_min = std::min(bc_shape[d], other[d]); |
| 229 | const size_t dim_max = std::max(bc_shape[d], other[d]); |
| 230 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 231 | if ((dim_min != 1) && (dim_min != dim_max)) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 232 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 233 | bc_shape = TensorShape{0U}; |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | |
| 237 | bc_shape.set(d, dim_max); |
| 238 | } |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | utility::for_each(broadcast, shapes...); |
| 243 | |
| 244 | return bc_shape; |
| 245 | } |
| 246 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 247 | private: |
| 248 | /** Remove trailing dimensions of size 1 from the reported number of dimensions. */ |
| 249 | void apply_dimension_correction() |
| 250 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 251 | for (int i = static_cast<int>(_num_dimensions) - 1; i > 0; --i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 252 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 253 | if (_id[i] == 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | { |
| 255 | --_num_dimensions; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | }; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 264 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 265 | #endif /*ARM_COMPUTE_TENSORSHAPE_H*/ |