blob: df4a040bada20ecbb807fce9e109796dd15aca9c [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>
35#include <tuple>
36
Gian Marco Iodice597a8562018-08-01 15:06:06 +010037using namespace arm_compute;
38using namespace arm_compute::misc::shape_calculator;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000039
Gian Marco Iodice597a8562018-08-01 15:06:06 +010040NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights()
41 : _weights_reshape_kernel()
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000042{
43}
44
Gian Marco Iodice597a8562018-08-01 15:06:06 +010045void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +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,
Gian Marco Iodice597a8562018-08-01 15:06:06 +010051 output->info()));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000052
Gian Marco Iodice597a8562018-08-01 15:06:06 +010053 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000054 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
55
Gian Marco Iodice597a8562018-08-01 15:06:06 +010056 _weights_reshape_kernel.configure(weights, biases_to_use, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000057
58 output->info()->set_quantization_info(weights->info()->quantization_info());
59}
60
Gian Marco Iodice597a8562018-08-01 15:06:06 +010061Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000062{
Gian Marco Iodice597a8562018-08-01 15:06:06 +010063 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010064 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 +000065 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000066
Gian Marco Iodice597a8562018-08-01 15:06:06 +010067 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000068 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010069 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +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);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010072 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000073 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
74 }
75
Gian Marco Iodice597a8562018-08-01 15:06:06 +010076 if((output != nullptr) && (output->total_size() != 0))
Michalis Spyroue2503892018-04-23 15:17:31 +010077 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010078 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Michalis Spyroue2503892018-04-23 15:17:31 +010079
Gian Marco Iodice597a8562018-08-01 15:06:06 +010080 NEWeightsReshapeKernel::validate(weights, biases, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000081 }
82
83 return Status{};
84}
85
86void NEConvolutionLayerReshapeWeights::run()
87{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000088 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000089}
90
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000091NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager)
Gian Marco Iodice597a8562018-08-01 15:06:06 +010092 : _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(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false), _skip_im2col(false),
94 _skip_col2im(false), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000095{
96}
97
Gian Marco Iodice597a8562018-08-01 15:06:06 +010098void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, ITensor *output, int gemm_3d_depth)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000099{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100100 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
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +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 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100121 // 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 */));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000124 }
125}
126
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100127Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
128{
129 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
130
131 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, gemm_3d_depth, skip_im2col);
132 if(is_quantized)
133 {
134 // 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();
138
139 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 NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
146 }
147 else
148 {
149 // Perform validation step on Matrix multiply function
150 NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
151 }
152 return Status{};
153}
154
Alex Gilday7da29b62018-03-23 14:16:00 +0000155void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000156 const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000157{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000158 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
159
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100160 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(),
161 weights->info(),
162 biases != nullptr ? biases->info() : nullptr,
163 output->info(),
164 conv_info,
165 weights_info,
166 dilation,
167 act_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000168
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100169 const DataType data_type = input->info()->data_type();
170 const DataLayout data_layout = input->info()->data_layout();
171 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
172 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
173 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
174 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100175
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100176 const unsigned int kernel_width = weights->info()->dimension(idx_width);
177 const unsigned int kernel_height = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000178
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100179 _is_prepared = weights_info.retain_internal_weights();
180 _original_weights = weights;
181 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
182 _data_layout = data_layout;
183 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1) && !_is_quantized;
184 _skip_col2im = (data_layout == DataLayout::NHWC) && !_is_quantized;
185 _append_bias = (biases != nullptr) && (!_is_quantized);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000186
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100187 // TODO (giaiod01): Validate GEMM3D
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000188
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100189 const bool is_nhwc = _data_layout == DataLayout::NHWC;
190 const ITensor *gemm_input_to_use = input;
191 ITensor *gemm_output_to_use = output;
192 ITensor *gemm_output_staged_to_use = output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000193
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100194 const unsigned bias_element = (_append_bias && !_skip_im2col) ? 1 : 0;
195 const ITensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000196
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100197 // Get parameters from conv_info
198 unsigned int stride_x = 0;
199 unsigned int stride_y = 0;
200 std::tie(stride_x, stride_y) = conv_info.stride();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000201
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100202 // Get convolved dimensions
203 unsigned int conv_w = 0;
204 unsigned int conv_h = 0;
205 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
206 input->info()->dimension(idx_height),
207 kernel_width,
208 kernel_height,
209 conv_info,
210 dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000211
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100212 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
213 unsigned int mat_weights_rows = weights->info()->dimension(idx_width) * weights->info()->dimension(idx_height) * weights->info()->dimension(idx_channel) + bias_element;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000214
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100215 // _weights_reshaped will be auto configured in the kernel.
216 // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM
217 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
218
219 weights = &_weights_reshaped;
220
221 // Create tensor to store im2col reshaped inputs
Michalis Spyroue2503892018-04-23 15:17:31 +0100222 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000223 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100224 // Calculate im2col shape
225 // For NEON the batch size is on the fourth dimension
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100226 // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100227 TensorShape shape_im2col = input->info()->tensor_shape();
228 shape_im2col.set(0, mat_weights_rows);
229 shape_im2col.set(1, conv_w * conv_h);
Michalis Spyroue2503892018-04-23 15:17:31 +0100230 shape_im2col.set(2, 1);
Michalis Spyroue2503892018-04-23 15:17:31 +0100231
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100232 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
233 _memory_group.manage(&_im2col_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100234
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100235 // Configure
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100236 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, false, false, dilation);
Michalis Spyroue2503892018-04-23 15:17:31 +0100237
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100238 // Update GEMM input
239 gemm_input_to_use = &_im2col_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000240 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100241 else if(_append_bias)
242 {
243 // Configure add bias kernel
244 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
245 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000246
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100247 // Create GEMM output tensor
248 if(!is_nhwc || _is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000249 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100250 // Calculate GEMM output shape
251 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
252 shape_gemm.set(0, mat_weights_cols);
253 shape_gemm.set(1, conv_w * conv_h);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000254
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100255 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
256 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
257 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
258 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
259 info_gemm.set_quantization_info(output->info()->quantization_info());
260 _gemm_output.allocator()->init(info_gemm);
261 _memory_group.manage(&_gemm_output);
262
263 // Update GEMM output
264 gemm_output_to_use = &_gemm_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000265 }
266
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100267 // Configure and tune GEMM
268 configure_mm(gemm_input_to_use, weights, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1);
269
Michalis Spyroue2503892018-04-23 15:17:31 +0100270 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000271 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100272 _im2col_output.allocator()->allocate();
273 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000274
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100275 // Configure output stage for quantized case
276 if(_is_quantized)
277 {
278 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
Michalis Spyroue2503892018-04-23 15:17:31 +0100279
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100280 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
281 int output_multiplier, output_shift;
282 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Michalis Spyroue2503892018-04-23 15:17:31 +0100283
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100284 _memory_group.manage(&_tmp_output);
285 gemm_output_staged_to_use = &_tmp_output;
Michalis Spyroue2503892018-04-23 15:17:31 +0100286
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100287 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset);
288 }
289
290 if(!_skip_col2im)
291 {
292 // Configure and tune Col2Im
293 _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, Size2D(conv_w, conv_h));
294 }
295
296 if(!is_nhwc || _is_quantized)
297 {
298 _tmp_output.allocator()->allocate();
Michalis Spyroue2503892018-04-23 15:17:31 +0100299 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000300 }
301
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100302 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
303 "Output shape does not match the expected one");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000304
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000305 //Configure Activation Layer
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100306 _is_activationlayer_enabled = act_info.enabled();
307
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000308 if(_is_activationlayer_enabled)
309 {
310 _activationlayer_function.configure(output, nullptr, act_info);
311 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100312
313 ARM_COMPUTE_UNUSED(weights_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000314}
315
316Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000317 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000318{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100319 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
320 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
321 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
322 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
323 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000324
Michalis Spyroue2503892018-04-23 15:17:31 +0100325 const DataLayout data_layout = input->data_layout();
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100326 const DataType data_type = input->data_type();
Michalis Spyroue2503892018-04-23 15:17:31 +0100327 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
328 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100329 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
330 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100331
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100332 const unsigned int kernel_width = weights->dimension(idx_width);
333 const unsigned int kernel_height = weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000334
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100335 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
336 const ITensorInfo *gemm_input_to_use = input;
337 const ITensorInfo *gemm_output_to_use = output;
338 const ITensorInfo *gemm_output_staged_to_use = output;
339 const ITensorInfo *weights_to_use = weights;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000340
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100341 const bool is_nhwc = data_layout == DataLayout::NHWC;
342 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
343 bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1) && !is_quantized;
344 const bool append_bias = (biases != nullptr) && (!is_quantized);
345 const unsigned bias_element = (append_bias && !skip_im2col) ? 1 : 0;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000346
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100347 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
348 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000349
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100350 // Validate biases
351 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000352 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100353 if(is_quantized)
354 {
355 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
356 }
357 else
358 {
359 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
360 }
361 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
362 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000363 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000364
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100365 if(act_info.enabled())
366 {
367 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
368 }
369
370 // Get convolved dimensions
371 unsigned int conv_w = 0;
372 unsigned int conv_h = 0;
373
374 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
375 input->dimension(idx_height),
376 kernel_width,
377 kernel_height,
378 conv_info,
379 dilation);
380
381 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
382 unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel) + bias_element;
383
384 // Output tensor auto inizialization if not yet initialized
385 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr));
386 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, append_bias), 1, data_type);
387 weights_to_use = &weights_reshaped_info;
388
389 // TODO (giaiod01): Validate GEMM3D
Michalis Spyroue2503892018-04-23 15:17:31 +0100390
391 if(!skip_im2col)
392 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100393 // Create tensor info for im2col reshaped inputs
394 // For NEON the batch size is on the fourth dimension
395 // TODO (giaiod01): Use auto-init COMPMID-1277
396 TensorShape shape_im2col = input->tensor_shape();
397 shape_im2col.set(0, mat_weights_rows);
398 shape_im2col.set(1, conv_w * conv_h);
399 shape_im2col.set(2, 1);
400
401 im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
402 im2col_reshaped_info.set_quantization_info(input->quantization_info());
403
404 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, false, false, dilation));
405 gemm_input_to_use = &im2col_reshaped_info;
Michalis Spyroue2503892018-04-23 15:17:31 +0100406 }
407 else if(append_bias)
408 {
409 // Validate add bias kernel
410 ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
411 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000412
413 // Create GEMM output tensor
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100414 if(!is_nhwc || is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000415 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100416 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
417 shape_gemm.set(0, mat_weights_cols);
418 shape_gemm.set(1, conv_w * conv_h);
419 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
420 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
421 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
422 info_gemm.set_quantization_info(output->quantization_info());
Gian Marco Iodicea72300a2018-04-12 11:41:26 +0100423
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100424 gemm_output_to_use = &info_gemm;
Michalis Spyroue2503892018-04-23 15:17:31 +0100425 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000426
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100427 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1, skip_im2col));
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000428
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100429 if(is_quantized)
430 {
431 float multiplier = input->quantization_info().scale * weights_to_use->quantization_info().scale / output->quantization_info().scale;
432 int output_multiplier, output_shift;
433 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
434
435 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
436 tmp_info.set_quantization_info(output->quantization_info());
437 gemm_output_staged_to_use = &tmp_info;
438
439 // Validate output stage for quantized case
440 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, output->quantization_info().offset);
441 }
442
443 // Validate Col2Im
444 if(!is_nhwc || is_quantized)
445 {
446 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
447 output,
448 Size2D(conv_w, conv_h)));
449 }
450
451 //Validate Activation Layer
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000452 if(act_info.enabled())
453 {
454 ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayer::validate(output, nullptr, act_info));
455 }
456
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000457 return Status{};
458}
459
460void NEGEMMConvolutionLayer::run()
461{
Georgios Pinitas72219332018-06-05 14:56:06 +0100462 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000463
464 _memory_group.acquire();
465
Michalis Spyroue2503892018-04-23 15:17:31 +0100466 if(!_skip_im2col)
467 {
468 // Run input reshaping
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100469 unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
470 NEScheduler::get().schedule(&_im2col_kernel, y_dim);
Michalis Spyroue2503892018-04-23 15:17:31 +0100471 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000472
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100473 // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions
474 if(_is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000475 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100476 // Run gemmlowp
477 _mm_gemmlowp.run();
478
479 // Run output stage
480 _gemmlowp_output_stage.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000481 }
482 else
483 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100484 // Run gemm
485 _mm_gemm.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000486 }
487
Michalis Spyroue2503892018-04-23 15:17:31 +0100488 if(_skip_im2col && _append_bias)
489 {
490 NEScheduler::get().schedule(&_add_bias_kernel, Window::DimY);
491 }
492
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000493 // Reshape output matrix
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100494 if(!_skip_col2im)
Michalis Spyroue2503892018-04-23 15:17:31 +0100495 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100496 NEScheduler::get().schedule(&_col2im_kernel, Window::DimY);
Michalis Spyroue2503892018-04-23 15:17:31 +0100497 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000498
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000499 if(_is_activationlayer_enabled)
500 {
501 _activationlayer_function.run();
502 }
503
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000504 _memory_group.release();
505}
Georgios Pinitas72219332018-06-05 14:56:06 +0100506
507void NEGEMMConvolutionLayer::prepare()
508{
509 if(!_is_prepared)
510 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100511 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Georgios Pinitas72219332018-06-05 14:56:06 +0100512
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100513 // Run weights reshaping and mark original weights tensor as unused
514 _weights_reshaped.allocator()->allocate();
515 _reshape_weights.run();
516 _original_weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100517
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100518 // Prepare GEMM
519 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100520 if(!_weights_reshaped.is_used())
521 {
522 _weights_reshaped.allocator()->free();
523 }
524
525 _is_prepared = true;
526 }
527}