blob: adf5309ea50d0880bc1207e86e4551d2746aa224 [file] [log] [blame]
Georgios Pinitas358ca202017-12-07 16:47:52 +00001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 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{
Abe Mbise7784c832018-05-31 16:48:41 +010041inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
42{
43 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
44 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
45 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
46
47 TensorShape output_shape(input);
48 output_shape.set(idx_w, conv_w);
49 output_shape.set(idx_h, conv_h);
50 output_shape.set(idx_c, input.x() / (conv_w * conv_h));
51
52 return output_shape;
53}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010054
Pablo Tello00afd112018-01-04 10:34:24 +000055inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
56{
57 TensorShape output_shape = input.tensor_shape();
58 permute(output_shape, perm);
59 return output_shape;
60}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010061
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010062inline TensorShape compute_reorg_output_shape(const ITensorInfo &input, int32_t stride)
63{
Gian Marco Iodice477531c2018-08-21 17:53:38 +010064 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
65 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
66 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010067
Gian Marco Iodice477531c2018-08-21 17:53:38 +010068 ARM_COMPUTE_ERROR_ON(stride <= 0);
69 ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_width] % stride != 0), "The width of the input tensor must be a multiple of stride");
70 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 +010071
72 TensorShape output_shape{ input.tensor_shape() };
Gian Marco Iodice477531c2018-08-21 17:53:38 +010073
74 output_shape.set(idx_width, output_shape[idx_width] / stride);
75 output_shape.set(idx_height, output_shape[idx_height] / stride);
76 output_shape.set(idx_channel, output_shape[idx_channel] * stride * stride);
Georgios Pinitasaa6a04a2018-08-29 12:53:41 +010077
78 return output_shape;
79}
Georgios Pinitase1a352c2018-09-03 12:42:19 +010080
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010081inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +000082{
Giorgio Arena088c2b02018-08-07 16:59:05 +010083 // 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 +010084 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010085 ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010086 ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010087
Georgios Pinitas78c00902018-01-09 17:33:11 +000088 // Calculate output shape
89 TensorShape weights_reshaped{ weights.tensor_shape() };
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010090 weights_reshaped.set(3, weights_reshaped[3] / num_groups);
91
Georgios Pinitas78c00902018-01-09 17:33:11 +000092 weights_reshaped.collapse(3);
93 const size_t tmp_dim = weights_reshaped[0];
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010094 weights_reshaped.set(0, weights_reshaped[1]);
Georgios Pinitas78c00902018-01-09 17:33:11 +000095 weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010096 if(weights.num_dimensions() < 5)
97 {
98 weights_reshaped.set(2, num_groups);
99 }
Georgios Pinitas78c00902018-01-09 17:33:11 +0000100
101 return weights_reshaped;
102}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100103
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000104inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false)
105{
106 ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0);
107 ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0);
108 ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0);
109
110 // Input width/height
111 const unsigned int input_width = a.dimension(0);
112 const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1);
113
114 // Number of horizontal/vertical blocks in the input tensor
115 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(lhs_info.k0));
116 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(lhs_info.m0));
117
118 // Block size
119 const unsigned int block_size = lhs_info.m0 * lhs_info.k0;
120
121 // Output width/height
122 const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0;
123 const unsigned int output_height = std::ceil(num_vert_blocks / static_cast<float>(lhs_info.v0));
124
125 TensorShape lhs_shape{ a.tensor_shape() };
126 lhs_shape.set(0, output_width);
127 lhs_shape.set(1, output_height);
128
129 if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2))
130 {
131 // When the data format is NHWC and the shapes are Nx1x1
132 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
133 // To avoid failures by removing a dimension that doesn't exist
134 // check if the number of dimensions is greater than 2.
135 lhs_shape.remove_dimension(2);
136 }
137
138 return lhs_shape;
139}
140
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +0000141inline TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
142{
143 ARM_COMPUTE_ERROR_ON(rhs_info.n0 == 0);
144 ARM_COMPUTE_ERROR_ON(rhs_info.k0 == 0);
145 ARM_COMPUTE_ERROR_ON(rhs_info.h0 == 0);
146
147 // Input width/height
148 const unsigned int input_width = a.dimension(0);
149 const unsigned int input_height = a.dimension(1);
150
151 // Number of horizontal/vertical blocks in the input tensor
152 const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(rhs_info.n0));
153 const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(rhs_info.k0));
154
155 // Block size
156 const unsigned int block_size = rhs_info.n0 * rhs_info.k0;
157
158 // Output width/height
159 const unsigned int output_width = block_size * num_vert_blocks * rhs_info.h0;
160 const unsigned int output_height = std::ceil(num_horiz_blocks / static_cast<float>(rhs_info.h0));
161
162 TensorShape rhs_shape{ a.tensor_shape() };
163 rhs_shape.set(0, output_width);
164 rhs_shape.set(1, output_height);
165
166 return rhs_shape;
167}
168
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100169inline 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 +0000170{
Gian Marco36a0a462018-01-12 10:21:40 +0000171 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
172 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
173 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000174 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000175 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100176 if(reinterpret_input_as_3d)
177 {
178 const int M = a.dimension(1) * a.dimension(2);
179 const int height = std::ceil(M / static_cast<float>(interleave_width));
180 shape_interleaved_a.set(1, height);
Isabella Gottardi089695f2018-10-17 18:04:15 +0100181
182 // When the data format is NHWC and the shapes are Nx1x1
183 // the tensor shape num_dimensions is automatically set to 1 instead of 3.
184 // To avoid failures by removing a dimension that doesn't exist
185 // check if the number of dimensions is greater than 2.
186 if(shape_interleaved_a.num_dimensions() > 2)
187 {
188 shape_interleaved_a.remove_dimension(2);
189 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100190 }
191 else
192 {
193 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
194 }
Georgios Pinitas358ca202017-12-07 16:47:52 +0000195
196 return shape_interleaved_a;
197}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100198
Georgios Pinitas358ca202017-12-07 16:47:52 +0000199inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
200{
201 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
202 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
203 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
204 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
205
206 return shape_transposed1xW_b;
207}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100208
Gian Marco36a0a462018-01-12 10:21:40 +0000209inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000210{
Gian Marco36a0a462018-01-12 10:21:40 +0000211 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
212 // The transpose1xW output matrix will have the following shape:
213 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
214 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000215 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000216 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000217 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
218 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
219
220 return shape_transposed1xW_b;
221}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100222
Georgios Pinitas358ca202017-12-07 16:47:52 +0000223inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
224{
225 TensorShape shape_vector_sum_col{ b.tensor_shape() };
226 if(shape_vector_sum_col.num_dimensions() > 1)
227 {
228 shape_vector_sum_col.remove_dimension(1);
229 }
230
231 return shape_vector_sum_col;
232}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100233
Georgios Pinitas358ca202017-12-07 16:47:52 +0000234inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
235{
236 TensorShape shape_vector_sum_row{ a.tensor_shape() };
237 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
Georgios Pinitas932491f2018-09-21 16:33:15 +0100238 if(shape_vector_sum_row.num_dimensions() > 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000239 {
240 shape_vector_sum_row.remove_dimension(1);
241 }
242
243 return shape_vector_sum_row;
244}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100245
Giorgio Arena226e4b92018-08-23 12:00:02 +0100246inline 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 +0000247{
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100248 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arena226e4b92018-08-23 12:00:02 +0100249 ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area()));
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100250 ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
251
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100252 const DataLayout data_layout = input.data_layout();
253 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
254 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
255 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100256
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100257 TensorShape col2im_shape{ input.tensor_shape() };
258 // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape,
259 // as first three will be override by H,W,C data
260 if(batch_size_on_z && num_groups == 1)
261 {
262 col2im_shape.shift_right(1);
263 }
264 col2im_shape.set(width_idx, convolved_dims.width);
265 col2im_shape.set(height_idx, convolved_dims.height);
266 col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000267
268 return col2im_shape;
269}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100270
Georgios Pinitas358ca202017-12-07 16:47:52 +0000271inline TensorShape compute_transposed_shape(const ITensorInfo &input)
272{
273 TensorShape shape_transposed{ input.tensor_shape() };
274
275 shape_transposed.set(0, input.dimension(1));
276 shape_transposed.set(1, input.dimension(0));
277
278 return shape_transposed;
279}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100280
Giorgio Arena76572242018-04-04 17:44:26 +0100281inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier)
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000282{
283 const TensorShape input_shape{ input.tensor_shape() };
284 const TensorShape weights_shape{ weights.tensor_shape() };
285
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000286 const DataLayout data_layout = input.data_layout();
287 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
288 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena76572242018-04-04 17:44:26 +0100289 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000290
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000291 unsigned int output_width = 0;
292 unsigned int output_height = 0;
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000293 std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
294 weights_shape[width_idx], weights_shape[height_idx],
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000295 conv_info);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000296
297 TensorShape output_shape{ input_shape };
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000298 output_shape.set(width_idx, output_width);
299 output_shape.set(height_idx, output_height);
Giorgio Arena76572242018-04-04 17:44:26 +0100300 output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000301
302 return output_shape;
303}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100304
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100305inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy, unsigned int inner_border_right,
306 unsigned int inner_border_top,
307 std::pair<unsigned int, unsigned int> &out_dims, unsigned int &padx, unsigned int &pady)
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000308{
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100309 const DataLayout data_layout = input.data_layout();
310 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
311 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
312
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100313 // Find the upsampled dimensions
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100314 unsigned int out_x = (input.dimension(idx_w) - 1) * sx + inner_border_right + 1;
315 unsigned int out_y = (input.dimension(idx_h) - 1) * sy + inner_border_top + 1;
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100316
317 // Find the padding needed for the convolution with stride 1 in order to match output shape
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100318 padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1);
319 pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1);
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100320 out_x += padx;
321 out_y += pady;
322
323 TensorShape scale_out_shape(input.tensor_shape());
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100324 scale_out_shape.set(idx_w, out_x);
325 scale_out_shape.set(idx_h, out_y);
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000326
327 return scale_out_shape;
328}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100329
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100330inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
331{
332 const TensorShape input_shape{ input.tensor_shape() };
333 const TensorShape weights_shape{ weights.tensor_shape() };
334
335 const DataLayout data_layout = input.data_layout();
336 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
337 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
338 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
339 const int batch_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
340
341 TensorShape out_shape{ input_shape };
342 out_shape.set(width_idx, out_dims.first);
343 out_shape.set(height_idx, out_dims.second);
344 out_shape.set(channel_idx, weights_shape[batch_idx]);
345 return out_shape;
346}
347
Giorgio Arena0f170392018-07-18 16:13:12 +0100348inline 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,
349 unsigned int num_groups = 1)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000350{
Giorgio Arena0f170392018-07-18 16:13:12 +0100351 // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
352 // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
353
354 ARM_COMPUTE_ERROR_ON(num_groups == 0);
355 ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
356 ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000357
358 TensorShape output_shape{ input->tensor_shape() };
359
360 const DataLayout data_layout = input->data_layout();
361 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
362 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
363 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
364
365 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 +0100366 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 +0100367 output_shape.set(1, (out_dims.first * out_dims.second));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100368 if(batch_size_on_z && output_shape.num_dimensions() >= 3)
369 {
370 output_shape.remove_dimension(2);
371 }
372 else
373 {
Giorgio Arena0f170392018-07-18 16:13:12 +0100374 output_shape.set(2, num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100375 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000376
377 return output_shape;
378}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100379
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100380inline TensorShape compute_flatten_shape(const ITensorInfo *input)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000381{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100382 // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
383
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000384 TensorShape output_shape{ input->tensor_shape() };
385
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100386 output_shape.collapse(3);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000387
388 return output_shape;
389}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100390
giuros01efbf6c82018-09-03 09:53:53 +0100391inline TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis = 1)
392{
393 // The output shape will be a 2D version of the input. For instance:
394 // - [x,y,z] and axis 1 will return [x, y*z]
395 // - [x,y,z,w] and axis 2 will return [x*y, w*z]
396 // - [x,y,z,w] and axis 3 will return [x*y*z, w]
397 TensorShape shape2D = input->tensor_shape();
398
399 if(axis < input->num_dimensions())
400 {
401 // Collapse from axis onward (this changes the shape)
402 shape2D.collapse_from(axis);
403
404 // Collapse the rest (collapse is inclusive)
405 shape2D.collapse(shape2D.num_dimensions() - 1);
406 }
407 else
408 {
409 // Collapse everything
410 shape2D.collapse(shape2D.num_dimensions());
411 }
412
413 if(axis == 0)
414 {
415 // If axis is zero the first dim should be one. Since
416 // collapse is an inclusive operation we need to shift
417 shape2D.shift_right(1);
418 }
419
420 return shape2D;
421}
422
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000423inline TensorShape compute_interleave_custom_shape(const TensorShape &input, const int x_interleave, const int y_interleave)
424{
425 TensorShape output_shape{ input };
426
427 output_shape.set(0, output_shape.x() * x_interleave);
428 output_shape.set(1, std::ceil(output_shape.y() / static_cast<float>(y_interleave)));
429
430 return output_shape;
431}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100432
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000433inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave)
434{
435 TensorShape output_shape{ input->tensor_shape() };
436
437 // Transpose weights if the user hasn't done it
438 if(transpose_weights)
439 {
440 output_shape = compute_transposed_shape(*input);
441 }
442
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000443 // If we run multiple batches we need 1xW transpose, too.
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000444 if(is_batched_fc_layer)
445 {
446 output_shape = compute_transposed_shape(input->clone()->set_tensor_shape(output_shape));
447 output_shape = compute_interleave_custom_shape(output_shape, interleave, interleave);
448 }
449
450 return output_shape;
451}
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000452
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000453inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000454{
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000455 TensorShape tensor_shape{ input.tensor_shape() };
456
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000457 const Size2D kernel_size = winograd_info.kernel_size;
458 const Size2D output_tile_size = winograd_info.output_tile_size;
459 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 +0000460
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000461 tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
462 tensor_shape.set(Window::DimX, input.dimension(3));
463 tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
464 tensor_shape.set(Window::DimZ, input_tile_size.area());
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000465
466 return tensor_shape;
467}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100468
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000469inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000470{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000471 const PadStrideInfo conv_info = winograd_info.convolution_info;
472 const Size2D kernel_size = winograd_info.kernel_size;
473 const Size2D output_tile_size = winograd_info.output_tile_size;
474 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
475
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100476 const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
477 const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
478 const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000479
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100480 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
481 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
482 kernel_size,
483 output_tile_size,
484 conv_info);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100485
486 const unsigned int width = input.tensor_shape()[idx_c];
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100487 const unsigned int height = num_tiles.area();
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000488 const unsigned int depth = input_tile_size.area();
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000489
490 TensorShape output_shape{ input.tensor_shape() };
491 output_shape.set(0, width);
492 output_shape.set(1, height);
493 output_shape.set(2, depth);
494
495 return output_shape;
496}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100497
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000498inline TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000499{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000500 const PadStrideInfo conv_info = winograd_info.convolution_info;
501 const Size2D kernel_size = winograd_info.kernel_size;
502 const Size2D input_dimensions = winograd_info.input_dimensions;
503 const DataLayout data_layout = winograd_info.output_data_layout;
504
505 // Compute output shape
506 unsigned int output_width = 0;
507 unsigned int output_height = 0;
508 std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
509 kernel_size.width, kernel_size.height, conv_info);
510
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000511 TensorShape tensor_shape{ input.tensor_shape() };
512
513 // Output dimension
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000514 const unsigned int out_w = output_width;
515 const unsigned int out_h = output_height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000516 const unsigned int out_c = input.dimension(0);
517
518 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w);
519 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h);
520 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c);
521
522 return tensor_shape;
523}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100524
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000525inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
526{
527 const TensorShape input_shape{ input.tensor_shape() };
528 const TensorShape weights_shape{ weights.tensor_shape() };
529
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000530 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
531 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
532 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
533
Giorgio Arenac0f54432018-03-16 14:02:34 +0000534 const unsigned int input_width = input_shape[idx_width];
535 const unsigned int input_height = input_shape[idx_height];
536 const unsigned int weights_width = weights_shape[idx_width];
537 const unsigned int weights_height = weights_shape[idx_height];
538 const unsigned int weights_out_channel = weights_shape[3];
539 unsigned int output_width = 0;
540 unsigned int output_height = 0;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000541 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 +0000542
543 TensorShape output_shape{ input_shape };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000544 output_shape.set(idx_width, output_width);
545 output_shape.set(idx_height, output_height);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000546 output_shape.set(idx_channel, weights_out_channel);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000547
548 return output_shape;
549}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100550
Alex Gilday60954c62018-03-05 16:22:48 +0000551inline TensorShape compute_min_max_shape(const ITensorInfo *input)
552{
553 TensorShape output_shape{ input->tensor_shape() };
554 output_shape.set(Window::DimX, 2);
555 output_shape.remove_dimension(1);
556 output_shape.remove_dimension(1);
557
558 return output_shape;
559}
560
Michalis Spyroue74b2012018-04-18 09:49:16 +0100561inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
562{
563 unsigned int pooled_w = 0;
564 unsigned int pooled_h = 0;
565
Giorgio Arena3c520c52018-05-01 11:47:24 +0100566 TensorShape output_shape{ input.tensor_shape() };
Michalis Spyroue74b2012018-04-18 09:49:16 +0100567
Giorgio Arena3c520c52018-05-01 11:47:24 +0100568 const bool is_global_pooling = pool_info.is_global_pooling();
569 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
570 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
571 const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width;
572 const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height;
573
574 std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
575 output_shape[idx_height],
Michalis Spyroue74b2012018-04-18 09:49:16 +0100576 pool_size_x,
577 pool_size_y,
578 pool_info.pad_stride_info());
579
Giorgio Arena3c520c52018-05-01 11:47:24 +0100580 output_shape.set(idx_width, pooled_w);
581 output_shape.set(idx_height, pooled_h);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100582
583 return output_shape;
584}
585
Michalis Spyrou36a559e2018-03-20 10:30:58 +0000586inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
587{
588 TensorShape output_shape{ input->tensor_shape() };
589 output_shape.set(1, batch_size);
590
591 return output_shape;
592}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100593
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100594inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
595{
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000596 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 +0100597 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 +0100598
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100599 const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000600 const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0;
601 const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100602 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 +0000603
604 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
605 // dimension of the output tensor
606 const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000607 const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d;
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100608 const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
609 const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000610
611 TensorShape output_shape{ input0.tensor_shape() };
612
613 output_shape.set(0, dim0);
614 output_shape.set(1, dim1);
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000615 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100616 output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
617 output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000618
619 return output_shape;
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100620}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100621
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +0000622inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMReshapeInfo &gemm_info)
623{
624 ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
625
626 const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d() != 0;
627 const int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d() : 1;
628
629 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
630 // dimension of the output tensor
631 const int dim0 = gemm_info.n();
632 const int dim1 = gemm_info.m() / depth_output_gemm3d;
633 const int dim2 = input0.tensor_shape()[2];
634 const int dim3 = input0.tensor_shape()[3];
635
636 TensorShape output_shape{ input0.tensor_shape() };
637
638 output_shape.set(0, dim0);
639 output_shape.set(1, dim1);
640 output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
641 output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
642 output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
643
644 return output_shape;
645}
646
Georgios Pinitas932491f2018-09-21 16:33:15 +0100647inline 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 +0100648{
649 ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1);
650
651 TensorShape output_shape = input.tensor_shape();
652 if(gemm_3d_depth > 1)
653 {
Georgios Pinitas932491f2018-09-21 16:33:15 +0100654 if(batch_size_on_z)
655 {
656 output_shape.shift_right(1);
657 }
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100658 output_shape.set(0, input.tensor_shape().x());
659 output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth);
660 output_shape.set(2, gemm_3d_depth);
661 }
662
663 return output_shape;
664}
665
Georgios Pinitas77589b52018-08-21 14:41:35 +0100666inline TensorShape compute_strided_slice_shape(const ITensorInfo &input,
667 const Coordinates &starts, const Coordinates &ends, const Coordinates &strides,
668 int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
669{
670 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000671 return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
672}
Georgios Pinitas77589b52018-08-21 14:41:35 +0100673
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000674inline TensorShape compute_slice_shape(const TensorShape &input_shape, const Coordinates &starts, const Coordinates &ends)
675{
676 using namespace arm_compute::helpers::tensor_transform;
Georgios Pinitas77589b52018-08-21 14:41:35 +0100677
Georgios Pinitasb4af2c62018-12-10 18:45:35 +0000678 return compute_strided_slice_output_shape(input_shape,
679 starts, ends, BiStrides(),
680 0, construct_slice_end_mask(ends), 0);
Georgios Pinitas77589b52018-08-21 14:41:35 +0100681}
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100682
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100683inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
684{
685 ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0);
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100686
687 const DataLayout data_layout = input->data_layout();
688 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
689 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100690 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100691
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100692 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrouf1addb62018-09-11 11:16:47 +0100693 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x);
694 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y);
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100695 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou6a8d3b62018-08-31 10:07:09 +0100696
697 return output_shape;
698}
Georgios Pinitas77589b52018-08-21 14:41:35 +0100699
Georgios Pinitase1a352c2018-09-03 12:42:19 +0100700inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
701{
702 TensorShape empty_shape;
703 empty_shape.set(0, 0);
704
705 TensorShape out_shape{ input->tensor_shape() };
706
707 // Return empty shape if axis is invalid
708 if(axis > input->tensor_shape().num_dimensions())
709 {
710 return empty_shape;
711 }
712
713 size_t axis_size = out_shape[axis];
714
715 // Return empty shape if num_split is not valid
716 if(axis_size % num_splits)
717 {
718 return empty_shape;
719 }
720
721 out_shape[axis] = axis_size / num_splits;
722 return out_shape;
723}
724
Michalis Spyrou16934a52018-08-21 18:03:58 +0100725inline 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)
726{
727 TensorShape output_shape{ input->tensor_shape() };
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100728
729 const DataLayout data_layout = input->data_layout();
730 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
731 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
732 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
733
734 output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x + padding_left.x() + padding_right.x());
735 output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y + padding_left.y() + padding_right.y());
736 output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
Michalis Spyrou16934a52018-08-21 18:03:58 +0100737
738 return output_shape;
739}
Pablo Tello32521432018-11-15 14:43:10 +0000740
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100741inline TensorShape compute_prior_box_shape(const ITensorInfo &input, const PriorBoxLayerInfo &info)
742{
743 DataLayout data_layout = input.data_layout();
744 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
745 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Pablo Tello32521432018-11-15 14:43:10 +0000746 const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100747
748 TensorShape output_shape{};
749 output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4);
750 output_shape.set(1, 2);
751
752 return output_shape;
753}
Michalis Spyrou16934a52018-08-21 18:03:58 +0100754
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100755inline TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
756{
757 TensorShape padded_shape = input_shape;
758 for(size_t dim = 0; dim < padding.size(); ++dim)
759 {
760 padded_shape.set(dim, padding[dim].first + input_shape[dim] + padding[dim].second);
761 }
762 return padded_shape;
763}
764
giuros013175fcf2018-11-21 09:59:17 +0000765inline TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples)
766{
767 TensorShape tiled_shape = input_shape;
768 for(size_t dim = 0; dim < multiples.size(); ++dim)
769 {
770 tiled_shape.set(dim, input_shape[dim] * multiples[dim]);
771 }
772 return tiled_shape;
773}
774
Michalis Spyrouceb889e2018-09-17 18:24:41 +0100775inline TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info)
776{
777 const DataLayout data_layout = input.data_layout();
778 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
779 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
780
781 TensorShape scale_out_shape(input.tensor_shape());
782 const unsigned int out_x = input.dimension(idx_width) * info.x();
783 const unsigned int out_y = input.dimension(idx_height) * info.y();
784 scale_out_shape.set(idx_width, out_x);
785 scale_out_shape.set(idx_height, out_y);
786
787 return scale_out_shape;
788}
789
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100790template <typename T>
Georgios Pinitase2220552018-07-20 13:23:44 +0100791inline TensorShape extract_shape(T *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100792{
Georgios Pinitase2220552018-07-20 13:23:44 +0100793 return data->info()->tensor_shape();
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100794}
795
Georgios Pinitase2220552018-07-20 13:23:44 +0100796inline TensorShape extract_shape(ITensorInfo *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100797{
Georgios Pinitase2220552018-07-20 13:23:44 +0100798 return data->tensor_shape();
799}
800
801inline TensorShape extract_shape(const TensorShape *data)
802{
803 return *data;
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100804}
805
806template <typename T>
Georgios Pinitase29acf12018-07-16 14:40:09 +0100807inline TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector)
808{
Georgios Pinitase2220552018-07-20 13:23:44 +0100809 TensorShape out_shape = extract_shape(inputs_vector[0]);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100810
811 size_t max_x = 0;
812 size_t max_y = 0;
813 size_t depth = 0;
814
815 for(const auto &tensor : inputs_vector)
816 {
817 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Georgios Pinitase2220552018-07-20 13:23:44 +0100818 const TensorShape shape = extract_shape(tensor);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100819 max_x = std::max(shape.x(), max_x);
820 max_y = std::max(shape.y(), max_y);
821 depth += shape.z();
822 }
823
824 out_shape.set(0, max_x);
825 out_shape.set(1, max_y);
826 out_shape.set(2, depth);
827
828 return out_shape;
829}
830
831template <typename T>
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100832inline TensorShape calculate_width_concatenate_shape(const std::vector<T *> &inputs_vector)
833{
Georgios Pinitase2220552018-07-20 13:23:44 +0100834 TensorShape out_shape = extract_shape(inputs_vector[0]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100835
836 size_t width = 0;
837 for(const auto &tensor : inputs_vector)
838 {
839 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Georgios Pinitase2220552018-07-20 13:23:44 +0100840 const TensorShape shape = extract_shape(tensor);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100841 width += shape.x();
842 }
843
844 out_shape.set(0, width);
845
846 return out_shape;
847}
Gian Marco Iodice8aa985e2018-11-27 15:58:08 +0000848
849inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
850{
851 ARM_COMPUTE_ERROR_ON(axis > a.num_dimensions());
852 ARM_COMPUTE_ERROR_ON(a.num_dimensions() > 4);
853
854 TensorShape shape_out{ a.tensor_shape() };
855 shape_out.set(axis, num_tensors);
856
857 unsigned int i_shift = 0;
858
859 for(unsigned int i = 0; i < a.num_dimensions(); ++i)
860 {
861 if(i == axis)
862 {
863 i_shift++;
864 }
865
866 shape_out.set(i + i_shift, a.tensor_shape()[i]);
867 }
868 return shape_out;
869}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000870} // namespace shape_calculator
871} // namespace misc
872} // namespace arm_compute
873#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */