blob: a85078cf71aa7f5aa6b914f82010eb48e2fed197 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
2 * Copyright (c) 2017-2018 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/NEGEMMConvolutionLayer.h"
25
26#include "arm_compute/core/NEON/kernels/arm32/NEGEMMAArch32Kernel.h"
27#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64Kernel.h"
28#include "arm_compute/core/NEON/kernels/arm64/NEGEMMAArch64NativeKernel.h"
29#include "arm_compute/core/PixelValue.h"
30#include "arm_compute/core/Size2D.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34#include "arm_compute/runtime/NEON/NEScheduler.h"
35#include "support/ToolchainSupport.h"
36
37namespace arm_compute
38{
39#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
40#include "arm_compute/core/NEON/kernels/assembly/kernels/a32_sgemm_8x6.hpp"
41#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_sgemm_12x8.hpp"
42} // namespace arm_compute
43
44#include <cmath>
45#include <tuple>
46
47namespace
48{
49arm_compute::TensorShape get_reshaped_weights_shape(const arm_compute::ITensorInfo *weights, bool append_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) + (append_bias ? 1 : 0);
53 return arm_compute::TensorShape(mat_weights_cols, mat_weights_rows);
54}
55} // namespace
56
57namespace arm_compute
58{
59NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
60 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
61{
62}
63
64void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
65{
66 // Perform validation step
67 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
68 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
69 (biases != nullptr) ? biases->info() : nullptr,
70 output->info(),
71 transpose1xW));
72
73 // Check if bias are present, if yes they will be embedded to the weights matrix
74 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
75 //const unsigned bias_element = (append_biases) ? 1 : 0;
76 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
77
78 _transpose1xW = transpose1xW;
79
80 if(transpose1xW)
81 {
82 // Create tensor to store the reshaped weights
83 TensorInfo info_wr = weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(get_reshaped_weights_shape(weights->info(), append_biases));
84
85 _weights_reshaped.allocator()->init(info_wr);
86 _memory_group.manage(&_weights_reshaped);
87
88 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
89 _weights_transposed_kernel.configure(&_weights_reshaped, output);
90
91 _weights_reshaped.allocator()->allocate();
92 }
93 else
94 {
95 _weights_reshape_kernel.configure(weights, biases_to_use, output);
96 }
97
98 output->info()->set_quantization_info(weights->info()->quantization_info());
99}
100
101Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW)
102{
103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
104 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
105 if(!is_data_type_quantized_asymmetric(weights->data_type()))
106 {
107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
108 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
109 }
110 // Check if bias are present, if yes they will be embedded to the weights matrix
111 const bool append_bias = (biases != nullptr);
112
113 if(append_bias)
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
116 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
117 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
118 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
119 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
120 }
121
122 // Checks performed when biases are present
123 if(append_bias)
124 {
125 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
126 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
127 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
128 }
129
130 if(transpose1xW)
131 {
132 TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, append_bias));
133 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped));
134 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output));
135 }
136 else
137 {
138 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output));
139 }
140
141 return Status{};
142}
143
144void NEConvolutionLayerReshapeWeights::run()
145{
146 _memory_group.acquire();
147
148 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
149
150 if(_transpose1xW)
151 {
152 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
153 }
154
155 _memory_group.release();
156}
157
158namespace
159{
160TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool append_bias, bool is_fully_connected_convolution)
161{
162 unsigned int mat_weights_cols = weights->dimension(3);
163 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
164
165 if(is_fully_connected_convolution)
166 {
167 // Create tensor to store the reshaped weights
168 return TensorShape(mat_weights_cols, mat_weights_rows);
169 }
170 else
171 {
172 // Create tensor to store transposed weights
173 const float transpose_width = 16.0f / weights->element_size();
174 return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
175 }
176}
177
178Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, DataType &dt,
179 bool &append_bias,
180 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000181 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000182 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
183 unsigned int &conv_w, unsigned int &conv_h)
184{
185 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
186 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
187 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
188 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(2) != input->dimension(2));
189 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
190 ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type()));
191
192 dt = input->data_type();
193 is_quantized = is_data_type_quantized_asymmetric(dt);
194
195 if(biases != nullptr)
196 {
197 if(is_quantized)
198 {
199 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
200 }
201 else
202 {
203 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
204 }
205 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
206 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
207 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
208 }
209
210 append_bias = (biases != nullptr) && (!is_quantized);
211 are_weights_reshaped = weights_info.are_reshaped();
212 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(0);
213 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(1);
214 mat_weights_cols = weights->dimension(3);
215 mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
216
217 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
218 conv_info);
219
220 // Check if its a "fully connected" convolution
221 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000222 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000223
224 return Status{};
225}
226} // namespace
227
228NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
229 : _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_optimised_kernel(nullptr), _mm_gemmlowp(memory_manager),
230 _gemmlowp_output_stage(), _output_col2im_kernel(), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _tmp_output(), _workspace(), _append_bias(false),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000231 _is_fully_connected_convolution(false), _are_weights_reshaped(false), _is_quantized(false), _is_interleaved(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000232{
233}
234
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000235void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, bool is_interleaved, const GEMMReshapeInfo &reshape_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000236{
237 if(_is_quantized)
238 {
239 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
240 // Extract and negate input and weights offset
241 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
242 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
243
244 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
245 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
246
247 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
248
249 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
250 input->info()->set_quantization_info(input_quantization_info);
251 weights->info()->set_quantization_info(weights_quantization_info);
252 }
253 else
254 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000255 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000256 }
257}
258
259void NEGEMMConvolutionLayer::configure_asm_mm(const struct CPUInfo &ci, int M, int N, int K)
260{
261 ARM_COMPUTE_UNUSED(ci);
262 ARM_COMPUTE_UNUSED(M);
263 ARM_COMPUTE_UNUSED(N);
264 ARM_COMPUTE_UNUSED(K);
265#if defined(__arm__) || defined(__aarch64__)
266#if defined(__arm__)
267 GemmInterleaved<sgemm_8x6, float, float> gemm(&ci, M, N, K, false, false);
268#elif defined(__aarch64__)
269 GemmInterleaved<sgemm_12x8, float, float> gemm(&ci, M, N, K, false, false);
270#endif /* defined(__arm__) || defined(__aarch64__) */
271
272 constexpr size_t alignment = 4096;
273 _workspace.allocator()->init(TensorInfo(TensorShape{ (gemm.get_working_size() + alignment - 1) * NEScheduler::get().num_threads() }, 1, DataType::U8));
274 _memory_group.manage(&_workspace);
275#endif /* defined(__arm__) || defined(__aarch64__) */
276}
277
278void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
279{
280 // Perform validate step
281 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
282
283 DataType dt{};
284 unsigned int kernel_width = 0;
285 unsigned int kernel_height = 0;
286 unsigned int mat_weights_cols = 0;
287 unsigned int mat_weights_rows = 0;
288 unsigned int conv_w = 0;
289 unsigned int conv_h = 0;
290
291 Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, dt, _append_bias, _are_weights_reshaped,
292 kernel_width, kernel_height,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000293 _is_fully_connected_convolution, _is_interleaved, _is_quantized,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000294 mat_weights_cols, mat_weights_rows, conv_w, conv_h);
295
296 ARM_COMPUTE_ERROR_THROW_ON(status);
297
298 const unsigned int fixed_point_position = input->info()->fixed_point_position();
299 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
300
301#if defined(__arm__)
302 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
303 {
304 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch32Kernel>();
305 }
306#elif defined(__aarch64__)
307 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
308 {
309 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64Kernel>();
310 }
311#endif /* defined(__arm__) || defined(__aarch64__) */
312
313 // Reshape weights if needed
314 if(_mm_optimised_kernel != nullptr)
315 {
316 if(_are_weights_reshaped)
317 {
318 mat_weights_cols = weights_info.num_kernels();
319 mat_weights_rows = weights->info()->dimension(1);
320 }
321 else
322 {
323 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
324
325 // Create tensor to store the reshaped weights
326 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
327 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
328 weights = &_weights_reshaped;
329 }
330 }
331 else
332 {
333 if(_are_weights_reshaped)
334 {
335 if(_is_fully_connected_convolution || _is_quantized)
336 {
337 mat_weights_cols = weights_info.num_kernels();
338 mat_weights_rows = weights->info()->dimension(1);
339 }
340 else
341 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000342 mat_weights_cols = weights_info.num_kernels();
343 mat_weights_rows = weights_info.kernel_size().first * weights_info.kernel_size().second * input->info()->dimension(2) + (_append_bias ? 1 : 0);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000344 }
345 }
346 else
347 {
348 TensorShape reshaped_weights_shape;
349
350 if(_is_fully_connected_convolution || _is_quantized)
351 {
352 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
353 }
354 else
355 {
356 // Create tensor to store transposed weights
357 const float transpose_width = 16.0f / input->info()->element_size();
358 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
359 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
360 }
361
362 // Create tensor to store the reshaped weights
363 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000364 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000365 weights = &_weights_reshaped;
366 }
367 }
368
369 // Create tensor to store im2col reshaped inputs
370 const unsigned int mat_input_cols = mat_weights_rows;
371 const unsigned int mat_input_rows = conv_w * conv_h;
372
373 TensorShape shape_im2col(input->info()->tensor_shape());
374 shape_im2col.set(0, mat_input_cols);
375 shape_im2col.set(1, mat_input_rows);
376 shape_im2col.set(2, 1);
377 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
378 _memory_group.manage(&_input_im2col_reshaped);
379
380 // Create tensor (interleave) to prepare input tensor for GEMM
381 if(!_is_fully_connected_convolution && _mm_optimised_kernel == nullptr)
382 {
383 TensorShape shape_interleaved(shape_im2col);
384 shape_interleaved.set(0, shape_interleaved.x() * 4);
385 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
386 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
387 _memory_group.manage(&_input_interleaved_reshaped);
388 }
389
390 // Create GEMM output tensor
391 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
392 shape_gemm.set(0, mat_weights_cols);
393 shape_gemm.set(1, mat_input_rows);
394 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
395 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
396 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
397 info_gemm.set_quantization_info(output->info()->quantization_info());
398 _gemm_output.allocator()->init(info_gemm);
399 _memory_group.manage(&_gemm_output);
400
401 // Configure kernels
402 // Configure im2col
403 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias);
404
405 // Configure matrix multiply
406 if(_mm_optimised_kernel != nullptr)
407 {
408 struct CPUInfo ci = NEScheduler::get().cpu_info();
409
410 const int M = _gemm_output.info()->tensor_shape().y();
411 const int N = _gemm_output.info()->tensor_shape().x();
412 const int K = _input_im2col_reshaped.info()->tensor_shape().x();
413
414#if defined(__aarch64__)
415 if((N <= 128) && (K <= 128))
416 {
417 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMAArch64NativeKernel>();
418 }
419 else
420#endif /* defined(__aarch64__) */
421 {
422 configure_asm_mm(ci, M, N, K);
423 }
424
425 // Configure matrix multiplication kernel
426 _mm_optimised_kernel->configure(&_input_im2col_reshaped, weights, &_gemm_output, &_workspace);
427
428 _workspace.allocator()->allocate();
429 }
430 else
431 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000432 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000433 {
434 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
435 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
436
437 // Configure GEMM
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000438 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(1), 0 /* no transpose */,
439 _input_im2col_reshaped.info()->dimension(0)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000440 _input_interleaved_reshaped.allocator()->allocate();
441 }
442 else
443 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000444 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000445 }
446 }
447
448 _input_im2col_reshaped.allocator()->allocate();
449
450 // Configure output stage for quantized case
451 if(_is_quantized)
452 {
453 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
454
455 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
456 int output_multiplier, output_shift;
457 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
458 _memory_group.manage(&_tmp_output);
459 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
460 }
461
462 // Configure Col2Im
463 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
464 if(_is_quantized)
465 {
466 _tmp_output.allocator()->allocate();
467 }
468 _gemm_output.allocator()->allocate();
469
470 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");
471
472 // Allocate intermediate tensor
473 if(!_are_weights_reshaped)
474 {
475 _weights_reshaped.allocator()->allocate();
476 }
477}
478
479Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
480 const WeightsInfo &weights_info)
481{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000482 ARM_COMPUTE_UNUSED(output);
483
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000484 DataType dt{};
485 bool append_bias{};
486 bool are_weights_reshaped{};
487 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000488 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000489 bool is_quantized{};
490 unsigned int kernel_width = 0;
491 unsigned int kernel_height = 0;
492 unsigned int mat_weights_cols = 0;
493 unsigned int mat_weights_rows = 0;
494 unsigned int conv_w = 0;
495 unsigned int conv_h = 0;
496
497 Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, dt, append_bias, are_weights_reshaped, kernel_width, kernel_height,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000498 is_fully_connected_convolution, is_interleaved, is_quantized, mat_weights_cols, mat_weights_rows,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000499 conv_w, conv_h);
500
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000501 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
502
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000503 ARM_COMPUTE_RETURN_ON_ERROR(status);
504
505 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
506 bool optimised_kernel = false;
507
508#if defined(__arm__)
509 if(NEScheduler::get().cpu_info().CPU == CPUTarget::ARMV7 && dt == DataType::F32)
510 {
511 optimised_kernel = true;
512 }
513#elif defined(__aarch64__)
514 if(NEScheduler::get().cpu_info().CPU >= CPUTarget::ARMV8 && dt == DataType::F32)
515 {
516 optimised_kernel = true;
517 }
518#endif /* defined(__arm__) || defined(__aarch64__) */
519
520 // Reshape weights if needed
521 if(optimised_kernel)
522 {
523 if(are_weights_reshaped)
524 {
525 mat_weights_cols = weights_info.num_kernels();
526 mat_weights_rows = weights->dimension(1);
527 }
528 else
529 {
530 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
531
532 // Create tensor to store the reshaped weights
533 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
534 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
535 weights = reshaped_weights.get();
536 }
537 }
538 else
539 {
540 if(are_weights_reshaped)
541 {
542 const unsigned int transpose_width = 16 / input->element_size();
543 mat_weights_cols = weights_info.num_kernels();
544 mat_weights_rows = weights->dimension(0) / transpose_width + (append_bias ? 1 : 0);
545 }
546 else
547 {
548 TensorShape reshaped_weights_shape;
549
550 if(is_fully_connected_convolution || is_quantized)
551 {
552 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
553 }
554 else
555 {
556 // Create tensor to store transposed weights
557 const float transpose_width = 16.0f / input->element_size();
558 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
559 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
560 }
561
562 // Create tensor to store the reshaped weights
563 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
564 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
565 weights = reshaped_weights.get();
566 }
567 }
568
569 // Validate im2col
570 const unsigned int mat_input_cols = mat_weights_rows;
571 const unsigned int mat_input_rows = conv_w * conv_h;
572 TensorShape shape_im2col = input->tensor_shape();
573 shape_im2col.set(0, mat_input_cols);
574 shape_im2col.set(1, mat_input_rows);
575 shape_im2col.set(2, 1);
576 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000577 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000578
579 // Create GEMM output tensor
580 TensorShape shape_gemm(im2_col_info.tensor_shape());
581 shape_gemm.set(0, mat_weights_cols);
582 shape_gemm.set(1, mat_input_rows);
583 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
584
585 // Validate GEMM interleave and multiply
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000586 if(is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000587 {
588 TensorShape shape_interleaved = shape_im2col;
589 shape_interleaved.set(0, shape_interleaved.x() * 4);
590 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
591 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
592 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000593 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000594 }
595 else
596 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000597 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000598 }
599
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000600 return Status{};
601}
602
603void NEGEMMConvolutionLayer::run()
604{
605 // Run weights reshaping (Runs once for every configure)
606 if(!_are_weights_reshaped)
607 {
608 _are_weights_reshaped = true;
609 _reshape_weights.run();
610 }
611
612 _memory_group.acquire();
613
614 // Run input reshaping
615 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
616
617 // Runs matrix multiply on reshaped matrices
618 if(_mm_optimised_kernel != nullptr)
619 {
620 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
621 }
622 else
623 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000624 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000625 {
626 // Run interleave
627 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
628 }
629
630 // Runs matrix multiply on reshaped matrices
631 if(_is_quantized)
632 {
633 _mm_gemmlowp.run();
634 }
635 else
636 {
637 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
638 }
639 }
640
641 // Run output stage for quantized case
642 if(_is_quantized)
643 {
644 _gemmlowp_output_stage.run();
645 }
646
647 // Reshape output matrix
648 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
649
650 _memory_group.release();
651}
652} // namespace arm_compute