blob: 94ef4e7b32157859a28cd363b6d8b8b62956f8c6 [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{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010093 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000094 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);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000098 }
99 // Check if bias are present, if yes they will be embedded to the weights matrix
100 const bool append_bias = (biases != nullptr);
101
102 if(append_bias)
103 {
104 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000106 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
107 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
108 }
109
Michalis Spyroue2503892018-04-23 15:17:31 +0100110 // Checks performed when biases are present
111 if(append_bias)
112 {
113 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
114 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
115 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
116 }
117
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000118 if(transpose1xW)
119 {
120 TensorInfo weights_reshaped = weights->clone()->set_tensor_shape(get_reshaped_weights_shape(weights, append_bias));
121 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped));
122 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(&weights_reshaped, output));
123 }
124 else
125 {
126 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, output));
127 }
128
129 return Status{};
130}
131
132void NEConvolutionLayerReshapeWeights::run()
133{
134 _memory_group.acquire();
135
136 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
137
138 if(_transpose1xW)
139 {
140 NEScheduler::get().schedule(&_weights_transposed_kernel, Window::DimY);
141 }
142
143 _memory_group.release();
144}
145
146namespace
147{
148TensorShape get_reshaped_weights_shape_conv(const ITensorInfo *weights, bool append_bias, bool is_fully_connected_convolution)
149{
150 unsigned int mat_weights_cols = weights->dimension(3);
151 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + (append_bias ? 1 : 0);
152
153 if(is_fully_connected_convolution)
154 {
155 // Create tensor to store the reshaped weights
156 return TensorShape(mat_weights_cols, mat_weights_rows);
157 }
158 else
159 {
160 // Create tensor to store transposed weights
161 const float transpose_width = 16.0f / weights->element_size();
162 return TensorShape(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
163 }
164}
165
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000166Status validate_and_initialize_values(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
167 const ActivationLayerInfo &act_info, DataType &dt,
Michalis Spyroue2503892018-04-23 15:17:31 +0100168 bool &append_bias, bool &skip_im2col,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000169 bool &are_weights_reshaped, unsigned int &kernel_width, unsigned int &kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000170 bool &is_fully_connected_convolution, bool &is_interleaved, bool &is_quantized, bool &is_activationlayer_enabled,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000171 unsigned int &mat_weights_cols, unsigned int &mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000172 unsigned int &conv_w, unsigned int &conv_h, const Size2D &dilation)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000173{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100174 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000175 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michalis Spyroue2503892018-04-23 15:17:31 +0100176 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
177
178 DataLayout data_layout = input->data_layout();
179 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
180 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
181 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
182
183 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && weights->dimension(idx_channel) != input->dimension(idx_channel));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000184 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
185 ARM_COMPUTE_RETURN_ERROR_ON(weights_info.are_reshaped() && is_data_type_quantized_asymmetric(input->data_type()));
Michalis Spyroue2503892018-04-23 15:17:31 +0100186 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 +0000187
188 dt = input->data_type();
189 is_quantized = is_data_type_quantized_asymmetric(dt);
190
191 if(biases != nullptr)
192 {
193 if(is_quantized)
194 {
195 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
196 }
197 else
198 {
199 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
200 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000201 ARM_COMPUTE_RETURN_ERROR_ON(!weights_info.are_reshaped() && biases->dimension(0) != weights->dimension(3));
202 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
203 }
204
Michalis Spyroue2503892018-04-23 15:17:31 +0100205 // If we have 1x1 convolution and data layout is NHWC we can disable im2col
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000206 append_bias = (biases != nullptr) && (!is_quantized);
207 are_weights_reshaped = weights_info.are_reshaped();
Michalis Spyroue2503892018-04-23 15:17:31 +0100208 kernel_width = (are_weights_reshaped) ? weights_info.kernel_size().first : weights->dimension(idx_width);
209 kernel_height = (are_weights_reshaped) ? weights_info.kernel_size().second : weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000210 mat_weights_cols = weights->dimension(3);
Michalis Spyroue2503892018-04-23 15:17:31 +0100211 mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + ((append_bias && !skip_im2col) ? 1 : 0);
212 skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000213
Michalis Spyroue2503892018-04-23 15:17:31 +0100214 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 +0000215 conv_info, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000216
217 // Check if its a "fully connected" convolution
218 is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000219 is_interleaved = (!is_fully_connected_convolution && !is_quantized);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000220 is_activationlayer_enabled = act_info.enabled();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000221
222 return Status{};
223}
224} // namespace
225
226NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000227 : _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 +0100228 _output_col2im_kernel(), _activationlayer_function(), _add_bias_kernel(), _original_weights(nullptr), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _gemm_output(),
229 _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 +0100230 _is_interleaved(false), _is_activationlayer_enabled(false), _skip_im2col(false), _is_prepared(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000231{
232}
233
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000234void 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 +0000235{
236 if(_is_quantized)
237 {
238 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
239 // Extract and negate input and weights offset
240 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
241 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
242
243 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
244 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
245
246 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
247
248 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
249 input->info()->set_quantization_info(input_quantization_info);
250 weights->info()->set_quantization_info(weights_quantization_info);
251 }
252 else
253 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000254 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved, reshape_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000255 }
256}
257
Alex Gilday7da29b62018-03-23 14:16:00 +0000258void 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 +0000259 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000260{
261 // Perform validate step
262 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
263
264 DataType dt{};
265 unsigned int kernel_width = 0;
266 unsigned int kernel_height = 0;
267 unsigned int mat_weights_cols = 0;
268 unsigned int mat_weights_rows = 0;
269 unsigned int conv_w = 0;
270 unsigned int conv_h = 0;
271
Michalis Spyroue2503892018-04-23 15:17:31 +0100272 _data_layout = input->info()->data_layout();
273 const bool is_nhwc = _data_layout == DataLayout::NHWC;
274 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
275 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
276 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
277
278 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 +0000279 _are_weights_reshaped,
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000280 kernel_width, kernel_height,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000281 _is_fully_connected_convolution, _is_interleaved, _is_quantized, _is_activationlayer_enabled,
Alex Gilday7da29b62018-03-23 14:16:00 +0000282 mat_weights_cols, mat_weights_rows, conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000283
284 ARM_COMPUTE_ERROR_THROW_ON(status);
285
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100286 _is_prepared = false;
287 _original_weights = weights;
288 const ITensor *biases_to_use = (_append_bias) ? biases : nullptr;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000289
Pablo Tello7fad9b12018-03-14 17:55:27 +0000290 bool run_optimised = dt == DataType::F32;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000291
292 // Reshape weights if needed
Pablo Telloeb82fd22018-02-23 13:43:50 +0000293 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000294 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100295 TensorShape reshaped_weights_shape{ mat_weights_cols, mat_weights_rows };
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000296
Michalis Spyroue2503892018-04-23 15:17:31 +0100297 // Create tensor to store the reshaped weights
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100298 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt));
Michalis Spyroue2503892018-04-23 15:17:31 +0100299 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
300 weights = &_weights_reshaped;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000301 }
302 else
303 {
304 if(_are_weights_reshaped)
305 {
306 if(_is_fully_connected_convolution || _is_quantized)
307 {
308 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100309 mat_weights_rows = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000310 }
311 else
312 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000313 mat_weights_cols = weights_info.num_kernels();
Michalis Spyroue2503892018-04-23 15:17:31 +0100314 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 +0000315 }
316 }
317 else
318 {
319 TensorShape reshaped_weights_shape;
320
321 if(_is_fully_connected_convolution || _is_quantized)
322 {
323 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
324 }
325 else
326 {
327 // Create tensor to store transposed weights
328 const float transpose_width = 16.0f / input->info()->element_size();
329 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
330 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
331 }
332
333 // Create tensor to store the reshaped weights
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100334 _weights_reshaped.allocator()->init(TensorInfo(reshaped_weights_shape, 1, dt));
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000335 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, _is_interleaved /* 1xW transpose */);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000336 weights = &_weights_reshaped;
337 }
338 }
339
Michalis Spyroue2503892018-04-23 15:17:31 +0100340 // In case we skip im2col we have to add bias
341 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000342 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100343 const unsigned int mat_input_cols = mat_weights_rows;
344 const unsigned int mat_input_rows = conv_w * conv_h;
345
346 // Create tensor to store im2col reshaped inputs
347 TensorShape shape_im2col(input->info()->tensor_shape());
348 shape_im2col.set(0, mat_input_cols);
349 shape_im2col.set(1, mat_input_rows);
350 shape_im2col.set(2, 1);
351 _input_im2col_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
352 _memory_group.manage(&_input_im2col_reshaped);
353
354 // Create tensor (interleave) to prepare input tensor for GEMM
355 if(!_is_fully_connected_convolution && !run_optimised && _is_interleaved)
356 {
357 TensorShape shape_interleaved(shape_im2col);
358 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
359 shape_interleaved.set(idx_height, std::ceil(shape_interleaved[idx_height] / 4.f));
360 _input_interleaved_reshaped.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_interleaved));
361 _memory_group.manage(&_input_interleaved_reshaped);
362 }
363
364 // Create GEMM output tensor
365 TensorShape shape_gemm(_input_im2col_reshaped.info()->tensor_shape());
366 shape_gemm.set(0, mat_weights_cols);
367 shape_gemm.set(1, mat_input_rows);
368 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
369 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100370 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Michalis Spyroue2503892018-04-23 15:17:31 +0100371 info_gemm.set_quantization_info(output->info()->quantization_info());
372 _gemm_output.allocator()->init(info_gemm);
Georgios Pinitas2345f432018-05-18 20:13:01 +0100373 _memory_group.manage(&_gemm_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100374
375 // Configure im2col
376 _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 +0000377 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100378 else if(_append_bias)
379 {
380 // Configure add bias kernel
381 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
382 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000383
384 // Configure matrix multiply
Pablo Telloeb82fd22018-02-23 13:43:50 +0000385 if(run_optimised)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000386 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100387 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 +0000388 {
389 ARM_COMPUTE_ERROR("setup_assembly_kernel failed.");
390 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000391 }
392 else
393 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000394 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000395 {
396 // Configure GEMMInterleave4x4. _input_interleaved_reshaped will be auto configured in the kernel
397 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
398
399 // Configure GEMM
Michalis Spyroue2503892018-04-23 15:17:31 +0100400 configure_mm(&_input_interleaved_reshaped, weights, &_gemm_output, _is_interleaved, GEMMReshapeInfo(_input_im2col_reshaped.info()->dimension(idx_height), 0 /* no transpose */,
401 _input_im2col_reshaped.info()->dimension(idx_width)));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000402 _input_interleaved_reshaped.allocator()->allocate();
403 }
404 else
405 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000406 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output, _is_interleaved);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000407 }
408 }
409
Michalis Spyroue2503892018-04-23 15:17:31 +0100410 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000411 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100412 _input_im2col_reshaped.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000413
Michalis Spyroue2503892018-04-23 15:17:31 +0100414 // Configure output stage for quantized case
415 if(_is_quantized)
416 {
417 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
418
419 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
420 int output_multiplier, output_shift;
421 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
422 _memory_group.manage(&_tmp_output);
423 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
424 }
425
426 // Configure Col2Im
427 if(!is_nhwc)
428 {
429 _output_col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, Size2D(conv_w, conv_h));
430 }
431
432 if(_is_quantized)
433 {
434 _tmp_output.allocator()->allocate();
435 }
436 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000437 }
438
Michalis Spyroue2503892018-04-23 15:17:31 +0100439 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 +0000440
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000441 //Configure Activation Layer
442 if(_is_activationlayer_enabled)
443 {
444 _activationlayer_function.configure(output, nullptr, act_info);
445 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000446}
447
448Status 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 +0000449 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000450{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000451 ARM_COMPUTE_UNUSED(output);
452
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000453 DataType dt{};
454 bool append_bias{};
Michalis Spyroue2503892018-04-23 15:17:31 +0100455 bool skip_im2col{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000456 bool are_weights_reshaped{};
457 bool is_fully_connected_convolution{};
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000458 bool is_interleaved{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000459 bool is_quantized{};
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000460 bool is_activationlayer_enabled{};
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000461 unsigned int kernel_width = 0;
462 unsigned int kernel_height = 0;
463 unsigned int mat_weights_cols = 0;
464 unsigned int mat_weights_rows = 0;
465 unsigned int conv_w = 0;
466 unsigned int conv_h = 0;
467
Michalis Spyroue2503892018-04-23 15:17:31 +0100468 const DataLayout data_layout = input->data_layout();
469 const bool is_nhwc = data_layout == DataLayout::NHWC;
470 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
471 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
472
473 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 +0000474 is_fully_connected_convolution, is_interleaved, is_quantized, is_activationlayer_enabled, mat_weights_cols, mat_weights_rows,
Alex Gilday7da29b62018-03-23 14:16:00 +0000475 conv_w, conv_h, dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000476
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000477 const Size2D kernel_weights = Size2D(kernel_width, kernel_height);
478
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000479 ARM_COMPUTE_RETURN_ON_ERROR(status);
480
481 std::unique_ptr<ITensorInfo> reshaped_weights = weights->clone();
482 bool optimised_kernel = false;
483
Pablo Tello7fad9b12018-03-14 17:55:27 +0000484 if(dt == DataType::F32)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000485 {
486 optimised_kernel = true;
487 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000488
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000489 const unsigned int mat_input_cols = mat_weights_rows;
490 const unsigned int mat_input_rows = conv_w * conv_h;
491 TensorShape shape_im2col = input->tensor_shape();
492 shape_im2col.set(0, mat_input_cols);
493 shape_im2col.set(1, mat_input_rows);
494 shape_im2col.set(2, 1);
495 TensorInfo im2_col_info = input->clone()->set_tensor_shape(shape_im2col);
Michalis Spyroue2503892018-04-23 15:17:31 +0100496
497 if(!skip_im2col)
498 {
499 // Validate im2col
500 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2_col_info, kernel_weights, conv_info, append_bias, false, false, dilation));
501 }
502 else if(append_bias)
503 {
504 // Validate add bias kernel
505 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
506 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000507
508 // Create GEMM output tensor
509 TensorShape shape_gemm(im2_col_info.tensor_shape());
510 shape_gemm.set(0, mat_weights_cols);
511 shape_gemm.set(1, mat_input_rows);
512 TensorInfo gemm_output_info = input->clone()->set_tensor_shape(shape_gemm);
513
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100514 // Reshape weights if needed
515 if(optimised_kernel)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000516 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100517 ARM_COMPUTE_RETURN_ERROR_ON(are_weights_reshaped);
518
519 // Create tensor to store the reshaped weights
520 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
521 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 +0000522 }
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100523 else if(!is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000524 {
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100525 TensorShape reshaped_weights_shape;
526
527 if(is_fully_connected_convolution || is_quantized)
528 {
529 reshaped_weights_shape = TensorShape{ mat_weights_cols, mat_weights_rows };
530 }
531 else
532 {
533 // Create tensor to store transposed weights
534 const float transpose_width = 16.0f / input->element_size();
535 reshaped_weights_shape = TensorShape{ mat_weights_rows *static_cast<unsigned int>(transpose_width),
536 static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)) };
537 }
538
539 // Create tensor to store the reshaped weights
540 reshaped_weights->set_tensor_shape(get_reshaped_weights_shape_conv(weights, append_bias, is_fully_connected_convolution));
541 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases, reshaped_weights.get(), !is_fully_connected_convolution /* 1xW transpose */));
542 weights = reshaped_weights.get();
543
544 // Validate GEMM interleave and multiply
545 if(is_interleaved)
546 {
547 TensorShape shape_interleaved = shape_im2col;
Michalis Spyroue2503892018-04-23 15:17:31 +0100548 shape_interleaved.set(idx_width, shape_interleaved.x() * 4);
549 shape_interleaved.set(idx_height, std::ceil(shape_interleaved.y() / 4.f));
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100550 TensorInfo input_interleaved_info = input->clone()->set_tensor_shape(shape_interleaved);
551 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(&im2_col_info, &input_interleaved_info));
552 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&input_interleaved_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo(shape_im2col[1], // m
553 weights->tensor_shape()[0], // n
554 shape_im2col[0]) /* k */));
555 }
556 else
557 {
558 ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(&im2_col_info, weights, &gemm_output_info, 1.f, is_interleaved, GEMMReshapeInfo()));
559 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000560 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100561 if(!is_nhwc)
562 {
563 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
564 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000565
Michalis Spyroue2503892018-04-23 15:17:31 +0100566 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 +0000567
568 if(act_info.enabled())
569 {
570 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
571 }
572
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000573 return Status{};
574}
575
576void NEGEMMConvolutionLayer::run()
577{
Georgios Pinitas72219332018-06-05 14:56:06 +0100578 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000579
580 _memory_group.acquire();
581
Michalis Spyroue2503892018-04-23 15:17:31 +0100582 if(!_skip_im2col)
583 {
584 // Run input reshaping
585 unsigned int _y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
586 NEScheduler::get().schedule(&_input_im2col_kernel, _y_dim);
587 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000588
589 // Runs matrix multiply on reshaped matrices
Pablo Telloeb82fd22018-02-23 13:43:50 +0000590 if(_asm_glue._optimised_kernel != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000591 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000592 _asm_glue.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000593 }
594 else
595 {
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000596 if(_is_interleaved)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000597 {
598 // Run interleave
599 NEScheduler::get().schedule(&_input_interleave_kernel, Window::DimY);
600 }
601
602 // Runs matrix multiply on reshaped matrices
603 if(_is_quantized)
604 {
605 _mm_gemmlowp.run();
606 }
607 else
608 {
609 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
610 }
611 }
612
Michalis Spyroue2503892018-04-23 15:17:31 +0100613 if(_skip_im2col && _append_bias)
614 {
615 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
616 }
617
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000618 // Run output stage for quantized case
619 if(_is_quantized)
620 {
621 _gemmlowp_output_stage.run();
622 }
623
624 // Reshape output matrix
Michalis Spyroue2503892018-04-23 15:17:31 +0100625 if(_data_layout == DataLayout::NCHW)
626 {
627 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
628 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000629
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000630 if(_is_activationlayer_enabled)
631 {
632 _activationlayer_function.run();
633 }
634
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000635 _memory_group.release();
636}
Georgios Pinitas72219332018-06-05 14:56:06 +0100637
638void NEGEMMConvolutionLayer::prepare()
639{
640 if(!_is_prepared)
641 {
642 // Run weights reshaping (Runs once for every configure)
643 if(!_are_weights_reshaped)
644 {
645 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
646
647 _weights_reshaped.allocator()->allocate();
648 _reshape_weights.run();
649 _reshape_weights = NEConvolutionLayerReshapeWeights();
650 _original_weights->mark_as_unused();
651 _are_weights_reshaped = true;
652 }
653
654 // Run GEMM prepare stage
655 if(_asm_glue._optimised_kernel)
656 {
657 _asm_glue.prepare();
658 }
659 else
660 {
661 if(_is_quantized)
662 {
663 _mm_gemmlowp.prepare();
664 }
665 }
666
667 // Release weights in case buffer is pretransposed
668 if(!_weights_reshaped.is_used())
669 {
670 _weights_reshaped.allocator()->free();
671 }
672
673 _is_prepared = true;
674 }
675}
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000676} // namespace arm_compute