Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | #include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" |
| 25 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/PixelValue.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 33 | #include "support/ToolchainSupport.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | #include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp" |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 38 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 39 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp" |
| 40 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | |
| 42 | #include <cmath> |
| 43 | #include <tuple> |
| 44 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 47 | namespace |
| 48 | { |
| 49 | TensorShape get_reshaped_weights_shape(const ITensorInfo *weights, bool has_bias) |
| 50 | { |
| 51 | const unsigned int mat_weights_cols = weights->dimension(3); |
| 52 | const unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0); |
| 53 | return TensorShape(mat_weights_cols, mat_weights_rows); |
| 54 | } |
| 55 | } // namespace |
| 56 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 57 | NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 58 | : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW) |
| 63 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 64 | // Perform validation step |
| 65 | ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output); |
| 66 | ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(), |
| 67 | (biases != nullptr) ? biases->info() : nullptr, |
| 68 | output->info(), |
| 69 | transpose1xW)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | |
| 71 | // Check if bias are present, if yes they will be embedded to the weights matrix |
| 72 | const bool _has_bias = (biases != nullptr); |
| 73 | |
| 74 | _transpose1xW = transpose1xW; |
| 75 | |
| 76 | if(transpose1xW) |
| 77 | { |
| 78 | // Create tensor to store the reshaped weights |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 79 | TensorInfo info_wr = weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(get_reshaped_weights_shape(weights->info(), _has_bias)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | |
| 81 | _weights_reshaped.allocator()->init(info_wr); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 82 | _memory_group.manage(&_weights_reshaped); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 83 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped); |
| 85 | _weights_transposed_kernel.configure(&_weights_reshaped, output); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 86 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | _weights_reshaped.allocator()->allocate(); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | _weights_reshape_kernel.configure(weights, biases, output); |
| 92 | } |
| 93 | } |
| 94 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 95 | Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW) |
| 96 | { |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 101 | |
| 102 | if(biases != nullptr) |
| 103 | { |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 108 | } |
| 109 | |
| 110 | // Check if bias are present, if yes they will be embedded to the weights matrix |
| 111 | const bool has_bias = (biases != nullptr); |
| 112 | |
| 113 | // Checks performed when biases are present |
| 114 | if(has_bias) |
| 115 | { |
| 116 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 117 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3)); |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 119 | } |
| 120 | |
| 121 | if(transpose1xW) |
| 122 | { |
| 123 | TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, has_bias)); |
| 124 | ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped)); |
| 125 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output)); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output)); |
| 130 | } |
| 131 | |
| 132 | return Status{}; |
| 133 | } |
| 134 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | void NEConvolutionLayerReshapeWeights::run() |
| 136 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 137 | _memory_group.acquire(); |
| 138 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | NEScheduler::get().schedule(&_weights_reshape_kernel, 3); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | if(_transpose1xW) |
| 142 | { |
| 143 | NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY); |
| 144 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 145 | |
| 146 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 149 | namespace |
| 150 | { |
| 151 | TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool has_bias, bool is_fully_connected_convolution) |
| 152 | { |
| 153 | unsigned int mat_weights_cols = weights->dimension(3); |
| 154 | unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0); |
| 155 | |
| 156 | if(is_fully_connected_convolution) |
| 157 | { |
| 158 | // Create tensor to store the reshaped weights |
| 159 | return TensorShape(mat_weights_cols, mat_weights_rows); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | // Create tensor to store transposed weights |
| 164 | const float transpose_width = 16.0f / weights->element_size(); |
| 165 | return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width))); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, DataType &dt, |
| 170 | bool &has_bias, |
| 171 | bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height, bool &is_fully_connected_convolution, unsigned int &mat_weights_cols, unsigned int &mat_weights_rows, |
| 172 | unsigned int &conv_w, unsigned int &conv_h) |
| 173 | { |
| 174 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 175 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 176 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
| 177 | ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(2) != input->dimension(2)); |
| 178 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4); |
| 179 | |
| 180 | if(biases != nullptr) |
| 181 | { |
| 182 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 183 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 184 | ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3)); |
| 185 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 186 | } |
| 187 | |
| 188 | dt = input->data_type(); |
| 189 | has_bias = (biases != nullptr); |
| 190 | are_weights_reshaped = weights_info.are_reshaped(); |
| 191 | kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(0); |
| 192 | kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(1); |
| 193 | mat_weights_cols = weights->dimension(3); |
| 194 | mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (has_bias ? 1 : 0); |
| 195 | |
| 196 | std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, |
| 197 | conv_info); |
| 198 | |
| 199 | is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1)); |
| 200 | |
| 201 | return Status{}; |
| 202 | } |
| 203 | } // namespace |
| 204 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 205 | NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 206 | : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_optimised_kernel(nullptr), _output_col2im_kernel(), |
| 207 | _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _workspace(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | { |
| 209 | } |
| 210 | |
| 211 | void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 212 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 213 | // Perform validate step |
| 214 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 215 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 216 | DataType dt{}; |
| 217 | unsigned int kernel_width = 0; |
| 218 | unsigned int kernel_height = 0; |
| 219 | unsigned int mat_weights_cols = 0; |
| 220 | unsigned int mat_weights_rows = 0; |
| 221 | unsigned int conv_w = 0; |
| 222 | unsigned int conv_h = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 224 | Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, dt, _has_bias, _are_weights_reshaped, |
| 225 | kernel_width, kernel_height, |
| 226 | _is_fully_connected_convolution, |
| 227 | mat_weights_cols, mat_weights_rows, conv_w, conv_h); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 229 | ARM_COMPUTE_ERROR_THROW_ON(status); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 231 | const unsigned int fixed_point_position = input->info()->fixed_point_position(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 233 | #if defined(__arm__) |
| 234 | if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32) |
| 235 | { |
| 236 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>(); |
| 237 | } |
| 238 | #elif defined(__aarch64__) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 239 | if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32) |
| 240 | { |
| 241 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>(); |
| 242 | } |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 243 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 244 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | // Reshape weights if needed |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 246 | if(_mm_optimised_kernel != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 247 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 248 | if(_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 249 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 250 | mat_weights_cols = weights_info.num_kernels(); |
| 251 | mat_weights_rows = weights->info()->dimension(1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 255 | TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows }; |
| 256 | |
| 257 | // Create tensor to store the reshaped weights |
| 258 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position)); |
| 259 | _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */); |
| 260 | weights = &_weights_reshaped; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 262 | } |
| 263 | else |
| 264 | { |
| 265 | if(_are_weights_reshaped) |
| 266 | { |
Georgios Pinitas | b660dcf | 2017-12-13 10:48:06 +0000 | [diff] [blame] | 267 | if(_is_fully_connected_convolution) |
| 268 | { |
| 269 | mat_weights_cols = weights_info.num_kernels(); |
| 270 | mat_weights_rows = weights->info()->dimension(1); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | const unsigned int transpose_width = 16 / input->info()->element_size(); |
| 275 | mat_weights_cols = weights_info.num_kernels(); |
| 276 | mat_weights_rows = weights->info()->dimension(0) / transpose_width + (_has_bias ? 1 : 0); |
| 277 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 278 | } |
| 279 | else |
| 280 | { |
| 281 | TensorShape reshaped_weights_shape; |
| 282 | |
| 283 | if(_is_fully_connected_convolution) |
| 284 | { |
| 285 | reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows }; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | // Create tensor to store transposed weights |
| 290 | const float transpose_width = 16.0f / input->info()->element_size(); |
| 291 | reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width), |
| 292 | static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) }; |
| 293 | } |
| 294 | |
| 295 | // Create tensor to store the reshaped weights |
| 296 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position)); |
| 297 | _reshape_weights.configure(weights, biases, &_weights_reshaped, !_is_fully_connected_convolution /* 1xW transpose */); |
| 298 | weights = &_weights_reshaped; |
| 299 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Create tensor to store im2col reshaped inputs |
| 303 | const unsigned int mat_input_cols = mat_weights_rows; |
| 304 | const unsigned int mat_input_rows = conv_w * conv_h; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 305 | |
| 306 | TensorShape shape_im2col(input->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 307 | shape_im2col.set(0, mat_input_cols); |
| 308 | shape_im2col.set(1, mat_input_rows); |
| 309 | shape_im2col.set(2, 1); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 310 | _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 311 | _memory_group.manage(&_input_im2col_reshaped); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | |
| 313 | // Create tensor (interleave) to prepare input tensor for GEMM |
Georgios Pinitas | 1b2e2e5 | 2017-09-28 11:30:27 +0100 | [diff] [blame] | 314 | if(!_is_fully_connected_convolution && _mm_optimised_kernel == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 315 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 316 | TensorShape shape_interleaved(shape_im2col); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 317 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
| 318 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 319 | _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 320 | _memory_group.manage(&_input_interleaved_reshaped); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Create GEMM output tensor |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 324 | TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | shape_gemm.set(0, mat_weights_cols); |
| 326 | shape_gemm.set(1, mat_input_rows); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 327 | _gemm_output.allocator()->init(_input_im2col_reshaped.info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_gemm)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 328 | _memory_group.manage(&_gemm_output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 329 | |
| 330 | // Configure kernels |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 331 | _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 332 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 333 | #if defined(__arm__) || defined(__aarch64__) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 334 | if(_mm_optimised_kernel != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 336 | struct CPUInfo ci = NEScheduler::get().cpu_info(); |
| 337 | |
| 338 | const int M = _gemm_output.info()->tensor_shape().y(); |
| 339 | const int N = _gemm_output.info()->tensor_shape().x(); |
| 340 | const int K = _input_im2col_reshaped.info()->tensor_shape().x(); |
| 341 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 342 | #if defined(__arm__) |
| 343 | GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false); |
| 344 | #elif defined(__aarch64__) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 345 | GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false); |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 346 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 347 | |
| 348 | constexpr size_t alignment = 4096; |
| 349 | _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8)); |
| 350 | _memory_group.manage(&_workspace); |
| 351 | |
| 352 | // Configure matrix multiplication kernel |
Georgios Pinitas | 08c5a06 | 2017-12-14 17:53:39 +0000 | [diff] [blame] | 353 | _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 354 | |
| 355 | _workspace.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 356 | } |
| 357 | else |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 358 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 359 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 360 | if(_is_fully_connected_convolution) |
| 361 | { |
| 362 | _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 367 | _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f); |
| 368 | _input_interleaved_reshaped.allocator()->allocate(); |
| 369 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 371 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 372 | _input_im2col_reshaped.allocator()->allocate(); |
Georgios Pinitas | d912fd8 | 2017-11-27 21:00:13 +0000 | [diff] [blame] | 373 | _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 374 | _gemm_output.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 375 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 376 | // Allocate intermediate tensor |
| 377 | if(!_are_weights_reshaped) |
| 378 | { |
| 379 | _weights_reshaped.allocator()->allocate(); |
| 380 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame^] | 383 | Status NEConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
| 384 | const WeightsInfo &weights_info) |
| 385 | { |
| 386 | DataType dt{}; |
| 387 | bool has_bias{}; |
| 388 | bool are_weights_reshaped{}; |
| 389 | bool is_fully_connected_convolution{}; |
| 390 | unsigned int kernel_width = 0; |
| 391 | unsigned int kernel_height = 0; |
| 392 | unsigned int mat_weights_cols = 0; |
| 393 | unsigned int mat_weights_rows = 0; |
| 394 | unsigned int conv_w = 0; |
| 395 | unsigned int conv_h = 0; |
| 396 | |
| 397 | Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, dt, has_bias, are_weights_reshaped, kernel_width, kernel_height, |
| 398 | is_fully_connected_convolution, mat_weights_cols, mat_weights_rows, |
| 399 | conv_w, conv_h); |
| 400 | |
| 401 | ARM_COMPUTE_RETURN_ON_ERROR(status); |
| 402 | |
| 403 | std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone(); |
| 404 | bool optimised_kernel = false; |
| 405 | |
| 406 | #if defined(__arm__) |
| 407 | if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32) |
| 408 | { |
| 409 | optimised_kernel = true; |
| 410 | } |
| 411 | #elif defined(__aarch64__) |
| 412 | if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32) |
| 413 | { |
| 414 | optimised_kernel = true; |
| 415 | } |
| 416 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
| 417 | |
| 418 | // Reshape weights if needed |
| 419 | if(optimised_kernel) |
| 420 | { |
| 421 | if(are_weights_reshaped) |
| 422 | { |
| 423 | mat_weights_cols = weights_info.num_kernels(); |
| 424 | mat_weights_rows = weights->dimension(1); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows }; |
| 429 | |
| 430 | // Create tensor to store the reshaped weights |
| 431 | reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, has_bias, is_fully_connected_convolution)); |
| 432 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */)); |
| 433 | weights = reshaped_weights.get(); |
| 434 | } |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | if(are_weights_reshaped) |
| 439 | { |
| 440 | const unsigned int transpose_width = 16 / input->element_size(); |
| 441 | mat_weights_cols = weights_info.num_kernels(); |
| 442 | mat_weights_rows = weights->dimension(0) / transpose_width + (has_bias ? 1 : 0); |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | TensorShape reshaped_weights_shape; |
| 447 | |
| 448 | if(is_fully_connected_convolution) |
| 449 | { |
| 450 | reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows }; |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | // Create tensor to store transposed weights |
| 455 | const float transpose_width = 16.0f / input->element_size(); |
| 456 | reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width), |
| 457 | static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) }; |
| 458 | } |
| 459 | |
| 460 | // Create tensor to store the reshaped weights |
| 461 | reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, has_bias, is_fully_connected_convolution)); |
| 462 | ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */)); |
| 463 | weights = reshaped_weights.get(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Validate im2col |
| 468 | const unsigned int mat_input_cols = mat_weights_rows; |
| 469 | const unsigned int mat_input_rows = conv_w * conv_h; |
| 470 | TensorShape shape_im2col = input->tensor_shape(); |
| 471 | shape_im2col.set(0, mat_input_cols); |
| 472 | shape_im2col.set(1, mat_input_rows); |
| 473 | shape_im2col.set(2, 1); |
| 474 | TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col); |
| 475 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, Size2D(weights->dimension(0), weights->dimension(1)), conv_info, has_bias)); |
| 476 | |
| 477 | // Create GEMM output tensor |
| 478 | TensorShape shape_gemm(im2_col_info.tensor_shape()); |
| 479 | shape_gemm.set(0, mat_weights_cols); |
| 480 | shape_gemm.set(1, mat_input_rows); |
| 481 | TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm); |
| 482 | |
| 483 | // Validate GEMM interleave and multiply |
| 484 | if(!is_fully_connected_convolution) |
| 485 | { |
| 486 | TensorShape shape_interleaved = shape_im2col; |
| 487 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
| 488 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
| 489 | TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved); |
| 490 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info)); |
| 491 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info)); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info)); |
| 496 | } |
| 497 | |
| 498 | ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h))); |
| 499 | |
| 500 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 501 | |
| 502 | return Status{}; |
| 503 | } |
| 504 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 505 | void NEConvolutionLayer::run() |
| 506 | { |
| 507 | // Run weights reshaping (Runs once for every configure) |
| 508 | if(!_are_weights_reshaped) |
| 509 | { |
| 510 | _are_weights_reshaped = true; |
| 511 | _reshape_weights.run(); |
| 512 | } |
| 513 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 514 | _memory_group.acquire(); |
| 515 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 516 | // Run input reshaping |
| 517 | NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 518 | |
| 519 | // Runs matrix multiply on reshaped matrices |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 520 | if(_mm_optimised_kernel != nullptr) |
| 521 | { |
| 522 | NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY); |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | if(!_is_fully_connected_convolution) |
| 527 | { |
| 528 | // Run interleave |
| 529 | NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY); |
| 530 | } |
| 531 | |
| 532 | NEScheduler::get().schedule(&_mm_kernel, Window::DimY); |
| 533 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 534 | |
| 535 | // Reshape output matrix |
| 536 | NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 537 | |
| 538 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 539 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 540 | } // namespace arm_compute |