blob: 92e641e7456dc5bb472408ae2ab0e7ac7558a916 [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +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
Anthony Barbier06ea0482018-02-22 15:45:35 +000026#include "arm_compute/core/Size2D.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Jenkins52ba29e2018-08-29 15:32:11 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier06ea0482018-02-22 15:45:35 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
32#include "support/ToolchainSupport.h"
33
Anthony Barbier06ea0482018-02-22 15:45:35 +000034#include <cmath>
35#include <tuple>
36
Jenkins52ba29e2018-08-29 15:32:11 +000037using namespace arm_compute;
38using namespace arm_compute::misc::shape_calculator;
Anthony Barbier06ea0482018-02-22 15:45:35 +000039
Jenkins52ba29e2018-08-29 15:32:11 +000040NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights()
41 : _weights_reshape_kernel()
Anthony Barbier06ea0482018-02-22 15:45:35 +000042{
43}
44
Jenkins52ba29e2018-08-29 15:32:11 +000045void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output)
Anthony Barbier06ea0482018-02-22 15:45:35 +000046{
47 // Perform validation step
48 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
49 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
50 (biases != nullptr) ? biases->info() : nullptr,
Jenkins52ba29e2018-08-29 15:32:11 +000051 output->info()));
Anthony Barbier06ea0482018-02-22 15:45:35 +000052
Jenkins52ba29e2018-08-29 15:32:11 +000053 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
Anthony Barbier06ea0482018-02-22 15:45:35 +000054 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
55
Jenkins52ba29e2018-08-29 15:32:11 +000056 _weights_reshape_kernel.configure(weights, biases_to_use, output);
Anthony Barbier06ea0482018-02-22 15:45:35 +000057
58 output->info()->set_quantization_info(weights->info()->quantization_info());
59}
60
Jenkins52ba29e2018-08-29 15:32:11 +000061Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
Anthony Barbier06ea0482018-02-22 15:45:35 +000062{
Jenkins52ba29e2018-08-29 15:32:11 +000063 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier06ea0482018-02-22 15:45:35 +000065 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Anthony Barbier06ea0482018-02-22 15:45:35 +000066
Jenkins52ba29e2018-08-29 15:32:11 +000067 if(biases != nullptr)
Anthony Barbier06ea0482018-02-22 15:45:35 +000068 {
Jenkins52ba29e2018-08-29 15:32:11 +000069 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Anthony Barbier06ea0482018-02-22 15:45:35 +000070 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Jenkins52ba29e2018-08-29 15:32:11 +000072 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +000073 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
74 }
75
Jenkins52ba29e2018-08-29 15:32:11 +000076 if((output != nullptr) && (output->total_size() != 0))
Anthony Barbier06ea0482018-02-22 15:45:35 +000077 {
Jenkins52ba29e2018-08-29 15:32:11 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Anthony Barbier06ea0482018-02-22 15:45:35 +000079
Jenkins52ba29e2018-08-29 15:32:11 +000080 NEWeightsReshapeKernel::validate(weights, biases, output);
Anthony Barbier06ea0482018-02-22 15:45:35 +000081 }
82
83 return Status{};
84}
85
86void NEConvolutionLayerReshapeWeights::run()
87{
Anthony Barbier06ea0482018-02-22 15:45:35 +000088 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Anthony Barbier06ea0482018-02-22 15:45:35 +000089}
90
Anthony Barbier06ea0482018-02-22 15:45:35 +000091NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Jenkins52ba29e2018-08-29 15:32:11 +000092 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(),
93 _add_bias_kernel(), _reshape_layer(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false),
94 _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +000095{
96}
97
Jenkins52ba29e2018-08-29 15:32:11 +000098void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, int gemm_3d_depth)
Anthony Barbier06ea0482018-02-22 15:45:35 +000099{
Jenkins52ba29e2018-08-29 15:32:11 +0000100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
101 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info(), gemm_3d_depth, _skip_im2col));
102
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103 if(_is_quantized)
104 {
105 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
106 // Extract and negate input and weights offset
107 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
108 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
109
110 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
111 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
112
113 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
114
115 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
116 input->info()->set_quantization_info(input_quantization_info);
117 weights->info()->set_quantization_info(weights_quantization_info);
118 }
119 else
120 {
Jenkins52ba29e2018-08-29 15:32:11 +0000121 // Configure matrix multiply function
122 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/, gemm_3d_depth,
123 _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000124 }
125}
126
Jenkins52ba29e2018-08-29 15:32:11 +0000127Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000128{
Jenkins52ba29e2018-08-29 15:32:11 +0000129 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
Anthony Barbier06ea0482018-02-22 15:45:35 +0000130
Jenkins52ba29e2018-08-29 15:32:11 +0000131 const GEMMInfo gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, gemm_3d_depth, skip_im2col);
132 if(is_quantized)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000133 {
Jenkins52ba29e2018-08-29 15:32:11 +0000134 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
135 // Extract and negate input and weights offset
136 const QuantizationInfo input_quantization_info = input->quantization_info();
137 const QuantizationInfo weights_quantization_info = weights->quantization_info();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000138
Jenkins52ba29e2018-08-29 15:32:11 +0000139 std::unique_ptr<ITensorInfo> input_qa = input->clone();
140 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
141 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
142 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
143
144 // Perform validation step on GEMMLowp
145 return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000146 }
147 else
148 {
Jenkins52ba29e2018-08-29 15:32:11 +0000149 // Perform validation step on Matrix multiply function
150 return NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
151 }
152}
Anthony Barbier06ea0482018-02-22 15:45:35 +0000153
Jenkins52ba29e2018-08-29 15:32:11 +0000154Status NEGEMMConvolutionLayer::validate_gemm3d(DataType data_type, int gemm_3d_depth, bool skip_im2col)
155{
156 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
157 const DataType output_gemm_data_type = is_quantized ? DataType::S32 : data_type;
158 const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth;
159 const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000160
Jenkins52ba29e2018-08-29 15:32:11 +0000161 // Set dummy tensor shapes for the validation
162 const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type);
163 const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type);
164 const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, output_gemm_data_type);
165
166 return validate_mm(&dummy_input_info, &dummy_weights_info, &dummy_output_info, gemm_3d_depth, skip_im2col);
167}
168
169void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
170 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
171{
172 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
173 ARM_COMPUTE_UNUSED(num_groups);
174 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(),
175 weights->info(),
176 biases != nullptr ? biases->info() : nullptr,
177 output->info(),
178 conv_info,
179 weights_info,
180 dilation,
181 act_info,
182 num_groups));
183
184 const DataType data_type = input->info()->data_type();
185 const DataLayout data_layout = input->info()->data_layout();
186 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
187 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
188 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
189 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
190
191 const unsigned int kernel_width = weights->info()->dimension(idx_width);
192 const unsigned int kernel_height = weights->info()->dimension(idx_height);
193
194 _is_prepared = weights_info.retain_internal_weights();
195 _original_weights = weights;
196 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
197 _data_layout = data_layout;
198 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
199 _skip_col2im = data_layout == DataLayout::NHWC;
200 _append_bias = (biases != nullptr) && (!_is_quantized);
201
202 const ITensor *gemm_input_to_use = input;
203 ITensor *gemm_output_to_use = output;
204 ITensor *gemm_output_staged_to_use = output;
205
206 // Get convolved dimensions
207 unsigned int conv_w = 0;
208 unsigned int conv_h = 0;
209 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
210 input->info()->dimension(idx_height),
211 kernel_width,
212 kernel_height,
213 conv_info,
214 dilation);
215
216 // Check if GEMM3D is supported
217 if(_skip_col2im)
218 {
219 // If not supported, we need to perform im2col and col2im (or reshape layer)
220 if(!bool(validate_gemm3d(input->info()->data_type(), conv_h, _skip_im2col)))
221 {
222 _skip_im2col = false;
223 _skip_col2im = false;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000224 }
225 }
226
Jenkins52ba29e2018-08-29 15:32:11 +0000227 const unsigned bias_element = (_append_bias && !_skip_im2col) ? 1 : 0;
228 const ITensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
229
230 // Get parameters from conv_info
231 unsigned int stride_x = 0;
232 unsigned int stride_y = 0;
233 std::tie(stride_x, stride_y) = conv_info.stride();
234
235 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
236 unsigned int mat_weights_rows = weights->info()->dimension(idx_width) * weights->info()->dimension(idx_height) * weights->info()->dimension(idx_channel) + bias_element;
237
238 // _weights_reshaped will be auto configured in the kernel.
239 // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM
240 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
241
242 // Create tensor to store im2col reshaped inputs
Jenkinsb3a371b2018-05-23 11:36:53 +0100243 if(!_skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000244 {
Jenkins52ba29e2018-08-29 15:32:11 +0000245 // Calculate im2col shape
246 // For NEON the batch size is on the fourth dimension
247 TensorShape shape_im2col = input->info()->tensor_shape();
248 shape_im2col.set(0, mat_weights_rows);
249 shape_im2col.set(1, conv_w * conv_h);
Jenkinsb3a371b2018-05-23 11:36:53 +0100250 shape_im2col.set(2, 1);
Jenkinsb3a371b2018-05-23 11:36:53 +0100251
Jenkins52ba29e2018-08-29 15:32:11 +0000252 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
253 _memory_group.manage(&_im2col_output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100254
Jenkins52ba29e2018-08-29 15:32:11 +0000255 // Configure
256 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation);
Jenkinsb3a371b2018-05-23 11:36:53 +0100257
Jenkins52ba29e2018-08-29 15:32:11 +0000258 // Update GEMM input
259 gemm_input_to_use = &_im2col_output;
Jenkinsb3a371b2018-05-23 11:36:53 +0100260 }
261 else if(_append_bias)
262 {
263 // Configure add bias kernel
264 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000265 }
266
Jenkins52ba29e2018-08-29 15:32:11 +0000267 // Create temporary GEMM output tensor in case we cannot skip col2im
268 if(!_skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000269 {
Jenkins52ba29e2018-08-29 15:32:11 +0000270 // Calculate GEMM output shape
271 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
272 shape_gemm.set(0, mat_weights_cols);
273 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000274
Jenkins52ba29e2018-08-29 15:32:11 +0000275 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
276 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
277 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
278 info_gemm.set_quantization_info(output->info()->quantization_info());
279 _gemm_output.allocator()->init(info_gemm);
280 _memory_group.manage(&_gemm_output);
281
282 // Update GEMM output
283 gemm_output_to_use = &_gemm_output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000284 }
285
Jenkins52ba29e2018-08-29 15:32:11 +0000286 // Configure GEMM
287 configure_mm(gemm_input_to_use, &_weights_reshaped, gemm_output_to_use, _skip_col2im ? conv_h : 1);
288
Jenkinsb3a371b2018-05-23 11:36:53 +0100289 if(!_skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000290 {
Jenkins52ba29e2018-08-29 15:32:11 +0000291 _im2col_output.allocator()->allocate();
292 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000293
Jenkins52ba29e2018-08-29 15:32:11 +0000294 // Configure output stage for quantized case
295 if(_is_quantized)
296 {
297 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
298
299 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
300 int output_multiplier, output_shift;
301 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
302
303 _memory_group.manage(&_tmp_output);
304 gemm_output_staged_to_use = &_tmp_output;
305
306 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset);
307 }
308
309 if(!_skip_col2im)
310 {
311 if(_data_layout == DataLayout::NCHW)
Jenkinsb3a371b2018-05-23 11:36:53 +0100312 {
Jenkins52ba29e2018-08-29 15:32:11 +0000313 // Configure col2im
314 _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, Size2D(conv_w, conv_h));
Jenkinsb3a371b2018-05-23 11:36:53 +0100315 }
Jenkins52ba29e2018-08-29 15:32:11 +0000316 else
Jenkinsb3a371b2018-05-23 11:36:53 +0100317 {
Jenkins52ba29e2018-08-29 15:32:11 +0000318 // Configure reshape layer
319 _reshape_layer.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output);
Jenkinsb3a371b2018-05-23 11:36:53 +0100320 }
Jenkins52ba29e2018-08-29 15:32:11 +0000321 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100322
Jenkins52ba29e2018-08-29 15:32:11 +0000323 if(_is_quantized)
324 {
325 _tmp_output.allocator()->allocate();
326 }
327
328 if(!_skip_col2im)
329 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100330 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000331 }
332
Jenkins52ba29e2018-08-29 15:32:11 +0000333 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
334 "Output shape does not match the expected one");
Jenkinsb3a371b2018-05-23 11:36:53 +0100335
336 //Configure Activation Layer
Jenkins52ba29e2018-08-29 15:32:11 +0000337 _is_activationlayer_enabled = act_info.enabled();
338
Jenkinsb3a371b2018-05-23 11:36:53 +0100339 if(_is_activationlayer_enabled)
340 {
341 _activationlayer_function.configure(output, nullptr, act_info);
342 }
Jenkins52ba29e2018-08-29 15:32:11 +0000343
344 ARM_COMPUTE_UNUSED(weights_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000345}
346
347Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000348 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000349{
Jenkins52ba29e2018-08-29 15:32:11 +0000350 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
351 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
352 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
353 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
354 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
355 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Grouping (num_groups != 1) is not supported on NEON");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000356
Jenkinsb3a371b2018-05-23 11:36:53 +0100357 const DataLayout data_layout = input->data_layout();
Jenkins52ba29e2018-08-29 15:32:11 +0000358 const DataType data_type = input->data_type();
Jenkinsb3a371b2018-05-23 11:36:53 +0100359 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
360 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Jenkins52ba29e2018-08-29 15:32:11 +0000361 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
362 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Jenkinsb3a371b2018-05-23 11:36:53 +0100363
Jenkins52ba29e2018-08-29 15:32:11 +0000364 const unsigned int kernel_width = weights->dimension(idx_width);
365 const unsigned int kernel_height = weights->dimension(idx_height);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000366
Jenkins52ba29e2018-08-29 15:32:11 +0000367 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
368 const ITensorInfo *gemm_input_to_use = input;
369 const ITensorInfo *gemm_output_to_use = output;
370 const ITensorInfo *gemm_output_staged_to_use = output;
371 const ITensorInfo *weights_to_use = weights;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000372
Jenkins52ba29e2018-08-29 15:32:11 +0000373 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
374 const bool append_bias = (biases != nullptr) && (!is_quantized);
375 bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
376 bool skip_col2im = data_layout == DataLayout::NHWC;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000377
Jenkins52ba29e2018-08-29 15:32:11 +0000378 // Get convolved dimensions
379 unsigned int conv_w = 0;
380 unsigned int conv_h = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000381
Jenkins52ba29e2018-08-29 15:32:11 +0000382 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
383 input->dimension(idx_height),
384 kernel_width,
385 kernel_height,
386 conv_info,
387 dilation);
388
389 // Check if GEMM3D is supported
390 if(skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000391 {
Jenkins52ba29e2018-08-29 15:32:11 +0000392 // If not supported, we need to perform im2col and col2im (or reshape layer)
393 if(!bool(validate_gemm3d(input->data_type(), conv_h, skip_im2col)))
394 {
395 skip_im2col = false;
396 skip_col2im = false;
397 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000398 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000399
Jenkins52ba29e2018-08-29 15:32:11 +0000400 const unsigned bias_element = (append_bias && !skip_im2col) ? 1 : 0;
401 const ITensorInfo *biases_to_use = (append_bias && !skip_im2col) ? biases : nullptr;
402
403 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
404 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
405
406 // Validate biases
407 if(biases != nullptr)
408 {
409 if(is_quantized)
410 {
411 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
412 }
413 else
414 {
415 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
416 }
417 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
418 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
419 }
420
421 if(act_info.enabled())
422 {
423 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
424 }
425
426 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
427 unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + bias_element;
428
429 // Output tensor auto inizialization if not yet initialized
430 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases_to_use, nullptr));
431 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col)), 1, data_type);
432 weights_to_use = &weights_reshaped_info;
Jenkinsb3a371b2018-05-23 11:36:53 +0100433
434 if(!skip_im2col)
435 {
Jenkins52ba29e2018-08-29 15:32:11 +0000436 // Create tensor info for im2col reshaped inputs
437 // For NEON the batch size is on the fourth dimension
438 TensorShape shape_im2col = input->tensor_shape();
439 shape_im2col.set(0, mat_weights_rows);
440 shape_im2col.set(1, conv_w * conv_h);
441 shape_im2col.set(2, 1);
442
443 im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
444 im2col_reshaped_info.set_quantization_info(input->quantization_info());
445
446 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation));
447 gemm_input_to_use = &im2col_reshaped_info;
Jenkinsb3a371b2018-05-23 11:36:53 +0100448 }
449 else if(append_bias)
450 {
451 // Validate add bias kernel
452 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
453 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000454
Jenkins52ba29e2018-08-29 15:32:11 +0000455 // Create temporary GEMM output tensor in case we cannot skip col2im
456 if(!skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000457 {
Jenkins52ba29e2018-08-29 15:32:11 +0000458 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
459 shape_gemm.set(0, mat_weights_cols);
460 shape_gemm.set(1, conv_w * conv_h);
461 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
462 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
463 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
464 info_gemm.set_quantization_info(output->quantization_info());
Jenkinsb3a371b2018-05-23 11:36:53 +0100465
Jenkins52ba29e2018-08-29 15:32:11 +0000466 gemm_output_to_use = &info_gemm;
Jenkinsb3a371b2018-05-23 11:36:53 +0100467 }
468
Jenkins52ba29e2018-08-29 15:32:11 +0000469 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, skip_col2im ? conv_h : 1, skip_im2col));
Jenkinsb3a371b2018-05-23 11:36:53 +0100470
Jenkins52ba29e2018-08-29 15:32:11 +0000471 if(is_quantized)
472 {
473 float multiplier = input->quantization_info().scale * weights_to_use->quantization_info().scale / output->quantization_info().scale;
474 int output_multiplier, output_shift;
475 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
476
477 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
478 tmp_info.set_quantization_info(output->quantization_info());
479 gemm_output_staged_to_use = &tmp_info;
480
481 // Validate output stage for quantized case
482 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, output->quantization_info().offset);
483 }
484
485 // Validate Col2Im/ReshapeLayer
486 if(!skip_col2im && (data_layout == DataLayout::NCHW))
487 {
488 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
489 output,
490 Size2D(conv_w, conv_h)));
491 }
492
493 //Validate Activation Layer
Jenkinsb3a371b2018-05-23 11:36:53 +0100494 if(act_info.enabled())
495 {
496 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000497 }
498
499 return Status{};
500}
501
502void NEGEMMConvolutionLayer::run()
503{
Jenkins52ba29e2018-08-29 15:32:11 +0000504 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000505
506 _memory_group.acquire();
507
Jenkinsb3a371b2018-05-23 11:36:53 +0100508 if(!_skip_im2col)
509 {
510 // Run input reshaping
Jenkins52ba29e2018-08-29 15:32:11 +0000511 unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
512 NEScheduler::get().schedule(&_im2col_kernel, y_dim);
Jenkinsb3a371b2018-05-23 11:36:53 +0100513 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000514
Jenkins52ba29e2018-08-29 15:32:11 +0000515 // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions
516 if(_is_quantized)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000517 {
Jenkins52ba29e2018-08-29 15:32:11 +0000518 // Run gemmlowp
519 _mm_gemmlowp.run();
520
521 // Run output stage
522 _gemmlowp_output_stage.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000523 }
524 else
525 {
Jenkins52ba29e2018-08-29 15:32:11 +0000526 // Run gemm
527 _mm_gemm.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000528 }
529
Jenkinsb3a371b2018-05-23 11:36:53 +0100530 if(_skip_im2col && _append_bias)
531 {
532 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
533 }
534
Anthony Barbier06ea0482018-02-22 15:45:35 +0000535 // Reshape output matrix
Jenkins52ba29e2018-08-29 15:32:11 +0000536 if(!_skip_col2im)
Jenkinsb3a371b2018-05-23 11:36:53 +0100537 {
Jenkins52ba29e2018-08-29 15:32:11 +0000538 if(_data_layout == DataLayout::NCHW)
539 {
540 NEScheduler::get().schedule(&_col2im_kernel, Window::DimY);
541 }
542 else
543 {
544 _reshape_layer.run();
545 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100546 }
547
548 if(_is_activationlayer_enabled)
549 {
550 _activationlayer_function.run();
551 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000552
553 _memory_group.release();
554}
Jenkins52ba29e2018-08-29 15:32:11 +0000555
556void NEGEMMConvolutionLayer::prepare()
557{
558 if(!_is_prepared)
559 {
560 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
561
562 // Run weights reshaping and mark original weights tensor as unused
563 _weights_reshaped.allocator()->allocate();
564 _reshape_weights.run();
565 _original_weights->mark_as_unused();
566
567 // Prepare GEMM
568 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
569 if(!_weights_reshaped.is_used())
570 {
571 _weights_reshaped.allocator()->free();
572 }
573
574 _is_prepared = true;
575 }
576}