blob: 24332014aa98173010f9244e2a4a94bb6be030c3 [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
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000104 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
105 gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */);
106
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000107 if(_is_quantized)
108 {
109 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
110 // Extract and negate input and weights offset
111 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
112 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
113
114 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
115 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
116
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000117 _mm_gemmlowp.configure(input, weights, nullptr, output, gemm_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000118
119 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
120 input->info()->set_quantization_info(input_quantization_info);
121 weights->info()->set_quantization_info(weights_quantization_info);
122 }
123 else
124 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100125 // Configure matrix multiply function
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000126 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000127 }
128}
129
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100130Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
131{
132 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
133
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000134 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
135 gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100136 if(is_quantized)
137 {
138 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
139 // Extract and negate input and weights offset
140 const QuantizationInfo input_quantization_info = input->quantization_info();
141 const QuantizationInfo weights_quantization_info = weights->quantization_info();
142
143 std::unique_ptr<ITensorInfo> input_qa = input->clone();
144 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
145 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
146 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
147
148 // Perform validation step on GEMMLowp
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100149 return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), nullptr, output, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100150 }
151 else
152 {
153 // Perform validation step on Matrix multiply function
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100154 return NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100155 }
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100156}
157
158Status NEGEMMConvolutionLayer::validate_gemm3d(DataType data_type, int gemm_3d_depth, bool skip_im2col)
159{
160 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
161 const DataType output_gemm_data_type = is_quantized ? DataType::S32 : data_type;
162 const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth;
163 const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U;
164
165 // Set dummy tensor shapes for the validation
166 const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type);
167 const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type);
168 const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, output_gemm_data_type);
169
170 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 +0100171}
172
Alex Gilday7da29b62018-03-23 14:16:00 +0000173void 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 +0100174 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000175{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000176 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100177 ARM_COMPUTE_UNUSED(num_groups);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100178 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(),
179 weights->info(),
180 biases != nullptr ? biases->info() : nullptr,
181 output->info(),
182 conv_info,
183 weights_info,
184 dilation,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100185 act_info,
186 num_groups));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000187
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100188 const DataType data_type = input->info()->data_type();
189 const DataLayout data_layout = input->info()->data_layout();
190 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
191 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100192 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100193
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100194 const unsigned int kernel_width = weights->info()->dimension(idx_width);
195 const unsigned int kernel_height = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000196
Georgios Pinitas08346e92018-10-16 19:10:46 +0100197 _is_prepared = weights_info.retain_internal_weights();
198 _original_weights = weights;
199 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
200 _data_layout = data_layout;
201 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
202 _skip_col2im = data_layout == DataLayout::NHWC;
203 _append_bias = (biases != nullptr) && (!_is_quantized);
204 _is_activationlayer_enabled = act_info.enabled();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000205
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100206 const ITensor *gemm_input_to_use = input;
207 ITensor *gemm_output_to_use = output;
208 ITensor *gemm_output_staged_to_use = output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000209
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100210 // Get convolved dimensions
211 unsigned int conv_w = 0;
212 unsigned int conv_h = 0;
213 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
214 input->info()->dimension(idx_height),
215 kernel_width,
216 kernel_height,
217 conv_info,
218 dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000219
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100220 // Check if GEMM3D is supported
221 if(_skip_col2im)
222 {
223 // If not supported, we need to perform im2col and col2im (or reshape layer)
224 if(!bool(validate_gemm3d(input->info()->data_type(), conv_h, _skip_im2col)))
225 {
226 _skip_im2col = false;
227 _skip_col2im = false;
228 }
229 }
230
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100231 const ITensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
232
233 // Get parameters from conv_info
234 unsigned int stride_x = 0;
235 unsigned int stride_y = 0;
236 std::tie(stride_x, stride_y) = conv_info.stride();
237
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100238 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000239
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100240 // _weights_reshaped will be auto configured in the kernel.
241 // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM
242 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
243
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100244 // Create tensor to store im2col reshaped inputs
Michalis Spyroue2503892018-04-23 15:17:31 +0100245 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000246 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100247 _memory_group.manage(&_im2col_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100248
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100249 // Configure
Giorgio Arena0f170392018-07-18 16:13:12 +0100250 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation);
Michalis Spyroue2503892018-04-23 15:17:31 +0100251
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100252 // Update GEMM input
253 gemm_input_to_use = &_im2col_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000254 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100255 else if(_append_bias)
256 {
257 // Configure add bias kernel
258 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
259 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000260
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100261 // Create temporary GEMM output tensor in case we cannot skip col2im
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000262 if(!_skip_col2im || _is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000263 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100264 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
265 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000266 TensorShape shape_gemm;
267
268 if(_is_quantized && _skip_col2im)
269 {
270 shape_gemm = output->info()->tensor_shape();
271 }
272 else
273 {
274 // Calculate GEMM output shape
275 shape_gemm = _im2col_output.info()->tensor_shape();
276 shape_gemm.set(0, mat_weights_cols);
277 shape_gemm.set(1, conv_w * conv_h);
278 }
279
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100280 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
281 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100282 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100283 _gemm_output.allocator()->init(info_gemm);
284 _memory_group.manage(&_gemm_output);
285
286 // Update GEMM output
287 gemm_output_to_use = &_gemm_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000288 }
289
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100290 // Configure GEMM
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000291 // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix
292 const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0;
293 configure_mm(gemm_input_to_use, &_weights_reshaped, gemm_output_to_use, gemm_3d_depth);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100294
Michalis Spyroue2503892018-04-23 15:17:31 +0100295 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000296 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100297 _im2col_output.allocator()->allocate();
298 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000299
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100300 // Configure output stage for quantized case
301 if(_is_quantized)
302 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100303 const bool skip_reshape = data_layout == DataLayout::NHWC;
Georgios Pinitas08346e92018-10-16 19:10:46 +0100304 const QuantizationInfo input_quant_info = input->info()->quantization_info();
305 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input_quant_info : output->info()->quantization_info();
Michalis Spyroue2503892018-04-23 15:17:31 +0100306
Georgios Pinitas08346e92018-10-16 19:10:46 +0100307 float multiplier = input_quant_info.scale * weights->info()->quantization_info().scale / output_quant_info.scale;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100308 int output_multiplier, output_shift;
309 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Michalis Spyroue2503892018-04-23 15:17:31 +0100310
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100311 if(!skip_reshape)
312 {
313 _memory_group.manage(&_tmp_output);
314 gemm_output_staged_to_use = &_tmp_output;
315 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100316
Georgios Pinitas08346e92018-10-16 19:10:46 +0100317 // Merge activation with output stage
Georgios Pinitasc73e2b82018-11-08 13:33:16 +0000318 int min_activation = 0;
319 int max_activation = 0;
320
Georgios Pinitas08346e92018-10-16 19:10:46 +0100321 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
322 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
323 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
324 };
325 if(_is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
326 {
Georgios Pinitasc73e2b82018-11-08 13:33:16 +0000327 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
328 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
329
330 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
331 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
332
Georgios Pinitas08346e92018-10-16 19:10:46 +0100333 _is_activationlayer_enabled = false;
334 }
335
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000336 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset, min_activation, max_activation);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100337 }
338
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100339 if(!_skip_col2im && _data_layout == DataLayout::NCHW)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100340 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100341 // Configure col2im
342 _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 +0100343 }
344
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100345 if(_is_quantized && data_layout == DataLayout::NCHW)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100346 {
347 _tmp_output.allocator()->allocate();
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100348 }
349
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000350 if(!_skip_col2im || _is_quantized)
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100351 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100352 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000353 }
354
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100355 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
356 "Output shape does not match the expected one");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000357
Georgios Pinitas08346e92018-10-16 19:10:46 +0100358 // Configure Activation Layer
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000359 if(_is_activationlayer_enabled)
360 {
361 _activationlayer_function.configure(output, nullptr, act_info);
362 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100363
364 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000365}
366
367Status 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 +0100368 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000369{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100370 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
371 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
372 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
373 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
374 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100375 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 +0000376
Michalis Spyroue2503892018-04-23 15:17:31 +0100377 const DataLayout data_layout = input->data_layout();
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100378 const DataType data_type = input->data_type();
Michalis Spyroue2503892018-04-23 15:17:31 +0100379 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
380 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100381 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
382 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100383
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100384 const unsigned int kernel_width = weights->dimension(idx_width);
385 const unsigned int kernel_height = weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000386
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100387 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
388 const ITensorInfo *gemm_input_to_use = input;
389 const ITensorInfo *gemm_output_to_use = output;
390 const ITensorInfo *gemm_output_staged_to_use = output;
391 const ITensorInfo *weights_to_use = weights;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000392
Georgios Pinitas08346e92018-10-16 19:10:46 +0100393 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
394 const bool append_bias = (biases != nullptr) && (!is_quantized);
395 bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
396 bool skip_col2im = data_layout == DataLayout::NHWC;
397 bool is_activation_enabled = act_info.enabled();
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100398
399 // Get convolved dimensions
400 unsigned int conv_w = 0;
401 unsigned int conv_h = 0;
402
403 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
404 input->dimension(idx_height),
405 kernel_width,
406 kernel_height,
407 conv_info,
408 dilation);
409
410 // Check if GEMM3D is supported
411 if(skip_col2im)
412 {
413 // If not supported, we need to perform im2col and col2im (or reshape layer)
414 if(!bool(validate_gemm3d(input->data_type(), conv_h, skip_im2col)))
415 {
416 skip_im2col = false;
417 skip_col2im = false;
418 }
419 }
420
421 const unsigned bias_element = (append_bias && !skip_im2col) ? 1 : 0;
422 const ITensorInfo *biases_to_use = (append_bias && !skip_im2col) ? biases : nullptr;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000423
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100424 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
425 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000426
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100427 // Validate biases
428 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000429 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100430 if(is_quantized)
431 {
432 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
433 }
434 else
435 {
436 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
437 }
438 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
439 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000440 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000441
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100442 if(act_info.enabled())
443 {
444 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
445 }
446
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100447 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
448 unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + bias_element;
449
450 // Output tensor auto inizialization if not yet initialized
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100451 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, biases_to_use, nullptr));
452 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col)), 1, data_type);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100453 weights_to_use = &weights_reshaped_info;
454
Michalis Spyroue2503892018-04-23 15:17:31 +0100455 if(!skip_im2col)
456 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100457 // Create tensor info for im2col reshaped inputs
458 // For NEON the batch size is on the fourth dimension
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100459 // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100460 TensorShape shape_im2col = input->tensor_shape();
461 shape_im2col.set(0, mat_weights_rows);
462 shape_im2col.set(1, conv_w * conv_h);
463 shape_im2col.set(2, 1);
464
465 im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
466 im2col_reshaped_info.set_quantization_info(input->quantization_info());
467
Giorgio Arena0f170392018-07-18 16:13:12 +0100468 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 +0100469 gemm_input_to_use = &im2col_reshaped_info;
Michalis Spyroue2503892018-04-23 15:17:31 +0100470 }
471 else if(append_bias)
472 {
473 // Validate add bias kernel
474 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
475 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000476
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100477 // Create temporary GEMM output tensor in case we cannot skip col2im
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000478 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100479 if(!skip_col2im)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000480 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100481 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
482 shape_gemm.set(0, mat_weights_cols);
483 shape_gemm.set(1, conv_w * conv_h);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100484 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
Michalis Spyroue2503892018-04-23 15:17:31 +0100485 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000486 else
487 {
488 info_gemm = TensorInfo(output->tensor_shape(), 1, gemm_data_type);
489 }
490 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
491 gemm_output_to_use = &info_gemm;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000492
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000493 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 +0000494
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100495 if(is_quantized)
496 {
Georgios Pinitas08346e92018-10-16 19:10:46 +0100497 const bool skip_reshape = data_layout == DataLayout::NHWC;
498 const QuantizationInfo input_quant_info = input->quantization_info();
499 const QuantizationInfo output_quant_info = (output->total_size() == 0) ? input_quant_info : output->quantization_info();
500 const float multiplier = input_quant_info.scale * weights_to_use->quantization_info().scale / output_quant_info.scale;
501 int output_multiplier, output_shift;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100502 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
503
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100504 if(!skip_reshape)
505 {
506 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
507 tmp_info.set_quantization_info(output->quantization_info());
508 gemm_output_staged_to_use = &tmp_info;
509 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100510
Georgios Pinitas08346e92018-10-16 19:10:46 +0100511 // Merge activation with output stage
Georgios Pinitasc73e2b82018-11-08 13:33:16 +0000512 int min_activation = 0;
513 int max_activation = 0;
514
Georgios Pinitas08346e92018-10-16 19:10:46 +0100515 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
516 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
517 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
518 };
Georgios Pinitasc73e2b82018-11-08 13:33:16 +0000519
Georgios Pinitas08346e92018-10-16 19:10:46 +0100520 if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0)
521 {
Georgios Pinitasc73e2b82018-11-08 13:33:16 +0000522 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
523 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
524
525 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
526 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
527
Georgios Pinitas08346e92018-10-16 19:10:46 +0100528 is_activation_enabled = false;
529 }
530
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100531 // Validate output stage for quantized case
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000532 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, min_activation, max_activation);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100533 }
534
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100535 // Validate Col2Im/ReshapeLayer
536 if(!skip_col2im && (data_layout == DataLayout::NCHW))
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100537 {
538 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
539 output,
540 Size2D(conv_w, conv_h)));
541 }
542
543 //Validate Activation Layer
Georgios Pinitas08346e92018-10-16 19:10:46 +0100544 if(is_activation_enabled)
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000545 {
546 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
547 }
548
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000549 return Status{};
550}
551
552void NEGEMMConvolutionLayer::run()
553{
Georgios Pinitas72219332018-06-05 14:56:06 +0100554 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000555
556 _memory_group.acquire();
557
Michalis Spyroue2503892018-04-23 15:17:31 +0100558 if(!_skip_im2col)
559 {
560 // Run input reshaping
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100561 unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
562 NEScheduler::get().schedule(&_im2col_kernel, y_dim);
Michalis Spyroue2503892018-04-23 15:17:31 +0100563 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000564
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100565 // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions
566 if(_is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000567 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100568 // Run gemmlowp
569 _mm_gemmlowp.run();
570
571 // Run output stage
572 _gemmlowp_output_stage.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000573 }
574 else
575 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100576 // Run gemm
577 _mm_gemm.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000578 }
579
Michalis Spyroue2503892018-04-23 15:17:31 +0100580 if(_skip_im2col && _append_bias)
581 {
582 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
583 }
584
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000585 // Reshape output matrix
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100586 if(!_skip_col2im && _data_layout == DataLayout::NCHW)
Michalis Spyroue2503892018-04-23 15:17:31 +0100587 {
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100588 NEScheduler::get().schedule(&_col2im_kernel, Window::DimY);
Michalis Spyroue2503892018-04-23 15:17:31 +0100589 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000590
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000591 if(_is_activationlayer_enabled)
592 {
593 _activationlayer_function.run();
594 }
595
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000596 _memory_group.release();
597}
Georgios Pinitas72219332018-06-05 14:56:06 +0100598
599void NEGEMMConvolutionLayer::prepare()
600{
601 if(!_is_prepared)
602 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100603 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100604
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100605 // Run weights reshaping and mark original weights tensor as unused
606 _weights_reshaped.allocator()->allocate();
607 _reshape_weights.run();
608 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100609
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100610 // Prepare GEMM
611 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100612 if(!_weights_reshaped.is_used())
613 {
614 _weights_reshaped.allocator()->free();
615 }
616
617 _is_prepared = true;
618 }
619}