Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_TENSORSHAPE_H__ |
| 25 | #define __ARM_COMPUTE_TENSORSHAPE_H__ |
| 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 */ |
| 39 | class TensorShape : public Dimensions<size_t> |
| 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> |
| 47 | TensorShape(Ts... dims) |
| 48 | : Dimensions{ dims... } |
| 49 | { |
| 50 | // Initialize unspecified dimensions to 1 |
| 51 | if(_num_dimensions > 0) |
| 52 | { |
| 53 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 54 | } |
| 55 | |
| 56 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 57 | apply_dimension_correction(); |
| 58 | } |
| 59 | /** Allow instances of this class to be copy constructed */ |
| 60 | TensorShape(const TensorShape &) = default; |
| 61 | /** Allow instances of this class to be copied */ |
| 62 | TensorShape &operator=(const TensorShape &) = default; |
| 63 | /** Allow instances of this class to be move constructed */ |
| 64 | TensorShape(TensorShape &&) = default; |
| 65 | /** Allow instances of this class to be moved */ |
| 66 | TensorShape &operator=(TensorShape &&) = default; |
| 67 | /** Default destructor */ |
| 68 | ~TensorShape() = default; |
| 69 | |
| 70 | /** Accessor to set the value of one of the dimensions. |
| 71 | * |
| 72 | * @param[in] dimension Dimension for which the value is set. |
| 73 | * @param[in] value Value to be set for the dimension. |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 74 | * |
| 75 | * @return *this. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 77 | TensorShape &set(size_t dimension, size_t value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | { |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 79 | // Clear entire shape if one dimension is zero |
| 80 | if(value == 0) |
| 81 | { |
| 82 | _num_dimensions = 0; |
| 83 | std::fill(_id.begin(), _id.end(), 0); |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 84 | } |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 85 | else |
| 86 | { |
| 87 | // Make sure all empty dimensions are filled with 1 |
| 88 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 90 | // Set the specified dimension and increase the number of dimensions if |
| 91 | // necessary |
| 92 | Dimensions::set(dimension, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 94 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 95 | apply_dimension_correction(); |
| 96 | } |
| 97 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Gian Marco Iodice | 42ab899 | 2017-08-04 10:54:55 +0100 | [diff] [blame] | 100 | /** Accessor to remove the dimension n from the tensor shape. |
| 101 | * |
| 102 | * @note The upper dimensions of the tensor shape will be shifted down by 1 |
| 103 | * |
| 104 | * @param[in] n Dimension to remove |
| 105 | */ |
| 106 | void remove_dimension(size_t n) |
| 107 | { |
| 108 | ARM_COMPUTE_ERROR_ON(_num_dimensions < 1); |
| 109 | ARM_COMPUTE_ERROR_ON(n >= _num_dimensions); |
| 110 | |
| 111 | std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n); |
| 112 | |
| 113 | // Reduce number of dimensions |
| 114 | _num_dimensions--; |
| 115 | |
| 116 | // Make sure all empty dimensions are filled with 1 |
| 117 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 118 | |
| 119 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 120 | apply_dimension_correction(); |
| 121 | } |
| 122 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | /** Collapse the first n dimensions. |
| 124 | * |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 125 | * @param[in] n Number of dimensions to collapse into @p first |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | * @param[in] first Dimensions into which the following @p n are collapsed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | */ |
| 128 | void collapse(size_t n, size_t first = 0) |
| 129 | { |
| 130 | Dimensions::collapse(n, first); |
| 131 | |
| 132 | // Make sure all empty dimensions are filled with 1 |
| 133 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 134 | } |
| 135 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 136 | /** Return a copy with collapsed dimensions starting from a given point. |
| 137 | * |
| 138 | * @param[in] start Starting point of collapsing dimensions. |
| 139 | * |
| 140 | * @return A copy with collapse dimensions starting from start. |
| 141 | */ |
| 142 | TensorShape collapsed_from(size_t start) const |
| 143 | { |
| 144 | TensorShape copy(*this); |
| 145 | copy.collapse(num_dimensions(), start); |
| 146 | return copy; |
| 147 | } |
| 148 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | /** Collapses all dimensions to a single linear total size. |
| 150 | * |
| 151 | * @return The total tensor size in terms of elements. |
| 152 | */ |
| 153 | size_t total_size() const |
| 154 | { |
| 155 | return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>()); |
| 156 | } |
| 157 | /** Collapses given dimension and above. |
| 158 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | * @param[in] dimension Size of the wanted dimension |
| 160 | * |
| 161 | * @return The linear size of the collapsed dimensions |
| 162 | */ |
| 163 | size_t total_size_upper(size_t dimension) const |
| 164 | { |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 165 | ARM_COMPUTE_ERROR_ON(dimension >= TensorShape::num_max_dimensions); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>()); |
| 167 | } |
| 168 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 169 | /** Compute size of dimensions lower than the given one. |
| 170 | * |
| 171 | * @param[in] dimension Upper boundary. |
| 172 | * |
| 173 | * @return The linear size of the collapsed dimensions. |
| 174 | */ |
| 175 | size_t total_size_lower(size_t dimension) const |
| 176 | { |
| 177 | ARM_COMPUTE_ERROR_ON(dimension > TensorShape::num_max_dimensions); |
| 178 | return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>()); |
| 179 | } |
| 180 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 181 | /** If shapes are broadcast compatible, return the broadcasted shape. |
| 182 | * |
| 183 | * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1. |
| 184 | * |
| 185 | * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions. |
| 186 | * |
| 187 | * @param[in] shapes Tensor shapes. |
| 188 | * |
| 189 | * @return The broadcasted shape or an empty shape if the shapes are not broadcast compatible. |
| 190 | */ |
| 191 | template <typename... Shapes> |
| 192 | static TensorShape broadcast_shape(const Shapes &... shapes) |
| 193 | { |
| 194 | TensorShape bc_shape; |
| 195 | |
| 196 | auto broadcast = [&bc_shape](const TensorShape & other) |
| 197 | { |
| 198 | if(bc_shape.num_dimensions() == 0) |
| 199 | { |
| 200 | bc_shape = other; |
| 201 | } |
| 202 | else if(other.num_dimensions() != 0) |
| 203 | { |
| 204 | for(size_t d = 0; d < TensorShape::num_max_dimensions; ++d) |
| 205 | { |
| 206 | const size_t dim_min = std::min(bc_shape[d], other[d]); |
| 207 | const size_t dim_max = std::max(bc_shape[d], other[d]); |
| 208 | |
| 209 | if((dim_min != 1) && (dim_min != dim_max)) |
| 210 | { |
| 211 | bc_shape = TensorShape{ 0U }; |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | bc_shape.set(d, dim_max); |
| 216 | } |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | utility::for_each(broadcast, shapes...); |
| 221 | |
| 222 | return bc_shape; |
| 223 | } |
| 224 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | private: |
| 226 | /** Remove trailing dimensions of size 1 from the reported number of dimensions. */ |
| 227 | void apply_dimension_correction() |
| 228 | { |
Anthony Barbier | a3b4ce2 | 2017-10-09 11:04:30 +0100 | [diff] [blame] | 229 | for(int i = static_cast<int>(_num_dimensions) - 1; i > 0; --i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | { |
| 231 | if(_id[i] == 1) |
| 232 | { |
| 233 | --_num_dimensions; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | }; |
| 242 | } |
| 243 | #endif /*__ARM_COMPUTE_TENSORSHAPE_H__*/ |