Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 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 | |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensorInfo.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 30 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 31 | #include <cmath> |
| 32 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace misc |
| 36 | { |
| 37 | namespace shape_calculator |
| 38 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 39 | inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm) |
| 40 | { |
| 41 | TensorShape output_shape = input.tensor_shape(); |
| 42 | permute(output_shape, perm); |
| 43 | return output_shape; |
| 44 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 45 | inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false) |
| 46 | { |
| 47 | // Calculate output shape |
| 48 | TensorShape weights_reshaped{ weights.tensor_shape() }; |
| 49 | weights_reshaped.collapse(3); |
| 50 | const size_t tmp_dim = weights_reshaped[0]; |
| 51 | weights_reshaped.set(0, weights_reshaped[1]); |
| 52 | weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0)); |
| 53 | |
| 54 | return weights_reshaped; |
| 55 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 56 | inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 57 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 58 | // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height |
| 59 | ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1); |
| 60 | const int interleave_width = 4 * mult_interleave4x4_height; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 61 | TensorShape shape_interleaved_a{ a.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 62 | shape_interleaved_a.set(0, a.dimension(0) * interleave_width); |
| 63 | shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width))); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 64 | |
| 65 | return shape_interleaved_a; |
| 66 | } |
| 67 | inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b) |
| 68 | { |
| 69 | // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ] |
| 70 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
| 71 | shape_transposed1xW_b.set(0, b.dimension(1) * 16); |
| 72 | shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f)); |
| 73 | |
| 74 | return shape_transposed1xW_b; |
| 75 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 76 | inline TensorShape compute_transpose1xW_with_element_size_shape(const ITensorInfo &b, int mult_transpose1xW_width = 1) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 77 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 78 | // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row |
| 79 | // The transpose1xW output matrix will have the following shape: |
| 80 | // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width |
| 81 | ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 82 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 83 | const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 84 | shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width); |
| 85 | shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width)))); |
| 86 | |
| 87 | return shape_transposed1xW_b; |
| 88 | } |
| 89 | inline TensorShape compute_reductionA_shape(const ITensorInfo &b) |
| 90 | { |
| 91 | TensorShape shape_vector_sum_col{ b.tensor_shape() }; |
| 92 | if(shape_vector_sum_col.num_dimensions() > 1) |
| 93 | { |
| 94 | shape_vector_sum_col.remove_dimension(1); |
| 95 | } |
| 96 | |
| 97 | return shape_vector_sum_col; |
| 98 | } |
| 99 | inline TensorShape compute_reductionB_shape(const ITensorInfo &a) |
| 100 | { |
| 101 | TensorShape shape_vector_sum_row{ a.tensor_shape() }; |
| 102 | shape_vector_sum_row.set(Window::DimX, a.dimension(1)); |
| 103 | if(a.num_dimensions() > 1) |
| 104 | { |
| 105 | shape_vector_sum_row.remove_dimension(1); |
| 106 | } |
| 107 | |
| 108 | return shape_vector_sum_row; |
| 109 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 110 | inline TensorShape compute_col2im_shape(const ITensorInfo &input, std::pair<unsigned int, unsigned int> convolved_dims) |
| 111 | { |
| 112 | TensorShape col2im_shape{ input.tensor_shape() }; |
Georgios Pinitas | 19ea419 | 2018-06-19 13:09:53 +0100 | [diff] [blame] | 113 | col2im_shape.shift_right(1); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 114 | col2im_shape.set(0, convolved_dims.first); |
| 115 | col2im_shape.set(1, convolved_dims.second); |
| 116 | col2im_shape.set(2, input.tensor_shape()[0]); |
| 117 | |
| 118 | return col2im_shape; |
| 119 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 120 | inline TensorShape compute_transposed_shape(const ITensorInfo &input) |
| 121 | { |
| 122 | TensorShape shape_transposed{ input.tensor_shape() }; |
| 123 | |
| 124 | shape_transposed.set(0, input.dimension(1)); |
| 125 | shape_transposed.set(1, input.dimension(0)); |
| 126 | |
| 127 | return shape_transposed; |
| 128 | } |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 129 | inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier) |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 130 | { |
| 131 | const TensorShape input_shape{ input.tensor_shape() }; |
| 132 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 133 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 134 | const DataLayout data_layout = input.data_layout(); |
| 135 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 136 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 137 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 138 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 139 | unsigned int output_width = 0; |
| 140 | unsigned int output_height = 0; |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 141 | std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx], |
| 142 | weights_shape[width_idx], weights_shape[height_idx], |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 143 | conv_info); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 144 | |
| 145 | TensorShape output_shape{ input_shape }; |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 146 | output_shape.set(width_idx, output_width); |
| 147 | output_shape.set(height_idx, output_height); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 148 | output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 149 | |
| 150 | return output_shape; |
| 151 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 152 | inline 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) |
| 153 | { |
| 154 | TensorShape scale_out_shape(input.tensor_shape()); |
| 155 | const unsigned int out_x = input.dimension(0) + (input.dimension(0) - 1) * (sx - 1) + inner_border_right + 2 * info.pad().first; |
| 156 | const unsigned int out_y = input.dimension(1) + (input.dimension(1) - 1) * (sy - 1) + inner_border_top + 2 * info.pad().second; |
| 157 | scale_out_shape.set(0, out_x); |
| 158 | scale_out_shape.set(1, out_y); |
| 159 | |
| 160 | return scale_out_shape; |
| 161 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 162 | inline TensorShape compute_im2col_conv_shape(const ITensorInfo *input, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation) |
| 163 | { |
| 164 | // The output shape will be the 2D shape used as input for GEMM [ out_channels * kernel_area, num_elems_per_out_channel ] |
| 165 | |
| 166 | TensorShape output_shape{ input->tensor_shape() }; |
| 167 | |
| 168 | const DataLayout data_layout = input->data_layout(); |
| 169 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 170 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 171 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 172 | |
| 173 | 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 Arena | f485a10 | 2018-04-20 16:06:21 +0100 | [diff] [blame] | 174 | output_shape.set(0, (output_shape[channel_idx] * kernel_dims.area() + (has_bias ? 1 : 0))); |
| 175 | output_shape.set(1, (out_dims.first * out_dims.second)); |
| 176 | output_shape.set(2, 1); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 177 | |
| 178 | return output_shape; |
| 179 | } |
| 180 | inline TensorShape compute_im2col_fc_shape(const ITensorInfo *input, const int num_input_dimensions = 3) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 181 | { |
| 182 | TensorShape output_shape{ input->tensor_shape() }; |
| 183 | |
| 184 | output_shape.collapse(num_input_dimensions); |
| 185 | |
| 186 | return output_shape; |
| 187 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 188 | inline TensorShape compute_im2col_flatten_shape(const ITensorInfo *input) |
| 189 | { |
| 190 | // The output shape will be the flatten version of the input (i.e. [ width * height * channels, 1, 1, ... ] ). Used for FlattenLayer. |
| 191 | |
| 192 | ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 3); |
| 193 | |
| 194 | TensorShape output_shape{ input->tensor_shape() }; |
| 195 | |
| 196 | const size_t flatten_shape = input->dimension(0) * input->dimension(1) * input->dimension(2); |
| 197 | output_shape.set(0, flatten_shape); |
| 198 | output_shape.remove_dimension(1); |
| 199 | output_shape.remove_dimension(1); |
| 200 | |
| 201 | return output_shape; |
| 202 | } |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 203 | inline TensorShape compute_interleave_custom_shape(const TensorShape &input, const int x_interleave, const int y_interleave) |
| 204 | { |
| 205 | TensorShape output_shape{ input }; |
| 206 | |
| 207 | output_shape.set(0, output_shape.x() * x_interleave); |
| 208 | output_shape.set(1, std::ceil(output_shape.y() / static_cast<float>(y_interleave))); |
| 209 | |
| 210 | return output_shape; |
| 211 | } |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 212 | inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave) |
| 213 | { |
| 214 | TensorShape output_shape{ input->tensor_shape() }; |
| 215 | |
| 216 | // Transpose weights if the user hasn't done it |
| 217 | if(transpose_weights) |
| 218 | { |
| 219 | output_shape = compute_transposed_shape(*input); |
| 220 | } |
| 221 | |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 222 | // If we run multiple batches we need 1xW transpose, too. |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 223 | if(is_batched_fc_layer) |
| 224 | { |
| 225 | output_shape = compute_transposed_shape(input->clone()->set_tensor_shape(output_shape)); |
| 226 | output_shape = compute_interleave_custom_shape(output_shape, interleave, interleave); |
| 227 | } |
| 228 | |
| 229 | return output_shape; |
| 230 | } |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 231 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 232 | inline TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info) |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 233 | { |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 234 | TensorShape tensor_shape{ input.tensor_shape() }; |
| 235 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 236 | const Size2D kernel_size = winograd_info.kernel_size; |
| 237 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 238 | const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1); |
Giorgio Arena | 2d9de0a | 2018-03-15 17:58:20 +0000 | [diff] [blame] | 239 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 240 | tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH)); |
| 241 | tensor_shape.set(Window::DimX, input.dimension(3)); |
| 242 | tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL))); |
| 243 | tensor_shape.set(Window::DimZ, input_tile_size.area()); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 244 | |
| 245 | return tensor_shape; |
| 246 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 247 | inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 248 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 249 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 250 | const Size2D kernel_size = winograd_info.kernel_size; |
| 251 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 252 | const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1); |
| 253 | |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 254 | const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 255 | const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 256 | const size_t idx_c = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 257 | |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 258 | // Compute height |
| 259 | const unsigned int num_tiles_x = std::ceil((input.tensor_shape()[idx_w] - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / static_cast<float>(output_tile_size.width)); |
| 260 | const unsigned int num_tiles_y = std::ceil((input.tensor_shape()[idx_h] - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / static_cast<float>(output_tile_size.height)); |
| 261 | |
| 262 | const unsigned int width = input.tensor_shape()[idx_c]; |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 263 | const unsigned int height = num_tiles_x * num_tiles_y; |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 264 | const unsigned int depth = input_tile_size.area(); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 265 | |
| 266 | TensorShape output_shape{ input.tensor_shape() }; |
| 267 | output_shape.set(0, width); |
| 268 | output_shape.set(1, height); |
| 269 | output_shape.set(2, depth); |
| 270 | |
| 271 | return output_shape; |
| 272 | } |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 273 | inline TensorShape compute_winograd_output_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 274 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 275 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 276 | const Size2D kernel_size = winograd_info.kernel_size; |
| 277 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 278 | const DataLayout data_layout = winograd_info.output_data_layout; |
| 279 | |
| 280 | // Compute output shape |
| 281 | unsigned int output_width = 0; |
| 282 | unsigned int output_height = 0; |
| 283 | std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height, |
| 284 | kernel_size.width, kernel_size.height, conv_info); |
| 285 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 286 | TensorShape tensor_shape{ input.tensor_shape() }; |
| 287 | |
| 288 | // Output dimension |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 289 | const unsigned int out_w = output_width; |
| 290 | const unsigned int out_h = output_height; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 291 | const unsigned int out_c = input.dimension(0); |
| 292 | |
| 293 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w); |
| 294 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h); |
| 295 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c); |
| 296 | |
| 297 | return tensor_shape; |
| 298 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 299 | inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info) |
| 300 | { |
| 301 | const TensorShape input_shape{ input.tensor_shape() }; |
| 302 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 303 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 304 | const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 305 | const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 306 | const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL); |
| 307 | |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 308 | const unsigned int input_width = input_shape[idx_width]; |
| 309 | const unsigned int input_height = input_shape[idx_height]; |
| 310 | const unsigned int weights_width = weights_shape[idx_width]; |
| 311 | const unsigned int weights_height = weights_shape[idx_height]; |
| 312 | const unsigned int weights_out_channel = weights_shape[3]; |
| 313 | unsigned int output_width = 0; |
| 314 | unsigned int output_height = 0; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 315 | std::tie(output_width, output_height) = scaled_dimensions(input_width, input_height, weights_width, weights_height, conv_info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 316 | |
| 317 | TensorShape output_shape{ input_shape }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 318 | output_shape.set(idx_width, output_width); |
| 319 | output_shape.set(idx_height, output_height); |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 320 | output_shape.set(idx_channel, weights_out_channel); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 321 | |
| 322 | return output_shape; |
| 323 | } |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 324 | inline TensorShape compute_min_max_shape(const ITensorInfo *input) |
| 325 | { |
| 326 | TensorShape output_shape{ input->tensor_shape() }; |
| 327 | output_shape.set(Window::DimX, 2); |
| 328 | output_shape.remove_dimension(1); |
| 329 | output_shape.remove_dimension(1); |
| 330 | |
| 331 | return output_shape; |
| 332 | } |
| 333 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 334 | inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info) |
| 335 | { |
| 336 | unsigned int pooled_w = 0; |
| 337 | unsigned int pooled_h = 0; |
| 338 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 339 | TensorShape output_shape{ input.tensor_shape() }; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 340 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 341 | const bool is_global_pooling = pool_info.is_global_pooling(); |
| 342 | const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 343 | const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 344 | const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width; |
| 345 | const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height; |
| 346 | |
| 347 | std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width], |
| 348 | output_shape[idx_height], |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 349 | pool_size_x, |
| 350 | pool_size_y, |
| 351 | pool_info.pad_stride_info()); |
| 352 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 353 | output_shape.set(idx_width, pooled_w); |
| 354 | output_shape.set(idx_height, pooled_h); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 355 | |
| 356 | return output_shape; |
| 357 | } |
| 358 | |
Michalis Spyrou | 36a559e | 2018-03-20 10:30:58 +0000 | [diff] [blame] | 359 | inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size) |
| 360 | { |
| 361 | TensorShape output_shape{ input->tensor_shape() }; |
| 362 | output_shape.set(1, batch_size); |
| 363 | |
| 364 | return output_shape; |
| 365 | } |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 366 | inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info) |
| 367 | { |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 368 | ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4"); |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 369 | |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 370 | const bool is_gemm3d = reshape_info.depth_output_gemm3d() != 1; |
| 371 | |
| 372 | // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third |
| 373 | // dimension of the output tensor |
| 374 | const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0); |
| 375 | const int dim1 = is_interleaved_transposed ? reshape_info.m() / reshape_info.depth_output_gemm3d() : input0.dimension(1) / reshape_info.depth_output_gemm3d(); |
| 376 | const int dim2 = input0.tensor_shape()[2]; |
| 377 | const int dim3 = input0.tensor_shape()[3]; |
| 378 | |
| 379 | TensorShape output_shape{ input0.tensor_shape() }; |
| 380 | |
| 381 | output_shape.set(0, dim0); |
| 382 | output_shape.set(1, dim1); |
| 383 | output_shape.set(2, is_gemm3d ? reshape_info.depth_output_gemm3d() : dim2); |
| 384 | output_shape.set(3, is_gemm3d ? dim2 : dim3); |
| 385 | output_shape.set(4, is_gemm3d ? dim3 : 1); |
| 386 | |
| 387 | return output_shape; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 388 | } |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 389 | |
| 390 | template <typename T> |
| 391 | inline TensorShape get_shape_from_info(T *info) |
| 392 | { |
| 393 | return info->info()->tensor_shape(); |
| 394 | } |
| 395 | |
| 396 | inline TensorShape get_shape_from_info(ITensorInfo *info) |
| 397 | { |
| 398 | return info->tensor_shape(); |
| 399 | } |
| 400 | |
| 401 | template <typename T> |
| 402 | inline TensorShape calculate_width_concatenate_shape(const std::vector<T *> &inputs_vector) |
| 403 | { |
| 404 | TensorShape out_shape = get_shape_from_info(inputs_vector[0]); |
| 405 | |
| 406 | size_t width = 0; |
| 407 | for(const auto &tensor : inputs_vector) |
| 408 | { |
| 409 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
| 410 | const TensorShape shape = get_shape_from_info(tensor); |
| 411 | width += shape.x(); |
| 412 | } |
| 413 | |
| 414 | out_shape.set(0, width); |
| 415 | |
| 416 | return out_shape; |
| 417 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 418 | } // namespace shape_calculator |
| 419 | } // namespace misc |
| 420 | } // namespace arm_compute |
| 421 | #endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */ |