blob: 55d815a1ef7cef18e7b40511c45a6550ae672a3c [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"
Manuel Bottinid87aded2021-07-16 10:23:31 +010031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "arm_compute/core/Validate.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010034#include "arm_compute/runtime/CL/CLScheduler.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035
36#include "src/common/utils/Log.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010039#include "src/gpu/cl/kernels/ClActivationKernel.h"
40#include "src/gpu/cl/kernels/ClCol2ImKernel.h"
41#include "src/gpu/cl/kernels/ClIm2ColKernel.h"
42#include "src/gpu/cl/kernels/ClWeightsReshapeKernel.h"
43#include "src/gpu/cl/operators/ClGemm.h"
44#include "src/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h"
45#include "src/gpu/cl/utils/ClAuxTensorHandler.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()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 : _weights_reshape_kernel(nullptr),
57 _im2col_kernel(nullptr),
58 _mm_gemm(nullptr),
59 _mm_gemmlowp(nullptr),
60 _col2im_kernel(nullptr),
61 _activation_kernel(nullptr),
62 _im2col_output(),
63 _weights_reshaped(),
64 _gemm_output(),
65 _skip_im2col(false),
66 _skip_col2im(false),
67 _is_quantized(false),
68 _fuse_activation(true),
69 _append_bias(false),
70 _is_prepared(false),
71 _aux_mem(AuxTensorIdx::Count)
Manuel Bottinid87aded2021-07-16 10:23:31 +010072{
73}
Georgios Pinitas19884632021-08-16 12:38:54 +010074ClGemmConv2d::~ClGemmConv2d() = default;
Manuel Bottinid87aded2021-07-16 10:23:31 +010075
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076void ClGemmConv2d::configure_mm(const ClCompileContext &compile_context,
77 const ITensorInfo *src,
78 ITensorInfo *weights,
79 ITensorInfo *biases,
80 ITensorInfo *dst,
Georgios Pinitas19884632021-08-16 12:38:54 +010081 const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 int gemm_3d_depth,
83 const ActivationLayerInfo &act_info)
Manuel Bottinid87aded2021-07-16 10:23:31 +010084{
85 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 ARM_COMPUTE_ERROR_THROW_ON(
87 validate_mm(src, weights, biases, dst, gemmlowp_output_stage, gemm_3d_depth, _skip_im2col, act_info));
Manuel Bottinid87aded2021-07-16 10:23:31 +010088
89 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
90 false, // is_b_reshaped
91 true, // reshape_b_only_on_first_run
92 gemm_3d_depth, // depth_output_gemm3d
93 _skip_im2col, // reinterpret_input_as_3d
94 false, // retain_internal_weights
95 gemmlowp_output_stage, // gemmlowp_output_stage
96 false, // fast_math
97 false, // fp_mixed_precision
98 true, // broadcast_bias
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010099 act_info // activation_info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 );
Manuel Bottinid87aded2021-07-16 10:23:31 +0100101
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 TensorInfo tmp_src{*src};
103 if (_is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100104 {
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 = src->quantization_info();
108 const QuantizationInfo weights_quantization_info = weights->quantization_info();
109
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 tmp_src.set_quantization_info(
111 QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
112 weights->set_quantization_info(
113 QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100114
115 _mm_gemmlowp = std::make_unique<ClGemmLowpMatrixMultiplyCore>();
116 _mm_gemmlowp->configure(compile_context, &tmp_src, weights, biases, dst, gemm_info);
117
118 // Revert back QuantizatioInfo as weights could be used in other convolution layers
119 weights->set_quantization_info(weights_quantization_info);
120
121 auto mm_mem_req = _mm_gemmlowp->workspace();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 for (unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100123 {
124 _aux_mem[cont] = mm_mem_req[cont];
125 }
126 }
127 else
128 {
129 // Configure matrix multiply function
130 _mm_gemm = std::make_unique<ClGemm>();
131 _mm_gemm->configure(compile_context, &tmp_src, weights, biases, dst, 1.0f, 1.0f, gemm_info);
132 auto mm_mem_req = _mm_gemm->workspace();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 for (unsigned int cont = 0; cont < mm_mem_req.size(); ++cont)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100134 {
135 _aux_mem[cont] = mm_mem_req[cont];
136 }
137 }
138}
139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140Status ClGemmConv2d::validate_mm(const ITensorInfo *src,
141 const ITensorInfo *weights,
142 const ITensorInfo *biases,
143 const ITensorInfo *dst,
144 const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
145 int gemm_3d_depth,
146 bool skip_im2col,
147 const ActivationLayerInfo &act_info)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100148{
149 const bool is_quantized = is_data_type_quantized_asymmetric(src->data_type());
150
151 const GEMMInfo &gemm_info = GEMMInfo(false, // is_a_reshaped
152 false, // is_b_reshaped
153 true, // reshape_b_only_on_first_run
154 gemm_3d_depth, // depth_output_gemm3d
155 skip_im2col, // reinterpret_input_as_3d
156 false, // retain_internal_weights
157 gemmlowp_output_stage, // gemmlowp_output_stage
158 false, // fast_math
159 false, // fp_mixed_precision
160 true, // broadcast_bias
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100161 act_info // activation_info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 );
Manuel Bottinid87aded2021-07-16 10:23:31 +0100163
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 if (is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100165 {
166 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
167 // Extract and negate input and weights offset
168 const QuantizationInfo input_quantization_info = src->quantization_info();
169 const QuantizationInfo weights_quantization_info = weights->quantization_info();
170
171 std::unique_ptr<ITensorInfo> src_qa = src->clone();
172 std::unique_ptr<ITensorInfo> weights_qa = weights->clone();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100173 src_qa->set_quantization_info(
174 QuantizationInfo(input_quantization_info.uniform().scale, -input_quantization_info.uniform().offset));
175 weights_qa->set_quantization_info(
176 QuantizationInfo(weights_quantization_info.uniform().scale, -weights_quantization_info.uniform().offset));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100177
178 // Perform validation step on GEMMLowp
179 return ClGemmLowpMatrixMultiplyCore::validate(src_qa.get(), weights_qa.get(), biases, dst, gemm_info);
180 }
181 else
182 {
183 // Perform validation step on Matrix multiply function
184 return ClGemm::validate(src, weights, biases, dst, 1.0f, 1.0f, gemm_info);
185 }
186}
187
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188void ClGemmConv2d::configure(const CLCompileContext &compile_context,
189 ITensorInfo *src,
190 ITensorInfo *weights,
191 ITensorInfo *biases,
192 ITensorInfo *dst,
193 const Conv2dInfo &conv2d_info,
194 const WeightsInfo &weights_info)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100195{
196 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
197
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 ARM_COMPUTE_ERROR_THROW_ON(ClGemmConv2d::validate(src, weights, biases, dst, conv2d_info, weights_info));
ramelg012e53f172021-09-22 10:48:25 +0100199 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv2d_info, weights_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100200
201 const DataType data_type = src->data_type();
202 const DataLayout data_layout = src->data_layout();
203 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
204 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
205 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
206
207 const unsigned int kernel_width = weights->dimension(idx_width);
208 const unsigned int kernel_height = weights->dimension(idx_height);
209 const unsigned int num_kernels = weights->dimension(idx_kernels);
210
211 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
212 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
213
214 _is_prepared = weights_info.retain_internal_weights();
215 _is_quantized = is_data_type_quantized_asymmetric(src->data_type());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 _skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 &&
217 conv2d_info.conv_info.stride().first == 1 && conv2d_info.conv_info.stride().second == 1);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100218 _skip_col2im = data_layout == DataLayout::NHWC;
219
220 // Only for quantize there are few cases where we cannot fuse the activation function in GEMM
221 _fuse_activation = true;
222
223 const ITensorInfo *gemm_input_to_use = src;
224 ITensorInfo *gemm_output_to_use = dst;
225
226 // Get parameters from conv_info
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100227 unsigned int stride_x = 0;
228 unsigned int stride_y = 0;
Manuel Bottinid87aded2021-07-16 10:23:31 +0100229 std::tie(stride_x, stride_y) = conv2d_info.conv_info.stride();
230
231 // Get convolved dimensions
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100232 unsigned int conv_w = 0;
233 unsigned int conv_h = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width), src->dimension(idx_height), kernel_width,
235 kernel_height, conv2d_info.conv_info, conv2d_info.dilation);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100236
237 unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups;
238
239 ITensorInfo *biases_to_use = biases;
240 _append_bias = false;
241
242 _weights_reshape_kernel = std::make_unique<kernels::ClWeightsReshapeKernel>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 if (conv2d_info.num_groups != 1 && biases != nullptr)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100244 {
245 // num_groups != 1 can only be for NCHW
246 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
247 biases_to_use = nullptr;
248 _append_bias = true;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100249 _weights_reshape_kernel->configure(compile_context, weights, biases, &_weights_reshaped,
250 conv2d_info.num_groups);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100251 }
252 else
253 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 _weights_reshape_kernel->configure(compile_context, weights, nullptr, &_weights_reshaped,
255 conv2d_info.num_groups);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100256 }
257
258 // Create tensor to store im2col reshaped inputs
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 if (!_skip_im2col)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100260 {
261 // Configure and tune im2col. im2col output shape is auto-initialized
262 _im2col_kernel = std::make_unique<opencl::kernels::ClIm2ColKernel>();
263
264 // Set the GPU target for im2col
265 _im2col_kernel->set_target(CLScheduler::get().target());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100266 _im2col_kernel->configure(compile_context, src, &_im2col_output, Size2D(kernel_width, kernel_height),
267 conv2d_info.conv_info, _append_bias, conv2d_info.dilation, conv2d_info.num_groups);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100268
269 // Set quantization info
270 _im2col_output.set_quantization_info(src->quantization_info());
271 CLScheduler::get().tune_kernel_static(*_im2col_kernel);
272
273 // Update GEMM input
274 gemm_input_to_use = &_im2col_output;
275 }
276
277 // Create GEMM output tensor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100278 if (!_skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100279 {
280 TensorShape shape_gemm;
281
282 // If we cannot skip col2im it means we run im2col as well
283 shape_gemm = _im2col_output.tensor_shape();
284 shape_gemm.set(0, mat_weights_cols);
285 shape_gemm.set(1, conv_w * conv_h);
286
287 _gemm_output = TensorInfo(shape_gemm, 1, data_type);
288 _gemm_output.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
289
290 // Update GEMM output
291 gemm_output_to_use = &_gemm_output;
292 }
293
294 GEMMLowpOutputStageInfo gemmlowp_output_stage;
295 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
296 gemmlowp_output_stage.gemmlowp_offset = 0;
297
298 // Configure output stage for quantized case
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100299 if (_is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100300 {
301 const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info;
302 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type());
303 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
304
305 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
306
307 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
308 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
309 quantization::compute_quantized_multipliers_and_shifts(src, weights, dst,
310 gemmlowp_output_stage.gemmlowp_multipliers.data(),
311 gemmlowp_output_stage.gemmlowp_shifts.data());
312 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
313 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
314
315 PixelValue min_val{};
316 PixelValue max_val{};
317 std::tie(min_val, max_val) = get_min_max(dst->data_type());
318
319 auto min_activation = min_val.get<int32_t>();
320 auto max_activation = max_val.get<int32_t>();
321
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100322 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = {
323 ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
324 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100325
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100326 if (conv2d_info.act_info.enabled())
Manuel Bottinid87aded2021-07-16 10:23:31 +0100327 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100328 if (supported_acts.count(conv2d_info.act_info.activation()) != 0)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100329 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 std::tie(min_activation, max_activation) =
331 get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100332 }
333 else
334 {
335 _fuse_activation = false;
336 }
337 }
338
339 // Set the GEMMLowp output stage info
340 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
341 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
342 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
343 }
344
345 // Configure and tune GEMM
346 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
347 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
348
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100349 configure_mm(compile_context, gemm_input_to_use, &_weights_reshaped, biases_to_use, gemm_output_to_use,
350 gemmlowp_output_stage, gemm_3d_depth, conv2d_info.act_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100351
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 if (!_skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100353 {
354 // Set the GPU target for col2im
355 _col2im_kernel = std::make_unique<opencl::kernels::ClCol2ImKernel>();
356 _col2im_kernel->set_target(CLScheduler::get().target());
357 // Configure and tune Col2Im
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100358 _col2im_kernel->configure(compile_context, gemm_output_to_use, dst, Size2D(conv_w, conv_h),
359 conv2d_info.num_groups);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100360 CLScheduler::get().tune_kernel_static(*_col2im_kernel.get());
361 }
362
363 ARM_COMPUTE_ERROR_ON_MSG((dst->dimension(idx_width) != conv_w) || (dst->dimension(idx_height) != conv_h),
364 "Output shape does not match the expected one");
365
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100366 if (!_fuse_activation)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100367 {
368 _activation_kernel = std::make_unique<opencl::kernels::ClActivationKernel>();
369 _activation_kernel->configure(compile_context, dst, nullptr, conv2d_info.act_info);
370 }
371
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100372 _aux_mem[Im2ColOutput] =
373 MemoryInfo(offset_int_vec(Im2ColOutput), MemoryLifetime::Temporary, _im2col_output.total_size());
374 _aux_mem[WeightsReshaped] =
375 MemoryInfo(offset_int_vec(WeightsReshaped), MemoryLifetime::Persistent, _weights_reshaped.total_size());
376 _aux_mem[GemmOutput] = MemoryInfo(offset_int_vec(GemmOutput), MemoryLifetime::Temporary, _gemm_output.total_size());
Manuel Bottinid87aded2021-07-16 10:23:31 +0100377}
378
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100379Status ClGemmConv2d::validate(const ITensorInfo *src,
380 const ITensorInfo *weights,
381 const ITensorInfo *biases,
382 const ITensorInfo *dst,
383 const Conv2dInfo &conv2d_info,
Georgios Pinitas19884632021-08-16 12:38:54 +0100384 const WeightsInfo &weights_info)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100385{
386 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, weights, dst);
387 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100388 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
389 DataType::F16, DataType::F32);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100390 const bool is_quantized_per_channel = is_data_type_quantized_per_channel(weights->data_type());
391
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100392 if (!is_quantized_per_channel)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100393 {
394 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
395 }
396 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100397 ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_layout() != DataLayout::NCHW),
398 "Grouping (num_groups != 1) with NHWC data layout is not supported");
399 ARM_COMPUTE_RETURN_ERROR_ON_MSG((conv2d_info.num_groups != 1) && (src->data_type() == DataType::QASYMM8),
400 "Grouping (num_groups != 1) is not supported with QASYMM8");
401 ARM_COMPUTE_RETURN_ERROR_ON(((src->dimension(2) / weights->dimension(2)) != conv2d_info.num_groups) &&
402 (src->data_layout() == DataLayout::NCHW));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100403
404 const DataLayout data_layout = src->data_layout();
405 const DataType data_type = src->data_type();
406 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
407 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
408 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
409 const int idx_kernels = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
410
411 const unsigned int kernel_width = weights->dimension(idx_width);
412 const unsigned int kernel_height = weights->dimension(idx_height);
413 const unsigned int num_kernels = weights->dimension(idx_kernels);
414
415 TensorInfo im2col_reshaped_info{};
416 TensorInfo info_gemm{};
417 TensorInfo weights_reshaped_info{};
418 const ITensorInfo *gemm_input_to_use = src;
419 const ITensorInfo *gemm_output_to_use = dst;
420 const ITensorInfo *weights_to_use = weights;
421 const bool is_quantized = is_data_type_quantized_asymmetric(data_type);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100422 const bool skip_im2col = (data_layout == DataLayout::NHWC && kernel_width == 1 && kernel_height == 1 &&
423 conv2d_info.conv_info.stride().first == 1 && conv2d_info.conv_info.stride().second == 1);
424 const bool skip_col2im = data_layout == DataLayout::NHWC;
425 bool fuse_activation = true;
Manuel Bottinid87aded2021-07-16 10:23:31 +0100426
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100427 ARM_COMPUTE_RETURN_ERROR_ON((weights->dimension(idx_channel) * conv2d_info.num_groups) !=
428 src->dimension(idx_channel));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100429 ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 4);
430
431 // Validate biases
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100432 if (biases != nullptr)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100433 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100434 if (is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100435 {
436 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
437 }
438 else
439 {
440 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, biases);
441 }
442 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(idx_kernels));
443 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
444 }
445
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100446 if (conv2d_info.act_info.enabled())
Manuel Bottinid87aded2021-07-16 10:23:31 +0100447 {
448 ARM_COMPUTE_ERROR_ON(conv2d_info.act_info.b() > conv2d_info.act_info.a());
449 }
450
451 // Get convolved dimensions
452 unsigned int conv_w = 0;
453 unsigned int conv_h = 0;
454
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100455 std::tie(conv_w, conv_h) = scaled_dimensions(src->dimension(idx_width), src->dimension(idx_height), kernel_width,
456 kernel_height, conv2d_info.conv_info, conv2d_info.dilation);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100457
458 unsigned int mat_weights_cols = num_kernels / conv2d_info.num_groups;
459
460 const ITensorInfo *biases_to_use = biases;
461 bool append_bias = false;
462
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100463 if (conv2d_info.num_groups != 1 && biases != nullptr)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100464 {
465 // num_groups != 1 can only be for NCHW
466 // Since it is missing an utility function to reshape the biases, we append the biases into the weights tensor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100467 biases_to_use = nullptr;
468 append_bias = true;
469 weights_reshaped_info =
470 TensorInfo(compute_weights_reshaped_shape(*weights, true, conv2d_info.num_groups), 1, data_type);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100471 }
472 else
473 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100474 weights_reshaped_info =
475 TensorInfo(compute_weights_reshaped_shape(*weights, false, conv2d_info.num_groups), 1, data_type);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100476 }
477
478 weights_to_use = &weights_reshaped_info;
479
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100480 if (!skip_im2col)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100481 {
482 const Size2D kernel_dims(kernel_width, kernel_height);
483
484 // Output tensor auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100485 TensorShape expected_output_shape =
486 compute_im2col_conv_shape(src, kernel_dims, conv2d_info.conv_info, append_bias, conv2d_info.dilation,
487 conv2d_info.num_groups == 1, conv2d_info.num_groups);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100488
489 auto_init_if_empty(im2col_reshaped_info, src->clone()->set_tensor_shape(expected_output_shape));
490
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100491 ARM_COMPUTE_RETURN_ON_ERROR(
492 opencl::kernels::ClIm2ColKernel::validate(src, &im2col_reshaped_info, kernel_dims, conv2d_info.conv_info,
493 append_bias, conv2d_info.dilation, conv2d_info.num_groups));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100494 gemm_input_to_use = &im2col_reshaped_info;
495 }
496
497 // Create GEMM output tensor
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100498 if (!skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100499 {
500 TensorShape shape_gemm;
501
502 shape_gemm = gemm_input_to_use->tensor_shape();
503 shape_gemm.set(0, mat_weights_cols);
504 shape_gemm.set(1, conv_w * conv_h);
505
506 info_gemm = TensorInfo(shape_gemm, 1, data_type);
507 info_gemm.set_quantization_info(dst->quantization_info()).set_data_layout(src->data_layout());
508 gemm_output_to_use = &info_gemm;
509 }
510
511 GEMMLowpOutputStageInfo gemmlowp_output_stage;
512 gemmlowp_output_stage.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT;
513 gemmlowp_output_stage.gemmlowp_offset = 0;
514 gemmlowp_output_stage.is_quantized_per_channel = is_quantized_per_channel;
515
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100516 if (is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100517 {
518 const UniformQuantizationInfo iq_info = src->quantization_info().uniform();
519 const UniformQuantizationInfo oq_info = dst->quantization_info().uniform();
520 const auto output_quant_info = (dst->total_size() == 0) ? iq_info : oq_info;
521 const unsigned int num_filters = (is_quantized_per_channel) ? num_kernels : 1;
522
523 gemmlowp_output_stage.gemmlowp_multipliers.resize(num_filters);
524 gemmlowp_output_stage.gemmlowp_shifts.resize(num_filters);
525 quantization::compute_quantized_multipliers_and_shifts(src, weights, dst,
526 gemmlowp_output_stage.gemmlowp_multipliers.data(),
527 gemmlowp_output_stage.gemmlowp_shifts.data());
528 gemmlowp_output_stage.gemmlowp_multiplier = gemmlowp_output_stage.gemmlowp_multipliers[0];
529 gemmlowp_output_stage.gemmlowp_shift = gemmlowp_output_stage.gemmlowp_shifts[0];
530
531 int min_activation = 0;
532 int max_activation = 0;
533
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100534 const std::set<ActivationLayerInfo::ActivationFunction> supported_acts = {
535 ActivationLayerInfo::ActivationFunction::RELU, ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
536 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100537
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100538 if (conv2d_info.act_info.enabled())
Manuel Bottinid87aded2021-07-16 10:23:31 +0100539 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100540 if (supported_acts.count(conv2d_info.act_info.activation()) != 0)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100541 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100542 std::tie(min_activation, max_activation) =
543 get_quantized_activation_min_max(conv2d_info.act_info, data_type, output_quant_info);
Manuel Bottinid87aded2021-07-16 10:23:31 +0100544 }
545 else
546 {
547 fuse_activation = false;
548 }
549 }
550
551 // Set the GEMMLowp output stage info
552 gemmlowp_output_stage.gemmlowp_offset = output_quant_info.offset;
553 gemmlowp_output_stage.gemmlowp_min_bound = min_activation;
554 gemmlowp_output_stage.gemmlowp_max_bound = max_activation;
555 }
556
557 // In case of NHWC, we need to run GEMM3D (gemm_3d_depth != 0) in order to avoid reshaping the output matrix
558 const unsigned int gemm_3d_depth = (data_layout == DataLayout::NHWC) ? conv_h : 0;
559
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100560 ARM_COMPUTE_RETURN_ON_ERROR(validate_mm(gemm_input_to_use, weights_to_use, biases_to_use, gemm_output_to_use,
561 gemmlowp_output_stage, gemm_3d_depth, skip_im2col, conv2d_info.act_info));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100562
563 // Validate Col2Im
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100564 if (!skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100565 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100566 ARM_COMPUTE_RETURN_ON_ERROR(
567 kernels::ClCol2ImKernel::validate(gemm_output_to_use, dst, Size2D(conv_w, conv_h), conv2d_info.num_groups));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100568 }
569
SiCongLi579ca842021-10-18 09:38:33 +0100570 // Validate Activation Layer
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100571 if (!fuse_activation)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100572 {
573 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClActivationKernel::validate(dst, nullptr, conv2d_info.act_info));
574 }
575
576 return Status{};
577}
578
Georgios Pinitas19884632021-08-16 12:38:54 +0100579void ClGemmConv2d::run(ITensorPack &tensors)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100580{
581 prepare(tensors);
582
583 auto src = tensors.get_const_tensor(ACL_SRC_0);
584 auto biases = tensors.get_const_tensor(ACL_SRC_2);
585 auto dst = tensors.get_tensor(ACL_DST);
586 auto gemm_input_to_use = src;
587 auto gemm_output_to_use = dst;
588
589 CLAuxTensorHandler im2col_output(offset_int_vec(Im2ColOutput), _im2col_output, tensors, false);
590 CLAuxTensorHandler gemm_output(offset_int_vec(GemmOutput), _gemm_output, tensors, false);
591 CLAuxTensorHandler weights_reshaped(offset_int_vec(WeightsReshaped), _weights_reshaped, tensors, false);
592
593 // Run im2col
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100594 if (!_skip_im2col)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100595 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100596 ITensorPack pack = {{TensorType::ACL_SRC, src}, {TensorType::ACL_DST, im2col_output.get()}};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100597 CLScheduler::get().enqueue_op(*_im2col_kernel, pack, false);
598 gemm_input_to_use = im2col_output.get();
599 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100600 if (!_skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100601 {
602 gemm_output_to_use = gemm_output.get();
603 }
604 ITensorPack pack_mm = tensors;
605 pack_mm.add_const_tensor(TensorType::ACL_SRC_0, gemm_input_to_use);
606 pack_mm.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100607 if (!_append_bias)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100608 {
609 pack_mm.add_const_tensor(TensorType::ACL_SRC_2, biases);
610 }
611 pack_mm.add_tensor(TensorType::ACL_DST, gemm_output_to_use);
612 // Runs ClGemm or ClGemmLowpMatrixMultiplyCore functions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100613 if (_is_quantized)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100614 {
615 // Run gemmlowp
616 _mm_gemmlowp->run(pack_mm);
617 }
618 else
619 {
620 // Run gemm
621 _mm_gemm->run(pack_mm);
622 }
623
624 // Reshape output matrix
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100625 if (!_skip_col2im)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100626 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100627 ITensorPack pack = {{TensorType::ACL_SRC, gemm_output_to_use}, {TensorType::ACL_DST, dst}};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100628 CLScheduler::get().enqueue_op(*_col2im_kernel.get(), pack, false);
629 }
630
631 //Run Activation Layer if we cannot fuse in GEMM
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100632 if (!_fuse_activation)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100633 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100634 ITensorPack pack = {{TensorType::ACL_SRC, dst}, {TensorType::ACL_DST, dst}};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100635 CLScheduler::get().enqueue_op(*_activation_kernel.get(), pack, false);
636 }
637}
638
Georgios Pinitas19884632021-08-16 12:38:54 +0100639void ClGemmConv2d::prepare(ITensorPack &tensors)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100640{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100641 if (!_is_prepared)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100642 {
643 // Run weights reshaping and mark original weights tensor as unused
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100644 ICLTensor *weights_reshaped_p =
645 utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(offset_int_vec(WeightsReshaped)));
Manuel Bottinid87aded2021-07-16 10:23:31 +0100646 CLAuxTensorHandler weights_reshaped(_weights_reshaped, *weights_reshaped_p);
647 auto weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100648 ITensorPack pack = {{TensorType::ACL_SRC, weights}, {TensorType::ACL_DST, weights_reshaped.get()}};
Manuel Bottinid87aded2021-07-16 10:23:31 +0100649
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100650 if (_append_bias)
Manuel Bottinid87aded2021-07-16 10:23:31 +0100651 {
652 const auto biases = tensors.get_const_tensor(TensorType::ACL_SRC_2);
653 pack.add_const_tensor(TensorType::ACL_BIAS, biases);
654 }
655 CLScheduler::get().enqueue_op(*_weights_reshape_kernel.get(), pack, true);
656 tensors.add_const_tensor(TensorType::ACL_SRC_1, weights_reshaped.get());
657
658 // Prepare GEMM
659 _is_quantized ? _mm_gemmlowp->prepare(tensors) : _mm_gemm->prepare(tensors);
660 _is_prepared = true;
661 }
662}
Georgios Pinitas19884632021-08-16 12:38:54 +0100663experimental::MemoryRequirements ClGemmConv2d::workspace() const
Manuel Bottinid87aded2021-07-16 10:23:31 +0100664{
665 return _aux_mem;
666}
667} // namespace opencl
668} // namespace arm_compute