blob: 303691aa7a219bba898d766c03d46afb0496081b [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);
Georgios Pinitas2345f432018-05-18 20:13:01 +0100377 _memory_group.manage(&_gemm_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100378
379 // Configure im2col
380 _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 +0000381 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100382 else if(_append_bias)
383 {
384 // Configure add bias kernel
385 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
386 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000387
388 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000389 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000390 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100391 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 +0000392 {
393 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
394 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000395 }
396 else
397 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000398 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000399 {
400 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
401 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
402
403 // Configure GEMM
Michalis Spyroue2503892018-04-23 15:17:31 +0100404 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */,
405 _input_im2col_reshaped.info()->dimension(idx_width)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000406 _input_interleaved_reshaped.allocator()->allocate();
407 }
408 else
409 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000410 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000411 }
412 }
413
Michalis Spyroue2503892018-04-23 15:17:31 +0100414 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000415 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100416 _input_im2col_reshaped.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000417
Michalis Spyroue2503892018-04-23 15:17:31 +0100418 // Configure output stage for quantized case
419 if(_is_quantized)
420 {
421 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
422
423 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
424 int output_multiplier, output_shift;
425 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
426 _memory_group.manage(&_tmp_output);
427 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
428 }
429
430 // Configure Col2Im
431 if(!is_nhwc)
432 {
433 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
434 }
435
436 if(_is_quantized)
437 {
438 _tmp_output.allocator()->allocate();
439 }
440 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000441 }
442
Michalis Spyroue2503892018-04-23 15:17:31 +0100443 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 +0000444
445 // Allocate intermediate tensor
446 if(!_are_weights_reshaped)
447 {
448 _weights_reshaped.allocator()->allocate();
449 }
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000450
451 //Configure Activation Layer
452 if(_is_activationlayer_enabled)
453 {
454 _activationlayer_function.configure(output, nullptr, act_info);
455 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000456}
457
458Status 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 +0000459 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000460{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000461 ARM_COMPUTE_UNUSED(output);
462
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000463 DataType dt{};
464 bool append_bias{};
Michalis Spyroue2503892018-04-23 15:17:31 +0100465 bool skip_im2col{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000466 bool are_weights_reshaped{};
467 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000468 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000469 bool is_quantized{};
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000470 bool is_activationlayer_enabled{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000471 unsigned int kernel_width = 0;
472 unsigned int kernel_height = 0;
473 unsigned int mat_weights_cols = 0;
474 unsigned int mat_weights_rows = 0;
475 unsigned int conv_w = 0;
476 unsigned int conv_h = 0;
477
Michalis Spyroue2503892018-04-23 15:17:31 +0100478 const DataLayout data_layout = input->data_layout();
479 const bool is_nhwc = data_layout == DataLayout::NHWC;
480 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
481 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
482
483 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 +0000484 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000485 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000486
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000487 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
488
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000489 ARM_COMPUTE_RETURN_ON_ERROR(status);
490
491 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
492 bool optimised_kernel = false;
493
Pablo Tello7fad9b12018-03-14 17:55:27 +0000494 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000495 {
496 optimised_kernel = true;
497 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000498
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000499 const unsigned int mat_input_cols = mat_weights_rows;
500 const unsigned int mat_input_rows = conv_w * conv_h;
501 TensorShape shape_im2col = input->tensor_shape();
502 shape_im2col.set(0, mat_input_cols);
503 shape_im2col.set(1, mat_input_rows);
504 shape_im2col.set(2, 1);
505 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Michalis Spyroue2503892018-04-23 15:17:31 +0100506
507 if(!skip_im2col)
508 {
509 // Validate im2col
510 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
511 }
512 else if(append_bias)
513 {
514 // Validate add bias kernel
515 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
516 }
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
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100524 // Reshape weights if needed
525 if(optimised_kernel)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000526 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100527 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
528
529 // Create tensor to store the reshaped weights
530 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
531 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 +0000532 }
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100533 else if(!is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000534 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100535 TensorShape reshaped_weights_shape;
536
537 if(is_fully_connected_convolution || is_quantized)
538 {
539 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
540 }
541 else
542 {
543 // Create tensor to store transposed weights
544 const float transpose_width = 16.0f / input->element_size();
545 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
546 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
547 }
548
549 // Create tensor to store the reshaped weights
550 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
551 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
552 weights = reshaped_weights.get();
553
554 // Validate GEMM interleave and multiply
555 if(is_interleaved)
556 {
557 TensorShape shape_interleaved = shape_im2col;
Michalis Spyroue2503892018-04-23 15:17:31 +0100558 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
559 shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f));
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100560 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
561 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
562 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
563 weights->tensor_shape()[0], // n
564 shape_im2col[0]) /* k */));
565 }
566 else
567 {
568 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
569 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000570 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100571 if(!is_nhwc)
572 {
573 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
574 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000575
Michalis Spyroue2503892018-04-23 15:17:31 +0100576 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 +0000577
578 if(act_info.enabled())
579 {
580 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
581 }
582
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000583 return Status{};
584}
585
586void NEGEMMConvolutionLayer::run()
587{
588 // Run weights reshaping (Runs once for every configure)
589 if(!_are_weights_reshaped)
590 {
Georgios Pinitas1562be32018-03-08 19:09:19 +0000591 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
592
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000593 _are_weights_reshaped = true;
594 _reshape_weights.run();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000595
596 // Mark original weights tensor as unused
597 _original_weights->mark_as_unused();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000598 }
599
600 _memory_group.acquire();
601
Michalis Spyroue2503892018-04-23 15:17:31 +0100602 if(!_skip_im2col)
603 {
604 // Run input reshaping
605 unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
606 NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim);
607 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000608
609 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000610 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000611 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000612 _asm_glue.run();
Georgios Pinitas932b5612018-05-03 13:44:35 +0100613 // Release weights in case buffer is pretransposed
Georgios Pinitasd8cde852018-05-08 18:58:19 +0100614 if(!_weights_reshaped.is_used())
Georgios Pinitas932b5612018-05-03 13:44:35 +0100615 {
616 _weights_reshaped.allocator()->free();
617 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000618 }
619 else
620 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000621 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000622 {
623 // Run interleave
624 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
625 }
626
627 // Runs matrix multiply on reshaped matrices
628 if(_is_quantized)
629 {
630 _mm_gemmlowp.run();
631 }
632 else
633 {
634 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
635 }
636 }
637
Michalis Spyroue2503892018-04-23 15:17:31 +0100638 if(_skip_im2col && _append_bias)
639 {
640 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
641 }
642
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000643 // Run output stage for quantized case
644 if(_is_quantized)
645 {
646 _gemmlowp_output_stage.run();
647 }
648
649 // Reshape output matrix
Michalis Spyroue2503892018-04-23 15:17:31 +0100650 if(_data_layout == DataLayout::NCHW)
651 {
652 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
653 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000654
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000655 if(_is_activationlayer_enabled)
656 {
657 _activationlayer_function.run();
658 }
659
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000660 _memory_group.release();
661}
662} // namespace arm_compute