blob: be6be047038049a4bb8318adaa360e54fcd33263 [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)
Jenkins975dfe12019-09-02 11:47:54 +010094 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _col2im_kernel(), _activationlayer_function(),
95 _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _fuse_activation(true), _is_prepared(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +000096{
97}
98
Jenkinsb9abeae2018-11-22 11:58:08 +000099void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Jenkins975dfe12019-09-02 11:47:54 +0100100 int gemm_3d_depth, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000101{
102 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Jenkins975dfe12019-09-02 11:47:54 +0100103 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, act_info));
Jenkinsb9abeae2018-11-22 11:58:08 +0000104
Jenkins975dfe12019-09-02 11:47:54 +0100105 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
106 false, // is_b_reshaped
107 true, // reshape_b_only_on_first_run
108 gemm_3d_depth, // depth_output_gemm3d
109 _skip_im2col, // reinterpret_input_as_3d
110 false, // retain_internal_weights
111 gemmlowp_output_stage, // gemmlowp_output_stage
112 false, // fp_mixed_precision
113 true, // broadcast_bias
114 act_info); // activation_info
Anthony Barbier06ea0482018-02-22 15:45:35 +0000115
116 if(_is_quantized)
117 {
118 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
119 // Extract and negate input and weights offset
120 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
121 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
122
Jenkins975dfe12019-09-02 11:47:54 +0100123 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
124 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000125
Jenkinsb9abeae2018-11-22 11:58:08 +0000126 _mm_gemmlowp.configure(input, weights, biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000127
128 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
129 input->info()->set_quantization_info(input_quantization_info);
130 weights->info()->set_quantization_info(weights_quantization_info);
131 }
132 else
133 {
134 // Configure matrix multiply function
Jenkins975dfe12019-09-02 11:47:54 +0100135 _mm_gemm.configure(input, weights, biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000136 }
137}
138
Jenkinsb9abeae2018-11-22 11:58:08 +0000139Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
Jenkins975dfe12019-09-02 11:47:54 +0100140 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000141{
142 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
143
Jenkins975dfe12019-09-02 11:47:54 +0100144 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
145 false, // is_b_reshaped
146 true, // reshape_b_only_on_first_run
147 gemm_3d_depth, // depth_output_gemm3d
148 skip_im2col, // reinterpret_input_as_3d
149 false, // retain_internal_weights
150 gemmlowp_output_stage, // gemmlowp_output_stage
151 false, // fp_mixed_precision
152 true, // broadcast_bias
153 act_info); // activation_info
Jenkinsb9abeae2018-11-22 11:58:08 +0000154
Anthony Barbier06ea0482018-02-22 15:45:35 +0000155 if(is_quantized)
156 {
157 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
158 // Extract and negate input and weights offset
159 const QuantizationInfo input_quantization_info = input->quantization_info();
160 const QuantizationInfo weights_quantization_info = weights->quantization_info();
161
162 std::unique_ptr<ITensorInfo> input_qa = input->clone();
163 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
Jenkins975dfe12019-09-02 11:47:54 +0100164 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
165 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000166
167 // Perform validation step on GEMMLowp
Jenkinsb9abeae2018-11-22 11:58:08 +0000168 return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000169 }
170 else
171 {
172 // Perform validation step on Matrix multiply function
Jenkins975dfe12019-09-02 11:47:54 +0100173 return CLGEMM::validate(input, weights, biases, output, 1.0f, 1.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000174 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000175}
176
Jenkinsb3a371b2018-05-23 11:36:53 +0100177void 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 +0000178 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000179{
180 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
181
182 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
183 weights->info(),
184 biases != nullptr ? biases->info() : nullptr,
185 output->info(),
186 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100187 weights_info,
188 dilation,
Jenkins52ba29e2018-08-29 15:32:11 +0000189 act_info,
190 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000191
Jenkins52ba29e2018-08-29 15:32:11 +0000192 const DataType data_type = input->info()->data_type();
193 const DataLayout data_layout = input->info()->data_layout();
194 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
195 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
196 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
197
198 const unsigned int kernel_width = weights->info()->dimension(idx_width);
199 const unsigned int kernel_height = weights->info()->dimension(idx_height);
200
Jenkins975dfe12019-09-02 11:47:54 +0100201 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
202 const UniformQuantizationInfo wq_info = weights->info()->quantization_info().uniform();
203 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
204
205 _is_prepared = weights_info.retain_internal_weights();
206 _original_weights = weights;
207 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
208 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
209 _skip_col2im = data_layout == DataLayout::NHWC;
210
211 // Only for quantize there are few cases where we cannot fuse the activation function in GEMM
212 _fuse_activation = true;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000213
214 // Set the GPU target for im2col and col2im
215 _im2col_kernel.set_target(CLScheduler::get().target());
216 _col2im_kernel.set_target(CLScheduler::get().target());
217
Jenkinsb9abeae2018-11-22 11:58:08 +0000218 const ICLTensor *gemm_input_to_use = input;
219 ICLTensor *gemm_output_to_use = output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000220
Anthony Barbier06ea0482018-02-22 15:45:35 +0000221 // Get parameters from conv_info
222 unsigned int stride_x = 0;
223 unsigned int stride_y = 0;
224 std::tie(stride_x, stride_y) = conv_info.stride();
225
226 // Get convolved dimensions
227 unsigned int conv_w = 0;
228 unsigned int conv_h = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000229 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
230 input->info()->dimension(idx_height),
231 kernel_width,
232 kernel_height,
233 conv_info,
234 dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000235
Jenkins52ba29e2018-08-29 15:32:11 +0000236 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels) / num_groups;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000237
Jenkins975dfe12019-09-02 11:47:54 +0100238 const ICLTensor *biases_to_use = biases;
239 bool append_bias = false;
240
241 if(num_groups != 1 && biases != nullptr)
242 {
243 // num_groups != 1 can only be for NCHW
244 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
245 biases_to_use = nullptr;
246 append_bias = true;
247
248 _reshape_weights.configure(weights, biases, &_weights_reshaped, num_groups);
249 }
250 else
251 {
252 _reshape_weights.configure(weights, nullptr, &_weights_reshaped, num_groups);
253 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000254
255 // Create tensor to store im2col reshaped inputs
Jenkins52ba29e2018-08-29 15:32:11 +0000256 if(!_skip_im2col)
257 {
258 _memory_group.manage(&_im2col_output);
259
260 // Configure and tune im2col. im2col output shape is auto-initialized
Jenkins975dfe12019-09-02 11:47:54 +0100261 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation, num_groups);
Jenkins52ba29e2018-08-29 15:32:11 +0000262
263 // Set quantization info
264 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
265 CLScheduler::get().tune_kernel_static(_im2col_kernel);
266
267 // Update GEMM input
268 gemm_input_to_use = &_im2col_output;
269 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000270
271 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000272 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000273 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000274 TensorShape shape_gemm;
275
276 // If we cannot skip col2im it means we run im2col as well
277 shape_gemm = _im2col_output.info()->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000278 shape_gemm.set(0, mat_weights_cols);
279 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000280
Jenkins4ba87db2019-05-23 17:11:51 +0100281 // TODO(COMPMID-2078): input->clone() doesn't work with subtensors for grouped convolutions.
Jenkinsb9abeae2018-11-22 11:58:08 +0000282 TensorInfo info_gemm(shape_gemm, 1, data_type);
283 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000284 _gemm_output.allocator()->init(info_gemm);
285 _memory_group.manage(&_gemm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000286
Jenkins52ba29e2018-08-29 15:32:11 +0000287 // Update GEMM output
288 gemm_output_to_use = &_gemm_output;
289 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000290
Jenkinsb9abeae2018-11-22 11:58:08 +0000291 GEMMLowpOutputStageInfo gemmlowp_output_stage;
292 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
293 gemmlowp_output_stage.gemmlowp_offset = 0;
294 gemmlowp_output_stage.gemmlowp_multiplier = 0;
295 gemmlowp_output_stage.gemmlowp_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000296
297 // Configure output stage for quantized case
298 if(_is_quantized)
299 {
Jenkins975dfe12019-09-02 11:47:54 +0100300 const auto output_quant_info = (output->info()->total_size() == 0) ? iq_info : oq_info;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000301
Jenkins975dfe12019-09-02 11:47:54 +0100302 const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale;
Jenkins514be652019-02-28 12:25:18 +0000303 int output_multiplier = 0;
304 int output_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000305 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Jenkins52ba29e2018-08-29 15:32:11 +0000306
Jenkinsb9abeae2018-11-22 11:58:08 +0000307 int min_activation = 0;
308 int max_activation = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000309
Jenkinsb9abeae2018-11-22 11:58:08 +0000310 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
311 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
312 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
313 };
314
Jenkins975dfe12019-09-02 11:47:54 +0100315 if(act_info.enabled())
Jenkinsb9abeae2018-11-22 11:58:08 +0000316 {
Jenkins975dfe12019-09-02 11:47:54 +0100317 if(supported_acts.count(act_info.activation()) != 0)
318 {
319 const int a_const_int = quantize_qasymm8(act_info.a(), output_quant_info);
320 const int b_const_int = quantize_qasymm8(act_info.b(), output_quant_info);
Jenkinsb9abeae2018-11-22 11:58:08 +0000321
Jenkins975dfe12019-09-02 11:47:54 +0100322 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
323 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
324 }
325 else
326 {
327 _fuse_activation = false;
328 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000329 }
330
331 // Set the GEMMLowp output stage info
332 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
333 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
334 gemmlowp_output_stage.gemmlowp_shift = output_shift;
335 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
336 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000337 }
338
Jenkinsb9abeae2018-11-22 11:58:08 +0000339 // Configure and tune GEMM
340 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
341 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
342
Jenkins975dfe12019-09-02 11:47:54 +0100343 configure_mm(gemm_input_to_use, &_weights_reshaped, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, act_info);
Jenkinsb9abeae2018-11-22 11:58:08 +0000344
345 if(!_skip_im2col)
Jenkins52ba29e2018-08-29 15:32:11 +0000346 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000347 _im2col_output.allocator()->allocate();
Jenkins52ba29e2018-08-29 15:32:11 +0000348 }
349
Jenkinsb9abeae2018-11-22 11:58:08 +0000350 if(!_skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000351 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000352 // Configure and tune Col2Im
353 _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups);
354 CLScheduler::get().tune_kernel_static(_col2im_kernel);
355 }
356
357 if(!_skip_col2im)
358 {
Jenkins52ba29e2018-08-29 15:32:11 +0000359 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000360 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000361
Jenkins52ba29e2018-08-29 15:32:11 +0000362 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
363 "Output shape does not match the expected one");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000364
Jenkins975dfe12019-09-02 11:47:54 +0100365 if(!_fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100366 {
367 _activationlayer_function.configure(output, nullptr, act_info);
368 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000369
370 ARM_COMPUTE_UNUSED(weights_info);
371}
372
373Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000374 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000375{
376 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
377 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Jenkins52ba29e2018-08-29 15:32:11 +0000378 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 +0000379 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000380 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
381 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");
382 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
383 ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW));
384
385 const DataLayout data_layout = input->data_layout();
386 const DataType data_type = input->data_type();
387 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
388 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
389 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
390 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
391
392 const unsigned int kernel_width = weights->dimension(idx_width);
393 const unsigned int kernel_height = weights->dimension(idx_height);
394
Jenkins4ba87db2019-05-23 17:11:51 +0100395 TensorInfo im2col_reshaped_info{};
396 TensorInfo info_gemm{};
397 TensorInfo weights_reshaped_info{};
Jenkinsb9abeae2018-11-22 11:58:08 +0000398 const ITensorInfo *gemm_input_to_use = input;
399 const ITensorInfo *gemm_output_to_use = output;
400 const ITensorInfo *weights_to_use = weights;
Jenkins52ba29e2018-08-29 15:32:11 +0000401
Jenkins975dfe12019-09-02 11:47:54 +0100402 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
403 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
404 const bool skip_col2im = data_layout == DataLayout::NHWC;
405 bool fuse_activation = true;
406
407 const UniformQuantizationInfo iq_info = input->quantization_info().uniform();
408 const UniformQuantizationInfo wq_info = weights->quantization_info().uniform();
409 const UniformQuantizationInfo oq_info = output->quantization_info().uniform();
Jenkins52ba29e2018-08-29 15:32:11 +0000410
411 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000412 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
413
Jenkins52ba29e2018-08-29 15:32:11 +0000414 // Validate biases
Anthony Barbier06ea0482018-02-22 15:45:35 +0000415 if(biases != nullptr)
416 {
417 if(is_quantized)
418 {
419 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
420 }
421 else
422 {
423 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
424 }
Jenkins52ba29e2018-08-29 15:32:11 +0000425 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000426 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
427 }
428
Jenkins52ba29e2018-08-29 15:32:11 +0000429 if(act_info.enabled())
430 {
431 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
432 }
433
434 // Get convolved dimensions
435 unsigned int conv_w = 0;
436 unsigned int conv_h = 0;
437
438 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
439 input->dimension(idx_height),
440 kernel_width,
441 kernel_height,
442 conv_info,
443 dilation);
444
445 unsigned int mat_weights_cols = weights->dimension(idx_kernels) / num_groups;
446
Jenkins975dfe12019-09-02 11:47:54 +0100447 const ITensorInfo *biases_to_use = biases;
448 bool append_bias = false;
449
450 if(num_groups != 1 && biases != nullptr)
451 {
452 // num_groups != 1 can only be for NCHW
453 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
454 biases_to_use = nullptr;
455 append_bias = true;
456
457 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, biases, nullptr, num_groups));
458 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, true, num_groups), 1, data_type);
459 }
460 else
461 {
462 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, nullptr, nullptr, num_groups));
463 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, false, num_groups), 1, data_type);
464 }
465
466 weights_to_use = &weights_reshaped_info;
Jenkins52ba29e2018-08-29 15:32:11 +0000467
468 if(!skip_im2col)
469 {
470 const Size2D kernel_dims(kernel_width, kernel_height);
471
472 // Output tensor auto initialization if not yet initialized
473 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups);
474
475 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
476
477 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups));
478 gemm_input_to_use = &im2col_reshaped_info;
479 }
Jenkins52ba29e2018-08-29 15:32:11 +0000480
481 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000482 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000483 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000484 TensorShape shape_gemm;
485
486 shape_gemm = gemm_input_to_use->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000487 shape_gemm.set(0, mat_weights_cols);
488 shape_gemm.set(1, conv_w * conv_h);
Jenkinsb9abeae2018-11-22 11:58:08 +0000489
490 info_gemm = TensorInfo(shape_gemm, 1, data_type);
491 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000492 gemm_output_to_use = &info_gemm;
493 }
494
Jenkinsb9abeae2018-11-22 11:58:08 +0000495 GEMMLowpOutputStageInfo gemmlowp_output_stage;
496 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
497 gemmlowp_output_stage.gemmlowp_offset = 0;
498 gemmlowp_output_stage.gemmlowp_multiplier = 0;
499 gemmlowp_output_stage.gemmlowp_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000500
501 if(is_quantized)
502 {
Jenkins975dfe12019-09-02 11:47:54 +0100503 const auto output_quant_info = (output->total_size() == 0) ? iq_info : oq_info;
Jenkins52ba29e2018-08-29 15:32:11 +0000504
Jenkins975dfe12019-09-02 11:47:54 +0100505 const float multiplier = (iq_info.scale * wq_info.scale) / output_quant_info.scale;
Jenkins514be652019-02-28 12:25:18 +0000506 int output_multiplier = 0;
507 int output_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000508
Jenkinsb9abeae2018-11-22 11:58:08 +0000509 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
510
511 int min_activation = 0;
512 int max_activation = 0;
513
514 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
515 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
516 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
517 };
518
Jenkins975dfe12019-09-02 11:47:54 +0100519 if(act_info.enabled())
Jenkinsb9abeae2018-11-22 11:58:08 +0000520 {
Jenkins975dfe12019-09-02 11:47:54 +0100521 if(supported_acts.count(act_info.activation()) != 0)
522 {
523 const int a_const_int = quantize_qasymm8(act_info.a(), output_quant_info);
524 const int b_const_int = quantize_qasymm8(act_info.b(), output_quant_info);
Jenkinsb9abeae2018-11-22 11:58:08 +0000525
Jenkins975dfe12019-09-02 11:47:54 +0100526 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
527 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
528 }
529 else
530 {
531 fuse_activation = false;
532 }
Jenkinsb9abeae2018-11-22 11:58:08 +0000533 }
534
535 // Set the GEMMLowp output stage info
536 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
537 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
538 gemmlowp_output_stage.gemmlowp_shift = output_shift;
539 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
540 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Jenkins52ba29e2018-08-29 15:32:11 +0000541 }
542
Jenkinsb9abeae2018-11-22 11:58:08 +0000543 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
544 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
545
Jenkins975dfe12019-09-02 11:47:54 +0100546 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, skip_im2col, act_info));
Jenkinsb9abeae2018-11-22 11:58:08 +0000547
Jenkins52ba29e2018-08-29 15:32:11 +0000548 // Validate Col2Im
Jenkinsb9abeae2018-11-22 11:58:08 +0000549 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000550 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000551 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 +0000552 }
553
Jenkinsb3a371b2018-05-23 11:36:53 +0100554 //Validate Activation Layer
Jenkins975dfe12019-09-02 11:47:54 +0100555 if(!fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100556 {
557 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
558 }
559
Anthony Barbier06ea0482018-02-22 15:45:35 +0000560 return Status{};
561}
562
563void CLGEMMConvolutionLayer::run()
564{
Jenkinsb3a371b2018-05-23 11:36:53 +0100565 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000566
Jenkins4ba87db2019-05-23 17:11:51 +0100567 MemoryGroupResourceScope scope_mg(_memory_group);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000568
569 // Run im2col
Jenkins52ba29e2018-08-29 15:32:11 +0000570 if(!_skip_im2col)
571 {
572 CLScheduler::get().enqueue(_im2col_kernel);
573 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000574
575 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
576 if(_is_quantized)
577 {
578 // Run gemmlowp
579 _mm_gemmlowp.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000580 }
581 else
582 {
583 // Run gemm
584 _mm_gemm.run();
585 }
586
587 // Reshape output matrix
Jenkinsb9abeae2018-11-22 11:58:08 +0000588 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000589 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000590 CLScheduler::get().enqueue(_col2im_kernel, false);
Jenkins52ba29e2018-08-29 15:32:11 +0000591 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000592
Jenkins975dfe12019-09-02 11:47:54 +0100593 //Run Activation Layer if we cannot fuse in GEMM
594 if(!_fuse_activation)
Jenkinsb3a371b2018-05-23 11:36:53 +0100595 {
596 _activationlayer_function.run();
597 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000598}
Jenkinsb3a371b2018-05-23 11:36:53 +0100599
600void CLGEMMConvolutionLayer::prepare()
601{
602 if(!_is_prepared)
603 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100604 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins52ba29e2018-08-29 15:32:11 +0000605
606 // Run weights reshaping and mark original weights tensor as unused
Jenkinsb3a371b2018-05-23 11:36:53 +0100607 _weights_reshaped.allocator()->allocate();
608 _reshape_weights.run();
609 _original_weights->mark_as_unused();
610
Jenkins52ba29e2018-08-29 15:32:11 +0000611 // Prepare GEMM
612 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
613 if(!_weights_reshaped.is_used())
Jenkinsb3a371b2018-05-23 11:36:53 +0100614 {
Jenkins52ba29e2018-08-29 15:32:11 +0000615 _weights_reshaped.allocator()->free();
Jenkinsb3a371b2018-05-23 11:36:53 +0100616 }
617
618 CLScheduler::get().queue().finish();
619 _is_prepared = true;
620 }
621}