blob: 3c48d691ed3cf72d04020063a80b32200d87df4b [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
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Size2D.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
32#include "support/ToolchainSupport.h"
33
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000034#include <cmath>
35#include <tuple>
36
37namespace
38{
39arm_compute::TensorShape get_reshaped_weights_shape(const arm_compute::ITensorInfo *weights, bool append_bias)
40{
41 const unsigned int mat_weights_cols = weights->dimension(3);
42 const unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
43 return arm_compute::TensorShape(mat_weights_cols, mat_weights_rows);
44}
45} // namespace
46
47namespace arm_compute
48{
49NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
50 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
51{
52}
53
54void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW)
55{
56 // Perform validation step
57 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
58 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
59 (biases != nullptr) ? biases->info() : nullptr,
60 output->info(),
61 transpose1xW));
62
63 // Check if bias are present, if yes they will be embedded to the weights matrix
64 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
65 //const unsigned bias_element = (append_biases) ? 1 : 0;
66 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
67
68 _transpose1xW = transpose1xW;
69
70 if(transpose1xW)
71 {
72 // Create tensor to store the reshaped weights
73 TensorInfo info_wr = weights->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(get_reshaped_weights_shape(weights->info(), append_biases));
74
75 _weights_reshaped.allocator()->init(info_wr);
76 _memory_group.manage(&_weights_reshaped);
77
78 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
79 _weights_transposed_kernel.configure(&_weights_reshaped, output);
80
81 _weights_reshaped.allocator()->allocate();
82 }
83 else
84 {
85 _weights_reshape_kernel.configure(weights, biases_to_use, output);
86 }
87
88 output->info()->set_quantization_info(weights->info()->quantization_info());
89}
90
91Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW)
92{
93 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
94 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
95 if(!is_data_type_quantized_asymmetric(weights->data_type()))
96 {
97 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
99 }
100 // Check if bias are present, if yes they will be embedded to the weights matrix
101 const bool append_bias = (biases != nullptr);
102
103 if(append_bias)
104 {
105 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
106 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases);
108 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
109 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
110 }
111
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000112 if(transpose1xW)
113 {
114 TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, append_bias));
115 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped));
116 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output));
117 }
118 else
119 {
120 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output));
121 }
122
123 return Status{};
124}
125
126void NEConvolutionLayerReshapeWeights::run()
127{
128 _memory_group.acquire();
129
130 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
131
132 if(_transpose1xW)
133 {
134 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
135 }
136
137 _memory_group.release();
138}
139
140namespace
141{
142TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool append_bias, bool is_fully_connected_convolution)
143{
144 unsigned int mat_weights_cols = weights->dimension(3);
145 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
146
147 if(is_fully_connected_convolution)
148 {
149 // Create tensor to store the reshaped weights
150 return TensorShape(mat_weights_cols, mat_weights_rows);
151 }
152 else
153 {
154 // Create tensor to store transposed weights
155 const float transpose_width = 16.0f / weights->element_size();
156 return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
157 }
158}
159
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000160Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
161 const ActivationLayerInfo &act_info, DataType &dt,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000162 bool &append_bias,
163 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000164 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized, bool &is_activationlayer_enabled,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000165 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000166 unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000167{
168 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
169 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
170 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
171 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(2) != input->dimension(2));
172 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
173 ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type()));
174
175 dt = input->data_type();
176 is_quantized = is_data_type_quantized_asymmetric(dt);
177
178 if(biases != nullptr)
179 {
180 if(is_quantized)
181 {
182 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
183 }
184 else
185 {
186 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
187 }
188 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
189 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
190 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
191 }
192
193 append_bias = (biases != nullptr) && (!is_quantized);
194 are_weights_reshaped = weights_info.are_reshaped();
195 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(0);
196 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(1);
197 mat_weights_cols = weights->dimension(3);
198 mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
199
200 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000201 conv_info, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000202
203 // Check if its a "fully connected" convolution
204 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000205 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000206 is_activationlayer_enabled = act_info.enabled();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000207
208 return Status{};
209}
210} // namespace
211
212NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000213 : _asm_glue(), _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(),
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000214 _output_col2im_kernel(), _activationlayer_function(), _original_weights(nullptr), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _tmp_output(),
215 _workspace(), _append_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false), _is_quantized(false), _is_interleaved(false), _is_activationlayer_enabled(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000216{
217}
218
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000219void 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 +0000220{
221 if(_is_quantized)
222 {
223 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
224 // Extract and negate input and weights offset
225 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
226 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
227
228 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
229 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
230
231 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
232
233 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
234 input->info()->set_quantization_info(input_quantization_info);
235 weights->info()->set_quantization_info(weights_quantization_info);
236 }
237 else
238 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000239 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000240 }
241}
242
Alex Gilday7da29b62018-03-23 14:16:00 +0000243void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000244 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000245{
246 // Perform validate step
247 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
248
249 DataType dt{};
250 unsigned int kernel_width = 0;
251 unsigned int kernel_height = 0;
252 unsigned int mat_weights_cols = 0;
253 unsigned int mat_weights_rows = 0;
254 unsigned int conv_w = 0;
255 unsigned int conv_h = 0;
256
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000257 Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, act_info, dt, _append_bias,
258 _are_weights_reshaped,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000259 kernel_width, kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000260 _is_fully_connected_convolution, _is_interleaved, _is_quantized, _is_activationlayer_enabled,
Alex Gilday7da29b62018-03-23 14:16:00 +0000261 mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000262
263 ARM_COMPUTE_ERROR_THROW_ON(status);
264
Georgios Pinitas1562be32018-03-08 19:09:19 +0000265 _original_weights = weights;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000266 const unsigned int fixed_point_position = input->info()->fixed_point_position();
267 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
268
Pablo Tello7fad9b12018-03-14 17:55:27 +0000269 bool run_optimised = dt == DataType::F32;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000270
271 // Reshape weights if needed
Pablo Telloeb82fd22018-02-23 13:43:50 +0000272 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000273 {
274 if(_are_weights_reshaped)
275 {
276 mat_weights_cols = weights_info.num_kernels();
277 mat_weights_rows = weights->info()->dimension(1);
278 }
279 else
280 {
281 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
282
283 // Create tensor to store the reshaped weights
284 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
285 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
286 weights = &_weights_reshaped;
287 }
288 }
289 else
290 {
291 if(_are_weights_reshaped)
292 {
293 if(_is_fully_connected_convolution || _is_quantized)
294 {
295 mat_weights_cols = weights_info.num_kernels();
296 mat_weights_rows = weights->info()->dimension(1);
297 }
298 else
299 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000300 mat_weights_cols = weights_info.num_kernels();
301 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 +0000302 }
303 }
304 else
305 {
306 TensorShape reshaped_weights_shape;
307
308 if(_is_fully_connected_convolution || _is_quantized)
309 {
310 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
311 }
312 else
313 {
314 // Create tensor to store transposed weights
315 const float transpose_width = 16.0f / input->info()->element_size();
316 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
317 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
318 }
319
320 // Create tensor to store the reshaped weights
321 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000322 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000323 weights = &_weights_reshaped;
324 }
325 }
326
327 // Create tensor to store im2col reshaped inputs
328 const unsigned int mat_input_cols = mat_weights_rows;
329 const unsigned int mat_input_rows = conv_w * conv_h;
330
331 TensorShape shape_im2col(input->info()->tensor_shape());
332 shape_im2col.set(0, mat_input_cols);
333 shape_im2col.set(1, mat_input_rows);
334 shape_im2col.set(2, 1);
335 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
336 _memory_group.manage(&_input_im2col_reshaped);
337
338 // Create tensor (interleave) to prepare input tensor for GEMM
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100339 if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000340 {
341 TensorShape shape_interleaved(shape_im2col);
342 shape_interleaved.set(0, shape_interleaved.x() * 4);
343 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
344 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
345 _memory_group.manage(&_input_interleaved_reshaped);
346 }
347
348 // Create GEMM output tensor
349 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
350 shape_gemm.set(0, mat_weights_cols);
351 shape_gemm.set(1, mat_input_rows);
352 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
353 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
354 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
355 info_gemm.set_quantization_info(output->info()->quantization_info());
356 _gemm_output.allocator()->init(info_gemm);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100357
358 // FIXME: enabling memory manager for _gemm_output gives incorrect results (maybe bound to the assembly kernel in GEMMLowp?)
359 // _memory_group.manage(&_gemm_output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000360
361 // Configure kernels
362 // Configure im2col
Alex Gilday7da29b62018-03-23 14:16:00 +0000363 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _append_bias, false, false, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000364
365 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000366 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000367 {
Pablo Tello7fad9b12018-03-14 17:55:27 +0000368 if(!setup_assembly_kernel(&_input_im2col_reshaped, weights, &_gemm_output, 1.f, 0.f, _workspace, _memory_group, _asm_glue))
369 {
370 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
371 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000372 }
373 else
374 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000375 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000376 {
377 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
378 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
379
380 // Configure GEMM
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000381 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(1), 0 /* no transpose */,
382 _input_im2col_reshaped.info()->dimension(0)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000383 _input_interleaved_reshaped.allocator()->allocate();
384 }
385 else
386 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000387 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000388 }
389 }
390
391 _input_im2col_reshaped.allocator()->allocate();
392
393 // Configure output stage for quantized case
394 if(_is_quantized)
395 {
396 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
397
398 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
399 int output_multiplier, output_shift;
400 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
401 _memory_group.manage(&_tmp_output);
402 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
403 }
404
405 // Configure Col2Im
406 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
407 if(_is_quantized)
408 {
409 _tmp_output.allocator()->allocate();
410 }
411 _gemm_output.allocator()->allocate();
412
413 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");
414
415 // Allocate intermediate tensor
416 if(!_are_weights_reshaped)
417 {
418 _weights_reshaped.allocator()->allocate();
419 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000420
421 //Configure Activation Layer
422 if(_is_activationlayer_enabled)
423 {
424 _activationlayer_function.configure(output, nullptr, act_info);
425 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000426}
427
428Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000429 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000430{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000431 ARM_COMPUTE_UNUSED(output);
432
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000433 DataType dt{};
434 bool append_bias{};
435 bool are_weights_reshaped{};
436 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000437 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000438 bool is_quantized{};
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000439 bool is_activationlayer_enabled{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000440 unsigned int kernel_width = 0;
441 unsigned int kernel_height = 0;
442 unsigned int mat_weights_cols = 0;
443 unsigned int mat_weights_rows = 0;
444 unsigned int conv_w = 0;
445 unsigned int conv_h = 0;
446
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000447 Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, act_info, dt, append_bias, are_weights_reshaped, kernel_width, kernel_height,
448 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000449 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000450
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000451 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
452
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000453 ARM_COMPUTE_RETURN_ON_ERROR(status);
454
455 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
456 bool optimised_kernel = false;
457
Pablo Tello7fad9b12018-03-14 17:55:27 +0000458 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000459 {
460 optimised_kernel = true;
461 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000462
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000463 // Validate im2col
464 const unsigned int mat_input_cols = mat_weights_rows;
465 const unsigned int mat_input_rows = conv_w * conv_h;
466 TensorShape shape_im2col = input->tensor_shape();
467 shape_im2col.set(0, mat_input_cols);
468 shape_im2col.set(1, mat_input_rows);
469 shape_im2col.set(2, 1);
470 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Alex Gilday7da29b62018-03-23 14:16:00 +0000471 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000472
473 // Create GEMM output tensor
474 TensorShape shape_gemm(im2_col_info.tensor_shape());
475 shape_gemm.set(0, mat_weights_cols);
476 shape_gemm.set(1, mat_input_rows);
477 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
478
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100479 // Reshape weights if needed
480 if(optimised_kernel)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000481 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100482 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
483
484 // Create tensor to store the reshaped weights
485 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
486 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000487 }
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100488 else if(!is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000489 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100490 TensorShape reshaped_weights_shape;
491
492 if(is_fully_connected_convolution || is_quantized)
493 {
494 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
495 }
496 else
497 {
498 // Create tensor to store transposed weights
499 const float transpose_width = 16.0f / input->element_size();
500 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
501 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
502 }
503
504 // Create tensor to store the reshaped weights
505 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
506 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
507 weights = reshaped_weights.get();
508
509 // Validate GEMM interleave and multiply
510 if(is_interleaved)
511 {
512 TensorShape shape_interleaved = shape_im2col;
513 shape_interleaved.set(0, shape_interleaved.x() * 4);
514 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
515 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
516 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
517 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
518 weights->tensor_shape()[0], // n
519 shape_im2col[0]) /* k */));
520 }
521 else
522 {
523 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
524 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000525 }
526
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000527 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
528
529 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
530
531 if(act_info.enabled())
532 {
533 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
534 }
535
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000536 return Status{};
537}
538
539void NEGEMMConvolutionLayer::run()
540{
541 // Run weights reshaping (Runs once for every configure)
542 if(!_are_weights_reshaped)
543 {
Georgios Pinitas1562be32018-03-08 19:09:19 +0000544 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
545
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000546 _are_weights_reshaped = true;
547 _reshape_weights.run();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000548
549 // Mark original weights tensor as unused
550 _original_weights->mark_as_unused();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000551 }
552
553 _memory_group.acquire();
554
555 // Run input reshaping
556 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
557
558 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000559 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000560 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000561 _asm_glue.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000562 }
563 else
564 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000565 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000566 {
567 // Run interleave
568 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
569 }
570
571 // Runs matrix multiply on reshaped matrices
572 if(_is_quantized)
573 {
574 _mm_gemmlowp.run();
575 }
576 else
577 {
578 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
579 }
580 }
581
582 // Run output stage for quantized case
583 if(_is_quantized)
584 {
585 _gemmlowp_output_stage.run();
586 }
587
588 // Reshape output matrix
589 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
590
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000591 if(_is_activationlayer_enabled)
592 {
593 _activationlayer_function.run();
594 }
595
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000596 _memory_group.release();
597}
598} // namespace arm_compute