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 | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 26 | #include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/PixelValue.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 32 | #include "support/ToolchainSupport.h" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | #include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp" |
| 37 | #include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp" |
| 38 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <cmath> |
| 41 | #include <tuple> |
| 42 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 43 | namespace arm_compute |
| 44 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 45 | NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 46 | : _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] | 47 | { |
| 48 | } |
| 49 | |
| 50 | void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW) |
| 51 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output); |
| 54 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output); |
| 55 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 56 | |
| 57 | if(biases != nullptr) |
| 58 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases); |
| 60 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases); |
| 61 | ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 62 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 63 | } |
| 64 | |
| 65 | // Check if bias are present, if yes they will be embedded to the weights matrix |
| 66 | const bool _has_bias = (biases != nullptr); |
| 67 | |
| 68 | _transpose1xW = transpose1xW; |
| 69 | |
| 70 | if(transpose1xW) |
| 71 | { |
| 72 | // Create tensor to store the reshaped weights |
| 73 | const unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 74 | const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0); |
| 75 | TensorShape shape_wr(mat_weights_cols, mat_weights_rows); |
| 76 | TensorInfo info_wr(shape_wr, 1, weights->info()->data_type(), weights->info()->fixed_point_position()); |
| 77 | |
| 78 | _weights_reshaped.allocator()->init(info_wr); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 79 | _memory_group.manage(&_weights_reshaped); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 80 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped); |
| 82 | _weights_transposed_kernel.configure(&_weights_reshaped, output); |
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_reshaped.allocator()->allocate(); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | _weights_reshape_kernel.configure(weights, biases, output); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void NEConvolutionLayerReshapeWeights::run() |
| 93 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 94 | _memory_group.acquire(); |
| 95 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | NEScheduler::get().schedule(&_weights_reshape_kernel, 3); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 97 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | if(_transpose1xW) |
| 99 | { |
| 100 | NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY); |
| 101 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 102 | |
| 103 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 106 | NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 107 | : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_optimised_kernel(nullptr), _output_col2im_kernel(), |
| 108 | _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] | 109 | { |
| 110 | } |
| 111 | |
| 112 | void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info) |
| 113 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 114 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights); |
| 116 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2)); |
| 118 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4); |
| 119 | |
| 120 | if(biases != nullptr) |
| 121 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 123 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 124 | ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3)); |
| 125 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
| 126 | } |
| 127 | |
| 128 | const DataType dt = input->info()->data_type(); |
| 129 | const int fixed_point_position = input->info()->fixed_point_position(); |
| 130 | |
| 131 | _has_bias = (biases != nullptr); |
| 132 | _are_weights_reshaped = weights_info.are_reshaped(); |
| 133 | |
| 134 | // Get parameters from conv_info |
| 135 | unsigned int stride_x = 0; |
| 136 | unsigned int stride_y = 0; |
| 137 | unsigned int pad_x = 0; |
| 138 | unsigned int pad_y = 0; |
| 139 | std::tie(stride_x, stride_y) = conv_info.stride(); |
| 140 | std::tie(pad_x, pad_y) = conv_info.pad(); |
| 141 | |
| 142 | // Get convolved dimensions |
| 143 | unsigned int conv_w = 0; |
| 144 | unsigned int conv_h = 0; |
| 145 | |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 146 | const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0); |
| 147 | const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1); |
| 148 | std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height, |
| 149 | conv_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 151 | // Check if its a "fully connected" convolution, i.e. the output size is 1x1xnum_kernels |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1)); |
| 153 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 154 | #if defined(__aarch64__) |
| 155 | if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32) |
| 156 | { |
| 157 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>(); |
| 158 | } |
| 159 | #endif /* defined(__aarch64__) */ |
| 160 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | unsigned int mat_weights_cols = weights->info()->dimension(3); |
| 162 | unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0); |
| 163 | |
| 164 | // Reshape weights if needed |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 165 | if(_mm_optimised_kernel != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 167 | if(_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 168 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 169 | mat_weights_cols = weights_info.num_kernels(); |
| 170 | mat_weights_rows = weights->info()->dimension(1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 174 | TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows }; |
| 175 | |
| 176 | // Create tensor to store the reshaped weights |
| 177 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position)); |
| 178 | _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */); |
| 179 | weights = &_weights_reshaped; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 181 | } |
| 182 | else |
| 183 | { |
| 184 | if(_are_weights_reshaped) |
| 185 | { |
| 186 | mat_weights_cols = weights_info.num_kernels(); |
| 187 | mat_weights_rows = weights->info()->dimension(0) / 4 + (_has_bias ? 1 : 0); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | TensorShape reshaped_weights_shape; |
| 192 | |
| 193 | if(_is_fully_connected_convolution) |
| 194 | { |
| 195 | reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows }; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | // Create tensor to store transposed weights |
| 200 | const float transpose_width = 16.0f / input->info()->element_size(); |
| 201 | reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width), |
| 202 | static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) }; |
| 203 | } |
| 204 | |
| 205 | // Create tensor to store the reshaped weights |
| 206 | _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position)); |
| 207 | _reshape_weights.configure(weights, biases, &_weights_reshaped, !_is_fully_connected_convolution /* 1xW transpose */); |
| 208 | weights = &_weights_reshaped; |
| 209 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Create tensor to store im2col reshaped inputs |
| 213 | const unsigned int mat_input_cols = mat_weights_rows; |
| 214 | const unsigned int mat_input_rows = conv_w * conv_h; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 215 | |
| 216 | TensorShape shape_im2col(input->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | shape_im2col.set(0, mat_input_cols); |
| 218 | shape_im2col.set(1, mat_input_rows); |
| 219 | shape_im2col.set(2, 1); |
| 220 | _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 221 | _memory_group.manage(&_input_im2col_reshaped); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 222 | |
| 223 | // Create tensor (interleave) to prepare input tensor for GEMM |
| 224 | if(!_is_fully_connected_convolution) |
| 225 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 226 | TensorShape shape_interleaved(shape_im2col); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 227 | shape_interleaved.set(0, shape_interleaved.x() * 4); |
| 228 | shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f)); |
| 229 | _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 230 | _memory_group.manage(&_input_interleaved_reshaped); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Create GEMM output tensor |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 234 | TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | shape_gemm.set(0, mat_weights_cols); |
| 236 | shape_gemm.set(1, mat_input_rows); |
| 237 | _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 238 | _memory_group.manage(&_gemm_output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 239 | |
| 240 | // Configure kernels |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 241 | _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^] | 242 | |
| 243 | #if defined(__aarch64__) |
| 244 | if(_mm_optimised_kernel != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 246 | struct CPUInfo ci = NEScheduler::get().cpu_info(); |
| 247 | |
| 248 | const int M = _gemm_output.info()->tensor_shape().y(); |
| 249 | const int N = _gemm_output.info()->tensor_shape().x(); |
| 250 | const int K = _input_im2col_reshaped.info()->tensor_shape().x(); |
| 251 | |
| 252 | GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false); |
| 253 | |
| 254 | constexpr size_t alignment = 4096; |
| 255 | _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8)); |
| 256 | _memory_group.manage(&_workspace); |
| 257 | |
| 258 | // Configure matrix multiplication kernel |
| 259 | if(_is_fully_connected_convolution) |
| 260 | { |
| 261 | _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace, 1.f, 0.f, false, false); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace); |
| 266 | } |
| 267 | |
| 268 | _workspace.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 269 | } |
| 270 | else |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 271 | #endif /* defined(__aarch64__) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 272 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 273 | if(_is_fully_connected_convolution) |
| 274 | { |
| 275 | _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped); |
| 280 | _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f); |
| 281 | _input_interleaved_reshaped.allocator()->allocate(); |
| 282 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 283 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 284 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 285 | _input_im2col_reshaped.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 286 | _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h)); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 287 | _gemm_output.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 288 | |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 289 | ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one"); |
| 290 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | // Allocate intermediate tensor |
| 292 | if(!_are_weights_reshaped) |
| 293 | { |
| 294 | _weights_reshaped.allocator()->allocate(); |
| 295 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void NEConvolutionLayer::run() |
| 299 | { |
| 300 | // Run weights reshaping (Runs once for every configure) |
| 301 | if(!_are_weights_reshaped) |
| 302 | { |
| 303 | _are_weights_reshaped = true; |
| 304 | _reshape_weights.run(); |
| 305 | } |
| 306 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 307 | _memory_group.acquire(); |
| 308 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 309 | // Run input reshaping |
| 310 | NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 311 | |
| 312 | // Runs matrix multiply on reshaped matrices |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 313 | if(_mm_optimised_kernel != nullptr) |
| 314 | { |
| 315 | NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | if(!_is_fully_connected_convolution) |
| 320 | { |
| 321 | // Run interleave |
| 322 | NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY); |
| 323 | } |
| 324 | |
| 325 | NEScheduler::get().schedule(&_mm_kernel, Window::DimY); |
| 326 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 327 | |
| 328 | // Reshape output matrix |
| 329 | NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 330 | |
| 331 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 332 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame^] | 333 | } // namespace arm_compute |