blob: 7cf1958c1ba1867c6ed95e599224cd8c003f0c27 [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2017-2023 Arm Limited.
steniu0127b386c2017-07-18 17:37:43 +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/kernels/ClDirectConv2dKernel.h"
steniu0127b386c2017-07-18 17:37:43 +010025
steniu0127b386c2017-07-18 17:37:43 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLTensor.h"
steniu0127b386c2017-07-18 17:37:43 +010028#include "arm_compute/core/Helpers.h"
steniu0127b386c2017-07-18 17:37:43 +010029#include "arm_compute/core/ITensor.h"
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +010030#include "arm_compute/core/KernelDescriptors.h"
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +000031#include "arm_compute/core/PixelValue.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000033#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Chunosovd621bca2017-11-03 17:33:15 +070035#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000036#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010037
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "src/core/AccessWindowStatic.h"
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +010039#include "src/core/CL/CLUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010040#include "src/core/CL/CLValidate.h"
41#include "src/core/helpers/AutoConfiguration.h"
42#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010043#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Sheri Zhang1efed922021-03-10 22:43:38 +000044#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000045#include "support/StringSupport.h"
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +010046
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010047namespace arm_compute
48{
Sheri Zhang1efed922021-03-10 22:43:38 +000049namespace opencl
50{
51namespace kernels
52{
Georgios Pinitas30902ed2017-11-14 15:32:57 +000053namespace
54{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055Status validate_arguments(const ITensorInfo *src,
56 const ITensorInfo *weights,
57 const ITensorInfo *biases,
58 const ITensorInfo *dst,
59 const PadStrideInfo &conv_info,
60 const ActivationLayerInfo &act_info,
61 const DirectConvComputeKernelInfo &desc)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000062{
Sheri Zhang1efed922021-03-10 22:43:38 +000063 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8,
65 DataType::F16, DataType::F32);
Sheri Zhang1efed922021-03-10 22:43:38 +000066 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
Pablo Tello3d319462018-06-21 15:13:17 +010067
Sheri Zhang1efed922021-03-10 22:43:38 +000068 const DataLayout data_layout = src->data_layout();
Pablo Tello3d319462018-06-21 15:13:17 +010069 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
70 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
71 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
72
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != src->dimension(channel_idx),
74 "Weights feature map dimension should match the respective src's one");
Pablo Tello3d319462018-06-21 15:13:17 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000076
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_input_to_cl_image == true,
78 "Export to CLImage is not supported for the input tensor");
79 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_output_to_cl_image == true,
80 "Export to CLImage is not supported for the output tensor");
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +000081
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 if (data_layout == DataLayout::NCHW)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000083 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx),
85 "Weights should have same width and height");
86 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 1) && std::get<0>(conv_info.stride()) > 3,
87 "Strides larger than 3 not supported for 1x1 convolution.");
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 3 || weights->dimension(width_idx) == 5 ||
89 weights->dimension(width_idx) == 9) &&
90 std::get<0>(conv_info.stride()) > 2,
Giorgio Arena945ae9e2021-10-13 11:13:04 +010091 "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
SiCong Li1b2f8682023-01-04 10:04:26 +000092 ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled(), "Fused activation is not supported for NCHW layout");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010093
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (is_data_type_quantized(src->data_type()))
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000095 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
97 weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 &&
98 weights->dimension(width_idx) != 5 && weights->dimension(width_idx) != 9,
99 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000100 }
101 else
102 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 ARM_COMPUTE_RETURN_ERROR_ON_MSG(
104 weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 &&
105 weights->dimension(width_idx) != 5,
106 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000107 }
108 }
109
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 if (data_layout == DataLayout::NHWC)
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100111 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled() && !is_data_type_float(src->data_type()),
113 "Fused activation in NHWC is only supported for floating point.");
114 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8,
115 "M0 can only be greater than 0 and less than or equal to 8");
116 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 &&
117 desc.n0 != 16,
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100118 "N0 can only be: 1, 2, 3, 4, 8, and 16");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 &&
120 desc.k0 != 16,
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100121 "K0 can only be: 1, 2, 3, 4, 8, and 16");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 if (desc.export_weights_to_cl_image)
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100123 {
124 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16,
125 "K0 can only be: 4, 8, and 16");
Gian Marco Iodicead9a7ed2022-09-16 14:14:21 +0100126 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(weights),
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100127 "Export to CLImage is not supported for this weight configuration");
128 }
129 }
130
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131 if (biases != nullptr)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000132 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 if (is_data_type_quantized_asymmetric(src->data_type()))
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000134 {
135 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
136 }
137 else
138 {
139 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
140 }
141 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100142 "Biases size and number of dst feature maps should match");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, "Biases should be one dimensional");
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000144 }
145
Sheri Zhang1efed922021-03-10 22:43:38 +0000146 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 if (dst->total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000148 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(
150 dst->tensor_shape(), misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info));
Sheri Zhang1efed922021-03-10 22:43:38 +0000151 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000152 }
153
Sheri Zhang1efed922021-03-10 22:43:38 +0000154 const auto data_type = src->data_type();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 if (is_data_type_quantized(data_type))
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100156 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000157 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100158 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
Sheri Zhang1efed922021-03-10 22:43:38 +0000159 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100160
161 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
162 int output_multiplier = 0;
163 int output_shift = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 ARM_COMPUTE_RETURN_ON_ERROR(
165 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100166 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000167 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000168}
Giorgio Arena59486342017-12-01 10:42:47 +0000169} // namespace
170
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100171ClDirectConv2dKernel::ClDirectConv2dKernel()
172{
173 _type = CLKernelType::DIRECT;
174}
175
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100176void ClDirectConv2dKernel::configure(const CLCompileContext &compile_context,
177 ITensorInfo *src,
178 ITensorInfo *weights,
179 ITensorInfo *biases,
180 ITensorInfo *dst,
181 const PadStrideInfo &conv_info,
182 const ActivationLayerInfo &act_info,
183 const DirectConvComputeKernelInfo &desc)
Giorgio Arena59486342017-12-01 10:42:47 +0000184{
Sheri Zhang1efed922021-03-10 22:43:38 +0000185 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Giorgio Arena59486342017-12-01 10:42:47 +0000186
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000187 // Perform validation
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100188 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc));
Giorgio Arena59486342017-12-01 10:42:47 +0000189
Sheri Zhang1efed922021-03-10 22:43:38 +0000190 const int conv_stride_x = std::get<0>(conv_info.stride());
191 const int conv_stride_y = std::get<1>(conv_info.stride());
192
193 _data_layout = src->data_layout();
194 _conv_info = conv_info;
Pablo Tello3d319462018-06-21 15:13:17 +0100195
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000196 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
197 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
198 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Sheri Zhang1efed922021-03-10 22:43:38 +0000199 const unsigned int kernel_size = weights->dimension(width_idx);
200 const DataType data_type = src->data_type();
Giorgio Arena59486342017-12-01 10:42:47 +0000201
Adnan AlSinan30124352021-12-02 19:12:20 +0000202 const GPUTarget gpu_target = get_target();
203 unsigned int _num_elems_processed_per_iteration = 0;
204
205 // Get dst shape
206 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
207
208 // Output auto inizialitation if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 auto_init_if_empty(*dst, output_shape, 1, src->data_type(), src->quantization_info());
Giorgio Arena59486342017-12-01 10:42:47 +0000210
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000211 // Configure kernel window
Adnan AlSinan30124352021-12-02 19:12:20 +0000212 Window win;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100213 if (_data_layout == DataLayout::NHWC)
Adnan AlSinan30124352021-12-02 19:12:20 +0000214 {
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100215 output_shape.collapse(2U, 1U);
216 const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]);
217 const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1]);
Adnan AlSinan30124352021-12-02 19:12:20 +0000218
219 // Create window and update padding
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100220 win = calculate_max_window(output_shape, Steps(n0, m0));
Adnan AlSinan30124352021-12-02 19:12:20 +0000221 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222 else if (_data_layout == DataLayout::NCHW)
Adnan AlSinan30124352021-12-02 19:12:20 +0000223 {
224 _num_elems_processed_per_iteration = 1u;
225 win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
226 }
227
228 ICLKernel::configure_internal(win);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000229
Giorgio Arena59486342017-12-01 10:42:47 +0000230 std::stringstream kernel_name;
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000231 CLBuildOptions build_options;
232
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 if (_data_layout == DataLayout::NHWC)
Pablo Tello3d319462018-06-21 15:13:17 +0100234 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000235 kernel_name << "direct_convolution_nhwc";
Giorgio Arena59486342017-12-01 10:42:47 +0000236
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100237 const unsigned int n0 = win.x().step();
238 const unsigned int m0 = win.y().step();
239 const unsigned int k0 = adjust_vec_size(desc.k0, src->dimension(channel_idx));
240 const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0;
241 const unsigned int pad_left = conv_info.pad_left();
242 const unsigned int pad_top = conv_info.pad_top();
243
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000244 _export_weights_to_cl_image = desc.export_weights_to_cl_image;
245 _export_input_to_cl_image = desc.export_input_to_cl_image;
246 _export_output_to_cl_image = desc.export_output_to_cl_image;
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000247
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100248 // Update the padding for the weights tensor if we can export to cl_image
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100249 if (_export_weights_to_cl_image)
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100250 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100251 gemm::update_padding_for_cl_image(weights);
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100252 }
Pablo Tello3d319462018-06-21 15:13:17 +0100253
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100254 if (_export_output_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000255 {
256 gemm::update_padding_for_cl_image(dst);
257 }
258
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 if (_export_input_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000260 {
261 gemm::update_padding_for_cl_image(src);
262 }
263
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100264 if (biases != nullptr)
Teresa Charlin8d8a1c52021-02-03 17:01:23 +0000265 {
266 build_options.add_option(std::string("-DHAS_BIAS"));
Sheri Zhang1efed922021-03-10 22:43:38 +0000267 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
Teresa Charlin8d8a1c52021-02-03 17:01:23 +0000268 }
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000269
Gunes Bayirfdb53422022-05-25 10:09:39 +0100270 // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324
271 const auto act_function = act_info.activation();
272 const auto dst_data_type = dst->data_type();
273
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274 if ((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST) &&
275 (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU ||
276 act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) &&
277 (dst_data_type == DataType::F32 || dst_data_type == DataType::F16))
Gunes Bayirfdb53422022-05-25 10:09:39 +0100278 {
279 // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations
280 // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations
281 build_options.add_option("-cl-unsafe-math-optimizations");
282 }
283 else
284 {
285 build_options.add_option("-cl-fast-relaxed-math");
286 }
287
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288 build_options.add_option_if_else(_export_input_to_cl_image, "-DSRC_TENSOR_TYPE=IMAGE",
289 "-DSRC_TENSOR_TYPE=BUFFER");
Sheri Zhang1efed922021-03-10 22:43:38 +0000290 build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
Gian Marco Iodice3394f3e2022-09-16 14:14:21 +0100291 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(0)));
292 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(1)));
293 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(2)));
294 build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(0)));
295 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(1)));
296 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(2)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100297 build_options.add_option_if_else(_export_output_to_cl_image, "-DDST_TENSOR_TYPE=IMAGE",
298 "-DDST_TENSOR_TYPE=BUFFER");
Gunes Bayirfdb53422022-05-25 10:09:39 +0100299 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst_data_type));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100300 build_options.add_option_if_else(_export_weights_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE",
301 "-DWEI_TENSOR_TYPE=BUFFER");
Sheri Zhang1efed922021-03-10 22:43:38 +0000302 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
303 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
304 build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
305 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
306 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000307 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
308 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
309 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
310 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
311 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000312 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
Giorgio Arena8d071272021-12-07 13:49:10 +0000313 build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP");
Gunes Bayirfdb53422022-05-25 10:09:39 +0100314 build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function)));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100315
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100316 if (is_data_type_quantized(data_type))
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100317 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000318 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
319 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
320 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100321
Sheri Zhang1efed922021-03-10 22:43:38 +0000322 PixelValue zero_value = PixelValue(0, src->data_type(), src->quantization_info());
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000323 int zero_value_s32;
324 zero_value.get(zero_value_s32);
325
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100326 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
327 int output_multiplier = 0;
328 int output_shift = 0;
329 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000330 build_options.add_option("-DIS_QUANTIZED");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000331 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
332 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
333 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
334 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
335 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000336 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000337 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100338 }
339 else
340 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000341 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000342 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000343 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
344 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
345 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 build_options.add_option_if(act_info.enabled(),
347 "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
348 build_options.add_option_if(act_info.enabled(),
349 "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000350 }
Viet-Hoa Dob5368fb2022-09-21 11:31:46 +0100351
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100352 if (compile_context.get_ddk_version() >= 30)
Viet-Hoa Dob5368fb2022-09-21 11:31:46 +0100353 {
354 build_options.add_option("-fregister-allocation=64");
355 }
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000356 }
357 else
358 {
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000359 _export_weights_to_cl_image = false;
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100360
Adnan AlSinan30124352021-12-02 19:12:20 +0000361 kernel_name << "direct_convolution_nchw";
Sheri Zhang1efed922021-03-10 22:43:38 +0000362 build_options.add_option_if(biases != nullptr, std::string("-DHAS_BIAS"));
Adnan AlSinan30124352021-12-02 19:12:20 +0000363 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx)));
364 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx)));
365 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx)));
366 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
367 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
368 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
369 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
370 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
371 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
372 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
373 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100374 build_options.add_option(
375 std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx))));
Adnan AlSinan30124352021-12-02 19:12:20 +0000376 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)));
377 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100378 build_options.add_option(
379 std::string("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration)));
380 build_options.add_option(
381 std::string("-DVEC_SIZE_LEFTOVER=" +
382 support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration)));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000383
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100384 if (is_data_type_quantized(data_type))
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000385 {
Adnan AlSinan30124352021-12-02 19:12:20 +0000386 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
387 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
388 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000389
Adnan AlSinan30124352021-12-02 19:12:20 +0000390 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
391 int output_multiplier = 0;
392 int output_shift = 0;
393 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
394 build_options.add_option("-DIS_QUANTIZED");
395 build_options.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
396 build_options.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift));
397 build_options.add_option("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size));
398 build_options.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
399 build_options.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
400 build_options.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100401 }
Giorgio Arena59486342017-12-01 10:42:47 +0000402 }
403
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000404 _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
Giorgio Arena59486342017-12-01 10:42:47 +0000405
Giorgio Arena59486342017-12-01 10:42:47 +0000406 // Set config_id for enabling LWS tuning
SiCong Li47f177e2023-02-22 17:24:09 +0000407 // config_id should include the variables used to parameterize the kernel
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000408 _config_id = kernel_name.str();
409 _config_id += "_";
Giorgio Arena59486342017-12-01 10:42:47 +0000410 _config_id += lower_string(string_from_data_type(data_type));
411 _config_id += "_";
412 _config_id += support::cpp11::to_string(kernel_size);
413 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000414 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000415 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000416 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000417 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000418 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000419 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000420 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000421 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000422 _config_id += support::cpp11::to_string(conv_stride_x);
Giorgio Arena59486342017-12-01 10:42:47 +0000423 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000424 _config_id += support::cpp11::to_string(conv_stride_y);
SiCong Li47f177e2023-02-22 17:24:09 +0000425 // SRC_CHANNELS, SRC_WIDTH, SRC_HEIGHT
426 _config_id += "_";
427 _config_id += support::cpp11::to_string(src->dimension(channel_idx));
428 _config_id += "_";
429 _config_id += support::cpp11::to_string(src->dimension(width_idx));
430 _config_id += "_";
431 _config_id += support::cpp11::to_string(src->dimension(height_idx));
432 _config_id += "_";
433 // DST_CHANNELS, DST_WIDTH, DST_HEIGHT
434 _config_id += support::cpp11::to_string(dst->dimension(channel_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000435 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000436 _config_id += support::cpp11::to_string(dst->dimension(width_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000437 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000438 _config_id += support::cpp11::to_string(dst->dimension(height_idx));
Pablo Tello3d319462018-06-21 15:13:17 +0100439 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000440 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena59486342017-12-01 10:42:47 +0000441}
442
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100443Status ClDirectConv2dKernel::validate(const ITensorInfo *src,
444 const ITensorInfo *weights,
445 const ITensorInfo *biases,
446 const ITensorInfo *dst,
447 const PadStrideInfo &conv_info,
448 const ActivationLayerInfo &act_info,
449 const DirectConvComputeKernelInfo &desc)
Giorgio Arena59486342017-12-01 10:42:47 +0000450{
Gian Marco Iodice2cc50b32022-05-30 14:41:49 +0100451 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000452 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000453}
454
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100455void ClDirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100456{
457 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
458 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
459
460 // Get initial windows
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000461 Window slice = window.first_slice_window_3D();
steniu0127b386c2017-07-18 17:37:43 +0100462
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100463 const auto src =
464 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
465 const auto weights =
466 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
467 const auto biases =
468 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
469 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Sheri Zhang1efed922021-03-10 22:43:38 +0000470
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100471 if (_data_layout == DataLayout::NHWC)
steniu0127b386c2017-07-18 17:37:43 +0100472 {
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100473 cl::Image2D weights_cl_image;
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000474 cl::Image2D output_cl_image;
475 cl::Image2D input_cl_image;
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100476
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100477 if (_export_weights_to_cl_image)
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100478 {
Jakub Sujak8c49f162023-06-16 09:52:50 +0100479 // Export tensor to cl_image
480 weights_cl_image = create_image2d_from_tensor(weights, CLImage2DType::ReadOnly);
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000481 }
482
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100483 if (_export_output_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000484 {
Jakub Sujak8c49f162023-06-16 09:52:50 +0100485 // Export tensor to cl_image
486 output_cl_image = create_image2d_from_tensor(dst, CLImage2DType::WriteOnly);
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000487 }
488
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100489 if (_export_input_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000490 {
Jakub Sujak8c49f162023-06-16 09:52:50 +0100491 // Export tensor to cl_image
492 input_cl_image = create_image2d_from_tensor(src, CLImage2DType::ReadOnly);
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100493 }
494
steniu0127b386c2017-07-18 17:37:43 +0100495 unsigned int idx = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100496 if (_export_input_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000497 {
498 _kernel.setArg(idx++, input_cl_image);
499 }
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000500 add_4d_tensor_nhwc_argument(idx, src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100501 if (_export_output_to_cl_image)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000502 {
503 _kernel.setArg(idx++, output_cl_image);
504 }
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000505 add_4d_tensor_nhwc_argument(idx, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100506 if (_export_weights_to_cl_image)
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100507 {
508 _kernel.setArg(idx++, weights_cl_image);
509 }
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000510 add_4d_tensor_nhwc_argument(idx, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100511 if (biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000512 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000513 add_1D_tensor_argument(idx, biases, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000514 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100515 enqueue(queue, *this, slice, lws_hint());
steniu0127b386c2017-07-18 17:37:43 +0100516 }
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000517 else
518 {
Adnan AlSinan30124352021-12-02 19:12:20 +0000519 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
Sheri Zhang1efed922021-03-10 22:43:38 +0000520 add_3D_tensor_argument(idx1, weights, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000521
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100522 if (biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000523 {
524 Window slice_biases;
Sheri Zhang1efed922021-03-10 22:43:38 +0000525 slice_biases.use_tensor_dimensions(biases->info()->tensor_shape());
526 add_1D_tensor_argument(idx1, biases, slice_biases);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000527 }
528
Sheri Zhang1efed922021-03-10 22:43:38 +0000529 _kernel.setArg(idx1++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000530
531 do
532 {
533 unsigned int idx = 0;
Adnan AlSinan30124352021-12-02 19:12:20 +0000534 add_3D_tensor_argument(idx, src, slice);
Sheri Zhang1efed922021-03-10 22:43:38 +0000535 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000536 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100537 } while (window.slide_window_slice_3D(slice));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000538 }
steniu0127b386c2017-07-18 17:37:43 +0100539}
Sheri Zhang1efed922021-03-10 22:43:38 +0000540} // namespace kernels
541} // namespace opencl
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100542} // namespace arm_compute