blob: 9f6cf773e43a6b056cbbfc95aaaaae3f06d11fac [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arenae7254a02021-02-24 12:46:35 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TENSORSHAPE_H
25#define ARM_COMPUTE_TENSORSHAPE_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Dimensions.h"
28#include "arm_compute/core/Error.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000029#include "arm_compute/core/utils/misc/Utility.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31#include <algorithm>
32#include <array>
33#include <functional>
34#include <numeric>
35
36namespace arm_compute
37{
38/** Shape of a tensor */
Sheri Zhangd80792a2020-11-05 10:43:37 +000039class TensorShape : public Dimensions<size_t>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
41public:
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 */
Giorgio Arenae7254a02021-02-24 12:46:35 +000060 // Avoid -O3 aggressive optimization for the copy constructor when building in release mode for armv7a
61#if defined(LINUX_V7_RELEASE)
62#pragma GCC push_options
63#pragma GCC optimize("O2")
64 TensorShape(const TensorShape &other)
65 : Dimensions(static_cast<const Dimensions &>(other))
66 {
67 }
68#pragma GCC pop_options
69#else // defined(LINUX_V7_RELEASE)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 TensorShape(const TensorShape &) = default;
Giorgio Arenae7254a02021-02-24 12:46:35 +000071#endif // defined(LINUX_V7_RELEASE)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 /** Allow instances of this class to be copied */
73 TensorShape &operator=(const TensorShape &) = default;
74 /** Allow instances of this class to be move constructed */
75 TensorShape(TensorShape &&) = default;
76 /** Allow instances of this class to be moved */
77 TensorShape &operator=(TensorShape &&) = default;
78 /** Default destructor */
79 ~TensorShape() = default;
80
81 /** Accessor to set the value of one of the dimensions.
82 *
Giorgio Arena563494c2018-04-30 17:29:41 +010083 * @param[in] dimension Dimension for which the value is set.
84 * @param[in] value Value to be set for the dimension.
Giorgio Arenaec241b42020-12-11 13:39:02 +000085 * @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.
86 * @param[in] increase_dim_unit (Optional) Set to true if new unit dimensions increase the number of dimensions of the shape.
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000087 *
88 * @return *this.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 */
Giorgio Arenaec241b42020-12-11 13:39:02 +000090 TensorShape &set(size_t dimension, size_t value, bool apply_dim_correction = true, bool increase_dim_unit = true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 {
Moritz Pflanzer0745a982017-07-05 16:34:28 +010092 // Clear entire shape if one dimension is zero
93 if(value == 0)
94 {
95 _num_dimensions = 0;
96 std::fill(_id.begin(), _id.end(), 0);
Moritz Pflanzer0745a982017-07-05 16:34:28 +010097 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000098 else
99 {
100 // Make sure all empty dimensions are filled with 1
101 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000103 // Set the specified dimension and increase the number of dimensions if
104 // necessary
Giorgio Arenaec241b42020-12-11 13:39:02 +0000105 Dimensions::set(dimension, value, increase_dim_unit);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000107 // Correct number dimensions to ignore trailing dimensions of size 1
Giorgio Arena563494c2018-04-30 17:29:41 +0100108 if(apply_dim_correction)
109 {
110 apply_dimension_correction();
111 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000112 }
113 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 }
115
Gian Marco Iodice42ab8992017-08-04 10:54:55 +0100116 /** Accessor to remove the dimension n from the tensor shape.
117 *
118 * @note The upper dimensions of the tensor shape will be shifted down by 1
119 *
120 * @param[in] n Dimension to remove
121 */
122 void remove_dimension(size_t n)
123 {
124 ARM_COMPUTE_ERROR_ON(_num_dimensions < 1);
125 ARM_COMPUTE_ERROR_ON(n >= _num_dimensions);
126
127 std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n);
128
129 // Reduce number of dimensions
130 _num_dimensions--;
131
132 // Make sure all empty dimensions are filled with 1
133 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
134
135 // Correct number dimensions to ignore trailing dimensions of size 1
136 apply_dimension_correction();
137 }
138
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 /** Collapse the first n dimensions.
140 *
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100141 * @param[in] n Number of dimensions to collapse into @p first
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 * @param[in] first Dimensions into which the following @p n are collapsed.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 */
144 void collapse(size_t n, size_t first = 0)
145 {
146 Dimensions::collapse(n, first);
147
148 // Make sure all empty dimensions are filled with 1
149 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
150 }
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100151 /** Shifts right the tensor shape increasing its dimensions
152 *
153 * @param[in] step Rotation step
154 */
155 void shift_right(size_t step)
156 {
157 ARM_COMPUTE_ERROR_ON(step > TensorShape::num_max_dimensions - num_dimensions());
158
159 std::rotate(begin(), begin() + TensorShape::num_max_dimensions - step, end());
160 _num_dimensions += step;
161
162 // Correct number dimensions to ignore trailing dimensions of size 1
163 apply_dimension_correction();
164 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000166 /** Return a copy with collapsed dimensions starting from a given point.
167 *
168 * @param[in] start Starting point of collapsing dimensions.
169 *
170 * @return A copy with collapse dimensions starting from start.
171 */
172 TensorShape collapsed_from(size_t start) const
173 {
174 TensorShape copy(*this);
Isabella Gottardi5f29d4a2018-07-16 19:02:47 +0100175 copy.collapse(num_dimensions() - start, start);
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000176 return copy;
177 }
178
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 /** Collapses all dimensions to a single linear total size.
180 *
181 * @return The total tensor size in terms of elements.
182 */
183 size_t total_size() const
184 {
185 return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
186 }
187 /** Collapses given dimension and above.
188 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 * @param[in] dimension Size of the wanted dimension
190 *
191 * @return The linear size of the collapsed dimensions
192 */
193 size_t total_size_upper(size_t dimension) const
194 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100195 ARM_COMPUTE_ERROR_ON(dimension >= TensorShape::num_max_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
197 }
198
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100199 /** Compute size of dimensions lower than the given one.
200 *
201 * @param[in] dimension Upper boundary.
202 *
203 * @return The linear size of the collapsed dimensions.
204 */
205 size_t total_size_lower(size_t dimension) const
206 {
207 ARM_COMPUTE_ERROR_ON(dimension > TensorShape::num_max_dimensions);
208 return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>());
209 }
210
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000211 /** If shapes are broadcast compatible, return the broadcasted shape.
212 *
213 * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
214 *
215 * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
216 *
217 * @param[in] shapes Tensor shapes.
218 *
219 * @return The broadcasted shape or an empty shape if the shapes are not broadcast compatible.
220 */
221 template <typename... Shapes>
222 static TensorShape broadcast_shape(const Shapes &... shapes)
223 {
224 TensorShape bc_shape;
225
226 auto broadcast = [&bc_shape](const TensorShape & other)
227 {
228 if(bc_shape.num_dimensions() == 0)
229 {
230 bc_shape = other;
231 }
232 else if(other.num_dimensions() != 0)
233 {
234 for(size_t d = 0; d < TensorShape::num_max_dimensions; ++d)
235 {
236 const size_t dim_min = std::min(bc_shape[d], other[d]);
237 const size_t dim_max = std::max(bc_shape[d], other[d]);
238
239 if((dim_min != 1) && (dim_min != dim_max))
240 {
241 bc_shape = TensorShape{ 0U };
242 break;
243 }
244
245 bc_shape.set(d, dim_max);
246 }
247 }
248 };
249
250 utility::for_each(broadcast, shapes...);
251
252 return bc_shape;
253 }
254
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255private:
256 /** Remove trailing dimensions of size 1 from the reported number of dimensions. */
257 void apply_dimension_correction()
258 {
Anthony Barbiera3b4ce22017-10-09 11:04:30 +0100259 for(int i = static_cast<int>(_num_dimensions) - 1; i > 0; --i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100260 {
261 if(_id[i] == 1)
262 {
263 --_num_dimensions;
264 }
265 else
266 {
267 break;
268 }
269 }
270 }
271};
272}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000273#endif /*ARM_COMPUTE_TENSORSHAPE_H*/