blob: 1926cbbe4dcc6f27d2a078bd18b4f2504b5df27e [file] [log] [blame]
Manuel Bottinid87aded2021-07-16 10:23:31 +01001/*
2 * Copyright (c) 2017-2021 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 "src/runtime/gpu/cl/operators/ClGemmConvolution.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/PixelValue.h"
28#include "arm_compute/core/Size2D.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34#include "arm_compute/runtime/CL/CLScheduler.h"
35#include "src/core/gpu/cl/kernels/ClActivationKernel.h"
36#include "src/core/gpu/cl/kernels/ClCol2ImKernel.h"
37#include "src/core/gpu/cl/kernels/ClIm2ColKernel.h"
38#include "src/core/gpu/cl/kernels/ClWeightsReshapeKernel.h"
39#include "src/core/helpers/AutoConfiguration.h"
40#include "src/core/helpers/MemoryHelpers.h"
41#include "src/runtime/gpu/cl/operators/ClGemm.h"
42#include "src/runtime/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h"
43#include "src/runtime/gpu/cl/utils/ClAuxTensorHandler.h"
44#include "support/Cast.h"
45
46namespace arm_compute
47{
48using namespace experimental;
49using namespace misc::shape_calculator;
50using namespace utils::cast;
51namespace opencl
52{
53ClGemmConvolution::ClGemmConvolution()
54 : _weights_reshape_kernel(nullptr), _im2col_kernel(nullptr), _mm_gemm(nullptr), _mm_gemmlowp(nullptr), _col2im_kernel(nullptr), _activation_kernel(nullptr), _im2col_output(), _weights_reshaped(),
55 _gemm_output(), _skip_im2col(false), _skip_col2im(false), _is_quantized(false), _fuse_activation(true), _append_bias(false), _is_prepared(false), _aux_mem(AuxTensorIdx::Count)
56{
57}
58ClGemmConvolution::~ClGemmConvolution() = default;
59
60void ClGemmConvolution::configure_mm(const ClCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
61 const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
62 int gemm_3d_depth, const ActivationLayerInfo &act_info)
63{
64 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights);
65 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(src, weights, biases, dst, gemmlowp_output_stage, gemm_3d_depth, _skip_im2col, act_info));
66
67 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
68 false, // is_b_reshaped
69 true, // reshape_b_only_on_first_run
70 gemm_3d_depth, // depth_output_gemm3d
71 _skip_im2col, // reinterpret_input_as_3d
72 false, // retain_internal_weights
73 gemmlowp_output_stage, // gemmlowp_output_stage
74 false, // fast_math
75 false, // fp_mixed_precision
76 true, // broadcast_bias
77 act_info); // activation_info
78
79 TensorInfo tmp_src{ *src };
80 if(_is_quantized)
81 {
82 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
83 // Extract and negate input and weights offset
84 const QuantizationInfo input_quantization_info = src->quantization_info();
85 const QuantizationInfo weights_quantization_info = weights->quantization_info();
86
87 tmp_src.set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
88 weights->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
89
90 _mm_gemmlowp = std::make_unique<ClGemmLowpMatrixMultiplyCore>();
91 _mm_gemmlowp->configure(compile_context, &tmp_src, weights, biases, dst, gemm_info);
92
93 // Revert back QuantizatioInfo as weights could be used in other convolution layers
94 weights->set_quantization_info(weights_quantization_info);
95
96 auto mm_mem_req = _mm_gemmlowp->workspace();
97 for(unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
98 {
99 _aux_mem[cont] = mm_mem_req[cont];
100 }
101 }
102 else
103 {
104 // Configure matrix multiply function
105 _mm_gemm = std::make_unique<ClGemm>();
106 _mm_gemm->configure(compile_context, &tmp_src, weights, biases, dst, 1.0f, 1.0f, gemm_info);
107 auto mm_mem_req = _mm_gemm->workspace();
108 for(unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
109 {
110 _aux_mem[cont] = mm_mem_req[cont];
111 }
112 }
113}
114
115Status ClGemmConvolution::validate_mm(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
116 const GEMMLowpOutputStageInfo &gemmlowp_output_stage, int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info)
117{
118 const bool is_quantized = is_data_type_quantized_asymmetric(src->data_type());
119
120 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
121 false, // is_b_reshaped
122 true, // reshape_b_only_on_first_run
123 gemm_3d_depth, // depth_output_gemm3d
124 skip_im2col, // reinterpret_input_as_3d
125 false, // retain_internal_weights
126 gemmlowp_output_stage, // gemmlowp_output_stage
127 false, // fast_math
128 false, // fp_mixed_precision
129 true, // broadcast_bias
130 act_info); // activation_info
131
132 if(is_quantized)
133 {
134 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
135 // Extract and negate input and weights offset
136 const QuantizationInfo input_quantization_info = src->quantization_info();
137 const QuantizationInfo weights_quantization_info = weights->quantization_info();
138
139 std::unique_ptr<ITensorInfo> src_qa = src->clone();
140 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
141 src_qa->set_quantization_info(QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
142 weights_qa->set_quantization_info(QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
143
144 // Perform validation step on GEMMLowp
145 return ClGemmLowpMatrixMultiplyCore::validate(src_qa.get(), weights_qa.get(), biases, dst, gemm_info);
146 }
147 else
148 {
149 // Perform validation step on Matrix multiply function
150 return ClGemm::validate(src, weights, biases, dst, 1.0f, 1.0f, gemm_info);
151 }
152}
153
154void ClGemmConvolution::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
155 const Conv2dInfo &conv2d_info, const WeightsInfo &weights_info)
156{
157 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
158
159 ARM_COMPUTE_ERROR_THROW_ON(ClGemmConvolution::validate(src, weights, biases, dst,
160 conv2d_info,
161 weights_info));
162
163 const DataType data_type = src->data_type();
164 const DataLayout data_layout = src->data_layout();
165 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
166 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
167 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
168
169 const unsigned int kernel_width = weights->dimension(idx_width);
170 const unsigned int kernel_height = weights->dimension(idx_height);
171 const unsigned int num_kernels = weights->dimension(idx_kernels);
172
173 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
174 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
175
176 _is_prepared = weights_info.retain_internal_weights();
177 _is_quantized = is_data_type_quantized_asymmetric(src->data_type());
178 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv2d_info.conv_info.stride().first == 1 && conv2d_info.conv_info.stride().second == 1);
179 _skip_col2im = data_layout == DataLayout::NHWC;
180
181 // Only for quantize there are few cases where we cannot fuse the activation function in GEMM
182 _fuse_activation = true;
183
184 const ITensorInfo *gemm_input_to_use = src;
185 ITensorInfo *gemm_output_to_use = dst;
186
187 // Get parameters from conv_info
188 unsigned int stride_x = 0;
189 unsigned int stride_y = 0;
190 std::tie(stride_x, stride_y) = conv2d_info.conv_info.stride();
191
192 // Get convolved dimensions
193 unsigned int conv_w = 0;
194 unsigned int conv_h = 0;
195 std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
196 src->dimension(idx_height),
197 kernel_width,
198 kernel_height,
199 conv2d_info.conv_info,
200 conv2d_info.dilation);
201
202 unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups;
203
204 ITensorInfo *biases_to_use = biases;
205 _append_bias = false;
206
207 _weights_reshape_kernel = std::make_unique<kernels::ClWeightsReshapeKernel>();
208 if(conv2d_info.num_groups != 1 && biases != nullptr)
209 {
210 // num_groups != 1 can only be for NCHW
211 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
212 biases_to_use = nullptr;
213 _append_bias = true;
214 _weights_reshape_kernel->configure(compile_context, weights, biases, &_weights_reshaped, conv2d_info.num_groups);
215 }
216 else
217 {
218 _weights_reshape_kernel->configure(compile_context, weights, nullptr, &_weights_reshaped, conv2d_info.num_groups);
219 }
220
221 // Create tensor to store im2col reshaped inputs
222 if(!_skip_im2col)
223 {
224 // Configure and tune im2col. im2col output shape is auto-initialized
225 _im2col_kernel = std::make_unique<opencl::kernels::ClIm2ColKernel>();
226
227 // Set the GPU target for im2col
228 _im2col_kernel->set_target(CLScheduler::get().target());
229 _im2col_kernel->configure(compile_context, src, &_im2col_output, Size2D(kernel_width, kernel_height), conv2d_info.conv_info, _append_bias, conv2d_info.dilation, conv2d_info.num_groups);
230
231 // Set quantization info
232 _im2col_output.set_quantization_info(src->quantization_info());
233 CLScheduler::get().tune_kernel_static(*_im2col_kernel);
234
235 // Update GEMM input
236 gemm_input_to_use = &_im2col_output;
237 }
238
239 // Create GEMM output tensor
240 if(!_skip_col2im)
241 {
242 TensorShape shape_gemm;
243
244 // If we cannot skip col2im it means we run im2col as well
245 shape_gemm = _im2col_output.tensor_shape();
246 shape_gemm.set(0, mat_weights_cols);
247 shape_gemm.set(1, conv_w * conv_h);
248
249 _gemm_output = TensorInfo(shape_gemm, 1, data_type);
250 _gemm_output.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
251
252 // Update GEMM output
253 gemm_output_to_use = &_gemm_output;
254 }
255
256 GEMMLowpOutputStageInfo gemmlowp_output_stage;
257 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
258 gemmlowp_output_stage.gemmlowp_offset = 0;
259
260 // Configure output stage for quantized case
261 if(_is_quantized)
262 {
263 const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info;
264 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type());
265 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
266
267 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
268
269 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
270 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
271 quantization::compute_quantized_multipliers_and_shifts(src, weights, dst,
272 gemmlowp_output_stage.gemmlowp_multipliers.data(),
273 gemmlowp_output_stage.gemmlowp_shifts.data());
274 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
275 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
276
277 PixelValue min_val{};
278 PixelValue max_val{};
279 std::tie(min_val, max_val) = get_min_max(dst->data_type());
280
281 auto min_activation = min_val.get<int32_t>();
282 auto max_activation = max_val.get<int32_t>();
283
284 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
285 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
286 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
287 };
288
289 if(conv2d_info.act_info.enabled())
290 {
291 if(supported_acts.count(conv2d_info.act_info.activation()) != 0)
292 {
293 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info);
294 }
295 else
296 {
297 _fuse_activation = false;
298 }
299 }
300
301 // Set the GEMMLowp output stage info
302 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
303 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
304 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
305 }
306
307 // Configure and tune GEMM
308 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
309 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
310
311 configure_mm(compile_context, gemm_input_to_use, &_weights_reshaped, biases_to_use, gemm_output_to_use, gemmlowp_output_stage, gemm_3d_depth, conv2d_info.act_info);
312
313 if(!_skip_col2im)
314 {
315 // Set the GPU target for col2im
316 _col2im_kernel = std::make_unique<opencl::kernels::ClCol2ImKernel>();
317 _col2im_kernel->set_target(CLScheduler::get().target());
318 // Configure and tune Col2Im
319 _col2im_kernel->configure(compile_context, gemm_output_to_use, dst, Size2D(conv_w, conv_h), conv2d_info.num_groups);
320 CLScheduler::get().tune_kernel_static(*_col2im_kernel.get());
321 }
322
323 ARM_COMPUTE_ERROR_ON_MSG((dst->dimension(idx_width) != conv_w) || (dst->dimension(idx_height) != conv_h),
324 "Output shape does not match the expected one");
325
326 if(!_fuse_activation)
327 {
328 _activation_kernel = std::make_unique<opencl::kernels::ClActivationKernel>();
329 _activation_kernel->configure(compile_context, dst, nullptr, conv2d_info.act_info);
330 }
331
332 _aux_mem[Im2ColOutput] = MemoryInfo(offset_int_vec(Im2ColOutput), MemoryLifetime::Temporary, _im2col_output.total_size());
333 _aux_mem[WeightsReshaped] = MemoryInfo(offset_int_vec(WeightsReshaped), MemoryLifetime::Persistent, _weights_reshaped.total_size());
334 _aux_mem[GemmOutput] = MemoryInfo(offset_int_vec(GemmOutput), MemoryLifetime::Temporary, _gemm_output.total_size());
335}
336
337Status ClGemmConvolution::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const Conv2dInfo &conv2d_info,
338 const WeightsInfo &weights_info)
339{
340 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
341 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
342 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
343 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type());
344
345 if(!is_quantized_per_channel)
346 {
347 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
348 }
349 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights);
350 ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported");
351 ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_type() == DataType::QASYMM8), "Grouping (num_groups != 1) is not supported with QASYMM8");
352 ARM_COMPUTE_RETURN_ERROR_ON(((src->dimension(2) / weights->dimension(2)) != conv2d_info.num_groups) && (src->data_layout() == DataLayout::NCHW));
353
354 const DataLayout data_layout = src->data_layout();
355 const DataType data_type = src->data_type();
356 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
357 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
358 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
359 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
360
361 const unsigned int kernel_width = weights->dimension(idx_width);
362 const unsigned int kernel_height = weights->dimension(idx_height);
363 const unsigned int num_kernels = weights->dimension(idx_kernels);
364
365 TensorInfo im2col_reshaped_info{};
366 TensorInfo info_gemm{};
367 TensorInfo weights_reshaped_info{};
368 const ITensorInfo *gemm_input_to_use = src;
369 const ITensorInfo *gemm_output_to_use = dst;
370 const ITensorInfo *weights_to_use = weights;
371 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
372 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 && conv2d_info.conv_info.stride().first == 1
373 && conv2d_info.conv_info.stride().second == 1);
374 const bool skip_col2im = data_layout == DataLayout::NHWC;
375 bool fuse_activation = true;
376
377 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * conv2d_info.num_groups) != src->dimension(idx_channel));
378 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
379
380 // Validate biases
381 if(biases != nullptr)
382 {
383 if(is_quantized)
384 {
385 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
386 }
387 else
388 {
389 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
390 }
391 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
392 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
393 }
394
395 if(conv2d_info.act_info.enabled())
396 {
397 ARM_COMPUTE_ERROR_ON(conv2d_info.act_info.b() > conv2d_info.act_info.a());
398 }
399
400 // Get convolved dimensions
401 unsigned int conv_w = 0;
402 unsigned int conv_h = 0;
403
404 std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width),
405 src->dimension(idx_height),
406 kernel_width,
407 kernel_height,
408 conv2d_info.conv_info,
409 conv2d_info.dilation);
410
411 unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups;
412
413 const ITensorInfo *biases_to_use = biases;
414 bool append_bias = false;
415
416 if(conv2d_info.num_groups != 1 && biases != nullptr)
417 {
418 // num_groups != 1 can only be for NCHW
419 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
420 biases_to_use = nullptr;
421 append_bias = true;
422 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, true, conv2d_info.num_groups), 1, data_type);
423 }
424 else
425 {
426 weights_reshaped_info = TensorInfo(compute_weights_reshaped_shape(*weights, false, conv2d_info.num_groups), 1, data_type);
427 }
428
429 weights_to_use = &weights_reshaped_info;
430
431 if(!skip_im2col)
432 {
433 const Size2D kernel_dims(kernel_width, kernel_height);
434
435 // Output tensor auto initialization if not yet initialized
436 TensorShape expected_output_shape = compute_im2col_conv_shape(src, kernel_dims, conv2d_info.conv_info, append_bias, conv2d_info.dilation, conv2d_info.num_groups == 1, conv2d_info.num_groups);
437
438 auto_init_if_empty(im2col_reshaped_info, src->clone()->set_tensor_shape(expected_output_shape));
439
440 ARM_COMPUTE_RETURN_ON_ERROR(opencl::kernels::ClIm2ColKernel::validate(src, &im2col_reshaped_info, kernel_dims, conv2d_info.conv_info, append_bias, conv2d_info.dilation, conv2d_info.num_groups));
441 gemm_input_to_use = &im2col_reshaped_info;
442 }
443
444 // Create GEMM output tensor
445 if(!skip_col2im)
446 {
447 TensorShape shape_gemm;
448
449 shape_gemm = gemm_input_to_use->tensor_shape();
450 shape_gemm.set(0, mat_weights_cols);
451 shape_gemm.set(1, conv_w * conv_h);
452
453 info_gemm = TensorInfo(shape_gemm, 1, data_type);
454 info_gemm.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
455 gemm_output_to_use = &info_gemm;
456 }
457
458 GEMMLowpOutputStageInfo gemmlowp_output_stage;
459 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
460 gemmlowp_output_stage.gemmlowp_offset = 0;
461 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
462
463 if(is_quantized)
464 {
465 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
466 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
467 const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info;
468 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
469
470 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
471 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
472 quantization::compute_quantized_multipliers_and_shifts(src, weights, dst,
473 gemmlowp_output_stage.gemmlowp_multipliers.data(),
474 gemmlowp_output_stage.gemmlowp_shifts.data());
475 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
476 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
477
478 int min_activation = 0;
479 int max_activation = 0;
480
481 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = { ActivationLayerInfo::ActivationFunction::RELU,
482 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
483 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU
484 };
485
486 if(conv2d_info.act_info.enabled())
487 {
488 if(supported_acts.count(conv2d_info.act_info.activation()) != 0)
489 {
490 std::tie(min_activation, max_activation) = get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info);
491 }
492 else
493 {
494 fuse_activation = false;
495 }
496 }
497
498 // Set the GEMMLowp output stage info
499 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
500 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
501 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
502 }
503
504 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
505 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
506
507 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, conv2d_info.act_info));
508
509 // Validate Col2Im
510 if(!skip_col2im)
511 {
512 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClCol2ImKernel::validate(gemm_output_to_use, dst, Size2D(conv_w, conv_h), conv2d_info.num_groups));
513 }
514
515 //Validate Activation Layer
516 if(!fuse_activation)
517 {
518 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClActivationKernel::validate(dst, nullptr, conv2d_info.act_info));
519 }
520
521 return Status{};
522}
523
524void ClGemmConvolution::run(ITensorPack &tensors)
525{
526 prepare(tensors);
527
528 auto src = tensors.get_const_tensor(ACL_SRC_0);
529 auto biases = tensors.get_const_tensor(ACL_SRC_2);
530 auto dst = tensors.get_tensor(ACL_DST);
531 auto gemm_input_to_use = src;
532 auto gemm_output_to_use = dst;
533
534 CLAuxTensorHandler im2col_output(offset_int_vec(Im2ColOutput), _im2col_output, tensors, false);
535 CLAuxTensorHandler gemm_output(offset_int_vec(GemmOutput), _gemm_output, tensors, false);
536 CLAuxTensorHandler weights_reshaped(offset_int_vec(WeightsReshaped), _weights_reshaped, tensors, false);
537
538 // Run im2col
539 if(!_skip_im2col)
540 {
541 ITensorPack pack =
542 {
543 { TensorType::ACL_SRC, src },
544 { TensorType::ACL_DST, im2col_output.get() }
545 };
546 CLScheduler::get().enqueue_op(*_im2col_kernel, pack, false);
547 gemm_input_to_use = im2col_output.get();
548 }
549 if(!_skip_col2im)
550 {
551 gemm_output_to_use = gemm_output.get();
552 }
553 ITensorPack pack_mm = tensors;
554 pack_mm.add_const_tensor(TensorType::ACL_SRC_0, gemm_input_to_use);
555 pack_mm.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get());
556 if(!_append_bias)
557 {
558 pack_mm.add_const_tensor(TensorType::ACL_SRC_2, biases);
559 }
560 pack_mm.add_tensor(TensorType::ACL_DST, gemm_output_to_use);
561 // Runs ClGemm or ClGemmLowpMatrixMultiplyCore functions
562 if(_is_quantized)
563 {
564 // Run gemmlowp
565 _mm_gemmlowp->run(pack_mm);
566 }
567 else
568 {
569 // Run gemm
570 _mm_gemm->run(pack_mm);
571 }
572
573 // Reshape output matrix
574 if(!_skip_col2im)
575 {
576 ITensorPack pack =
577 {
578 { TensorType::ACL_SRC, gemm_output_to_use },
579 { TensorType::ACL_DST, dst }
580 };
581 CLScheduler::get().enqueue_op(*_col2im_kernel.get(), pack, false);
582 }
583
584 //Run Activation Layer if we cannot fuse in GEMM
585 if(!_fuse_activation)
586 {
587 ITensorPack pack =
588 {
589 { TensorType::ACL_SRC, dst },
590 { TensorType::ACL_DST, dst }
591 };
592 CLScheduler::get().enqueue_op(*_activation_kernel.get(), pack, false);
593 }
594}
595
596void ClGemmConvolution::prepare(ITensorPack &tensors)
597{
598 if(!_is_prepared)
599 {
600 // Run weights reshaping and mark original weights tensor as unused
601 ICLTensor *weights_reshaped_p = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(offset_int_vec(WeightsReshaped)));
602 CLAuxTensorHandler weights_reshaped(_weights_reshaped, *weights_reshaped_p);
603 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
604 ITensorPack pack =
605 {
606 { TensorType::ACL_SRC, weights },
607 { TensorType::ACL_DST, weights_reshaped.get() }
608 };
609
610 if(_append_bias)
611 {
612 const auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2);
613 pack.add_const_tensor(TensorType::ACL_BIAS, biases);
614 }
615 CLScheduler::get().enqueue_op(*_weights_reshape_kernel.get(), pack, true);
616 tensors.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get());
617
618 // Prepare GEMM
619 _is_quantized ? _mm_gemmlowp->prepare(tensors) : _mm_gemm->prepare(tensors);
620 _is_prepared = true;
621 }
622}
623experimental::MemoryRequirements ClGemmConvolution::workspace() const
624{
625 return _aux_mem;
626}
627} // namespace opencl
628} // namespace arm_compute