blob: ff8c2c32a08dd2d13e630fc35220e58f31eb80fc [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +00002 * Copyright (c) 2017-2021 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/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
steniu0127b386c2017-07-18 17:37:43 +010029#include "arm_compute/core/Helpers.h"
steniu0127b386c2017-07-18 17:37:43 +010030#include "arm_compute/core/ITensor.h"
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +000031#include "arm_compute/core/PixelValue.h"
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010032#include "arm_compute/core/Utils.h"
Giorgio Arenac0f54432018-03-16 14:02:34 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Chunosovd621bca2017-11-03 17:33:15 +070034#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +010036#include "src/core/CL/CLUtils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/CL/CLValidate.h"
38#include "src/core/helpers/AutoConfiguration.h"
39#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010040#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
Sheri Zhang1efed922021-03-10 22:43:38 +000041#include "support/Cast.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000042#include "support/StringSupport.h"
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010043namespace arm_compute
44{
Sheri Zhang1efed922021-03-10 22:43:38 +000045namespace opencl
46{
47namespace kernels
48{
Georgios Pinitas30902ed2017-11-14 15:32:57 +000049namespace
50{
Georgios Pinitas9fc3be62021-05-29 04:01:51 +010051Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
52 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
Georgios Pinitas30902ed2017-11-14 15:32:57 +000053{
Sheri Zhang1efed922021-03-10 22:43:38 +000054 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
55 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
Pablo Tello3d319462018-06-21 15:13:17 +010057
Sheri Zhang1efed922021-03-10 22:43:38 +000058 const DataLayout data_layout = src->data_layout();
Pablo Tello3d319462018-06-21 15:13:17 +010059 const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
60 const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
61 const int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
62
Giorgio Arena945ae9e2021-10-13 11:13:04 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != src->dimension(channel_idx), "Weights feature map dimension should match the respective src's one");
Pablo Tello3d319462018-06-21 15:13:17 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000065
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000066 if(data_layout == DataLayout::NCHW)
67 {
Giorgio Arena945ae9e2021-10-13 11:13:04 +010068 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height");
69 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 1) && std::get<0>(conv_info.stride()) > 3, "Strides larger than 3 not supported for 1x1 convolution.");
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG((weights->dimension(width_idx) == 3 || weights->dimension(width_idx) == 5 || weights->dimension(width_idx) == 9) && std::get<0>(conv_info.stride()) > 2,
71 "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_data_type_float(src->data_type()) && act_info.enabled(), "Activation supported only for floating point and NHWC.");
73
Sheri Zhang1efed922021-03-10 22:43:38 +000074 if(is_data_type_quantized(src->data_type()))
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000075 {
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5 && weights->dimension(width_idx) != 9,
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +000077 "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +000078 }
79 else
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5,
82 "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
83 }
84 }
85
Georgios Pinitas30902ed2017-11-14 15:32:57 +000086 if(biases != nullptr)
87 {
Sheri Zhang1efed922021-03-10 22:43:38 +000088 if(is_data_type_quantized_asymmetric(src->data_type()))
Georgios Pinitas30902ed2017-11-14 15:32:57 +000089 {
90 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
91 }
92 else
93 {
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
95 }
96 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
Giorgio Arena945ae9e2021-10-13 11:13:04 +010097 "Biases size and number of dst feature maps should match");
Georgios Pinitas30902ed2017-11-14 15:32:57 +000098 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
99 "Biases should be one dimensional");
100 }
101
Sheri Zhang1efed922021-03-10 22:43:38 +0000102 // Checks performed when dst is configured
103 if(dst->total_size() != 0)
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000104 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
106 misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info));
107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000108 }
109
Sheri Zhang1efed922021-03-10 22:43:38 +0000110 const auto data_type = src->data_type();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100111 if(is_data_type_quantized(data_type))
112 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000113 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100114 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
Sheri Zhang1efed922021-03-10 22:43:38 +0000115 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100116
117 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
118 int output_multiplier = 0;
119 int output_shift = 0;
120 ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
121 }
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000122 return Status{};
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000123}
124
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100125bool export_to_cl_image_support(ITensorInfo *tensor, GPUTarget gpu_target, DataLayout data_layout)
126{
127 if(tensor->tensor_shape()[0] % 4 || (data_layout != DataLayout::NHWC))
128 {
129 return false;
130 }
131
132 // If not floating point
133 if(!is_data_type_float(tensor->data_type()))
134 {
135 return false;
136 }
137
138 if(gpu_target == GPUTarget::G71 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD)
139 {
140 return false;
141 }
142
143 // Check if the cl_khr_image2d_from_buffer extension is supported on the target platform
144 if(!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()))
145 {
146 return false;
147 }
148
149 // Check cl image pitch alignment
150 if(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0)
151 {
152 return false;
153 }
154
155 const size_t image_w = tensor->tensor_shape()[0] / 4;
156 const size_t image_h = tensor->tensor_shape()[1] * tensor->tensor_shape()[2] * tensor->tensor_shape()[3];
157 const size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>();
158 const size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>();
159
160 if(image_w > max_image_w || image_h > max_image_h)
161 {
162 return false;
163 }
164
165 return true;
166}
167
Giorgio Arena59486342017-12-01 10:42:47 +0000168} // namespace
169
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100170ClDirectConv2dKernel::ClDirectConv2dKernel()
171{
172 _type = CLKernelType::DIRECT;
173}
174
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100175void ClDirectConv2dKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100176 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
Giorgio Arena59486342017-12-01 10:42:47 +0000177{
Sheri Zhang1efed922021-03-10 22:43:38 +0000178 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
Giorgio Arena59486342017-12-01 10:42:47 +0000179
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000180 // Perform validation
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100181 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info));
Giorgio Arena59486342017-12-01 10:42:47 +0000182
Sheri Zhang1efed922021-03-10 22:43:38 +0000183 const int conv_stride_x = std::get<0>(conv_info.stride());
184 const int conv_stride_y = std::get<1>(conv_info.stride());
185
186 _data_layout = src->data_layout();
187 _conv_info = conv_info;
Pablo Tello3d319462018-06-21 15:13:17 +0100188
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000189 const unsigned int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
190 const unsigned int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
191 const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Sheri Zhang1efed922021-03-10 22:43:38 +0000192 const unsigned int kernel_size = weights->dimension(width_idx);
193 const DataType data_type = src->data_type();
Giorgio Arena59486342017-12-01 10:42:47 +0000194
Adnan AlSinan30124352021-12-02 19:12:20 +0000195 const GPUTarget gpu_target = get_target();
196 unsigned int _num_elems_processed_per_iteration = 0;
197
198 // Get dst shape
199 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
200
201 // Output auto inizialitation if not yet initialized
202 auto_init_if_empty(*dst, output_shape,
203 1,
204 src->data_type(),
205 src->quantization_info());
Giorgio Arena59486342017-12-01 10:42:47 +0000206
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000207 // Configure kernel window
Adnan AlSinan30124352021-12-02 19:12:20 +0000208 Window win;
209 if(_data_layout == DataLayout::NHWC)
210 {
211 const unsigned int vec_size = std::min(static_cast<unsigned int>(dst->tensor_shape()[0]), 4u);
212 unsigned int num_rows = 1U;
213 if(dst->tensor_shape()[0] > 16)
214 {
215 num_rows = src->data_type() == DataType::F32 ? 2U : 4U;
216 }
217
218 // Create window and update padding
219 win = calculate_max_window(output_shape, Steps(vec_size, num_rows));
220 }
221 else if(_data_layout == DataLayout::NCHW)
222 {
223 _num_elems_processed_per_iteration = 1u;
224 win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
225 }
226
227 ICLKernel::configure_internal(win);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000228
Giorgio Arena59486342017-12-01 10:42:47 +0000229 std::stringstream kernel_name;
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000230 CLBuildOptions build_options;
231
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000232 if(_data_layout == DataLayout::NHWC)
Pablo Tello3d319462018-06-21 15:13:17 +0100233 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000234 kernel_name << "direct_convolution_nhwc";
Giorgio Arena59486342017-12-01 10:42:47 +0000235
Adnan AlSinan30124352021-12-02 19:12:20 +0000236 const unsigned int n0 = win.x().step();
237 const unsigned int m0 = win.y().step();
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100238 const unsigned int k0 = adjust_vec_size(is_data_type_quantized(data_type) ? 16u : 8u, src->dimension(channel_idx));
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100239 const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0;
240 const unsigned int pad_left = conv_info.pad_left();
241 const unsigned int pad_top = conv_info.pad_top();
242 const bool export_to_cl_image = export_to_cl_image_support(weights, gpu_target, _data_layout);
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000243
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100244 // Update the padding for the weights tensor if we can export to cl_image
245 if(export_to_cl_image)
246 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100247 gemm::update_padding_for_cl_image(weights);
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100248 }
Pablo Tello3d319462018-06-21 15:13:17 +0100249
Sheri Zhang1efed922021-03-10 22:43:38 +0000250 if(biases != nullptr)
Teresa Charlin8d8a1c52021-02-03 17:01:23 +0000251 {
252 build_options.add_option(std::string("-DHAS_BIAS"));
Sheri Zhang1efed922021-03-10 22:43:38 +0000253 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 +0000254 }
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000255
256 build_options.add_option("-cl-fast-relaxed-math");
257 build_options.add_option("-DSRC_TENSOR_TYPE=BUFFER");
Sheri Zhang1efed922021-03-10 22:43:38 +0000258 build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000259 build_options.add_option("-DDST_TENSOR_TYPE=BUFFER");
Sheri Zhang1efed922021-03-10 22:43:38 +0000260 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100261 build_options.add_option_if_else(export_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER");
Sheri Zhang1efed922021-03-10 22:43:38 +0000262 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
263 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
264 build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
265 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
266 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000267 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
268 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
269 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
270 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
271 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000272 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
Giorgio Arena8d071272021-12-07 13:49:10 +0000273 build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP");
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100274 build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation())));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100275
276 if(is_data_type_quantized(data_type))
277 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000278 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
279 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
280 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100281
Sheri Zhang1efed922021-03-10 22:43:38 +0000282 PixelValue zero_value = PixelValue(0, src->data_type(), src->quantization_info());
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000283 int zero_value_s32;
284 zero_value.get(zero_value_s32);
285
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100286 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
287 int output_multiplier = 0;
288 int output_shift = 0;
289 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000290 build_options.add_option("-DIS_QUANTIZED");
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000291 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
292 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
293 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
294 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
295 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
Gian Marco Iodiced95c3e82021-01-19 17:39:02 +0000296 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000297 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100298 }
299 else
300 {
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000301 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000302 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000303 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
304 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
305 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100306 build_options.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
307 build_options.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000308 }
309 }
310 else
311 {
Adnan AlSinan30124352021-12-02 19:12:20 +0000312 kernel_name << "direct_convolution_nchw";
Sheri Zhang1efed922021-03-10 22:43:38 +0000313 build_options.add_option_if(biases != nullptr, std::string("-DHAS_BIAS"));
Adnan AlSinan30124352021-12-02 19:12:20 +0000314 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx)));
315 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx)));
316 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx)));
317 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
318 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
319 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
320 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
321 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
322 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
323 build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
324 build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
325 build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx))));
326 build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)));
327 build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
328 build_options.add_option(std::string("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration)));
329 build_options.add_option(std::string("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration)));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000330
Adnan AlSinan30124352021-12-02 19:12:20 +0000331 if(is_data_type_quantized(data_type))
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000332 {
Adnan AlSinan30124352021-12-02 19:12:20 +0000333 const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
334 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
335 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000336
Adnan AlSinan30124352021-12-02 19:12:20 +0000337 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
338 int output_multiplier = 0;
339 int output_shift = 0;
340 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
341 build_options.add_option("-DIS_QUANTIZED");
342 build_options.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
343 build_options.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift));
344 build_options.add_option("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size));
345 build_options.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
346 build_options.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
347 build_options.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100348 }
Giorgio Arena59486342017-12-01 10:42:47 +0000349 }
350
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000351 _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
Giorgio Arena59486342017-12-01 10:42:47 +0000352
Giorgio Arena59486342017-12-01 10:42:47 +0000353 // Set config_id for enabling LWS tuning
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000354 _config_id = kernel_name.str();
355 _config_id += "_";
Giorgio Arena59486342017-12-01 10:42:47 +0000356 _config_id += lower_string(string_from_data_type(data_type));
357 _config_id += "_";
358 _config_id += support::cpp11::to_string(kernel_size);
359 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000360 _config_id += support::cpp11::to_string(border_size().left);
Giorgio Arena59486342017-12-01 10:42:47 +0000361 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000362 _config_id += support::cpp11::to_string(border_size().top);
Giorgio Arena59486342017-12-01 10:42:47 +0000363 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000364 _config_id += support::cpp11::to_string(border_size().right);
Giorgio Arena59486342017-12-01 10:42:47 +0000365 _config_id += "_";
Georgios Pinitas15997872018-02-19 13:58:22 +0000366 _config_id += support::cpp11::to_string(border_size().bottom);
Giorgio Arena59486342017-12-01 10:42:47 +0000367 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000368 _config_id += support::cpp11::to_string(conv_stride_x);
Giorgio Arena59486342017-12-01 10:42:47 +0000369 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000370 _config_id += support::cpp11::to_string(conv_stride_y);
Giorgio Arena59486342017-12-01 10:42:47 +0000371 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000372 _config_id += support::cpp11::to_string(dst->dimension(width_idx));
Giorgio Arena59486342017-12-01 10:42:47 +0000373 _config_id += "_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000374 _config_id += support::cpp11::to_string(dst->dimension(height_idx));
Pablo Tello3d319462018-06-21 15:13:17 +0100375 _config_id += "_";
Georgios Pinitas7fdcfb12020-01-09 16:45:46 +0000376 _config_id += lower_string(string_from_data_layout(_data_layout));
Giorgio Arena59486342017-12-01 10:42:47 +0000377}
378
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100379Status ClDirectConv2dKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
Adnan AlSinan30124352021-12-02 19:12:20 +0000380 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info)
Giorgio Arena59486342017-12-01 10:42:47 +0000381{
Georgios Pinitas9fc3be62021-05-29 04:01:51 +0100382 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000383 return Status{};
Giorgio Arena59486342017-12-01 10:42:47 +0000384}
385
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100386void ClDirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100387{
388 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
389 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
390
391 // Get initial windows
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000392 Window slice = window.first_slice_window_3D();
steniu0127b386c2017-07-18 17:37:43 +0100393
Sheri Zhang1efed922021-03-10 22:43:38 +0000394 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
395 const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
396 const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
397 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
398
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000399 if(_data_layout == DataLayout::NHWC)
steniu0127b386c2017-07-18 17:37:43 +0100400 {
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100401 cl::Image2D weights_cl_image;
402
403 const size_t dim_y_collapsed = ceil_to_multiple(dst->info()->dimension(1) * dst->info()->dimension(2), slice.y().step());
404 const bool export_to_cl_image = export_to_cl_image_support(weights->info(), get_target(), _data_layout);
405
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000406 slice.set(Window::DimY, Window::Dimension(0, dim_y_collapsed, slice.y().step()));
Sheri Zhang1efed922021-03-10 22:43:38 +0000407 slice.set(Window::DimZ, Window::Dimension(0, dst->info()->dimension(3), 1));
steniu0127b386c2017-07-18 17:37:43 +0100408
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100409 if(export_to_cl_image)
410 {
411 const size_t image_w = weights->info()->dimension(0) / 4;
412 const size_t image_h = weights->info()->dimension(1) * weights->info()->dimension(2) * weights->info()->dimension(3);
413 const TensorShape shape2d(image_w, image_h);
414 const size_t image_row_pitch = weights->info()->strides_in_bytes()[1];
415
416 // Export cl_buffer to cl_image
417 weights_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), weights->cl_buffer(), shape2d, weights->info()->data_type(), image_row_pitch);
418 }
419
steniu0127b386c2017-07-18 17:37:43 +0100420 unsigned int idx = 0;
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000421 add_4d_tensor_nhwc_argument(idx, src);
422 add_4d_tensor_nhwc_argument(idx, dst);
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100423 if(export_to_cl_image)
424 {
425 _kernel.setArg(idx++, weights_cl_image);
426 }
Gian Marco Iodice78baa482021-12-01 09:26:14 +0000427 add_4d_tensor_nhwc_argument(idx, weights);
Sheri Zhang1efed922021-03-10 22:43:38 +0000428 if(biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000429 {
Sheri Zhang1efed922021-03-10 22:43:38 +0000430 add_1D_tensor_argument(idx, biases, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000431 }
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100432 enqueue(queue, *this, slice, lws_hint());
steniu0127b386c2017-07-18 17:37:43 +0100433 }
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000434 else
435 {
Adnan AlSinan30124352021-12-02 19:12:20 +0000436 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
Sheri Zhang1efed922021-03-10 22:43:38 +0000437 add_3D_tensor_argument(idx1, weights, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000438
Sheri Zhang1efed922021-03-10 22:43:38 +0000439 if(biases != nullptr)
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000440 {
441 Window slice_biases;
Sheri Zhang1efed922021-03-10 22:43:38 +0000442 slice_biases.use_tensor_dimensions(biases->info()->tensor_shape());
443 add_1D_tensor_argument(idx1, biases, slice_biases);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000444 }
445
Sheri Zhang1efed922021-03-10 22:43:38 +0000446 _kernel.setArg(idx1++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000447
448 do
449 {
450 unsigned int idx = 0;
Adnan AlSinan30124352021-12-02 19:12:20 +0000451 add_3D_tensor_argument(idx, src, slice);
Sheri Zhang1efed922021-03-10 22:43:38 +0000452 add_3D_tensor_argument(idx, dst, slice);
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000453 enqueue(queue, *this, slice, lws_hint());
454 }
Adnan AlSinan30124352021-12-02 19:12:20 +0000455 while(window.slide_window_slice_3D(slice));
Gian Marco Iodiceff1fe3e2021-01-02 09:58:51 +0000456 }
steniu0127b386c2017-07-18 17:37:43 +0100457}
Sheri Zhang1efed922021-03-10 22:43:38 +0000458} // namespace kernels
459} // namespace opencl
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100460} // namespace arm_compute