blob: 9cb8023463ac001d4e77bb9ef70fac1817cab485 [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
31namespace arm_compute
32{
33namespace misc
34{
35namespace shape_calculator
36{
Pablo Tello00afd112018-01-04 10:34:24 +000037inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
38{
39 TensorShape output_shape = input.tensor_shape();
40 permute(output_shape, perm);
41 return output_shape;
42}
Georgios Pinitas78c00902018-01-09 17:33:11 +000043inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false)
44{
45 // Calculate output shape
46 TensorShape weights_reshaped{ weights.tensor_shape() };
47 weights_reshaped.collapse(3);
48 const size_t tmp_dim = weights_reshaped[0];
49 weights_reshaped.set(0, weights_reshaped[1]);
50 weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0));
51
52 return weights_reshaped;
53}
Gian Marco36a0a462018-01-12 10:21:40 +000054inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +000055{
Gian Marco36a0a462018-01-12 10:21:40 +000056 // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height
57 ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1);
58 const int interleave_width = 4 * mult_interleave4x4_height;
Georgios Pinitas358ca202017-12-07 16:47:52 +000059 TensorShape shape_interleaved_a{ a.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +000060 shape_interleaved_a.set(0, a.dimension(0) * interleave_width);
61 shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width)));
Georgios Pinitas358ca202017-12-07 16:47:52 +000062
63 return shape_interleaved_a;
64}
65inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b)
66{
67 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
68 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
69 shape_transposed1xW_b.set(0, b.dimension(1) * 16);
70 shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f));
71
72 return shape_transposed1xW_b;
73}
Gian Marco36a0a462018-01-12 10:21:40 +000074inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1)
Georgios Pinitas358ca202017-12-07 16:47:52 +000075{
Gian Marco36a0a462018-01-12 10:21:40 +000076 // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row
77 // The transpose1xW output matrix will have the following shape:
78 // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width
79 ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1);
Georgios Pinitas358ca202017-12-07 16:47:52 +000080 TensorShape shape_transposed1xW_b{ b.tensor_shape() };
Gian Marco36a0a462018-01-12 10:21:40 +000081 const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +000082 shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width);
83 shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width))));
84
85 return shape_transposed1xW_b;
86}
87inline TensorShape compute_reductionA_shape(const ITensorInfo &b)
88{
89 TensorShape shape_vector_sum_col{ b.tensor_shape() };
90 if(shape_vector_sum_col.num_dimensions() > 1)
91 {
92 shape_vector_sum_col.remove_dimension(1);
93 }
94
95 return shape_vector_sum_col;
96}
97inline TensorShape compute_reductionB_shape(const ITensorInfo &a)
98{
99 TensorShape shape_vector_sum_row{ a.tensor_shape() };
100 shape_vector_sum_row.set(Window::DimX, a.dimension(1));
101 if(a.num_dimensions() > 1)
102 {
103 shape_vector_sum_row.remove_dimension(1);
104 }
105
106 return shape_vector_sum_row;
107}
108inline TensorShape compute_im2col_shape(const ITensorInfo &input)
109{
110 TensorShape shape_im2col{ input.tensor_shape() };
111 shape_im2col.collapse(3);
112
113 return shape_im2col;
114}
Georgios Pinitas78c00902018-01-09 17:33:11 +0000115inline TensorShape compute_col2im_shape(const ITensorInfo &input, std::pair<unsigned int, unsigned int> convolved_dims)
116{
117 TensorShape col2im_shape{ input.tensor_shape() };
118 col2im_shape.set(0, convolved_dims.first);
119 col2im_shape.set(1, convolved_dims.second);
120 col2im_shape.set(2, input.tensor_shape()[0]);
121
122 return col2im_shape;
123}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000124inline TensorShape compute_transposed_shape(const ITensorInfo &input)
125{
126 TensorShape shape_transposed{ input.tensor_shape() };
127
128 shape_transposed.set(0, input.dimension(1));
129 shape_transposed.set(1, input.dimension(0));
130
131 return shape_transposed;
132}
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000133inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
134{
135 const TensorShape input_shape{ input.tensor_shape() };
136 const TensorShape weights_shape{ weights.tensor_shape() };
137
138 unsigned int output_width = 0;
139 unsigned int output_height = 0;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000140 std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(),
141 weights_shape.x(), weights_shape.y(),
142 conv_info);
Georgios Pinitas1250a5a2018-01-02 13:27:37 +0000143
144 TensorShape output_shape{ input_shape };
145 output_shape.set(0, output_width);
146 output_shape.set(1, output_height);
147
148 return output_shape;
149}
Michalis Spyrou780db4e2017-11-23 09:49:51 +0000150inline 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)
151{
152 TensorShape scale_out_shape(input.tensor_shape());
153 const unsigned int out_x = input.dimension(0) + (input.dimension(0) - 1) * (sx - 1) + inner_border_right + 2 * info.pad().first;
154 const unsigned int out_y = input.dimension(1) + (input.dimension(1) - 1) * (sy - 1) + inner_border_top + 2 * info.pad().second;
155 scale_out_shape.set(0, out_x);
156 scale_out_shape.set(1, out_y);
157
158 return scale_out_shape;
159}
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000160inline TensorShape compute_im2col_shape(const ITensorInfo *input, const int num_input_dimensions = 3)
161{
162 TensorShape output_shape{ input->tensor_shape() };
163
164 output_shape.collapse(num_input_dimensions);
165
166 return output_shape;
167}
168inline TensorShape compute_interleave_custom_shape(const TensorShape &input, const int x_interleave, const int y_interleave)
169{
170 TensorShape output_shape{ input };
171
172 output_shape.set(0, output_shape.x() * x_interleave);
173 output_shape.set(1, std::ceil(output_shape.y() / static_cast<float>(y_interleave)));
174
175 return output_shape;
176}
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000177inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave)
178{
179 TensorShape output_shape{ input->tensor_shape() };
180
181 // Transpose weights if the user hasn't done it
182 if(transpose_weights)
183 {
184 output_shape = compute_transposed_shape(*input);
185 }
186
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000187 // If we run multiple batches we need 1xW transpose, too.
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000188 if(is_batched_fc_layer)
189 {
190 output_shape = compute_transposed_shape(input->clone()->set_tensor_shape(output_shape));
191 output_shape = compute_interleave_custom_shape(output_shape, interleave, interleave);
192 }
193
194 return output_shape;
195}
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000196
197inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input)
198{
199 // COMPMID-984 (giaiod01)
200 TensorShape tensor_shape{ input.tensor_shape() };
201
202 if(input.data_layout() == DataLayout::NCHW)
203 {
204 tensor_shape.remove_dimension(0);
205 tensor_shape.set(Window::DimX, input.dimension(3));
206 tensor_shape.set(Window::DimY, input.dimension(2));
207 tensor_shape.set(Window::DimZ, 16);
208 }
209 else
210 {
211 tensor_shape.remove_dimension(1);
212 tensor_shape.set(Window::DimY, input.dimension(2));
213 tensor_shape.set(Window::DimZ, 16);
214 }
215
216 return tensor_shape;
217}
218
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000219inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const PadStrideInfo &conv_info, const Size2D &kernel_size)
220{
221 // Compute height
222 const unsigned int num_tiles_x = std::ceil((input.tensor_shape().x() - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f);
223 const unsigned int num_tiles_y = std::ceil((input.tensor_shape().y() - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f);
224
225 const unsigned int width = input.tensor_shape()[get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)];
226 const unsigned int height = num_tiles_x * num_tiles_y;
227 const unsigned int depth = 16; // COMPMID-990
228
229 TensorShape output_shape{ input.tensor_shape() };
230 output_shape.set(0, width);
231 output_shape.set(1, height);
232 output_shape.set(2, depth);
233
234 return output_shape;
235}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000236inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info)
237{
238 const TensorShape input_shape{ input.tensor_shape() };
239 const TensorShape weights_shape{ weights.tensor_shape() };
240
241 unsigned int output_width = 0;
242 unsigned int output_height = 0;
243 std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info);
244
245 TensorShape output_shape{ input_shape };
246 output_shape.set(0, output_width);
247 output_shape.set(1, output_height);
248 output_shape.set(2, weights_shape[3]);
249
250 return output_shape;
251}
Georgios Pinitas358ca202017-12-07 16:47:52 +0000252} // namespace shape_calculator
253} // namespace misc
254} // namespace arm_compute
255#endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */