blob: cbe3b65c349ad6b063b141b0c26155d7c2eed1fb [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 Pflanzer80373f62017-09-15 10:42:58 +010026#include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010027#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010029#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010033#include "support/ToolchainSupport.h"
34
35namespace arm_compute
36{
37#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Moritz Pflanzer80373f62017-09-15 10:42:58 +010038#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010039#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
40} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
42#include <cmath>
43#include <tuple>
44
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010045namespace arm_compute
46{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010047NEConvolutionLayerReshapeWeights::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 Barbier6ff3b192017-09-04 18:44:23 +010049{
50}
51
52void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
53{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010054 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
57 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
58
59 if(biases != nullptr)
60 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
62 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
63 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
64 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
65 }
66
67 // Check if bias are present, if yes they will be embedded to the weights matrix
68 const bool _has_bias = (biases != nullptr);
69
70 _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);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010081 _memory_group.manage(&_weights_reshaped);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010082
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
84 _weights_transposed_kernel.configure(&_weights_reshaped, output);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010085
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 _weights_reshaped.allocator()->allocate();
87 }
88 else
89 {
90 _weights_reshape_kernel.configure(weights, biases, output);
91 }
92}
93
94void NEConvolutionLayerReshapeWeights::run()
95{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010096 _memory_group.acquire();
97
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010099
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 if(_transpose1xW)
101 {
102 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
103 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100104
105 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106}
107
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100108NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100109 : _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 Barbier6ff3b192017-09-04 18:44:23 +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{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100116 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100117 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
118 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 Barbier6ff3b192017-09-04 18:44:23 +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
137 unsigned int stride_x = 0;
138 unsigned int stride_y = 0;
139 unsigned int pad_x = 0;
140 unsigned int pad_y = 0;
141 std::tie(stride_x, stride_y) = conv_info.stride();
142 std::tie(pad_x, pad_y) = conv_info.pad();
143
144 // Get convolved dimensions
145 unsigned int conv_w = 0;
146 unsigned int conv_h = 0;
147
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100148 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
149 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
150 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
151 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100153 // Check if its a "fully connected" convolution, i.e. the output size is 1x1xnum_kernels
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
155
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100156#if defined(__arm__)
157 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
158 {
159 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
160 }
161#elif defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100162 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
163 {
164 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
165 }
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100166#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100167
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 unsigned int mat_weights_cols = weights->info()->dimension(3);
169 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
170
171 // Reshape weights if needed
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100172 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100174 if(_are_weights_reshaped)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100176 mat_weights_cols = weights_info.num_kernels();
177 mat_weights_rows = weights->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 }
179 else
180 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100181 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
182
183 // Create tensor to store the reshaped weights
184 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
185 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
186 weights = &_weights_reshaped;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100188 }
189 else
190 {
191 if(_are_weights_reshaped)
192 {
193 mat_weights_cols = weights_info.num_kernels();
194 mat_weights_rows = weights->info()->dimension(0) / 4 + (_has_bias ? 1 : 0);
195 }
196 else
197 {
198 TensorShape reshaped_weights_shape;
199
200 if(_is_fully_connected_convolution)
201 {
202 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
203 }
204 else
205 {
206 // Create tensor to store transposed weights
207 const float transpose_width = 16.0f / input->info()->element_size();
208 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
209 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
210 }
211
212 // Create tensor to store the reshaped weights
213 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
214 _reshape_weights.configure(weights, biases, &_weights_reshaped, !_is_fully_connected_convolution /* 1xW transpose */);
215 weights = &_weights_reshaped;
216 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 }
218
219 // Create tensor to store im2col reshaped inputs
220 const unsigned int mat_input_cols = mat_weights_rows;
221 const unsigned int mat_input_rows = conv_w * conv_h;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100222
223 TensorShape shape_im2col(input->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 shape_im2col.set(0, mat_input_cols);
225 shape_im2col.set(1, mat_input_rows);
226 shape_im2col.set(2, 1);
227 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100228 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230 // Create tensor (interleave) to prepare input tensor for GEMM
231 if(!_is_fully_connected_convolution)
232 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100233 TensorShape shape_interleaved(shape_im2col);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234 shape_interleaved.set(0, shape_interleaved.x() * 4);
235 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
236 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100237 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238 }
239
240 // Create GEMM output tensor
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100241 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242 shape_gemm.set(0, mat_weights_cols);
243 shape_gemm.set(1, mat_input_rows);
244 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100245 _memory_group.manage(&_gemm_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246
247 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100248 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100249
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100250#if defined(__arm__) || defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100251 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100253 struct CPUInfo ci = NEScheduler::get().cpu_info();
254
255 const int M = _gemm_output.info()->tensor_shape().y();
256 const int N = _gemm_output.info()->tensor_shape().x();
257 const int K = _input_im2col_reshaped.info()->tensor_shape().x();
258
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100259#if defined(__arm__)
260 GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false);
261#elif defined(__aarch64__)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100262 GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false);
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100263#endif /* defined(__arm__) || defined(__aarch64__) */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100264
265 constexpr size_t alignment = 4096;
266 _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
267 _memory_group.manage(&_workspace);
268
269 // Configure matrix multiplication kernel
270 if(_is_fully_connected_convolution)
271 {
272 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace, 1.f, 0.f, false, false);
273 }
274 else
275 {
276 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace);
277 }
278
279 _workspace.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280 }
281 else
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100282#endif /* defined(__arm__) || defined(__aarch64__) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100283 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100284 if(_is_fully_connected_convolution)
285 {
286 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
287 }
288 else
289 {
290 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
291 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
292 _input_interleaved_reshaped.allocator()->allocate();
293 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100295
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100296 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100298 _gemm_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100300 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");
301
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302 // Allocate intermediate tensor
303 if(!_are_weights_reshaped)
304 {
305 _weights_reshaped.allocator()->allocate();
306 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307}
308
309void NEConvolutionLayer::run()
310{
311 // Run weights reshaping (Runs once for every configure)
312 if(!_are_weights_reshaped)
313 {
314 _are_weights_reshaped = true;
315 _reshape_weights.run();
316 }
317
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100318 _memory_group.acquire();
319
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 // Run input reshaping
321 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322
323 // Runs matrix multiply on reshaped matrices
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100324 if(_mm_optimised_kernel != nullptr)
325 {
326 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
327 }
328 else
329 {
330 if(!_is_fully_connected_convolution)
331 {
332 // Run interleave
333 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
334 }
335
336 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
337 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100338
339 // Reshape output matrix
340 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100341
342 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100344} // namespace arm_compute