blob: 44bf2de70c08a7b14a9f666765b0a5ac84b2d1c0 [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 Pflanzerbeabe3b2017-08-31 14:56:32 +010026#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010028#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010032#include "support/ToolchainSupport.h"
33
34namespace 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 Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <cmath>
41#include <tuple>
42
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010043namespace arm_compute
44{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010045NEConvolutionLayerReshapeWeights::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 Barbier6ff3b192017-09-04 18:44:23 +010047{
48}
49
50void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
51{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010052 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 +010053 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 Barbier6ff3b192017-09-04 18:44:23 +010059 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 Pinitasbaf174e2017-09-08 19:47:30 +010079 _memory_group.manage(&_weights_reshaped);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010080
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
82 _weights_transposed_kernel.configure(&_weights_reshaped, output);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010083
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 _weights_reshaped.allocator()->allocate();
85 }
86 else
87 {
88 _weights_reshape_kernel.configure(weights, biases, output);
89 }
90}
91
92void NEConvolutionLayerReshapeWeights::run()
93{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010094 _memory_group.acquire();
95
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010097
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 if(_transpose1xW)
99 {
100 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
101 }
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100102
103 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104}
105
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100106NEConvolutionLayer::NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100107 : _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 Barbier6ff3b192017-09-04 18:44:23 +0100109{
110}
111
112void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
113{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100114 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 +0100115 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
116 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 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 Barbier6ff3b192017-09-04 18:44:23 +0100122 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 Iodice4e288692017-06-27 11:41:59 +0100146 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 Barbier6ff3b192017-09-04 18:44:23 +0100150
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100151 // Check if its a "fully connected" convolution, i.e. the output size is 1x1xnum_kernels
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
153
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100154#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 Barbier6ff3b192017-09-04 18:44:23 +0100161 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 Pflanzerbeabe3b2017-08-31 14:56:32 +0100165 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100167 if(_are_weights_reshaped)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100169 mat_weights_cols = weights_info.num_kernels();
170 mat_weights_rows = weights->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 }
172 else
173 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100174 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 Barbier6ff3b192017-09-04 18:44:23 +0100180 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100181 }
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 Barbier6ff3b192017-09-04 18:44:23 +0100210 }
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 Pflanzerbeabe3b2017-08-31 14:56:32 +0100215
216 TensorShape shape_im2col(input->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 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 Pinitasbaf174e2017-09-08 19:47:30 +0100221 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
223 // Create tensor (interleave) to prepare input tensor for GEMM
224 if(!_is_fully_connected_convolution)
225 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100226 TensorShape shape_interleaved(shape_im2col);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 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 Pinitasbaf174e2017-09-08 19:47:30 +0100230 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 }
232
233 // Create GEMM output tensor
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100234 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235 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 Pinitasbaf174e2017-09-08 19:47:30 +0100238 _memory_group.manage(&_gemm_output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239
240 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100241 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100242
243#if defined(__aarch64__)
244 if(_mm_optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100246 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 Barbier6ff3b192017-09-04 18:44:23 +0100269 }
270 else
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100271#endif /* defined(__aarch64__) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100273 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 Barbier6ff3b192017-09-04 18:44:23 +0100283 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100284
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100285 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100287 _gemm_output.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100289 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 Barbier6ff3b192017-09-04 18:44:23 +0100291 // Allocate intermediate tensor
292 if(!_are_weights_reshaped)
293 {
294 _weights_reshaped.allocator()->allocate();
295 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100296}
297
298void 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 Pinitasbaf174e2017-09-08 19:47:30 +0100307 _memory_group.acquire();
308
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 // Run input reshaping
310 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311
312 // Runs matrix multiply on reshaped matrices
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100313 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 Barbier6ff3b192017-09-04 18:44:23 +0100327
328 // Reshape output matrix
329 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +0100330
331 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100333} // namespace arm_compute