Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ |
| 25 | #define __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ |
| 26 | |
Georgios Pinitas | 9be0c5a | 2018-02-19 12:46:29 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensorInfo.h" |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Utils.h" |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace misc |
| 34 | { |
| 35 | namespace shape_calculator |
| 36 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 37 | inline TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm) |
| 38 | { |
| 39 | TensorShape output_shape = input.tensor_shape(); |
| 40 | permute(output_shape, perm); |
| 41 | return output_shape; |
| 42 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 43 | inline TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias = false) |
| 44 | { |
| 45 | // Calculate output shape |
| 46 | TensorShape weights_reshaped{ weights.tensor_shape() }; |
| 47 | weights_reshaped.collapse(3); |
| 48 | const size_t tmp_dim = weights_reshaped[0]; |
| 49 | weights_reshaped.set(0, weights_reshaped[1]); |
| 50 | weights_reshaped.set(1, tmp_dim + (has_bias ? 1 : 0)); |
| 51 | |
| 52 | return weights_reshaped; |
| 53 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 54 | inline TensorShape compute_interleaved_shape(const ITensorInfo &a, int mult_interleave4x4_height = 1) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 55 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 56 | // The interleaved output matrix will have the following shape: [ a_height * W, ceil(a_width / W) ] where W = 4 * mult_interleave4x4_height |
| 57 | ARM_COMPUTE_ERROR_ON(mult_interleave4x4_height < 1); |
| 58 | const int interleave_width = 4 * mult_interleave4x4_height; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 59 | TensorShape shape_interleaved_a{ a.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 60 | shape_interleaved_a.set(0, a.dimension(0) * interleave_width); |
| 61 | shape_interleaved_a.set(1, std::ceil(a.dimension(1) / static_cast<float>(interleave_width))); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 62 | |
| 63 | return shape_interleaved_a; |
| 64 | } |
| 65 | inline TensorShape compute_transpose1xW_shape(const ITensorInfo &b) |
| 66 | { |
| 67 | // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ] |
| 68 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
| 69 | shape_transposed1xW_b.set(0, b.dimension(1) * 16); |
| 70 | shape_transposed1xW_b.set(1, std::ceil(b.dimension(0) / 16.f)); |
| 71 | |
| 72 | return shape_transposed1xW_b; |
| 73 | } |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 74 | 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] | 75 | { |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 76 | // Note: mult_transpose1xW_width expresses the number of chunks with size 1x(W) we want to store on the same row |
| 77 | // The transpose1xW output matrix will have the following shape: |
| 78 | // [ b_height * W, ceil(b_width / W) ] where W = (16 / element size of the tensor) * mult_transpose1xW_width |
| 79 | ARM_COMPUTE_ERROR_ON(mult_transpose1xW_width < 1); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 80 | TensorShape shape_transposed1xW_b{ b.tensor_shape() }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 81 | const size_t transpose_width = (16 / b.element_size()) * mult_transpose1xW_width; |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 82 | shape_transposed1xW_b.set(0, b.dimension(1) * transpose_width); |
| 83 | shape_transposed1xW_b.set(1, static_cast<size_t>(std::ceil(b.dimension(0) / static_cast<float>(transpose_width)))); |
| 84 | |
| 85 | return shape_transposed1xW_b; |
| 86 | } |
| 87 | inline TensorShape compute_reductionA_shape(const ITensorInfo &b) |
| 88 | { |
| 89 | TensorShape shape_vector_sum_col{ b.tensor_shape() }; |
| 90 | if(shape_vector_sum_col.num_dimensions() > 1) |
| 91 | { |
| 92 | shape_vector_sum_col.remove_dimension(1); |
| 93 | } |
| 94 | |
| 95 | return shape_vector_sum_col; |
| 96 | } |
| 97 | inline TensorShape compute_reductionB_shape(const ITensorInfo &a) |
| 98 | { |
| 99 | TensorShape shape_vector_sum_row{ a.tensor_shape() }; |
| 100 | shape_vector_sum_row.set(Window::DimX, a.dimension(1)); |
| 101 | if(a.num_dimensions() > 1) |
| 102 | { |
| 103 | shape_vector_sum_row.remove_dimension(1); |
| 104 | } |
| 105 | |
| 106 | return shape_vector_sum_row; |
| 107 | } |
| 108 | inline TensorShape compute_im2col_shape(const ITensorInfo &input) |
| 109 | { |
| 110 | TensorShape shape_im2col{ input.tensor_shape() }; |
| 111 | shape_im2col.collapse(3); |
| 112 | |
| 113 | return shape_im2col; |
| 114 | } |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 115 | inline TensorShape compute_col2im_shape(const ITensorInfo &input, std::pair<unsigned int, unsigned int> convolved_dims) |
| 116 | { |
| 117 | TensorShape col2im_shape{ input.tensor_shape() }; |
| 118 | col2im_shape.set(0, convolved_dims.first); |
| 119 | col2im_shape.set(1, convolved_dims.second); |
| 120 | col2im_shape.set(2, input.tensor_shape()[0]); |
| 121 | |
| 122 | return col2im_shape; |
| 123 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 124 | inline TensorShape compute_transposed_shape(const ITensorInfo &input) |
| 125 | { |
| 126 | TensorShape shape_transposed{ input.tensor_shape() }; |
| 127 | |
| 128 | shape_transposed.set(0, input.dimension(1)); |
| 129 | shape_transposed.set(1, input.dimension(0)); |
| 130 | |
| 131 | return shape_transposed; |
| 132 | } |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 133 | inline TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info) |
| 134 | { |
| 135 | const TensorShape input_shape{ input.tensor_shape() }; |
| 136 | const TensorShape weights_shape{ weights.tensor_shape() }; |
| 137 | |
| 138 | unsigned int output_width = 0; |
| 139 | unsigned int output_height = 0; |
Georgios Pinitas | d05dce4 | 2018-01-22 16:29:17 +0000 | [diff] [blame] | 140 | std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), |
| 141 | weights_shape.x(), weights_shape.y(), |
| 142 | conv_info); |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 143 | |
| 144 | TensorShape output_shape{ input_shape }; |
| 145 | output_shape.set(0, output_width); |
| 146 | output_shape.set(1, output_height); |
| 147 | |
| 148 | return output_shape; |
| 149 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 150 | inline TensorShape compute_deconvolution_shape(const ITensorInfo &input, unsigned int sx, unsigned int sy, unsigned int inner_border_right, unsigned int inner_border_top, const PadStrideInfo &info) |
| 151 | { |
| 152 | TensorShape scale_out_shape(input.tensor_shape()); |
| 153 | const unsigned int out_x = input.dimension(0) + (input.dimension(0) - 1) * (sx - 1) + inner_border_right + 2 * info.pad().first; |
| 154 | const unsigned int out_y = input.dimension(1) + (input.dimension(1) - 1) * (sy - 1) + inner_border_top + 2 * info.pad().second; |
| 155 | scale_out_shape.set(0, out_x); |
| 156 | scale_out_shape.set(1, out_y); |
| 157 | |
| 158 | return scale_out_shape; |
| 159 | } |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 160 | inline TensorShape compute_im2col_shape(const ITensorInfo *input, const int num_input_dimensions = 3) |
| 161 | { |
| 162 | TensorShape output_shape{ input->tensor_shape() }; |
| 163 | |
| 164 | output_shape.collapse(num_input_dimensions); |
| 165 | |
| 166 | return output_shape; |
| 167 | } |
| 168 | inline TensorShape compute_interleave_custom_shape(const TensorShape &input, const int x_interleave, const int y_interleave) |
| 169 | { |
| 170 | TensorShape output_shape{ input }; |
| 171 | |
| 172 | output_shape.set(0, output_shape.x() * x_interleave); |
| 173 | output_shape.set(1, std::ceil(output_shape.y() / static_cast<float>(y_interleave))); |
| 174 | |
| 175 | return output_shape; |
| 176 | } |
| 177 | |
| 178 | inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave) |
| 179 | { |
| 180 | TensorShape output_shape{ input->tensor_shape() }; |
| 181 | |
| 182 | // Transpose weights if the user hasn't done it |
| 183 | if(transpose_weights) |
| 184 | { |
| 185 | output_shape = compute_transposed_shape(*input); |
| 186 | } |
| 187 | |
| 188 | // If the we run multiple batches we need 1xW transpose, too. |
| 189 | if(is_batched_fc_layer) |
| 190 | { |
| 191 | output_shape = compute_transposed_shape(input->clone()->set_tensor_shape(output_shape)); |
| 192 | output_shape = compute_interleave_custom_shape(output_shape, interleave, interleave); |
| 193 | } |
| 194 | |
| 195 | return output_shape; |
| 196 | } |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 197 | |
| 198 | inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const PadStrideInfo &conv_info, const Size2D &kernel_size) |
| 199 | { |
| 200 | // Compute height |
| 201 | const unsigned int num_tiles_x = std::ceil((input.tensor_shape().x() - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right()) / 2.f); |
| 202 | const unsigned int num_tiles_y = std::ceil((input.tensor_shape().y() - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom()) / 2.f); |
| 203 | |
| 204 | const unsigned int width = input.tensor_shape()[get_data_layout_dimension_index(input.data_layout(), DataLayoutDimension::CHANNEL)]; |
| 205 | const unsigned int height = num_tiles_x * num_tiles_y; |
| 206 | const unsigned int depth = 16; // COMPMID-990 |
| 207 | |
| 208 | TensorShape output_shape{ input.tensor_shape() }; |
| 209 | output_shape.set(0, width); |
| 210 | output_shape.set(1, height); |
| 211 | output_shape.set(2, depth); |
| 212 | |
| 213 | return output_shape; |
| 214 | } |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 215 | } // namespace shape_calculator |
| 216 | } // namespace misc |
| 217 | } // namespace arm_compute |
| 218 | #endif /* __ARM_COMPUTE_MISC_SHAPE_CALCULATOR_H__ */ |