xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClTransposedConvolutionKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2022 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/ClTransposedConvolutionKernel.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
29*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
30*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
31*c217d954SCole Faust #include "support/Cast.h"
32*c217d954SCole Faust 
33*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34*c217d954SCole Faust 
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace opencl
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace kernels
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace
42*c217d954SCole Faust {
validate_arguments(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & deconv_info)43*c217d954SCole Faust Status validate_arguments(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
44*c217d954SCole Faust                           const PadStrideInfo &deconv_info)
45*c217d954SCole Faust {
46*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
47*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32, DataType::QASYMM8_SIGNED, DataType::QASYMM8);
48*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
49*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
50*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(weights, DataLayout::NHWC);
51*c217d954SCole Faust 
52*c217d954SCole Faust     constexpr unsigned int channel_idx = 0;
53*c217d954SCole Faust     constexpr unsigned int width_idx   = 1;
54*c217d954SCole Faust     constexpr unsigned int height_idx  = 2;
55*c217d954SCole Faust     constexpr unsigned int batch_idx   = 3;
56*c217d954SCole Faust 
57*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != input->dimension(channel_idx), "Weights feature map dimension should match the respective src's one");
58*c217d954SCole Faust     ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
59*c217d954SCole Faust 
60*c217d954SCole Faust     if(biases != nullptr)
61*c217d954SCole Faust     {
62*c217d954SCole Faust         if(is_data_type_quantized_asymmetric(input->data_type()))
63*c217d954SCole Faust         {
64*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
65*c217d954SCole Faust         }
66*c217d954SCole Faust         else
67*c217d954SCole Faust         {
68*c217d954SCole Faust             ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
69*c217d954SCole Faust         }
70*c217d954SCole Faust 
71*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(channel_idx) != weights->dimension(batch_idx),
72*c217d954SCole Faust                                         "Biases size and number of dst feature maps should match");
73*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, "Biases should be one dimensional");
74*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
75*c217d954SCole Faust     }
76*c217d954SCole Faust 
77*c217d954SCole Faust     // Checks performed when output is configured
78*c217d954SCole Faust     if(output->total_size() != 0)
79*c217d954SCole Faust     {
80*c217d954SCole Faust         const size_t input_width    = input->dimension(width_idx);
81*c217d954SCole Faust         const size_t input_height   = input->dimension(height_idx);
82*c217d954SCole Faust         const size_t weights_width  = weights->dimension(width_idx);
83*c217d954SCole Faust         const size_t weights_height = weights->dimension(height_idx);
84*c217d954SCole Faust 
85*c217d954SCole Faust         auto        out_dims     = deconvolution_output_dimensions(input_width, input_height, weights_width, weights_height, deconv_info);
86*c217d954SCole Faust         TensorShape output_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights);
87*c217d954SCole Faust 
88*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
89*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
90*c217d954SCole Faust         ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(output, DataLayout::NHWC);
91*c217d954SCole Faust     }
92*c217d954SCole Faust 
93*c217d954SCole Faust     return Status{};
94*c217d954SCole Faust }
95*c217d954SCole Faust } // namespace
96*c217d954SCole Faust 
configure(const CLCompileContext & compile_context,const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,ITensorInfo * output,const PadStrideInfo & deconv_info)97*c217d954SCole Faust void ClTransposedConvolutionKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, const ITensorInfo *weights,
98*c217d954SCole Faust                                               const ITensorInfo *biases, ITensorInfo *output, const PadStrideInfo &deconv_info)
99*c217d954SCole Faust {
100*c217d954SCole Faust     ARM_COMPUTE_UNUSED(biases, deconv_info);
101*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
102*c217d954SCole Faust 
103*c217d954SCole Faust     // Perform validation
104*c217d954SCole Faust     ARM_COMPUTE_ERROR_THROW_ON(validate(input, weights, biases, output, deconv_info));
105*c217d954SCole Faust 
106*c217d954SCole Faust     constexpr unsigned int channel_idx = 0;
107*c217d954SCole Faust     constexpr unsigned int width_idx   = 1;
108*c217d954SCole Faust     constexpr unsigned int height_idx  = 2;
109*c217d954SCole Faust 
110*c217d954SCole Faust     const size_t input_channels  = input->dimension(channel_idx); // same as weight channels
111*c217d954SCole Faust     const size_t input_width     = input->dimension(width_idx);
112*c217d954SCole Faust     const size_t input_height    = input->dimension(height_idx);
113*c217d954SCole Faust     const size_t weights_width   = weights->dimension(width_idx);
114*c217d954SCole Faust     const size_t weights_height  = weights->dimension(height_idx);
115*c217d954SCole Faust     const size_t output_width    = output->dimension(width_idx);
116*c217d954SCole Faust     const size_t output_height   = output->dimension(height_idx);
117*c217d954SCole Faust     const size_t output_channels = output->dimension(channel_idx);
118*c217d954SCole Faust 
119*c217d954SCole Faust     // Calculate output shape
120*c217d954SCole Faust     auto        out_dims     = deconvolution_output_dimensions(input_width, input_height, weights_width, weights_height, deconv_info);
121*c217d954SCole Faust     TensorShape output_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights);
122*c217d954SCole Faust     auto_init_if_empty(*output, output_shape, 1, input->data_type(), input->quantization_info());
123*c217d954SCole Faust 
124*c217d954SCole Faust     // Calculate updated paddings
125*c217d954SCole Faust     // p' = k - p - 1 (k: kernel dimensions)
126*c217d954SCole Faust     const uint32_t pad_left = weights_width - deconv_info.pad_left() - 1;
127*c217d954SCole Faust     const uint32_t pad_top  = weights_height - deconv_info.pad_top() - 1;
128*c217d954SCole Faust 
129*c217d954SCole Faust     // Configure kernel window
130*c217d954SCole Faust     Window win;
131*c217d954SCole Faust     output_shape.collapse(2U, 1U); // Collapse width and height into single dimension
132*c217d954SCole Faust 
133*c217d954SCole Faust     const unsigned int n0               = adjust_vec_size(16 / output->element_size(), output_channels);
134*c217d954SCole Faust     const unsigned int m0               = 1;
135*c217d954SCole Faust     const unsigned int k0               = adjust_vec_size(16 / input->element_size(), input_channels);
136*c217d954SCole Faust     const unsigned int partial_store_n0 = output_channels % n0;
137*c217d954SCole Faust 
138*c217d954SCole Faust     // Create window and update padding
139*c217d954SCole Faust     win = calculate_max_window(output_shape, Steps(n0, m0));
140*c217d954SCole Faust     ICLKernel::configure_internal(win);
141*c217d954SCole Faust 
142*c217d954SCole Faust     const std::string kernel_name = "transposed_convolution_nhwc";
143*c217d954SCole Faust     CLBuildOptions    build_options;
144*c217d954SCole Faust 
145*c217d954SCole Faust     const DataType    input_data_type = input->data_type();
146*c217d954SCole Faust     const PaddingInfo strides         = deconv_info.stride();
147*c217d954SCole Faust 
148*c217d954SCole Faust     if(biases != nullptr)
149*c217d954SCole Faust     {
150*c217d954SCole Faust         build_options.add_option(std::string("-DHAS_BIAS"));
151*c217d954SCole Faust         build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
152*c217d954SCole Faust     }
153*c217d954SCole Faust 
154*c217d954SCole Faust     const auto output_data_type = output->data_type();
155*c217d954SCole Faust 
156*c217d954SCole Faust     build_options.add_option("-cl-fast-relaxed-math");
157*c217d954SCole Faust     build_options.add_option("-DSRC_TENSOR_TYPE=BUFFER");
158*c217d954SCole Faust     build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(input_data_type));
159*c217d954SCole Faust     build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(input_channels));
160*c217d954SCole Faust     build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
161*c217d954SCole Faust     build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
162*c217d954SCole Faust     build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(output_channels));
163*c217d954SCole Faust     build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(output_width));
164*c217d954SCole Faust     build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(output_height));
165*c217d954SCole Faust     build_options.add_option("-DDST_TENSOR_TYPE=BUFFER");
166*c217d954SCole Faust     build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(output_data_type));
167*c217d954SCole Faust     build_options.add_option("-DWEI_TENSOR_TYPE=BUFFER");
168*c217d954SCole Faust     build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights_width));
169*c217d954SCole Faust     build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights_height));
170*c217d954SCole Faust     build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
171*c217d954SCole Faust     build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(strides.first));
172*c217d954SCole Faust     build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(strides.second));
173*c217d954SCole Faust     build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
174*c217d954SCole Faust     build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
175*c217d954SCole Faust     build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
176*c217d954SCole Faust     build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
177*c217d954SCole Faust     build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
178*c217d954SCole Faust     build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
179*c217d954SCole Faust     build_options.add_option_if((input_channels % k0) != 0, "-DLEFTOVER_LOOP");
180*c217d954SCole Faust 
181*c217d954SCole Faust     if(is_data_type_quantized(output_data_type))
182*c217d954SCole Faust     {
183*c217d954SCole Faust         const UniformQuantizationInfo iqinfo = input->quantization_info().uniform();
184*c217d954SCole Faust         const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
185*c217d954SCole Faust         const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
186*c217d954SCole Faust 
187*c217d954SCole Faust         PixelValue zero_value = PixelValue(0, input->data_type(), input->quantization_info());
188*c217d954SCole Faust         int        zero_value_s32;
189*c217d954SCole Faust         zero_value.get(zero_value_s32);
190*c217d954SCole Faust 
191*c217d954SCole Faust         float multiplier        = iqinfo.scale * wqinfo.scale / oqinfo.scale;
192*c217d954SCole Faust         int   output_multiplier = 0;
193*c217d954SCole Faust         int   output_shift      = 0;
194*c217d954SCole Faust 
195*c217d954SCole Faust         quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
196*c217d954SCole Faust         build_options.add_option("-DIS_QUANTIZED");
197*c217d954SCole Faust         build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
198*c217d954SCole Faust         build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
199*c217d954SCole Faust         build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
200*c217d954SCole Faust         build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
201*c217d954SCole Faust         build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
202*c217d954SCole Faust         build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
203*c217d954SCole Faust         build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
204*c217d954SCole Faust     }
205*c217d954SCole Faust     else
206*c217d954SCole Faust     {
207*c217d954SCole Faust         build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(input_data_type));
208*c217d954SCole Faust         build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
209*c217d954SCole Faust     }
210*c217d954SCole Faust 
211*c217d954SCole Faust     if(compile_context.get_ddk_version() >= 30)
212*c217d954SCole Faust     {
213*c217d954SCole Faust         build_options.add_option("-fregister-allocation=64");
214*c217d954SCole Faust     }
215*c217d954SCole Faust 
216*c217d954SCole Faust     _kernel = create_kernel(compile_context, kernel_name, build_options.options());
217*c217d954SCole Faust 
218*c217d954SCole Faust     // Set config_id for enabling LWS tuning
219*c217d954SCole Faust     _config_id = kernel_name;
220*c217d954SCole Faust     _config_id += "_";
221*c217d954SCole Faust     _config_id += lower_string(string_from_data_type(input_data_type));
222*c217d954SCole Faust     _config_id += "_";
223*c217d954SCole Faust     _config_id += support::cpp11::to_string(weights_width);
224*c217d954SCole Faust     _config_id += "_";
225*c217d954SCole Faust     _config_id += support::cpp11::to_string(strides.first);
226*c217d954SCole Faust     _config_id += "_";
227*c217d954SCole Faust     _config_id += support::cpp11::to_string(strides.second);
228*c217d954SCole Faust     _config_id += "_";
229*c217d954SCole Faust     _config_id += support::cpp11::to_string(output_width);
230*c217d954SCole Faust     _config_id += "_";
231*c217d954SCole Faust     _config_id += support::cpp11::to_string(m0);
232*c217d954SCole Faust     _config_id += "_";
233*c217d954SCole Faust     _config_id += support::cpp11::to_string(n0);
234*c217d954SCole Faust }
235*c217d954SCole Faust 
validate(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * dst,const PadStrideInfo & deconv_info)236*c217d954SCole Faust Status ClTransposedConvolutionKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases,
237*c217d954SCole Faust                                                const ITensorInfo *dst, const PadStrideInfo &deconv_info)
238*c217d954SCole Faust {
239*c217d954SCole Faust     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, deconv_info));
240*c217d954SCole Faust     return Status{};
241*c217d954SCole Faust }
242*c217d954SCole Faust 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)243*c217d954SCole Faust void ClTransposedConvolutionKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
244*c217d954SCole Faust {
245*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
246*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
247*c217d954SCole Faust 
248*c217d954SCole Faust     // Get initial windows
249*c217d954SCole Faust     Window slice = window.first_slice_window_3D();
250*c217d954SCole Faust 
251*c217d954SCole Faust     const auto src     = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
252*c217d954SCole Faust     const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
253*c217d954SCole Faust     const auto biases  = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
254*c217d954SCole Faust     auto       dst     = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
255*c217d954SCole Faust 
256*c217d954SCole Faust     unsigned int idx = 0;
257*c217d954SCole Faust     add_4d_tensor_nhwc_argument(idx, src);
258*c217d954SCole Faust     add_4d_tensor_nhwc_argument(idx, dst);
259*c217d954SCole Faust 
260*c217d954SCole Faust     add_4d_tensor_nhwc_argument(idx, weights);
261*c217d954SCole Faust     if(biases != nullptr)
262*c217d954SCole Faust     {
263*c217d954SCole Faust         add_1D_tensor_argument(idx, biases, slice);
264*c217d954SCole Faust     }
265*c217d954SCole Faust 
266*c217d954SCole Faust     enqueue(queue, *this, slice, lws_hint());
267*c217d954SCole Faust }
268*c217d954SCole Faust } // namespace kernels
269*c217d954SCole Faust } // namespace opencl
270*c217d954SCole Faust } // namespace arm_compute
271