xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClDirectConv2dKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2023 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "src/gpu/cl/kernels/ClDirectConv2dKernel.h"
25 
26 #include "arm_compute/core/CL/CLKernelLibrary.h"
27 #include "arm_compute/core/CL/ICLTensor.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/ITensor.h"
30 #include "arm_compute/core/KernelDescriptors.h"
31 #include "arm_compute/core/PixelValue.h"
32 #include "arm_compute/core/Utils.h"
33 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
34 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
35 #include "src/core/AccessWindowStatic.h"
36 #include "src/core/CL/CLUtils.h"
37 #include "src/core/CL/CLValidate.h"
38 #include "src/core/helpers/AutoConfiguration.h"
39 #include "src/core/helpers/WindowHelpers.h"
40 #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
41 #include "support/Cast.h"
42 #include "support/StringSupport.h"
43 
44 namespace arm_compute
45 {
46 namespace opencl
47 {
48 namespace kernels
49 {
50 namespace
51 {
validate_arguments(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,const DirectConvComputeKernelInfo & desc)52 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
53                           const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc)
54 {
55     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
56     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::F16, DataType::F32);
57     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
58 
59     const DataLayout data_layout = src->data_layout();
60     const int        width_idx   = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
61     const int        height_idx  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
62     const int        channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
63 
64     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");
65     ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
66 
67     ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_input_to_cl_image == true, "Export to CLImage is not supported for the input tensor");
68     ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.export_output_to_cl_image == true, "Export to CLImage is not supported for the output tensor");
69 
70     if(data_layout == DataLayout::NCHW)
71     {
72         ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != weights->dimension(height_idx), "Weights should have same width and height");
73         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.");
74         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,
75                                         "Strides larger than 2 not supported for 3x3, 5x5, 9x9 convolution.");
76         ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled(), "Fused activation is not supported for NCHW layout");
77 
78         if(is_data_type_quantized(src->data_type()))
79         {
80             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,
81                                             "Kernel sizes other than 1x1, 3x3, 5x5 or 9x9 are not supported with quantized data types");
82         }
83         else
84         {
85             ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(width_idx) != 1 && weights->dimension(width_idx) != 3 && weights->dimension(width_idx) != 5,
86                                             "Kernel sizes other than 1x1, 3x3 or 5x5 are not supported with float data types");
87         }
88     }
89 
90     if(data_layout == DataLayout::NHWC)
91     {
92         ARM_COMPUTE_RETURN_ERROR_ON_MSG(act_info.enabled() && !is_data_type_float(src->data_type()), "Fused activation in NHWC is only supported for floating point.");
93         ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8, "M0 can only be greater than 0 and less than or equal to 8");
94         ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.n0 != 1 && desc.n0 != 2 && desc.n0 != 3 && desc.n0 != 4 && desc.n0 != 8 && desc.n0 != 16,
95                                         "N0 can only be: 1, 2, 3, 4, 8, and 16");
96         ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 1 && desc.k0 != 2 && desc.k0 != 3 && desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16,
97                                         "K0 can only be: 1, 2, 3, 4, 8, and 16");
98         if(desc.export_weights_to_cl_image)
99         {
100             ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.k0 != 4 && desc.k0 != 8 && desc.k0 != 16,
101                                             "K0 can only be: 4, 8, and 16");
102             ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(weights),
103                                             "Export to CLImage is not supported for this weight configuration");
104         }
105     }
106 
107     if(biases != nullptr)
108     {
109         if(is_data_type_quantized_asymmetric(src->data_type()))
110         {
111             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
112         }
113         else
114         {
115             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
116         }
117         ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(0) != weights->dimension(3),
118                                         "Biases size and number of dst feature maps should match");
119         ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1,
120                                         "Biases should be one dimensional");
121     }
122 
123     // Checks performed when dst is configured
124     if(dst->total_size() != 0)
125     {
126         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
127                                                            misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info));
128         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
129     }
130 
131     const auto data_type = src->data_type();
132     if(is_data_type_quantized(data_type))
133     {
134         const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
135         const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
136         const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
137 
138         float multiplier        = iqinfo.scale * wqinfo.scale / oqinfo.scale;
139         int   output_multiplier = 0;
140         int   output_shift      = 0;
141         ARM_COMPUTE_RETURN_ON_ERROR(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift));
142     }
143     return Status{};
144 }
145 } // namespace
146 
ClDirectConv2dKernel()147 ClDirectConv2dKernel::ClDirectConv2dKernel()
148 {
149     _type = CLKernelType::DIRECT;
150 }
151 
configure(const CLCompileContext & compile_context,ITensorInfo * src,ITensorInfo * weights,ITensorInfo * biases,ITensorInfo * dst,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,const DirectConvComputeKernelInfo & desc)152 void ClDirectConv2dKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
153                                      const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc)
154 {
155     ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
156 
157     // Perform validation
158     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc));
159 
160     const int conv_stride_x = std::get<0>(conv_info.stride());
161     const int conv_stride_y = std::get<1>(conv_info.stride());
162 
163     _data_layout = src->data_layout();
164     _conv_info   = conv_info;
165 
166     const unsigned int width_idx   = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
167     const unsigned int height_idx  = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
168     const unsigned int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
169     const unsigned int kernel_size = weights->dimension(width_idx);
170     const DataType     data_type   = src->data_type();
171 
172     const GPUTarget gpu_target                         = get_target();
173     unsigned int    _num_elems_processed_per_iteration = 0;
174 
175     // Get dst shape
176     TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
177 
178     // Output auto inizialitation if not yet initialized
179     auto_init_if_empty(*dst, output_shape,
180                        1,
181                        src->data_type(),
182                        src->quantization_info());
183 
184     // Configure kernel window
185     Window win;
186     if(_data_layout == DataLayout::NHWC)
187     {
188         output_shape.collapse(2U, 1U);
189         const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]);
190         const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1]);
191 
192         // Create window and update padding
193         win = calculate_max_window(output_shape, Steps(n0, m0));
194     }
195     else if(_data_layout == DataLayout::NCHW)
196     {
197         _num_elems_processed_per_iteration = 1u;
198         win                                = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
199     }
200 
201     ICLKernel::configure_internal(win);
202 
203     std::stringstream kernel_name;
204     CLBuildOptions    build_options;
205 
206     if(_data_layout == DataLayout::NHWC)
207     {
208         kernel_name << "direct_convolution_nhwc";
209 
210         const unsigned int n0               = win.x().step();
211         const unsigned int m0               = win.y().step();
212         const unsigned int k0               = adjust_vec_size(desc.k0, src->dimension(channel_idx));
213         const unsigned int partial_store_n0 = dst->dimension(channel_idx) % n0;
214         const unsigned int pad_left         = conv_info.pad_left();
215         const unsigned int pad_top          = conv_info.pad_top();
216 
217         _export_weights_to_cl_image = desc.export_weights_to_cl_image;
218         _export_input_to_cl_image   = desc.export_input_to_cl_image;
219         _export_output_to_cl_image  = desc.export_output_to_cl_image;
220 
221         // Update the padding for the weights tensor if we can export to cl_image
222         if(_export_weights_to_cl_image)
223         {
224             gemm::update_padding_for_cl_image(weights);
225         }
226 
227         if(_export_output_to_cl_image)
228         {
229             gemm::update_padding_for_cl_image(dst);
230         }
231 
232         if(_export_input_to_cl_image)
233         {
234             gemm::update_padding_for_cl_image(src);
235         }
236 
237         if(biases != nullptr)
238         {
239             build_options.add_option(std::string("-DHAS_BIAS"));
240             build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
241         }
242 
243         // Conditions of -cl-fast-relaxed-math causing accuracy issues can be traced from COMPMID-5324
244         const auto act_function  = act_info.activation();
245         const auto dst_data_type = dst->data_type();
246 
247         if((gpu_target != GPUTarget::G71 && (gpu_target & GPUTarget::GPU_ARCH_MASK) == GPUTarget::BIFROST)
248            && (act_function == ActivationLayerInfo::ActivationFunction::BOUNDED_RELU || act_function == ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU)
249            && (dst_data_type == DataType::F32 || dst_data_type == DataType::F16))
250         {
251             // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations
252             // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations
253             build_options.add_option("-cl-unsafe-math-optimizations");
254         }
255         else
256         {
257             build_options.add_option("-cl-fast-relaxed-math");
258         }
259 
260         build_options.add_option_if_else(_export_input_to_cl_image, "-DSRC_TENSOR_TYPE=IMAGE", "-DSRC_TENSOR_TYPE=BUFFER");
261         build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
262         build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(0)));
263         build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(1)));
264         build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(2)));
265         build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(0)));
266         build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst->dimension(1)));
267         build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(2)));
268         build_options.add_option_if_else(_export_output_to_cl_image, "-DDST_TENSOR_TYPE=IMAGE", "-DDST_TENSOR_TYPE=BUFFER");
269         build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst_data_type));
270         build_options.add_option_if_else(_export_weights_to_cl_image, "-DWEI_TENSOR_TYPE=IMAGE", "-DWEI_TENSOR_TYPE=BUFFER");
271         build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
272         build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
273         build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
274         build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
275         build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
276         build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
277         build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
278         build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
279         build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
280         build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
281         build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
282         build_options.add_option_if((src->dimension(channel_idx) % k0) != 0, "-DLEFTOVER_LOOP");
283         build_options.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_function)));
284 
285         if(is_data_type_quantized(data_type))
286         {
287             const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
288             const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
289             const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
290 
291             PixelValue zero_value = PixelValue(0, src->data_type(), src->quantization_info());
292             int        zero_value_s32;
293             zero_value.get(zero_value_s32);
294 
295             float multiplier        = iqinfo.scale * wqinfo.scale / oqinfo.scale;
296             int   output_multiplier = 0;
297             int   output_shift      = 0;
298             quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
299             build_options.add_option("-DIS_QUANTIZED");
300             build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
301             build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
302             build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
303             build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
304             build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
305             build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
306             build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
307         }
308         else
309         {
310             build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(data_type));
311             build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
312             build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
313             build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
314             build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
315             build_options.add_option_if(act_info.enabled(), "-DA_VAL=" + float_to_string_with_full_precision(act_info.a()));
316             build_options.add_option_if(act_info.enabled(), "-DB_VAL=" + float_to_string_with_full_precision(act_info.b()));
317         }
318 
319         if(compile_context.get_ddk_version() >= 30)
320         {
321             build_options.add_option("-fregister-allocation=64");
322         }
323     }
324     else
325     {
326         _export_weights_to_cl_image = false;
327 
328         kernel_name << "direct_convolution_nchw";
329         build_options.add_option_if(biases != nullptr, std::string("-DHAS_BIAS"));
330         build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx)));
331         build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx)));
332         build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src->dimension(channel_idx)));
333         build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(conv_info.pad_left()));
334         build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(conv_info.pad_top()));
335         build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
336         build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
337         build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
338         build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights->dimension(height_idx)));
339         build_options.add_option(std::string("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)));
340         build_options.add_option(std::string("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)));
341         build_options.add_option(std::string("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(weights->dimension(channel_idx))));
342         build_options.add_option(std::string("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x)));
343         build_options.add_option(std::string("-DDATA_TYPE_PROMOTED=" + get_cl_type_from_data_type(data_type)));
344         build_options.add_option(std::string("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration)));
345         build_options.add_option(std::string("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration)));
346 
347         if(is_data_type_quantized(data_type))
348         {
349             const UniformQuantizationInfo iqinfo = src->quantization_info().uniform();
350             const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
351             const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
352 
353             float multiplier        = iqinfo.scale * wqinfo.scale / oqinfo.scale;
354             int   output_multiplier = 0;
355             int   output_shift      = 0;
356             quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
357             build_options.add_option("-DIS_QUANTIZED");
358             build_options.add_option("-DOUTPUT_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
359             build_options.add_option("-DOUTPUT_SHIFT=" + support::cpp11::to_string(output_shift));
360             build_options.add_option("-DKERNEL_SIZE=" + support::cpp11::to_string(kernel_size));
361             build_options.add_option("-DINPUT_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
362             build_options.add_option("-DWEIGHTS_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
363             build_options.add_option("-DOUTPUT_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
364         }
365     }
366 
367     _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
368 
369     // Set config_id for enabling LWS tuning
370     _config_id = kernel_name.str();
371     _config_id += "_";
372     _config_id += lower_string(string_from_data_type(data_type));
373     _config_id += "_";
374     _config_id += support::cpp11::to_string(kernel_size);
375     _config_id += "_";
376     _config_id += support::cpp11::to_string(border_size().left);
377     _config_id += "_";
378     _config_id += support::cpp11::to_string(border_size().top);
379     _config_id += "_";
380     _config_id += support::cpp11::to_string(border_size().right);
381     _config_id += "_";
382     _config_id += support::cpp11::to_string(border_size().bottom);
383     _config_id += "_";
384     _config_id += support::cpp11::to_string(conv_stride_x);
385     _config_id += "_";
386     _config_id += support::cpp11::to_string(conv_stride_y);
387     _config_id += "_";
388     _config_id += support::cpp11::to_string(dst->dimension(width_idx));
389     _config_id += "_";
390     _config_id += support::cpp11::to_string(dst->dimension(height_idx));
391     _config_id += "_";
392     _config_id += lower_string(string_from_data_layout(_data_layout));
393 }
394 
validate(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const PadStrideInfo & conv_info,const ActivationLayerInfo & act_info,const DirectConvComputeKernelInfo & desc)395 Status ClDirectConv2dKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
396                                       const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, const DirectConvComputeKernelInfo &desc)
397 {
398     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info, desc));
399     return Status{};
400 }
401 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)402 void ClDirectConv2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
403 {
404     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
405     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
406 
407     // Get initial windows
408     Window slice = window.first_slice_window_3D();
409 
410     const auto src     = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
411     const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
412     const auto biases  = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
413     auto       dst     = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
414 
415     if(_data_layout == DataLayout::NHWC)
416     {
417         cl::Image2D weights_cl_image;
418         cl::Image2D output_cl_image;
419         cl::Image2D input_cl_image;
420 
421         if(_export_weights_to_cl_image)
422         {
423             const size_t      image_w = weights->info()->dimension(0) / 4;
424             const size_t      image_h = weights->info()->dimension(1) * weights->info()->dimension(2) * weights->info()->dimension(3);
425             const TensorShape shape2d(image_w, image_h);
426             const size_t      image_row_pitch = weights->info()->strides_in_bytes()[1];
427 
428             // Export cl_buffer to cl_image
429             weights_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), weights->cl_buffer(), shape2d, weights->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
430         }
431 
432         if(_export_output_to_cl_image)
433         {
434             const size_t      image_w = dst->info()->dimension(0) / 4;
435             const size_t      image_h = dst->info()->dimension(1) * dst->info()->dimension(2) * dst->info()->dimension(3);
436             const TensorShape shape2d(image_w, image_h);
437             const size_t      image_row_pitch = dst->info()->strides_in_bytes()[1];
438 
439             // Export cl_buffer to cl_image
440             output_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), dst->cl_buffer(), shape2d, dst->info()->data_type(), image_row_pitch, CLImage2DType::WriteOnly);
441         }
442 
443         if(_export_input_to_cl_image)
444         {
445             const size_t      image_w = src->info()->dimension(0) / 4;
446             const size_t      image_h = src->info()->dimension(1) * src->info()->dimension(2) * src->info()->dimension(3);
447             const TensorShape shape2d(image_w, image_h);
448             const size_t      image_row_pitch = src->info()->strides_in_bytes()[1];
449 
450             // Export cl_buffer to cl_image
451             input_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), src->cl_buffer(), shape2d, src->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
452         }
453 
454         unsigned int idx = 0;
455         if(_export_input_to_cl_image)
456         {
457             _kernel.setArg(idx++, input_cl_image);
458         }
459         add_4d_tensor_nhwc_argument(idx, src);
460         if(_export_output_to_cl_image)
461         {
462             _kernel.setArg(idx++, output_cl_image);
463         }
464         add_4d_tensor_nhwc_argument(idx, dst);
465         if(_export_weights_to_cl_image)
466         {
467             _kernel.setArg(idx++, weights_cl_image);
468         }
469         add_4d_tensor_nhwc_argument(idx, weights);
470         if(biases != nullptr)
471         {
472             add_1D_tensor_argument(idx, biases, slice);
473         }
474         enqueue(queue, *this, slice, lws_hint());
475     }
476     else
477     {
478         unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
479         add_3D_tensor_argument(idx1, weights, slice);
480 
481         if(biases != nullptr)
482         {
483             Window slice_biases;
484             slice_biases.use_tensor_dimensions(biases->info()->tensor_shape());
485             add_1D_tensor_argument(idx1, biases, slice_biases);
486         }
487 
488         _kernel.setArg(idx1++, static_cast<unsigned int>(weights->info()->strides_in_bytes()[3]));
489 
490         do
491         {
492             unsigned int idx = 0;
493             add_3D_tensor_argument(idx, src, slice);
494             add_3D_tensor_argument(idx, dst, slice);
495             enqueue(queue, *this, slice, lws_hint());
496         }
497         while(window.slide_window_slice_3D(slice));
498     }
499 }
500 } // namespace kernels
501 } // namespace opencl
502 } // namespace arm_compute
503