blob: 76f39ac500178eb632c15a1c20bcecd1298a8c7c [file] [log] [blame]
Gunes Bayirec0113d2022-11-09 09:26:27 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2022-2023 Arm Limited.
Gunes Bayirec0113d2022-11-09 09:26:27 +00003 *
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/ClTransposedConvolutionKernel.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Gunes Bayirec0113d2022-11-09 09:26:27 +000028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Gunes Bayirec0113d2022-11-09 09:26:27 +000032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
35#include "support/Cast.h"
36
37namespace arm_compute
38{
39namespace opencl
40{
41namespace kernels
42{
43namespace
44{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045Status validate_arguments(const ITensorInfo *input,
46 const ITensorInfo *weights,
47 const ITensorInfo *biases,
48 const ITensorInfo *output,
Gunes Bayirec0113d2022-11-09 09:26:27 +000049 const PadStrideInfo &deconv_info)
50{
51 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32,
53 DataType::QASYMM8_SIGNED, DataType::QASYMM8);
Gunes Bayirec0113d2022-11-09 09:26:27 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
55 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(weights, DataLayout::NHWC);
57
58 constexpr unsigned int channel_idx = 0;
59 constexpr unsigned int width_idx = 1;
60 constexpr unsigned int height_idx = 2;
61 constexpr unsigned int batch_idx = 3;
62
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(channel_idx) != input->dimension(channel_idx),
64 "Weights feature map dimension should match the respective src's one");
Gunes Bayirec0113d2022-11-09 09:26:27 +000065 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 if (biases != nullptr)
Gunes Bayirec0113d2022-11-09 09:26:27 +000068 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 if (is_data_type_quantized_asymmetric(input->data_type()))
Gunes Bayira0ae8d22022-12-12 17:47:49 +000070 {
71 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::S32);
72 }
73 else
74 {
75 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
76 }
77
Gunes Bayirec0113d2022-11-09 09:26:27 +000078 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->dimension(channel_idx) != weights->dimension(batch_idx),
79 "Biases size and number of dst feature maps should match");
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG(biases->num_dimensions() > 1, "Biases should be one dimensional");
81 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC);
82 }
83
84 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 if (output->total_size() != 0)
Gunes Bayirec0113d2022-11-09 09:26:27 +000086 {
87 const size_t input_width = input->dimension(width_idx);
88 const size_t input_height = input->dimension(height_idx);
89 const size_t weights_width = weights->dimension(width_idx);
90 const size_t weights_height = weights->dimension(height_idx);
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 auto out_dims =
93 deconvolution_output_dimensions(input_width, input_height, weights_width, weights_height, deconv_info);
94 TensorShape output_shape =
95 misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights);
Gunes Bayirec0113d2022-11-09 09:26:27 +000096
97 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
98 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
99 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(output, DataLayout::NHWC);
100 }
101
102 return Status{};
103}
104} // namespace
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106void ClTransposedConvolutionKernel::configure(const CLCompileContext &compile_context,
107 const ITensorInfo *input,
108 const ITensorInfo *weights,
109 const ITensorInfo *biases,
110 ITensorInfo *output,
111 const PadStrideInfo &deconv_info)
Gunes Bayirec0113d2022-11-09 09:26:27 +0000112{
113 ARM_COMPUTE_UNUSED(biases, deconv_info);
114 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
115
116 // Perform validation
117 ARM_COMPUTE_ERROR_THROW_ON(validate(input, weights, biases, output, deconv_info));
118
119 constexpr unsigned int channel_idx = 0;
120 constexpr unsigned int width_idx = 1;
121 constexpr unsigned int height_idx = 2;
122
123 const size_t input_channels = input->dimension(channel_idx); // same as weight channels
124 const size_t input_width = input->dimension(width_idx);
125 const size_t input_height = input->dimension(height_idx);
126 const size_t weights_width = weights->dimension(width_idx);
127 const size_t weights_height = weights->dimension(height_idx);
128 const size_t output_width = output->dimension(width_idx);
129 const size_t output_height = output->dimension(height_idx);
130 const size_t output_channels = output->dimension(channel_idx);
131
132 // Calculate output shape
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 auto out_dims =
134 deconvolution_output_dimensions(input_width, input_height, weights_width, weights_height, deconv_info);
Gunes Bayirec0113d2022-11-09 09:26:27 +0000135 TensorShape output_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input, *weights);
136 auto_init_if_empty(*output, output_shape, 1, input->data_type(), input->quantization_info());
137
138 // Calculate updated paddings
139 // p' = k - p - 1 (k: kernel dimensions)
140 const uint32_t pad_left = weights_width - deconv_info.pad_left() - 1;
141 const uint32_t pad_top = weights_height - deconv_info.pad_top() - 1;
142
143 // Configure kernel window
144 Window win;
145 output_shape.collapse(2U, 1U); // Collapse width and height into single dimension
146
Gunes Bayir8a2d7ce2022-12-28 10:28:20 +0000147 const unsigned int n0 = adjust_vec_size(16 / output->element_size(), output_channels);
148 const unsigned int m0 = 1;
149 const unsigned int k0 = adjust_vec_size(16 / input->element_size(), input_channels);
150 const unsigned int partial_store_n0 = output_channels % n0;
151
Gunes Bayirec0113d2022-11-09 09:26:27 +0000152 // Create window and update padding
Gunes Bayir8a2d7ce2022-12-28 10:28:20 +0000153 win = calculate_max_window(output_shape, Steps(n0, m0));
Gunes Bayirec0113d2022-11-09 09:26:27 +0000154 ICLKernel::configure_internal(win);
155
156 const std::string kernel_name = "transposed_convolution_nhwc";
157 CLBuildOptions build_options;
158
Gunes Bayira0ae8d22022-12-12 17:47:49 +0000159 const DataType input_data_type = input->data_type();
160 const PaddingInfo strides = deconv_info.stride();
Gunes Bayirec0113d2022-11-09 09:26:27 +0000161
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100162 if (biases != nullptr)
Gunes Bayirec0113d2022-11-09 09:26:27 +0000163 {
164 build_options.add_option(std::string("-DHAS_BIAS"));
165 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(biases->data_type())));
166 }
167
168 const auto output_data_type = output->data_type();
169
170 build_options.add_option("-cl-fast-relaxed-math");
171 build_options.add_option("-DSRC_TENSOR_TYPE=BUFFER");
172 build_options.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(input_data_type));
173 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(input_channels));
174 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width));
175 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height));
176 build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(output_channels));
177 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(output_width));
178 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(output_height));
179 build_options.add_option("-DDST_TENSOR_TYPE=BUFFER");
180 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(output_data_type));
181 build_options.add_option("-DWEI_TENSOR_TYPE=BUFFER");
182 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights_width));
183 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights_height));
184 build_options.add_option("-DWEI_DATA_TYPE=" + get_cl_type_from_data_type(weights->data_type()));
185 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(strides.first));
186 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(strides.second));
187 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
188 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
189 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
190 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
191 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
192 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
193 build_options.add_option_if((input_channels % k0) != 0, "-DLEFTOVER_LOOP");
Gunes Bayira0ae8d22022-12-12 17:47:49 +0000194
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 if (is_data_type_quantized(output_data_type))
Gunes Bayira0ae8d22022-12-12 17:47:49 +0000196 {
197 const UniformQuantizationInfo iqinfo = input->quantization_info().uniform();
198 const UniformQuantizationInfo wqinfo = weights->quantization_info().uniform();
199 const UniformQuantizationInfo oqinfo = output->quantization_info().uniform();
200
201 PixelValue zero_value = PixelValue(0, input->data_type(), input->quantization_info());
202 int zero_value_s32;
203 zero_value.get(zero_value_s32);
204
205 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
206 int output_multiplier = 0;
207 int output_shift = 0;
208
209 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
210 build_options.add_option("-DIS_QUANTIZED");
211 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
212 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
213 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
214 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
215 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
216 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
217 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
218 }
219 else
220 {
221 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(input_data_type));
222 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
223 }
Gunes Bayirec0113d2022-11-09 09:26:27 +0000224
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100225 if (compile_context.get_ddk_version() >= 30)
Gunes Bayirec0113d2022-11-09 09:26:27 +0000226 {
227 build_options.add_option("-fregister-allocation=64");
228 }
229
230 _kernel = create_kernel(compile_context, kernel_name, build_options.options());
231
232 // Set config_id for enabling LWS tuning
233 _config_id = kernel_name;
234 _config_id += "_";
235 _config_id += lower_string(string_from_data_type(input_data_type));
236 _config_id += "_";
237 _config_id += support::cpp11::to_string(weights_width);
238 _config_id += "_";
239 _config_id += support::cpp11::to_string(strides.first);
240 _config_id += "_";
241 _config_id += support::cpp11::to_string(strides.second);
242 _config_id += "_";
243 _config_id += support::cpp11::to_string(output_width);
244 _config_id += "_";
245 _config_id += support::cpp11::to_string(m0);
246 _config_id += "_";
247 _config_id += support::cpp11::to_string(n0);
248}
249
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100250Status ClTransposedConvolutionKernel::validate(const ITensorInfo *src,
251 const ITensorInfo *weights,
252 const ITensorInfo *biases,
253 const ITensorInfo *dst,
254 const PadStrideInfo &deconv_info)
Gunes Bayirec0113d2022-11-09 09:26:27 +0000255{
256 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, deconv_info));
257 return Status{};
258}
259
260void ClTransposedConvolutionKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
261{
262 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
263 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
264
265 // Get initial windows
266 Window slice = window.first_slice_window_3D();
267
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100268 const auto src =
269 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
270 const auto weights =
271 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
272 const auto biases =
273 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
274 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Gunes Bayirec0113d2022-11-09 09:26:27 +0000275
276 unsigned int idx = 0;
277 add_4d_tensor_nhwc_argument(idx, src);
278 add_4d_tensor_nhwc_argument(idx, dst);
279
280 add_4d_tensor_nhwc_argument(idx, weights);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100281 if (biases != nullptr)
Gunes Bayirec0113d2022-11-09 09:26:27 +0000282 {
283 add_1D_tensor_argument(idx, biases, slice);
284 }
285
286 enqueue(queue, *this, slice, lws_hint());
287}
288} // namespace kernels
289} // namespace opencl
290} // namespace arm_compute