blob: 92d04d6699af5f72a9c09153605fdd3990b1afda [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)
Jenkinsb3a371b2018-05-23 11:36:53 +010094 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(),
Jenkins52ba29e2018-08-29 15:32:11 +000095 _add_bias_kernel(), _reshape_layer(), _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _data_layout(DataLayout::NCHW), _append_bias(false),
96 _skip_im2col(false), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +000097{
98}
99
Jenkins52ba29e2018-08-29 15:32:11 +0000100void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, int gemm_3d_depth)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000101{
102 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000103 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info(), gemm_3d_depth, _skip_im2col));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000104
105 if(_is_quantized)
106 {
107 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
108 // Extract and negate input and weights offset
109 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
110 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
111
112 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
113 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
114
115 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
116
117 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
118 input->info()->set_quantization_info(input_quantization_info);
119 weights->info()->set_quantization_info(weights_quantization_info);
120 }
121 else
122 {
123 // Configure matrix multiply function
Jenkins52ba29e2018-08-29 15:32:11 +0000124 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/, gemm_3d_depth,
125 _skip_im2col /* Reinterpret the input as 3D if im2col is skipped */));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000126 }
127}
128
Jenkins52ba29e2018-08-29 15:32:11 +0000129Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, int gemm_3d_depth, bool skip_im2col)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000130{
131 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
132
Jenkins52ba29e2018-08-29 15:32:11 +0000133 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */, gemm_3d_depth, skip_im2col /* Reinterpret the input as 3D if im2col is skipped */);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000134 if(is_quantized)
135 {
136 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
137 // Extract and negate input and weights offset
138 const QuantizationInfo input_quantization_info = input->quantization_info();
139 const QuantizationInfo weights_quantization_info = weights->quantization_info();
140
141 std::unique_ptr<ITensorInfo> input_qa = input->clone();
142 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
143 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
144 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
145
146 // Perform validation step on GEMMLowp
Jenkins52ba29e2018-08-29 15:32:11 +0000147 return CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000148 }
149 else
150 {
151 // Perform validation step on Matrix multiply function
Jenkins52ba29e2018-08-29 15:32:11 +0000152 return CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000153 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000154}
155
Jenkinsb3a371b2018-05-23 11:36:53 +0100156void 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 +0000157 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000158{
159 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
160
161 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
162 weights->info(),
163 biases != nullptr ? biases->info() : nullptr,
164 output->info(),
165 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100166 weights_info,
167 dilation,
Jenkins52ba29e2018-08-29 15:32:11 +0000168 act_info,
169 num_groups));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000170
Jenkins52ba29e2018-08-29 15:32:11 +0000171 const DataType data_type = input->info()->data_type();
172 const DataLayout data_layout = input->info()->data_layout();
173 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
174 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
175 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
176
177 const unsigned int kernel_width = weights->info()->dimension(idx_width);
178 const unsigned int kernel_height = weights->info()->dimension(idx_height);
179
180 _is_prepared = weights_info.retain_internal_weights();
Jenkinsb3a371b2018-05-23 11:36:53 +0100181 _original_weights = weights;
182 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Jenkins52ba29e2018-08-29 15:32:11 +0000183 _data_layout = data_layout;
184 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1) && !_is_quantized;
185 _append_bias = (biases != nullptr) && (!_is_quantized);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000186
187 // Set the GPU target for im2col and col2im
188 _im2col_kernel.set_target(CLScheduler::get().target());
189 _col2im_kernel.set_target(CLScheduler::get().target());
190
Jenkins52ba29e2018-08-29 15:32:11 +0000191 bool is_nhwc = _data_layout == DataLayout::NHWC;
192 const ICLTensor *gemm_input_to_use = input;
193 ICLTensor *gemm_output_to_use = output;
194 ICLTensor *gemm_output_staged_to_use = output;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000195
Jenkins52ba29e2018-08-29 15:32:11 +0000196 const ICLTensor *biases_to_use = (_append_bias && !_skip_im2col) ? biases : nullptr;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000197
198 // Get parameters from conv_info
199 unsigned int stride_x = 0;
200 unsigned int stride_y = 0;
201 std::tie(stride_x, stride_y) = conv_info.stride();
202
203 // Get convolved dimensions
204 unsigned int conv_w = 0;
205 unsigned int conv_h = 0;
Jenkins52ba29e2018-08-29 15:32:11 +0000206 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(idx_width),
207 input->info()->dimension(idx_height),
208 kernel_width,
209 kernel_height,
210 conv_info,
211 dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000212
Jenkins52ba29e2018-08-29 15:32:11 +0000213 unsigned int mat_weights_cols = weights->info()->dimension(idx_kernels) / num_groups;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000214
215 // _weights_reshaped will be auto configured in the kernel.
216 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
Jenkins52ba29e2018-08-29 15:32:11 +0000217 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped, num_groups);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000218
219 // Create tensor to store im2col reshaped inputs
Jenkins52ba29e2018-08-29 15:32:11 +0000220 if(!_skip_im2col)
221 {
222 _memory_group.manage(&_im2col_output);
223
224 // Configure and tune im2col. im2col output shape is auto-initialized
225 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, _append_bias, dilation, num_groups);
226
227 // Set quantization info
228 _im2col_output.info()->set_quantization_info(input->info()->quantization_info());
229 CLScheduler::get().tune_kernel_static(_im2col_kernel);
230
231 // Update GEMM input
232 gemm_input_to_use = &_im2col_output;
233 }
234 else if(_append_bias)
235 {
236 // Configure add bias kernel
237 _add_bias_kernel.configure(output, biases, output, ConvertPolicy::SATURATE);
238 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000239
240 // Create GEMM output tensor
Jenkins52ba29e2018-08-29 15:32:11 +0000241 if(!is_nhwc || _is_quantized)
242 {
243 // Calculate GEMM output shape
244 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
245 shape_gemm.set(0, mat_weights_cols);
246 shape_gemm.set(1, conv_w * conv_h);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000247
Jenkins52ba29e2018-08-29 15:32:11 +0000248 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
249 const DataType gemm_data_type = _is_quantized ? DataType::S32 : data_type;
250 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
251 info_gemm.set_quantization_info(output->info()->quantization_info());
252 _gemm_output.allocator()->init(info_gemm);
253 _memory_group.manage(&_gemm_output);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000254
Jenkins52ba29e2018-08-29 15:32:11 +0000255 // Update GEMM output
256 gemm_output_to_use = &_gemm_output;
257 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000258
Jenkins52ba29e2018-08-29 15:32:11 +0000259 // Configure and tune GEMM
260 configure_mm(gemm_input_to_use, &_weights_reshaped, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1);
261
262 if(!_skip_im2col)
263 {
264 _im2col_output.allocator()->allocate();
265 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000266
267 // Configure output stage for quantized case
268 if(_is_quantized)
269 {
270 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
271
272 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
273 int output_multiplier, output_shift;
274 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
Jenkins52ba29e2018-08-29 15:32:11 +0000275
Anthony Barbier06ea0482018-02-22 15:45:35 +0000276 _memory_group.manage(&_tmp_output);
Jenkins52ba29e2018-08-29 15:32:11 +0000277 gemm_output_staged_to_use = &_tmp_output;
278
279 _gemmlowp_output_stage.configure(gemm_output_to_use, biases, gemm_output_staged_to_use, output_multiplier, output_shift, output_quant_info.offset);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000280 }
281
Jenkins52ba29e2018-08-29 15:32:11 +0000282 if(!is_nhwc || _is_quantized)
283 {
284 if(input->info()->data_layout() == DataLayout::NCHW)
285 {
286 // Configure and tune Col2Im
287 _col2im_kernel.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output, std::make_pair(conv_w, conv_h), num_groups);
288 CLScheduler::get().tune_kernel_static(_col2im_kernel);
289 }
290 else
291 {
292 // Configure reshape layer
293 _reshape_layer.configure(_is_quantized ? gemm_output_staged_to_use : gemm_output_to_use, output);
294 }
295 }
296
297 if(!is_nhwc || _is_quantized)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000298 {
299 _tmp_output.allocator()->allocate();
Jenkins52ba29e2018-08-29 15:32:11 +0000300 _gemm_output.allocator()->allocate();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000301 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000302
Jenkins52ba29e2018-08-29 15:32:11 +0000303 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(idx_width) != conv_w) || (output->info()->dimension(idx_height) != conv_h),
304 "Output shape does not match the expected one");
Anthony Barbier06ea0482018-02-22 15:45:35 +0000305
Jenkinsb3a371b2018-05-23 11:36:53 +0100306 //Configure Activation Layer
307 _is_activationlayer_enabled = act_info.enabled();
308
309 if(_is_activationlayer_enabled)
310 {
311 _activationlayer_function.configure(output, nullptr, act_info);
312 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000313
314 ARM_COMPUTE_UNUSED(weights_info);
315}
316
317Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkins52ba29e2018-08-29 15:32:11 +0000318 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000319{
320 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
321 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Jenkins52ba29e2018-08-29 15:32:11 +0000322 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 +0000323 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Jenkins52ba29e2018-08-29 15:32:11 +0000324 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, weights);
325 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");
326 ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
327 ARM_COMPUTE_RETURN_ERROR_ON(((input->dimension(2) / weights->dimension(2)) != num_groups) && (input->data_layout() == DataLayout::NCHW));
328
329 const DataLayout data_layout = input->data_layout();
330 const DataType data_type = input->data_type();
331 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
332 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
333 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
334 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
335
336 const unsigned int kernel_width = weights->dimension(idx_width);
337 const unsigned int kernel_height = weights->dimension(idx_height);
338
339 TensorInfo im2col_reshaped_info, info_gemm, tmp_info, weights_reshaped_info;
340 const ITensorInfo *gemm_input_to_use = input;
341 const ITensorInfo *gemm_output_to_use = output;
342 const ITensorInfo *gemm_output_staged_to_use = output;
343 const ITensorInfo *weights_to_use = weights;
344
345 const bool is_nhwc = data_layout == DataLayout::NHWC;
346 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
347 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv_info.stride().first == 1 && conv_info.stride().second == 1) && !is_quantized;
348 const bool append_bias = (biases != nullptr) && (!is_quantized);
349
350 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * num_groups) != input->dimension(idx_channel));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000351 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
352
Jenkins52ba29e2018-08-29 15:32:11 +0000353 // Validate biases
Anthony Barbier06ea0482018-02-22 15:45:35 +0000354 if(biases != nullptr)
355 {
356 if(is_quantized)
357 {
358 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
359 }
360 else
361 {
362 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
363 }
Jenkins52ba29e2018-08-29 15:32:11 +0000364 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000365 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
366 }
367
Jenkins52ba29e2018-08-29 15:32:11 +0000368 if(act_info.enabled())
369 {
370 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
371 }
372
373 // Get convolved dimensions
374 unsigned int conv_w = 0;
375 unsigned int conv_h = 0;
376
377 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(idx_width),
378 input->dimension(idx_height),
379 kernel_width,
380 kernel_height,
381 conv_info,
382 dilation);
383
384 unsigned int mat_weights_cols = weights->dimension(idx_kernels) / num_groups;
385
386 // Output tensor auto inizialitation if not yet initialized
387 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr, num_groups));
388 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, (append_bias && !skip_im2col), num_groups), 1, data_type);
389 weights_to_use = &weights_reshaped_info;
390
391 if(!skip_im2col)
392 {
393 const Size2D kernel_dims(kernel_width, kernel_height);
394
395 // Output tensor auto initialization if not yet initialized
396 TensorShape expected_output_shape = compute_im2col_conv_shape(input, kernel_dims, conv_info, append_bias, dilation, num_groups == 1, num_groups);
397
398 auto_init_if_empty(im2col_reshaped_info, input->clone()->set_tensor_shape(expected_output_shape));
399
400 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, kernel_dims, conv_info, append_bias, dilation, num_groups));
401 gemm_input_to_use = &im2col_reshaped_info;
402 }
403 else if(append_bias)
404 {
405 // Validate add bias kernel
406 ARM_COMPUTE_RETURN_ON_ERROR(CLArithmeticAdditionKernel::validate(output, biases, output, ConvertPolicy::SATURATE));
407 }
408
409 // Create GEMM output tensor
410 if(!is_nhwc || is_quantized)
411 {
412 TensorShape shape_gemm = gemm_input_to_use->tensor_shape();
413 shape_gemm.set(0, mat_weights_cols);
414 shape_gemm.set(1, conv_w * conv_h);
415 const DataType gemm_data_type = is_quantized ? DataType::S32 : data_type;
416 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
417 info_gemm = TensorInfo(shape_gemm, 1, gemm_data_type);
418 info_gemm.set_quantization_info(output->quantization_info());
419 gemm_output_to_use = &info_gemm;
420 }
421
422 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, gemm_output_to_use, (data_layout == DataLayout::NHWC) ? conv_h : 1, skip_im2col));
423
424 if(is_quantized)
425 {
426 float multiplier = input->quantization_info().scale * weights_to_use->quantization_info().scale / output->quantization_info().scale;
427 int output_multiplier, output_shift;
428 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
429
430 tmp_info = TensorInfo(gemm_output_to_use->tensor_shape(), 1, DataType::QASYMM8);
431 tmp_info.set_quantization_info(output->quantization_info());
432 gemm_output_staged_to_use = &tmp_info;
433
434 // Validate output stage for quantized case
435 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(gemm_output_to_use, biases, gemm_output_staged_to_use, output->quantization_info().offset);
436 }
437
438 // Validate Col2Im
439 if(!is_nhwc || is_quantized)
440 {
441 if(input->data_layout() == DataLayout::NCHW)
442 {
443 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(is_quantized ? gemm_output_staged_to_use : gemm_output_to_use,
444 output,
445 std::make_pair(conv_w, conv_h), num_groups));
446 }
447 }
448
Jenkinsb3a371b2018-05-23 11:36:53 +0100449 //Validate Activation Layer
450 if(act_info.enabled())
451 {
452 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
453 }
454
Anthony Barbier06ea0482018-02-22 15:45:35 +0000455 return Status{};
456}
457
458void CLGEMMConvolutionLayer::run()
459{
Jenkinsb3a371b2018-05-23 11:36:53 +0100460 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000461
462 _memory_group.acquire();
463
464 // Run im2col
Jenkins52ba29e2018-08-29 15:32:11 +0000465 if(!_skip_im2col)
466 {
467 CLScheduler::get().enqueue(_im2col_kernel);
468 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000469
470 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
471 if(_is_quantized)
472 {
473 // Run gemmlowp
474 _mm_gemmlowp.run();
475
476 // Run output stage
477 _gemmlowp_output_stage.run();
478 }
479 else
480 {
481 // Run gemm
482 _mm_gemm.run();
483 }
484
Jenkins52ba29e2018-08-29 15:32:11 +0000485 if(_skip_im2col && _append_bias)
486 {
487 CLScheduler::get().enqueue(_add_bias_kernel);
488 }
489
Anthony Barbier06ea0482018-02-22 15:45:35 +0000490 // Reshape output matrix
Jenkins52ba29e2018-08-29 15:32:11 +0000491 if(_data_layout == DataLayout::NCHW || _is_quantized)
492 {
493 if(_data_layout == DataLayout::NCHW)
494 {
495 CLScheduler::get().enqueue(_col2im_kernel, false);
496 }
497 else
498 {
499 _reshape_layer.run();
500 }
501 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000502
Jenkinsb3a371b2018-05-23 11:36:53 +0100503 //Run Activation Layer if enabled
504 if(_is_activationlayer_enabled)
505 {
506 _activationlayer_function.run();
507 }
508
Anthony Barbier06ea0482018-02-22 15:45:35 +0000509 _memory_group.release();
510}
Jenkinsb3a371b2018-05-23 11:36:53 +0100511
512void CLGEMMConvolutionLayer::prepare()
513{
514 if(!_is_prepared)
515 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100516 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
Jenkins52ba29e2018-08-29 15:32:11 +0000517
518 // Run weights reshaping and mark original weights tensor as unused
Jenkinsb3a371b2018-05-23 11:36:53 +0100519 _weights_reshaped.allocator()->allocate();
520 _reshape_weights.run();
521 _original_weights->mark_as_unused();
522
Jenkins52ba29e2018-08-29 15:32:11 +0000523 // Prepare GEMM
524 _is_quantized ? _mm_gemmlowp.prepare() : _mm_gemm.prepare();
525 if(!_weights_reshaped.is_used())
Jenkinsb3a371b2018-05-23 11:36:53 +0100526 {
Jenkins52ba29e2018-08-29 15:32:11 +0000527 _weights_reshaped.allocator()->free();
Jenkinsb3a371b2018-05-23 11:36:53 +0100528 }
529
530 CLScheduler::get().queue().finish();
531 _is_prepared = true;
532 }
533}