blob: d4400b8864c2a6d38a1ccda44a0cc7e91d51f279 [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
Michalis Spyroue2503892018-04-23 15:17:31 +0100112 // 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
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000120 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
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000168Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
169 const ActivationLayerInfo &act_info, DataType &dt,
Michalis Spyroue2503892018-04-23 15:17:31 +0100170 bool &append_bias, bool &skip_im2col,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000171 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000172 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized, bool &is_activationlayer_enabled,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000173 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000174 unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000175{
176 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
177 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
178 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Michalis Spyroue2503892018-04-23 15:17:31 +0100179 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
180
181 DataLayout data_layout = input->data_layout();
182 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
183 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
184 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
185
186 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(idx_channel) != input->dimension(idx_channel));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000187 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
188 ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type()));
Michalis Spyroue2503892018-04-23 15:17:31 +0100189 ARM_COMPUTE_RETURN_ERROR_ON_MSG(data_layout == DataLayout::NHWC && input->data_type() != DataType::F32, "NHWC is only supported for FP32 data type.");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000190
191 dt = input->data_type();
192 is_quantized = is_data_type_quantized_asymmetric(dt);
193
194 if(biases != nullptr)
195 {
196 if(is_quantized)
197 {
198 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
199 }
200 else
201 {
202 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
203 }
204 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
205 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
206 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
207 }
208
Michalis Spyroue2503892018-04-23 15:17:31 +0100209 // If we have 1x1 convolution and data layout is NHWC we can disable im2col
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000210 append_bias = (biases != nullptr) && (!is_quantized);
211 are_weights_reshaped = weights_info.are_reshaped();
Michalis Spyroue2503892018-04-23 15:17:31 +0100212 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(idx_width);
213 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000214 mat_weights_cols = weights->dimension(3);
Michalis Spyroue2503892018-04-23 15:17:31 +0100215 mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + ((append_bias && !skip_im2col) ? 1 : 0);
216 skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000217
Michalis Spyroue2503892018-04-23 15:17:31 +0100218 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width), input->dimension(idx_height), kernel_width, kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000219 conv_info, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000220
221 // Check if its a "fully connected" convolution
222 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000223 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000224 is_activationlayer_enabled = act_info.enabled();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000225
226 return Status{};
227}
228} // namespace
229
230NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000231 : _asm_glue(), _memory_group(memory_manager), _input_im2col_kernel(), _input_interleave_kernel(), _reshape_weights(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(),
Michalis Spyroue2503892018-04-23 15:17:31 +0100232 _output_col2im_kernel(), _activationlayer_function(), _add_bias_kernel(), _original_weights(nullptr), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(),
233 _tmp_output(), _workspace(), _B_pretransposed(), _data_layout(DataLayout::NCHW), _append_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false), _is_quantized(false),
Georgios Pinitas72219332018-06-05 14:56:06 +0100234 _is_interleaved(false), _is_activationlayer_enabled(false), _skip_im2col(false), _is_prepared(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000235{
236}
237
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000238void 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 +0000239{
240 if(_is_quantized)
241 {
242 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
243 // Extract and negate input and weights offset
244 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
245 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
246
247 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
248 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
249
250 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
251
252 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
253 input->info()->set_quantization_info(input_quantization_info);
254 weights->info()->set_quantization_info(weights_quantization_info);
255 }
256 else
257 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000258 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000259 }
260}
261
Alex Gilday7da29b62018-03-23 14:16:00 +0000262void 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 +0000263 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000264{
265 // Perform validate step
266 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
267
268 DataType dt{};
269 unsigned int kernel_width = 0;
270 unsigned int kernel_height = 0;
271 unsigned int mat_weights_cols = 0;
272 unsigned int mat_weights_rows = 0;
273 unsigned int conv_w = 0;
274 unsigned int conv_h = 0;
275
Michalis Spyroue2503892018-04-23 15:17:31 +0100276 _data_layout = input->info()->data_layout();
277 const bool is_nhwc = _data_layout == DataLayout::NHWC;
278 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
279 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
280 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
281
282 Status status = validate_and_initialize_values(input->info(), weights->info(), (biases == nullptr) ? nullptr : biases->info(), conv_info, weights_info, act_info, dt, _append_bias, _skip_im2col,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000283 _are_weights_reshaped,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000284 kernel_width, kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000285 _is_fully_connected_convolution, _is_interleaved, _is_quantized, _is_activationlayer_enabled,
Alex Gilday7da29b62018-03-23 14:16:00 +0000286 mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000287
288 ARM_COMPUTE_ERROR_THROW_ON(status);
289
Georgios Pinitas72219332018-06-05 14:56:06 +0100290 _is_prepared = false;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000291 _original_weights = weights;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000292 const unsigned int fixed_point_position = input->info()->fixed_point_position();
293 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
294
Pablo Tello7fad9b12018-03-14 17:55:27 +0000295 bool run_optimised = dt == DataType::F32;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000296
297 // Reshape weights if needed
Pablo Telloeb82fd22018-02-23 13:43:50 +0000298 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000299 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100300 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000301
Michalis Spyroue2503892018-04-23 15:17:31 +0100302 // Create tensor to store the reshaped weights
303 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
304 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
305 weights = &_weights_reshaped;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000306 }
307 else
308 {
309 if(_are_weights_reshaped)
310 {
311 if(_is_fully_connected_convolution || _is_quantized)
312 {
313 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100314 mat_weights_rows = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000315 }
316 else
317 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000318 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100319 mat_weights_rows = weights_info.kernel_size().first * weights_info.kernel_size().second * input->info()->dimension(idx_channel) + (_append_bias ? 1 : 0);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000320 }
321 }
322 else
323 {
324 TensorShape reshaped_weights_shape;
325
326 if(_is_fully_connected_convolution || _is_quantized)
327 {
328 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
329 }
330 else
331 {
332 // Create tensor to store transposed weights
333 const float transpose_width = 16.0f / input->info()->element_size();
334 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
335 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
336 }
337
338 // Create tensor to store the reshaped weights
339 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000340 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000341 weights = &_weights_reshaped;
342 }
343 }
344
Michalis Spyroue2503892018-04-23 15:17:31 +0100345 // In case we skip im2col we have to add bias
346 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000347 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100348 const unsigned int mat_input_cols = mat_weights_rows;
349 const unsigned int mat_input_rows = conv_w * conv_h;
350
351 // Create tensor to store im2col reshaped inputs
352 TensorShape shape_im2col(input->info()->tensor_shape());
353 shape_im2col.set(0, mat_input_cols);
354 shape_im2col.set(1, mat_input_rows);
355 shape_im2col.set(2, 1);
356 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
357 _memory_group.manage(&_input_im2col_reshaped);
358
359 // Create tensor (interleave) to prepare input tensor for GEMM
360 if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved)
361 {
362 TensorShape shape_interleaved(shape_im2col);
363 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
364 shape_interleaved.set(idx_height, std::ceil(shape_interleaved[idx_height] / 4.f));
365 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
366 _memory_group.manage(&_input_interleaved_reshaped);
367 }
368
369 // Create GEMM output tensor
370 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
371 shape_gemm.set(0, mat_weights_cols);
372 shape_gemm.set(1, mat_input_rows);
373 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
374 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
375 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
376 info_gemm.set_quantization_info(output->info()->quantization_info());
377 _gemm_output.allocator()->init(info_gemm);
Georgios Pinitas2345f432018-05-18 20:13:01 +0100378 _memory_group.manage(&_gemm_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100379
380 // Configure im2col
381 _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 +0000382 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100383 else if(_append_bias)
384 {
385 // Configure add bias kernel
386 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
387 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000388
389 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000390 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000391 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100392 if(!setup_assembly_kernel(_skip_im2col ? input : &_input_im2col_reshaped, weights, is_nhwc ? output : &_gemm_output, 1.f, 0.f, true, _workspace, _B_pretransposed, _memory_group, _asm_glue))
Pablo Tello7fad9b12018-03-14 17:55:27 +0000393 {
394 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
395 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000396 }
397 else
398 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000399 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000400 {
401 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
402 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
403
404 // Configure GEMM
Michalis Spyroue2503892018-04-23 15:17:31 +0100405 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */,
406 _input_im2col_reshaped.info()->dimension(idx_width)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000407 _input_interleaved_reshaped.allocator()->allocate();
408 }
409 else
410 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000411 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000412 }
413 }
414
Michalis Spyroue2503892018-04-23 15:17:31 +0100415 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000416 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100417 _input_im2col_reshaped.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000418
Michalis Spyroue2503892018-04-23 15:17:31 +0100419 // Configure output stage for quantized case
420 if(_is_quantized)
421 {
422 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
423
424 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
425 int output_multiplier, output_shift;
426 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
427 _memory_group.manage(&_tmp_output);
428 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
429 }
430
431 // Configure Col2Im
432 if(!is_nhwc)
433 {
434 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
435 }
436
437 if(_is_quantized)
438 {
439 _tmp_output.allocator()->allocate();
440 }
441 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000442 }
443
Michalis Spyroue2503892018-04-23 15:17:31 +0100444 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h), "Output shape does not match the expected one");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000445
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000446 //Configure Activation Layer
447 if(_is_activationlayer_enabled)
448 {
449 _activationlayer_function.configure(output, nullptr, act_info);
450 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000451}
452
453Status 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 +0000454 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000455{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000456 ARM_COMPUTE_UNUSED(output);
457
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000458 DataType dt{};
459 bool append_bias{};
Michalis Spyroue2503892018-04-23 15:17:31 +0100460 bool skip_im2col{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000461 bool are_weights_reshaped{};
462 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000463 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000464 bool is_quantized{};
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000465 bool is_activationlayer_enabled{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000466 unsigned int kernel_width = 0;
467 unsigned int kernel_height = 0;
468 unsigned int mat_weights_cols = 0;
469 unsigned int mat_weights_rows = 0;
470 unsigned int conv_w = 0;
471 unsigned int conv_h = 0;
472
Michalis Spyroue2503892018-04-23 15:17:31 +0100473 const DataLayout data_layout = input->data_layout();
474 const bool is_nhwc = data_layout == DataLayout::NHWC;
475 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
476 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
477
478 Status status = validate_and_initialize_values(input, weights, biases, conv_info, weights_info, act_info, dt, append_bias, skip_im2col, are_weights_reshaped, kernel_width, kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000479 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000480 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000481
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000482 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
483
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000484 ARM_COMPUTE_RETURN_ON_ERROR(status);
485
486 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
487 bool optimised_kernel = false;
488
Pablo Tello7fad9b12018-03-14 17:55:27 +0000489 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000490 {
491 optimised_kernel = true;
492 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000493
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000494 const unsigned int mat_input_cols = mat_weights_rows;
495 const unsigned int mat_input_rows = conv_w * conv_h;
496 TensorShape shape_im2col = input->tensor_shape();
497 shape_im2col.set(0, mat_input_cols);
498 shape_im2col.set(1, mat_input_rows);
499 shape_im2col.set(2, 1);
500 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Michalis Spyroue2503892018-04-23 15:17:31 +0100501
502 if(!skip_im2col)
503 {
504 // Validate im2col
505 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
506 }
507 else if(append_bias)
508 {
509 // Validate add bias kernel
510 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
511 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000512
513 // Create GEMM output tensor
514 TensorShape shape_gemm(im2_col_info.tensor_shape());
515 shape_gemm.set(0, mat_weights_cols);
516 shape_gemm.set(1, mat_input_rows);
517 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
518
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100519 // Reshape weights if needed
520 if(optimised_kernel)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000521 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100522 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
523
524 // Create tensor to store the reshaped weights
525 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
526 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 +0000527 }
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100528 else if(!is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000529 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100530 TensorShape reshaped_weights_shape;
531
532 if(is_fully_connected_convolution || is_quantized)
533 {
534 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
535 }
536 else
537 {
538 // Create tensor to store transposed weights
539 const float transpose_width = 16.0f / input->element_size();
540 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
541 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
542 }
543
544 // Create tensor to store the reshaped weights
545 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
546 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
547 weights = reshaped_weights.get();
548
549 // Validate GEMM interleave and multiply
550 if(is_interleaved)
551 {
552 TensorShape shape_interleaved = shape_im2col;
Michalis Spyroue2503892018-04-23 15:17:31 +0100553 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
554 shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f));
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100555 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
556 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
557 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
558 weights->tensor_shape()[0], // n
559 shape_im2col[0]) /* k */));
560 }
561 else
562 {
563 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
564 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000565 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100566 if(!is_nhwc)
567 {
568 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
569 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000570
Michalis Spyroue2503892018-04-23 15:17:31 +0100571 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(idx_width) != conv_w) || (output->dimension(idx_height) != conv_h), "Output shape does not match the expected one");
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000572
573 if(act_info.enabled())
574 {
575 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
576 }
577
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000578 return Status{};
579}
580
581void NEGEMMConvolutionLayer::run()
582{
Georgios Pinitas72219332018-06-05 14:56:06 +0100583 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000584
585 _memory_group.acquire();
586
Michalis Spyroue2503892018-04-23 15:17:31 +0100587 if(!_skip_im2col)
588 {
589 // Run input reshaping
590 unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
591 NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim);
592 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000593
594 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000595 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000596 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000597 _asm_glue.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000598 }
599 else
600 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000601 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000602 {
603 // Run interleave
604 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
605 }
606
607 // Runs matrix multiply on reshaped matrices
608 if(_is_quantized)
609 {
610 _mm_gemmlowp.run();
611 }
612 else
613 {
614 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
615 }
616 }
617
Michalis Spyroue2503892018-04-23 15:17:31 +0100618 if(_skip_im2col && _append_bias)
619 {
620 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
621 }
622
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000623 // Run output stage for quantized case
624 if(_is_quantized)
625 {
626 _gemmlowp_output_stage.run();
627 }
628
629 // Reshape output matrix
Michalis Spyroue2503892018-04-23 15:17:31 +0100630 if(_data_layout == DataLayout::NCHW)
631 {
632 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
633 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000634
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000635 if(_is_activationlayer_enabled)
636 {
637 _activationlayer_function.run();
638 }
639
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000640 _memory_group.release();
641}
Georgios Pinitas72219332018-06-05 14:56:06 +0100642
643void NEGEMMConvolutionLayer::prepare()
644{
645 if(!_is_prepared)
646 {
647 // Run weights reshaping (Runs once for every configure)
648 if(!_are_weights_reshaped)
649 {
650 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
651
652 _weights_reshaped.allocator()->allocate();
653 _reshape_weights.run();
654 _reshape_weights = NEConvolutionLayerReshapeWeights();
655 _original_weights->mark_as_unused();
656 _are_weights_reshaped = true;
657 }
658
659 // Run GEMM prepare stage
660 if(_asm_glue._optimised_kernel)
661 {
662 _asm_glue.prepare();
663 }
664 else
665 {
666 if(_is_quantized)
667 {
668 _mm_gemmlowp.prepare();
669 }
670 }
671
672 // Release weights in case buffer is pretransposed
673 if(!_weights_reshaped.is_used())
674 {
675 _weights_reshaped.allocator()->free();
676 }
677
678 _is_prepared = true;
679 }
680}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000681} // namespace arm_compute