blob: a5f30557a01113b6beb48a418210881d4ed26ce3 [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),
234 _is_interleaved(false), _is_activationlayer_enabled(false), _skip_im2col(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 Pinitas1562be32018-03-08 19:09:19 +0000290 _original_weights = weights;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000291 const unsigned int fixed_point_position = input->info()->fixed_point_position();
292 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
293
Pablo Tello7fad9b12018-03-14 17:55:27 +0000294 bool run_optimised = dt == DataType::F32;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000295
296 // Reshape weights if needed
Pablo Telloeb82fd22018-02-23 13:43:50 +0000297 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000298 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100299 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000300
Michalis Spyroue2503892018-04-23 15:17:31 +0100301 // Create tensor to store the reshaped weights
302 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
303 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
304 weights = &_weights_reshaped;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000305 }
306 else
307 {
308 if(_are_weights_reshaped)
309 {
310 if(_is_fully_connected_convolution || _is_quantized)
311 {
312 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100313 mat_weights_rows = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000314 }
315 else
316 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000317 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100318 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 +0000319 }
320 }
321 else
322 {
323 TensorShape reshaped_weights_shape;
324
325 if(_is_fully_connected_convolution || _is_quantized)
326 {
327 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
328 }
329 else
330 {
331 // Create tensor to store transposed weights
332 const float transpose_width = 16.0f / input->info()->element_size();
333 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
334 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
335 }
336
337 // Create tensor to store the reshaped weights
338 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt, fixed_point_position));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000339 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000340 weights = &_weights_reshaped;
341 }
342 }
343
Michalis Spyroue2503892018-04-23 15:17:31 +0100344 // In case we skip im2col we have to add bias
345 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000346 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100347 const unsigned int mat_input_cols = mat_weights_rows;
348 const unsigned int mat_input_rows = conv_w * conv_h;
349
350 // Create tensor to store im2col reshaped inputs
351 TensorShape shape_im2col(input->info()->tensor_shape());
352 shape_im2col.set(0, mat_input_cols);
353 shape_im2col.set(1, mat_input_rows);
354 shape_im2col.set(2, 1);
355 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
356 _memory_group.manage(&_input_im2col_reshaped);
357
358 // Create tensor (interleave) to prepare input tensor for GEMM
359 if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved)
360 {
361 TensorShape shape_interleaved(shape_im2col);
362 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
363 shape_interleaved.set(idx_height, std::ceil(shape_interleaved[idx_height] / 4.f));
364 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
365 _memory_group.manage(&_input_interleaved_reshaped);
366 }
367
368 // Create GEMM output tensor
369 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
370 shape_gemm.set(0, mat_weights_cols);
371 shape_gemm.set(1, mat_input_rows);
372 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
373 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
374 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
375 info_gemm.set_quantization_info(output->info()->quantization_info());
376 _gemm_output.allocator()->init(info_gemm);
377
378 // FIXME: enabling memory manager for _gemm_output gives incorrect results (maybe bound to the assembly kernel in GEMMLowp?)
379 // _memory_group.manage(&_gemm_output);
380
381 // Configure im2col
382 _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 +0000383 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100384 else if(_append_bias)
385 {
386 // Configure add bias kernel
387 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
388 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000389
390 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000391 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000392 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100393 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 +0000394 {
395 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
396 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000397 }
398 else
399 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000400 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000401 {
402 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
403 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
404
405 // Configure GEMM
Michalis Spyroue2503892018-04-23 15:17:31 +0100406 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */,
407 _input_im2col_reshaped.info()->dimension(idx_width)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000408 _input_interleaved_reshaped.allocator()->allocate();
409 }
410 else
411 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000412 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000413 }
414 }
415
Michalis Spyroue2503892018-04-23 15:17:31 +0100416 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000417 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100418 _input_im2col_reshaped.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000419
Michalis Spyroue2503892018-04-23 15:17:31 +0100420 // Configure output stage for quantized case
421 if(_is_quantized)
422 {
423 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
424
425 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
426 int output_multiplier, output_shift;
427 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
428 _memory_group.manage(&_tmp_output);
429 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
430 }
431
432 // Configure Col2Im
433 if(!is_nhwc)
434 {
435 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
436 }
437
438 if(_is_quantized)
439 {
440 _tmp_output.allocator()->allocate();
441 }
442 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000443 }
444
Michalis Spyroue2503892018-04-23 15:17:31 +0100445 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 +0000446
447 // Allocate intermediate tensor
448 if(!_are_weights_reshaped)
449 {
450 _weights_reshaped.allocator()->allocate();
451 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000452
453 //Configure Activation Layer
454 if(_is_activationlayer_enabled)
455 {
456 _activationlayer_function.configure(output, nullptr, act_info);
457 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000458}
459
460Status 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 +0000461 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000462{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000463 ARM_COMPUTE_UNUSED(output);
464
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000465 DataType dt{};
466 bool append_bias{};
Michalis Spyroue2503892018-04-23 15:17:31 +0100467 bool skip_im2col{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000468 bool are_weights_reshaped{};
469 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000470 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000471 bool is_quantized{};
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000472 bool is_activationlayer_enabled{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000473 unsigned int kernel_width = 0;
474 unsigned int kernel_height = 0;
475 unsigned int mat_weights_cols = 0;
476 unsigned int mat_weights_rows = 0;
477 unsigned int conv_w = 0;
478 unsigned int conv_h = 0;
479
Michalis Spyroue2503892018-04-23 15:17:31 +0100480 const DataLayout data_layout = input->data_layout();
481 const bool is_nhwc = data_layout == DataLayout::NHWC;
482 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
483 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
484
485 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 +0000486 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000487 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000488
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000489 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
490
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000491 ARM_COMPUTE_RETURN_ON_ERROR(status);
492
493 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
494 bool optimised_kernel = false;
495
Pablo Tello7fad9b12018-03-14 17:55:27 +0000496 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000497 {
498 optimised_kernel = true;
499 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000500
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000501 const unsigned int mat_input_cols = mat_weights_rows;
502 const unsigned int mat_input_rows = conv_w * conv_h;
503 TensorShape shape_im2col = input->tensor_shape();
504 shape_im2col.set(0, mat_input_cols);
505 shape_im2col.set(1, mat_input_rows);
506 shape_im2col.set(2, 1);
507 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Michalis Spyroue2503892018-04-23 15:17:31 +0100508
509 if(!skip_im2col)
510 {
511 // Validate im2col
512 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
513 }
514 else if(append_bias)
515 {
516 // Validate add bias kernel
517 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
518 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000519
520 // Create GEMM output tensor
521 TensorShape shape_gemm(im2_col_info.tensor_shape());
522 shape_gemm.set(0, mat_weights_cols);
523 shape_gemm.set(1, mat_input_rows);
524 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
525
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100526 // Reshape weights if needed
527 if(optimised_kernel)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000528 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100529 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
530
531 // Create tensor to store the reshaped weights
532 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
533 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 +0000534 }
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100535 else if(!is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000536 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100537 TensorShape reshaped_weights_shape;
538
539 if(is_fully_connected_convolution || is_quantized)
540 {
541 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
542 }
543 else
544 {
545 // Create tensor to store transposed weights
546 const float transpose_width = 16.0f / input->element_size();
547 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
548 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
549 }
550
551 // Create tensor to store the reshaped weights
552 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
553 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
554 weights = reshaped_weights.get();
555
556 // Validate GEMM interleave and multiply
557 if(is_interleaved)
558 {
559 TensorShape shape_interleaved = shape_im2col;
Michalis Spyroue2503892018-04-23 15:17:31 +0100560 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
561 shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f));
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100562 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
563 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
564 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
565 weights->tensor_shape()[0], // n
566 shape_im2col[0]) /* k */));
567 }
568 else
569 {
570 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
571 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000572 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100573 if(!is_nhwc)
574 {
575 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
576 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000577
Michalis Spyroue2503892018-04-23 15:17:31 +0100578 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 +0000579
580 if(act_info.enabled())
581 {
582 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
583 }
584
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000585 return Status{};
586}
587
588void NEGEMMConvolutionLayer::run()
589{
590 // Run weights reshaping (Runs once for every configure)
591 if(!_are_weights_reshaped)
592 {
Georgios Pinitas1562be32018-03-08 19:09:19 +0000593 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
594
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000595 _are_weights_reshaped = true;
596 _reshape_weights.run();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000597
598 // Mark original weights tensor as unused
599 _original_weights->mark_as_unused();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000600 }
601
602 _memory_group.acquire();
603
Michalis Spyroue2503892018-04-23 15:17:31 +0100604 if(!_skip_im2col)
605 {
606 // Run input reshaping
607 unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
608 NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim);
609 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000610
611 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000612 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000613 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000614 _asm_glue.run();
Georgios Pinitas932b5612018-05-03 13:44:35 +0100615 // Release weights in case buffer is pretransposed
Georgios Pinitasd8cde852018-05-08 18:58:19 +0100616 if(!_weights_reshaped.is_used())
Georgios Pinitas932b5612018-05-03 13:44:35 +0100617 {
618 _weights_reshaped.allocator()->free();
619 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000620 }
621 else
622 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000623 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000624 {
625 // Run interleave
626 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
627 }
628
629 // Runs matrix multiply on reshaped matrices
630 if(_is_quantized)
631 {
632 _mm_gemmlowp.run();
633 }
634 else
635 {
636 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
637 }
638 }
639
Michalis Spyroue2503892018-04-23 15:17:31 +0100640 if(_skip_im2col && _append_bias)
641 {
642 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
643 }
644
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000645 // Run output stage for quantized case
646 if(_is_quantized)
647 {
648 _gemmlowp_output_stage.run();
649 }
650
651 // Reshape output matrix
Michalis Spyroue2503892018-04-23 15:17:31 +0100652 if(_data_layout == DataLayout::NCHW)
653 {
654 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
655 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000656
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000657 if(_is_activationlayer_enabled)
658 {
659 _activationlayer_function.run();
660 }
661
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000662 _memory_group.release();
663}
664} // namespace arm_compute