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