blob: 02ae1715dae8acc7923e0b1ddb7aa96da287862b [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/Size2D.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Gian Marco Iodice597a8562018-08-01 15:06:06 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000030#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>
Georgios Pinitas08346e92018-10-16 19:10:46 +010035#include <set>
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000036#include <tuple>
37
Gian Marco Iodice597a8562018-08-01 15:06:06 +010038using namespace arm_compute;
39using namespace arm_compute::misc::shape_calculator;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000040
Gian Marco Iodice597a8562018-08-01 15:06:06 +010041NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights()
42 : _weights_reshape_kernel()
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000043{
44}
45
Gian Marco Iodice597a8562018-08-01 15:06:06 +010046void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000047{
48 // Perform validation step
49 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
50 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
51 (biases != nullptr) ? biases->info() : nullptr,
Gian Marco Iodice597a8562018-08-01 15:06:06 +010052 output->info()));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000053
Gian Marco Iodice597a8562018-08-01 15:06:06 +010054 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000055 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
56
Gian Marco Iodice597a8562018-08-01 15:06:06 +010057 _weights_reshape_kernel.configure(weights, biases_to_use, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000058
59 output->info()->set_quantization_info(weights->info()->quantization_info());
60}
61
Gian Marco Iodice597a8562018-08-01 15:06:06 +010062Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000063{
Gian Marco Iodice597a8562018-08-01 15:06:06 +010064 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010065 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 +000066 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000067
Gian Marco Iodice597a8562018-08-01 15:06:06 +010068 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000069 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010070 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000071 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010073 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000074 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
75 }
76
Gian Marco Iodice597a8562018-08-01 15:06:06 +010077 if((output != nullptr) && (output->total_size() != 0))
Michalis Spyroue2503892018-04-23 15:17:31 +010078 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010079 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Michalis Spyroue2503892018-04-23 15:17:31 +010080
Gian Marco Iodice597a8562018-08-01 15:06:06 +010081 NEWeightsReshapeKernel::validate(weights, biases, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000082 }
83
84 return Status{};
85}
86
87void NEConvolutionLayerReshapeWeights::run()
88{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000089 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000090}
91
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000092NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Gian Marco Iodice597a8562018-08-01 15:06:06 +010093 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(),
Georgios Pinitas041f36d2018-09-18 18:38:37 +010094 _add_bias_kernel(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false), _skip_im2col(false),
95 _skip_col2im(false), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000096{
97}
98
Gian Marco Iodice597a8562018-08-01 15:06:06 +010099void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, int gemm_3d_depth)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000100{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100101 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
102 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info(), gemm_3d_depth, _skip_im2col));
103
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000104 if(_is_quantized)
105 {
106 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
107 // Extract and negate input and weights offset
108 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
109 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
110
111 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
112 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
113
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100114 _mm_gemmlowp.configure(input, weights, nullptr, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000115
116 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
117 input->info()->set_quantization_info(input_quantization_info);
118 weights->info()->set_quantization_info(weights_quantization_info);
119 }
120 else
121 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100122 // Configure matrix multiply function
123 _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,
124 _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000125 }
126}
127
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100128Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
129{
130 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
131
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100132 const GEMMInfo gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, gemm_3d_depth, skip_im2col);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100133 if(is_quantized)
134 {
135 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
136 // Extract and negate input and weights offset
137 const QuantizationInfo input_quantization_info = input->quantization_info();
138 const QuantizationInfo weights_quantization_info = weights->quantization_info();
139
140 std::unique_ptr<ITensorInfo> input_qa = input->clone();
141 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
142 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
143 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
144
145 // Perform validation step on GEMMLowp
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100146 return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), nullptr, output, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100147 }
148 else
149 {
150 // Perform validation step on Matrix multiply function
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100151 return NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100152 }
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100153}
154
155Status NEGEMMConvolutionLayer::validate_gemm3d(DataType data_type, int gemm_3d_depth, bool skip_im2col)
156{
157 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
158 const DataType output_gemm_data_type = is_quantized ? DataType::S32 : data_type;
159 const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth;
160 const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U;
161
162 // Set dummy tensor shapes for the validation
163 const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type);
164 const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type);
165 const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, output_gemm_data_type);
166
167 return validate_mm(&dummy_input_info, &dummy_weights_info, &dummy_output_info, gemm_3d_depth, skip_im2col);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100168}
169
Alex Gilday7da29b62018-03-23 14:16:00 +0000170void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100171 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000172{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000173 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100174 ARM_COMPUTE_UNUSED(num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100175 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(),
176 weights->info(),
177 biases != nullptr ? biases->info() : nullptr,
178 output->info(),
179 conv_info,
180 weights_info,
181 dilation,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100182 act_info,
183 num_groups));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000184
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100185 const DataType data_type = input->info()->data_type();
186 const DataLayout data_layout = input->info()->data_layout();
187 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
188 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100189 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100190
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100191 const unsigned int kernel_width = weights->info()->dimension(idx_width);
192 const unsigned int kernel_height = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000193
Georgios Pinitas08346e92018-10-16 19:10:46 +0100194 _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 _is_activationlayer_enabled = act_info.enabled();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000202
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100203 const ITensor *gemm_input_to_use = input;
204 ITensor *gemm_output_to_use = output;
205 ITensor *gemm_output_staged_to_use = output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000206
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100207 // Get convolved dimensions
208 unsigned int conv_w = 0;
209 unsigned int conv_h = 0;
210 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
211 input->info()->dimension(idx_height),
212 kernel_width,
213 kernel_height,
214 conv_info,
215 dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000216
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100217 // Check if GEMM3D is supported
218 if(_skip_col2im)
219 {
220 // If not supported, we need to perform im2col and col2im (or reshape layer)
221 if(!bool(validate_gemm3d(input->info()->data_type(), conv_h, _skip_im2col)))
222 {
223 _skip_im2col = false;
224 _skip_col2im = false;
225 }
226 }
227
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100228 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
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100235 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000236
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100237 // _weights_reshaped will be auto configured in the kernel.
238 // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM
239 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
240
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100241 // Create tensor to store im2col reshaped inputs
Michalis Spyroue2503892018-04-23 15:17:31 +0100242 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000243 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100244 _memory_group.manage(&_im2col_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100245
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100246 // Configure
Giorgio Arena0f170392018-07-18 16:13:12 +0100247 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation);
Michalis Spyroue2503892018-04-23 15:17:31 +0100248
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100249 // Update GEMM input
250 gemm_input_to_use = &_im2col_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000251 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100252 else if(_append_bias)
253 {
254 // Configure add bias kernel
255 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
256 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000257
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100258 // Create temporary GEMM output tensor in case we cannot skip col2im
259 if(!_skip_col2im)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000260 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100261 // Calculate GEMM output shape
262 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
263 shape_gemm.set(0, mat_weights_cols);
264 shape_gemm.set(1, conv_w * conv_h);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000265
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100266 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
267 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
268 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
269 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100270 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100271 _gemm_output.allocator()->init(info_gemm);
272 _memory_group.manage(&_gemm_output);
273
274 // Update GEMM output
275 gemm_output_to_use = &_gemm_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000276 }
277
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100278 // Configure GEMM
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000279 // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix
280 const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0;
281 configure_mm(gemm_input_to_use, &_weights_reshaped, gemm_output_to_use, gemm_3d_depth);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100282
Michalis Spyroue2503892018-04-23 15:17:31 +0100283 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000284 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100285 _im2col_output.allocator()->allocate();
286 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000287
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100288 // Configure output stage for quantized case
289 if(_is_quantized)
290 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100291 const bool skip_reshape = data_layout == DataLayout::NHWC;
Georgios Pinitas08346e92018-10-16 19:10:46 +0100292 const QuantizationInfo input_quant_info = input->info()->quantization_info();
293 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input_quant_info : output->info()->quantization_info();
Michalis Spyroue2503892018-04-23 15:17:31 +0100294
Georgios Pinitas08346e92018-10-16 19:10:46 +0100295 float multiplier = input_quant_info.scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100296 int output_multiplier, output_shift;
297 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Michalis Spyroue2503892018-04-23 15:17:31 +0100298
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100299 if(!skip_reshape)
300 {
301 _memory_group.manage(&_tmp_output);
302 gemm_output_staged_to_use = &_tmp_output;
303 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100304
Georgios Pinitas08346e92018-10-16 19:10:46 +0100305 // Merge activation with output stage
306 uint8_t min = 0;
307 uint8_t max = 0;
308 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
309 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
310 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
311 };
312 if(_is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
313 {
314 min = sqcvt_qasymm8_f32(act_info.b(), input_quant_info.scale, input_quant_info.offset);
315 max = sqcvt_qasymm8_f32(act_info.a(), input_quant_info.scale, input_quant_info.offset);
316 if(act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
317 {
318 min = sqcvt_qasymm8_f32(0.f, input_quant_info.scale, input_quant_info.offset);
319 }
320 if(act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU)
321 {
322 max = 255;
323 }
324 _is_activationlayer_enabled = false;
325 }
326
327 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset, min, max, skip_reshape ? conv_h : 1);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100328 }
329
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100330 if(!_skip_col2im && _data_layout == DataLayout::NCHW)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100331 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100332 // Configure col2im
333 _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, Size2D(conv_w, conv_h));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100334 }
335
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100336 if(_is_quantized && data_layout == DataLayout::NCHW)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100337 {
338 _tmp_output.allocator()->allocate();
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100339 }
340
341 if(!_skip_col2im)
342 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100343 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000344 }
345
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100346 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
347 "Output shape does not match the expected one");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000348
Georgios Pinitas08346e92018-10-16 19:10:46 +0100349 // Configure Activation Layer
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000350 if(_is_activationlayer_enabled)
351 {
352 _activationlayer_function.configure(output, nullptr, act_info);
353 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100354
355 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000356}
357
358Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100359 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000360{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100361 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
362 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
363 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
364 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
365 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100366 ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups > 1, "Grouping (num_groups != 1) is not supported on NEON");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000367
Michalis Spyroue2503892018-04-23 15:17:31 +0100368 const DataLayout data_layout = input->data_layout();
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100369 const DataType data_type = input->data_type();
Michalis Spyroue2503892018-04-23 15:17:31 +0100370 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
371 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100372 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
373 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100374
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100375 const unsigned int kernel_width = weights->dimension(idx_width);
376 const unsigned int kernel_height = weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000377
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100378 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
379 const ITensorInfo *gemm_input_to_use = input;
380 const ITensorInfo *gemm_output_to_use = output;
381 const ITensorInfo *gemm_output_staged_to_use = output;
382 const ITensorInfo *weights_to_use = weights;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000383
Georgios Pinitas08346e92018-10-16 19:10:46 +0100384 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
385 const bool append_bias = (biases != nullptr) && (!is_quantized);
386 bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
387 bool skip_col2im = data_layout == DataLayout::NHWC;
388 bool is_activation_enabled = act_info.enabled();
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100389
390 // Get convolved dimensions
391 unsigned int conv_w = 0;
392 unsigned int conv_h = 0;
393
394 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
395 input->dimension(idx_height),
396 kernel_width,
397 kernel_height,
398 conv_info,
399 dilation);
400
401 // Check if GEMM3D is supported
402 if(skip_col2im)
403 {
404 // If not supported, we need to perform im2col and col2im (or reshape layer)
405 if(!bool(validate_gemm3d(input->data_type(), conv_h, skip_im2col)))
406 {
407 skip_im2col = false;
408 skip_col2im = false;
409 }
410 }
411
412 const unsigned bias_element = (append_bias && !skip_im2col) ? 1 : 0;
413 const ITensorInfo *biases_to_use = (append_bias && !skip_im2col) ? biases : nullptr;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000414
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100415 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
416 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000417
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100418 // Validate biases
419 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000420 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100421 if(is_quantized)
422 {
423 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
424 }
425 else
426 {
427 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
428 }
429 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
430 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000431 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000432
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100433 if(act_info.enabled())
434 {
435 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
436 }
437
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100438 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
439 unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + bias_element;
440
441 // Output tensor auto inizialization if not yet initialized
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100442 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases_to_use, nullptr));
443 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col)), 1, data_type);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100444 weights_to_use = &weights_reshaped_info;
445
Michalis Spyroue2503892018-04-23 15:17:31 +0100446 if(!skip_im2col)
447 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100448 // Create tensor info for im2col reshaped inputs
449 // For NEON the batch size is on the fourth dimension
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100450 // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100451 TensorShape shape_im2col = input->tensor_shape();
452 shape_im2col.set(0, mat_weights_rows);
453 shape_im2col.set(1, conv_w * conv_h);
454 shape_im2col.set(2, 1);
455
456 im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
457 im2col_reshaped_info.set_quantization_info(input->quantization_info());
458
Giorgio Arena0f170392018-07-18 16:13:12 +0100459 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100460 gemm_input_to_use = &im2col_reshaped_info;
Michalis Spyroue2503892018-04-23 15:17:31 +0100461 }
462 else if(append_bias)
463 {
464 // Validate add bias kernel
465 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
466 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000467
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100468 // Create temporary GEMM output tensor in case we cannot skip col2im
469 if(!skip_col2im)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000470 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100471 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
472 shape_gemm.set(0, mat_weights_cols);
473 shape_gemm.set(1, conv_w * conv_h);
474 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
475 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
476 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100477 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100478
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100479 gemm_output_to_use = &info_gemm;
Michalis Spyroue2503892018-04-23 15:17:31 +0100480 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000481
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000482 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, skip_col2im ? conv_h : 0, skip_im2col));
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000483
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100484 if(is_quantized)
485 {
Georgios Pinitas08346e92018-10-16 19:10:46 +0100486 const bool skip_reshape = data_layout == DataLayout::NHWC;
487 const QuantizationInfo input_quant_info = input->quantization_info();
488 const QuantizationInfo output_quant_info = (output->total_size() == 0) ? input_quant_info : output->quantization_info();
489 const float multiplier = input_quant_info.scale * weights_to_use->quantization_info().scale / output_quant_info.scale;
490 int output_multiplier, output_shift;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100491 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
492
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100493 if(!skip_reshape)
494 {
495 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
496 tmp_info.set_quantization_info(output->quantization_info());
497 gemm_output_staged_to_use = &tmp_info;
498 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100499
Georgios Pinitas08346e92018-10-16 19:10:46 +0100500 // Merge activation with output stage
501 uint8_t min = 0;
502 uint8_t max = 0;
503 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
504 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
505 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
506 };
507 if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0)
508 {
509 min = sqcvt_qasymm8_f32(act_info.b(), input_quant_info.scale, input_quant_info.offset);
510 max = sqcvt_qasymm8_f32(act_info.a(), input_quant_info.scale, input_quant_info.offset);
511 if(act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
512 {
513 min = sqcvt_qasymm8_f32(0.f, input_quant_info.scale, input_quant_info.offset);
514 }
515 if(act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU)
516 {
517 max = 255;
518 }
519 is_activation_enabled = false;
520 }
521
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100522 // Validate output stage for quantized case
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000523 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, min, max, skip_reshape ? conv_h : 0);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100524 }
525
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100526 // Validate Col2Im/ReshapeLayer
527 if(!skip_col2im && (data_layout == DataLayout::NCHW))
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100528 {
529 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
530 output,
531 Size2D(conv_w, conv_h)));
532 }
533
534 //Validate Activation Layer
Georgios Pinitas08346e92018-10-16 19:10:46 +0100535 if(is_activation_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000536 {
537 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
538 }
539
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000540 return Status{};
541}
542
543void NEGEMMConvolutionLayer::run()
544{
Georgios Pinitas72219332018-06-05 14:56:06 +0100545 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000546
547 _memory_group.acquire();
548
Michalis Spyroue2503892018-04-23 15:17:31 +0100549 if(!_skip_im2col)
550 {
551 // Run input reshaping
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100552 unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
553 NEScheduler::get().schedule(&_im2col_kernel, y_dim);
Michalis Spyroue2503892018-04-23 15:17:31 +0100554 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000555
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100556 // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions
557 if(_is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000558 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100559 // Run gemmlowp
560 _mm_gemmlowp.run();
561
562 // Run output stage
563 _gemmlowp_output_stage.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000564 }
565 else
566 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100567 // Run gemm
568 _mm_gemm.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000569 }
570
Michalis Spyroue2503892018-04-23 15:17:31 +0100571 if(_skip_im2col && _append_bias)
572 {
573 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
574 }
575
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000576 // Reshape output matrix
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100577 if(!_skip_col2im && _data_layout == DataLayout::NCHW)
Michalis Spyroue2503892018-04-23 15:17:31 +0100578 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100579 NEScheduler::get().schedule(&_col2im_kernel, Window::DimY);
Michalis Spyroue2503892018-04-23 15:17:31 +0100580 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000581
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000582 if(_is_activationlayer_enabled)
583 {
584 _activationlayer_function.run();
585 }
586
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000587 _memory_group.release();
588}
Georgios Pinitas72219332018-06-05 14:56:06 +0100589
590void NEGEMMConvolutionLayer::prepare()
591{
592 if(!_is_prepared)
593 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100594 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100595
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100596 // Run weights reshaping and mark original weights tensor as unused
597 _weights_reshaped.allocator()->allocate();
598 _reshape_weights.run();
599 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100600
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100601 // Prepare GEMM
602 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100603 if(!_weights_reshaped.is_used())
604 {
605 _weights_reshaped.allocator()->free();
606 }
607
608 _is_prepared = true;
609 }
610}