blob: 25c639f7ea393d627d952d5c89f06c2af9259c9d [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
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
Kaizen8938bd32017-09-28 14:38:23 +010026#include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h"
27#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000028#include "arm_compute/core/PixelValue.h"
Kaizen8938bd32017-09-28 14:38:23 +010029#include "arm_compute/core/Size2D.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000030#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
Kaizen8938bd32017-09-28 14:38:23 +010033#include "support/ToolchainSupport.h"
34
35namespace arm_compute
36{
37#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
38#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
39#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
40} // namespace arm_compute
Anthony Barbier871448e2017-03-24 14:54:29 +000041
42#include <cmath>
43#include <tuple>
44
Kaizen8938bd32017-09-28 14:38:23 +010045namespace arm_compute
46{
47NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
48 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000049{
50}
51
Anthony Barbierdbdab852017-06-23 15:42:00 +010052void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
Anthony Barbier871448e2017-03-24 14:54:29 +000053{
Kaizen8938bd32017-09-28 14:38:23 +010054 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbierdbdab852017-06-23 15:42:00 +010055 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
Anthony Barbier871448e2017-03-24 14:54:29 +000057 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
58
59 if(biases != nullptr)
60 {
Anthony Barbierdbdab852017-06-23 15:42:00 +010061 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
62 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
Anthony Barbier871448e2017-03-24 14:54:29 +000063 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
64 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
65 }
66
Anthony Barbierdbdab852017-06-23 15:42:00 +010067 // Check if bias are present, if yes they will be embedded to the weights matrix
68 const bool _has_bias = (biases != nullptr);
Anthony Barbier871448e2017-03-24 14:54:29 +000069
Anthony Barbierdbdab852017-06-23 15:42:00 +010070 _transpose1xW = transpose1xW;
71
72 if(transpose1xW)
73 {
74 // Create tensor to store the reshaped weights
75 const unsigned int mat_weights_cols = weights->info()->dimension(3);
76 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
77 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
78 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type(), weights->info()->fixed_point_position());
79
80 _weights_reshaped.allocator()->init(info_wr);
Kaizen8938bd32017-09-28 14:38:23 +010081 _memory_group.manage(&_weights_reshaped);
82
Anthony Barbierdbdab852017-06-23 15:42:00 +010083 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
84 _weights_transposed_kernel.configure(&_weights_reshaped, output);
Kaizen8938bd32017-09-28 14:38:23 +010085
Anthony Barbierdbdab852017-06-23 15:42:00 +010086 _weights_reshaped.allocator()->allocate();
87 }
88 else
89 {
90 _weights_reshape_kernel.configure(weights, biases, output);
91 }
92}
93
94void NEConvolutionLayerReshapeWeights::run()
95{
Kaizen8938bd32017-09-28 14:38:23 +010096 _memory_group.acquire();
97
Anthony Barbierdbdab852017-06-23 15:42:00 +010098 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Kaizen8938bd32017-09-28 14:38:23 +010099
Anthony Barbierdbdab852017-06-23 15:42:00 +0100100 if(_transpose1xW)
101 {
102 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
103 }
Kaizen8938bd32017-09-28 14:38:23 +0100104
105 _memory_group.release();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100106}
107
Kaizen8938bd32017-09-28 14:38:23 +0100108NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
109 : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_optimised_kernel(nullptr), _output_col2im_kernel(),
110 _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _workspace(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100111{
112}
113
114void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
115{
Kaizen8938bd32017-09-28 14:38:23 +0100116 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
117 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
118 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100119 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
120 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
121
122 if(biases != nullptr)
123 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100124 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
125 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
126 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
127 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
128 }
129
130 const DataType dt = input->info()->data_type();
131 const int fixed_point_position = input->info()->fixed_point_position();
132
133 _has_bias = (biases != nullptr);
134 _are_weights_reshaped = weights_info.are_reshaped();
135
136 // Get parameters from conv_info
Anthony Barbier46d59272017-05-04 09:15:15 +0100137 unsigned int stride_x = 0;
138 unsigned int stride_y = 0;
Anthony Barbier871448e2017-03-24 14:54:29 +0000139 std::tie(stride_x, stride_y) = conv_info.stride();
Anthony Barbier871448e2017-03-24 14:54:29 +0000140
Anthony Barbier871448e2017-03-24 14:54:29 +0000141 // Get convolved dimensions
142 unsigned int conv_w = 0;
143 unsigned int conv_h = 0;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100144
Kaizen8938bd32017-09-28 14:38:23 +0100145 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
146 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
147 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
148 conv_info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000149
Kaizen8938bd32017-09-28 14:38:23 +0100150 // Check if its a "fully connected" convolution, i.e. the output size is 1x1xnum_kernels
Anthony Barbierdbdab852017-06-23 15:42:00 +0100151 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Anthony Barbier871448e2017-03-24 14:54:29 +0000152
Kaizen8938bd32017-09-28 14:38:23 +0100153#if defined(__arm__)
154 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
155 {
156 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
157 }
158#elif defined(__aarch64__)
159 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
160 {
161 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
162 }
163#endif /* defined(__arm__) || defined(__aarch64__) */
164
Anthony Barbierdbdab852017-06-23 15:42:00 +0100165 unsigned int mat_weights_cols = weights->info()->dimension(3);
166 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
167
168 // Reshape weights if needed
Kaizen8938bd32017-09-28 14:38:23 +0100169 if(_mm_optimised_kernel != nullptr)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100170 {
Kaizen8938bd32017-09-28 14:38:23 +0100171 if(_are_weights_reshaped)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100172 {
Kaizen8938bd32017-09-28 14:38:23 +0100173 mat_weights_cols = weights_info.num_kernels();
174 mat_weights_rows = weights->info()->dimension(1);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100175 }
176 else
177 {
Kaizen8938bd32017-09-28 14:38:23 +0100178 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
179
180 // Create tensor to store the reshaped weights
181 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
182 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
183 weights = &_weights_reshaped;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100184 }
Kaizen8938bd32017-09-28 14:38:23 +0100185 }
186 else
187 {
188 if(_are_weights_reshaped)
189 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000190 if(_is_fully_connected_convolution)
191 {
192 mat_weights_cols = weights_info.num_kernels();
193 mat_weights_rows = weights->info()->dimension(1);
194 }
195 else
196 {
197 const unsigned int transpose_width = 16 / input->info()->element_size();
198 mat_weights_cols = weights_info.num_kernels();
199 mat_weights_rows = weights->info()->dimension(0) / transpose_width + (_has_bias ? 1 : 0);
200 }
Kaizen8938bd32017-09-28 14:38:23 +0100201 }
202 else
203 {
204 TensorShape reshaped_weights_shape;
205
206 if(_is_fully_connected_convolution)
207 {
208 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
209 }
210 else
211 {
212 // Create tensor to store transposed weights
213 const float transpose_width = 16.0f / input->info()->element_size();
214 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
215 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
216 }
217
218 // Create tensor to store the reshaped weights
219 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
220 _reshape_weights.configure(weights, biases, &_weights_reshaped, !_is_fully_connected_convolution /* 1xW transpose */);
221 weights = &_weights_reshaped;
222 }
Anthony Barbierdbdab852017-06-23 15:42:00 +0100223 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000224
225 // Create tensor to store im2col reshaped inputs
Anthony Barbier46d59272017-05-04 09:15:15 +0100226 const unsigned int mat_input_cols = mat_weights_rows;
227 const unsigned int mat_input_rows = conv_w * conv_h;
Kaizen8938bd32017-09-28 14:38:23 +0100228
229 TensorShape shape_im2col(input->info()->tensor_shape());
Anthony Barbier871448e2017-03-24 14:54:29 +0000230 shape_im2col.set(0, mat_input_cols);
231 shape_im2col.set(1, mat_input_rows);
232 shape_im2col.set(2, 1);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100233 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
Kaizen8938bd32017-09-28 14:38:23 +0100234 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier871448e2017-03-24 14:54:29 +0000235
Anthony Barbierdbdab852017-06-23 15:42:00 +0100236 // Create tensor (interleave) to prepare input tensor for GEMM
Kaizen8938bd32017-09-28 14:38:23 +0100237 if(!_is_fully_connected_convolution && _mm_optimised_kernel == nullptr)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100238 {
Kaizen8938bd32017-09-28 14:38:23 +0100239 TensorShape shape_interleaved(shape_im2col);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100240 shape_interleaved.set(0, shape_interleaved.x() * 4);
241 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
242 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
Kaizen8938bd32017-09-28 14:38:23 +0100243 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100244 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000245
246 // Create GEMM output tensor
Kaizen8938bd32017-09-28 14:38:23 +0100247 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
Anthony Barbier871448e2017-03-24 14:54:29 +0000248 shape_gemm.set(0, mat_weights_cols);
249 shape_gemm.set(1, mat_input_rows);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100250 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
Kaizen8938bd32017-09-28 14:38:23 +0100251 _memory_group.manage(&_gemm_output);
Anthony Barbier871448e2017-03-24 14:54:29 +0000252
253 // Configure kernels
Kaizen8938bd32017-09-28 14:38:23 +0100254 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
255
256#if defined(__arm__) || defined(__aarch64__)
257 if(_mm_optimised_kernel != nullptr)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100258 {
Kaizen8938bd32017-09-28 14:38:23 +0100259 struct CPUInfo ci = NEScheduler::get().cpu_info();
260
261 const int M = _gemm_output.info()->tensor_shape().y();
262 const int N = _gemm_output.info()->tensor_shape().x();
263 const int K = _input_im2col_reshaped.info()->tensor_shape().x();
264
265#if defined(__arm__)
266 GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false);
267#elif defined(__aarch64__)
268 GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false);
269#endif /* defined(__arm__) || defined(__aarch64__) */
270
271 constexpr size_t alignment = 4096;
272 _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
273 _memory_group.manage(&_workspace);
274
275 // Configure matrix multiplication kernel
276 if(_is_fully_connected_convolution)
277 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000278 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace, 1.f, 0.f);
Kaizen8938bd32017-09-28 14:38:23 +0100279 }
280 else
281 {
282 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace);
283 }
284
285 _workspace.allocator()->allocate();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100286 }
287 else
Kaizen8938bd32017-09-28 14:38:23 +0100288#endif /* defined(__arm__) || defined(__aarch64__) */
Anthony Barbierdbdab852017-06-23 15:42:00 +0100289 {
Kaizen8938bd32017-09-28 14:38:23 +0100290 if(_is_fully_connected_convolution)
291 {
292 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
293 }
294 else
295 {
296 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
297 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
298 _input_interleaved_reshaped.allocator()->allocate();
299 }
Anthony Barbierdbdab852017-06-23 15:42:00 +0100300 }
Kaizen8938bd32017-09-28 14:38:23 +0100301
302 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000303 _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h));
Kaizen8938bd32017-09-28 14:38:23 +0100304 _gemm_output.allocator()->allocate();
305
306 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");
Anthony Barbier871448e2017-03-24 14:54:29 +0000307
Anthony Barbierdbdab852017-06-23 15:42:00 +0100308 // Allocate intermediate tensor
309 if(!_are_weights_reshaped)
310 {
311 _weights_reshaped.allocator()->allocate();
312 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000313}
314
315void NEConvolutionLayer::run()
316{
317 // Run weights reshaping (Runs once for every configure)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100318 if(!_are_weights_reshaped)
Anthony Barbier871448e2017-03-24 14:54:29 +0000319 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100320 _are_weights_reshaped = true;
321 _reshape_weights.run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000322 }
323
Kaizen8938bd32017-09-28 14:38:23 +0100324 _memory_group.acquire();
325
Anthony Barbier871448e2017-03-24 14:54:29 +0000326 // Run input reshaping
Anthony Barbierdbdab852017-06-23 15:42:00 +0100327 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
Anthony Barbier871448e2017-03-24 14:54:29 +0000328
Anthony Barbierdbdab852017-06-23 15:42:00 +0100329 // Runs matrix multiply on reshaped matrices
Kaizen8938bd32017-09-28 14:38:23 +0100330 if(_mm_optimised_kernel != nullptr)
331 {
332 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
333 }
334 else
335 {
336 if(!_is_fully_connected_convolution)
337 {
338 // Run interleave
339 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
340 }
341
342 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
343 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000344
345 // Reshape output matrix
Anthony Barbierdbdab852017-06-23 15:42:00 +0100346 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Kaizen8938bd32017-09-28 14:38:23 +0100347
348 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000349}
Kaizen8938bd32017-09-28 14:38:23 +0100350} // namespace arm_compute