blob: 4694aa7f37527e31d68f21a01a886c4c1069a250 [file] [log] [blame]
Anthony Barbier06ea0482018-02-22 15:45:35 +00001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/runtime/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),
96 _is_activationlayer_enabled(false), _is_prepared(false)
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);
Jenkinsb9abeae2018-11-22 11:58:08 +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
106 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
107 gemm_3d_depth, _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
108 false, gemmlowp_output_stage);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109
110 if(_is_quantized)
111 {
112 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
113 // Extract and negate input and weights offset
114 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
115 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
116
117 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
118 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
119
Jenkinsb9abeae2018-11-22 11:58:08 +0000120 _mm_gemmlowp.configure(input, weights, biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000121
122 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
123 input->info()->set_quantization_info(input_quantization_info);
124 weights->info()->set_quantization_info(weights_quantization_info);
125 }
126 else
127 {
128 // Configure matrix multiply function
Jenkinsb9abeae2018-11-22 11:58:08 +0000129 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000130 }
131}
132
Jenkinsb9abeae2018-11-22 11:58:08 +0000133Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
134 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000135{
136 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
137
Jenkinsb9abeae2018-11-22 11:58:08 +0000138 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */,
139 gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */,
140 false, gemmlowp_output_stage);
141
Anthony Barbier06ea0482018-02-22 15:45:35 +0000142 if(is_quantized)
143 {
144 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
145 // Extract and negate input and weights offset
146 const QuantizationInfo input_quantization_info = input->quantization_info();
147 const QuantizationInfo weights_quantization_info = weights->quantization_info();
148
149 std::unique_ptr<ITensorInfo> input_qa = input->clone();
150 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
151 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
152 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
153
154 // Perform validation step on GEMMLowp
Jenkinsb9abeae2018-11-22 11:58:08 +0000155 return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), biases, output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000156 }
157 else
158 {
159 // Perform validation step on Matrix multiply function
Jenkins52ba29e2018-08-29 15:32:11 +0000160 return CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000161 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000162}
163
Jenkinsb3a371b2018-05-23 11:36:53 +0100164void 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 +0000165 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000166{
167 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
168
169 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
170 weights->info(),
171 biases != nullptr ? biases->info() : nullptr,
172 output->info(),
173 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100174 weights_info,
175 dilation,
Jenkins52ba29e2018-08-29 15:32:11 +0000176 act_info,
177 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000178
Jenkins52ba29e2018-08-29 15:32:11 +0000179 const DataType data_type = input->info()->data_type();
180 const DataLayout data_layout = input->info()->data_layout();
181 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
182 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
183 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
184
185 const unsigned int kernel_width = weights->info()->dimension(idx_width);
186 const unsigned int kernel_height = weights->info()->dimension(idx_height);
187
Jenkinsb9abeae2018-11-22 11:58:08 +0000188 _is_prepared = weights_info.retain_internal_weights();
189 _original_weights = weights;
190 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
191 _data_layout = data_layout;
192 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
193 _skip_col2im = data_layout == DataLayout::NHWC;
194 _append_bias = (biases != nullptr) && (!_is_quantized);
195 _is_activationlayer_enabled = act_info.enabled();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000196
197 // Set the GPU target for im2col and col2im
198 _im2col_kernel.set_target(CLScheduler::get().target());
199 _col2im_kernel.set_target(CLScheduler::get().target());
200
Jenkinsb9abeae2018-11-22 11:58:08 +0000201 const ICLTensor *gemm_input_to_use = input;
202 ICLTensor *gemm_output_to_use = output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000203
Jenkins52ba29e2018-08-29 15:32:11 +0000204 const ICLTensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000205
206 // Get parameters from conv_info
207 unsigned int stride_x = 0;
208 unsigned int stride_y = 0;
209 std::tie(stride_x, stride_y) = conv_info.stride();
210
211 // Get convolved dimensions
212 unsigned int conv_w = 0;
213 unsigned int conv_h = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000214 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
215 input->info()->dimension(idx_height),
216 kernel_width,
217 kernel_height,
218 conv_info,
219 dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000220
Jenkins52ba29e2018-08-29 15:32:11 +0000221 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels) / num_groups;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000222
223 // _weights_reshaped will be auto configured in the kernel.
224 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
Jenkins52ba29e2018-08-29 15:32:11 +0000225 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000226
227 // Create tensor to store im2col reshaped inputs
Jenkins52ba29e2018-08-29 15:32:11 +0000228 if(!_skip_im2col)
229 {
230 _memory_group.manage(&_im2col_output);
231
232 // Configure and tune im2col. im2col output shape is auto-initialized
233 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation, num_groups);
234
235 // Set quantization info
236 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
237 CLScheduler::get().tune_kernel_static(_im2col_kernel);
238
239 // Update GEMM input
240 gemm_input_to_use = &_im2col_output;
241 }
242 else if(_append_bias)
243 {
244 // Configure add bias kernel
245 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
246 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000247
248 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000249 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000250 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000251 TensorShape shape_gemm;
252
253 // If we cannot skip col2im it means we run im2col as well
254 shape_gemm = _im2col_output.info()->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000255 shape_gemm.set(0, mat_weights_cols);
256 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000257
Jenkinsb9abeae2018-11-22 11:58:08 +0000258 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
259 TensorInfo info_gemm(shape_gemm, 1, data_type);
260 info_gemm.set_quantization_info(output->info()->quantization_info()).set_data_layout(input->info()->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000261 _gemm_output.allocator()->init(info_gemm);
262 _memory_group.manage(&_gemm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000263
Jenkins52ba29e2018-08-29 15:32:11 +0000264 // Update GEMM output
265 gemm_output_to_use = &_gemm_output;
266 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000267
Jenkinsb9abeae2018-11-22 11:58:08 +0000268 GEMMLowpOutputStageInfo gemmlowp_output_stage;
269 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
270 gemmlowp_output_stage.gemmlowp_offset = 0;
271 gemmlowp_output_stage.gemmlowp_multiplier = 0;
272 gemmlowp_output_stage.gemmlowp_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000273
274 // Configure output stage for quantized case
275 if(_is_quantized)
276 {
277 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
278
Jenkinsb9abeae2018-11-22 11:58:08 +0000279 const float multiplier = (input->info()->quantization_info().scale * weights->info()->quantization_info().scale) / output_quant_info.scale;
280 int output_multiplier = 0;
281 int output_shift = 0;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000282 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Jenkins52ba29e2018-08-29 15:32:11 +0000283
Jenkinsb9abeae2018-11-22 11:58:08 +0000284 int min_activation = 0;
285 int max_activation = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000286
Jenkinsb9abeae2018-11-22 11:58:08 +0000287 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
288 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
289 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
290 };
291
292 if(_is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
293 {
294 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
295 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
296
297 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
298 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
299
300 // If the activation layer is RELU, BOUNDED_RELU or LU_BOUNDED_RELU, we can use the GEMMLowp output stage to perform this operation
301 _is_activationlayer_enabled = false;
302 }
303
304 // Set the GEMMLowp output stage info
305 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
306 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
307 gemmlowp_output_stage.gemmlowp_shift = output_shift;
308 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
309 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000310 }
311
Jenkinsb9abeae2018-11-22 11:58:08 +0000312 // Configure and tune GEMM
313 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
314 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
315
316 configure_mm(gemm_input_to_use, &_weights_reshaped, biases, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth);
317
318 if(!_skip_im2col)
Jenkins52ba29e2018-08-29 15:32:11 +0000319 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000320 _im2col_output.allocator()->allocate();
Jenkins52ba29e2018-08-29 15:32:11 +0000321 }
322
Jenkinsb9abeae2018-11-22 11:58:08 +0000323 if(!_skip_col2im)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000324 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000325 // Configure and tune Col2Im
326 _col2im_kernel.configure(gemm_output_to_use, output, Size2D(conv_w, conv_h), num_groups);
327 CLScheduler::get().tune_kernel_static(_col2im_kernel);
328 }
329
330 if(!_skip_col2im)
331 {
Jenkins52ba29e2018-08-29 15:32:11 +0000332 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000333 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000334
Jenkins52ba29e2018-08-29 15:32:11 +0000335 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
336 "Output shape does not match the expected one");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000337
Jenkinsb3a371b2018-05-23 11:36:53 +0100338 if(_is_activationlayer_enabled)
339 {
340 _activationlayer_function.configure(output, nullptr, act_info);
341 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000342
343 ARM_COMPUTE_UNUSED(weights_info);
344}
345
346Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000347 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000348{
349 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
350 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Jenkins52ba29e2018-08-29 15:32:11 +0000351 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 +0000352 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000353 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
354 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");
355 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
356 ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW));
357
358 const DataLayout data_layout = input->data_layout();
359 const DataType data_type = input->data_type();
360 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
361 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
362 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
363 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
364
365 const unsigned int kernel_width = weights->dimension(idx_width);
366 const unsigned int kernel_height = weights->dimension(idx_height);
367
Jenkinsb9abeae2018-11-22 11:58:08 +0000368 TensorInfo im2col_reshaped_info, info_gemm, weights_reshaped_info;
369 const ITensorInfo *gemm_input_to_use = input;
370 const ITensorInfo *gemm_output_to_use = output;
371 const ITensorInfo *weights_to_use = weights;
Jenkins52ba29e2018-08-29 15:32:11 +0000372
Jenkinsb9abeae2018-11-22 11:58:08 +0000373 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
374 const bool append_bias = (biases != nullptr) && (!is_quantized);
375 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1);
376 const bool skip_col2im = data_layout == DataLayout::NHWC;
377 bool is_activationlayer_enabled = act_info.enabled();
Jenkins52ba29e2018-08-29 15:32:11 +0000378
379 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000380 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
381
Jenkins52ba29e2018-08-29 15:32:11 +0000382 // Validate biases
Anthony Barbier06ea0482018-02-22 15:45:35 +0000383 if(biases != nullptr)
384 {
385 if(is_quantized)
386 {
387 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
388 }
389 else
390 {
391 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
392 }
Jenkins52ba29e2018-08-29 15:32:11 +0000393 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000394 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
395 }
396
Jenkins52ba29e2018-08-29 15:32:11 +0000397 if(act_info.enabled())
398 {
399 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
400 }
401
402 // Get convolved dimensions
403 unsigned int conv_w = 0;
404 unsigned int conv_h = 0;
405
406 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
407 input->dimension(idx_height),
408 kernel_width,
409 kernel_height,
410 conv_info,
411 dilation);
412
413 unsigned int mat_weights_cols = weights->dimension(idx_kernels) / num_groups;
414
415 // Output tensor auto inizialitation if not yet initialized
416 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr, num_groups));
417 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col), num_groups), 1, data_type);
418 weights_to_use = &weights_reshaped_info;
419
420 if(!skip_im2col)
421 {
422 const Size2D kernel_dims(kernel_width, kernel_height);
423
424 // Output tensor auto initialization if not yet initialized
425 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups);
426
427 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
428
429 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups));
430 gemm_input_to_use = &im2col_reshaped_info;
431 }
432 else if(append_bias)
433 {
434 // Validate add bias kernel
435 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
436 }
437
438 // Create GEMM output tensor
Jenkinsb9abeae2018-11-22 11:58:08 +0000439 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000440 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000441 TensorShape shape_gemm;
442
443 shape_gemm = gemm_input_to_use->tensor_shape();
Jenkins52ba29e2018-08-29 15:32:11 +0000444 shape_gemm.set(0, mat_weights_cols);
445 shape_gemm.set(1, conv_w * conv_h);
Jenkinsb9abeae2018-11-22 11:58:08 +0000446
447 info_gemm = TensorInfo(shape_gemm, 1, data_type);
448 info_gemm.set_quantization_info(output->quantization_info()).set_data_layout(input->data_layout());
Jenkins52ba29e2018-08-29 15:32:11 +0000449 gemm_output_to_use = &info_gemm;
450 }
451
Jenkinsb9abeae2018-11-22 11:58:08 +0000452 GEMMLowpOutputStageInfo gemmlowp_output_stage;
453 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
454 gemmlowp_output_stage.gemmlowp_offset = 0;
455 gemmlowp_output_stage.gemmlowp_multiplier = 0;
456 gemmlowp_output_stage.gemmlowp_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000457
458 if(is_quantized)
459 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000460 const QuantizationInfo output_quant_info = (output->total_size() == 0) ? input->quantization_info() : output->quantization_info();
Jenkins52ba29e2018-08-29 15:32:11 +0000461
Jenkinsb9abeae2018-11-22 11:58:08 +0000462 const float multiplier = (input->quantization_info().scale * weights->quantization_info().scale) / output_quant_info.scale;
463 int output_multiplier = 0;
464 int output_shift = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000465
Jenkinsb9abeae2018-11-22 11:58:08 +0000466 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift));
467
468 int min_activation = 0;
469 int max_activation = 0;
470
471 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
472 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
473 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
474 };
475
476 if(is_activationlayer_enabled && supported_acts.count(act_info.activation()) != 0)
477 {
478 const int a_const_int = output_quant_info.quantize(act_info.a(), RoundingPolicy::TO_NEAREST_UP);
479 const int b_const_int = output_quant_info.quantize(act_info.b(), RoundingPolicy::TO_NEAREST_UP);
480
481 min_activation = act_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU ? output_quant_info.offset : b_const_int;
482 max_activation = act_info.activation() == ActivationLayerInfo::ActivationFunction::RELU ? 255 : a_const_int;
483
484 // If the activation layer is RELU, BOUNDED_RELU or LU_BOUNDED_RELU, we can use the GEMMLowp output stage to perform this operation
485 is_activationlayer_enabled = false;
486 }
487
488 // Set the GEMMLowp output stage info
489 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
490 gemmlowp_output_stage.gemmlowp_multiplier = output_multiplier;
491 gemmlowp_output_stage.gemmlowp_shift = output_shift;
492 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
493 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
Jenkins52ba29e2018-08-29 15:32:11 +0000494 }
495
Jenkinsb9abeae2018-11-22 11:58:08 +0000496 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
497 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
498
499 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));
500
Jenkins52ba29e2018-08-29 15:32:11 +0000501 // Validate Col2Im
Jenkinsb9abeae2018-11-22 11:58:08 +0000502 if(!skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000503 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000504 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 +0000505 }
506
Jenkinsb3a371b2018-05-23 11:36:53 +0100507 //Validate Activation Layer
Jenkinsb9abeae2018-11-22 11:58:08 +0000508 if(is_activationlayer_enabled)
Jenkinsb3a371b2018-05-23 11:36:53 +0100509 {
510 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
511 }
512
Anthony Barbier06ea0482018-02-22 15:45:35 +0000513 return Status{};
514}
515
516void CLGEMMConvolutionLayer::run()
517{
Jenkinsb3a371b2018-05-23 11:36:53 +0100518 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000519
520 _memory_group.acquire();
521
522 // Run im2col
Jenkins52ba29e2018-08-29 15:32:11 +0000523 if(!_skip_im2col)
524 {
525 CLScheduler::get().enqueue(_im2col_kernel);
526 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000527
528 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
529 if(_is_quantized)
530 {
531 // Run gemmlowp
532 _mm_gemmlowp.run();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000533 }
534 else
535 {
536 // Run gemm
537 _mm_gemm.run();
538 }
539
Jenkins52ba29e2018-08-29 15:32:11 +0000540 if(_skip_im2col && _append_bias)
541 {
542 CLScheduler::get().enqueue(_add_bias_kernel);
543 }
544
Anthony Barbier06ea0482018-02-22 15:45:35 +0000545 // Reshape output matrix
Jenkinsb9abeae2018-11-22 11:58:08 +0000546 if(!_skip_col2im)
Jenkins52ba29e2018-08-29 15:32:11 +0000547 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000548 CLScheduler::get().enqueue(_col2im_kernel, false);
Jenkins52ba29e2018-08-29 15:32:11 +0000549 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000550
Jenkinsb3a371b2018-05-23 11:36:53 +0100551 //Run Activation Layer if enabled
552 if(_is_activationlayer_enabled)
553 {
554 _activationlayer_function.run();
555 }
556
Anthony Barbier06ea0482018-02-22 15:45:35 +0000557 _memory_group.release();
558}
Jenkinsb3a371b2018-05-23 11:36:53 +0100559
560void CLGEMMConvolutionLayer::prepare()
561{
562 if(!_is_prepared)
563 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100564 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins52ba29e2018-08-29 15:32:11 +0000565
566 // Run weights reshaping and mark original weights tensor as unused
Jenkinsb3a371b2018-05-23 11:36:53 +0100567 _weights_reshaped.allocator()->allocate();
568 _reshape_weights.run();
569 _original_weights->mark_as_unused();
570
Jenkins52ba29e2018-08-29 15:32:11 +0000571 // Prepare GEMM
572 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
573 if(!_weights_reshaped.is_used())
Jenkinsb3a371b2018-05-23 11:36:53 +0100574 {
Jenkins52ba29e2018-08-29 15:32:11 +0000575 _weights_reshaped.allocator()->free();
Jenkinsb3a371b2018-05-23 11:36:53 +0100576 }
577
578 CLScheduler::get().queue().finish();
579 _is_prepared = true;
580 }
581}