blob: 65a2a1edf4d86468ff439c07ed732cf8d4024458 [file] [log] [blame]
Georgios Pinitas358ca202017-12-07 16:47:52 +00001/*
Manuel Bottini8529bd62018-11-21 11:53:04 +00002 * Copyright (c) 2017-2019 ARM Limited.
Georgios Pinitas358ca202017-12-07 16:47:52 +00003 *
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_MISC_SHAPE_CALCULATOR_H__
25#define __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__
26
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000027#include "arm_compute/core/Helpers.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000028#include "arm_compute/core/ITensorInfo.h"
Gian Marco Iodice7026b302019-06-26 17:18:11 +010029#include "arm_compute/core/KernelDescriptors.h"
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000030#include "arm_compute/core/Utils.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000031
Georgios Pinitas77589b52018-08-21 14:41:35 +010032#include "arm_compute/core/utils/helpers/tensor_transform.h"
33
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000034#include <cmath>
35
Georgios Pinitas358ca202017-12-07 16:47:52 +000036namespace arm_compute
37{
38namespace misc
39{
40namespace shape_calculator
41{
Michalis Spyroud33fe342019-01-04 17:10:25 +000042/** Calculate the output tensor shape of a vector input given the convolution dimensions
43 *
44 * @param[in] input Input tensor shape
45 * @param[in] conv_w Convolution width
46 * @param[in] conv_h Convolution height
47 * @param[in] data_layout Data layout
48 *
49 * @return the calculated shape
50 */
Abe Mbise7784c832018-05-31 16:48:41 +010051inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
52{
53 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
54 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
55 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
56
57 TensorShape output_shape(input);
58 output_shape.set(idx_w, conv_w);
59 output_shape.set(idx_h, conv_h);
60 output_shape.set(idx_c, input.x() / (conv_w * conv_h));
61
62 return output_shape;
63}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010064
Michalis Spyroud33fe342019-01-04 17:10:25 +000065/** Calculate the permuted shape of an input given a permutation vector
66 *
67 * @param[in] input Input tensor info
68 * @param[in] perm Permutation vector
69 *
70 * @return the calculated shape
71 */
Pablo Tello00afd112018-01-04 10:34:24 +000072inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
73{
74 TensorShape output_shape = input.tensor_shape();
75 permute(output_shape, perm);
76 return output_shape;
77}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010078
Michalis Spyroud33fe342019-01-04 17:10:25 +000079/** Calculate the output shape of the reorg layer given a stride
80 *
81 * @param[in] input Input tensor info
82 * @param[in] stride Stride
83 *
84 * @return the calculated shape
85 */
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010086inline TensorShape compute_reorg_output_shape(const ITensorInfo &input, int32_t stride)
87{
Gian Marco Iodice477531c2018-08-21 17:53:38 +010088 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
89 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
90 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010091
Gian Marco Iodice477531c2018-08-21 17:53:38 +010092 ARM_COMPUTE_ERROR_ON(stride <= 0);
93 ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_width] % stride != 0), "The width of the input tensor must be a multiple of stride");
94 ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_height] % stride != 0), "The height of the input tensor must be a multiple of stride");
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010095
96 TensorShape output_shape{ input.tensor_shape() };
Gian Marco Iodice477531c2018-08-21 17:53:38 +010097
98 output_shape.set(idx_width, output_shape[idx_width] / stride);
99 output_shape.set(idx_height, output_shape[idx_height] / stride);
100 output_shape.set(idx_channel, output_shape[idx_channel] * stride * stride);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +0100101
102 return output_shape;
103}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100104
Michalis Spyroud33fe342019-01-04 17:10:25 +0000105/** Calculate the reshaped shape of the weights
106 *
107 * @param[in] weights Weights tensor info
108 * @param[in] has_bias (Optional) Set to true if there is bias
109 * @param[in] num_groups (Optional) Number of groups
110 *
111 * @return the calculated shape of the reshaped weights
112 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100113inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000114{
Giorgio Arena088c2b02018-08-07 16:59:05 +0100115 // Number of groups greater than one are only supported for NCHW data layout, and the number of weights must be a multiple of it.
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100116 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100117 ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100118 ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100119
Georgios Pinitas78c00902018-01-09 17:33:11 +0000120 // Calculate output shape
121 TensorShape weights_reshaped{ weights.tensor_shape() };
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100122 weights_reshaped.set(3, weights_reshaped[3] / num_groups);
123
Georgios Pinitas78c00902018-01-09 17:33:11 +0000124 weights_reshaped.collapse(3);
125 const size_t tmp_dim = weights_reshaped[0];
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100126 weights_reshaped.set(0, weights_reshaped[1]);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000127 weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100128 if(weights.num_dimensions() < 5)
129 {
130 weights_reshaped.set(2, num_groups);
131 }
Georgios Pinitas78c00902018-01-09 17:33:11 +0000132
133 return weights_reshaped;
134}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100135
Michalis Spyroud33fe342019-01-04 17:10:25 +0000136/** Calculate the Left Hand Side matrix reshaped shape
137 *
138 * @param[in] a Input tensor info
139 * @param[in] lhs_info Left Hand Side matrix information
140 * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
141 *
142 * @return the calculated shape
143 */
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000144inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false)
145{
146 ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0);
147 ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0);
148 ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0);
149
150 // Input width/height
151 const unsigned int input_width = a.dimension(0);
152 const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1);
153
154 // Number of horizontal/vertical blocks in the input tensor
155 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(lhs_info.k0));
156 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(lhs_info.m0));
157
158 // Block size
159 const unsigned int block_size = lhs_info.m0 * lhs_info.k0;
160
161 // Output width/height
162 const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0;
163 const unsigned int output_height = std::ceil(num_vert_blocks / static_cast<float>(lhs_info.v0));
164
165 TensorShape lhs_shape{ a.tensor_shape() };
166 lhs_shape.set(0, output_width);
167 lhs_shape.set(1, output_height);
168
169 if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2))
170 {
171 // When the data format is NHWC and the shapes are Nx1x1
172 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
173 // To avoid failures by removing a dimension that doesn't exist
174 // check if the number of dimensions is greater than 2.
175 lhs_shape.remove_dimension(2);
176 }
177
178 return lhs_shape;
179}
180
Michalis Spyroud33fe342019-01-04 17:10:25 +0000181/** Calculate the Right Hand Side matrix reshaped shape
182 *
183 * @param[in] a Input tensor info
184 * @param[in] rhs_info Right Hand Side matrix information
185 *
186 * @return the calculated shape
187 */
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000188inline TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
189{
190 ARM_COMPUTE_ERROR_ON(rhs_info.n0 == 0);
191 ARM_COMPUTE_ERROR_ON(rhs_info.k0 == 0);
192 ARM_COMPUTE_ERROR_ON(rhs_info.h0 == 0);
193
194 // Input width/height
195 const unsigned int input_width = a.dimension(0);
196 const unsigned int input_height = a.dimension(1);
197
198 // Number of horizontal/vertical blocks in the input tensor
199 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(rhs_info.n0));
200 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(rhs_info.k0));
201
202 // Block size
203 const unsigned int block_size = rhs_info.n0 * rhs_info.k0;
204
205 // Output width/height
206 const unsigned int output_width = block_size * num_vert_blocks * rhs_info.h0;
207 const unsigned int output_height = std::ceil(num_horiz_blocks / static_cast<float>(rhs_info.h0));
208
209 TensorShape rhs_shape{ a.tensor_shape() };
210 rhs_shape.set(0, output_width);
211 rhs_shape.set(1, output_height);
212
213 return rhs_shape;
214}
215
Michalis Spyroud33fe342019-01-04 17:10:25 +0000216/** Calculate the interleaved shape of an input tensor
217 *
218 * @param[in] a Input tensor info
219 * @param[in] mult_interleave4x4_height (Optional) Interleave4x4 height
220 * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
221 *
222 * @return the calculated shape
223 */
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100224inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1, bool reinterpret_input_as_3d = false)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000225{
Gian Marco36a0a462018-01-12 10:21:40 +0000226 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
227 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
228 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000229 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000230 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100231 if(reinterpret_input_as_3d)
232 {
233 const int M = a.dimension(1) * a.dimension(2);
234 const int height = std::ceil(M / static_cast<float>(interleave_width));
235 shape_interleaved_a.set(1, height);
Isabella Gottardi089695f2018-10-17 18:04:15 +0100236
237 // When the data format is NHWC and the shapes are Nx1x1
238 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
239 // To avoid failures by removing a dimension that doesn't exist
240 // check if the number of dimensions is greater than 2.
241 if(shape_interleaved_a.num_dimensions() > 2)
242 {
243 shape_interleaved_a.remove_dimension(2);
244 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100245 }
246 else
247 {
248 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
249 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000250
251 return shape_interleaved_a;
252}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100253
giuros016d109962019-01-07 17:47:19 +0000254/** Calculate the reshaped shape of the weights to use in depthwise convolution
255 *
256 * @param[in] input Input tensor info
257 * @param[in] info Depthwise convolution information to be used for reshaping.
258 *
259 * @return the calculated shape
260 */
261inline TensorShape compute_reshaped_depthwise_weights_shape(const ITensorInfo &input, const DepthwiseConvolutionReshapeInfo &info)
262{
263 const auto data_layout = input.data_layout();
264 TensorShape weights_shape{};
265
266 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
267 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
268 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
269 const size_t num_channels = input.dimension(channel_idx);
270 const size_t num_rows = input.dimension(height_idx);
271 const size_t num_cols = input.dimension(width_idx);
272
273 weights_shape.set(0, num_rows * num_cols * info.c0);
274 weights_shape.set(1, DIV_CEIL(num_channels, info.c0));
275 return weights_shape;
276}
277
Michalis Spyroud33fe342019-01-04 17:10:25 +0000278/** Calculate the transposed 1xW shape
279 *
280 * @param[in] b Input tensor info
281 *
282 * @return the calculated shape
283 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000284inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
285{
286 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
287 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
288 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
289 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
290
291 return shape_transposed1xW_b;
292}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100293
Michalis Spyroud33fe342019-01-04 17:10:25 +0000294/** Calculate the transposed 1xW width element shape
295 *
296 * @param[in] b Input tensor info
297 * @param[in] mult_transpose1xW_width (Optional) Transpose1xW width
298 *
299 * @return the calculated shape
300 */
Gian Marco36a0a462018-01-12 10:21:40 +0000301inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000302{
Gian Marco36a0a462018-01-12 10:21:40 +0000303 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
304 // The transpose1xW output matrix will have the following shape:
305 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
306 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000307 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000308 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000309 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
310 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
311
312 return shape_transposed1xW_b;
313}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100314
Michalis Spyroud33fe342019-01-04 17:10:25 +0000315/** Calculate the reductionA shape used in GEMMLowp
316 *
317 * @param[in] b Input tensor info
318 *
319 * @return the calculated shape
320 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000321inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
322{
323 TensorShape shape_vector_sum_col{ b.tensor_shape() };
324 if(shape_vector_sum_col.num_dimensions() > 1)
325 {
326 shape_vector_sum_col.remove_dimension(1);
327 }
328
329 return shape_vector_sum_col;
330}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100331
Michalis Spyroud33fe342019-01-04 17:10:25 +0000332/** Calculate the reductionB shape used in GEMMLowp
333 *
334 * @param[in] a Input tensor info
335 *
336 * @return the calculated shape
337 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000338inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
339{
340 TensorShape shape_vector_sum_row{ a.tensor_shape() };
341 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
Georgios Pinitas932491f2018-09-21 16:33:15 +0100342 if(shape_vector_sum_row.num_dimensions() > 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000343 {
344 shape_vector_sum_row.remove_dimension(1);
345 }
346
347 return shape_vector_sum_row;
348}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100349
Michalis Spyroud33fe342019-01-04 17:10:25 +0000350/** Calculate the Col2Im shape
351 *
352 * @param[in] input Input tensor info
353 * @param[in] convolved_dims Convolved dimensions
354 * @param[in] batch_size_on_z True if batch size is on z axis
355 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
356 *
357 * @return the calculated shape
358 */
Giorgio Arena226e4b92018-08-23 12:00:02 +0100359inline TensorShape compute_col2im_shape(const ITensorInfo &input, const Size2D &convolved_dims, bool batch_size_on_z, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000360{
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100361 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arena226e4b92018-08-23 12:00:02 +0100362 ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area()));
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100363 ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
364
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100365 const DataLayout data_layout = input.data_layout();
366 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
367 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
368 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100369
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100370 TensorShape col2im_shape{ input.tensor_shape() };
371 // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape,
372 // as first three will be override by H,W,C data
373 if(batch_size_on_z && num_groups == 1)
374 {
375 col2im_shape.shift_right(1);
376 }
377 col2im_shape.set(width_idx, convolved_dims.width);
378 col2im_shape.set(height_idx, convolved_dims.height);
379 col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000380
381 return col2im_shape;
382}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100383
Michalis Spyroud33fe342019-01-04 17:10:25 +0000384/** Calculate the transposed shape of a tensor
385 *
386 * @param[in] input Input tensor info
387 *
388 * @return the calculated shape
389 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000390inline TensorShape compute_transposed_shape(const ITensorInfo &input)
391{
392 TensorShape shape_transposed{ input.tensor_shape() };
393
394 shape_transposed.set(0, input.dimension(1));
395 shape_transposed.set(1, input.dimension(0));
396
397 return shape_transposed;
398}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100399
Michalis Spyroud33fe342019-01-04 17:10:25 +0000400/** Calculate the depthwise convolution output shape of a tensor
401 *
402 * @param[in] input Input tensor info
403 * @param[in] weights Weights tensor info
404 * @param[in] conv_info Padding and stride information to use for the convolution.
405 * @param[in] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth.
Usama Arife73686a2019-04-08 17:30:48 +0100406 * @param[in] dilation Dilation, in elements, across x and y. Defaults to (1, 1).
Michalis Spyroud33fe342019-01-04 17:10:25 +0000407 *
408 * @return the calculated shape
409 */
Usama Arife73686a2019-04-08 17:30:48 +0100410inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier, const Size2D &dilation = Size2D(1U,
411 1U))
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000412{
413 const TensorShape input_shape{ input.tensor_shape() };
414 const TensorShape weights_shape{ weights.tensor_shape() };
415
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000416 const DataLayout data_layout = input.data_layout();
417 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
418 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena76572242018-04-04 17:44:26 +0100419 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000420
Usama Arife73686a2019-04-08 17:30:48 +0100421 const DataLayout weights_data_layout = weights.data_layout();
422 const int weights_width_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::WIDTH);
423 const int weights_height_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::HEIGHT);
giuros016d109962019-01-07 17:47:19 +0000424
425 unsigned int output_width = 0;
426 unsigned int output_height = 0;
427 std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
Usama Arife73686a2019-04-08 17:30:48 +0100428 weights_shape[weights_width_idx], weights_shape[weights_height_idx],
429 conv_info, dilation);
giuros016d109962019-01-07 17:47:19 +0000430
431 TensorShape output_shape{ input_shape };
432 output_shape.set(width_idx, output_width);
433 output_shape.set(height_idx, output_height);
434 output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier);
435
436 return output_shape;
437}
438
Michalis Spyroud33fe342019-01-04 17:10:25 +0000439/** Calculate the upsampled output shape used for deconvolution
440 *
Manuel Bottinic1b76fa2019-06-17 12:04:40 +0100441 * @param[in] input Input tensor info
442 * @param[in] weights Weights tensor shape
443 * @param[in] sx Stride on x axis
444 * @param[in] sy Stride on y axis
445 * @param[in] out_dims Output shape dimensions
446 * @param[in] padx Padding on x axis
447 * @param[in] pady Padding on y axis
Michalis Spyroud33fe342019-01-04 17:10:25 +0000448 *
449 * @return the calculated shape
450 */
Manuel Bottinic1b76fa2019-06-17 12:04:40 +0100451inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy,
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100452 std::pair<unsigned int, unsigned int> &out_dims, unsigned int &padx, unsigned int &pady)
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000453{
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100454 const DataLayout data_layout = input.data_layout();
455 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
456 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
457
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100458 // Find the upsampled dimensions
Manuel Bottinic1b76fa2019-06-17 12:04:40 +0100459 unsigned int out_x = (input.dimension(idx_w) - 1) * sx + 1;
460 unsigned int out_y = (input.dimension(idx_h) - 1) * sy + 1;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100461
462 // Find the padding needed for the convolution with stride 1 in order to match output shape
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100463 padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1);
464 pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100465 out_x += padx;
466 out_y += pady;
467
468 TensorShape scale_out_shape(input.tensor_shape());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100469 scale_out_shape.set(idx_w, out_x);
470 scale_out_shape.set(idx_h, out_y);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000471
472 return scale_out_shape;
473}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100474
Michalis Spyroud33fe342019-01-04 17:10:25 +0000475/** Calculate the output shape of the deconvolution layer
476 *
477 * @param[in] out_dims Output x and y shape dimensions
478 * @param[in] input Input tensor info
479 * @param[in] weights Weights tensor shape
480 *
481 * @return the calculated shape
482 */
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100483inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
484{
485 const TensorShape input_shape{ input.tensor_shape() };
486 const TensorShape weights_shape{ weights.tensor_shape() };
487
488 const DataLayout data_layout = input.data_layout();
489 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
490 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
491 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
492 const int batch_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
493
494 TensorShape out_shape{ input_shape };
495 out_shape.set(width_idx, out_dims.first);
496 out_shape.set(height_idx, out_dims.second);
497 out_shape.set(channel_idx, weights_shape[batch_idx]);
498 return out_shape;
499}
500
Michalis Spyroud33fe342019-01-04 17:10:25 +0000501/** Calculate the im2col output shape of a tensor
502 *
503 * @param[in] input Input tensor info
504 * @param[in] kernel_dims The kernel dimensions (width and height).
505 * @param[in] conv_info Contains padding and stride information
506 * @param[in] has_bias In case biases are provided expands the matrix with 1
507 * @param[in] dilation Dilation, in elements, across x and y
508 * @param[in] batch_size_on_z True if batch size is on z axis
509 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
510 *
511 * @return the calculated shape
512 */
Giorgio Arena0f170392018-07-18 16:13:12 +0100513inline TensorShape compute_im2col_conv_shape(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation, bool batch_size_on_z,
514 unsigned int num_groups = 1)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000515{
Giorgio Arena0f170392018-07-18 16:13:12 +0100516 // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
517 // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
518
519 ARM_COMPUTE_ERROR_ON(num_groups == 0);
520 ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
521 ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000522
523 TensorShape output_shape{ input->tensor_shape() };
524
525 const DataLayout data_layout = input->data_layout();
526 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
527 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
528 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
529
530 std::pair<unsigned int, unsigned int> out_dims = scaled_dimensions(output_shape[width_idx], output_shape[height_idx], kernel_dims.width, kernel_dims.height, conv_info, dilation);
Giorgio Arena0f170392018-07-18 16:13:12 +0100531 output_shape.set(0, (output_shape[channel_idx] / num_groups * kernel_dims.area() + (has_bias ? 1 : 0))); // NOLINT
Giorgio Arenaf485a102018-04-20 16:06:21 +0100532 output_shape.set(1, (out_dims.first * out_dims.second));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100533 if(batch_size_on_z && output_shape.num_dimensions() >= 3)
534 {
535 output_shape.remove_dimension(2);
536 }
537 else
538 {
Giorgio Arena0f170392018-07-18 16:13:12 +0100539 output_shape.set(2, num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100540 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000541
542 return output_shape;
543}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100544
Michalis Spyroud33fe342019-01-04 17:10:25 +0000545/** Calculate the flattened output shape of a tensor
546 *
547 * @param[in] input Input tensor info
548 *
549 * @return the calculated shape
550 */
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100551inline TensorShape compute_flatten_shape(const ITensorInfo *input)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000552{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100553 // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
554
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000555 TensorShape output_shape{ input->tensor_shape() };
556
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100557 output_shape.collapse(3);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000558
559 return output_shape;
560}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100561
Michalis Spyroud33fe342019-01-04 17:10:25 +0000562/** Calculate the softmax output shape of a tensor
563 *
564 * @param[in] input Input tensor info
565 * @param[in] axis (Optional) Softmax axis
566 *
567 * @return the calculated shape
568 */
giuros01efbf6c82018-09-03 09:53:53 +0100569inline TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis = 1)
570{
571 // The output shape will be a 2D version of the input. For instance:
572 // - [x,y,z] and axis 1 will return [x, y*z]
573 // - [x,y,z,w] and axis 2 will return [x*y, w*z]
574 // - [x,y,z,w] and axis 3 will return [x*y*z, w]
575 TensorShape shape2D = input->tensor_shape();
576
577 if(axis < input->num_dimensions())
578 {
579 // Collapse from axis onward (this changes the shape)
580 shape2D.collapse_from(axis);
581
582 // Collapse the rest (collapse is inclusive)
583 shape2D.collapse(shape2D.num_dimensions() - 1);
584 }
585 else
586 {
587 // Collapse everything
588 shape2D.collapse(shape2D.num_dimensions());
589 }
590
591 if(axis == 0)
592 {
593 // If axis is zero the first dim should be one. Since
594 // collapse is an inclusive operation we need to shift
595 shape2D.shift_right(1);
596 }
597
598 return shape2D;
599}
600
Michalis Spyroud33fe342019-01-04 17:10:25 +0000601/** Calculate the winograd filter transform shape
602 *
603 * @param[in] input Input tensor info
604 * @param[in] winograd_info Winograd information
605 *
606 * @return the calculated shape
607 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000608inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000609{
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000610 TensorShape tensor_shape{ input.tensor_shape() };
611
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000612 const Size2D kernel_size = winograd_info.kernel_size;
613 const Size2D output_tile_size = winograd_info.output_tile_size;
614 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000615
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000616 tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
617 tensor_shape.set(Window::DimX, input.dimension(3));
618 tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
619 tensor_shape.set(Window::DimZ, input_tile_size.area());
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000620
621 return tensor_shape;
622}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100623
Michalis Spyroud33fe342019-01-04 17:10:25 +0000624/** Calculate the winograd input transform shape
625 *
626 * @param[in] input Input tensor info
627 * @param[in] winograd_info Winograd information
628 *
629 * @return the calculated shape
630 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000631inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000632{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000633 const PadStrideInfo conv_info = winograd_info.convolution_info;
634 const Size2D kernel_size = winograd_info.kernel_size;
635 const Size2D output_tile_size = winograd_info.output_tile_size;
636 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
637
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100638 const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
639 const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
640 const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000641
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100642 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
643 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
644 kernel_size,
645 output_tile_size,
646 conv_info);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100647
648 const unsigned int width = input.tensor_shape()[idx_c];
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100649 const unsigned int height = num_tiles.area();
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000650 const unsigned int depth = input_tile_size.area();
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000651
652 TensorShape output_shape{ input.tensor_shape() };
653 output_shape.set(0, width);
654 output_shape.set(1, height);
655 output_shape.set(2, depth);
656
657 return output_shape;
658}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100659
Michalis Spyroud33fe342019-01-04 17:10:25 +0000660/** Calculate the winograd output transform shape
661 *
662 * @param[in] input Input tensor info
663 * @param[in] winograd_info Winograd information
664 *
665 * @return the calculated shape
666 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000667inline TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000668{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000669 const PadStrideInfo conv_info = winograd_info.convolution_info;
670 const Size2D kernel_size = winograd_info.kernel_size;
671 const Size2D input_dimensions = winograd_info.input_dimensions;
672 const DataLayout data_layout = winograd_info.output_data_layout;
673
674 // Compute output shape
675 unsigned int output_width = 0;
676 unsigned int output_height = 0;
677 std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
678 kernel_size.width, kernel_size.height, conv_info);
679
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000680 TensorShape tensor_shape{ input.tensor_shape() };
681
682 // Output dimension
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000683 const unsigned int out_w = output_width;
684 const unsigned int out_h = output_height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000685 const unsigned int out_c = input.dimension(0);
686
687 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w);
688 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h);
689 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c);
690
691 return tensor_shape;
692}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100693
Michalis Spyroud33fe342019-01-04 17:10:25 +0000694/** Calculate the deep convolution shape output shape of a tensor
695 *
696 * @param[in] input Input tensor info
697 * @param[in] weights Weights tensor info
698 * @param[in] conv_info Contains padding and stride information
699 *
700 * @return the calculated shape
701 */
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000702inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
703{
704 const TensorShape input_shape{ input.tensor_shape() };
705 const TensorShape weights_shape{ weights.tensor_shape() };
706
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000707 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
708 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
709 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
710
Giorgio Arenac0f54432018-03-16 14:02:34 +0000711 const unsigned int input_width = input_shape[idx_width];
712 const unsigned int input_height = input_shape[idx_height];
713 const unsigned int weights_width = weights_shape[idx_width];
714 const unsigned int weights_height = weights_shape[idx_height];
715 const unsigned int weights_out_channel = weights_shape[3];
716 unsigned int output_width = 0;
717 unsigned int output_height = 0;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000718 std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, weights_width, weights_height, conv_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000719
720 TensorShape output_shape{ input_shape };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000721 output_shape.set(idx_width, output_width);
722 output_shape.set(idx_height, output_height);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000723 output_shape.set(idx_channel, weights_out_channel);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000724
725 return output_shape;
726}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100727
Michalis Spyroud33fe342019-01-04 17:10:25 +0000728/** Calculate the min/max shape output shape of a tensor
729 *
730 * @param[in] input Input tensor info
731 *
732 * @return the calculated shape
733 */
Alex Gilday60954c62018-03-05 16:22:48 +0000734inline TensorShape compute_min_max_shape(const ITensorInfo *input)
735{
736 TensorShape output_shape{ input->tensor_shape() };
737 output_shape.set(Window::DimX, 2);
738 output_shape.remove_dimension(1);
739 output_shape.remove_dimension(1);
740
741 return output_shape;
742}
743
Michalis Spyroud33fe342019-01-04 17:10:25 +0000744/** Calculate the output pool shape of a tensor
745 *
746 * @param[in] input Input tensor info
747 * @param[in] pool_info Pooling layer info
748 *
749 * @return the calculated shape
750 */
Michalis Spyroue74b2012018-04-18 09:49:16 +0100751inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
752{
753 unsigned int pooled_w = 0;
754 unsigned int pooled_h = 0;
755
Giorgio Arena3c520c52018-05-01 11:47:24 +0100756 TensorShape output_shape{ input.tensor_shape() };
Michalis Spyroue74b2012018-04-18 09:49:16 +0100757
Giorgio Arena3c520c52018-05-01 11:47:24 +0100758 const bool is_global_pooling = pool_info.is_global_pooling();
759 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
760 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
761 const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width;
762 const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height;
763
764 std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
765 output_shape[idx_height],
Michalis Spyroue74b2012018-04-18 09:49:16 +0100766 pool_size_x,
767 pool_size_y,
768 pool_info.pad_stride_info());
769
Giorgio Arena3c520c52018-05-01 11:47:24 +0100770 output_shape.set(idx_width, pooled_w);
771 output_shape.set(idx_height, pooled_h);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100772
773 return output_shape;
774}
775
George Wort44b4e972019-01-08 11:41:54 +0000776/** Calculate the output roi align shape of a tensor
777 *
778 * @param[in] input Input tensor info
779 * @param[in] rois Rois tensor info
780 * @param[in] pool_info Pooling layer info
781 *
782 * @return the calculated shape
783 */
784inline TensorShape compute_roi_align_shape(const ITensorInfo &input, const ITensorInfo &rois, ROIPoolingLayerInfo pool_info)
785{
786 TensorShape output_shape{ input.tensor_shape() };
787
788 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
789 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
790
791 output_shape.set(idx_width, pool_info.pooled_width());
792 output_shape.set(idx_height, pool_info.pooled_height());
793 output_shape.set(3, rois.dimension(1));
794
795 return output_shape;
796}
797
Michalis Spyroud33fe342019-01-04 17:10:25 +0000798/** Calculate the RNN shape of a tensor
799 *
800 * @param[in] input Input tensor info
801 * @param[in] batch_size Batch size
802 *
803 * @return the calculated shape
804 */
Michalis Spyrou36a559e2018-03-20 10:30:58 +0000805inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
806{
807 TensorShape output_shape{ input->tensor_shape() };
808 output_shape.set(1, batch_size);
809
810 return output_shape;
811}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100812
Michalis Spyroud33fe342019-01-04 17:10:25 +0000813/** Calculate the matrix multiplication output shape of two tensors
814 *
815 * @param[in] input0 First input tensor info
816 * @param[in] input1 Second input tensor info
817 * @param[in] is_interleaved_transposed True if the input is interleaved transposed
818 * @param[in] reshape_info GEMM reshape info
819 *
820 * @return the calculated shape
821 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100822inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
823{
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000824 ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100825 ARM_COMPUTE_ERROR_ON_MSG(is_interleaved_transposed && reshape_info.reinterpret_input_as_3d(), "The first input tensor cannot be reinterpreted as 3D if is_interleaved_transposed is true");
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100826
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100827 const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000828 const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0;
829 const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100830 const int m = reshape_info.reinterpret_input_as_3d() ? input0.dimension(1) * input0.dimension(2) : input0.dimension(1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000831
832 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
833 // dimension of the output tensor
834 const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000835 const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100836 const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
837 const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000838
839 TensorShape output_shape{ input0.tensor_shape() };
840
841 output_shape.set(0, dim0);
842 output_shape.set(1, dim1);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000843 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100844 output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
845 output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000846
847 return output_shape;
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100848}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100849
Michalis Spyroud33fe342019-01-04 17:10:25 +0000850/** Calculate the matrix multiplication output shape of two tensors
851 *
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100852 * @note Deprecated. Remove when GEMMReshapeInfo is not used anymore by any other kernels
853 *
Michalis Spyroud33fe342019-01-04 17:10:25 +0000854 * @param[in] input0 First input tensor info
855 * @param[in] input1 Second input tensor info
856 * @param[in] gemm_info GEMM reshape info
857 *
858 * @return the calculated shape
859 */
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000860inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMReshapeInfo &gemm_info)
861{
862 ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
863
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000864 const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000865 const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d() != 0;
866 const int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d() : 1;
867
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000868 TensorShape output_shape{ input0.tensor_shape() };
869
Vidhya Sudhan Loganathanae1a89e2019-05-03 09:13:55 +0100870 if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
871 {
872 output_shape.set(0, gemm_info.n());
873 output_shape.set(1, gemm_info.m());
874 }
875 else
876 {
877 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
878 // dimension of the output tensor
879 const int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
880 output_shape.set(0, gemm_info.n());
881 output_shape.set(1, gemm_info.m() / depth_output_gemm3d);
882 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
883 output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
884 }
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000885
886 return output_shape;
887}
888
Michalis Spyroud33fe342019-01-04 17:10:25 +0000889/** Calculate the matrix multiplication output shape of two tensors
890 *
Gian Marco Iodice7026b302019-06-26 17:18:11 +0100891 * @param[in] input0 First input tensor info
892 * @param[in] input1 Second input tensor info
893 * @param[in] gemm_info GEMM kernel info used to retrieve the original dimensions of the input matrices
894 *
895 * @return the calculated shape
896 */
897inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMKernelInfo &gemm_info)
898{
899 ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
900
901 const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
902 const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
903 const unsigned int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d : 1;
904
905 TensorShape output_shape{ input0.tensor_shape() };
906
907 if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
908 {
909 output_shape.set(0, gemm_info.n);
910 output_shape.set(1, gemm_info.m);
911 }
912 else
913 {
914 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
915 // dimension of the output tensor
916 const unsigned int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
917 output_shape.set(0, gemm_info.n);
918 output_shape.set(1, gemm_info.m / depth_output_gemm3d);
919 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
920 output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
921 }
922
923 return output_shape;
924}
925
926/** Calculate the matrix multiplication output shape of two tensors
927 *
Michalis Spyroud33fe342019-01-04 17:10:25 +0000928 * @param[in] input Input tensor info
929 * @param[in] gemm_3d_depth (Optional) GEMM 3d depth
930 * @param[in] batch_size_on_z (Optional) True if batch size is on z axis
931 *
932 * @return the calculated shape
933 */
Georgios Pinitas932491f2018-09-21 16:33:15 +0100934inline TensorShape compute_output_stage_shape(const ITensorInfo &input, unsigned int gemm_3d_depth = 1, bool batch_size_on_z = false)
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100935{
936 ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1);
937
938 TensorShape output_shape = input.tensor_shape();
939 if(gemm_3d_depth > 1)
940 {
Georgios Pinitas932491f2018-09-21 16:33:15 +0100941 if(batch_size_on_z)
942 {
943 output_shape.shift_right(1);
944 }
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100945 output_shape.set(0, input.tensor_shape().x());
946 output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth);
947 output_shape.set(2, gemm_3d_depth);
948 }
949
950 return output_shape;
951}
952
Michalis Spyroud33fe342019-01-04 17:10:25 +0000953/** Calculate the strided slice output shape of a tensor
954 *
955 * @param[in] input Input tensor info
956 * @param[in] starts The starts of the dimensions of the input tensor to be sliced
957 * @param[in] ends The ends of the dimensions of the input tensor to be sliced
958 * @param[in] strides The strides of the dimensions of the input tensor to be sliced
959 * @param[in] begin_mask If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
960 * @param[in] end_mask If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
961 * @param[in] shrink_axis_mask If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1
962 *
963 * @return the calculated shape
964 */
Georgios Pinitas77589b52018-08-21 14:41:35 +0100965inline TensorShape compute_strided_slice_shape(const ITensorInfo &input,
966 const Coordinates &starts, const Coordinates &ends, const Coordinates &strides,
967 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
968{
969 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000970 return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
971}
Georgios Pinitas77589b52018-08-21 14:41:35 +0100972
Michalis Spyroud33fe342019-01-04 17:10:25 +0000973/** Calculate the slice output shape of a tensor
974 *
975 * @param[in] input_shape Input tensor info
976 * @param[in] starts The starts of the dimensions of the input tensor to be sliced
977 * @param[in] ends The ends of the dimensions of the input tensor to be sliced
978 *
979 * @return the calculated shape
980 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000981inline TensorShape compute_slice_shape(const TensorShape &input_shape, const Coordinates &starts, const Coordinates &ends)
982{
983 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100984
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000985 return compute_strided_slice_output_shape(input_shape,
986 starts, ends, BiStrides(),
987 0, construct_slice_end_mask(ends), 0);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100988}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100989
Michalis Spyroud33fe342019-01-04 17:10:25 +0000990/** Calculate the batch to space output shape of a tensor
991 *
992 * @param[in] input Input tensor info
993 * @param[in] block_x Block shape x value
994 * @param[in] block_y Block shape y value
995 *
996 * @return the calculated shape
997 */
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100998inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
999{
1000 ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0);
Michalis Spyrouf1addb62018-09-11 11:16:47 +01001001
1002 const DataLayout data_layout = input->data_layout();
1003 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1004 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou13a51e12018-09-18 13:09:30 +01001005 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyrouf1addb62018-09-11 11:16:47 +01001006
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +01001007 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrouf1addb62018-09-11 11:16:47 +01001008 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x);
1009 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y);
Michalis Spyrou13a51e12018-09-18 13:09:30 +01001010 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +01001011
1012 return output_shape;
1013}
Georgios Pinitas77589b52018-08-21 14:41:35 +01001014
Michalis Spyrou22f917c2019-05-21 13:30:10 +01001015/** Calculate the depth to space output shape of a tensor
1016 *
1017 * @param[in] input Input tensor info
1018 * @param[in] block Block shape value
1019 *
1020 * @return the calculated shape
1021 */
1022inline TensorShape compute_depth_to_space_shape(const ITensorInfo *input, int block)
1023{
1024 ARM_COMPUTE_ERROR_ON(block < 2);
1025
1026 const DataLayout data_layout = input->data_layout();
1027 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1028 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1029 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
1030
1031 TensorShape output_shape{ input->tensor_shape() };
1032 output_shape.set(idx_width, input->dimension(idx_width) * block);
1033 output_shape.set(idx_height, input->dimension(idx_height) * block);
1034 output_shape.set(idx_channel, input->dimension(idx_channel) / (block * block));
1035
1036 return output_shape;
1037}
1038
Michalis Spyroud33fe342019-01-04 17:10:25 +00001039/** Calculate the split output shape of a tensor
1040 *
1041 * @param[in] input Input tensor info
1042 * @param[in] axis Axis on which to split the input
1043 * @param[in] num_splits Number of splits
1044 *
1045 * @return the calculated shape
1046 */
Georgios Pinitase1a352c2018-09-03 12:42:19 +01001047inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
1048{
1049 TensorShape empty_shape;
1050 empty_shape.set(0, 0);
1051
1052 TensorShape out_shape{ input->tensor_shape() };
1053
1054 // Return empty shape if axis is invalid
1055 if(axis > input->tensor_shape().num_dimensions())
1056 {
1057 return empty_shape;
1058 }
1059
1060 size_t axis_size = out_shape[axis];
1061
1062 // Return empty shape if num_split is not valid
1063 if(axis_size % num_splits)
1064 {
1065 return empty_shape;
1066 }
1067
1068 out_shape[axis] = axis_size / num_splits;
1069 return out_shape;
1070}
1071
Michalis Spyroud33fe342019-01-04 17:10:25 +00001072/** Calculate the space to batch output shape of a tensor
1073 *
1074 * @param[in] input Input tensor info
1075 * @param[in] block_x Block shape x value
1076 * @param[in] block_y Block shape y value
1077 * @param[in] padding_left Left padding values
1078 * @param[in] padding_right Right padding values
1079 *
1080 * @return the calculated shape
1081 */
Michalis Spyrou16934a52018-08-21 18:03:58 +01001082inline TensorShape compute_space_to_batch_shape(const ITensorInfo *input, const int block_x, const int block_y, const Size2D &padding_left, const Size2D &padding_right)
1083{
1084 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrou13a51e12018-09-18 13:09:30 +01001085
1086 const DataLayout data_layout = input->data_layout();
1087 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1088 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1089 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
1090
1091 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x + padding_left.x() + padding_right.x());
1092 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y + padding_left.y() + padding_right.y());
1093 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou16934a52018-08-21 18:03:58 +01001094
1095 return output_shape;
1096}
Pablo Tello32521432018-11-15 14:43:10 +00001097
Manuel Bottini5b7d5372019-05-17 14:04:22 +01001098/** Calculate the space to batch output shape of a tensor
1099 *
1100 * @param[in] input Input tensor info
1101 * @param[in] block_shape Block shape value
1102 *
1103 * @return the calculated shape
1104 */
1105inline TensorShape compute_space_to_depth_shape(const ITensorInfo *input, int32_t block_shape)
1106{
1107 TensorShape output_shape{ input->tensor_shape() };
1108
1109 const DataLayout data_layout = input->data_layout();
1110 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1111 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1112 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
1113
1114 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_shape);
1115 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_shape);
1116 output_shape.set(idx_depth, input->tensor_shape()[idx_depth] / (block_shape * block_shape));
1117
1118 return output_shape;
1119}
1120
Michalis Spyroud33fe342019-01-04 17:10:25 +00001121/** Calculate the prior box output shape of a tensor
1122 *
1123 * @param[in] input Input tensor info
1124 * @param[in] info PriorBoxLayer info
1125 *
1126 * @return the calculated shape
1127 */
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +01001128inline TensorShape compute_prior_box_shape(const ITensorInfo &input, const PriorBoxLayerInfo &info)
1129{
1130 DataLayout data_layout = input.data_layout();
1131 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1132 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Pablo Tello32521432018-11-15 14:43:10 +00001133 const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +01001134
1135 TensorShape output_shape{};
1136 output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4);
1137 output_shape.set(1, 2);
1138
1139 return output_shape;
1140}
Michalis Spyrou16934a52018-08-21 18:03:58 +01001141
Michalis Spyroud33fe342019-01-04 17:10:25 +00001142/** Calculate the padded shape of a tensor
1143 *
1144 * @param[in] input_shape Input tensor shape
1145 * @param[in] padding Paddings list
1146 *
1147 * @return the calculated shape
1148 */
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001149inline TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
1150{
1151 TensorShape padded_shape = input_shape;
1152 for(size_t dim = 0; dim < padding.size(); ++dim)
1153 {
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001154 const auto &padding_pair = padding[dim];
1155 const uint32_t shape_on_index = (padded_shape.num_dimensions() <= dim) ? 1 : input_shape[dim];
1156 padded_shape.set(dim, padding_pair.first + shape_on_index + padding_pair.second);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001157 }
1158 return padded_shape;
1159}
1160
Michalis Spyroud33fe342019-01-04 17:10:25 +00001161/** Calculate the tiled shape of a tensor
1162 *
1163 * @param[in] input_shape Input tensor shape
1164 * @param[in] multiples Paddings list
1165 *
1166 * @return the calculated shape
1167 */
giuros013175fcf2018-11-21 09:59:17 +00001168inline TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples)
1169{
1170 TensorShape tiled_shape = input_shape;
1171 for(size_t dim = 0; dim < multiples.size(); ++dim)
1172 {
1173 tiled_shape.set(dim, input_shape[dim] * multiples[dim]);
1174 }
1175 return tiled_shape;
1176}
1177
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001178/** Calculate the reduced shape of a tensor given an axis
1179 *
1180 * @param[in] input Input tensor info
1181 * @param[in] axis Axis on which to perform reduction
1182 *
1183 * @return the calculated shape
1184 */
1185inline TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis)
1186{
1187 TensorShape output_shape{ input };
1188 output_shape.set(axis, 1);
1189
1190 return output_shape;
1191}
1192
Michalis Spyroud33fe342019-01-04 17:10:25 +00001193/** Calculate the upsampled shape of a tensor
1194 *
1195 * @param[in] input Input tensor info
1196 * @param[in] info Contains stride information (x and y)
1197 *
1198 * @return the calculated shape
1199 */
Michalis Spyrouceb889e2018-09-17 18:24:41 +01001200inline TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info)
1201{
1202 const DataLayout data_layout = input.data_layout();
1203 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1204 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1205
1206 TensorShape scale_out_shape(input.tensor_shape());
1207 const unsigned int out_x = input.dimension(idx_width) * info.x();
1208 const unsigned int out_y = input.dimension(idx_height) * info.y();
1209 scale_out_shape.set(idx_width, out_x);
1210 scale_out_shape.set(idx_height, out_y);
1211
1212 return scale_out_shape;
1213}
1214
Michalis Spyroud33fe342019-01-04 17:10:25 +00001215/** Get the tensor shape
1216 *
1217 * @param[in] data Input data
1218 *
1219 * @return the extracted tensor shape
1220 */
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001221template <typename T>
Georgios Pinitase2220552018-07-20 13:23:44 +01001222inline TensorShape extract_shape(T *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001223{
Georgios Pinitase2220552018-07-20 13:23:44 +01001224 return data->info()->tensor_shape();
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001225}
1226
John Kesapidescafec8f2019-02-19 15:53:59 +00001227inline TensorShape extract_shape(ITensorInfo *data)
John Kesapides917959c2019-02-04 12:37:29 +00001228{
1229 return data->tensor_shape();
1230}
John Kesapidescafec8f2019-02-19 15:53:59 +00001231inline TensorShape extract_shape(const ITensorInfo *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001232{
Georgios Pinitase2220552018-07-20 13:23:44 +01001233 return data->tensor_shape();
1234}
1235
1236inline TensorShape extract_shape(const TensorShape *data)
1237{
1238 return *data;
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001239}
1240
Michalis Spyroua9c44722019-04-05 17:18:36 +01001241inline TensorShape extract_shape(TensorShape *data)
1242{
1243 return *data;
1244}
1245
Michalis Spyroud33fe342019-01-04 17:10:25 +00001246/** Calculate the unstack shape of a tensor
1247 *
1248 * @param[in] input_shape Input tensor shape
1249 * @param[in] axis Axis on which to perform the unstack operation
1250 *
1251 * @return the calculated shape
1252 */
Pablo Tello54303692018-11-22 16:14:36 +00001253inline TensorShape calculate_unstack_shape(TensorShape input_shape, unsigned int axis)
1254{
1255 ARM_COMPUTE_ERROR_ON(axis > input_shape.num_dimensions());
1256 input_shape.remove_dimension(axis);
1257 return input_shape;
1258}
1259
Pablo Tello3dd5b682019-03-04 14:14:02 +00001260/** Calculate the concatenate output shape of the concatenate operation along a single axis
Michalis Spyroud33fe342019-01-04 17:10:25 +00001261 *
Pablo Tello3dd5b682019-03-04 14:14:02 +00001262 * @param[in] input Vector containing the shapes of the inputs
1263 * @param[in] axis Axis along which to concatenate the input tensors
Michalis Spyroud33fe342019-01-04 17:10:25 +00001264 *
1265 * @return the calculated shape
1266 */
Georgios Pinitase29acf12018-07-16 14:40:09 +01001267template <typename T>
Pablo Tello3dd5b682019-03-04 14:14:02 +00001268inline TensorShape calculate_concatenate_shape(const std::vector<T *> &input, size_t axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001269{
Pablo Tello3dd5b682019-03-04 14:14:02 +00001270 TensorShape out_shape = extract_shape(input[0]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001271
Georgios Pinitasdcd949d2019-04-17 11:04:28 +01001272#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
Michalis Spyroua9c44722019-04-05 17:18:36 +01001273 // All dimensions must match except the axis one
1274 for(unsigned int i = 0; i < MAX_DIMS; ++i)
1275 {
1276 if(i == axis)
1277 {
1278 continue;
1279 }
1280
1281 for(const auto &tensor : input)
1282 {
1283 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
1284 const TensorShape shape = extract_shape(tensor);
1285 ARM_COMPUTE_ERROR_ON(out_shape[i] != shape[i]);
1286 }
1287 }
Georgios Pinitasdcd949d2019-04-17 11:04:28 +01001288#endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
Michalis Spyroua9c44722019-04-05 17:18:36 +01001289
1290 // Calculate output shape
Pablo Tello3dd5b682019-03-04 14:14:02 +00001291 size_t new_size = 0;
1292 for(const auto &tensor : input)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001293 {
Georgios Pinitase2220552018-07-20 13:23:44 +01001294 const TensorShape shape = extract_shape(tensor);
Pablo Tello3dd5b682019-03-04 14:14:02 +00001295 new_size += shape[axis];
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001296 }
1297
Pablo Tello3dd5b682019-03-04 14:14:02 +00001298 out_shape.set(axis, new_size);
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001299
1300 return out_shape;
1301}
Michalis Spyroud33fe342019-01-04 17:10:25 +00001302/** Calculate the stack output shape of a tensor
1303 *
1304 * @param[in] a Input tensor info
1305 * @param[in] axis Axis on which to perform the stack operation
1306 * @param[in] num_tensors Number of tensors to stack
1307 *
1308 * @return the calculated shape
1309 */
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00001310inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
1311{
1312 ARM_COMPUTE_ERROR_ON(axis > a.num_dimensions());
1313 ARM_COMPUTE_ERROR_ON(a.num_dimensions() > 4);
1314
1315 TensorShape shape_out{ a.tensor_shape() };
1316 shape_out.set(axis, num_tensors);
1317
1318 unsigned int i_shift = 0;
1319
1320 for(unsigned int i = 0; i < a.num_dimensions(); ++i)
1321 {
1322 if(i == axis)
1323 {
1324 i_shift++;
1325 }
1326
1327 shape_out.set(i + i_shift, a.tensor_shape()[i]);
1328 }
1329 return shape_out;
1330}
Manuel Bottini8529bd62018-11-21 11:53:04 +00001331
1332inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
1333{
1334 ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1);
1335 ARM_COMPUTE_ERROR_ON(input_shape.num_dimensions() > 4);
1336 ARM_COMPUTE_ERROR_ON(actual_axis >= input_shape.num_dimensions());
1337
1338 TensorShape output_shape = input_shape;
1339 output_shape[actual_axis] = indices_shape[0];
1340
1341 return output_shape;
1342}
Georgios Pinitas358ca202017-12-07 16:47:52 +00001343} // namespace shape_calculator
1344} // namespace misc
1345} // namespace arm_compute
1346#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */