blob: caff117e095e53135158915fc334da5d2a5e8790 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
George Wort2d7e6832019-02-22 16:37:41 +00002 * Copyright (c) 2017-2019 ARM Limited.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00003 *
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"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000032
Georgios Pinitas08346e92018-10-16 19:10:46 +010033#include <set>
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000034#include <tuple>
35
Gian Marco Iodice597a8562018-08-01 15:06:06 +010036using namespace arm_compute;
37using namespace arm_compute::misc::shape_calculator;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000038
Gian Marco Iodice597a8562018-08-01 15:06:06 +010039NEConvolutionLayerReshapeWeights::NEConvolutionLayerReshapeWeights()
40 : _weights_reshape_kernel()
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000041{
42}
43
Gian Marco Iodice597a8562018-08-01 15:06:06 +010044void NEConvolutionLayerReshapeWeights::configure(const ITensor *weights, const ITensor *biases, ITensor *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000045{
46 // Perform validation step
47 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
48 ARM_COMPUTE_ERROR_THROW_ON(NEConvolutionLayerReshapeWeights::validate(weights->info(),
49 (biases != nullptr) ? biases->info() : nullptr,
Gian Marco Iodice597a8562018-08-01 15:06:06 +010050 output->info()));
Gian Marco Iodice597a8562018-08-01 15:06:06 +010051 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000052 const ITensor *biases_to_use = (append_biases) ? biases : nullptr;
53
Gian Marco Iodice597a8562018-08-01 15:06:06 +010054 _weights_reshape_kernel.configure(weights, biases_to_use, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000055
56 output->info()->set_quantization_info(weights->info()->quantization_info());
57}
58
Gian Marco Iodice597a8562018-08-01 15:06:06 +010059Status NEConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060{
Gian Marco Iodice597a8562018-08-01 15:06:06 +010061 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010062 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QSYMM8_PER_CHANNEL, DataType::F16, DataType::F32);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000063 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000064
Gian Marco Iodice597a8562018-08-01 15:06:06 +010065 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000066 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010067 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000068 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Gian Marco Iodice597a8562018-08-01 15:06:06 +010070 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000071 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
72 }
73
Gian Marco Iodice597a8562018-08-01 15:06:06 +010074 if((output != nullptr) && (output->total_size() != 0))
Michalis Spyroue2503892018-04-23 15:17:31 +010075 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Michalis Spyroue2503892018-04-23 15:17:31 +010077
Gian Marco Iodice597a8562018-08-01 15:06:06 +010078 NEWeightsReshapeKernel::validate(weights, biases, output);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000079 }
80
81 return Status{};
82}
83
84void NEConvolutionLayerReshapeWeights::run()
85{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000086 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000087}
88
Michalis Spyrou1a569a32019-09-10 17:20:34 +010089NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, IWeightsManager *weights_manager)
90 : _memory_group(memory_manager), _weights_manager(weights_manager), _reshape_weights(), _reshape_weights_managed(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager),
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010091 _col2im_kernel(), _reshape_layer(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _skip_im2col(false),
92 _skip_col2im(false), _is_quantized(false), _is_prepared(false)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000093{
94}
95
George Wort2d7e6832019-02-22 16:37:41 +000096void NEGEMMConvolutionLayer::configure_mm(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const ActivationLayerInfo &act_info, int gemm_3d_depth)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000097{
Gian Marco Iodice597a8562018-08-01 15:06:06 +010098 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010099 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output == nullptr ? nullptr : output->info(),
100 act_info, gemm_3d_depth, _skip_im2col));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100101
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100102 // Create GEMMInfo structure
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000103 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100104 gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
105 false, GEMMLowpOutputStageInfo(), false, false, act_info);
106
107 // Supported activations in GEMM
108 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
109 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
110 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
111 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000112
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000113 if(_is_quantized)
114 {
115 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
116 // Extract and negate input and weights offset
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100117 const QuantizationInfo iqinfo = input->info()->quantization_info();
118 const QuantizationInfo wqinfo = weights->info()->quantization_info();
119 const QuantizationInfo oqinfo = (output->info()->total_size() == 0) ? iqinfo : output->info()->quantization_info();
120 const UniformQuantizationInfo uiqinfo = iqinfo.uniform();
121 const UniformQuantizationInfo uoqinfo = oqinfo.uniform();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000122
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100123 input->info()->set_quantization_info(QuantizationInfo(uiqinfo.scale, -uiqinfo.offset));
124 if(!is_data_type_quantized_per_channel(weights->info()->data_type()))
125 {
126 const UniformQuantizationInfo uwqinfo = wqinfo.uniform();
127 weights->info()->set_quantization_info(QuantizationInfo(uwqinfo.scale, -uwqinfo.offset));
128 }
George Wort2d7e6832019-02-22 16:37:41 +0000129
130 // Merge activation with output stage
131 int min_activation = 0;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100132 int max_activation = 255;
George Wort2d7e6832019-02-22 16:37:41 +0000133
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100134 if(supported_acts.count(act_info.activation()) != 0)
George Wort2d7e6832019-02-22 16:37:41 +0000135 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100136 const int a_const_int = quantize_qasymm8(act_info.a(), uoqinfo);
137 const int b_const_int = quantize_qasymm8(act_info.b(), uoqinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000138
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100139 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? uoqinfo.offset : b_const_int;
George Wort2d7e6832019-02-22 16:37:41 +0000140 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
George Wort2d7e6832019-02-22 16:37:41 +0000141 }
142
143 GEMMLowpOutputStageInfo output_info;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100144 output_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
145 output_info.gemmlowp_offset = uoqinfo.offset;
146 output_info.gemmlowp_min_bound = min_activation;
147 output_info.gemmlowp_max_bound = max_activation;
148 quantization::calculate_quantized_multipliers_less_than_one(iqinfo, wqinfo, oqinfo, output_info);
George Wort2d7e6832019-02-22 16:37:41 +0000149
150 _mm_gemmlowp.configure(input, weights, biases, output, GEMMInfo(false, false, true, gemm_3d_depth, _skip_im2col, false, output_info));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000151
152 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100153 input->info()->set_quantization_info(iqinfo);
154 weights->info()->set_quantization_info(wqinfo);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000155 }
156 else
157 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100158 // Configure matrix multiply function
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100159 _mm_gemm.configure(input, weights, biases, output, 1.0f, 0.0f, gemm_info);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000160 }
161}
162
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100163Status NEGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
164 const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100165{
George Wort2d7e6832019-02-22 16:37:41 +0000166 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
167 const bool is_activation_enabled = act_info.enabled();
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100168
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100169 // Create GEMMInfo structure
170 const GEMMInfo gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
171 gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
172 false, GEMMLowpOutputStageInfo(), false, false, act_info);
173
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100174 if(is_quantized)
175 {
176 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
177 // Extract and negate input and weights offset
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100178 const QuantizationInfo &iqinfo = input->quantization_info();
179 const QuantizationInfo &wqinfo = weights->quantization_info();
180 const QuantizationInfo &oqinfo = (output->total_size() == 0) ? iqinfo : output->quantization_info();
181 const UniformQuantizationInfo uoqinfo = oqinfo.uniform();
George Wort2d7e6832019-02-22 16:37:41 +0000182
183 // Merge activation with output stage
184 int min_activation = 0;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100185 int max_activation = 255;
George Wort2d7e6832019-02-22 16:37:41 +0000186
187 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
188 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
189 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
190 };
191 if(is_activation_enabled && supported_acts.count(act_info.activation()) != 0)
192 {
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100193 const int a_const_int = quantize_qasymm8(act_info.a(), uoqinfo);
194 const int b_const_int = quantize_qasymm8(act_info.b(), uoqinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000195
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100196 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? uoqinfo.offset : b_const_int;
George Wort2d7e6832019-02-22 16:37:41 +0000197 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
198 }
199
200 GEMMLowpOutputStageInfo output_info;
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100201 output_info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
202 output_info.gemmlowp_offset = uoqinfo.offset;
203 output_info.gemmlowp_min_bound = min_activation;
204 output_info.gemmlowp_max_bound = max_activation;
205 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multipliers_less_than_one(iqinfo, wqinfo, oqinfo, output_info));
George Wort2d7e6832019-02-22 16:37:41 +0000206
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100207 // Perform validation step on GEMMLowp
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100208 std::unique_ptr<ITensorInfo> input_qa = input->clone();
209 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
210 input_qa->set_quantization_info(QuantizationInfo(iqinfo.uniform().scale, -iqinfo.uniform().offset));
211 weights_qa->set_quantization_info(QuantizationInfo(wqinfo.uniform().scale, -wqinfo.uniform().offset));
George Wort2d7e6832019-02-22 16:37:41 +0000212 return NEGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, GEMMInfo(false, false, true, gemm_3d_depth, skip_im2col, false, output_info));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100213 }
214 else
215 {
216 // Perform validation step on Matrix multiply function
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100217 return NEGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100218 }
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100219}
220
George Wort2d7e6832019-02-22 16:37:41 +0000221Status NEGEMMConvolutionLayer::validate_gemm3d(const ITensorInfo *input_info, const ActivationLayerInfo &act_info, int gemm_3d_depth, bool skip_im2col)
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100222{
George Wort2d7e6832019-02-22 16:37:41 +0000223 const DataType data_type = input_info->data_type();
224 const unsigned int mult_y = skip_im2col ? 1U : gemm_3d_depth;
225 const unsigned int mult_z = skip_im2col ? gemm_3d_depth : 1U;
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100226
227 // Set dummy tensor shapes for the validation
George Wort2d7e6832019-02-22 16:37:41 +0000228 const TensorInfo dummy_input_info(TensorShape(4U, 4U * mult_y, 1U * mult_z), 1, data_type, input_info->quantization_info());
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100229 const TensorInfo dummy_weights_info(TensorShape(4U, 4U), 1, data_type);
George Wort2d7e6832019-02-22 16:37:41 +0000230 const TensorInfo dummy_output_info(TensorShape(4U, 4U, gemm_3d_depth), 1, data_type, input_info->quantization_info());
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100231
George Wort2d7e6832019-02-22 16:37:41 +0000232 return validate_mm(&dummy_input_info, &dummy_weights_info, nullptr, &dummy_output_info, act_info, gemm_3d_depth, skip_im2col);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100233}
234
Alex Gilday7da29b62018-03-23 14:16:00 +0000235void 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 +0100236 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000237{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000238 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100239 ARM_COMPUTE_UNUSED(num_groups, weights_info);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100240 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMConvolutionLayer::validate(input->info(),
241 weights->info(),
242 biases != nullptr ? biases->info() : nullptr,
243 output->info(),
244 conv_info,
245 weights_info,
246 dilation,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100247 act_info,
248 num_groups));
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000249
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100250 const DataType data_type = input->info()->data_type();
251 const DataLayout data_layout = input->info()->data_layout();
252 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
253 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100254 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100255
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100256 const unsigned int kernel_width = weights->info()->dimension(idx_width);
257 const unsigned int kernel_height = weights->info()->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000258
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100259 _is_prepared = weights_info.retain_internal_weights();
260 _original_weights = weights;
261 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
262 _data_layout = data_layout;
263 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000264
George Wort2d7e6832019-02-22 16:37:41 +0000265 const ITensor *gemm_input_to_use = input;
266 ITensor *gemm_output_to_use = output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000267
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100268 // Get convolved dimensions
269 unsigned int conv_w = 0;
270 unsigned int conv_h = 0;
271 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
272 input->info()->dimension(idx_height),
273 kernel_width,
274 kernel_height,
275 conv_info,
276 dilation);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000277
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100278 // Check if GEMM3D is supported
Georgios Pinitase413d252018-11-14 18:29:58 +0000279 if(data_layout == DataLayout::NHWC)
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100280 {
George Wort2d7e6832019-02-22 16:37:41 +0000281 _skip_col2im = bool(validate_gemm3d(input->info(), act_info, conv_h, true));
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100282 // If not supported, we need to perform im2col and col2im (or reshape layer)
Georgios Pinitase413d252018-11-14 18:29:58 +0000283 if(!_skip_col2im)
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100284 {
285 _skip_im2col = false;
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100286 }
287 }
Georgios Pinitase413d252018-11-14 18:29:58 +0000288 else
289 {
290 _skip_col2im = false;
291 }
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100292
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100293 // Get parameters from conv_info
294 unsigned int stride_x = 0;
295 unsigned int stride_y = 0;
296 std::tie(stride_x, stride_y) = conv_info.stride();
297
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100298 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000299
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100300 // _weights_reshaped will be auto configured in the kernel.
301 // Just append biases and do not transpose 1xW as it will be reshaped in NEGEMM
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100302 const ITensor *weights_to_use = weights;
303
304 if(_weights_manager && _weights_manager->are_weights_managed(weights))
305 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100306 _reshape_weights_managed.configure(weights, nullptr);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100307 weights_to_use = _weights_manager->acquire(weights, &_reshape_weights_managed);
308 }
309 else
310 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100311 _reshape_weights.configure(weights, nullptr, &_weights_reshaped);
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100312 weights_to_use = &_weights_reshaped;
313 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100314
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100315 // Create tensor to store im2col reshaped inputs
Michalis Spyroue2503892018-04-23 15:17:31 +0100316 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000317 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100318 _memory_group.manage(&_im2col_output);
Michalis Spyroue2503892018-04-23 15:17:31 +0100319
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100320 // Configure
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100321 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, false, dilation);
Michalis Spyroue2503892018-04-23 15:17:31 +0100322
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100323 // Update GEMM input
324 gemm_input_to_use = &_im2col_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000325 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000326
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100327 // Create temporary GEMM output tensor in case we cannot skip col2im
George Wort2d7e6832019-02-22 16:37:41 +0000328 if(!_skip_col2im)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000329 {
George Wort2d7e6832019-02-22 16:37:41 +0000330 TensorShape shape_gemm;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000331
George Wort2d7e6832019-02-22 16:37:41 +0000332 // Calculate GEMM output shape
333 shape_gemm = _im2col_output.info()->tensor_shape();
334 shape_gemm.set(0, mat_weights_cols);
335 shape_gemm.set(1, conv_w * conv_h);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000336
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100337 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
George Wort2d7e6832019-02-22 16:37:41 +0000338 TensorInfo info_gemm(shape_gemm, 1, data_type);
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100339 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100340 _gemm_output.allocator()->init(info_gemm);
341 _memory_group.manage(&_gemm_output);
342
343 // Update GEMM output
344 gemm_output_to_use = &_gemm_output;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000345 }
346
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100347 // Configure GEMM
Gian Marco Iodice3139f032018-11-05 14:26:32 +0000348 // In case we need to skip col2im, GEMM3D (gemm_3d_depth != 0) must be called in order to avoid reshaping the output matrix
349 const unsigned int gemm_3d_depth = _skip_col2im ? conv_h : 0;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100350 configure_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, act_info, gemm_3d_depth);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100351
Michalis Spyroue2503892018-04-23 15:17:31 +0100352 if(!_skip_im2col)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000353 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100354 _im2col_output.allocator()->allocate();
355 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000356
Georgios Pinitase413d252018-11-14 18:29:58 +0000357 if(!_skip_col2im)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100358 {
Georgios Pinitase413d252018-11-14 18:29:58 +0000359 if(_data_layout == DataLayout::NCHW)
360 {
361 // Configure col2im
George Wort2d7e6832019-02-22 16:37:41 +0000362 _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h));
Georgios Pinitase413d252018-11-14 18:29:58 +0000363 }
364 else
365 {
366 // Configure reshape layer
George Wort2d7e6832019-02-22 16:37:41 +0000367 _reshape_layer.configure(gemm_output_to_use, output);
Georgios Pinitase413d252018-11-14 18:29:58 +0000368 }
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100369 }
370
Georgios Pinitase413d252018-11-14 18:29:58 +0000371 if(_is_quantized && !_skip_col2im)
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100372 {
373 _tmp_output.allocator()->allocate();
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100374 }
375
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000376 if(!_skip_col2im || _is_quantized)
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100377 {
Michalis Spyroue2503892018-04-23 15:17:31 +0100378 _gemm_output.allocator()->allocate();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000379 }
380
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100381 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
382 "Output shape does not match the expected one");
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000383}
384
385Status 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 +0100386 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000387{
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100388 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
389 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
390 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100391 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::QSYMM8_PER_CHANNEL, DataType::F16, DataType::F32);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100392 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100393 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 +0000394
Michalis Spyroue2503892018-04-23 15:17:31 +0100395 const DataLayout data_layout = input->data_layout();
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100396 const DataType data_type = input->data_type();
Michalis Spyroue2503892018-04-23 15:17:31 +0100397 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
398 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100399 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
400 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
Michalis Spyroue2503892018-04-23 15:17:31 +0100401
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100402 const unsigned int kernel_width = weights->dimension(idx_width);
403 const unsigned int kernel_height = weights->dimension(idx_height);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000404
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100405 TensorInfo im2col_reshaped_info{};
406 TensorInfo info_gemm{};
407 TensorInfo tmp_info{};
408 TensorInfo weights_reshaped_info{};
George Wort2d7e6832019-02-22 16:37:41 +0000409 const ITensorInfo *gemm_input_to_use = input;
410 const ITensorInfo *gemm_output_to_use = output;
411 const ITensorInfo *weights_to_use = weights;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000412
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100413 const bool append_bias = false;
414 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
415 bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100416
417 // Get convolved dimensions
418 unsigned int conv_w = 0;
419 unsigned int conv_h = 0;
420
421 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
422 input->dimension(idx_height),
423 kernel_width,
424 kernel_height,
425 conv_info,
426 dilation);
427
428 // Check if GEMM3D is supported
Georgios Pinitase413d252018-11-14 18:29:58 +0000429 bool skip_col2im = false;
430 if(data_layout == DataLayout::NHWC)
431 {
George Wort2d7e6832019-02-22 16:37:41 +0000432 skip_col2im = bool(validate_gemm3d(input, act_info, conv_h, true));
Georgios Pinitase413d252018-11-14 18:29:58 +0000433 // If not supported, we need to perform im2col and col2im (or reshape layer)
434 if(!skip_col2im)
435 {
436 skip_im2col = false;
437 }
438 }
439
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100440 if(skip_col2im)
441 {
442 // If not supported, we need to perform im2col and col2im (or reshape layer)
George Wort2d7e6832019-02-22 16:37:41 +0000443 if(!bool(validate_gemm3d(input, act_info, conv_h, skip_im2col)))
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100444 {
445 skip_im2col = false;
446 skip_col2im = false;
447 }
448 }
449
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100450 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(idx_channel) != input->dimension(idx_channel));
451 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000452
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100453 // Validate biases
454 if(biases != nullptr)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000455 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100456 if(is_quantized)
457 {
458 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
459 }
460 else
461 {
462 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
463 }
464 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
465 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000466 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000467
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100468 unsigned int mat_weights_cols = weights->dimension(idx_kernels);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100469 unsigned int mat_weights_rows = weights->dimension(idx_width) * weights->dimension(idx_height) * weights->dimension(idx_channel);
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100470
471 // Output tensor auto inizialization if not yet initialized
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100472 ARM_COMPUTE_RETURN_ON_ERROR(NEConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr));
473 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, append_bias), 1, data_type);
Georgios Pinitas4d600c72019-07-30 15:09:10 +0100474 weights_reshaped_info.set_quantization_info(weights->quantization_info());
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100475 weights_to_use = &weights_reshaped_info;
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100476
Michalis Spyroue2503892018-04-23 15:17:31 +0100477 if(!skip_im2col)
478 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100479 // Create tensor info for im2col reshaped inputs
480 // For NEON the batch size is on the fourth dimension
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100481 // TODO (giaiod01): Auto-initialize the output shape of im2col COMPMID-1482
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100482 TensorShape shape_im2col = input->tensor_shape();
483 shape_im2col.set(0, mat_weights_rows);
484 shape_im2col.set(1, conv_w * conv_h);
485 shape_im2col.set(2, 1);
486
487 im2col_reshaped_info = TensorInfo(shape_im2col, 1, data_type);
488 im2col_reshaped_info.set_quantization_info(input->quantization_info());
489
Giorgio Arena0f170392018-07-18 16:13:12 +0100490 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 +0100491 gemm_input_to_use = &im2col_reshaped_info;
Michalis Spyroue2503892018-04-23 15:17:31 +0100492 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000493
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100494 // Create temporary GEMM output tensor in case we cannot skip col2im
495 if(!skip_col2im)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000496 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100497 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
498 shape_gemm.set(0, mat_weights_cols);
499 shape_gemm.set(1, conv_w * conv_h);
George Wort2d7e6832019-02-22 16:37:41 +0000500 info_gemm = TensorInfo(shape_gemm, 1, data_type);
Michalis Spyroue2503892018-04-23 15:17:31 +0100501 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000502 else
503 {
George Wort2d7e6832019-02-22 16:37:41 +0000504 info_gemm = TensorInfo(output->tensor_shape(), 1, data_type);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000505 }
506 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
507 gemm_output_to_use = &info_gemm;
George Wort2d7e6832019-02-22 16:37:41 +0000508 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, act_info, skip_col2im ? conv_h : 0, skip_im2col));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100509
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +0100510 // Validate Col2Im/ReshapeLayer
511 if(!skip_col2im && (data_layout == DataLayout::NCHW))
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100512 {
George Wort2d7e6832019-02-22 16:37:41 +0000513 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h)));
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100514 }
515
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000516 return Status{};
517}
518
519void NEGEMMConvolutionLayer::run()
520{
Georgios Pinitas72219332018-06-05 14:56:06 +0100521 prepare();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000522
Georgios Pinitasda953f22019-04-02 17:27:03 +0100523 MemoryGroupResourceScope scope_mg(_memory_group);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000524
Michalis Spyroue2503892018-04-23 15:17:31 +0100525 if(!_skip_im2col)
526 {
527 // Run input reshaping
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100528 unsigned int y_dim = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
529 NEScheduler::get().schedule(&_im2col_kernel, y_dim);
Michalis Spyroue2503892018-04-23 15:17:31 +0100530 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000531
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100532 // Runs NEGEMM or NEGEMMLowpMatrixMultiplyCore functions
533 if(_is_quantized)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000534 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100535 // Run gemmlowp
536 _mm_gemmlowp.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000537 }
538 else
539 {
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100540 // Run gemm
541 _mm_gemm.run();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000542 }
543
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000544 // Reshape output matrix
Georgios Pinitase413d252018-11-14 18:29:58 +0000545 if(!_skip_col2im)
Michalis Spyroue2503892018-04-23 15:17:31 +0100546 {
Georgios Pinitase413d252018-11-14 18:29:58 +0000547 if(_data_layout == DataLayout::NCHW)
548 {
549 NEScheduler::get().schedule(&_col2im_kernel, Window::DimY);
550 }
551 else
552 {
553 _reshape_layer.run();
554 }
Michalis Spyroue2503892018-04-23 15:17:31 +0100555 }
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000556}
Georgios Pinitas72219332018-06-05 14:56:06 +0100557
558void NEGEMMConvolutionLayer::prepare()
559{
560 if(!_is_prepared)
561 {
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100562 if(_weights_manager && _weights_manager->are_weights_managed(_original_weights))
563 {
564 _weights_manager->run(_original_weights, &_reshape_weights_managed);
565 }
566 else
567 {
568 // Run weights reshaping and mark original weights tensor as unused
569 _weights_reshaped.allocator()->allocate();
570 _reshape_weights.run();
571 _original_weights->mark_as_unused();
572 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100573
Gian Marco Iodice597a8562018-08-01 15:06:06 +0100574 // Prepare GEMM
575 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
Georgios Pinitas72219332018-06-05 14:56:06 +0100576 if(!_weights_reshaped.is_used())
577 {
578 _weights_reshaped.allocator()->free();
579 }
580
581 _is_prepared = true;
582 }
583}