blob: cdbd32373ad7efb375f07c17e2c898c8e1e76803 [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
112 // Checks performed when biases are present
113 if(append_bias)
114 {
115 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
116 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
117 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
118 }
119
120 if(transpose1xW)
121 {
122 TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, append_bias));
123 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped));
124 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output));
125 }
126 else
127 {
128 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output));
129 }
130
131 return Status{};
132}
133
134void NEConvolutionLayerReshapeWeights::run()
135{
136 _memory_group.acquire();
137
138 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
139
140 if(_transpose1xW)
141 {
142 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
143 }
144
145 _memory_group.release();
146}
147
148namespace
149{
150TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool append_bias, bool is_fully_connected_convolution)
151{
152 unsigned int mat_weights_cols = weights->dimension(3);
153 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
154
155 if(is_fully_connected_convolution)
156 {
157 // Create tensor to store the reshaped weights
158 return TensorShape(mat_weights_cols, mat_weights_rows);
159 }
160 else
161 {
162 // Create tensor to store transposed weights
163 const float transpose_width = 16.0f / weights->element_size();
164 return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
165 }
166}
167
168Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, DataType &dt,
169 bool &append_bias,
170 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000171 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000172 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000173 unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000174{
175 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
176 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
177 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
178 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(2) != input->dimension(2));
179 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
180 ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type()));
181
182 dt = input->data_type();
183 is_quantized = is_data_type_quantized_asymmetric(dt);
184
185 if(biases != nullptr)
186 {
187 if(is_quantized)
188 {
189 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
190 }
191 else
192 {
193 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
194 }
195 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
196 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
197 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
198 }
199
200 append_bias = (biases != nullptr) && (!is_quantized);
201 are_weights_reshaped = weights_info.are_reshaped();
202 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(0);
203 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(1);
204 mat_weights_cols = weights->dimension(3);
205 mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
206
207 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 +0000208 conv_info, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000209
210 // Check if its a "fully connected" convolution
211 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000212 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000213
214 return Status{};
215}
216} // namespace
217
218NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000219 : _asm_glue(), _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(),
Georgios Pinitas1562be32018-03-08 19:09:19 +0000220 _output_col2im_kernel(), _original_weights(nullptr), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(), _tmp_output(), _workspace(), _append_bias(false),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000221 _is_fully_connected_convolution(false), _are_weights_reshaped(false), _is_quantized(false), _is_interleaved(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000222{
223}
224
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000225void 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 +0000226{
227 if(_is_quantized)
228 {
229 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
230 // Extract and negate input and weights offset
231 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
232 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
233
234 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
235 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
236
237 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
238
239 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
240 input->info()->set_quantization_info(input_quantization_info);
241 weights->info()->set_quantization_info(weights_quantization_info);
242 }
243 else
244 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000245 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000246 }
247}
248
Alex Gilday7da29b62018-03-23 14:16:00 +0000249void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
250 const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000251{
252 // Perform validate step
253 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
254
255 DataType dt{};
256 unsigned int kernel_width = 0;
257 unsigned int kernel_height = 0;
258 unsigned int mat_weights_cols = 0;
259 unsigned int mat_weights_rows = 0;
260 unsigned int conv_w = 0;
261 unsigned int conv_h = 0;
262
263 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,
264 kernel_width, kernel_height,
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000265 _is_fully_connected_convolution, _is_interleaved, _is_quantized,
Alex Gilday7da29b62018-03-23 14:16:00 +0000266 mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000267
268 ARM_COMPUTE_ERROR_THROW_ON(status);
269
Georgios Pinitas1562be32018-03-08 19:09:19 +0000270 _original_weights = weights;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000271 const unsigned int fixed_point_position = input->info()->fixed_point_position();
272 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
273
Pablo Tello7fad9b12018-03-14 17:55:27 +0000274 bool run_optimised = dt == DataType::F32;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000275
276 // Reshape weights if needed
Pablo Telloeb82fd22018-02-23 13:43:50 +0000277 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000278 {
279 if(_are_weights_reshaped)
280 {
281 mat_weights_cols = weights_info.num_kernels();
282 mat_weights_rows = weights->info()->dimension(1);
283 }
284 else
285 {
286 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
287
288 // Create tensor to store the reshaped weights
289 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
290 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
291 weights = &_weights_reshaped;
292 }
293 }
294 else
295 {
296 if(_are_weights_reshaped)
297 {
298 if(_is_fully_connected_convolution || _is_quantized)
299 {
300 mat_weights_cols = weights_info.num_kernels();
301 mat_weights_rows = weights->info()->dimension(1);
302 }
303 else
304 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000305 mat_weights_cols = weights_info.num_kernels();
306 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 +0000307 }
308 }
309 else
310 {
311 TensorShape reshaped_weights_shape;
312
313 if(_is_fully_connected_convolution || _is_quantized)
314 {
315 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
316 }
317 else
318 {
319 // Create tensor to store transposed weights
320 const float transpose_width = 16.0f / input->info()->element_size();
321 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
322 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
323 }
324
325 // Create tensor to store the reshaped weights
326 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000327 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000328 weights = &_weights_reshaped;
329 }
330 }
331
332 // Create tensor to store im2col reshaped inputs
333 const unsigned int mat_input_cols = mat_weights_rows;
334 const unsigned int mat_input_rows = conv_w * conv_h;
335
336 TensorShape shape_im2col(input->info()->tensor_shape());
337 shape_im2col.set(0, mat_input_cols);
338 shape_im2col.set(1, mat_input_rows);
339 shape_im2col.set(2, 1);
340 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
341 _memory_group.manage(&_input_im2col_reshaped);
342
343 // Create tensor (interleave) to prepare input tensor for GEMM
Pablo Telloeb82fd22018-02-23 13:43:50 +0000344 if(!_is_fully_connected_convolution && !run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000345 {
346 TensorShape shape_interleaved(shape_im2col);
347 shape_interleaved.set(0, shape_interleaved.x() * 4);
348 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
349 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
350 _memory_group.manage(&_input_interleaved_reshaped);
351 }
352
353 // Create GEMM output tensor
354 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
355 shape_gemm.set(0, mat_weights_cols);
356 shape_gemm.set(1, mat_input_rows);
357 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
358 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
359 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
360 info_gemm.set_quantization_info(output->info()->quantization_info());
361 _gemm_output.allocator()->init(info_gemm);
362 _memory_group.manage(&_gemm_output);
363
364 // Configure kernels
365 // Configure im2col
Alex Gilday7da29b62018-03-23 14:16:00 +0000366 _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 +0000367
368 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000369 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000370 {
Pablo Tello7fad9b12018-03-14 17:55:27 +0000371 if(!setup_assembly_kernel(&_input_im2col_reshaped, weights, &_gemm_output, 1.f, 0.f, _workspace, _memory_group, _asm_glue))
372 {
373 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
374 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000375 }
376 else
377 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000378 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000379 {
380 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
381 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
382
383 // Configure GEMM
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000384 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(1), 0 /* no transpose */,
385 _input_im2col_reshaped.info()->dimension(0)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000386 _input_interleaved_reshaped.allocator()->allocate();
387 }
388 else
389 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000390 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000391 }
392 }
393
394 _input_im2col_reshaped.allocator()->allocate();
395
396 // Configure output stage for quantized case
397 if(_is_quantized)
398 {
399 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
400
401 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
402 int output_multiplier, output_shift;
403 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
404 _memory_group.manage(&_tmp_output);
405 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
406 }
407
408 // Configure Col2Im
409 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
410 if(_is_quantized)
411 {
412 _tmp_output.allocator()->allocate();
413 }
414 _gemm_output.allocator()->allocate();
415
416 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");
417
418 // Allocate intermediate tensor
419 if(!_are_weights_reshaped)
420 {
421 _weights_reshaped.allocator()->allocate();
422 }
423}
424
425Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Alex Gilday7da29b62018-03-23 14:16:00 +0000426 const WeightsInfo &weights_info, const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000427{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000428 ARM_COMPUTE_UNUSED(output);
429
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000430 DataType dt{};
431 bool append_bias{};
432 bool are_weights_reshaped{};
433 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000434 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000435 bool is_quantized{};
436 unsigned int kernel_width = 0;
437 unsigned int kernel_height = 0;
438 unsigned int mat_weights_cols = 0;
439 unsigned int mat_weights_rows = 0;
440 unsigned int conv_w = 0;
441 unsigned int conv_h = 0;
442
443 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 +0000444 is_fully_connected_convolution, is_interleaved, is_quantized, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000445 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000446
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000447 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
448
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000449 ARM_COMPUTE_RETURN_ON_ERROR(status);
450
451 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
452 bool optimised_kernel = false;
453
Pablo Tello7fad9b12018-03-14 17:55:27 +0000454 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000455 {
456 optimised_kernel = true;
457 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000458
459 // Reshape weights if needed
460 if(optimised_kernel)
461 {
462 if(are_weights_reshaped)
463 {
464 mat_weights_cols = weights_info.num_kernels();
465 mat_weights_rows = weights->dimension(1);
466 }
467 else
468 {
469 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
470
471 // Create tensor to store the reshaped weights
472 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
473 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
474 weights = reshaped_weights.get();
475 }
476 }
477 else
478 {
479 if(are_weights_reshaped)
480 {
481 const unsigned int transpose_width = 16 / input->element_size();
482 mat_weights_cols = weights_info.num_kernels();
483 mat_weights_rows = weights->dimension(0) / transpose_width + (append_bias ? 1 : 0);
484 }
485 else
486 {
487 TensorShape reshaped_weights_shape;
488
489 if(is_fully_connected_convolution || is_quantized)
490 {
491 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
492 }
493 else
494 {
495 // Create tensor to store transposed weights
496 const float transpose_width = 16.0f / input->element_size();
497 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
498 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
499 }
500
501 // Create tensor to store the reshaped weights
502 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
503 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
504 weights = reshaped_weights.get();
505 }
506 }
507
508 // Validate im2col
509 const unsigned int mat_input_cols = mat_weights_rows;
510 const unsigned int mat_input_rows = conv_w * conv_h;
511 TensorShape shape_im2col = input->tensor_shape();
512 shape_im2col.set(0, mat_input_cols);
513 shape_im2col.set(1, mat_input_rows);
514 shape_im2col.set(2, 1);
515 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Alex Gilday7da29b62018-03-23 14:16:00 +0000516 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 +0000517
518 // Create GEMM output tensor
519 TensorShape shape_gemm(im2_col_info.tensor_shape());
520 shape_gemm.set(0, mat_weights_cols);
521 shape_gemm.set(1, mat_input_rows);
522 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
523
524 // Validate GEMM interleave and multiply
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000525 if(is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000526 {
527 TensorShape shape_interleaved = shape_im2col;
528 shape_interleaved.set(0, shape_interleaved.x() * 4);
529 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
530 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
531 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000532 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 +0000533 }
534 else
535 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000536 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 +0000537 }
538
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000539 return Status{};
540}
541
542void NEGEMMConvolutionLayer::run()
543{
544 // Run weights reshaping (Runs once for every configure)
545 if(!_are_weights_reshaped)
546 {
Georgios Pinitas1562be32018-03-08 19:09:19 +0000547 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
548
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000549 _are_weights_reshaped = true;
550 _reshape_weights.run();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000551
552 // Mark original weights tensor as unused
553 _original_weights->mark_as_unused();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000554 }
555
556 _memory_group.acquire();
557
558 // Run input reshaping
559 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
560
561 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000562 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000563 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000564 _asm_glue.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000565 }
566 else
567 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000568 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000569 {
570 // Run interleave
571 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
572 }
573
574 // Runs matrix multiply on reshaped matrices
575 if(_is_quantized)
576 {
577 _mm_gemmlowp.run();
578 }
579 else
580 {
581 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
582 }
583 }
584
585 // Run output stage for quantized case
586 if(_is_quantized)
587 {
588 _gemmlowp_output_stage.run();
589 }
590
591 // Reshape output matrix
592 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
593
594 _memory_group.release();
595}
596} // namespace arm_compute