blob: b46b1b2535083b78d92d7b11bb95845787b0bd91 [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"
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000029#include "arm_compute/core/Utils.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000030
Georgios Pinitas77589b52018-08-21 14:41:35 +010031#include "arm_compute/core/utils/helpers/tensor_transform.h"
32
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000033#include <cmath>
34
Georgios Pinitas358ca202017-12-07 16:47:52 +000035namespace arm_compute
36{
37namespace misc
38{
39namespace shape_calculator
40{
Michalis Spyroud33fe342019-01-04 17:10:25 +000041/** Calculate the output tensor shape of a vector input given the convolution dimensions
42 *
43 * @param[in] input Input tensor shape
44 * @param[in] conv_w Convolution width
45 * @param[in] conv_h Convolution height
46 * @param[in] data_layout Data layout
47 *
48 * @return the calculated shape
49 */
Abe Mbise7784c832018-05-31 16:48:41 +010050inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
51{
52 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
53 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
54 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
55
56 TensorShape output_shape(input);
57 output_shape.set(idx_w, conv_w);
58 output_shape.set(idx_h, conv_h);
59 output_shape.set(idx_c, input.x() / (conv_w * conv_h));
60
61 return output_shape;
62}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010063
Michalis Spyroud33fe342019-01-04 17:10:25 +000064/** Calculate the permuted shape of an input given a permutation vector
65 *
66 * @param[in] input Input tensor info
67 * @param[in] perm Permutation vector
68 *
69 * @return the calculated shape
70 */
Pablo Tello00afd112018-01-04 10:34:24 +000071inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
72{
73 TensorShape output_shape = input.tensor_shape();
74 permute(output_shape, perm);
75 return output_shape;
76}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010077
Michalis Spyroud33fe342019-01-04 17:10:25 +000078/** Calculate the output shape of the reorg layer given a stride
79 *
80 * @param[in] input Input tensor info
81 * @param[in] stride Stride
82 *
83 * @return the calculated shape
84 */
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010085inline TensorShape compute_reorg_output_shape(const ITensorInfo &input, int32_t stride)
86{
Gian Marco Iodice477531c2018-08-21 17:53:38 +010087 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
88 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
89 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010090
Gian Marco Iodice477531c2018-08-21 17:53:38 +010091 ARM_COMPUTE_ERROR_ON(stride <= 0);
92 ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_width] % stride != 0), "The width of the input tensor must be a multiple of stride");
93 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 +010094
95 TensorShape output_shape{ input.tensor_shape() };
Gian Marco Iodice477531c2018-08-21 17:53:38 +010096
97 output_shape.set(idx_width, output_shape[idx_width] / stride);
98 output_shape.set(idx_height, output_shape[idx_height] / stride);
99 output_shape.set(idx_channel, output_shape[idx_channel] * stride * stride);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +0100100
101 return output_shape;
102}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100103
Michalis Spyroud33fe342019-01-04 17:10:25 +0000104/** Calculate the reshaped shape of the weights
105 *
106 * @param[in] weights Weights tensor info
107 * @param[in] has_bias (Optional) Set to true if there is bias
108 * @param[in] num_groups (Optional) Number of groups
109 *
110 * @return the calculated shape of the reshaped weights
111 */
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100112inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000113{
Giorgio Arena088c2b02018-08-07 16:59:05 +0100114 // 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 +0100115 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100116 ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100117 ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100118
Georgios Pinitas78c00902018-01-09 17:33:11 +0000119 // Calculate output shape
120 TensorShape weights_reshaped{ weights.tensor_shape() };
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100121 weights_reshaped.set(3, weights_reshaped[3] / num_groups);
122
Georgios Pinitas78c00902018-01-09 17:33:11 +0000123 weights_reshaped.collapse(3);
124 const size_t tmp_dim = weights_reshaped[0];
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100125 weights_reshaped.set(0, weights_reshaped[1]);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000126 weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100127 if(weights.num_dimensions() < 5)
128 {
129 weights_reshaped.set(2, num_groups);
130 }
Georgios Pinitas78c00902018-01-09 17:33:11 +0000131
132 return weights_reshaped;
133}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100134
Michalis Spyroud33fe342019-01-04 17:10:25 +0000135/** Calculate the Left Hand Side matrix reshaped shape
136 *
137 * @param[in] a Input tensor info
138 * @param[in] lhs_info Left Hand Side matrix information
139 * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
140 *
141 * @return the calculated shape
142 */
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000143inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false)
144{
145 ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0);
146 ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0);
147 ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0);
148
149 // Input width/height
150 const unsigned int input_width = a.dimension(0);
151 const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1);
152
153 // Number of horizontal/vertical blocks in the input tensor
154 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(lhs_info.k0));
155 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(lhs_info.m0));
156
157 // Block size
158 const unsigned int block_size = lhs_info.m0 * lhs_info.k0;
159
160 // Output width/height
161 const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0;
162 const unsigned int output_height = std::ceil(num_vert_blocks / static_cast<float>(lhs_info.v0));
163
164 TensorShape lhs_shape{ a.tensor_shape() };
165 lhs_shape.set(0, output_width);
166 lhs_shape.set(1, output_height);
167
168 if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2))
169 {
170 // When the data format is NHWC and the shapes are Nx1x1
171 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
172 // To avoid failures by removing a dimension that doesn't exist
173 // check if the number of dimensions is greater than 2.
174 lhs_shape.remove_dimension(2);
175 }
176
177 return lhs_shape;
178}
179
Michalis Spyroud33fe342019-01-04 17:10:25 +0000180/** Calculate the Right Hand Side matrix reshaped shape
181 *
182 * @param[in] a Input tensor info
183 * @param[in] rhs_info Right Hand Side matrix information
184 *
185 * @return the calculated shape
186 */
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000187inline TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
188{
189 ARM_COMPUTE_ERROR_ON(rhs_info.n0 == 0);
190 ARM_COMPUTE_ERROR_ON(rhs_info.k0 == 0);
191 ARM_COMPUTE_ERROR_ON(rhs_info.h0 == 0);
192
193 // Input width/height
194 const unsigned int input_width = a.dimension(0);
195 const unsigned int input_height = a.dimension(1);
196
197 // Number of horizontal/vertical blocks in the input tensor
198 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(rhs_info.n0));
199 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(rhs_info.k0));
200
201 // Block size
202 const unsigned int block_size = rhs_info.n0 * rhs_info.k0;
203
204 // Output width/height
205 const unsigned int output_width = block_size * num_vert_blocks * rhs_info.h0;
206 const unsigned int output_height = std::ceil(num_horiz_blocks / static_cast<float>(rhs_info.h0));
207
208 TensorShape rhs_shape{ a.tensor_shape() };
209 rhs_shape.set(0, output_width);
210 rhs_shape.set(1, output_height);
211
212 return rhs_shape;
213}
214
Michalis Spyroud33fe342019-01-04 17:10:25 +0000215/** Calculate the interleaved shape of an input tensor
216 *
217 * @param[in] a Input tensor info
218 * @param[in] mult_interleave4x4_height (Optional) Interleave4x4 height
219 * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
220 *
221 * @return the calculated shape
222 */
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100223inline 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 +0000224{
Gian Marco36a0a462018-01-12 10:21:40 +0000225 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
226 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
227 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000228 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000229 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100230 if(reinterpret_input_as_3d)
231 {
232 const int M = a.dimension(1) * a.dimension(2);
233 const int height = std::ceil(M / static_cast<float>(interleave_width));
234 shape_interleaved_a.set(1, height);
Isabella Gottardi089695f2018-10-17 18:04:15 +0100235
236 // When the data format is NHWC and the shapes are Nx1x1
237 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
238 // To avoid failures by removing a dimension that doesn't exist
239 // check if the number of dimensions is greater than 2.
240 if(shape_interleaved_a.num_dimensions() > 2)
241 {
242 shape_interleaved_a.remove_dimension(2);
243 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100244 }
245 else
246 {
247 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
248 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000249
250 return shape_interleaved_a;
251}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100252
giuros016d109962019-01-07 17:47:19 +0000253/** Calculate the reshaped shape of the weights to use in depthwise convolution
254 *
255 * @param[in] input Input tensor info
256 * @param[in] info Depthwise convolution information to be used for reshaping.
257 *
258 * @return the calculated shape
259 */
260inline TensorShape compute_reshaped_depthwise_weights_shape(const ITensorInfo &input, const DepthwiseConvolutionReshapeInfo &info)
261{
262 const auto data_layout = input.data_layout();
263 TensorShape weights_shape{};
264
265 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
266 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
267 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
268 const size_t num_channels = input.dimension(channel_idx);
269 const size_t num_rows = input.dimension(height_idx);
270 const size_t num_cols = input.dimension(width_idx);
271
272 weights_shape.set(0, num_rows * num_cols * info.c0);
273 weights_shape.set(1, DIV_CEIL(num_channels, info.c0));
274 return weights_shape;
275}
276
Michalis Spyroud33fe342019-01-04 17:10:25 +0000277/** Calculate the transposed 1xW shape
278 *
279 * @param[in] b Input tensor info
280 *
281 * @return the calculated shape
282 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000283inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
284{
285 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
286 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
287 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
288 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
289
290 return shape_transposed1xW_b;
291}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100292
Michalis Spyroud33fe342019-01-04 17:10:25 +0000293/** Calculate the transposed 1xW width element shape
294 *
295 * @param[in] b Input tensor info
296 * @param[in] mult_transpose1xW_width (Optional) Transpose1xW width
297 *
298 * @return the calculated shape
299 */
Gian Marco36a0a462018-01-12 10:21:40 +0000300inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000301{
Gian Marco36a0a462018-01-12 10:21:40 +0000302 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
303 // The transpose1xW output matrix will have the following shape:
304 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
305 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000306 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000307 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000308 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
309 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
310
311 return shape_transposed1xW_b;
312}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100313
Michalis Spyroud33fe342019-01-04 17:10:25 +0000314/** Calculate the reductionA shape used in GEMMLowp
315 *
316 * @param[in] b Input tensor info
317 *
318 * @return the calculated shape
319 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000320inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
321{
322 TensorShape shape_vector_sum_col{ b.tensor_shape() };
323 if(shape_vector_sum_col.num_dimensions() > 1)
324 {
325 shape_vector_sum_col.remove_dimension(1);
326 }
327
328 return shape_vector_sum_col;
329}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100330
Michalis Spyroud33fe342019-01-04 17:10:25 +0000331/** Calculate the reductionB shape used in GEMMLowp
332 *
333 * @param[in] a Input tensor info
334 *
335 * @return the calculated shape
336 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000337inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
338{
339 TensorShape shape_vector_sum_row{ a.tensor_shape() };
340 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
Georgios Pinitas932491f2018-09-21 16:33:15 +0100341 if(shape_vector_sum_row.num_dimensions() > 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000342 {
343 shape_vector_sum_row.remove_dimension(1);
344 }
345
346 return shape_vector_sum_row;
347}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100348
Michalis Spyroud33fe342019-01-04 17:10:25 +0000349/** Calculate the Col2Im shape
350 *
351 * @param[in] input Input tensor info
352 * @param[in] convolved_dims Convolved dimensions
353 * @param[in] batch_size_on_z True if batch size is on z axis
354 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
355 *
356 * @return the calculated shape
357 */
Giorgio Arena226e4b92018-08-23 12:00:02 +0100358inline 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 +0000359{
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100360 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arena226e4b92018-08-23 12:00:02 +0100361 ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area()));
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100362 ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
363
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100364 const DataLayout data_layout = input.data_layout();
365 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
366 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
367 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100368
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100369 TensorShape col2im_shape{ input.tensor_shape() };
370 // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape,
371 // as first three will be override by H,W,C data
372 if(batch_size_on_z && num_groups == 1)
373 {
374 col2im_shape.shift_right(1);
375 }
376 col2im_shape.set(width_idx, convolved_dims.width);
377 col2im_shape.set(height_idx, convolved_dims.height);
378 col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000379
380 return col2im_shape;
381}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100382
Michalis Spyroud33fe342019-01-04 17:10:25 +0000383/** Calculate the transposed shape of a tensor
384 *
385 * @param[in] input Input tensor info
386 *
387 * @return the calculated shape
388 */
Georgios Pinitas358ca202017-12-07 16:47:52 +0000389inline TensorShape compute_transposed_shape(const ITensorInfo &input)
390{
391 TensorShape shape_transposed{ input.tensor_shape() };
392
393 shape_transposed.set(0, input.dimension(1));
394 shape_transposed.set(1, input.dimension(0));
395
396 return shape_transposed;
397}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100398
Michalis Spyroud33fe342019-01-04 17:10:25 +0000399/** Calculate the depthwise convolution output shape of a tensor
400 *
401 * @param[in] input Input tensor info
402 * @param[in] weights Weights tensor info
403 * @param[in] conv_info Padding and stride information to use for the convolution.
404 * @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 +0100405 * @param[in] dilation Dilation, in elements, across x and y. Defaults to (1, 1).
Michalis Spyroud33fe342019-01-04 17:10:25 +0000406 *
407 * @return the calculated shape
408 */
Usama Arife73686a2019-04-08 17:30:48 +0100409inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier, const Size2D &dilation = Size2D(1U,
410 1U))
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000411{
412 const TensorShape input_shape{ input.tensor_shape() };
413 const TensorShape weights_shape{ weights.tensor_shape() };
414
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000415 const DataLayout data_layout = input.data_layout();
416 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
417 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena76572242018-04-04 17:44:26 +0100418 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000419
Usama Arife73686a2019-04-08 17:30:48 +0100420 const DataLayout weights_data_layout = weights.data_layout();
421 const int weights_width_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::WIDTH);
422 const int weights_height_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::HEIGHT);
giuros016d109962019-01-07 17:47:19 +0000423
424 unsigned int output_width = 0;
425 unsigned int output_height = 0;
426 std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
Usama Arife73686a2019-04-08 17:30:48 +0100427 weights_shape[weights_width_idx], weights_shape[weights_height_idx],
428 conv_info, dilation);
giuros016d109962019-01-07 17:47:19 +0000429
430 TensorShape output_shape{ input_shape };
431 output_shape.set(width_idx, output_width);
432 output_shape.set(height_idx, output_height);
433 output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier);
434
435 return output_shape;
436}
437
Michalis Spyroud33fe342019-01-04 17:10:25 +0000438/** Calculate the upsampled output shape used for deconvolution
439 *
440 * @param[in] input Input tensor info
441 * @param[in] weights Weights tensor shape
442 * @param[in] sx Stride on x axis
443 * @param[in] sy Stride on y axis
444 * @param[in] inner_border_right The number of zeros added to right edge of the input.
445 * @param[in] inner_border_top The number of zeros added to top edge of the input.
446 * @param[in] out_dims Output shape dimensions
447 * @param[in] padx Padding on x axis
448 * @param[in] pady Padding on y axis
449 *
450 * @return the calculated shape
451 */
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100452inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy, unsigned int inner_border_right,
453 unsigned int inner_border_top,
454 std::pair<unsigned int, unsigned int> &out_dims, unsigned int &padx, unsigned int &pady)
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000455{
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100456 const DataLayout data_layout = input.data_layout();
457 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
458 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
459
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100460 // Find the upsampled dimensions
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100461 unsigned int out_x = (input.dimension(idx_w) - 1) * sx + inner_border_right + 1;
462 unsigned int out_y = (input.dimension(idx_h) - 1) * sy + inner_border_top + 1;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100463
464 // Find the padding needed for the convolution with stride 1 in order to match output shape
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100465 padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1);
466 pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100467 out_x += padx;
468 out_y += pady;
469
470 TensorShape scale_out_shape(input.tensor_shape());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100471 scale_out_shape.set(idx_w, out_x);
472 scale_out_shape.set(idx_h, out_y);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000473
474 return scale_out_shape;
475}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100476
Michalis Spyroud33fe342019-01-04 17:10:25 +0000477/** Calculate the output shape of the deconvolution layer
478 *
479 * @param[in] out_dims Output x and y shape dimensions
480 * @param[in] input Input tensor info
481 * @param[in] weights Weights tensor shape
482 *
483 * @return the calculated shape
484 */
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100485inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
486{
487 const TensorShape input_shape{ input.tensor_shape() };
488 const TensorShape weights_shape{ weights.tensor_shape() };
489
490 const DataLayout data_layout = input.data_layout();
491 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
492 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
493 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
494 const int batch_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
495
496 TensorShape out_shape{ input_shape };
497 out_shape.set(width_idx, out_dims.first);
498 out_shape.set(height_idx, out_dims.second);
499 out_shape.set(channel_idx, weights_shape[batch_idx]);
500 return out_shape;
501}
502
Michalis Spyroud33fe342019-01-04 17:10:25 +0000503/** Calculate the im2col output shape of a tensor
504 *
505 * @param[in] input Input tensor info
506 * @param[in] kernel_dims The kernel dimensions (width and height).
507 * @param[in] conv_info Contains padding and stride information
508 * @param[in] has_bias In case biases are provided expands the matrix with 1
509 * @param[in] dilation Dilation, in elements, across x and y
510 * @param[in] batch_size_on_z True if batch size is on z axis
511 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
512 *
513 * @return the calculated shape
514 */
Giorgio Arena0f170392018-07-18 16:13:12 +0100515inline 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,
516 unsigned int num_groups = 1)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000517{
Giorgio Arena0f170392018-07-18 16:13:12 +0100518 // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
519 // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
520
521 ARM_COMPUTE_ERROR_ON(num_groups == 0);
522 ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
523 ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000524
525 TensorShape output_shape{ input->tensor_shape() };
526
527 const DataLayout data_layout = input->data_layout();
528 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
529 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
530 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
531
532 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 +0100533 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 +0100534 output_shape.set(1, (out_dims.first * out_dims.second));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100535 if(batch_size_on_z && output_shape.num_dimensions() >= 3)
536 {
537 output_shape.remove_dimension(2);
538 }
539 else
540 {
Giorgio Arena0f170392018-07-18 16:13:12 +0100541 output_shape.set(2, num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100542 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000543
544 return output_shape;
545}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100546
Michalis Spyroud33fe342019-01-04 17:10:25 +0000547/** Calculate the flattened output shape of a tensor
548 *
549 * @param[in] input Input tensor info
550 *
551 * @return the calculated shape
552 */
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100553inline TensorShape compute_flatten_shape(const ITensorInfo *input)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000554{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100555 // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
556
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000557 TensorShape output_shape{ input->tensor_shape() };
558
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100559 output_shape.collapse(3);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000560
561 return output_shape;
562}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100563
Michalis Spyroud33fe342019-01-04 17:10:25 +0000564/** Calculate the softmax output shape of a tensor
565 *
566 * @param[in] input Input tensor info
567 * @param[in] axis (Optional) Softmax axis
568 *
569 * @return the calculated shape
570 */
giuros01efbf6c82018-09-03 09:53:53 +0100571inline TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis = 1)
572{
573 // The output shape will be a 2D version of the input. For instance:
574 // - [x,y,z] and axis 1 will return [x, y*z]
575 // - [x,y,z,w] and axis 2 will return [x*y, w*z]
576 // - [x,y,z,w] and axis 3 will return [x*y*z, w]
577 TensorShape shape2D = input->tensor_shape();
578
579 if(axis < input->num_dimensions())
580 {
581 // Collapse from axis onward (this changes the shape)
582 shape2D.collapse_from(axis);
583
584 // Collapse the rest (collapse is inclusive)
585 shape2D.collapse(shape2D.num_dimensions() - 1);
586 }
587 else
588 {
589 // Collapse everything
590 shape2D.collapse(shape2D.num_dimensions());
591 }
592
593 if(axis == 0)
594 {
595 // If axis is zero the first dim should be one. Since
596 // collapse is an inclusive operation we need to shift
597 shape2D.shift_right(1);
598 }
599
600 return shape2D;
601}
602
Michalis Spyroud33fe342019-01-04 17:10:25 +0000603/** Calculate the winograd filter transform shape
604 *
605 * @param[in] input Input tensor info
606 * @param[in] winograd_info Winograd information
607 *
608 * @return the calculated shape
609 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000610inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000611{
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000612 TensorShape tensor_shape{ input.tensor_shape() };
613
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000614 const Size2D kernel_size = winograd_info.kernel_size;
615 const Size2D output_tile_size = winograd_info.output_tile_size;
616 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 +0000617
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000618 tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
619 tensor_shape.set(Window::DimX, input.dimension(3));
620 tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
621 tensor_shape.set(Window::DimZ, input_tile_size.area());
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000622
623 return tensor_shape;
624}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100625
Michalis Spyroud33fe342019-01-04 17:10:25 +0000626/** Calculate the winograd input transform shape
627 *
628 * @param[in] input Input tensor info
629 * @param[in] winograd_info Winograd information
630 *
631 * @return the calculated shape
632 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000633inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000634{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000635 const PadStrideInfo conv_info = winograd_info.convolution_info;
636 const Size2D kernel_size = winograd_info.kernel_size;
637 const Size2D output_tile_size = winograd_info.output_tile_size;
638 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
639
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100640 const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
641 const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
642 const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000643
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100644 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
645 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
646 kernel_size,
647 output_tile_size,
648 conv_info);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100649
650 const unsigned int width = input.tensor_shape()[idx_c];
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100651 const unsigned int height = num_tiles.area();
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000652 const unsigned int depth = input_tile_size.area();
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000653
654 TensorShape output_shape{ input.tensor_shape() };
655 output_shape.set(0, width);
656 output_shape.set(1, height);
657 output_shape.set(2, depth);
658
659 return output_shape;
660}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100661
Michalis Spyroud33fe342019-01-04 17:10:25 +0000662/** Calculate the winograd output transform shape
663 *
664 * @param[in] input Input tensor info
665 * @param[in] winograd_info Winograd information
666 *
667 * @return the calculated shape
668 */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000669inline TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000670{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000671 const PadStrideInfo conv_info = winograd_info.convolution_info;
672 const Size2D kernel_size = winograd_info.kernel_size;
673 const Size2D input_dimensions = winograd_info.input_dimensions;
674 const DataLayout data_layout = winograd_info.output_data_layout;
675
676 // Compute output shape
677 unsigned int output_width = 0;
678 unsigned int output_height = 0;
679 std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
680 kernel_size.width, kernel_size.height, conv_info);
681
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000682 TensorShape tensor_shape{ input.tensor_shape() };
683
684 // Output dimension
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000685 const unsigned int out_w = output_width;
686 const unsigned int out_h = output_height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000687 const unsigned int out_c = input.dimension(0);
688
689 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w);
690 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h);
691 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c);
692
693 return tensor_shape;
694}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100695
Michalis Spyroud33fe342019-01-04 17:10:25 +0000696/** Calculate the deep convolution shape output shape of a tensor
697 *
698 * @param[in] input Input tensor info
699 * @param[in] weights Weights tensor info
700 * @param[in] conv_info Contains padding and stride information
701 *
702 * @return the calculated shape
703 */
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000704inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
705{
706 const TensorShape input_shape{ input.tensor_shape() };
707 const TensorShape weights_shape{ weights.tensor_shape() };
708
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000709 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
710 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
711 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
712
Giorgio Arenac0f54432018-03-16 14:02:34 +0000713 const unsigned int input_width = input_shape[idx_width];
714 const unsigned int input_height = input_shape[idx_height];
715 const unsigned int weights_width = weights_shape[idx_width];
716 const unsigned int weights_height = weights_shape[idx_height];
717 const unsigned int weights_out_channel = weights_shape[3];
718 unsigned int output_width = 0;
719 unsigned int output_height = 0;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000720 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 +0000721
722 TensorShape output_shape{ input_shape };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000723 output_shape.set(idx_width, output_width);
724 output_shape.set(idx_height, output_height);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000725 output_shape.set(idx_channel, weights_out_channel);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000726
727 return output_shape;
728}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100729
Michalis Spyroud33fe342019-01-04 17:10:25 +0000730/** Calculate the min/max shape output shape of a tensor
731 *
732 * @param[in] input Input tensor info
733 *
734 * @return the calculated shape
735 */
Alex Gilday60954c62018-03-05 16:22:48 +0000736inline TensorShape compute_min_max_shape(const ITensorInfo *input)
737{
738 TensorShape output_shape{ input->tensor_shape() };
739 output_shape.set(Window::DimX, 2);
740 output_shape.remove_dimension(1);
741 output_shape.remove_dimension(1);
742
743 return output_shape;
744}
745
Michalis Spyroud33fe342019-01-04 17:10:25 +0000746/** Calculate the output pool shape of a tensor
747 *
748 * @param[in] input Input tensor info
749 * @param[in] pool_info Pooling layer info
750 *
751 * @return the calculated shape
752 */
Michalis Spyroue74b2012018-04-18 09:49:16 +0100753inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
754{
755 unsigned int pooled_w = 0;
756 unsigned int pooled_h = 0;
757
Giorgio Arena3c520c52018-05-01 11:47:24 +0100758 TensorShape output_shape{ input.tensor_shape() };
Michalis Spyroue74b2012018-04-18 09:49:16 +0100759
Giorgio Arena3c520c52018-05-01 11:47:24 +0100760 const bool is_global_pooling = pool_info.is_global_pooling();
761 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
762 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
763 const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width;
764 const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height;
765
766 std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
767 output_shape[idx_height],
Michalis Spyroue74b2012018-04-18 09:49:16 +0100768 pool_size_x,
769 pool_size_y,
770 pool_info.pad_stride_info());
771
Giorgio Arena3c520c52018-05-01 11:47:24 +0100772 output_shape.set(idx_width, pooled_w);
773 output_shape.set(idx_height, pooled_h);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100774
775 return output_shape;
776}
777
George Wort44b4e972019-01-08 11:41:54 +0000778/** Calculate the output roi align shape of a tensor
779 *
780 * @param[in] input Input tensor info
781 * @param[in] rois Rois tensor info
782 * @param[in] pool_info Pooling layer info
783 *
784 * @return the calculated shape
785 */
786inline TensorShape compute_roi_align_shape(const ITensorInfo &input, const ITensorInfo &rois, ROIPoolingLayerInfo pool_info)
787{
788 TensorShape output_shape{ input.tensor_shape() };
789
790 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
791 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
792
793 output_shape.set(idx_width, pool_info.pooled_width());
794 output_shape.set(idx_height, pool_info.pooled_height());
795 output_shape.set(3, rois.dimension(1));
796
797 return output_shape;
798}
799
Michalis Spyroud33fe342019-01-04 17:10:25 +0000800/** Calculate the RNN shape of a tensor
801 *
802 * @param[in] input Input tensor info
803 * @param[in] batch_size Batch size
804 *
805 * @return the calculated shape
806 */
Michalis Spyrou36a559e2018-03-20 10:30:58 +0000807inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
808{
809 TensorShape output_shape{ input->tensor_shape() };
810 output_shape.set(1, batch_size);
811
812 return output_shape;
813}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100814
Michalis Spyroud33fe342019-01-04 17:10:25 +0000815/** Calculate the matrix multiplication output shape of two tensors
816 *
817 * @param[in] input0 First input tensor info
818 * @param[in] input1 Second input tensor info
819 * @param[in] is_interleaved_transposed True if the input is interleaved transposed
820 * @param[in] reshape_info GEMM reshape info
821 *
822 * @return the calculated shape
823 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100824inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
825{
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000826 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 +0100827 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 +0100828
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100829 const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000830 const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0;
831 const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100832 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 +0000833
834 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
835 // dimension of the output tensor
836 const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000837 const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100838 const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
839 const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000840
841 TensorShape output_shape{ input0.tensor_shape() };
842
843 output_shape.set(0, dim0);
844 output_shape.set(1, dim1);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000845 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100846 output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
847 output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000848
849 return output_shape;
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100850}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100851
Michalis Spyroud33fe342019-01-04 17:10:25 +0000852/** Calculate the matrix multiplication output shape of two tensors
853 *
854 * @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
868 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
869 // dimension of the output tensor
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000870 const int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000871
872 TensorShape output_shape{ input0.tensor_shape() };
873
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000874 output_shape.set(0, gemm_info.n());
875 output_shape.set(1, gemm_info.m() / depth_output_gemm3d);
876 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
877 output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000878
879 return output_shape;
880}
881
Michalis Spyroud33fe342019-01-04 17:10:25 +0000882/** Calculate the matrix multiplication output shape of two tensors
883 *
884 * @param[in] input Input tensor info
885 * @param[in] gemm_3d_depth (Optional) GEMM 3d depth
886 * @param[in] batch_size_on_z (Optional) True if batch size is on z axis
887 *
888 * @return the calculated shape
889 */
Georgios Pinitas932491f2018-09-21 16:33:15 +0100890inline 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 +0100891{
892 ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1);
893
894 TensorShape output_shape = input.tensor_shape();
895 if(gemm_3d_depth > 1)
896 {
Georgios Pinitas932491f2018-09-21 16:33:15 +0100897 if(batch_size_on_z)
898 {
899 output_shape.shift_right(1);
900 }
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100901 output_shape.set(0, input.tensor_shape().x());
902 output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth);
903 output_shape.set(2, gemm_3d_depth);
904 }
905
906 return output_shape;
907}
908
Michalis Spyroud33fe342019-01-04 17:10:25 +0000909/** Calculate the strided slice output shape of a tensor
910 *
911 * @param[in] input Input tensor info
912 * @param[in] starts The starts of the dimensions of the input tensor to be sliced
913 * @param[in] ends The ends of the dimensions of the input tensor to be sliced
914 * @param[in] strides The strides of the dimensions of the input tensor to be sliced
915 * @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.
916 * @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.
917 * @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
918 *
919 * @return the calculated shape
920 */
Georgios Pinitas77589b52018-08-21 14:41:35 +0100921inline TensorShape compute_strided_slice_shape(const ITensorInfo &input,
922 const Coordinates &starts, const Coordinates &ends, const Coordinates &strides,
923 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
924{
925 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000926 return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
927}
Georgios Pinitas77589b52018-08-21 14:41:35 +0100928
Michalis Spyroud33fe342019-01-04 17:10:25 +0000929/** Calculate the slice output shape of a tensor
930 *
931 * @param[in] input_shape Input tensor info
932 * @param[in] starts The starts of the dimensions of the input tensor to be sliced
933 * @param[in] ends The ends of the dimensions of the input tensor to be sliced
934 *
935 * @return the calculated shape
936 */
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000937inline TensorShape compute_slice_shape(const TensorShape &input_shape, const Coordinates &starts, const Coordinates &ends)
938{
939 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100940
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000941 return compute_strided_slice_output_shape(input_shape,
942 starts, ends, BiStrides(),
943 0, construct_slice_end_mask(ends), 0);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100944}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100945
Michalis Spyroud33fe342019-01-04 17:10:25 +0000946/** Calculate the batch to space output shape of a tensor
947 *
948 * @param[in] input Input tensor info
949 * @param[in] block_x Block shape x value
950 * @param[in] block_y Block shape y value
951 *
952 * @return the calculated shape
953 */
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100954inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
955{
956 ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0);
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100957
958 const DataLayout data_layout = input->data_layout();
959 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
960 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100961 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100962
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100963 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100964 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x);
965 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y);
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100966 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100967
968 return output_shape;
969}
Georgios Pinitas77589b52018-08-21 14:41:35 +0100970
Michalis Spyroud33fe342019-01-04 17:10:25 +0000971/** Calculate the split output shape of a tensor
972 *
973 * @param[in] input Input tensor info
974 * @param[in] axis Axis on which to split the input
975 * @param[in] num_splits Number of splits
976 *
977 * @return the calculated shape
978 */
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100979inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
980{
981 TensorShape empty_shape;
982 empty_shape.set(0, 0);
983
984 TensorShape out_shape{ input->tensor_shape() };
985
986 // Return empty shape if axis is invalid
987 if(axis > input->tensor_shape().num_dimensions())
988 {
989 return empty_shape;
990 }
991
992 size_t axis_size = out_shape[axis];
993
994 // Return empty shape if num_split is not valid
995 if(axis_size % num_splits)
996 {
997 return empty_shape;
998 }
999
1000 out_shape[axis] = axis_size / num_splits;
1001 return out_shape;
1002}
1003
Michalis Spyroud33fe342019-01-04 17:10:25 +00001004/** Calculate the space to batch output shape of a tensor
1005 *
1006 * @param[in] input Input tensor info
1007 * @param[in] block_x Block shape x value
1008 * @param[in] block_y Block shape y value
1009 * @param[in] padding_left Left padding values
1010 * @param[in] padding_right Right padding values
1011 *
1012 * @return the calculated shape
1013 */
Michalis Spyrou16934a52018-08-21 18:03:58 +01001014inline 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)
1015{
1016 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrou13a51e12018-09-18 13:09:30 +01001017
1018 const DataLayout data_layout = input->data_layout();
1019 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1020 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1021 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
1022
1023 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x + padding_left.x() + padding_right.x());
1024 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y + padding_left.y() + padding_right.y());
1025 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou16934a52018-08-21 18:03:58 +01001026
1027 return output_shape;
1028}
Pablo Tello32521432018-11-15 14:43:10 +00001029
Michalis Spyroud33fe342019-01-04 17:10:25 +00001030/** Calculate the prior box output shape of a tensor
1031 *
1032 * @param[in] input Input tensor info
1033 * @param[in] info PriorBoxLayer info
1034 *
1035 * @return the calculated shape
1036 */
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +01001037inline TensorShape compute_prior_box_shape(const ITensorInfo &input, const PriorBoxLayerInfo &info)
1038{
1039 DataLayout data_layout = input.data_layout();
1040 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1041 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Pablo Tello32521432018-11-15 14:43:10 +00001042 const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +01001043
1044 TensorShape output_shape{};
1045 output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4);
1046 output_shape.set(1, 2);
1047
1048 return output_shape;
1049}
Michalis Spyrou16934a52018-08-21 18:03:58 +01001050
Michalis Spyroud33fe342019-01-04 17:10:25 +00001051/** Calculate the padded shape of a tensor
1052 *
1053 * @param[in] input_shape Input tensor shape
1054 * @param[in] padding Paddings list
1055 *
1056 * @return the calculated shape
1057 */
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001058inline TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
1059{
1060 TensorShape padded_shape = input_shape;
1061 for(size_t dim = 0; dim < padding.size(); ++dim)
1062 {
Georgios Pinitasdea2d2d2018-12-19 16:23:17 +00001063 const auto &padding_pair = padding[dim];
1064 const uint32_t shape_on_index = (padded_shape.num_dimensions() <= dim) ? 1 : input_shape[dim];
1065 padded_shape.set(dim, padding_pair.first + shape_on_index + padding_pair.second);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +01001066 }
1067 return padded_shape;
1068}
1069
Michalis Spyroud33fe342019-01-04 17:10:25 +00001070/** Calculate the tiled shape of a tensor
1071 *
1072 * @param[in] input_shape Input tensor shape
1073 * @param[in] multiples Paddings list
1074 *
1075 * @return the calculated shape
1076 */
giuros013175fcf2018-11-21 09:59:17 +00001077inline TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples)
1078{
1079 TensorShape tiled_shape = input_shape;
1080 for(size_t dim = 0; dim < multiples.size(); ++dim)
1081 {
1082 tiled_shape.set(dim, input_shape[dim] * multiples[dim]);
1083 }
1084 return tiled_shape;
1085}
1086
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001087/** Calculate the reduced shape of a tensor given an axis
1088 *
1089 * @param[in] input Input tensor info
1090 * @param[in] axis Axis on which to perform reduction
1091 *
1092 * @return the calculated shape
1093 */
1094inline TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis)
1095{
1096 TensorShape output_shape{ input };
1097 output_shape.set(axis, 1);
1098
1099 return output_shape;
1100}
1101
Michalis Spyroud33fe342019-01-04 17:10:25 +00001102/** Calculate the upsampled shape of a tensor
1103 *
1104 * @param[in] input Input tensor info
1105 * @param[in] info Contains stride information (x and y)
1106 *
1107 * @return the calculated shape
1108 */
Michalis Spyrouceb889e2018-09-17 18:24:41 +01001109inline TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info)
1110{
1111 const DataLayout data_layout = input.data_layout();
1112 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
1113 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
1114
1115 TensorShape scale_out_shape(input.tensor_shape());
1116 const unsigned int out_x = input.dimension(idx_width) * info.x();
1117 const unsigned int out_y = input.dimension(idx_height) * info.y();
1118 scale_out_shape.set(idx_width, out_x);
1119 scale_out_shape.set(idx_height, out_y);
1120
1121 return scale_out_shape;
1122}
1123
Michalis Spyroud33fe342019-01-04 17:10:25 +00001124/** Get the tensor shape
1125 *
1126 * @param[in] data Input data
1127 *
1128 * @return the extracted tensor shape
1129 */
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001130template <typename T>
Georgios Pinitase2220552018-07-20 13:23:44 +01001131inline TensorShape extract_shape(T *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001132{
Georgios Pinitase2220552018-07-20 13:23:44 +01001133 return data->info()->tensor_shape();
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001134}
1135
John Kesapidescafec8f2019-02-19 15:53:59 +00001136inline TensorShape extract_shape(ITensorInfo *data)
John Kesapides917959c2019-02-04 12:37:29 +00001137{
1138 return data->tensor_shape();
1139}
John Kesapidescafec8f2019-02-19 15:53:59 +00001140inline TensorShape extract_shape(const ITensorInfo *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001141{
Georgios Pinitase2220552018-07-20 13:23:44 +01001142 return data->tensor_shape();
1143}
1144
1145inline TensorShape extract_shape(const TensorShape *data)
1146{
1147 return *data;
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001148}
1149
Michalis Spyroua9c44722019-04-05 17:18:36 +01001150inline TensorShape extract_shape(TensorShape *data)
1151{
1152 return *data;
1153}
1154
Michalis Spyroud33fe342019-01-04 17:10:25 +00001155/** Calculate the unstack shape of a tensor
1156 *
1157 * @param[in] input_shape Input tensor shape
1158 * @param[in] axis Axis on which to perform the unstack operation
1159 *
1160 * @return the calculated shape
1161 */
Pablo Tello54303692018-11-22 16:14:36 +00001162inline TensorShape calculate_unstack_shape(TensorShape input_shape, unsigned int axis)
1163{
1164 ARM_COMPUTE_ERROR_ON(axis > input_shape.num_dimensions());
1165 input_shape.remove_dimension(axis);
1166 return input_shape;
1167}
1168
Pablo Tello3dd5b682019-03-04 14:14:02 +00001169/** Calculate the concatenate output shape of the concatenate operation along a single axis
Michalis Spyroud33fe342019-01-04 17:10:25 +00001170 *
Pablo Tello3dd5b682019-03-04 14:14:02 +00001171 * @param[in] input Vector containing the shapes of the inputs
1172 * @param[in] axis Axis along which to concatenate the input tensors
Michalis Spyroud33fe342019-01-04 17:10:25 +00001173 *
1174 * @return the calculated shape
1175 */
Georgios Pinitase29acf12018-07-16 14:40:09 +01001176template <typename T>
Pablo Tello3dd5b682019-03-04 14:14:02 +00001177inline TensorShape calculate_concatenate_shape(const std::vector<T *> &input, size_t axis)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001178{
Pablo Tello3dd5b682019-03-04 14:14:02 +00001179 TensorShape out_shape = extract_shape(input[0]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001180
Georgios Pinitasdcd949d2019-04-17 11:04:28 +01001181#if defined(ARM_COMPUTE_ASSERTS_ENABLED)
Michalis Spyroua9c44722019-04-05 17:18:36 +01001182 // All dimensions must match except the axis one
1183 for(unsigned int i = 0; i < MAX_DIMS; ++i)
1184 {
1185 if(i == axis)
1186 {
1187 continue;
1188 }
1189
1190 for(const auto &tensor : input)
1191 {
1192 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
1193 const TensorShape shape = extract_shape(tensor);
1194 ARM_COMPUTE_ERROR_ON(out_shape[i] != shape[i]);
1195 }
1196 }
Georgios Pinitasdcd949d2019-04-17 11:04:28 +01001197#endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
Michalis Spyroua9c44722019-04-05 17:18:36 +01001198
1199 // Calculate output shape
Pablo Tello3dd5b682019-03-04 14:14:02 +00001200 size_t new_size = 0;
1201 for(const auto &tensor : input)
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001202 {
Georgios Pinitase2220552018-07-20 13:23:44 +01001203 const TensorShape shape = extract_shape(tensor);
Pablo Tello3dd5b682019-03-04 14:14:02 +00001204 new_size += shape[axis];
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001205 }
1206
Pablo Tello3dd5b682019-03-04 14:14:02 +00001207 out_shape.set(axis, new_size);
Michalis Spyrou55b3d122018-05-09 09:59:23 +01001208
1209 return out_shape;
1210}
Michalis Spyroud33fe342019-01-04 17:10:25 +00001211/** Calculate the stack output shape of a tensor
1212 *
1213 * @param[in] a Input tensor info
1214 * @param[in] axis Axis on which to perform the stack operation
1215 * @param[in] num_tensors Number of tensors to stack
1216 *
1217 * @return the calculated shape
1218 */
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +00001219inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
1220{
1221 ARM_COMPUTE_ERROR_ON(axis > a.num_dimensions());
1222 ARM_COMPUTE_ERROR_ON(a.num_dimensions() > 4);
1223
1224 TensorShape shape_out{ a.tensor_shape() };
1225 shape_out.set(axis, num_tensors);
1226
1227 unsigned int i_shift = 0;
1228
1229 for(unsigned int i = 0; i < a.num_dimensions(); ++i)
1230 {
1231 if(i == axis)
1232 {
1233 i_shift++;
1234 }
1235
1236 shape_out.set(i + i_shift, a.tensor_shape()[i]);
1237 }
1238 return shape_out;
1239}
Manuel Bottini8529bd62018-11-21 11:53:04 +00001240
1241inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
1242{
1243 ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1);
1244 ARM_COMPUTE_ERROR_ON(input_shape.num_dimensions() > 4);
1245 ARM_COMPUTE_ERROR_ON(actual_axis >= input_shape.num_dimensions());
1246
1247 TensorShape output_shape = input_shape;
1248 output_shape[actual_axis] = indices_shape[0];
1249
1250 return output_shape;
1251}
Georgios Pinitas358ca202017-12-07 16:47:52 +00001252} // namespace shape_calculator
1253} // namespace misc
1254} // namespace arm_compute
1255#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */