Compute Library
 21.05
ShapeCalculator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H
25 #define ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H
26 
30 #include "arm_compute/core/Utils.h"
31 
33 
34 #include <cmath>
35 
36 namespace arm_compute
37 {
38 namespace misc
39 {
40 namespace shape_calculator
41 {
42 /** Calculate the output tensor shape for the reduce mean operation
43  *
44  * @param[in] input Input tensor shape
45  * @param[in] reduction_axis Reduction axis
46  * @param[in] keep_dims Flag to indicate if dimensions are kept
47  *
48  * @return the calculated shape
49  */
50 inline TensorShape calculate_reduce_mean_shape(ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims)
51 {
52  const int reduction_ops = reduction_axis.num_dimensions();
53  Coordinates axis_local = reduction_axis;
54  const int input_dims = input->num_dimensions();
55  convert_negative_axis(axis_local, input_dims);
56  TensorShape out_shape = input->tensor_shape();
57  // Configure reshape layer if we want to drop the dimensions
58  if(!keep_dims)
59  {
60  // We have to sort the reduction axis vectors in order for remove_dimension
61  // to work properly
62  std::sort(axis_local.begin(), axis_local.begin() + reduction_ops);
63  for(int i = 0; i < reduction_ops; ++i)
64  {
65  out_shape.remove_dimension(axis_local[i] - i);
66  }
67  return out_shape;
68  }
69  else
70  {
71  for(int i = 0; i < reduction_ops; ++i)
72  {
73  out_shape.set(axis_local[i], 1);
74  }
75  return out_shape;
76  }
77 }
78 /** Calculate the output tensor shape of a vector input given the convolution dimensions
79  *
80  * @param[in] input Input tensor shape
81  * @param[in] conv_w Convolution width
82  * @param[in] conv_h Convolution height
83  * @param[in] data_layout Data layout
84  *
85  * @return the calculated shape
86  */
87 inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
88 {
92 
94  output_shape.set(idx_w, conv_w);
95  output_shape.set(idx_h, conv_h);
96  output_shape.set(idx_c, input.x() / (conv_w * conv_h));
97 
98  return output_shape;
99 }
100 
101 /** Calculate the permuted shape of an input given a permutation vector
102  *
103  * @param[in] input Input tensor info
104  * @param[in] perm Permutation vector
105  *
106  * @return the calculated shape
107  */
109 {
110  TensorShape output_shape = input.tensor_shape();
111  permute(output_shape, perm);
112  return output_shape;
113 }
114 
115 /** Calculate the output shape of the reorg layer given a stride
116  *
117  * @param[in] input Input tensor info
118  * @param[in] stride Stride
119  *
120  * @return the calculated shape
121  */
123 {
126  const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
127 
128  ARM_COMPUTE_ERROR_ON(stride <= 0);
129  ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_width] % stride != 0), "The width of the input tensor must be a multiple of stride");
130  ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_height] % stride != 0), "The height of the input tensor must be a multiple of stride");
131 
132  TensorShape output_shape{ input.tensor_shape() };
133 
136  output_shape.set(idx_channel, output_shape[idx_channel] * stride * stride);
137 
138  return output_shape;
139 }
140 
141 /** Calculate the reshaped shape of the weights
142  *
143  * @param[in] weights Weights tensor info
144  * @param[in] has_bias (Optional) Set to true if there is bias
145  * @param[in] num_groups (Optional) Number of groups
146  *
147  * @return the calculated shape of the reshaped weights
148  */
149 inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1)
150 {
151  // Number of groups greater than one are only supported for NCHW data layout, and the number of weights must be a multiple of it.
154  ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0);
155 
156  // Calculate output shape
157  TensorShape weights_reshaped{ weights.tensor_shape() };
158  weights_reshaped.set(3, weights_reshaped[3] / num_groups);
159 
160  weights_reshaped.collapse(3);
161  const size_t tmp_dim = weights_reshaped[0];
162  weights_reshaped.set(0, weights_reshaped[1]);
163  weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
164  if(weights.num_dimensions() < 5)
165  {
166  weights_reshaped.set(2, num_groups);
167  }
168 
169  return weights_reshaped;
170 }
171 
172 /** Calculate the Left Hand Side matrix reshaped shape
173  *
174  * @param[in] a Input tensor info
175  * @param[in] lhs_info Left Hand Side matrix information
176  * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
177  *
178  * @return the calculated shape
179  */
180 inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false)
181 {
182  ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0);
183  ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0);
184  ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0);
185 
186  // Input width/height
187  const unsigned int input_width = a.dimension(0);
188  const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1);
189 
190  // Number of horizontal/vertical blocks in the input tensor
191  const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(lhs_info.k0));
192  const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(lhs_info.m0));
193 
194  // Block size
195  const unsigned int block_size = lhs_info.m0 * lhs_info.k0;
196 
197  // Output width/height
198  const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0;
199  const unsigned int output_height = std::ceil(num_vert_blocks / static_cast<float>(lhs_info.v0));
200 
201  TensorShape lhs_shape{ a.tensor_shape() };
202  lhs_shape.set(0, output_width);
203  lhs_shape.set(1, output_height);
204 
205  if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2))
206  {
207  // When the data format is NHWC and the shapes are Nx1x1
208  // the tensor shape num_dimensions is automatically set to 1 instead of 3.
209  // To avoid failures by removing a dimension that doesn't exist
210  // check if the number of dimensions is greater than 2.
211  lhs_shape.remove_dimension(2);
212  }
213 
214  return lhs_shape;
215 }
216 
217 /** Calculate the Right Hand Side matrix reshaped shape
218  *
219  * @param[in] a Input tensor info
220  * @param[in] rhs_info Right Hand Side matrix information
221  *
222  * @return the calculated shape
223  */
225 {
226  ARM_COMPUTE_ERROR_ON(rhs_info.n0 == 0);
227  ARM_COMPUTE_ERROR_ON(rhs_info.k0 == 0);
228  ARM_COMPUTE_ERROR_ON(rhs_info.h0 == 0);
229 
230  // Input width/height
231  const unsigned int input_width = a.dimension(0);
232  const unsigned int input_height = a.dimension(1);
233 
234  // Number of horizontal/vertical blocks in the input tensor
235  const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(rhs_info.n0));
236  const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(rhs_info.k0));
237 
238  // Block size
239  const unsigned int block_size = rhs_info.n0 * rhs_info.k0;
240 
241  // Output width/height
242  const unsigned int output_width = block_size * num_vert_blocks * rhs_info.h0;
243  const unsigned int output_height = std::ceil(num_horiz_blocks / static_cast<float>(rhs_info.h0));
244 
245  TensorShape rhs_shape{ a.tensor_shape() };
246  rhs_shape.set(0, output_width);
247  rhs_shape.set(1, output_height);
248 
249  return rhs_shape;
250 }
251 
252 /** Calculate the interleaved shape of an input tensor
253  *
254  * @param[in] a Input tensor info
255  * @param[in] mult_interleave4x4_height (Optional) Interleave4x4 height
256  * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d
257  *
258  * @return the calculated shape
259  */
260 inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1, bool reinterpret_input_as_3d = false)
261 {
262  // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
263  ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
264  const int interleave_width = 4 * mult_interleave4x4_height;
265  TensorShape shape_interleaved_a{ a.tensor_shape() };
266  shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
267  if(reinterpret_input_as_3d)
268  {
269  const int M = a.dimension(1) * a.dimension(2);
270  const int height = std::ceil(M / static_cast<float>(interleave_width));
271  shape_interleaved_a.set(1, height);
272 
273  // When the data format is NHWC and the shapes are Nx1x1
274  // the tensor shape num_dimensions is automatically set to 1 instead of 3.
275  // To avoid failures by removing a dimension that doesn't exist
276  // check if the number of dimensions is greater than 2.
277  if(shape_interleaved_a.num_dimensions() > 2)
278  {
279  shape_interleaved_a.remove_dimension(2);
280  }
281  }
282  else
283  {
284  shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
285  }
286 
287  return shape_interleaved_a;
288 }
289 
290 /** Calculate the transposed 1xW shape
291  *
292  * @param[in] b Input tensor info
293  *
294  * @return the calculated shape
295  */
297 {
298  // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
299  TensorShape shape_transposed1xW_b{ b.tensor_shape() };
300  shape_transposed1xW_b.set(0, b.dimension(1) * 16);
301  shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
302 
303  return shape_transposed1xW_b;
304 }
305 
306 /** Calculate the transposed 1xW width element shape
307  *
308  * @param[in] b Input tensor info
309  * @param[in] mult_transpose1xW_width (Optional) Transpose1xW width
310  *
311  * @return the calculated shape
312  */
313 inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
314 {
315  // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
316  // The transpose1xW output matrix will have the following shape:
317  // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
318  ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
319  TensorShape shape_transposed1xW_b{ b.tensor_shape() };
320  const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
321  shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
322  shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
323 
324  return shape_transposed1xW_b;
325 }
326 
327 /** Calculate the reductionA shape used in GEMMLowp
328  *
329  * @param[in] b Input tensor info
330  *
331  * @return the calculated shape
332  */
334 {
335  TensorShape shape_vector_sum_col{ b.tensor_shape() };
336  if(shape_vector_sum_col.num_dimensions() > 1)
337  {
338  shape_vector_sum_col.remove_dimension(1);
339  }
340 
341  return shape_vector_sum_col;
342 }
343 
344 /** Calculate the reductionB shape used in GEMMLowp
345  *
346  * @param[in] a Input tensor info
347  *
348  * @return the calculated shape
349  */
351 {
352  TensorShape shape_vector_sum_row{ a.tensor_shape() };
353  shape_vector_sum_row.set(Window::DimX, a.dimension(1));
354  if(shape_vector_sum_row.num_dimensions() > 1)
355  {
356  shape_vector_sum_row.remove_dimension(1);
357  }
358 
359  return shape_vector_sum_row;
360 }
361 
362 /** Calculate the Col2Im shape
363  *
364  * @param[in] input Input tensor info
365  * @param[in] convolved_dims Convolved dimensions
366  * @param[in] batch_size_on_z True if batch size is on z axis
367  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
368  *
369  * @return the calculated shape
370  */
371 inline TensorShape compute_col2im_shape(const ITensorInfo &input, const Size2D &convolved_dims, bool batch_size_on_z, unsigned int num_groups = 1)
372 {
374  ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area()));
375  ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
376 
377  const DataLayout data_layout = input.data_layout();
381 
382  TensorShape col2im_shape{ input.tensor_shape() };
383  // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape,
384  // as first three will be override by H,W,C data
385  if(batch_size_on_z && num_groups == 1)
386  {
387  col2im_shape.shift_right(1);
388  }
389  col2im_shape.set(width_idx, convolved_dims.width);
390  col2im_shape.set(height_idx, convolved_dims.height);
391  col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups);
392 
393  return col2im_shape;
394 }
395 
396 /** Calculate the transposed shape of a tensor
397  *
398  * @param[in] input Input tensor info
399  *
400  * @return the calculated shape
401  */
403 {
404  TensorShape shape_transposed{ input.tensor_shape() };
405 
406  shape_transposed.set(0, input.dimension(1));
407  shape_transposed.set(1, input.dimension(0));
408 
409  return shape_transposed;
410 }
411 
412 /** Calculate the depthwise convolution output shape of a tensor
413  *
414  * @param[in] input Input tensor info
415  * @param[in] weights Weights tensor info
416  * @param[in] info Convolution info
417  *
418  * @return the calculated shape
419  */
421 {
422  const TensorShape input_shape{ input.tensor_shape() };
423  const TensorShape weights_shape{ weights.tensor_shape() };
424 
425  const DataLayout data_layout = input.data_layout();
429 
430  const DataLayout weights_data_layout = weights.data_layout();
431  const int weights_width_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::WIDTH);
432  const int weights_height_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::HEIGHT);
433 
434  unsigned int output_width = 0;
435  unsigned int output_height = 0;
436  std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
437  weights_shape[weights_width_idx], weights_shape[weights_height_idx],
438  info.pad_stride_info, info.dilation);
439 
441  output_shape.set(width_idx, output_width);
442  output_shape.set(height_idx, output_height);
443  output_shape.set(channel_idx, input_shape[channel_idx] * info.depth_multiplier);
444 
445  return output_shape;
446 }
447 
448 /** Calculate the upsampled output shape used for deconvolution
449  *
450  * @param[in] input Input tensor info
451  * @param[in] weights Weights tensor shape
452  * @param[in] sx Stride on x axis
453  * @param[in] sy Stride on y axis
454  * @param[in] out_dims Output shape dimensions
455  * @param[in] padx Padding on x axis
456  * @param[in] pady Padding on y axis
457  *
458  * @return the calculated shape
459  */
460 inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy,
461  std::pair<unsigned int, unsigned int> &out_dims, uint32_t &padx, uint32_t &pady)
462 {
463  const DataLayout data_layout = input.data_layout();
466 
467  // Find the upsampled dimensions
468  unsigned int out_x = (input.dimension(idx_w) - 1) * sx + 1;
469  unsigned int out_y = (input.dimension(idx_h) - 1) * sy + 1;
470 
471  // Find the padding needed for the convolution with stride 1 in order to match output shape
472  padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1);
473  pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1);
474  out_x += padx;
475  out_y += pady;
476 
477  TensorShape scale_out_shape(input.tensor_shape());
478  scale_out_shape.set(idx_w, out_x);
479  scale_out_shape.set(idx_h, out_y);
480 
481  return scale_out_shape;
482 }
483 
484 /** Calculate the output shape of the deconvolution layer
485  *
486  * @param[in] out_dims Output x and y shape dimensions
487  * @param[in] input Input tensor info
488  * @param[in] weights Weights tensor shape
489  *
490  * @return the calculated shape
491  */
492 inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
493 {
494  const TensorShape input_shape{ input.tensor_shape() };
495  const TensorShape weights_shape{ weights.tensor_shape() };
496 
497  const DataLayout data_layout = input.data_layout();
502 
503  TensorShape out_shape{ input_shape };
504  out_shape.set(width_idx, out_dims.first);
505  out_shape.set(height_idx, out_dims.second);
506  out_shape.set(channel_idx, weights_shape[batch_idx]);
507  return out_shape;
508 }
509 
510 /** Calculate the im2col output shape of a tensor
511  *
512  * @param[in] input Input tensor info
513  * @param[in] kernel_dims The kernel dimensions (width and height).
514  * @param[in] conv_info Contains padding and stride information
515  * @param[in] has_bias In case biases are provided expands the matrix with 1
516  * @param[in] dilation Dilation, in elements, across x and y
517  * @param[in] batch_size_on_z True if batch size is on z axis
518  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
519  *
520  * @return the calculated shape
521  */
522 inline 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,
523  unsigned int num_groups = 1)
524 {
525  // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
526  // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
527 
529  ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
530  ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
531 
532  TensorShape output_shape{ input->tensor_shape() };
533 
534  const DataLayout data_layout = input->data_layout();
538 
539  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);
540  output_shape.set(0, (output_shape[channel_idx] / num_groups * kernel_dims.area() + (has_bias ? 1 : 0))); // NOLINT
541  output_shape.set(1, (out_dims.first * out_dims.second));
542  if(batch_size_on_z && output_shape.num_dimensions() >= 3)
543  {
545  }
546  else
547  {
549  }
550 
551  return output_shape;
552 }
553 
554 /** Calculate the flattened output shape of a tensor
555  *
556  * @param[in] input Input tensor info
557  *
558  * @return the calculated shape
559  */
561 {
562  // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
563 
564  TensorShape output_shape{ input->tensor_shape() };
565 
567 
568  return output_shape;
569 }
570 
571 /** Calculate the softmax output shape of a tensor
572  *
573  * @param[in] input Input tensor info
574  * @param[in] axis (Optional) Softmax axis
575  *
576  * @return the calculated shape
577  */
578 inline TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis = 1)
579 {
580  // The output shape will be a 2D version of the input. For instance:
581  // - [x,y,z] and axis 1 will return [x, y*z]
582  // - [x,y,z,w] and axis 2 will return [x*y, w*z]
583  // - [x,y,z,w] and axis 3 will return [x*y*z, w]
584  TensorShape shape2D = input->tensor_shape();
585 
586  if(axis < input->num_dimensions())
587  {
588  // Collapse from axis onward (this changes the shape)
589  shape2D.collapse_from(axis);
590 
591  // Collapse the rest (collapse is inclusive)
592  shape2D.collapse(shape2D.num_dimensions() - 1);
593  }
594  else
595  {
596  // Collapse everything
597  shape2D.collapse(shape2D.num_dimensions());
598  }
599 
600  if(axis == 0)
601  {
602  // If axis is zero the first dim should be one. Since
603  // collapse is an inclusive operation we need to shift
604  shape2D.shift_right(1);
605  }
606 
607  return shape2D;
608 }
609 
610 /** Calculate the winograd filter transform shape
611  *
612  * @param[in] input Input tensor info
613  * @param[in] winograd_info Winograd information
614  *
615  * @return the calculated shape
616  */
618 {
619  TensorShape tensor_shape{ input.tensor_shape() };
620 
621  const Size2D kernel_size = winograd_info.kernel_size;
622  const Size2D output_tile_size = winograd_info.output_tile_size;
623  const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
624 
625  tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
626  tensor_shape.set(Window::DimX, input.dimension(3));
627  tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
628  tensor_shape.set(Window::DimZ, input_tile_size.area());
629 
630  return tensor_shape;
631 }
632 
633 /** Calculate the winograd input transform shape
634  *
635  * @param[in] input Input tensor info
636  * @param[in] winograd_info Winograd information
637  *
638  * @return the calculated shape
639  */
641 {
642  const PadStrideInfo conv_info = winograd_info.convolution_info;
643  const Size2D kernel_size = winograd_info.kernel_size;
644  const Size2D output_tile_size = winograd_info.output_tile_size;
645  const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
646 
647  const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
648  const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
649  const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
650 
651  // Compute the number of output tiles along the x and y direction of size "output_tile_size"
652  const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
653  kernel_size,
654  output_tile_size,
655  conv_info);
656 
657  const unsigned int width = input.tensor_shape()[idx_c];
658  const unsigned int height = num_tiles.area();
659  const unsigned int depth = input_tile_size.area();
660 
661  TensorShape output_shape{ input.tensor_shape() };
662  output_shape.set(0, width);
663  output_shape.set(1, height);
664  output_shape.set(2, depth);
665 
666  return output_shape;
667 }
668 
669 /** Calculate the winograd output transform shape
670  *
671  * @param[in] input Input tensor info
672  * @param[in] winograd_info Winograd information
673  *
674  * @return the calculated shape
675  */
677 {
678  const PadStrideInfo conv_info = winograd_info.convolution_info;
679  const Size2D kernel_size = winograd_info.kernel_size;
680  const Size2D input_dimensions = winograd_info.input_dimensions;
681  const DataLayout data_layout = winograd_info.output_data_layout;
682 
683  // Compute output shape
684  unsigned int output_width = 0;
685  unsigned int output_height = 0;
686  std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
687  kernel_size.width, kernel_size.height, conv_info);
688 
689  TensorShape tensor_shape{ input.tensor_shape() };
690 
691  // Output dimension
692  const unsigned int out_w = output_width;
693  const unsigned int out_h = output_height;
694  const unsigned int out_c = input.dimension(0);
695 
699 
700  return tensor_shape;
701 }
702 
703 /** Calculate the deep convolution shape output shape of a tensor
704  *
705  * @param[in] input Input tensor info
706  * @param[in] weights Weights tensor info
707  * @param[in] conv_info Contains padding and stride information
708  *
709  * @return the calculated shape
710  */
712 {
713  const TensorShape input_shape{ input.tensor_shape() };
714  const TensorShape weights_shape{ weights.tensor_shape() };
715 
718  const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
719 
720  const unsigned int input_width = input_shape[idx_width];
721  const unsigned int input_height = input_shape[idx_height];
722  const unsigned int weights_width = weights_shape[idx_width];
723  const unsigned int weights_height = weights_shape[idx_height];
724  const unsigned int weights_out_channel = weights_shape[3];
725  unsigned int output_width = 0;
726  unsigned int output_height = 0;
727  std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, weights_width, weights_height, conv_info);
728 
730  output_shape.set(idx_width, output_width);
731  output_shape.set(idx_height, output_height);
732  output_shape.set(idx_channel, weights_out_channel);
733 
734  return output_shape;
735 }
736 
737 /** Calculate the min/max shape output shape of a tensor
738  *
739  * @param[in] input Input tensor info
740  *
741  * @return the calculated shape
742  */
744 {
745  TensorShape output_shape{ input->tensor_shape() };
749 
750  return output_shape;
751 }
752 
753 /** Calculate the output pool shape of a tensor
754  *
755  * @param[in] input Input tensor info
756  * @param[in] pool_info Pooling layer info
757  *
758  * @return the calculated shape
759  */
761 {
762  unsigned int pooled_w = 0;
763  unsigned int pooled_h = 0;
764 
765  TensorShape output_shape{ input.tensor_shape() };
766 
767  const bool is_global_pooling = pool_info.is_global_pooling;
768  const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
770  const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size.width;
771  const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size.height;
772 
773  std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
775  pool_size_x,
776  pool_size_y,
777  pool_info.pad_stride_info);
778 
779  output_shape.set(idx_width, pooled_w);
780  output_shape.set(idx_height, pooled_h);
781 
782  return output_shape;
783 }
784 
785 /** Calculate the output unpool shape of a tensor
786  *
787  * @param[in] input Input tensor info
788  * @param[in] pool_info Pooling layer info
789  *
790  * @return the calculated shape
791  */
793 {
794  const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
796  const TensorShape input_shape = input.tensor_shape();
798  const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
799  const unsigned int stride_x = pad_stride_info.stride().first;
800  const unsigned int stride_y = pad_stride_info.stride().second;
801 
802  const int pad_left = pad_stride_info.pad_left();
803  const int pad_top = pad_stride_info.pad_top();
804  const int pad_right = pad_stride_info.pad_right();
805  const int pad_bottom = pad_stride_info.pad_bottom();
806 
808  const unsigned int out_width = (input_shape[idx_width] - 1) * stride_x - pad_left - pad_right + pool_info.pool_size.width;
809  const unsigned int out_height = (input_shape[idx_height] - 1) * stride_y - pad_top - pad_bottom + pool_info.pool_size.height;
810 
811  output_shape.set(idx_width, out_width);
812  output_shape.set(idx_height, out_height);
813  return output_shape;
814 }
815 
816 /** Calculate the output roi align shape of a tensor
817  *
818  * @param[in] input Input tensor info
819  * @param[in] rois Rois tensor info
820  * @param[in] pool_info Pooling layer info
821  *
822  * @return the calculated shape
823  */
825 {
826  TensorShape output_shape{ input.tensor_shape() };
827 
828  const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
830 
831  output_shape.set(idx_width, pool_info.pooled_width());
833  output_shape.set(3, rois.dimension(1));
834 
835  return output_shape;
836 }
837 
838 /** Calculate the RNN shape of a tensor
839  *
840  * @param[in] input Input tensor info
841  * @param[in] batch_size Batch size
842  *
843  * @return the calculated shape
844  */
845 inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
846 {
847  TensorShape output_shape{ input->tensor_shape() };
848  output_shape.set(1, batch_size);
849 
850  return output_shape;
851 }
852 
853 /** Calculate the matrix multiplication output shape of two tensors
854  *
855  * @param[in] input0 First input tensor info
856  * @param[in] input1 Second input tensor info
857  * @param[in] is_interleaved_transposed True if the input is interleaved transposed
858  * @param[in] reshape_info GEMM reshape info
859  *
860  * @return the calculated shape
861  */
862 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
863 {
864  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
865  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");
866 
867  const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
868  const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0;
869  const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1;
870  const int m = reshape_info.reinterpret_input_as_3d() ? input0.dimension(1) * input0.dimension(2) : input0.dimension(1);
871 
872  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
873  // dimension of the output tensor
874  const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
875  const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d;
876  const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
877  const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
878 
880 
881  output_shape.set(0, dim0);
882  output_shape.set(1, dim1);
883  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
884  output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
885  output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
886 
887  return output_shape;
888 }
889 
890 /** Calculate the matrix multiplication output shape of two tensors
891  *
892  * @param[in] input0 First input tensor info
893  * @param[in] input1 Second input tensor info
894  * @param[in] gemm_info GEMM reshape info
895  *
896  * @return the calculated shape
897  */
898 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMReshapeInfo &gemm_info)
899 {
900  ARM_COMPUTE_UNUSED(input1);
901  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
902 
903  const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
904  const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d() != 0;
905  const int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d() : 1;
906 
908 
909  if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
910  {
911  output_shape.set(0, gemm_info.n());
912  output_shape.set(1, gemm_info.m());
913  }
914  else
915  {
916  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
917  // dimension of the output tensor
918  const int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
919  output_shape.set(0, gemm_info.n());
920  output_shape.set(1, gemm_info.m() / depth_output_gemm3d);
921  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
922  output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
923  }
924 
925  return output_shape;
926 }
927 
928 /** Calculate the matrix multiplication output shape of two tensors
929  *
930  * @param[in] input0 First input tensor info
931  * @param[in] input1 Second input tensor info
932  * @param[in] gemm_info GEMM kernel info used to retrieve the original dimensions of the input matrices
933  *
934  * @return the calculated shape
935  */
936 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMKernelInfo &gemm_info)
937 {
938  ARM_COMPUTE_UNUSED(input1);
939  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
940 
941  const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
942  const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
943  const unsigned int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d : 1;
944 
946 
947  if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
948  {
949  output_shape.set(0, gemm_info.n);
950  output_shape.set(1, gemm_info.m);
951  }
952  else
953  {
954  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
955  // dimension of the output tensor
956  const unsigned int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
957  output_shape.set(0, gemm_info.n);
958  output_shape.set(1, gemm_info.m / depth_output_gemm3d);
959  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
960  output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
961  }
962 
963  return output_shape;
964 }
965 
966 /** Calculate the matrix multiplication output shape of two tensors
967  *
968  * @param[in] input Input tensor info
969  * @param[in] gemm_3d_depth (Optional) GEMM 3d depth
970  * @param[in] batch_size_on_z (Optional) True if batch size is on z axis
971  *
972  * @return the calculated shape
973  */
974 inline TensorShape compute_output_stage_shape(const ITensorInfo &input, unsigned int gemm_3d_depth = 1, bool batch_size_on_z = false)
975 {
976  ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1);
977 
978  TensorShape output_shape = input.tensor_shape();
979  if(gemm_3d_depth > 1)
980  {
981  if(batch_size_on_z)
982  {
984  }
985  output_shape.set(0, input.tensor_shape().x());
986  output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth);
987  output_shape.set(2, gemm_3d_depth);
988  }
989 
990  return output_shape;
991 }
992 
993 /** Calculate the strided slice output shape of a tensor
994  *
995  * @param[in] input Input tensor info
996  * @param[in] starts The starts of the dimensions of the input tensor to be sliced
997  * @param[in] ends The ends of the dimensions of the input tensor to be sliced
998  * @param[in] strides The strides of the dimensions of the input tensor to be sliced
999  * @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.
1000  * @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.
1001  * @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
1002  *
1003  * @return the calculated shape
1004  */
1006  const Coordinates &starts, const Coordinates &ends, const Coordinates &strides,
1007  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
1008 {
1010  return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
1011 }
1012 
1013 /** Calculate the slice output shape of a tensor
1014  *
1015  * @param[in] input_shape Input tensor info
1016  * @param[in] starts The starts of the dimensions of the input tensor to be sliced
1017  * @param[in] ends The ends of the dimensions of the input tensor to be sliced
1018  *
1019  * @return the calculated shape
1020  */
1022 {
1024 
1026  starts, ends, BiStrides(),
1027  0, construct_slice_end_mask(ends), 0);
1028 }
1029 
1030 /** Calculate the batch to space output shape of a tensor
1031  *
1032  * @param[in] input Input tensor info
1033  * @param[in] block_x Block shape x value
1034  * @param[in] block_y Block shape y value
1035  *
1036  * @return the calculated shape
1037  */
1038 inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
1039 {
1040  ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0);
1041 
1042  const DataLayout data_layout = input->data_layout();
1046 
1047  TensorShape output_shape{ input->tensor_shape() };
1048  output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x);
1049  output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y);
1050  output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
1051 
1052  return output_shape;
1053 }
1054 
1055 /** Calculate the depth to space output shape of a tensor
1056  *
1057  * @param[in] input_shape Input tensor shape
1058  * @param[in] data_layout Operation data layout
1059  * @param[in] block Block shape value
1060  *
1061  * @return the calculated shape
1062  */
1064 {
1065  ARM_COMPUTE_ERROR_ON(block < 2);
1066 
1070 
1074  output_shape.set(idx_channel, input_shape[idx_channel] / (block * block));
1075 
1076  return output_shape;
1077 }
1078 
1079 /** Calculate the split output shape of a tensor
1080  *
1081  * @param[in] input Input tensor info
1082  * @param[in] axis Axis on which to split the input
1083  * @param[in] num_splits Number of splits
1084  *
1085  * @return the calculated shape
1086  */
1087 inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
1088 {
1089  TensorShape empty_shape;
1090  empty_shape.set(0, 0);
1091 
1092  TensorShape out_shape{ input->tensor_shape() };
1093 
1094  // Return empty shape if axis is invalid
1095  if(axis > input->tensor_shape().num_dimensions())
1096  {
1097  return empty_shape;
1098  }
1099 
1100  size_t axis_size = out_shape[axis];
1101 
1102  // Return empty shape if num_split is not valid
1103  if(axis_size % num_splits)
1104  {
1105  return empty_shape;
1106  }
1107 
1108  out_shape[axis] = axis_size / num_splits;
1109  return out_shape;
1110 }
1111 
1112 /** Calculate the space to batch output shape of a tensor
1113  *
1114  * @param[in] input Input tensor info
1115  * @param[in] block_x Block shape x value
1116  * @param[in] block_y Block shape y value
1117  * @param[in] padding_left Left padding values
1118  * @param[in] padding_right Right padding values
1119  *
1120  * @return the calculated shape
1121  */
1122 inline 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)
1123 {
1124  TensorShape output_shape{ input->tensor_shape() };
1125 
1126  const DataLayout data_layout = input->data_layout();
1130 
1131  ARM_COMPUTE_ERROR_ON((input->tensor_shape()[idx_width] + padding_left.x() + padding_right.x()) % block_x != 0);
1132  ARM_COMPUTE_ERROR_ON((input->tensor_shape()[idx_height] + padding_left.y() + padding_right.y()) % block_y != 0);
1133 
1134  output_shape.set(idx_width, (input->tensor_shape()[idx_width] + padding_left.x() + padding_right.x()) / block_x);
1135  output_shape.set(idx_height, (input->tensor_shape()[idx_height] + padding_left.y() + padding_right.y()) / block_y);
1136  output_shape.set(idx_batch, input->tensor_shape()[idx_batch] * block_x * block_y);
1137 
1138  return output_shape;
1139 }
1140 
1141 /** Calculate the space to batch output shape of a tensor
1142  *
1143  * @param[in] input Input tensor info
1144  * @param[in] block_shape Block shape value
1145  *
1146  * @return the calculated shape
1147  */
1148 inline TensorShape compute_space_to_depth_shape(const ITensorInfo *input, int32_t block_shape)
1149 {
1150  TensorShape output_shape{ input->tensor_shape() };
1151 
1152  const DataLayout data_layout = input->data_layout();
1156 
1157  output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_shape);
1158  output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_shape);
1159  output_shape.set(idx_depth, input->tensor_shape()[idx_depth] / (block_shape * block_shape));
1160 
1161  return output_shape;
1162 }
1163 
1164 /** Calculate the prior box output shape of a tensor
1165  *
1166  * @param[in] input Input tensor info
1167  * @param[in] info PriorBoxLayer info
1168  *
1169  * @return the calculated shape
1170  */
1172 {
1173  DataLayout data_layout = input.data_layout();
1176  const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
1177 
1179  output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4);
1180  output_shape.set(1, 2);
1181 
1182  return output_shape;
1183 }
1184 
1185 /** Calculate the padded shape of a tensor
1186  *
1187  * @param[in] input_shape Input tensor shape
1188  * @param[in] padding Paddings list
1189  *
1190  * @return the calculated shape
1191  */
1193 {
1194  TensorShape padded_shape = input_shape;
1195  for(size_t dim = 0; dim < padding.size(); ++dim)
1196  {
1197  const auto &padding_pair = padding[dim];
1198  const uint32_t shape_on_index = (padded_shape.num_dimensions() <= dim) ? 1 : input_shape[dim];
1199  padded_shape.set(dim, padding_pair.first + shape_on_index + padding_pair.second);
1200  }
1201  return padded_shape;
1202 }
1203 
1204 /** Calculate the tiled shape of a tensor
1205  *
1206  * @param[in] input_shape Input tensor shape
1207  * @param[in] multiples Paddings list
1208  *
1209  * @return the calculated shape
1210  */
1212 {
1213  TensorShape tiled_shape = input_shape;
1214  for(size_t dim = 0; dim < multiples.size(); ++dim)
1215  {
1216  tiled_shape.set(dim, input_shape[dim] * multiples[dim]);
1217  }
1218  return tiled_shape;
1219 }
1220 
1221 /** Calculate the reduced shape of a tensor given an axis
1222  *
1223  * @param[in] input Input tensor info
1224  * @param[in] axis Axis on which to perform reduction
1225  * @param[in] keep_dims (Optional) Whether to keep the dimension after reduction operation. Defaults to true.
1226  *
1227  * @return the calculated shape
1228  */
1229 inline TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis, bool keep_dims = true)
1230 {
1232 
1233  if(!keep_dims)
1234  {
1236  }
1237  else
1238  {
1239  output_shape.set(axis, 1);
1240  }
1241 
1242  return output_shape;
1243 }
1244 
1245 /** Calculate the upsampled shape of a tensor
1246  *
1247  * @param[in] input Input tensor info
1248  * @param[in] info Contains stride information (x and y)
1249  *
1250  * @return the calculated shape
1251  */
1253 {
1254  const DataLayout data_layout = input.data_layout();
1257 
1258  TensorShape scale_out_shape(input.tensor_shape());
1259  const unsigned int out_x = input.dimension(idx_width) * info.x();
1260  const unsigned int out_y = input.dimension(idx_height) * info.y();
1261  scale_out_shape.set(idx_width, out_x);
1262  scale_out_shape.set(idx_height, out_y);
1263 
1264  return scale_out_shape;
1265 }
1266 
1267 /** Get the tensor shape
1268  *
1269  * @param[in] data Input data
1270  *
1271  * @return the extracted tensor shape
1272  */
1273 template <typename T>
1275 {
1276  return data->info()->tensor_shape();
1277 }
1278 
1280 {
1281  return data->tensor_shape();
1282 }
1284 {
1285  return data->tensor_shape();
1286 }
1287 
1289 {
1290  return *data;
1291 }
1292 
1294 {
1295  return *data;
1296 }
1297 
1298 /** Calculate the unstack shape of a tensor
1299  *
1300  * @param[in] input_shape Input tensor shape
1301  * @param[in] axis Axis on which to perform the unstack operation
1302  *
1303  * @return the calculated shape
1304  */
1306 {
1309  return input_shape;
1310 }
1311 
1312 /** Calculate the concatenate output shape of the concatenate operation along a single axis
1313  *
1314  * @param[in] input Vector containing the shapes of the inputs
1315  * @param[in] axis Axis along which to concatenate the input tensors
1316  *
1317  * @return the calculated shape
1318  */
1319 template <typename T>
1320 inline TensorShape calculate_concatenate_shape(const std::vector<T *> &input, size_t axis)
1321 {
1322  TensorShape out_shape = extract_shape(input[0]);
1323 
1324 #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
1325  // All dimensions must match except the axis one
1326  for(unsigned int i = 0; i < MAX_DIMS; ++i)
1327  {
1328  if(i == axis)
1329  {
1330  continue;
1331  }
1332 
1333  for(const auto &tensor : input)
1334  {
1335  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
1336  const TensorShape shape = extract_shape(tensor);
1337  ARM_COMPUTE_ERROR_ON(out_shape[i] != shape[i]);
1338  }
1339  }
1340 #endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
1341 
1342  // Calculate output shape
1343  size_t new_size = 0;
1344  for(const auto &tensor : input)
1345  {
1346  const TensorShape shape = extract_shape(tensor);
1347  new_size += shape[axis];
1348  }
1349 
1350  out_shape.set(axis, new_size);
1351 
1352  return out_shape;
1353 }
1354 /** Calculate the stack output shape of a tensor
1355  *
1356  * @param[in] a Input tensor info
1357  * @param[in] axis Axis on which to perform the stack operation
1358  * @param[in] num_tensors Number of tensors to stack
1359  *
1360  * @return the calculated shape
1361  */
1362 inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
1363 {
1366 
1367  TensorShape shape_out{ a.tensor_shape() };
1368  shape_out.set(axis, num_tensors);
1369 
1370  unsigned int i_shift = 0;
1371 
1372  for(unsigned int i = 0; i < a.num_dimensions(); ++i)
1373  {
1374  if(i == axis)
1375  {
1376  i_shift++;
1377  }
1378 
1379  shape_out.set(i + i_shift, a.tensor_shape()[i]);
1380  }
1381  return shape_out;
1382 }
1383 
1384 inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
1385 {
1386  ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1);
1389 
1391  output_shape[actual_axis] = indices_shape[0];
1392 
1393  return output_shape;
1394 }
1395 } // namespace shape_calculator
1396 } // namespace misc
1397 } // namespace arm_compute
1398 #endif /* ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H */
unsigned int M
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
void shift_right(size_t step)
Shifts right the tensor shape increasing its dimensions.
Definition: TensorShape.h:144
TensorShape compute_slice_shape(const TensorShape &input_shape, const Coordinates &starts, const Coordinates &ends)
Calculate the slice output shape of a tensor.
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
Calculate the permuted shape of an input given a permutation vector.
TensorShape calculate_concatenate_shape(const std::vector< T * > &input, size_t axis)
Calculate the concatenate output shape of the concatenate operation along a single axis.
TensorShape compute_depth_to_space_shape(const TensorShape &input_shape, DataLayout data_layout, int block)
Calculate the depth to space output shape of a tensor.
TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, const ConvolutionInfo &info)
Calculate the depthwise convolution output shape of a tensor.
void remove_dimension(size_t n)
Accessor to remove the dimension n from the tensor shape.
Definition: TensorShape.h:111
TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd input transform shape.
TensorShape calculate_unstack_shape(TensorShape input_shape, unsigned int axis)
Calculate the unstack shape of a tensor.
TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width=1)
Calculate the transposed 1xW width element shape.
Descriptor used by the GEMM kernels.
TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
Calculate the stack output shape of a tensor.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:434
TensorShape compute_roi_align_shape(const ITensorInfo &input, const ITensorInfo &rois, ROIPoolingLayerInfo pool_info)
Calculate the output roi align shape of a tensor.
SimpleTensor< float > b
Definition: DFT.cpp:157
TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
Calculate the deep convolution shape output shape of a tensor.
unsigned int v0
Number of vertical blocks of size (m0xk0) stored on the same output row.
Definition: Types.h:1912
unsigned int depth_output_gemm3d
Depth of the output tensor in case is reinterpreted as 3D.
Winograd information.
Definition: Types.h:2117
GEMM reshape information class.
Definition: Types.h:1759
TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
Calculate the matrix multiplication output shape of two tensors.
TensorShape compute_reductionA_shape(const ITensorInfo &b)
Calculate the reductionA shape used in GEMMLowp.
TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis=1)
Calculate the softmax output shape of a tensor.
Coordinates BiStrides
Bidirectional strides.
Definition: Types.h:51
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)
Calculate the space to batch output shape of a tensor.
unsigned int h0
Number of horizontal blocks of size (k0xn0) stored on the same output row.
Definition: Types.h:1927
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
const DataLayout data_layout
Definition: Im2Col.cpp:151
GEMM LHS (Left Hand Side) matrix information.
Definition: Types.h:1903
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Size2D compute_winograd_convolution_tiles(const Size2D &in_dims, const Size2D &kernel_size, const Size2D &output_tile_size, const PadStrideInfo &conv_info)
Calculate the number of output tiles required by Winograd Convolution layer.
Definition: Helpers.h:211
TensorShape compute_reorg_output_shape(const ITensorInfo &input, int32_t stride)
Calculate the output shape of the reorg layer given a stride.
size_t x() const
Semantic accessor for width as x.
Definition: Size2D.h:74
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1252
unsigned int pad_top() const
Get the top padding.
Definition: Types.h:734
TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height=1, bool reinterpret_input_as_3d=false)
Calculate the interleaved shape of an input tensor.
TensorShape compute_output_stage_shape(const ITensorInfo &input, unsigned int gemm_3d_depth=1, bool batch_size_on_z=false)
Calculate the matrix multiplication output shape of two tensors.
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
TensorShape compute_min_max_shape(const ITensorInfo *input)
Calculate the min/max shape output shape of a tensor.
int32_t construct_slice_end_mask(Coordinates ends)
Constructs end mask in case we want to perform a slice operation using the strided slice interface.
TensorShape compute_strided_slice_shape(const ITensorInfo &input, const Coordinates &starts, const Coordinates &ends, const Coordinates &strides, int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
Calculate the strided slice output shape of a tensor.
std::pair< unsigned int, unsigned int > scaled_dimensions(int width, int height, int kernel_width, int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:395
constexpr size_t MAX_DIMS
Constant value used to indicate maximum dimensions of a Window, TensorShape and Coordinates.
Definition: Dimensions.h:38
TensorShape compute_transposed_shape(const ITensorInfo &input)
Calculate the transposed shape of a tensor.
void permute(Dimensions< T > &dimensions, const PermutationVector &perm)
Permutes given Dimensions according to a permutation vector.
Definition: Helpers.h:125
TensorShape compute_deconvolution_output_shape(const std::pair< unsigned int, unsigned int > &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
Calculate the output shape of the deconvolution layer.
TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
Calculate the output pool shape of a tensor.
int n() const
Number of matrix B columns.
Definition: Types.h:1797
TensorShape compute_flatten_shape(const ITensorInfo *input)
Calculate the flattened output shape of a tensor.
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
Definition: Types.h:1926
TensorShape compute_prior_box_shape(const ITensorInfo &input, const PriorBoxLayerInfo &info)
Calculate the prior box output shape of a tensor.
TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
Calculate the output tensor shape of a vector input given the convolution dimensions.
unsigned int m
Number of LHS rows.
unsigned int n
Number of RHS columns.
TensorShape input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void collapse_from(size_t start)
Collapse dimensions starting from a given point.
Definition: Dimensions.h:183
TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d=false)
Calculate the Left Hand Side matrix reshaped shape.
TensorShape calculate_reduce_mean_shape(ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims)
Calculate the output tensor shape for the reduce mean operation.
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1918
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
TensorShape compute_unpool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
Calculate the output unpool shape of a tensor.
unsigned int n0
Number of columns processed by the matrix multiplication.
Definition: Types.h:1925
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
const unsigned int num_groups
Definition: Im2Col.cpp:153
TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples)
Calculate the tiled shape of a tensor.
const size_t input_width
Coordinates of an item.
Definition: Coordinates.h:37
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:698
TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis, bool keep_dims=true)
Calculate the reduced shape of a tensor given an axis.
Pooling Layer Information struct.
Definition: Types.h:1142
TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info)
Calculate the Right Hand Side matrix reshaped shape.
bool reinterpret_input_as_3d
Flag used to reinterpret the input as 3D.
PriorBox layer info.
Definition: Types.h:767
TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy, std::pair< unsigned int, unsigned int > &out_dims, uint32_t &padx, uint32_t &pady)
Calculate the upsampled output shape used for deconvolution.
unsigned int pad_right() const
Get the right padding.
Definition: Types.h:729
const size_t input_height
Padding and stride information class.
Definition: Types.h:650
std::array< T, num_max_dimensions >::iterator begin()
Returns a read/write iterator that points to the first element in the dimension array.
Definition: Dimensions.h:215
TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd filter transform shape.
TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
Calculate the padded shape of a tensor.
Num samples, channels, height, width.
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:83
Strides of an item in bytes.
Definition: Strides.h:37
TensorShape compute_reductionB_shape(const ITensorInfo &a)
Calculate the reductionB shape used in GEMMLowp.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
TensorShape extract_shape(T *data)
Get the tensor shape.
const size_t weights_width
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
const size_t weights_height
PadStrideInfo pad_stride_info
Definition: Types.h:1230
TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info)
Calculate the upsampled shape of a tensor.
TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd output transform shape.
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int pooled_height() const
Get the pooled height of the layer.
Definition: Types.h:1257
TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias=false, unsigned int num_groups=1)
Calculate the reshaped shape of the weights.
int m() const
Number of matrix A rows.
Definition: Types.h:1789
ROI Pooling Layer Information class.
Definition: Types.h:1237
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
Num samples, height, width, channels.
TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
Calculate the batch to space output shape of a tensor.
TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
Calculate the transposed 1xW shape.
int depth_output_gemm3d() const
Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
Definition: Types.h:1832
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
Definition: Types.h:1911
unsigned int m0
Number of rows processed by the matrix multiplication.
Definition: Types.h:1910
Coordinates & convert_negative_axis(Coordinates &coords, int max_value)
Convert negative coordinates to positive in the range [0, num_dims_input].
Definition: Helpers.h:241
TensorShape compute_col2im_shape(const ITensorInfo &input, const Size2D &convolved_dims, bool batch_size_on_z, unsigned int num_groups=1)
Calculate the Col2Im shape.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
unsigned int pad_bottom() const
Get the bottom padding.
Definition: Types.h:739
bool reinterpret_input_as_3d() const
Flag which specifies if the input tensor has to be reinterpreted as 3D.
Definition: Types.h:1840
TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
Calculate the split output shape of a tensor.
unsigned int pad_left() const
Get the left padding.
Definition: Types.h:724
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
std::vector< uint32_t > Multiples
Information to produce a tiled version of a Tensor.
Definition: Types.h:437
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, unsigned int num_groups=1)
Calculate the im2col output shape of a tensor.
void collapse(size_t n, size_t first=0)
Collapse the first n dimensions.
Definition: TensorShape.h:133
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
size_t area() const
The area of the image or rectangle calculated as (width * height)
Definition: Size2D.h:53
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
TensorShape compute_space_to_depth_shape(const ITensorInfo *input, int32_t block_shape)
Calculate the space to batch output shape of a tensor.
TensorShape compute_strided_slice_output_shape(TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides, int32_t begin_mask=0, int32_t end_mask=0, int32_t shrink_axis_mask=0, bool return_unshrinked=false)
Computes output shape of strided slice.
TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)