blob: 7105e85061173dec7876751eb73b9801a410622d [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier06ea0482018-02-22 15:45:35 +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/CL/functions/CLGEMMConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Size2D.h"
28#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
33
34#include <cmath>
35#include <memory>
36#include <tuple>
37
38using namespace arm_compute;
39using namespace arm_compute::misc::shape_calculator;
40
Jenkinsb3a371b2018-05-23 11:36:53 +010041CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
42 : _weights_reshape_kernel()
Anthony Barbier06ea0482018-02-22 15:45:35 +000043{
44}
45
Jenkins52ba29e2018-08-29 15:32:11 +000046void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +000047{
48 // Perform validation step
49 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
50 ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayerReshapeWeights::validate(weights->info(),
51 (biases != nullptr) ? biases->info() : nullptr,
Jenkins52ba29e2018-08-29 15:32:11 +000052 output->info(),
53 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +000054
55 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
56 const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr;
57
Jenkins52ba29e2018-08-29 15:32:11 +000058 _weights_reshape_kernel.configure(weights, biases_to_use, output, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +000059
60 output->info()->set_quantization_info(weights->info()->quantization_info());
61}
62
Jenkins52ba29e2018-08-29 15:32:11 +000063Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +000064{
65 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
Jenkins52ba29e2018-08-29 15:32:11 +000066 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier06ea0482018-02-22 15:45:35 +000067 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
68
69 if(biases != nullptr)
70 {
Jenkins52ba29e2018-08-29 15:32:11 +000071 const int idx_kernels = get_data_layout_dimension_index(weights->data_layout(), DataLayoutDimension::BATCHES);
Anthony Barbier06ea0482018-02-22 15:45:35 +000072 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
Jenkins52ba29e2018-08-29 15:32:11 +000074 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +000075 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
76 }
77
78 if((output != nullptr) && (output->total_size() != 0))
79 {
80 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
Anthony Barbier06ea0482018-02-22 15:45:35 +000081
Jenkins52ba29e2018-08-29 15:32:11 +000082 CLWeightsReshapeKernel::validate(weights, biases, output, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +000083 }
84
85 return Status{};
86}
87
88void CLConvolutionLayerReshapeWeights::run()
89{
Anthony Barbier06ea0482018-02-22 15:45:35 +000090 CLScheduler::get().enqueue(_weights_reshape_kernel);
Anthony Barbier06ea0482018-02-22 15:45:35 +000091}
92
93CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Jenkinsb9abeae2018-11-22 11:58:08 +000094 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _col2im_kernel(), _activationlayer_function(), _add_bias_kernel(),
95 _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _data_layout(DataLayout::NCHW), _append_bias(false), _skip_im2col(false), _skip_col2im(false), _is_quantized(false),
Jenkins514be652019-02-28 12:25:18 +000096 _is_activationlayer_enabled(false), _is_prepared(false), _run_addition(true)
Anthony Barbier06ea0482018-02-22 15:45:35 +000097{
98}
99
Jenkinsb9abeae2018-11-22 11:58:08 +0000100void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
101 int gemm_3d_depth)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000102{
103 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Jenkins514be652019-02-28 12:25:18 +0000104 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), gemmlowp_output_stage, gemm_3d_depth, _skip_im2col,
105 _run_addition));
Jenkinsb9abeae2018-11-22 11:58:08 +0000106
107 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
108 gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
109 false, gemmlowp_output_stage);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000110
111 if(_is_quantized)
112 {
113 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
114 // Extract and negate input and weights offset
115 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
116 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
117
118 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
119 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
120
Jenkinsb9abeae2018-11-22 11:58:08 +0000121 _mm_gemmlowp.configure(input, weights, biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000122
123 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
124 input->info()->set_quantization_info(input_quantization_info);
125 weights->info()->set_quantization_info(weights_quantization_info);
126 }
127 else
128 {
Jenkins514be652019-02-28 12:25:18 +0000129 // Bias does not need to be added in GEMM if im2col is being used or the Matrix Addition kernel needs to be run
130 const bool skip_bias_in_gemm = _run_addition || !_skip_im2col;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000131 // Configure matrix multiply function
Jenkins514be652019-02-28 12:25:18 +0000132 _mm_gemm.configure(input, weights, (skip_bias_in_gemm) ? nullptr : biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000133 }
134}
135
Jenkinsb9abeae2018-11-22 11:58:08 +0000136Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Jenkins514be652019-02-28 12:25:18 +0000137 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, bool run_addition)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000138{
139 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
140
Jenkinsb9abeae2018-11-22 11:58:08 +0000141 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
142 gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
143 false, gemmlowp_output_stage);
144
Anthony Barbier06ea0482018-02-22 15:45:35 +0000145 if(is_quantized)
146 {
147 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
148 // Extract and negate input and weights offset
149 const QuantizationInfo input_quantization_info = input->quantization_info();
150 const QuantizationInfo weights_quantization_info = weights->quantization_info();
151
152 std::unique_ptr<ITensorInfo> input_qa = input->clone();
153 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
154 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
155 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
156
157 // Perform validation step on GEMMLowp
Jenkinsb9abeae2018-11-22 11:58:08 +0000158 return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000159 }
160 else
161 {
Jenkins514be652019-02-28 12:25:18 +0000162 // Bias does not need to be added in GEMM if im2col is being used or the Matrix Addition kernel needs to be run
163 const bool skip_bias_in_gemm = run_addition || !skip_im2col;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000164 // Perform validation step on Matrix multiply function
Jenkins514be652019-02-28 12:25:18 +0000165 return CLGEMM::validate(input, weights, (skip_bias_in_gemm) ? nullptr : biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000166 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000167}
168
Jenkinsb3a371b2018-05-23 11:36:53 +0100169void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000170 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000171{
172 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
173
174 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
175 weights->info(),
176 biases != nullptr ? biases->info() : nullptr,
177 output->info(),
178 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100179 weights_info,
180 dilation,
Jenkins52ba29e2018-08-29 15:32:11 +0000181 act_info,
182 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000183
Jenkins52ba29e2018-08-29 15:32:11 +0000184 const DataType data_type = input->info()->data_type();
185 const DataLayout data_layout = input->info()->data_layout();
186 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
187 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
188 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
189
190 const unsigned int kernel_width = weights->info()->dimension(idx_width);
191 const unsigned int kernel_height = weights->info()->dimension(idx_height);
192
Jenkinsb9abeae2018-11-22 11:58:08 +0000193 _is_prepared = weights_info.retain_internal_weights();
194 _original_weights = weights;
195 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
196 _data_layout = data_layout;
197 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
198 _skip_col2im = data_layout == DataLayout::NHWC;
199 _append_bias = (biases != nullptr) && (!_is_quantized);
200 _is_activationlayer_enabled = act_info.enabled();
Jenkins514be652019-02-28 12:25:18 +0000201 // In case of F16, fused bias will be used in GEMM
202 _run_addition = (_skip_im2col) && (_append_bias) && (data_type != DataType::F16);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000203
204 // Set the GPU target for im2col and col2im
205 _im2col_kernel.set_target(CLScheduler::get().target());
206 _col2im_kernel.set_target(CLScheduler::get().target());
207
Jenkinsb9abeae2018-11-22 11:58:08 +0000208 const ICLTensor *gemm_input_to_use = input;
209 ICLTensor *gemm_output_to_use = output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000210
Jenkins52ba29e2018-08-29 15:32:11 +0000211 const ICLTensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000212
213 // Get parameters from conv_info
214 unsigned int stride_x = 0;
215 unsigned int stride_y = 0;
216 std::tie(stride_x, stride_y) = conv_info.stride();
217
218 // Get convolved dimensions
219 unsigned int conv_w = 0;
220 unsigned int conv_h = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000221 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
222 input->info()->dimension(idx_height),
223 kernel_width,
224 kernel_height,
225 conv_info,
226 dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000227
Jenkins52ba29e2018-08-29 15:32:11 +0000228 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels) / num_groups;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000229
230 // _weights_reshaped will be auto configured in the kernel.
231 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
Jenkins52ba29e2018-08-29 15:32:11 +0000232 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000233
234 // Create tensor to store im2col reshaped inputs
Jenkins52ba29e2018-08-29 15:32:11 +0000235 if(!_skip_im2col)
236 {
237 _memory_group.manage(&_im2col_output);
238
239 // Configure and tune im2col. im2col output shape is auto-initialized
240 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation, num_groups);
241
242 // Set quantization info
243 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
244 CLScheduler::get().tune_kernel_static(_im2col_kernel);
245
246 // Update GEMM input
247 gemm_input_to_use = &_im2col_output;
248 }
249 else if(_append_bias)
250 {
251 // Configure add bias kernel
Jenkins514be652019-02-28 12:25:18 +0000252 _add_bias_kernel.configure(ArithmeticOperation::ADD, output, biases, output, ConvertPolicy::SATURATE);
Jenkins52ba29e2018-08-29 15:32:11 +0000253 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000254
255 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000256 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000257 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000258 TensorShape shape_gemm;
259
260 // If we cannot skip col2im it means we run im2col as well
261 shape_gemm = _im2col_output.info()->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000262 shape_gemm.set(0, mat_weights_cols);
263 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000264
Jenkinsb9abeae2018-11-22 11:58:08 +0000265 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
266 TensorInfo info_gemm(shape_gemm, 1, data_type);
267 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000268 _gemm_output.allocator()->init(info_gemm);
269 _memory_group.manage(&_gemm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000270
Jenkins52ba29e2018-08-29 15:32:11 +0000271 // Update GEMM output
272 gemm_output_to_use = &_gemm_output;
273 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000274
Jenkinsb9abeae2018-11-22 11:58:08 +0000275 GEMMLowpOutputStageInfo gemmlowp_output_stage;
276 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
277 gemmlowp_output_stage.gemmlowp_offset = 0;
278 gemmlowp_output_stage.gemmlowp_multiplier = 0;
279 gemmlowp_output_stage.gemmlowp_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000280
281 // Configure output stage for quantized case
282 if(_is_quantized)
283 {
284 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
285
Jenkins514be652019-02-28 12:25:18 +0000286 const float multiplier = (input->info()->quantization_info().scale * weights->info()->quantization_info().scale) / output_quant_info.scale;
287 int output_multiplier = 0;
288 int output_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000289 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Jenkins52ba29e2018-08-29 15:32:11 +0000290
Jenkinsb9abeae2018-11-22 11:58:08 +0000291 int min_activation = 0;
292 int max_activation = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000293
Jenkinsb9abeae2018-11-22 11:58:08 +0000294 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
295 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
296 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
297 };
298
299 if(_is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
300 {
301 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
302 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
303
304 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
305 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
306
307 // If the activation layer is RELU, BOUNDED_RELU or LU_BOUNDED_RELU, we can use the GEMMLowp output stage to perform this operation
308 _is_activationlayer_enabled = false;
309 }
310
311 // Set the GEMMLowp output stage info
312 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
313 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
314 gemmlowp_output_stage.gemmlowp_shift = output_shift;
315 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
316 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000317 }
318
Jenkinsb9abeae2018-11-22 11:58:08 +0000319 // Configure and tune GEMM
320 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
321 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
322
323 configure_mm(gemm_input_to_use, &_weights_reshaped, biases, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth);
324
325 if(!_skip_im2col)
Jenkins52ba29e2018-08-29 15:32:11 +0000326 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000327 _im2col_output.allocator()->allocate();
Jenkins52ba29e2018-08-29 15:32:11 +0000328 }
329
Jenkinsb9abeae2018-11-22 11:58:08 +0000330 if(!_skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000331 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000332 // Configure and tune Col2Im
333 _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups);
334 CLScheduler::get().tune_kernel_static(_col2im_kernel);
335 }
336
337 if(!_skip_col2im)
338 {
Jenkins52ba29e2018-08-29 15:32:11 +0000339 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000340 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000341
Jenkins52ba29e2018-08-29 15:32:11 +0000342 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
343 "Output shape does not match the expected one");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000344
Jenkinsb3a371b2018-05-23 11:36:53 +0100345 if(_is_activationlayer_enabled)
346 {
347 _activationlayer_function.configure(output, nullptr, act_info);
348 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000349
350 ARM_COMPUTE_UNUSED(weights_info);
351}
352
353Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000354 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000355{
356 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
357 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Jenkins52ba29e2018-08-29 15:32:11 +0000358 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000359 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000360 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
361 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported");
362 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
363 ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW));
364
365 const DataLayout data_layout = input->data_layout();
366 const DataType data_type = input->data_type();
367 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
368 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
369 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
370 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
371
372 const unsigned int kernel_width = weights->dimension(idx_width);
373 const unsigned int kernel_height = weights->dimension(idx_height);
374
Jenkinsb9abeae2018-11-22 11:58:08 +0000375 TensorInfo im2col_reshaped_info, info_gemm, weights_reshaped_info;
376 const ITensorInfo *gemm_input_to_use = input;
377 const ITensorInfo *gemm_output_to_use = output;
378 const ITensorInfo *weights_to_use = weights;
Jenkins52ba29e2018-08-29 15:32:11 +0000379
Jenkinsb9abeae2018-11-22 11:58:08 +0000380 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
381 const bool append_bias = (biases != nullptr) && (!is_quantized);
382 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
383 const bool skip_col2im = data_layout == DataLayout::NHWC;
384 bool is_activationlayer_enabled = act_info.enabled();
Jenkins514be652019-02-28 12:25:18 +0000385 // In case of F16, fused bias will be used in GEMM
386 const bool run_addition = (skip_im2col) && (append_bias) && (data_type != DataType::F16);
Jenkins52ba29e2018-08-29 15:32:11 +0000387
388 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000389 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
390
Jenkins52ba29e2018-08-29 15:32:11 +0000391 // Validate biases
Anthony Barbier06ea0482018-02-22 15:45:35 +0000392 if(biases != nullptr)
393 {
394 if(is_quantized)
395 {
396 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
397 }
398 else
399 {
400 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
401 }
Jenkins52ba29e2018-08-29 15:32:11 +0000402 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000403 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
404 }
405
Jenkins52ba29e2018-08-29 15:32:11 +0000406 if(act_info.enabled())
407 {
408 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
409 }
410
411 // Get convolved dimensions
412 unsigned int conv_w = 0;
413 unsigned int conv_h = 0;
414
415 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
416 input->dimension(idx_height),
417 kernel_width,
418 kernel_height,
419 conv_info,
420 dilation);
421
422 unsigned int mat_weights_cols = weights->dimension(idx_kernels) / num_groups;
423
424 // Output tensor auto inizialitation if not yet initialized
425 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr, num_groups));
426 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col), num_groups), 1, data_type);
427 weights_to_use = &weights_reshaped_info;
428
429 if(!skip_im2col)
430 {
431 const Size2D kernel_dims(kernel_width, kernel_height);
432
433 // Output tensor auto initialization if not yet initialized
434 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups);
435
436 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
437
438 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups));
439 gemm_input_to_use = &im2col_reshaped_info;
440 }
Jenkins514be652019-02-28 12:25:18 +0000441 else if(run_addition)
Jenkins52ba29e2018-08-29 15:32:11 +0000442 {
443 // Validate add bias kernel
Jenkins514be652019-02-28 12:25:18 +0000444 ARM_COMPUTE_RETURN_ON_ERROR(CLSaturatedArithmeticOperationKernel::validate(ArithmeticOperation::ADD, output, biases, output, ConvertPolicy::SATURATE));
Jenkins52ba29e2018-08-29 15:32:11 +0000445 }
446
447 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000448 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000449 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000450 TensorShape shape_gemm;
451
452 shape_gemm = gemm_input_to_use->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000453 shape_gemm.set(0, mat_weights_cols);
454 shape_gemm.set(1, conv_w * conv_h);
Jenkinsb9abeae2018-11-22 11:58:08 +0000455
456 info_gemm = TensorInfo(shape_gemm, 1, data_type);
457 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000458 gemm_output_to_use = &info_gemm;
459 }
460
Jenkinsb9abeae2018-11-22 11:58:08 +0000461 GEMMLowpOutputStageInfo gemmlowp_output_stage;
462 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
463 gemmlowp_output_stage.gemmlowp_offset = 0;
464 gemmlowp_output_stage.gemmlowp_multiplier = 0;
465 gemmlowp_output_stage.gemmlowp_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000466
467 if(is_quantized)
468 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000469 const QuantizationInfo output_quant_info = (output->total_size() == 0) ? input->quantization_info() : output->quantization_info();
Jenkins52ba29e2018-08-29 15:32:11 +0000470
Jenkins514be652019-02-28 12:25:18 +0000471 const float multiplier = (input->quantization_info().scale * weights->quantization_info().scale) / output_quant_info.scale;
472 int output_multiplier = 0;
473 int output_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000474
Jenkinsb9abeae2018-11-22 11:58:08 +0000475 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
476
477 int min_activation = 0;
478 int max_activation = 0;
479
480 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
481 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
482 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
483 };
484
485 if(is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
486 {
487 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
488 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
489
490 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
491 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
492
493 // If the activation layer is RELU, BOUNDED_RELU or LU_BOUNDED_RELU, we can use the GEMMLowp output stage to perform this operation
494 is_activationlayer_enabled = false;
495 }
496
497 // Set the GEMMLowp output stage info
498 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
499 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
500 gemmlowp_output_stage.gemmlowp_shift = output_shift;
501 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
502 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Jenkins52ba29e2018-08-29 15:32:11 +0000503 }
504
Jenkinsb9abeae2018-11-22 11:58:08 +0000505 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
506 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
507
Jenkins514be652019-02-28 12:25:18 +0000508 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, skip_im2col, run_addition));
Jenkinsb9abeae2018-11-22 11:58:08 +0000509
Jenkins52ba29e2018-08-29 15:32:11 +0000510 // Validate Col2Im
Jenkinsb9abeae2018-11-22 11:58:08 +0000511 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000512 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000513 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups));
Jenkins52ba29e2018-08-29 15:32:11 +0000514 }
515
Jenkinsb3a371b2018-05-23 11:36:53 +0100516 //Validate Activation Layer
Jenkinsb9abeae2018-11-22 11:58:08 +0000517 if(is_activationlayer_enabled)
Jenkinsb3a371b2018-05-23 11:36:53 +0100518 {
519 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
520 }
521
Anthony Barbier06ea0482018-02-22 15:45:35 +0000522 return Status{};
523}
524
525void CLGEMMConvolutionLayer::run()
526{
Jenkinsb3a371b2018-05-23 11:36:53 +0100527 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000528
529 _memory_group.acquire();
530
531 // Run im2col
Jenkins52ba29e2018-08-29 15:32:11 +0000532 if(!_skip_im2col)
533 {
534 CLScheduler::get().enqueue(_im2col_kernel);
535 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000536
537 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
538 if(_is_quantized)
539 {
540 // Run gemmlowp
541 _mm_gemmlowp.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000542 }
543 else
544 {
545 // Run gemm
546 _mm_gemm.run();
547 }
548
Jenkins514be652019-02-28 12:25:18 +0000549 if(_run_addition)
Jenkins52ba29e2018-08-29 15:32:11 +0000550 {
551 CLScheduler::get().enqueue(_add_bias_kernel);
552 }
553
Anthony Barbier06ea0482018-02-22 15:45:35 +0000554 // Reshape output matrix
Jenkinsb9abeae2018-11-22 11:58:08 +0000555 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000556 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000557 CLScheduler::get().enqueue(_col2im_kernel, false);
Jenkins52ba29e2018-08-29 15:32:11 +0000558 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000559
Jenkinsb3a371b2018-05-23 11:36:53 +0100560 //Run Activation Layer if enabled
561 if(_is_activationlayer_enabled)
562 {
563 _activationlayer_function.run();
564 }
565
Anthony Barbier06ea0482018-02-22 15:45:35 +0000566 _memory_group.release();
567}
Jenkinsb3a371b2018-05-23 11:36:53 +0100568
569void CLGEMMConvolutionLayer::prepare()
570{
571 if(!_is_prepared)
572 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100573 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins52ba29e2018-08-29 15:32:11 +0000574
575 // Run weights reshaping and mark original weights tensor as unused
Jenkinsb3a371b2018-05-23 11:36:53 +0100576 _weights_reshaped.allocator()->allocate();
577 _reshape_weights.run();
578 _original_weights->mark_as_unused();
579
Jenkins52ba29e2018-08-29 15:32:11 +0000580 // Prepare GEMM
581 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
582 if(!_weights_reshaped.is_used())
Jenkinsb3a371b2018-05-23 11:36:53 +0100583 {
Jenkins52ba29e2018-08-29 15:32:11 +0000584 _weights_reshaped.allocator()->free();
Jenkinsb3a371b2018-05-23 11:36:53 +0100585 }
586
587 CLScheduler::get().queue().finish();
588 _is_prepared = true;
589 }
590}