blob: 79495e495d7bf97737f41ba53e83d56e16a7992f [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
46void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output)
47{
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,
52 output->info()));
53
54 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
55 const ICLTensor *biases_to_use = (append_biases) ? biases : nullptr;
56
57 _weights_reshape_kernel.configure(weights, biases_to_use, output);
58
59 output->info()->set_quantization_info(weights->info()->quantization_info());
60}
61
62Status CLConvolutionLayerReshapeWeights::validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(weights);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
66 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
67
68 if(biases != nullptr)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(weights->data_type()));
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
72 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
73 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
74 }
75
76 if((output != nullptr) && (output->total_size() != 0))
77 {
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
80
81 CLWeightsReshapeKernel::validate(weights, biases, output);
82 }
83
84 return Status{};
85}
86
87void CLConvolutionLayerReshapeWeights::run()
88{
Anthony Barbier06ea0482018-02-22 15:45:35 +000089 CLScheduler::get().enqueue(_weights_reshape_kernel);
Anthony Barbier06ea0482018-02-22 15:45:35 +000090}
91
92CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Jenkinsb3a371b2018-05-23 11:36:53 +010093 : _memory_group(memory_manager), _reshape_weights(), _im2col_kernel(), _mm_gemm(memory_manager), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _col2im_kernel(), _activationlayer_function(),
94 _original_weights(nullptr), _im2col_output(), _weights_reshaped(), _gemm_output(), _tmp_output(), _is_quantized(false), _is_activationlayer_enabled(false), _is_prepared(false)
Anthony Barbier06ea0482018-02-22 15:45:35 +000095{
96}
97
98void CLGEMMConvolutionLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
99{
100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
101 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info()));
102
103 if(_is_quantized)
104 {
105 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
106 // Extract and negate input and weights offset
107 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
108 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
109
110 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
111 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
112
113 _mm_gemmlowp.configure(input, weights, output, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
114
115 // Revert back QuantizatioInfo as input and weights could be used in other convolution layers
116 input->info()->set_quantization_info(input_quantization_info);
117 weights->info()->set_quantization_info(weights_quantization_info);
118 }
119 else
120 {
121 // Configure matrix multiply function
122 _mm_gemm.configure(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/));
123 }
124}
125
126Status CLGEMMConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output)
127{
128 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
129
130 const GEMMInfo &gemm_info = GEMMInfo(false, false, true /* Reshape weights only for the first run */);
131 if(is_quantized)
132 {
133 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
134 // Extract and negate input and weights offset
135 const QuantizationInfo input_quantization_info = input->quantization_info();
136 const QuantizationInfo weights_quantization_info = weights->quantization_info();
137
138 std::unique_ptr<ITensorInfo> input_qa = input->clone();
139 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
140 input_qa->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
141 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
142
143 // Perform validation step on GEMMLowp
144 CLGEMMLowpMatrixMultiplyCore::validate(input_qa.get(), weights_qa.get(), output, gemm_info);
145 }
146 else
147 {
148 // Perform validation step on Matrix multiply function
149 CLGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, gemm_info);
150 }
151 return Status{};
152}
153
Jenkinsb3a371b2018-05-23 11:36:53 +0100154void CLGEMMConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
155 const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000156{
157 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
158
159 ARM_COMPUTE_ERROR_THROW_ON(CLGEMMConvolutionLayer::validate(input->info(),
160 weights->info(),
161 biases != nullptr ? biases->info() : nullptr,
162 output->info(),
163 conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100164 weights_info,
165 dilation,
166 act_info));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000167
Jenkinsb3a371b2018-05-23 11:36:53 +0100168 _is_prepared = false;
169 _original_weights = weights;
170 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Anthony Barbier06ea0482018-02-22 15:45:35 +0000171
172 const DataType dt = input->info()->data_type();
173
174 // Set the GPU target for im2col and col2im
175 _im2col_kernel.set_target(CLScheduler::get().target());
176 _col2im_kernel.set_target(CLScheduler::get().target());
177
178 const bool append_bias = (biases != nullptr) && (!_is_quantized);
179
180 const unsigned bias_element = (append_bias) ? 1 : 0;
181 const ICLTensor *biases_to_use = (append_bias) ? biases : nullptr;
182
183 // Get parameters from conv_info
184 unsigned int stride_x = 0;
185 unsigned int stride_y = 0;
186 std::tie(stride_x, stride_y) = conv_info.stride();
187
188 // Get convolved dimensions
189 unsigned int conv_w = 0;
190 unsigned int conv_h = 0;
191
192 const unsigned int kernel_width = weights->info()->dimension(0);
193 const unsigned int kernel_height = weights->info()->dimension(1);
194 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
Jenkinsb3a371b2018-05-23 11:36:53 +0100195 conv_info, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000196
197 unsigned int mat_weights_cols = weights->info()->dimension(3);
198 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
199
200 // _weights_reshaped will be auto configured in the kernel.
201 // Just append biases and do not transpose 1xW as it will be reshaped in CLGEMM
202 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
203
204 weights = &_weights_reshaped;
205
206 // Create tensor to store im2col reshaped inputs
207 const unsigned int mat_input_cols = mat_weights_rows;
208 const unsigned int mat_input_rows = conv_w * conv_h;
209 TensorShape shape_im2col = input->info()->tensor_shape();
210 shape_im2col.set(0, mat_input_cols);
211 shape_im2col.set(1, mat_input_rows);
212 shape_im2col.set(2, 1);
213 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->info()->fixed_point_position());
214 im2col_reshaped_info.set_quantization_info(input->info()->quantization_info());
215 _im2col_output.allocator()->init(im2col_reshaped_info);
216 _memory_group.manage(&_im2col_output);
217
218 // Create GEMM output tensor
219 TensorShape shape_gemm = _im2col_output.info()->tensor_shape();
220 shape_gemm.set(0, mat_weights_cols);
221 shape_gemm.set(1, mat_input_rows);
222 const DataType gemm_data_type = _is_quantized ? DataType::S32 : dt;
223 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
224 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->info()->fixed_point_position());
225 info_gemm.set_quantization_info(output->info()->quantization_info());
226 _gemm_output.allocator()->init(info_gemm);
227 _memory_group.manage(&_gemm_output);
228
229 // Configure im2col
Jenkinsb3a371b2018-05-23 11:36:53 +0100230 _im2col_kernel.configure(input, &_im2col_output, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000231
232 // Configure GEMM
233 configure_mm(&_im2col_output, weights, &_gemm_output);
234
235 _im2col_output.allocator()->allocate();
236
237 // Configure output stage for quantized case
238 if(_is_quantized)
239 {
240 const QuantizationInfo output_quant_info = (output->info()->total_size() == 0) ? input->info()->quantization_info() : output->info()->quantization_info();
241
242 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output_quant_info.scale;
243 int output_multiplier, output_shift;
244 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
245 _memory_group.manage(&_tmp_output);
246 _gemmlowp_output_stage.configure(&_gemm_output, biases, &_tmp_output, output_multiplier, output_shift, output_quant_info.offset);
247 }
248
249 // Configure Col2Im
250 _col2im_kernel.configure(_is_quantized ? &_tmp_output : &_gemm_output, output, std::make_pair(conv_w, conv_h));
251 if(_is_quantized)
252 {
253 _tmp_output.allocator()->allocate();
254 }
255 _gemm_output.allocator()->allocate();
256
257 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one");
258
Jenkinsb3a371b2018-05-23 11:36:53 +0100259 //Configure Activation Layer
260 _is_activationlayer_enabled = act_info.enabled();
261
262 if(_is_activationlayer_enabled)
263 {
264 _activationlayer_function.configure(output, nullptr, act_info);
265 }
Anthony Barbier06ea0482018-02-22 15:45:35 +0000266
267 ARM_COMPUTE_UNUSED(weights_info);
268}
269
270Status CLGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Jenkinsb3a371b2018-05-23 11:36:53 +0100271 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000272{
273 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
274 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
275 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
276 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
277 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
278 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
279 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
280
Jenkinsb3a371b2018-05-23 11:36:53 +0100281 if(act_info.enabled())
282 {
283 ARM_COMPUTE_ERROR_ON(act_info.b() > act_info.a());
284 }
285
Anthony Barbier06ea0482018-02-22 15:45:35 +0000286 const bool is_quantized = is_data_type_quantized_asymmetric(input->data_type());
287 const bool append_bias = (biases != nullptr) && (!is_quantized);
288 const unsigned bias_element = (append_bias) ? 1 : 0;
289 const DataType dt = input->data_type();
290
291 // Get convolved dimensions
292 unsigned int conv_w = 0;
293 unsigned int conv_h = 0;
294
295 const unsigned int kernel_width = weights->dimension(0);
296 const unsigned int kernel_height = weights->dimension(1);
297
Jenkinsb3a371b2018-05-23 11:36:53 +0100298 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height, conv_info, dilation);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000299
300 unsigned int mat_weights_cols = weights->dimension(3);
301 unsigned int mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + bias_element;
302
Jenkinsb3a371b2018-05-23 11:36:53 +0100303 ARM_COMPUTE_RETURN_ON_ERROR(CLConvolutionLayerReshapeWeights::validate(weights, is_quantized ? nullptr : biases, nullptr));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000304
305 // Create tensor info for im2col reshaped inputs
306 const unsigned int mat_input_cols = mat_weights_rows;
307 const unsigned int mat_input_rows = conv_w * conv_h;
308 TensorShape shape_im2col = input->tensor_shape();
309 shape_im2col.set(0, mat_input_cols);
310 shape_im2col.set(1, mat_input_rows);
311 shape_im2col.set(2, 1);
312 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt, input->fixed_point_position());
313 im2col_reshaped_info.set_quantization_info(input->quantization_info());
Jenkinsb3a371b2018-05-23 11:36:53 +0100314 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000315
316 // Create GEMM output tensor
317 TensorShape shape_gemm = im2col_reshaped_info.tensor_shape();
318 shape_gemm.set(0, mat_weights_cols);
319 shape_gemm.set(1, mat_input_rows);
320 const DataType gemm_data_type = is_quantized ? DataType::S32 : dt;
321 // GEMM output should be S32 for acquiring raw integer accumulator without quantized postprocessing for quantized asymmetric input.
322 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type, input->fixed_point_position());
323 info_gemm.set_quantization_info(output->quantization_info());
324
Jenkinsb3a371b2018-05-23 11:36:53 +0100325 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(&im2col_reshaped_info, weights, &info_gemm));
326 TensorInfo tmp_info(shape_gemm, 1, DataType::QASYMM8, input->fixed_point_position());
327 tmp_info.set_quantization_info(output->quantization_info());
Anthony Barbier06ea0482018-02-22 15:45:35 +0000328
Anthony Barbier06ea0482018-02-22 15:45:35 +0000329 if(is_quantized)
330 {
331 float multiplier = input->quantization_info().scale * weights->quantization_info().scale / output->quantization_info().scale;
332 int output_multiplier, output_shift;
333 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
334 // Validate output stage for quantized case
335 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(&info_gemm, biases, &tmp_info, output->quantization_info().offset);
336 }
337
338 // Validate Col2Im
Jenkinsb3a371b2018-05-23 11:36:53 +0100339 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(is_quantized ? &tmp_info : &info_gemm, output, std::make_pair(conv_w, conv_h)));
Anthony Barbier06ea0482018-02-22 15:45:35 +0000340
341 if(biases != nullptr)
342 {
343 if(is_quantized)
344 {
345 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
346 }
347 else
348 {
349 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
350 }
351 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
352 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
353 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
354 }
355
Jenkinsb3a371b2018-05-23 11:36:53 +0100356 //Validate Activation Layer
357 if(act_info.enabled())
358 {
359 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
360 }
361
Anthony Barbier06ea0482018-02-22 15:45:35 +0000362 return Status{};
363}
364
365void CLGEMMConvolutionLayer::run()
366{
Jenkinsb3a371b2018-05-23 11:36:53 +0100367 prepare();
Anthony Barbier06ea0482018-02-22 15:45:35 +0000368
369 _memory_group.acquire();
370
371 // Run im2col
372 CLScheduler::get().enqueue(_im2col_kernel);
373
374 // Runs CLGEMM or CLGEMMLowpMatrixMultiplyCore functions
375 if(_is_quantized)
376 {
377 // Run gemmlowp
378 _mm_gemmlowp.run();
379
380 // Run output stage
381 _gemmlowp_output_stage.run();
382 }
383 else
384 {
385 // Run gemm
386 _mm_gemm.run();
387 }
388
389 // Reshape output matrix
390 CLScheduler::get().enqueue(_col2im_kernel, false);
391
Jenkinsb3a371b2018-05-23 11:36:53 +0100392 //Run Activation Layer if enabled
393 if(_is_activationlayer_enabled)
394 {
395 _activationlayer_function.run();
396 }
397
Anthony Barbier06ea0482018-02-22 15:45:35 +0000398 _memory_group.release();
399}
Jenkinsb3a371b2018-05-23 11:36:53 +0100400
401void CLGEMMConvolutionLayer::prepare()
402{
403 if(!_is_prepared)
404 {
405 // Run weights reshaping and mark as unused
406 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
407 _weights_reshaped.allocator()->allocate();
408 _reshape_weights.run();
409 _original_weights->mark_as_unused();
410
411 // Run GEMM prepare
412 if(!_is_quantized)
413 {
414 _mm_gemm.prepare();
415 if(!_weights_reshaped.is_used())
416 {
417 _weights_reshaped.allocator()->free();
418 }
419 }
420
421 CLScheduler::get().queue().finish();
422 _is_prepared = true;
423 }
424}