blob: b33e0a8b6f84d0c90e38c385646f61879c9ceab2 [file] [log] [blame]
giuros0146a49a02019-04-01 13:50:22 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2021, 2023 Arm Limited.
giuros0146a49a02019-04-01 13:50:22 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLDeconvolutionReshapeOutputKernel.h"
giuros0146a49a02019-04-01 13:50:22 +010025
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Helpers.h"
giuros0146a49a02019-04-01 13:50:22 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000031#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/Validate.h"
33
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
giuros0146a49a02019-04-01 13:50:22 +010037
38namespace arm_compute
39{
40namespace
41{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042Status validate_arguments(const ITensorInfo *input,
43 const ITensorInfo *bias,
44 const ITensorInfo *output,
45 const ITensorInfo *input_info,
46 const ITensorInfo *weights_info,
giuros0146a49a02019-04-01 13:50:22 +010047 const PadStrideInfo &deconv_info)
48{
49 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, input_info, weights_info);
Manuel Bottini8481d832019-12-10 15:28:40 +000050 const DataLayout data_layout = input_info->data_layout();
giuros0146a49a02019-04-01 13:50:22 +010051
52 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
53 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
54 const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
55
56 const bool is_qasymm = is_data_type_quantized_asymmetric(input_info->data_type());
57
58 ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_w) != deconv_info.stride().first);
59 ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_h) != deconv_info.stride().second);
60
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010061 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8,
62 DataType::QASYMM8_SIGNED, DataType::S32);
63 if (!is_qasymm)
giuros0146a49a02019-04-01 13:50:22 +010064 {
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_info, weights_info);
66 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_info->dimension(idx_w) * weights_info->dimension(idx_h) *
68 weights_info->dimension(idx_b));
giuros0146a49a02019-04-01 13:50:22 +010069 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != input_info->dimension(idx_w));
70 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != input_info->dimension(idx_h));
71 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) != input_info->dimension(idx_b));
72
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if (bias != nullptr)
giuros0146a49a02019-04-01 13:50:22 +010074 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 if (is_qasymm)
giuros0146a49a02019-04-01 13:50:22 +010076 {
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
78 }
79 else
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bias, input);
82 }
83 ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(0) != weights_info->dimension(idx_b));
84 }
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (output->total_size() != 0)
giuros0146a49a02019-04-01 13:50:22 +010087 {
Matthew Jacksonb9070a42019-08-22 16:13:27 +010088 const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 auto out_dims = deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h),
90 weights_info->dimension(idx_w), weights_info->dimension(idx_h),
91 stride_info);
giuros0146a49a02019-04-01 13:50:22 +010092
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 const TensorShape output_shape =
94 misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info);
giuros0146a49a02019-04-01 13:50:22 +010095
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
97 }
98 return Status{};
99}
100
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input,
102 ITensorInfo *output,
103 const ITensorInfo *input_info,
104 const ITensorInfo *weights_info,
105 const PadStrideInfo &deconv_info)
giuros0146a49a02019-04-01 13:50:22 +0100106{
107 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
108
Manuel Bottini8481d832019-12-10 15:28:40 +0000109 const DataLayout data_layout = input_info->data_layout();
110 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
111 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100112 const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
giuros0146a49a02019-04-01 13:50:22 +0100113
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 auto out_dims =
115 deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h),
116 weights_info->dimension(idx_w), weights_info->dimension(idx_h), stride_info);
giuros0146a49a02019-04-01 13:50:22 +0100117
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 const TensorShape output_shape =
119 misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info);
giuros0146a49a02019-04-01 13:50:22 +0100120
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 auto_init_if_empty(*output, input->clone()
122 ->set_tensor_shape(output_shape)
123 .set_data_layout(data_layout)
124 .set_quantization_info(input->quantization_info()));
giuros0146a49a02019-04-01 13:50:22 +0100125
126 Window win = calculate_max_window(*input);
127
128 return std::make_pair(Status{}, win);
129}
130} // namespace
131
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132CLDeconvolutionReshapeOutputKernel::CLDeconvolutionReshapeOutputKernel() : _add_bias(false), _bias(nullptr)
giuros0146a49a02019-04-01 13:50:22 +0100133{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100134 _type = CLKernelType::ELEMENTWISE;
giuros0146a49a02019-04-01 13:50:22 +0100135}
136
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137void CLDeconvolutionReshapeOutputKernel::configure(const ICLTensor *input,
138 const ICLTensor *bias,
139 ICLTensor *output,
140 const ITensorInfo *input_info,
141 const ITensorInfo *weights_info,
giuros0146a49a02019-04-01 13:50:22 +0100142 const PadStrideInfo &deconv_info)
143{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100144 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, input_info, weights_info, deconv_info);
145}
146
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147void CLDeconvolutionReshapeOutputKernel::configure(const CLCompileContext &compile_context,
148 const ICLTensor *input,
149 const ICLTensor *bias,
150 ICLTensor *output,
151 const ITensorInfo *input_info,
152 const ITensorInfo *weights_info,
153 const PadStrideInfo &deconv_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100154{
giuros0146a49a02019-04-01 13:50:22 +0100155 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, input_info, weights_info);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100156 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr ? bias->info() : nullptr),
157 output->info(), input_info, weights_info, deconv_info));
giuros0146a49a02019-04-01 13:50:22 +0100158
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100159 auto padding_info = get_padding_info({input, bias, output});
giuros0146a49a02019-04-01 13:50:22 +0100160 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 auto win_config =
162 validate_and_configure_window(input->info(), output->info(), input_info, weights_info, deconv_info);
giuros0146a49a02019-04-01 13:50:22 +0100163 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
164
165 const DataLayout data_layout = input_info->data_layout();
166 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
167 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
168 const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
169
170 _input = input;
171 _output = output;
172 _add_bias = (bias != nullptr);
173 _bias = bias;
174
175 const int filter_w = weights_info->dimension(idx_w);
176 const int filter_h = weights_info->dimension(idx_h);
177 const int filter_b = weights_info->dimension(idx_b);
178 const int img_w = input_info->dimension(idx_w);
179 const int img_h = input_info->dimension(idx_h);
180
181 CLBuildOptions build_opts;
182 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
183 build_opts.add_option("-DFILTER_WIDTH=" + support::cpp11::to_string(filter_w));
184 build_opts.add_option("-DFILTER_HEIGHT=" + support::cpp11::to_string(filter_h));
185 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(img_w));
186 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(img_h));
187 build_opts.add_option_if(data_layout == DataLayout::NCHW, "-DNUM_FILTERS=" + support::cpp11::to_string(filter_b));
188 build_opts.add_option_if(_add_bias, "-DADD_BIAS");
189
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100190 _kernel = create_kernel(compile_context, "deconvolution_reshape", build_opts.options());
giuros0146a49a02019-04-01 13:50:22 +0100191 ICLKernel::configure_internal(win_config.second);
192
193 // Set config_id for enabling LWS tuning
194 _config_id = "deconvolution_reshape_output_";
195 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
196 _config_id += "_";
197 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
198 _config_id += "_";
199 _config_id += support::cpp11::to_string(input->info()->dimension(0));
200 _config_id += "_";
201 _config_id += support::cpp11::to_string(input->info()->dimension(1));
202 _config_id += "_";
203 _config_id += support::cpp11::to_string(output->info()->dimension(0));
204 _config_id += "_";
205 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000206 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
giuros0146a49a02019-04-01 13:50:22 +0100207}
208
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209Status CLDeconvolutionReshapeOutputKernel::validate(const ITensorInfo *input,
210 const ITensorInfo *bias,
211 const ITensorInfo *output,
212 const ITensorInfo *input_info,
213 const ITensorInfo *weights_info,
giuros0146a49a02019-04-01 13:50:22 +0100214 const PadStrideInfo &deconv_info)
215{
216 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, input_info, weights_info, deconv_info));
217 return Status{};
218}
219
220void CLDeconvolutionReshapeOutputKernel::run(const Window &window, cl::CommandQueue &queue)
221{
222 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
223 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
224 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
225
226 unsigned int idx = 0;
227 add_3D_tensor_argument(idx, _input, collapsed);
228 add_3D_tensor_argument(idx, _output, collapsed);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100229 if (_add_bias)
giuros0146a49a02019-04-01 13:50:22 +0100230 {
231 add_1D_tensor_argument(idx, _bias, collapsed);
232 }
233 enqueue(queue, *this, collapsed, lws_hint());
234}
235} // namespace arm_compute