Compute Library
 20.02.1
ShapeCalculator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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(ITensor *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->info()->num_dimensions();
55  convert_negative_axis(axis_local, input_dims);
56  TensorShape out_shape = input->info()->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 {
124  const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
125  const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
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 
134  output_shape.set(idx_width, output_shape[idx_width] / stride);
135  output_shape.set(idx_height, output_shape[idx_height] / stride);
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.
153  ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1);
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 reshaped shape of the weights to use in depthwise convolution
291  *
292  * @param[in] input Input tensor info
293  * @param[in] info Depthwise convolution information to be used for reshaping.
294  *
295  * @return the calculated shape
296  */
298 {
299  const auto data_layout = input.data_layout();
301 
305  const size_t num_channels = input.dimension(channel_idx);
306  const size_t num_rows = input.dimension(height_idx);
307  const size_t num_cols = input.dimension(width_idx);
308 
309  weights_shape.set(0, num_rows * num_cols * info.c0);
310  weights_shape.set(1, DIV_CEIL(num_channels, info.c0));
311  return weights_shape;
312 }
313 
314 /** Calculate the transposed 1xW shape
315  *
316  * @param[in] b Input tensor info
317  *
318  * @return the calculated shape
319  */
321 {
322  // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
323  TensorShape shape_transposed1xW_b{ b.tensor_shape() };
324  shape_transposed1xW_b.set(0, b.dimension(1) * 16);
325  shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
326 
327  return shape_transposed1xW_b;
328 }
329 
330 /** Calculate the transposed 1xW width element shape
331  *
332  * @param[in] b Input tensor info
333  * @param[in] mult_transpose1xW_width (Optional) Transpose1xW width
334  *
335  * @return the calculated shape
336  */
337 inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
338 {
339  // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
340  // The transpose1xW output matrix will have the following shape:
341  // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
342  ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
343  TensorShape shape_transposed1xW_b{ b.tensor_shape() };
344  const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
345  shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
346  shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
347 
348  return shape_transposed1xW_b;
349 }
350 
351 /** Calculate the reductionA shape used in GEMMLowp
352  *
353  * @param[in] b Input tensor info
354  *
355  * @return the calculated shape
356  */
358 {
359  TensorShape shape_vector_sum_col{ b.tensor_shape() };
360  if(shape_vector_sum_col.num_dimensions() > 1)
361  {
362  shape_vector_sum_col.remove_dimension(1);
363  }
364 
365  return shape_vector_sum_col;
366 }
367 
368 /** Calculate the reductionB shape used in GEMMLowp
369  *
370  * @param[in] a Input tensor info
371  *
372  * @return the calculated shape
373  */
375 {
376  TensorShape shape_vector_sum_row{ a.tensor_shape() };
377  shape_vector_sum_row.set(Window::DimX, a.dimension(1));
378  if(shape_vector_sum_row.num_dimensions() > 1)
379  {
380  shape_vector_sum_row.remove_dimension(1);
381  }
382 
383  return shape_vector_sum_row;
384 }
385 
386 /** Calculate the Col2Im shape
387  *
388  * @param[in] input Input tensor info
389  * @param[in] convolved_dims Convolved dimensions
390  * @param[in] batch_size_on_z True if batch size is on z axis
391  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
392  *
393  * @return the calculated shape
394  */
395 inline TensorShape compute_col2im_shape(const ITensorInfo &input, const Size2D &convolved_dims, bool batch_size_on_z, unsigned int num_groups = 1)
396 {
398  ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area()));
399  ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
400 
401  const DataLayout data_layout = input.data_layout();
405 
406  TensorShape col2im_shape{ input.tensor_shape() };
407  // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape,
408  // as first three will be override by H,W,C data
409  if(batch_size_on_z && num_groups == 1)
410  {
411  col2im_shape.shift_right(1);
412  }
413  col2im_shape.set(width_idx, convolved_dims.width);
414  col2im_shape.set(height_idx, convolved_dims.height);
415  col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups);
416 
417  return col2im_shape;
418 }
419 
420 /** Calculate the transposed shape of a tensor
421  *
422  * @param[in] input Input tensor info
423  *
424  * @return the calculated shape
425  */
427 {
428  TensorShape shape_transposed{ input.tensor_shape() };
429 
430  shape_transposed.set(0, input.dimension(1));
431  shape_transposed.set(1, input.dimension(0));
432 
433  return shape_transposed;
434 }
435 
436 /** Calculate the depthwise convolution output shape of a tensor
437  *
438  * @param[in] input Input tensor info
439  * @param[in] weights Weights tensor info
440  * @param[in] conv_info Padding and stride information to use for the convolution.
441  * @param[in] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth.
442  * @param[in] dilation Dilation, in elements, across x and y. Defaults to (1, 1).
443  *
444  * @return the calculated shape
445  */
447  1U))
448 {
449  const TensorShape input_shape{ input.tensor_shape() };
450  const TensorShape weights_shape{ weights.tensor_shape() };
451 
452  const DataLayout data_layout = input.data_layout();
456 
457  const DataLayout weights_data_layout = weights.data_layout();
458  const int weights_width_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::WIDTH);
459  const int weights_height_idx = get_data_layout_dimension_index(weights_data_layout, DataLayoutDimension::HEIGHT);
460 
461  unsigned int output_width = 0;
462  unsigned int output_height = 0;
463  std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
464  weights_shape[weights_width_idx], weights_shape[weights_height_idx],
466 
468  output_shape.set(width_idx, output_width);
469  output_shape.set(height_idx, output_height);
470  output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier);
471 
472  return output_shape;
473 }
474 
475 /** Calculate the upsampled output shape used for deconvolution
476  *
477  * @param[in] input Input tensor info
478  * @param[in] weights Weights tensor shape
479  * @param[in] sx Stride on x axis
480  * @param[in] sy Stride on y axis
481  * @param[in] out_dims Output shape dimensions
482  * @param[in] padx Padding on x axis
483  * @param[in] pady Padding on y axis
484  *
485  * @return the calculated shape
486  */
487 inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy,
488  std::pair<unsigned int, unsigned int> &out_dims, unsigned int &padx, unsigned int &pady)
489 {
490  const DataLayout data_layout = input.data_layout();
493 
494  // Find the upsampled dimensions
495  unsigned int out_x = (input.dimension(idx_w) - 1) * sx + 1;
496  unsigned int out_y = (input.dimension(idx_h) - 1) * sy + 1;
497 
498  // Find the padding needed for the convolution with stride 1 in order to match output shape
499  padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1);
500  pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1);
501  out_x += padx;
502  out_y += pady;
503 
504  TensorShape scale_out_shape(input.tensor_shape());
505  scale_out_shape.set(idx_w, out_x);
506  scale_out_shape.set(idx_h, out_y);
507 
508  return scale_out_shape;
509 }
510 
511 /** Calculate the output shape of the deconvolution layer
512  *
513  * @param[in] out_dims Output x and y shape dimensions
514  * @param[in] input Input tensor info
515  * @param[in] weights Weights tensor shape
516  *
517  * @return the calculated shape
518  */
519 inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights)
520 {
521  const TensorShape input_shape{ input.tensor_shape() };
522  const TensorShape weights_shape{ weights.tensor_shape() };
523 
524  const DataLayout data_layout = input.data_layout();
529 
530  TensorShape out_shape{ input_shape };
531  out_shape.set(width_idx, out_dims.first);
532  out_shape.set(height_idx, out_dims.second);
533  out_shape.set(channel_idx, weights_shape[batch_idx]);
534  return out_shape;
535 }
536 
537 /** Calculate the im2col output shape of a tensor
538  *
539  * @param[in] input Input tensor info
540  * @param[in] kernel_dims The kernel dimensions (width and height).
541  * @param[in] conv_info Contains padding and stride information
542  * @param[in] has_bias In case biases are provided expands the matrix with 1
543  * @param[in] dilation Dilation, in elements, across x and y
544  * @param[in] batch_size_on_z True if batch size is on z axis
545  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution
546  *
547  * @return the calculated shape
548  */
549 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,
550  unsigned int num_groups = 1)
551 {
552  // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
553  // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
554 
556  ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
557  ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
558 
559  TensorShape output_shape{ input->tensor_shape() };
560 
561  const DataLayout data_layout = input->data_layout();
565 
566  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);
567  output_shape.set(0, (output_shape[channel_idx] / num_groups * kernel_dims.area() + (has_bias ? 1 : 0))); // NOLINT
568  output_shape.set(1, (out_dims.first * out_dims.second));
569  if(batch_size_on_z && output_shape.num_dimensions() >= 3)
570  {
571  output_shape.remove_dimension(2);
572  }
573  else
574  {
575  output_shape.set(2, num_groups);
576  }
577 
578  return output_shape;
579 }
580 
581 /** Calculate the flattened output shape of a tensor
582  *
583  * @param[in] input Input tensor info
584  *
585  * @return the calculated shape
586  */
588 {
589  // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
590 
591  TensorShape output_shape{ input->tensor_shape() };
592 
593  output_shape.collapse(3);
594 
595  return output_shape;
596 }
597 
598 /** Calculate the softmax output shape of a tensor
599  *
600  * @param[in] input Input tensor info
601  * @param[in] axis (Optional) Softmax axis
602  *
603  * @return the calculated shape
604  */
606 {
607  // The output shape will be a 2D version of the input. For instance:
608  // - [x,y,z] and axis 1 will return [x, y*z]
609  // - [x,y,z,w] and axis 2 will return [x*y, w*z]
610  // - [x,y,z,w] and axis 3 will return [x*y*z, w]
611  TensorShape shape2D = input->tensor_shape();
612 
613  if(axis < input->num_dimensions())
614  {
615  // Collapse from axis onward (this changes the shape)
616  shape2D.collapse_from(axis);
617 
618  // Collapse the rest (collapse is inclusive)
619  shape2D.collapse(shape2D.num_dimensions() - 1);
620  }
621  else
622  {
623  // Collapse everything
624  shape2D.collapse(shape2D.num_dimensions());
625  }
626 
627  if(axis == 0)
628  {
629  // If axis is zero the first dim should be one. Since
630  // collapse is an inclusive operation we need to shift
631  shape2D.shift_right(1);
632  }
633 
634  return shape2D;
635 }
636 
637 /** Calculate the winograd filter transform shape
638  *
639  * @param[in] input Input tensor info
640  * @param[in] winograd_info Winograd information
641  *
642  * @return the calculated shape
643  */
645 {
646  TensorShape tensor_shape{ input.tensor_shape() };
647 
648  const Size2D kernel_size = winograd_info.kernel_size;
649  const Size2D output_tile_size = winograd_info.output_tile_size;
650  const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
651 
652  tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
653  tensor_shape.set(Window::DimX, input.dimension(3));
654  tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
655  tensor_shape.set(Window::DimZ, input_tile_size.area());
656 
657  return tensor_shape;
658 }
659 
660 /** Calculate the winograd input transform shape
661  *
662  * @param[in] input Input tensor info
663  * @param[in] winograd_info Winograd information
664  *
665  * @return the calculated shape
666  */
668 {
669  const PadStrideInfo conv_info = winograd_info.convolution_info;
670  const Size2D kernel_size = winograd_info.kernel_size;
671  const Size2D output_tile_size = winograd_info.output_tile_size;
672  const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
673 
674  const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
675  const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
676  const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
677 
678  // Compute the number of output tiles along the x and y direction of size "output_tile_size"
679  const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
680  kernel_size,
681  output_tile_size,
682  conv_info);
683 
684  const unsigned int width = input.tensor_shape()[idx_c];
685  const unsigned int height = num_tiles.area();
686  const unsigned int depth = input_tile_size.area();
687 
688  TensorShape output_shape{ input.tensor_shape() };
689  output_shape.set(0, width);
690  output_shape.set(1, height);
691  output_shape.set(2, depth);
692 
693  return output_shape;
694 }
695 
696 /** Calculate the winograd output transform shape
697  *
698  * @param[in] input Input tensor info
699  * @param[in] winograd_info Winograd information
700  *
701  * @return the calculated shape
702  */
704 {
705  const PadStrideInfo conv_info = winograd_info.convolution_info;
706  const Size2D kernel_size = winograd_info.kernel_size;
707  const Size2D input_dimensions = winograd_info.input_dimensions;
708  const DataLayout data_layout = winograd_info.output_data_layout;
709 
710  // Compute output shape
711  unsigned int output_width = 0;
712  unsigned int output_height = 0;
713  std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
714  kernel_size.width, kernel_size.height, conv_info);
715 
716  TensorShape tensor_shape{ input.tensor_shape() };
717 
718  // Output dimension
719  const unsigned int out_w = output_width;
720  const unsigned int out_h = output_height;
721  const unsigned int out_c = input.dimension(0);
722 
726 
727  return tensor_shape;
728 }
729 
730 /** Calculate the deep convolution shape output shape of a tensor
731  *
732  * @param[in] input Input tensor info
733  * @param[in] weights Weights tensor info
734  * @param[in] conv_info Contains padding and stride information
735  *
736  * @return the calculated shape
737  */
739 {
740  const TensorShape input_shape{ input.tensor_shape() };
741  const TensorShape weights_shape{ weights.tensor_shape() };
742 
743  const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
744  const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
745  const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
746 
747  const unsigned int input_width = input_shape[idx_width];
748  const unsigned int input_height = input_shape[idx_height];
749  const unsigned int weights_width = weights_shape[idx_width];
750  const unsigned int weights_height = weights_shape[idx_height];
751  const unsigned int weights_out_channel = weights_shape[3];
752  unsigned int output_width = 0;
753  unsigned int output_height = 0;
754  std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, weights_width, weights_height, conv_info);
755 
757  output_shape.set(idx_width, output_width);
758  output_shape.set(idx_height, output_height);
759  output_shape.set(idx_channel, weights_out_channel);
760 
761  return output_shape;
762 }
763 
764 /** Calculate the min/max shape output shape of a tensor
765  *
766  * @param[in] input Input tensor info
767  *
768  * @return the calculated shape
769  */
771 {
772  TensorShape output_shape{ input->tensor_shape() };
773  output_shape.set(Window::DimX, 2);
774  output_shape.remove_dimension(1);
775  output_shape.remove_dimension(1);
776 
777  return output_shape;
778 }
779 
780 /** Calculate the output pool shape of a tensor
781  *
782  * @param[in] input Input tensor info
783  * @param[in] pool_info Pooling layer info
784  *
785  * @return the calculated shape
786  */
788 {
789  unsigned int pooled_w = 0;
790  unsigned int pooled_h = 0;
791 
792  TensorShape output_shape{ input.tensor_shape() };
793 
794  const bool is_global_pooling = pool_info.is_global_pooling;
795  const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
796  const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
797  const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size.width;
798  const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size.height;
799 
800  std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
801  output_shape[idx_height],
802  pool_size_x,
803  pool_size_y,
804  pool_info.pad_stride_info);
805 
806  output_shape.set(idx_width, pooled_w);
807  output_shape.set(idx_height, pooled_h);
808 
809  return output_shape;
810 }
811 
812 /** Calculate the output roi align shape of a tensor
813  *
814  * @param[in] input Input tensor info
815  * @param[in] rois Rois tensor info
816  * @param[in] pool_info Pooling layer info
817  *
818  * @return the calculated shape
819  */
821 {
822  TensorShape output_shape{ input.tensor_shape() };
823 
824  const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
825  const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
826 
827  output_shape.set(idx_width, pool_info.pooled_width());
828  output_shape.set(idx_height, pool_info.pooled_height());
829  output_shape.set(3, rois.dimension(1));
830 
831  return output_shape;
832 }
833 
834 /** Calculate the RNN shape of a tensor
835  *
836  * @param[in] input Input tensor info
837  * @param[in] batch_size Batch size
838  *
839  * @return the calculated shape
840  */
841 inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
842 {
843  TensorShape output_shape{ input->tensor_shape() };
844  output_shape.set(1, batch_size);
845 
846  return output_shape;
847 }
848 
849 /** Calculate the matrix multiplication output shape of two tensors
850  *
851  * @param[in] input0 First input tensor info
852  * @param[in] input1 Second input tensor info
853  * @param[in] is_interleaved_transposed True if the input is interleaved transposed
854  * @param[in] reshape_info GEMM reshape info
855  *
856  * @return the calculated shape
857  */
858 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
859 {
860  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
861  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");
862 
863  const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
864  const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0;
865  const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1;
866  const int m = reshape_info.reinterpret_input_as_3d() ? input0.dimension(1) * input0.dimension(2) : input0.dimension(1);
867 
868  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
869  // dimension of the output tensor
870  const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
871  const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d;
872  const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
873  const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
874 
876 
877  output_shape.set(0, dim0);
878  output_shape.set(1, dim1);
879  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2);
880  output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
881  output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
882 
883  return output_shape;
884 }
885 
886 /** Calculate the matrix multiplication output shape of two tensors
887  *
888  * @note Deprecated. Remove when GEMMReshapeInfo is not used anymore by any other kernels
889  *
890  * @param[in] input0 First input tensor info
891  * @param[in] input1 Second input tensor info
892  * @param[in] gemm_info GEMM reshape info
893  *
894  * @return the calculated shape
895  */
896 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMReshapeInfo &gemm_info)
897 {
898  ARM_COMPUTE_UNUSED(input1);
899  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
900 
901  const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d();
902  const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d() != 0;
903  const int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d() : 1;
904 
906 
907  if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
908  {
909  output_shape.set(0, gemm_info.n());
910  output_shape.set(1, gemm_info.m());
911  }
912  else
913  {
914  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
915  // dimension of the output tensor
916  const int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
917  output_shape.set(0, gemm_info.n());
918  output_shape.set(1, gemm_info.m() / depth_output_gemm3d);
919  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
920  output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
921  }
922 
923  return output_shape;
924 }
925 
926 /** Calculate the matrix multiplication output shape of two tensors
927  *
928  * @param[in] input0 First input tensor info
929  * @param[in] input1 Second input tensor info
930  * @param[in] gemm_info GEMM kernel info used to retrieve the original dimensions of the input matrices
931  *
932  * @return the calculated shape
933  */
934 inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMKernelInfo &gemm_info)
935 {
936  ARM_COMPUTE_UNUSED(input1);
937  ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
938 
939  const bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d;
940  const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0;
941  const unsigned int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d : 1;
942 
944 
945  if(!reinterpret_input_as_3d && !reinterpret_output_as_3d)
946  {
947  output_shape.set(0, gemm_info.n);
948  output_shape.set(1, gemm_info.m);
949  }
950  else
951  {
952  // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
953  // dimension of the output tensor
954  const unsigned int batch_size = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
955  output_shape.set(0, gemm_info.n);
956  output_shape.set(1, gemm_info.m / depth_output_gemm3d);
957  output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : batch_size);
958  output_shape.set(3, reinterpret_output_as_3d ? batch_size : 1);
959  }
960 
961  return output_shape;
962 }
963 
964 /** Calculate the matrix multiplication output shape of two tensors
965  *
966  * @param[in] input Input tensor info
967  * @param[in] gemm_3d_depth (Optional) GEMM 3d depth
968  * @param[in] batch_size_on_z (Optional) True if batch size is on z axis
969  *
970  * @return the calculated shape
971  */
972 inline TensorShape compute_output_stage_shape(const ITensorInfo &input, unsigned int gemm_3d_depth = 1, bool batch_size_on_z = false)
973 {
974  ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1);
975 
976  TensorShape output_shape = input.tensor_shape();
977  if(gemm_3d_depth > 1)
978  {
979  if(batch_size_on_z)
980  {
981  output_shape.shift_right(1);
982  }
983  output_shape.set(0, input.tensor_shape().x());
984  output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth);
985  output_shape.set(2, gemm_3d_depth);
986  }
987 
988  return output_shape;
989 }
990 
991 /** Calculate the strided slice output shape of a tensor
992  *
993  * @param[in] input Input tensor info
994  * @param[in] starts The starts of the dimensions of the input tensor to be sliced
995  * @param[in] ends The ends of the dimensions of the input tensor to be sliced
996  * @param[in] strides The strides of the dimensions of the input tensor to be sliced
997  * @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.
998  * @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.
999  * @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
1000  *
1001  * @return the calculated shape
1002  */
1004  const Coordinates &starts, const Coordinates &ends, const Coordinates &strides,
1005  int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask)
1006 {
1008  return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask);
1009 }
1010 
1011 /** Calculate the slice output shape of a tensor
1012  *
1013  * @param[in] input_shape Input tensor info
1014  * @param[in] starts The starts of the dimensions of the input tensor to be sliced
1015  * @param[in] ends The ends of the dimensions of the input tensor to be sliced
1016  *
1017  * @return the calculated shape
1018  */
1020 {
1022 
1024  starts, ends, BiStrides(),
1025  0, construct_slice_end_mask(ends), 0);
1026 }
1027 
1028 /** Calculate the batch to space output shape of a tensor
1029  *
1030  * @param[in] input Input tensor info
1031  * @param[in] block_x Block shape x value
1032  * @param[in] block_y Block shape y value
1033  *
1034  * @return the calculated shape
1035  */
1036 inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y)
1037 {
1038  ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0);
1039 
1040  const DataLayout data_layout = input->data_layout();
1044 
1045  TensorShape output_shape{ input->tensor_shape() };
1046  output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x);
1047  output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y);
1048  output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
1049 
1050  return output_shape;
1051 }
1052 
1053 /** Calculate the depth to space output shape of a tensor
1054  *
1055  * @param[in] input Input tensor info
1056  * @param[in] block Block shape value
1057  *
1058  * @return the calculated shape
1059  */
1061 {
1062  ARM_COMPUTE_ERROR_ON(block < 2);
1063 
1064  const DataLayout data_layout = input->data_layout();
1068 
1069  TensorShape output_shape{ input->tensor_shape() };
1070  output_shape.set(idx_width, input->dimension(idx_width) * block);
1071  output_shape.set(idx_height, input->dimension(idx_height) * block);
1072  output_shape.set(idx_channel, input->dimension(idx_channel) / (block * block));
1073 
1074  return output_shape;
1075 }
1076 
1077 /** Calculate the split output shape of a tensor
1078  *
1079  * @param[in] input Input tensor info
1080  * @param[in] axis Axis on which to split the input
1081  * @param[in] num_splits Number of splits
1082  *
1083  * @return the calculated shape
1084  */
1085 inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
1086 {
1087  TensorShape empty_shape;
1088  empty_shape.set(0, 0);
1089 
1090  TensorShape out_shape{ input->tensor_shape() };
1091 
1092  // Return empty shape if axis is invalid
1093  if(axis > input->tensor_shape().num_dimensions())
1094  {
1095  return empty_shape;
1096  }
1097 
1098  size_t axis_size = out_shape[axis];
1099 
1100  // Return empty shape if num_split is not valid
1101  if(axis_size % num_splits)
1102  {
1103  return empty_shape;
1104  }
1105 
1106  out_shape[axis] = axis_size / num_splits;
1107  return out_shape;
1108 }
1109 
1110 /** Calculate the space to batch output shape of a tensor
1111  *
1112  * @param[in] input Input tensor info
1113  * @param[in] block_x Block shape x value
1114  * @param[in] block_y Block shape y value
1115  * @param[in] padding_left Left padding values
1116  * @param[in] padding_right Right padding values
1117  *
1118  * @return the calculated shape
1119  */
1120 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)
1121 {
1122  TensorShape output_shape{ input->tensor_shape() };
1123 
1124  const DataLayout data_layout = input->data_layout();
1128 
1129  output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x + padding_left.x() + padding_right.x());
1130  output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y + padding_left.y() + padding_right.y());
1131  output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y));
1132 
1133  return output_shape;
1134 }
1135 
1136 /** Calculate the space to batch output shape of a tensor
1137  *
1138  * @param[in] input Input tensor info
1139  * @param[in] block_shape Block shape value
1140  *
1141  * @return the calculated shape
1142  */
1143 inline TensorShape compute_space_to_depth_shape(const ITensorInfo *input, int32_t block_shape)
1144 {
1145  TensorShape output_shape{ input->tensor_shape() };
1146 
1147  const DataLayout data_layout = input->data_layout();
1151 
1152  output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_shape);
1153  output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_shape);
1154  output_shape.set(idx_depth, input->tensor_shape()[idx_depth] / (block_shape * block_shape));
1155 
1156  return output_shape;
1157 }
1158 
1159 /** Calculate the prior box output shape of a tensor
1160  *
1161  * @param[in] input Input tensor info
1162  * @param[in] info PriorBoxLayer info
1163  *
1164  * @return the calculated shape
1165  */
1167 {
1168  DataLayout data_layout = input.data_layout();
1171  const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
1172 
1174  output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4);
1175  output_shape.set(1, 2);
1176 
1177  return output_shape;
1178 }
1179 
1180 /** Calculate the padded shape of a tensor
1181  *
1182  * @param[in] input_shape Input tensor shape
1183  * @param[in] padding Paddings list
1184  *
1185  * @return the calculated shape
1186  */
1188 {
1189  TensorShape padded_shape = input_shape;
1190  for(size_t dim = 0; dim < padding.size(); ++dim)
1191  {
1192  const auto &padding_pair = padding[dim];
1193  const uint32_t shape_on_index = (padded_shape.num_dimensions() <= dim) ? 1 : input_shape[dim];
1194  padded_shape.set(dim, padding_pair.first + shape_on_index + padding_pair.second);
1195  }
1196  return padded_shape;
1197 }
1198 
1199 /** Calculate the tiled shape of a tensor
1200  *
1201  * @param[in] input_shape Input tensor shape
1202  * @param[in] multiples Paddings list
1203  *
1204  * @return the calculated shape
1205  */
1207 {
1208  TensorShape tiled_shape = input_shape;
1209  for(size_t dim = 0; dim < multiples.size(); ++dim)
1210  {
1211  tiled_shape.set(dim, input_shape[dim] * multiples[dim]);
1212  }
1213  return tiled_shape;
1214 }
1215 
1216 /** Calculate the reduced shape of a tensor given an axis
1217  *
1218  * @param[in] input Input tensor info
1219  * @param[in] axis Axis on which to perform reduction
1220  * @param[in] keep_dims (Optional) Whether to keep the dimension after reduction operation. Defaults to true.
1221  *
1222  * @return the calculated shape
1223  */
1224 inline TensorShape compute_reduced_shape(const TensorShape &input, unsigned int axis, bool keep_dims = true)
1225 {
1227 
1228  if(!keep_dims)
1229  {
1230  output_shape.remove_dimension(axis);
1231  }
1232  else
1233  {
1234  output_shape.set(axis, 1);
1235  }
1236 
1237  return output_shape;
1238 }
1239 
1240 /** Calculate the upsampled shape of a tensor
1241  *
1242  * @param[in] input Input tensor info
1243  * @param[in] info Contains stride information (x and y)
1244  *
1245  * @return the calculated shape
1246  */
1248 {
1249  const DataLayout data_layout = input.data_layout();
1252 
1253  TensorShape scale_out_shape(input.tensor_shape());
1254  const unsigned int out_x = input.dimension(idx_width) * info.x();
1255  const unsigned int out_y = input.dimension(idx_height) * info.y();
1256  scale_out_shape.set(idx_width, out_x);
1257  scale_out_shape.set(idx_height, out_y);
1258 
1259  return scale_out_shape;
1260 }
1261 
1262 /** Get the tensor shape
1263  *
1264  * @param[in] data Input data
1265  *
1266  * @return the extracted tensor shape
1267  */
1268 template <typename T>
1270 {
1271  return data->info()->tensor_shape();
1272 }
1273 
1275 {
1276  return data->tensor_shape();
1277 }
1279 {
1280  return data->tensor_shape();
1281 }
1282 
1284 {
1285  return *data;
1286 }
1287 
1289 {
1290  return *data;
1291 }
1292 
1293 /** Calculate the unstack shape of a tensor
1294  *
1295  * @param[in] input_shape Input tensor shape
1296  * @param[in] axis Axis on which to perform the unstack operation
1297  *
1298  * @return the calculated shape
1299  */
1301 {
1302  ARM_COMPUTE_ERROR_ON(axis > input_shape.num_dimensions());
1303  input_shape.remove_dimension(axis);
1304  return input_shape;
1305 }
1306 
1307 /** Calculate the concatenate output shape of the concatenate operation along a single axis
1308  *
1309  * @param[in] input Vector containing the shapes of the inputs
1310  * @param[in] axis Axis along which to concatenate the input tensors
1311  *
1312  * @return the calculated shape
1313  */
1314 template <typename T>
1315 inline TensorShape calculate_concatenate_shape(const std::vector<T *> &input, size_t axis)
1316 {
1317  TensorShape out_shape = extract_shape(input[0]);
1318 
1319 #if defined(ARM_COMPUTE_ASSERTS_ENABLED)
1320  // All dimensions must match except the axis one
1321  for(unsigned int i = 0; i < MAX_DIMS; ++i)
1322  {
1323  if(i == axis)
1324  {
1325  continue;
1326  }
1327 
1328  for(const auto &tensor : input)
1329  {
1330  ARM_COMPUTE_ERROR_ON(tensor == nullptr);
1331  const TensorShape shape = extract_shape(tensor);
1332  ARM_COMPUTE_ERROR_ON(out_shape[i] != shape[i]);
1333  }
1334  }
1335 #endif // defined(ARM_COMPUTE_ASSERTS_ENABLED)
1336 
1337  // Calculate output shape
1338  size_t new_size = 0;
1339  for(const auto &tensor : input)
1340  {
1341  const TensorShape shape = extract_shape(tensor);
1342  new_size += shape[axis];
1343  }
1344 
1345  out_shape.set(axis, new_size);
1346 
1347  return out_shape;
1348 }
1349 /** Calculate the stack output shape of a tensor
1350  *
1351  * @param[in] a Input tensor info
1352  * @param[in] axis Axis on which to perform the stack operation
1353  * @param[in] num_tensors Number of tensors to stack
1354  *
1355  * @return the calculated shape
1356  */
1357 inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors)
1358 {
1361 
1362  TensorShape shape_out{ a.tensor_shape() };
1363  shape_out.set(axis, num_tensors);
1364 
1365  unsigned int i_shift = 0;
1366 
1367  for(unsigned int i = 0; i < a.num_dimensions(); ++i)
1368  {
1369  if(i == axis)
1370  {
1371  i_shift++;
1372  }
1373 
1374  shape_out.set(i + i_shift, a.tensor_shape()[i]);
1375  }
1376  return shape_out;
1377 }
1378 
1379 inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
1380 {
1381  ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1);
1382  ARM_COMPUTE_ERROR_ON(input_shape.num_dimensions() > 4);
1383  ARM_COMPUTE_ERROR_ON(actual_axis >= input_shape.num_dimensions());
1384 
1386  output_shape[actual_axis] = indices_shape[0];
1387 
1388  return output_shape;
1389 }
1390 } // namespace shape_calculator
1391 } // namespace misc
1392 } // namespace arm_compute
1393 #endif /* ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H */
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:143
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.
const DataLayout data_layout
Definition: Im2Col.cpp:146
void remove_dimension(size_t n)
Accessor to remove the dimension n from the tensor shape.
Definition: TensorShape.h:110
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.
TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier, const Size2D &dilation=Size2D(1U, 1U))
Calculate the depthwise convolution 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:455
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:1963
unsigned int depth_output_gemm3d
Depth of the output tensor in case is reinterpreted as 3D.
Winograd information.
Definition: Types.h:2154
GEMM reshape information class.
Definition: Types.h:1823
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:50
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:1973
#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_depth_to_space_shape(const ITensorInfo *input, int block)
Calculate the depth to space output shape of a tensor.
TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
Calculate the RNN shape of a tensor.
GEMM LHS (Left Hand Side) matrix information.
Definition: Types.h:1959
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:744
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:77
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1389
TensorShape compute_reshaped_depthwise_weights_shape(const ITensorInfo &input, const DepthwiseConvolutionReshapeInfo &info)
Calculate the reshaped shape of the weights to use in depthwise convolution.
Interface for NEON tensor.
Definition: ITensor.h:36
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-2020 ARM Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:93
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.
constexpr auto DIV_CEIL(S val, T m) -> decltype((val+m - 1)/m)
Calculate the rounded up quotient of val / m.
Definition: Utils.h:53
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:402
constexpr size_t MAX_DIMS
Constant value used to indicate maximum dimensions of a Window, TensorShape and Coordinates.
Definition: Dimensions.h:37
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:570
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:1861
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:1972
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.
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:162
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.
GEMM RHS (Right Hand Side) matrix information.
Definition: Types.h:1969
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
unsigned int n0
Number of columns processed by the matrix multiplication.
Definition: Types.h:1971
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
const unsigned int num_groups
Definition: Im2Col.cpp:148
TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples)
Calculate the tiled shape of a tensor.
Coordinates of an item.
Definition: Coordinates.h:37
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:1211
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:836
Padding and stride information class.
Definition: Types.h:686
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:194
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.
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, unsigned int &padx, unsigned int &pady)
Calculate the upsampled output shape used for deconvolution.
Num samples, channels, height, width.
size_t y() const
Semantic accessor for height as y.
Definition: Size2D.h:86
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.
PadStrideInfo pad_stride_info
Definition: Types.h:1367
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:92
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:1394
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:1853
ROI Pooling Layer Information class.
Definition: Types.h:1374
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:122
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:1896
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
unsigned int k0
Number of partial accumulations performed by the matrix multiplication.
Definition: Types.h:1962
unsigned int m0
Number of rows processed by the matrix multiplication.
Definition: Types.h:1961
Coordinates & convert_negative_axis(Coordinates &coords, int max_value)
Convert negative coordinates to positive in the range [0, num_dims_input].
Definition: Helpers.h:774
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:327
bool reinterpret_input_as_3d() const
Flag which specifies if the input tensor has to be reinterpreted as 3D.
Definition: Types.h:1904
TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits)
Calculate the split output shape of a tensor.
DataLayout
[DataLayout enum definition]
Definition: Types.h:117
std::vector< uint32_t > Multiples
Information to produce a tiled version of a Tensor.
Definition: Types.h:458
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:132
TensorShape calculate_reduce_mean_shape(ITensor *input, const Coordinates &reduction_axis, bool keep_dims)
Calculate the output tensor shape for the reduce mean operation.
size_t area() const
The area of the image or rectangle calculated as (width * height)
Definition: Size2D.h:53
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)