Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 32 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 33 | #include <cmath> |
| 34 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace misc |
| 38 | { |
| 39 | namespace shape_calculator |
| 40 | { |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 41 | /** Calculate the output tensor shape of a vector input given the convolution dimensions |
| 42 | * |
| 43 | * @param[in] input Input tensor shape |
| 44 | * @param[in] conv_w Convolution width |
| 45 | * @param[in] conv_h Convolution height |
| 46 | * @param[in] data_layout Data layout |
| 47 | * |
| 48 | * @return the calculated shape |
| 49 | */ |
Abe Mbise | 7784c83 | 2018-05-31 16:48:41 +0100 | [diff] [blame] | 50 | inline TensorShape compute_vector_to_tensor_output_shape(const TensorShape &input, size_t conv_w, size_t conv_h, const DataLayout &data_layout) |
| 51 | { |
| 52 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 53 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 54 | const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 55 | |
| 56 | TensorShape output_shape(input); |
| 57 | output_shape.set(idx_w, conv_w); |
| 58 | output_shape.set(idx_h, conv_h); |
| 59 | output_shape.set(idx_c, input.x() / (conv_w * conv_h)); |
| 60 | |
| 61 | return output_shape; |
| 62 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 63 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 64 | /** Calculate the permuted shape of an input given a permutation vector |
| 65 | * |
| 66 | * @param[in] input Input tensor info |
| 67 | * @param[in] perm Permutation vector |
| 68 | * |
| 69 | * @return the calculated shape |
| 70 | */ |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 71 | inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm) |
| 72 | { |
| 73 | TensorShape output_shape = input.tensor_shape(); |
| 74 | permute(output_shape, perm); |
| 75 | return output_shape; |
| 76 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 77 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 78 | /** Calculate the output shape of the reorg layer given a stride |
| 79 | * |
| 80 | * @param[in] input Input tensor info |
| 81 | * @param[in] stride Stride |
| 82 | * |
| 83 | * @return the calculated shape |
| 84 | */ |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 85 | inline TensorShape compute_reorg_output_shape(const ITensorInfo &input, int32_t stride) |
| 86 | { |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 87 | const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 88 | const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 89 | const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 90 | |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 91 | ARM_COMPUTE_ERROR_ON(stride <= 0); |
| 92 | ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_width] % stride != 0), "The width of the input tensor must be a multiple of stride"); |
| 93 | ARM_COMPUTE_ERROR_ON_MSG((input.tensor_shape()[idx_height] % stride != 0), "The height of the input tensor must be a multiple of stride"); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 94 | |
| 95 | TensorShape output_shape{ input.tensor_shape() }; |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 96 | |
| 97 | output_shape.set(idx_width, output_shape[idx_width] / stride); |
| 98 | output_shape.set(idx_height, output_shape[idx_height] / stride); |
| 99 | output_shape.set(idx_channel, output_shape[idx_channel] * stride * stride); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 100 | |
| 101 | return output_shape; |
| 102 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 103 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 104 | /** Calculate the reshaped shape of the weights |
| 105 | * |
| 106 | * @param[in] weights Weights tensor info |
| 107 | * @param[in] has_bias (Optional) Set to true if there is bias |
| 108 | * @param[in] num_groups (Optional) Number of groups |
| 109 | * |
| 110 | * @return the calculated shape of the reshaped weights |
| 111 | */ |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 112 | inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false, unsigned int num_groups = 1) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 113 | { |
Giorgio Arena | 088c2b0 | 2018-08-07 16:59:05 +0100 | [diff] [blame] | 114 | // 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 Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_ON(num_groups == 0); |
Giorgio Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_ERROR_ON(weights.data_layout() == DataLayout::NHWC && num_groups > 1); |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON((weights.dimension(3) % num_groups) != 0); |
Giorgio Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 118 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 119 | // Calculate output shape |
| 120 | TensorShape weights_reshaped{ weights.tensor_shape() }; |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 121 | weights_reshaped.set(3, weights_reshaped[3] / num_groups); |
| 122 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 123 | weights_reshaped.collapse(3); |
| 124 | const size_t tmp_dim = weights_reshaped[0]; |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 125 | weights_reshaped.set(0, weights_reshaped[1]); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 126 | weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0)); |
Giorgio Arena | c6aa49b | 2018-08-07 11:53:30 +0100 | [diff] [blame] | 127 | if(weights.num_dimensions() < 5) |
| 128 | { |
| 129 | weights_reshaped.set(2, num_groups); |
| 130 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 131 | |
| 132 | return weights_reshaped; |
| 133 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 134 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 135 | /** Calculate the Left Hand Side matrix reshaped shape |
| 136 | * |
| 137 | * @param[in] a Input tensor info |
| 138 | * @param[in] lhs_info Left Hand Side matrix information |
| 139 | * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d |
| 140 | * |
| 141 | * @return the calculated shape |
| 142 | */ |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 143 | inline TensorShape compute_lhs_reshaped_shape(const ITensorInfo &a, const GEMMLHSMatrixInfo &lhs_info, bool reinterpret_input_as_3d = false) |
| 144 | { |
| 145 | ARM_COMPUTE_ERROR_ON(lhs_info.m0 == 0); |
| 146 | ARM_COMPUTE_ERROR_ON(lhs_info.k0 == 0); |
| 147 | ARM_COMPUTE_ERROR_ON(lhs_info.v0 == 0); |
| 148 | |
| 149 | // Input width/height |
| 150 | const unsigned int input_width = a.dimension(0); |
| 151 | const unsigned int input_height = reinterpret_input_as_3d ? a.dimension(1) * a.dimension(2) : a.dimension(1); |
| 152 | |
| 153 | // Number of horizontal/vertical blocks in the input tensor |
| 154 | const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(lhs_info.k0)); |
| 155 | const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(lhs_info.m0)); |
| 156 | |
| 157 | // Block size |
| 158 | const unsigned int block_size = lhs_info.m0 * lhs_info.k0; |
| 159 | |
| 160 | // Output width/height |
| 161 | const unsigned int output_width = block_size * num_horiz_blocks * lhs_info.v0; |
| 162 | const unsigned int output_height = std::ceil(num_vert_blocks / static_cast<float>(lhs_info.v0)); |
| 163 | |
| 164 | TensorShape lhs_shape{ a.tensor_shape() }; |
| 165 | lhs_shape.set(0, output_width); |
| 166 | lhs_shape.set(1, output_height); |
| 167 | |
| 168 | if((reinterpret_input_as_3d) && (lhs_shape.num_dimensions() > 2)) |
| 169 | { |
| 170 | // When the data format is NHWC and the shapes are Nx1x1 |
| 171 | // the tensor shape num_dimensions is automatically set to 1 instead of 3. |
| 172 | // To avoid failures by removing a dimension that doesn't exist |
| 173 | // check if the number of dimensions is greater than 2. |
| 174 | lhs_shape.remove_dimension(2); |
| 175 | } |
| 176 | |
| 177 | return lhs_shape; |
| 178 | } |
| 179 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 180 | /** Calculate the Right Hand Side matrix reshaped shape |
| 181 | * |
| 182 | * @param[in] a Input tensor info |
| 183 | * @param[in] rhs_info Right Hand Side matrix information |
| 184 | * |
| 185 | * @return the calculated shape |
| 186 | */ |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 187 | inline TensorShape compute_rhs_reshaped_shape(const ITensorInfo &a, const GEMMRHSMatrixInfo &rhs_info) |
| 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON(rhs_info.n0 == 0); |
| 190 | ARM_COMPUTE_ERROR_ON(rhs_info.k0 == 0); |
| 191 | ARM_COMPUTE_ERROR_ON(rhs_info.h0 == 0); |
| 192 | |
| 193 | // Input width/height |
| 194 | const unsigned int input_width = a.dimension(0); |
| 195 | const unsigned int input_height = a.dimension(1); |
| 196 | |
| 197 | // Number of horizontal/vertical blocks in the input tensor |
| 198 | const unsigned int num_horiz_blocks = std::ceil(input_width / static_cast<float>(rhs_info.n0)); |
| 199 | const unsigned int num_vert_blocks = std::ceil(input_height / static_cast<float>(rhs_info.k0)); |
| 200 | |
| 201 | // Block size |
| 202 | const unsigned int block_size = rhs_info.n0 * rhs_info.k0; |
| 203 | |
| 204 | // Output width/height |
| 205 | const unsigned int output_width = block_size * num_vert_blocks * rhs_info.h0; |
| 206 | const unsigned int output_height = std::ceil(num_horiz_blocks / static_cast<float>(rhs_info.h0)); |
| 207 | |
| 208 | TensorShape rhs_shape{ a.tensor_shape() }; |
| 209 | rhs_shape.set(0, output_width); |
| 210 | rhs_shape.set(1, output_height); |
| 211 | |
| 212 | return rhs_shape; |
| 213 | } |
| 214 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 215 | /** Calculate the interleaved shape of an input tensor |
| 216 | * |
| 217 | * @param[in] a Input tensor info |
| 218 | * @param[in] mult_interleave4x4_height (Optional) Interleave4x4 height |
| 219 | * @param[in] reinterpret_input_as_3d (Optional) Set to true if the input need to be interpreted as 3d |
| 220 | * |
| 221 | * @return the calculated shape |
| 222 | */ |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 223 | inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1, bool reinterpret_input_as_3d = false) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 224 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 225 | // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height |
| 226 | ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1); |
| 227 | const int interleave_width = 4 * mult_interleave4x4_height; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 228 | TensorShape shape_interleaved_a{ a.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 229 | shape_interleaved_a.set(0, a.dimension(0) * interleave_width); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 230 | if(reinterpret_input_as_3d) |
| 231 | { |
| 232 | const int M = a.dimension(1) * a.dimension(2); |
| 233 | const int height = std::ceil(M / static_cast<float>(interleave_width)); |
| 234 | shape_interleaved_a.set(1, height); |
Isabella Gottardi | 089695f | 2018-10-17 18:04:15 +0100 | [diff] [blame] | 235 | |
| 236 | // When the data format is NHWC and the shapes are Nx1x1 |
| 237 | // the tensor shape num_dimensions is automatically set to 1 instead of 3. |
| 238 | // To avoid failures by removing a dimension that doesn't exist |
| 239 | // check if the number of dimensions is greater than 2. |
| 240 | if(shape_interleaved_a.num_dimensions() > 2) |
| 241 | { |
| 242 | shape_interleaved_a.remove_dimension(2); |
| 243 | } |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 244 | } |
| 245 | else |
| 246 | { |
| 247 | shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width))); |
| 248 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 249 | |
| 250 | return shape_interleaved_a; |
| 251 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 252 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 253 | /** Calculate the transposed 1xW shape |
| 254 | * |
| 255 | * @param[in] b Input tensor info |
| 256 | * |
| 257 | * @return the calculated shape |
| 258 | */ |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 259 | inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b) |
| 260 | { |
| 261 | // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ] |
| 262 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
| 263 | shape_transposed1xW_b.set(0, b.dimension(1) * 16); |
| 264 | shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f)); |
| 265 | |
| 266 | return shape_transposed1xW_b; |
| 267 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 268 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 269 | /** Calculate the transposed 1xW width element shape |
| 270 | * |
| 271 | * @param[in] b Input tensor info |
| 272 | * @param[in] mult_transpose1xW_width (Optional) Transpose1xW width |
| 273 | * |
| 274 | * @return the calculated shape |
| 275 | */ |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 276 | 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] | 277 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 278 | // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row |
| 279 | // The transpose1xW output matrix will have the following shape: |
| 280 | // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width |
| 281 | ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 282 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 283 | const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 284 | shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width); |
| 285 | shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width)))); |
| 286 | |
| 287 | return shape_transposed1xW_b; |
| 288 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 289 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 290 | /** Calculate the reductionA shape used in GEMMLowp |
| 291 | * |
| 292 | * @param[in] b Input tensor info |
| 293 | * |
| 294 | * @return the calculated shape |
| 295 | */ |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 296 | inline TensorShape compute_reductionA_shape(const ITensorInfo &b) |
| 297 | { |
| 298 | TensorShape shape_vector_sum_col{ b.tensor_shape() }; |
| 299 | if(shape_vector_sum_col.num_dimensions() > 1) |
| 300 | { |
| 301 | shape_vector_sum_col.remove_dimension(1); |
| 302 | } |
| 303 | |
| 304 | return shape_vector_sum_col; |
| 305 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 306 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 307 | /** Calculate the reductionB shape used in GEMMLowp |
| 308 | * |
| 309 | * @param[in] a Input tensor info |
| 310 | * |
| 311 | * @return the calculated shape |
| 312 | */ |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 313 | inline TensorShape compute_reductionB_shape(const ITensorInfo &a) |
| 314 | { |
| 315 | TensorShape shape_vector_sum_row{ a.tensor_shape() }; |
| 316 | shape_vector_sum_row.set(Window::DimX, a.dimension(1)); |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 317 | if(shape_vector_sum_row.num_dimensions() > 1) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 318 | { |
| 319 | shape_vector_sum_row.remove_dimension(1); |
| 320 | } |
| 321 | |
| 322 | return shape_vector_sum_row; |
| 323 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 324 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 325 | /** Calculate the Col2Im shape |
| 326 | * |
| 327 | * @param[in] input Input tensor info |
| 328 | * @param[in] convolved_dims Convolved dimensions |
| 329 | * @param[in] batch_size_on_z True if batch size is on z axis |
| 330 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution |
| 331 | * |
| 332 | * @return the calculated shape |
| 333 | */ |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 334 | inline TensorShape compute_col2im_shape(const ITensorInfo &input, const Size2D &convolved_dims, bool batch_size_on_z, unsigned int num_groups = 1) |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 335 | { |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 336 | ARM_COMPUTE_ERROR_ON(num_groups == 0); |
Giorgio Arena | 226e4b9 | 2018-08-23 12:00:02 +0100 | [diff] [blame] | 337 | ARM_COMPUTE_ERROR_ON(input.tensor_shape()[1] != (convolved_dims.area())); |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 338 | ARM_COMPUTE_ERROR_ON((num_groups > 1) && input.tensor_shape()[2] != num_groups); |
| 339 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 340 | const DataLayout data_layout = input.data_layout(); |
| 341 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 342 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 343 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
Michele Di Giorgio | 980002b | 2018-08-08 09:25:51 +0100 | [diff] [blame] | 344 | |
Georgios Pinitas | e55b40a | 2018-09-13 17:20:04 +0100 | [diff] [blame] | 345 | TensorShape col2im_shape{ input.tensor_shape() }; |
| 346 | // If batches start on 3rd dimension shift dimensions right by 1 to retain upper tensor shape, |
| 347 | // as first three will be override by H,W,C data |
| 348 | if(batch_size_on_z && num_groups == 1) |
| 349 | { |
| 350 | col2im_shape.shift_right(1); |
| 351 | } |
| 352 | col2im_shape.set(width_idx, convolved_dims.width); |
| 353 | col2im_shape.set(height_idx, convolved_dims.height); |
| 354 | col2im_shape.set(channel_idx, input.tensor_shape()[0] * num_groups); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 355 | |
| 356 | return col2im_shape; |
| 357 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 358 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 359 | /** Calculate the transposed shape of a tensor |
| 360 | * |
| 361 | * @param[in] input Input tensor info |
| 362 | * |
| 363 | * @return the calculated shape |
| 364 | */ |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 365 | inline TensorShape compute_transposed_shape(const ITensorInfo &input) |
| 366 | { |
| 367 | TensorShape shape_transposed{ input.tensor_shape() }; |
| 368 | |
| 369 | shape_transposed.set(0, input.dimension(1)); |
| 370 | shape_transposed.set(1, input.dimension(0)); |
| 371 | |
| 372 | return shape_transposed; |
| 373 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 374 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 375 | /** Calculate the depthwise convolution output shape of a tensor |
| 376 | * |
| 377 | * @param[in] input Input tensor info |
| 378 | * @param[in] weights Weights tensor info |
| 379 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 380 | * @param[in] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth. |
| 381 | * |
| 382 | * @return the calculated shape |
| 383 | */ |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 384 | 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] | 385 | { |
| 386 | const TensorShape input_shape{ input.tensor_shape() }; |
| 387 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 388 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 389 | const DataLayout data_layout = input.data_layout(); |
| 390 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 391 | 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] | 392 | 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] | 393 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 394 | unsigned int output_width = 0; |
| 395 | unsigned int output_height = 0; |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 396 | std::tie(output_width, output_height) = scaled_dimensions(input_shape[width_idx], input_shape[height_idx], |
| 397 | weights_shape[width_idx], weights_shape[height_idx], |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 398 | conv_info); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 399 | |
| 400 | TensorShape output_shape{ input_shape }; |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 401 | output_shape.set(width_idx, output_width); |
| 402 | output_shape.set(height_idx, output_height); |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 403 | output_shape.set(channel_idx, input_shape[channel_idx] * depth_multiplier); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 404 | |
| 405 | return output_shape; |
| 406 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 407 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 408 | /** Calculate the upsampled output shape used for deconvolution |
| 409 | * |
| 410 | * @param[in] input Input tensor info |
| 411 | * @param[in] weights Weights tensor shape |
| 412 | * @param[in] sx Stride on x axis |
| 413 | * @param[in] sy Stride on y axis |
| 414 | * @param[in] inner_border_right The number of zeros added to right edge of the input. |
| 415 | * @param[in] inner_border_top The number of zeros added to top edge of the input. |
| 416 | * @param[in] out_dims Output shape dimensions |
| 417 | * @param[in] padx Padding on x axis |
| 418 | * @param[in] pady Padding on y axis |
| 419 | * |
| 420 | * @return the calculated shape |
| 421 | */ |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 422 | inline TensorShape compute_deconvolution_upsampled_shape(const ITensorInfo &input, const ITensorInfo &weights, unsigned int sx, unsigned int sy, unsigned int inner_border_right, |
| 423 | unsigned int inner_border_top, |
| 424 | std::pair<unsigned int, unsigned int> &out_dims, unsigned int &padx, unsigned int &pady) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 425 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 426 | const DataLayout data_layout = input.data_layout(); |
| 427 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 428 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 429 | |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 430 | // Find the upsampled dimensions |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 431 | unsigned int out_x = (input.dimension(idx_w) - 1) * sx + inner_border_right + 1; |
| 432 | unsigned int out_y = (input.dimension(idx_h) - 1) * sy + inner_border_top + 1; |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 433 | |
| 434 | // Find the padding needed for the convolution with stride 1 in order to match output shape |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 435 | padx = out_dims.first - (out_x - weights.dimension(idx_w) + 1); |
| 436 | pady = out_dims.second - (out_y - weights.dimension(idx_h) + 1); |
Michalis Spyrou | afbc5ff | 2018-10-03 14:18:19 +0100 | [diff] [blame] | 437 | out_x += padx; |
| 438 | out_y += pady; |
| 439 | |
| 440 | TensorShape scale_out_shape(input.tensor_shape()); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 441 | scale_out_shape.set(idx_w, out_x); |
| 442 | scale_out_shape.set(idx_h, out_y); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 443 | |
| 444 | return scale_out_shape; |
| 445 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 446 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 447 | /** Calculate the output shape of the deconvolution layer |
| 448 | * |
| 449 | * @param[in] out_dims Output x and y shape dimensions |
| 450 | * @param[in] input Input tensor info |
| 451 | * @param[in] weights Weights tensor shape |
| 452 | * |
| 453 | * @return the calculated shape |
| 454 | */ |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 455 | inline TensorShape compute_deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, const ITensorInfo &input, const ITensorInfo &weights) |
| 456 | { |
| 457 | const TensorShape input_shape{ input.tensor_shape() }; |
| 458 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 459 | |
| 460 | const DataLayout data_layout = input.data_layout(); |
| 461 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 462 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 463 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 464 | const int batch_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 465 | |
| 466 | TensorShape out_shape{ input_shape }; |
| 467 | out_shape.set(width_idx, out_dims.first); |
| 468 | out_shape.set(height_idx, out_dims.second); |
| 469 | out_shape.set(channel_idx, weights_shape[batch_idx]); |
| 470 | return out_shape; |
| 471 | } |
| 472 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 473 | /** Calculate the im2col output shape of a tensor |
| 474 | * |
| 475 | * @param[in] input Input tensor info |
| 476 | * @param[in] kernel_dims The kernel dimensions (width and height). |
| 477 | * @param[in] conv_info Contains padding and stride information |
| 478 | * @param[in] has_bias In case biases are provided expands the matrix with 1 |
| 479 | * @param[in] dilation Dilation, in elements, across x and y |
| 480 | * @param[in] batch_size_on_z True if batch size is on z axis |
| 481 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution |
| 482 | * |
| 483 | * @return the calculated shape |
| 484 | */ |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 485 | 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, |
| 486 | unsigned int num_groups = 1) |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 487 | { |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 488 | // The output shape will be the 3D shape [ out_channels * kernel_area, num_elems_per_out_channel, batches ] if batch_size_on_z == true |
| 489 | // or the 4D shape [ out_channels * kernel_area / num_groups, num_elems_per_out_channel, num_groups, batches ] if batch_size_on_z == false |
| 490 | |
| 491 | ARM_COMPUTE_ERROR_ON(num_groups == 0); |
| 492 | ARM_COMPUTE_ERROR_ON(num_groups > 1 && input->data_layout() != DataLayout::NCHW); |
| 493 | ARM_COMPUTE_ERROR_ON(num_groups > 1 && batch_size_on_z); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 494 | |
| 495 | TensorShape output_shape{ input->tensor_shape() }; |
| 496 | |
| 497 | const DataLayout data_layout = input->data_layout(); |
| 498 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 499 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 500 | const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 501 | |
| 502 | 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 | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 503 | output_shape.set(0, (output_shape[channel_idx] / num_groups * kernel_dims.area() + (has_bias ? 1 : 0))); // NOLINT |
Giorgio Arena | f485a10 | 2018-04-20 16:06:21 +0100 | [diff] [blame] | 504 | output_shape.set(1, (out_dims.first * out_dims.second)); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 505 | if(batch_size_on_z && output_shape.num_dimensions() >= 3) |
| 506 | { |
| 507 | output_shape.remove_dimension(2); |
| 508 | } |
| 509 | else |
| 510 | { |
Giorgio Arena | 0f17039 | 2018-07-18 16:13:12 +0100 | [diff] [blame] | 511 | output_shape.set(2, num_groups); |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 512 | } |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 513 | |
| 514 | return output_shape; |
| 515 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 516 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 517 | /** Calculate the flattened output shape of a tensor |
| 518 | * |
| 519 | * @param[in] input Input tensor info |
| 520 | * |
| 521 | * @return the calculated shape |
| 522 | */ |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 523 | inline TensorShape compute_flatten_shape(const ITensorInfo *input) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 524 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 525 | // The output shape will be the flatten version of the input (i.e. [ width * height * channels, num_batches, ... ] ). Used for FlattenLayer and FullyConnectedLayer. |
| 526 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 527 | TensorShape output_shape{ input->tensor_shape() }; |
| 528 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 529 | output_shape.collapse(3); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 530 | |
| 531 | return output_shape; |
| 532 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 533 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 534 | /** Calculate the softmax output shape of a tensor |
| 535 | * |
| 536 | * @param[in] input Input tensor info |
| 537 | * @param[in] axis (Optional) Softmax axis |
| 538 | * |
| 539 | * @return the calculated shape |
| 540 | */ |
giuros01 | efbf6c8 | 2018-09-03 09:53:53 +0100 | [diff] [blame] | 541 | inline TensorShape compute_softmax_shape(const ITensorInfo *input, size_t axis = 1) |
| 542 | { |
| 543 | // The output shape will be a 2D version of the input. For instance: |
| 544 | // - [x,y,z] and axis 1 will return [x, y*z] |
| 545 | // - [x,y,z,w] and axis 2 will return [x*y, w*z] |
| 546 | // - [x,y,z,w] and axis 3 will return [x*y*z, w] |
| 547 | TensorShape shape2D = input->tensor_shape(); |
| 548 | |
| 549 | if(axis < input->num_dimensions()) |
| 550 | { |
| 551 | // Collapse from axis onward (this changes the shape) |
| 552 | shape2D.collapse_from(axis); |
| 553 | |
| 554 | // Collapse the rest (collapse is inclusive) |
| 555 | shape2D.collapse(shape2D.num_dimensions() - 1); |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | // Collapse everything |
| 560 | shape2D.collapse(shape2D.num_dimensions()); |
| 561 | } |
| 562 | |
| 563 | if(axis == 0) |
| 564 | { |
| 565 | // If axis is zero the first dim should be one. Since |
| 566 | // collapse is an inclusive operation we need to shift |
| 567 | shape2D.shift_right(1); |
| 568 | } |
| 569 | |
| 570 | return shape2D; |
| 571 | } |
| 572 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 573 | /** Calculate the winograd filter transform shape |
| 574 | * |
| 575 | * @param[in] input Input tensor info |
| 576 | * @param[in] winograd_info Winograd information |
| 577 | * |
| 578 | * @return the calculated shape |
| 579 | */ |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 580 | 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] | 581 | { |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 582 | TensorShape tensor_shape{ input.tensor_shape() }; |
| 583 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 584 | const Size2D kernel_size = winograd_info.kernel_size; |
| 585 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 586 | 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] | 587 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 588 | tensor_shape.remove_dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH)); |
| 589 | tensor_shape.set(Window::DimX, input.dimension(3)); |
| 590 | tensor_shape.set(Window::DimY, input.dimension(get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL))); |
| 591 | tensor_shape.set(Window::DimZ, input_tile_size.area()); |
Gian Marco Iodice | 7e4b239 | 2018-02-22 16:17:20 +0000 | [diff] [blame] | 592 | |
| 593 | return tensor_shape; |
| 594 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 595 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 596 | /** Calculate the winograd input transform shape |
| 597 | * |
| 598 | * @param[in] input Input tensor info |
| 599 | * @param[in] winograd_info Winograd information |
| 600 | * |
| 601 | * @return the calculated shape |
| 602 | */ |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 603 | 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] | 604 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 605 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 606 | const Size2D kernel_size = winograd_info.kernel_size; |
| 607 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 608 | const Size2D input_tile_size = Size2D(output_tile_size.width + kernel_size.width - 1, output_tile_size.height + kernel_size.height - 1); |
| 609 | |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 610 | const size_t idx_w = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 611 | const size_t idx_h = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 612 | 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] | 613 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 614 | // Compute the number of output tiles along the x and y direction of size "output_tile_size" |
| 615 | const Size2D num_tiles = compute_winograd_convolution_tiles(Size2D(input.tensor_shape()[idx_w], input.tensor_shape()[idx_h]), |
| 616 | kernel_size, |
| 617 | output_tile_size, |
| 618 | conv_info); |
Giorgio Arena | c42f28d | 2018-04-26 11:33:05 +0100 | [diff] [blame] | 619 | |
| 620 | const unsigned int width = input.tensor_shape()[idx_c]; |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 621 | const unsigned int height = num_tiles.area(); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 622 | const unsigned int depth = input_tile_size.area(); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 623 | |
| 624 | TensorShape output_shape{ input.tensor_shape() }; |
| 625 | output_shape.set(0, width); |
| 626 | output_shape.set(1, height); |
| 627 | output_shape.set(2, depth); |
| 628 | |
| 629 | return output_shape; |
| 630 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 631 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 632 | /** Calculate the winograd output transform shape |
| 633 | * |
| 634 | * @param[in] input Input tensor info |
| 635 | * @param[in] winograd_info Winograd information |
| 636 | * |
| 637 | * @return the calculated shape |
| 638 | */ |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 639 | 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] | 640 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 641 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 642 | const Size2D kernel_size = winograd_info.kernel_size; |
| 643 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 644 | const DataLayout data_layout = winograd_info.output_data_layout; |
| 645 | |
| 646 | // Compute output shape |
| 647 | unsigned int output_width = 0; |
| 648 | unsigned int output_height = 0; |
| 649 | std::tie(output_width, output_height) = scaled_dimensions(input_dimensions.width, input_dimensions.height, |
| 650 | kernel_size.width, kernel_size.height, conv_info); |
| 651 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 652 | TensorShape tensor_shape{ input.tensor_shape() }; |
| 653 | |
| 654 | // Output dimension |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 655 | const unsigned int out_w = output_width; |
| 656 | const unsigned int out_h = output_height; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 657 | const unsigned int out_c = input.dimension(0); |
| 658 | |
| 659 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH), out_w); |
| 660 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT), out_h); |
| 661 | tensor_shape.set(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL), out_c); |
| 662 | |
| 663 | return tensor_shape; |
| 664 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 665 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 666 | /** Calculate the deep convolution shape output shape of a tensor |
| 667 | * |
| 668 | * @param[in] input Input tensor info |
| 669 | * @param[in] weights Weights tensor info |
| 670 | * @param[in] conv_info Contains padding and stride information |
| 671 | * |
| 672 | * @return the calculated shape |
| 673 | */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 674 | inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info) |
| 675 | { |
| 676 | const TensorShape input_shape{ input.tensor_shape() }; |
| 677 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 678 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 679 | const size_t idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 680 | const size_t idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 681 | const size_t idx_channel = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL); |
| 682 | |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 683 | const unsigned int input_width = input_shape[idx_width]; |
| 684 | const unsigned int input_height = input_shape[idx_height]; |
| 685 | const unsigned int weights_width = weights_shape[idx_width]; |
| 686 | const unsigned int weights_height = weights_shape[idx_height]; |
| 687 | const unsigned int weights_out_channel = weights_shape[3]; |
| 688 | unsigned int output_width = 0; |
| 689 | unsigned int output_height = 0; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 690 | 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] | 691 | |
| 692 | TensorShape output_shape{ input_shape }; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 693 | output_shape.set(idx_width, output_width); |
| 694 | output_shape.set(idx_height, output_height); |
Giorgio Arena | c0f5443 | 2018-03-16 14:02:34 +0000 | [diff] [blame] | 695 | output_shape.set(idx_channel, weights_out_channel); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 696 | |
| 697 | return output_shape; |
| 698 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 699 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 700 | /** Calculate the min/max shape output shape of a tensor |
| 701 | * |
| 702 | * @param[in] input Input tensor info |
| 703 | * |
| 704 | * @return the calculated shape |
| 705 | */ |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 706 | inline TensorShape compute_min_max_shape(const ITensorInfo *input) |
| 707 | { |
| 708 | TensorShape output_shape{ input->tensor_shape() }; |
| 709 | output_shape.set(Window::DimX, 2); |
| 710 | output_shape.remove_dimension(1); |
| 711 | output_shape.remove_dimension(1); |
| 712 | |
| 713 | return output_shape; |
| 714 | } |
| 715 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 716 | /** Calculate the output pool shape of a tensor |
| 717 | * |
| 718 | * @param[in] input Input tensor info |
| 719 | * @param[in] pool_info Pooling layer info |
| 720 | * |
| 721 | * @return the calculated shape |
| 722 | */ |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 723 | inline TensorShape compute_pool_shape(const ITensorInfo &input, PoolingLayerInfo pool_info) |
| 724 | { |
| 725 | unsigned int pooled_w = 0; |
| 726 | unsigned int pooled_h = 0; |
| 727 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 728 | TensorShape output_shape{ input.tensor_shape() }; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 729 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 730 | const bool is_global_pooling = pool_info.is_global_pooling(); |
| 731 | const unsigned int idx_width = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::WIDTH); |
| 732 | const unsigned int idx_height = get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::HEIGHT); |
| 733 | const unsigned int pool_size_x = is_global_pooling ? output_shape[idx_width] : pool_info.pool_size().width; |
| 734 | const unsigned int pool_size_y = is_global_pooling ? output_shape[idx_height] : pool_info.pool_size().height; |
| 735 | |
| 736 | std::tie(pooled_w, pooled_h) = scaled_dimensions(output_shape[idx_width], |
| 737 | output_shape[idx_height], |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 738 | pool_size_x, |
| 739 | pool_size_y, |
| 740 | pool_info.pad_stride_info()); |
| 741 | |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 742 | output_shape.set(idx_width, pooled_w); |
| 743 | output_shape.set(idx_height, pooled_h); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 744 | |
| 745 | return output_shape; |
| 746 | } |
| 747 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 748 | /** Calculate the RNN shape of a tensor |
| 749 | * |
| 750 | * @param[in] input Input tensor info |
| 751 | * @param[in] batch_size Batch size |
| 752 | * |
| 753 | * @return the calculated shape |
| 754 | */ |
Michalis Spyrou | 36a559e | 2018-03-20 10:30:58 +0000 | [diff] [blame] | 755 | inline TensorShape compute_rnn_shape(const ITensorInfo *input, const unsigned int batch_size) |
| 756 | { |
| 757 | TensorShape output_shape{ input->tensor_shape() }; |
| 758 | output_shape.set(1, batch_size); |
| 759 | |
| 760 | return output_shape; |
| 761 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 762 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 763 | /** Calculate the matrix multiplication output shape of two tensors |
| 764 | * |
| 765 | * @param[in] input0 First input tensor info |
| 766 | * @param[in] input1 Second input tensor info |
| 767 | * @param[in] is_interleaved_transposed True if the input is interleaved transposed |
| 768 | * @param[in] reshape_info GEMM reshape info |
| 769 | * |
| 770 | * @return the calculated shape |
| 771 | */ |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 772 | inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, bool is_interleaved_transposed, const GEMMReshapeInfo &reshape_info) |
| 773 | { |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 774 | ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4"); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 775 | 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 Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 776 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 777 | const bool reinterpret_input_as_3d = reshape_info.reinterpret_input_as_3d(); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 778 | const bool reinterpret_output_as_3d = reshape_info.depth_output_gemm3d() != 0; |
| 779 | const int depth_output_gemm3d = reinterpret_output_as_3d ? reshape_info.depth_output_gemm3d() : 1; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 780 | const int m = reshape_info.reinterpret_input_as_3d() ? input0.dimension(1) * input0.dimension(2) : input0.dimension(1); |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 781 | |
| 782 | // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third |
| 783 | // dimension of the output tensor |
| 784 | const int dim0 = is_interleaved_transposed ? reshape_info.n() : input1.dimension(0); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 785 | const int dim1 = is_interleaved_transposed ? reshape_info.m() / depth_output_gemm3d : m / depth_output_gemm3d; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 786 | const int dim2 = reinterpret_input_as_3d ? input0.tensor_shape()[3] : input0.tensor_shape()[2]; |
| 787 | const int dim3 = reinterpret_input_as_3d ? 1 : input0.tensor_shape()[3]; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 788 | |
| 789 | TensorShape output_shape{ input0.tensor_shape() }; |
| 790 | |
| 791 | output_shape.set(0, dim0); |
| 792 | output_shape.set(1, dim1); |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 793 | output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2); |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 794 | output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3); |
| 795 | output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1); |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 796 | |
| 797 | return output_shape; |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 798 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 799 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 800 | /** Calculate the matrix multiplication output shape of two tensors |
| 801 | * |
| 802 | * @param[in] input0 First input tensor info |
| 803 | * @param[in] input1 Second input tensor info |
| 804 | * @param[in] gemm_info GEMM reshape info |
| 805 | * |
| 806 | * @return the calculated shape |
| 807 | */ |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 808 | inline TensorShape compute_mm_shape(const ITensorInfo &input0, const ITensorInfo &input1, const GEMMReshapeInfo &gemm_info) |
| 809 | { |
| 810 | ARM_COMPUTE_ERROR_ON_MSG(input0.num_dimensions() > 4, "The number of dimensions for the matrix A must be <= 4"); |
| 811 | |
| 812 | const bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d() != 0; |
| 813 | const int depth_output_gemm3d = reinterpret_output_as_3d ? gemm_info.depth_output_gemm3d() : 1; |
| 814 | |
| 815 | // If the output of GEMM has to be reinterpreted as 3D, the number of input0 rows (M) is obtained collapsing the second and third |
| 816 | // dimension of the output tensor |
| 817 | const int dim0 = gemm_info.n(); |
| 818 | const int dim1 = gemm_info.m() / depth_output_gemm3d; |
| 819 | const int dim2 = input0.tensor_shape()[2]; |
| 820 | const int dim3 = input0.tensor_shape()[3]; |
| 821 | |
| 822 | TensorShape output_shape{ input0.tensor_shape() }; |
| 823 | |
| 824 | output_shape.set(0, dim0); |
| 825 | output_shape.set(1, dim1); |
| 826 | output_shape.set(2, reinterpret_output_as_3d ? depth_output_gemm3d : dim2); |
| 827 | output_shape.set(3, reinterpret_output_as_3d ? dim2 : dim3); |
| 828 | output_shape.set(4, reinterpret_output_as_3d ? dim3 : 1); |
| 829 | |
| 830 | return output_shape; |
| 831 | } |
| 832 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 833 | /** Calculate the matrix multiplication output shape of two tensors |
| 834 | * |
| 835 | * @param[in] input Input tensor info |
| 836 | * @param[in] gemm_3d_depth (Optional) GEMM 3d depth |
| 837 | * @param[in] batch_size_on_z (Optional) True if batch size is on z axis |
| 838 | * |
| 839 | * @return the calculated shape |
| 840 | */ |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 841 | inline TensorShape compute_output_stage_shape(const ITensorInfo &input, unsigned int gemm_3d_depth = 1, bool batch_size_on_z = false) |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 842 | { |
| 843 | ARM_COMPUTE_ERROR_ON(input.data_layout() != DataLayout::NHWC && gemm_3d_depth > 1); |
| 844 | |
| 845 | TensorShape output_shape = input.tensor_shape(); |
| 846 | if(gemm_3d_depth > 1) |
| 847 | { |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 848 | if(batch_size_on_z) |
| 849 | { |
| 850 | output_shape.shift_right(1); |
| 851 | } |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 852 | output_shape.set(0, input.tensor_shape().x()); |
| 853 | output_shape.set(1, input.tensor_shape().y() / gemm_3d_depth); |
| 854 | output_shape.set(2, gemm_3d_depth); |
| 855 | } |
| 856 | |
| 857 | return output_shape; |
| 858 | } |
| 859 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 860 | /** Calculate the strided slice output shape of a tensor |
| 861 | * |
| 862 | * @param[in] input Input tensor info |
| 863 | * @param[in] starts The starts of the dimensions of the input tensor to be sliced |
| 864 | * @param[in] ends The ends of the dimensions of the input tensor to be sliced |
| 865 | * @param[in] strides The strides of the dimensions of the input tensor to be sliced |
| 866 | * @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. |
| 867 | * @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. |
| 868 | * @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 |
| 869 | * |
| 870 | * @return the calculated shape |
| 871 | */ |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 872 | inline TensorShape compute_strided_slice_shape(const ITensorInfo &input, |
| 873 | const Coordinates &starts, const Coordinates &ends, const Coordinates &strides, |
| 874 | int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask) |
| 875 | { |
| 876 | using namespace arm_compute::helpers::tensor_transform; |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 877 | return compute_strided_slice_output_shape(input.tensor_shape(), starts, ends, strides, begin_mask, end_mask, shrink_axis_mask); |
| 878 | } |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 879 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 880 | /** Calculate the slice output shape of a tensor |
| 881 | * |
| 882 | * @param[in] input_shape Input tensor info |
| 883 | * @param[in] starts The starts of the dimensions of the input tensor to be sliced |
| 884 | * @param[in] ends The ends of the dimensions of the input tensor to be sliced |
| 885 | * |
| 886 | * @return the calculated shape |
| 887 | */ |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 888 | inline TensorShape compute_slice_shape(const TensorShape &input_shape, const Coordinates &starts, const Coordinates &ends) |
| 889 | { |
| 890 | using namespace arm_compute::helpers::tensor_transform; |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 891 | |
Georgios Pinitas | b4af2c6 | 2018-12-10 18:45:35 +0000 | [diff] [blame] | 892 | return compute_strided_slice_output_shape(input_shape, |
| 893 | starts, ends, BiStrides(), |
| 894 | 0, construct_slice_end_mask(ends), 0); |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 895 | } |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 896 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 897 | /** Calculate the batch to space output shape of a tensor |
| 898 | * |
| 899 | * @param[in] input Input tensor info |
| 900 | * @param[in] block_x Block shape x value |
| 901 | * @param[in] block_y Block shape y value |
| 902 | * |
| 903 | * @return the calculated shape |
| 904 | */ |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 905 | inline TensorShape compute_batch_to_space_shape(const ITensorInfo *input, const int block_x, const int block_y) |
| 906 | { |
| 907 | ARM_COMPUTE_ERROR_ON(block_x <= 0 || block_y <= 0); |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 908 | |
| 909 | const DataLayout data_layout = input->data_layout(); |
| 910 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 911 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 912 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 913 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 914 | TensorShape output_shape{ input->tensor_shape() }; |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 915 | output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x); |
| 916 | output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y); |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 917 | output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y)); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 918 | |
| 919 | return output_shape; |
| 920 | } |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 921 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 922 | /** Calculate the split output shape of a tensor |
| 923 | * |
| 924 | * @param[in] input Input tensor info |
| 925 | * @param[in] axis Axis on which to split the input |
| 926 | * @param[in] num_splits Number of splits |
| 927 | * |
| 928 | * @return the calculated shape |
| 929 | */ |
Georgios Pinitas | e1a352c | 2018-09-03 12:42:19 +0100 | [diff] [blame] | 930 | inline TensorShape compute_split_shape(const ITensorInfo *input, unsigned int axis, unsigned int num_splits) |
| 931 | { |
| 932 | TensorShape empty_shape; |
| 933 | empty_shape.set(0, 0); |
| 934 | |
| 935 | TensorShape out_shape{ input->tensor_shape() }; |
| 936 | |
| 937 | // Return empty shape if axis is invalid |
| 938 | if(axis > input->tensor_shape().num_dimensions()) |
| 939 | { |
| 940 | return empty_shape; |
| 941 | } |
| 942 | |
| 943 | size_t axis_size = out_shape[axis]; |
| 944 | |
| 945 | // Return empty shape if num_split is not valid |
| 946 | if(axis_size % num_splits) |
| 947 | { |
| 948 | return empty_shape; |
| 949 | } |
| 950 | |
| 951 | out_shape[axis] = axis_size / num_splits; |
| 952 | return out_shape; |
| 953 | } |
| 954 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 955 | /** Calculate the space to batch output shape of a tensor |
| 956 | * |
| 957 | * @param[in] input Input tensor info |
| 958 | * @param[in] block_x Block shape x value |
| 959 | * @param[in] block_y Block shape y value |
| 960 | * @param[in] padding_left Left padding values |
| 961 | * @param[in] padding_right Right padding values |
| 962 | * |
| 963 | * @return the calculated shape |
| 964 | */ |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 965 | 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) |
| 966 | { |
| 967 | TensorShape output_shape{ input->tensor_shape() }; |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 968 | |
| 969 | const DataLayout data_layout = input->data_layout(); |
| 970 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 971 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 972 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 973 | |
| 974 | output_shape.set(idx_width, input->tensor_shape()[idx_width] * block_x + padding_left.x() + padding_right.x()); |
| 975 | output_shape.set(idx_height, input->tensor_shape()[idx_height] * block_y + padding_left.y() + padding_right.y()); |
| 976 | output_shape.set(idx_batch, input->tensor_shape()[idx_batch] / (block_x * block_y)); |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 977 | |
| 978 | return output_shape; |
| 979 | } |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 980 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 981 | /** Calculate the prior box output shape of a tensor |
| 982 | * |
| 983 | * @param[in] input Input tensor info |
| 984 | * @param[in] info PriorBoxLayer info |
| 985 | * |
| 986 | * @return the calculated shape |
| 987 | */ |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 988 | inline TensorShape compute_prior_box_shape(const ITensorInfo &input, const PriorBoxLayerInfo &info) |
| 989 | { |
| 990 | DataLayout data_layout = input.data_layout(); |
| 991 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 992 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 993 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 994 | |
| 995 | TensorShape output_shape{}; |
| 996 | output_shape.set(0, input.dimension(idx_w) * input.dimension(idx_h) * num_priors * 4); |
| 997 | output_shape.set(1, 2); |
| 998 | |
| 999 | return output_shape; |
| 1000 | } |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 1001 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1002 | /** Calculate the padded shape of a tensor |
| 1003 | * |
| 1004 | * @param[in] input_shape Input tensor shape |
| 1005 | * @param[in] padding Paddings list |
| 1006 | * |
| 1007 | * @return the calculated shape |
| 1008 | */ |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 1009 | inline TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding) |
| 1010 | { |
| 1011 | TensorShape padded_shape = input_shape; |
| 1012 | for(size_t dim = 0; dim < padding.size(); ++dim) |
| 1013 | { |
| 1014 | padded_shape.set(dim, padding[dim].first + input_shape[dim] + padding[dim].second); |
| 1015 | } |
| 1016 | return padded_shape; |
| 1017 | } |
| 1018 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1019 | /** Calculate the tiled shape of a tensor |
| 1020 | * |
| 1021 | * @param[in] input_shape Input tensor shape |
| 1022 | * @param[in] multiples Paddings list |
| 1023 | * |
| 1024 | * @return the calculated shape |
| 1025 | */ |
giuros01 | 3175fcf | 2018-11-21 09:59:17 +0000 | [diff] [blame] | 1026 | inline TensorShape compute_tiled_shape(const TensorShape &input_shape, const Multiples &multiples) |
| 1027 | { |
| 1028 | TensorShape tiled_shape = input_shape; |
| 1029 | for(size_t dim = 0; dim < multiples.size(); ++dim) |
| 1030 | { |
| 1031 | tiled_shape.set(dim, input_shape[dim] * multiples[dim]); |
| 1032 | } |
| 1033 | return tiled_shape; |
| 1034 | } |
| 1035 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1036 | /** Calculate the upsampled shape of a tensor |
| 1037 | * |
| 1038 | * @param[in] input Input tensor info |
| 1039 | * @param[in] info Contains stride information (x and y) |
| 1040 | * |
| 1041 | * @return the calculated shape |
| 1042 | */ |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 1043 | inline TensorShape compute_upsample_shape(const ITensorInfo &input, const Size2D &info) |
| 1044 | { |
| 1045 | const DataLayout data_layout = input.data_layout(); |
| 1046 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 1047 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 1048 | |
| 1049 | TensorShape scale_out_shape(input.tensor_shape()); |
| 1050 | const unsigned int out_x = input.dimension(idx_width) * info.x(); |
| 1051 | const unsigned int out_y = input.dimension(idx_height) * info.y(); |
| 1052 | scale_out_shape.set(idx_width, out_x); |
| 1053 | scale_out_shape.set(idx_height, out_y); |
| 1054 | |
| 1055 | return scale_out_shape; |
| 1056 | } |
| 1057 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1058 | /** Get the tensor shape |
| 1059 | * |
| 1060 | * @param[in] data Input data |
| 1061 | * |
| 1062 | * @return the extracted tensor shape |
| 1063 | */ |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1064 | template <typename T> |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1065 | inline TensorShape extract_shape(T *data) |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1066 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1067 | return data->info()->tensor_shape(); |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1070 | inline TensorShape extract_shape(ITensorInfo *data) |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1071 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1072 | return data->tensor_shape(); |
| 1073 | } |
| 1074 | |
| 1075 | inline TensorShape extract_shape(const TensorShape *data) |
| 1076 | { |
| 1077 | return *data; |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1080 | /** Calculate the unstack shape of a tensor |
| 1081 | * |
| 1082 | * @param[in] input_shape Input tensor shape |
| 1083 | * @param[in] axis Axis on which to perform the unstack operation |
| 1084 | * |
| 1085 | * @return the calculated shape |
| 1086 | */ |
Pablo Tello | 5430369 | 2018-11-22 16:14:36 +0000 | [diff] [blame] | 1087 | inline TensorShape calculate_unstack_shape(TensorShape input_shape, unsigned int axis) |
| 1088 | { |
| 1089 | ARM_COMPUTE_ERROR_ON(axis > input_shape.num_dimensions()); |
| 1090 | input_shape.remove_dimension(axis); |
| 1091 | return input_shape; |
| 1092 | } |
| 1093 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1094 | /** Calculate the depth concatenate output shape of a vector of tensors |
| 1095 | * |
| 1096 | * @param[in] inputs_vector Vector containing the shapes of the inputs |
| 1097 | * |
| 1098 | * @return the calculated shape |
| 1099 | */ |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1100 | template <typename T> |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 1101 | inline TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector) |
| 1102 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1103 | TensorShape out_shape = extract_shape(inputs_vector[0]); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 1104 | |
| 1105 | size_t max_x = 0; |
| 1106 | size_t max_y = 0; |
| 1107 | size_t depth = 0; |
| 1108 | |
| 1109 | for(const auto &tensor : inputs_vector) |
| 1110 | { |
| 1111 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1112 | const TensorShape shape = extract_shape(tensor); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 1113 | max_x = std::max(shape.x(), max_x); |
| 1114 | max_y = std::max(shape.y(), max_y); |
| 1115 | depth += shape.z(); |
| 1116 | } |
| 1117 | |
| 1118 | out_shape.set(0, max_x); |
| 1119 | out_shape.set(1, max_y); |
| 1120 | out_shape.set(2, depth); |
| 1121 | |
| 1122 | return out_shape; |
| 1123 | } |
| 1124 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1125 | /** Calculate the width concatenate output shape of a vector of tensors |
| 1126 | * |
| 1127 | * @param[in] inputs_vector Vector containing the shapes of the inputs |
| 1128 | * |
| 1129 | * @return the calculated shape |
| 1130 | */ |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 1131 | template <typename T> |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1132 | inline TensorShape calculate_width_concatenate_shape(const std::vector<T *> &inputs_vector) |
| 1133 | { |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1134 | TensorShape out_shape = extract_shape(inputs_vector[0]); |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1135 | |
| 1136 | size_t width = 0; |
| 1137 | for(const auto &tensor : inputs_vector) |
| 1138 | { |
| 1139 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 1140 | const TensorShape shape = extract_shape(tensor); |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 1141 | width += shape.x(); |
| 1142 | } |
| 1143 | |
| 1144 | out_shape.set(0, width); |
| 1145 | |
| 1146 | return out_shape; |
| 1147 | } |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 1148 | |
Michalis Spyrou | d33fe34 | 2019-01-04 17:10:25 +0000 | [diff] [blame] | 1149 | /** Calculate the stack output shape of a tensor |
| 1150 | * |
| 1151 | * @param[in] a Input tensor info |
| 1152 | * @param[in] axis Axis on which to perform the stack operation |
| 1153 | * @param[in] num_tensors Number of tensors to stack |
| 1154 | * |
| 1155 | * @return the calculated shape |
| 1156 | */ |
Gian Marco Iodice | 8aa985e | 2018-11-27 15:58:08 +0000 | [diff] [blame] | 1157 | inline TensorShape compute_stack_shape(const ITensorInfo &a, unsigned int axis, unsigned int num_tensors) |
| 1158 | { |
| 1159 | ARM_COMPUTE_ERROR_ON(axis > a.num_dimensions()); |
| 1160 | ARM_COMPUTE_ERROR_ON(a.num_dimensions() > 4); |
| 1161 | |
| 1162 | TensorShape shape_out{ a.tensor_shape() }; |
| 1163 | shape_out.set(axis, num_tensors); |
| 1164 | |
| 1165 | unsigned int i_shift = 0; |
| 1166 | |
| 1167 | for(unsigned int i = 0; i < a.num_dimensions(); ++i) |
| 1168 | { |
| 1169 | if(i == axis) |
| 1170 | { |
| 1171 | i_shift++; |
| 1172 | } |
| 1173 | |
| 1174 | shape_out.set(i + i_shift, a.tensor_shape()[i]); |
| 1175 | } |
| 1176 | return shape_out; |
| 1177 | } |
Manuel Bottini | 8529bd6 | 2018-11-21 11:53:04 +0000 | [diff] [blame] | 1178 | |
| 1179 | inline TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis) |
| 1180 | { |
| 1181 | ARM_COMPUTE_ERROR_ON(indices_shape.num_dimensions() > 1); |
| 1182 | ARM_COMPUTE_ERROR_ON(input_shape.num_dimensions() > 4); |
| 1183 | ARM_COMPUTE_ERROR_ON(actual_axis >= input_shape.num_dimensions()); |
| 1184 | |
| 1185 | TensorShape output_shape = input_shape; |
| 1186 | output_shape[actual_axis] = indices_shape[0]; |
| 1187 | |
| 1188 | return output_shape; |
| 1189 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 1190 | } // namespace shape_calculator |
| 1191 | } // namespace misc |
| 1192 | } // namespace arm_compute |
| 1193 | #endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */ |