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