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