blob: c40e7119b274756f01e790cf884351955fa3912b [file] [log] [blame]
Georgios Pinitas358ca202017-12-07 16:47:52 +00001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitas358ca202017-12-07 16:47:52 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__
25#define __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__
26
Georgios Pinitas9be0c5a2018-02-19 12:46:29 +000027#include "arm_compute/core/Helpers.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000028#include "arm_compute/core/ITensorInfo.h"
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000029#include "arm_compute/core/Utils.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000030
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000031#include <cmath>
32
Georgios Pinitas358ca202017-12-07 16:47:52 +000033namespace arm_compute
34{
35namespace misc
36{
37namespace shape_calculator
38{
Abe Mbise7784c832018-05-31 16:48:41 +010039inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout)
40{
41 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
42 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
43 const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
44
45 TensorShape output_shape(input);
46 output_shape.set(idx_w, conv_w);
47 output_shape.set(idx_h, conv_h);
48 output_shape.set(idx_c, input.x() / (conv_w * conv_h));
49
50 return output_shape;
51}
Pablo Tello00afd112018-01-04 10:34:24 +000052inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
53{
54 TensorShape output_shape = input.tensor_shape();
55 permute(output_shape, perm);
56 return output_shape;
57}
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010058inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +000059{
Giorgio Arena088c2b02018-08-07 16:59:05 +010060 // Number of groups greater than one are only supported for NCHW data layout, and the number of weights must be a multiple of it.
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010061 ARM_COMPUTE_ERROR_ON(num_groups == 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010062 ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010063 ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010064
Georgios Pinitas78c00902018-01-09 17:33:11 +000065 // Calculate output shape
66 TensorShape weights_reshaped{ weights.tensor_shape() };
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010067 weights_reshaped.set(3, weights_reshaped[3] / num_groups);
68
Georgios Pinitas78c00902018-01-09 17:33:11 +000069 weights_reshaped.collapse(3);
70 const size_t tmp_dim = weights_reshaped[0];
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010071 weights_reshaped.set(0, weights_reshaped[1]);
Georgios Pinitas78c00902018-01-09 17:33:11 +000072 weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010073 if(weights.num_dimensions() < 5)
74 {
75 weights_reshaped.set(2, num_groups);
76 }
Georgios Pinitas78c00902018-01-09 17:33:11 +000077
78 return weights_reshaped;
79}
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010080inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1, bool reinterpret_input_as_3d = false)
Georgios Pinitas358ca202017-12-07 16:47:52 +000081{
Gian Marco36a0a462018-01-12 10:21:40 +000082 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
83 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
84 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +000085 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +000086 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +010087 if(reinterpret_input_as_3d)
88 {
89 const int M = a.dimension(1) * a.dimension(2);
90 const int height = std::ceil(M / static_cast<float>(interleave_width));
91 shape_interleaved_a.set(1, height);
92 shape_interleaved_a.remove_dimension(2);
93 }
94 else
95 {
96 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
97 }
Georgios Pinitas358ca202017-12-07 16:47:52 +000098
99 return shape_interleaved_a;
100}
101inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
102{
103 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
104 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
105 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
106 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
107
108 return shape_transposed1xW_b;
109}
Gian Marco36a0a462018-01-12 10:21:40 +0000110inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000111{
Gian Marco36a0a462018-01-12 10:21:40 +0000112 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
113 // The transpose1xW output matrix will have the following shape:
114 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
115 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000116 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +0000117 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
119 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
120
121 return shape_transposed1xW_b;
122}
123inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
124{
125 TensorShape shape_vector_sum_col{ b.tensor_shape() };
126 if(shape_vector_sum_col.num_dimensions() > 1)
127 {
128 shape_vector_sum_col.remove_dimension(1);
129 }
130
131 return shape_vector_sum_col;
132}
133inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
134{
135 TensorShape shape_vector_sum_row{ a.tensor_shape() };
136 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
137 if(a.num_dimensions() > 1)
138 {
139 shape_vector_sum_row.remove_dimension(1);
140 }
141
142 return shape_vector_sum_row;
143}
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100144inline TensorShape compute_col2im_shape(const ITensorInfo &input, std::pair<unsigned int, unsigned int> convolved_dims, unsigned int num_groups = 1)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000145{
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100146 ARM_COMPUTE_ERROR_ON(num_groups == 0);
147 ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.first * convolved_dims.second));
148 ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups);
149
Georgios Pinitas78c00902018-01-09 17:33:11 +0000150 TensorShape col2im_shape{ input.tensor_shape() };
151 col2im_shape.set(0, convolved_dims.first);
152 col2im_shape.set(1, convolved_dims.second);
Michele Di Giorgio980002b2018-08-08 09:25:51 +0100153 col2im_shape.set(2, input.tensor_shape()[0] * num_groups);
154
155 const unsigned int batch_idx = (num_groups == 1) ? 2 : 3;
156 col2im_shape.set(3, input.tensor_shape()[batch_idx]);
Georgios Pinitas78c00902018-01-09 17:33:11 +0000157
158 return col2im_shape;
159}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000160inline TensorShape compute_transposed_shape(const ITensorInfo &input)
161{
162 TensorShape shape_transposed{ input.tensor_shape() };
163
164 shape_transposed.set(0, input.dimension(1));
165 shape_transposed.set(1, input.dimension(0));
166
167 return shape_transposed;
168}
Giorgio Arena76572242018-04-04 17:44:26 +0100169inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier)
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000170{
171 const TensorShape input_shape{ input.tensor_shape() };
172 const TensorShape weights_shape{ weights.tensor_shape() };
173
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000174 const DataLayout data_layout = input.data_layout();
175 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
176 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Giorgio Arena76572242018-04-04 17:44:26 +0100177 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000178
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000179 unsigned int output_width = 0;
180 unsigned int output_height = 0;
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000181 std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx],
182 weights_shape[width_idx], weights_shape[height_idx],
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000183 conv_info);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000184
185 TensorShape output_shape{ input_shape };
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000186 output_shape.set(width_idx, output_width);
187 output_shape.set(height_idx, output_height);
Giorgio Arena76572242018-04-04 17:44:26 +0100188 output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000189
190 return output_shape;
191}
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000192inline TensorShape compute_deconvolution_shape(const ITensorInfo &input, unsigned int sx, unsigned int sy, unsigned int inner_border_right, unsigned int inner_border_top, const PadStrideInfo &info)
193{
194 TensorShape scale_out_shape(input.tensor_shape());
195 const unsigned int out_x = input.dimension(0) + (input.dimension(0) - 1) * (sx - 1) + inner_border_right + 2 * info.pad().first;
196 const unsigned int out_y = input.dimension(1) + (input.dimension(1) - 1) * (sy - 1) + inner_border_top + 2 * info.pad().second;
197 scale_out_shape.set(0, out_x);
198 scale_out_shape.set(1, out_y);
199
200 return scale_out_shape;
201}
Giorgio Arena0f170392018-07-18 16:13:12 +0100202inline 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,
203 unsigned int num_groups = 1)
Giorgio Arena156fcf32018-03-09 15:30:43 +0000204{
Giorgio Arena0f170392018-07-18 16:13:12 +0100205 // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true
206 // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false
207
208 ARM_COMPUTE_ERROR_ON(num_groups == 0);
209 ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW);
210 ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000211
212 TensorShape output_shape{ input->tensor_shape() };
213
214 const DataLayout data_layout = input->data_layout();
215 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
216 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
217 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
218
219 std::pair<unsigned int, unsigned int> out_dims = scaled_dimensions(output_shape[width_idx], output_shape[height_idx], kernel_dims.width, kernel_dims.height, conv_info, dilation);
Giorgio Arena0f170392018-07-18 16:13:12 +0100220 output_shape.set(0, (output_shape[channel_idx] / num_groups * kernel_dims.area() + (has_bias ? 1 : 0))); // NOLINT
Giorgio Arenaf485a102018-04-20 16:06:21 +0100221 output_shape.set(1, (out_dims.first * out_dims.second));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100222 if(batch_size_on_z && output_shape.num_dimensions() >= 3)
223 {
224 output_shape.remove_dimension(2);
225 }
226 else
227 {
Giorgio Arena0f170392018-07-18 16:13:12 +0100228 output_shape.set(2, num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100229 }
Giorgio Arena156fcf32018-03-09 15:30:43 +0000230
231 return output_shape;
232}
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100233inline TensorShape compute_flatten_shape(const ITensorInfo *input)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000234{
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100235 // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer.
236
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000237 TensorShape output_shape{ input->tensor_shape() };
238
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100239 output_shape.collapse(3);
Giorgio Arena156fcf32018-03-09 15:30:43 +0000240
241 return output_shape;
242}
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000243inline TensorShape compute_interleave_custom_shape(const TensorShape &input, const int x_interleave, const int y_interleave)
244{
245 TensorShape output_shape{ input };
246
247 output_shape.set(0, output_shape.x() * x_interleave);
248 output_shape.set(1, std::ceil(output_shape.y() / static_cast<float>(y_interleave)));
249
250 return output_shape;
251}
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000252inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave)
253{
254 TensorShape output_shape{ input->tensor_shape() };
255
256 // Transpose weights if the user hasn't done it
257 if(transpose_weights)
258 {
259 output_shape = compute_transposed_shape(*input);
260 }
261
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000262 // If we run multiple batches we need 1xW transpose, too.
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000263 if(is_batched_fc_layer)
264 {
265 output_shape = compute_transposed_shape(input->clone()->set_tensor_shape(output_shape));
266 output_shape = compute_interleave_custom_shape(output_shape, interleave, interleave);
267 }
268
269 return output_shape;
270}
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000271
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000272inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000273{
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000274 TensorShape tensor_shape{ input.tensor_shape() };
275
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000276 const Size2D kernel_size = winograd_info.kernel_size;
277 const Size2D output_tile_size = winograd_info.output_tile_size;
278 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
Giorgio Arena2d9de0a2018-03-15 17:58:20 +0000279
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000280 tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH));
281 tensor_shape.set(Window::DimX, input.dimension(3));
282 tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)));
283 tensor_shape.set(Window::DimZ, input_tile_size.area());
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000284
285 return tensor_shape;
286}
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000287inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000288{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000289 const PadStrideInfo conv_info = winograd_info.convolution_info;
290 const Size2D kernel_size = winograd_info.kernel_size;
291 const Size2D output_tile_size = winograd_info.output_tile_size;
292 const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1);
293
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100294 const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
295 const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
296 const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000297
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100298 // Compute the number of output tiles along the x and y direction of size "output_tile_size"
299 const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]),
300 kernel_size,
301 output_tile_size,
302 conv_info);
Giorgio Arenac42f28d2018-04-26 11:33:05 +0100303
304 const unsigned int width = input.tensor_shape()[idx_c];
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100305 const unsigned int height = num_tiles.area();
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000306 const unsigned int depth = input_tile_size.area();
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000307
308 TensorShape output_shape{ input.tensor_shape() };
309 output_shape.set(0, width);
310 output_shape.set(1, height);
311 output_shape.set(2, depth);
312
313 return output_shape;
314}
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000315inline TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000316{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000317 const PadStrideInfo conv_info = winograd_info.convolution_info;
318 const Size2D kernel_size = winograd_info.kernel_size;
319 const Size2D input_dimensions = winograd_info.input_dimensions;
320 const DataLayout data_layout = winograd_info.output_data_layout;
321
322 // Compute output shape
323 unsigned int output_width = 0;
324 unsigned int output_height = 0;
325 std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height,
326 kernel_size.width, kernel_size.height, conv_info);
327
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000328 TensorShape tensor_shape{ input.tensor_shape() };
329
330 // Output dimension
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000331 const unsigned int out_w = output_width;
332 const unsigned int out_h = output_height;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000333 const unsigned int out_c = input.dimension(0);
334
335 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w);
336 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h);
337 tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c);
338
339 return tensor_shape;
340}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000341inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
342{
343 const TensorShape input_shape{ input.tensor_shape() };
344 const TensorShape weights_shape{ weights.tensor_shape() };
345
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000346 const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
347 const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
348 const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL);
349
Giorgio Arenac0f54432018-03-16 14:02:34 +0000350 const unsigned int input_width = input_shape[idx_width];
351 const unsigned int input_height = input_shape[idx_height];
352 const unsigned int weights_width = weights_shape[idx_width];
353 const unsigned int weights_height = weights_shape[idx_height];
354 const unsigned int weights_out_channel = weights_shape[3];
355 unsigned int output_width = 0;
356 unsigned int output_height = 0;
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000357 std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, weights_width, weights_height, conv_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000358
359 TensorShape output_shape{ input_shape };
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000360 output_shape.set(idx_width, output_width);
361 output_shape.set(idx_height, output_height);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000362 output_shape.set(idx_channel, weights_out_channel);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000363
364 return output_shape;
365}
Alex Gilday60954c62018-03-05 16:22:48 +0000366inline TensorShape compute_min_max_shape(const ITensorInfo *input)
367{
368 TensorShape output_shape{ input->tensor_shape() };
369 output_shape.set(Window::DimX, 2);
370 output_shape.remove_dimension(1);
371 output_shape.remove_dimension(1);
372
373 return output_shape;
374}
375
Michalis Spyroue74b2012018-04-18 09:49:16 +0100376inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info)
377{
378 unsigned int pooled_w = 0;
379 unsigned int pooled_h = 0;
380
Giorgio Arena3c520c52018-05-01 11:47:24 +0100381 TensorShape output_shape{ input.tensor_shape() };
Michalis Spyroue74b2012018-04-18 09:49:16 +0100382
Giorgio Arena3c520c52018-05-01 11:47:24 +0100383 const bool is_global_pooling = pool_info.is_global_pooling();
384 const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH);
385 const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT);
386 const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width;
387 const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height;
388
389 std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width],
390 output_shape[idx_height],
Michalis Spyroue74b2012018-04-18 09:49:16 +0100391 pool_size_x,
392 pool_size_y,
393 pool_info.pad_stride_info());
394
Giorgio Arena3c520c52018-05-01 11:47:24 +0100395 output_shape.set(idx_width, pooled_w);
396 output_shape.set(idx_height, pooled_h);
Michalis Spyroue74b2012018-04-18 09:49:16 +0100397
398 return output_shape;
399}
400
Michalis Spyrou36a559e2018-03-20 10:30:58 +0000401inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size)
402{
403 TensorShape output_shape{ input->tensor_shape() };
404 output_shape.set(1, batch_size);
405
406 return output_shape;
407}
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100408inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info)
409{
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000410 ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4");
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100411 ARM_COMPUTE_ERROR_ON_MSG(is_interleaved_transposed && reshape_info.reinterpret_input_as_3d(), "The first input tensor cannot be reinterpreted as 3D if is_interleaved_transposed is true");
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100412
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100413 const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d();
414 const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 1;
415 const int m = reshape_info.reinterpret_input_as_3d() ? input0.dimension(1) * input0.dimension(2) : input0.dimension(1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000416
417 // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third
418 // dimension of the output tensor
419 const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100420 const int dim1 = is_interleaved_transposed ? reshape_info.m() / reshape_info.depth_output_gemm3d() : m / reshape_info.depth_output_gemm3d();
421 const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2];
422 const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3];
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000423
424 TensorShape output_shape{ input0.tensor_shape() };
425
426 output_shape.set(0, dim0);
427 output_shape.set(1, dim1);
Gian Marco Iodice68a3f562018-07-26 11:44:03 +0100428 output_shape.set(2, reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : dim2);
429 output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3);
430 output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1);
Isabella Gottardi8e74f442018-03-01 16:42:00 +0000431
432 return output_shape;
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100433}
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100434
435template <typename T>
Georgios Pinitase2220552018-07-20 13:23:44 +0100436inline TensorShape extract_shape(T *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100437{
Georgios Pinitase2220552018-07-20 13:23:44 +0100438 return data->info()->tensor_shape();
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100439}
440
Georgios Pinitase2220552018-07-20 13:23:44 +0100441inline TensorShape extract_shape(ITensorInfo *data)
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100442{
Georgios Pinitase2220552018-07-20 13:23:44 +0100443 return data->tensor_shape();
444}
445
446inline TensorShape extract_shape(const TensorShape *data)
447{
448 return *data;
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100449}
450
451template <typename T>
Georgios Pinitase29acf12018-07-16 14:40:09 +0100452inline TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector)
453{
Georgios Pinitase2220552018-07-20 13:23:44 +0100454 TensorShape out_shape = extract_shape(inputs_vector[0]);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100455
456 size_t max_x = 0;
457 size_t max_y = 0;
458 size_t depth = 0;
459
460 for(const auto &tensor : inputs_vector)
461 {
462 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Georgios Pinitase2220552018-07-20 13:23:44 +0100463 const TensorShape shape = extract_shape(tensor);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100464 max_x = std::max(shape.x(), max_x);
465 max_y = std::max(shape.y(), max_y);
466 depth += shape.z();
467 }
468
469 out_shape.set(0, max_x);
470 out_shape.set(1, max_y);
471 out_shape.set(2, depth);
472
473 return out_shape;
474}
475
476template <typename T>
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100477inline TensorShape calculate_width_concatenate_shape(const std::vector<T *> &inputs_vector)
478{
Georgios Pinitase2220552018-07-20 13:23:44 +0100479 TensorShape out_shape = extract_shape(inputs_vector[0]);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100480
481 size_t width = 0;
482 for(const auto &tensor : inputs_vector)
483 {
484 ARM_COMPUTE_ERROR_ON(tensor == nullptr);
Georgios Pinitase2220552018-07-20 13:23:44 +0100485 const TensorShape shape = extract_shape(tensor);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100486 width += shape.x();
487 }
488
489 out_shape.set(0, width);
490
491 return out_shape;
492}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000493} // namespace shape_calculator
494} // namespace misc
495} // namespace arm_compute
496#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */