blob: 8863de5c576e44c000f68e5878c379a946d8a6ac [file] [log] [blame]
giuros0146a49a02019-04-01 13:50:22 +01001/*
Giorgio Arena4a95bba2021-06-28 11:00:27 +01002 * Copyright (c) 2019-2021 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"
30#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
giuros0146a49a02019-04-01 13:50:22 +010036
37namespace arm_compute
38{
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info,
42 const PadStrideInfo &deconv_info)
43{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, input_info, weights_info);
Manuel Bottini8481d832019-12-10 15:28:40 +000045 const DataLayout data_layout = input_info->data_layout();
giuros0146a49a02019-04-01 13:50:22 +010046
47 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
48 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
49 const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
50
51 const bool is_qasymm = is_data_type_quantized_asymmetric(input_info->data_type());
52
53 ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_w) != deconv_info.stride().first);
54 ARM_COMPUTE_RETURN_ERROR_ON(weights_info->dimension(idx_h) != deconv_info.stride().second);
55
Manuel Bottini8481d832019-12-10 15:28:40 +000056 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S32);
giuros0146a49a02019-04-01 13:50:22 +010057 if(!is_qasymm)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, input_info, weights_info);
60 }
61 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != weights_info->dimension(idx_w) * weights_info->dimension(idx_h) * weights_info->dimension(idx_b));
62 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != input_info->dimension(idx_w));
63 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != input_info->dimension(idx_h));
64 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(3) != input_info->dimension(idx_b));
65
66 if(bias != nullptr)
67 {
68 if(is_qasymm)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
71 }
72 else
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bias, input);
75 }
76 ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(0) != weights_info->dimension(idx_b));
77 }
78
79 if(output->total_size() != 0)
80 {
Matthew Jacksonb9070a42019-08-22 16:13:27 +010081 const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
Manuel Bottini8481d832019-12-10 15:28:40 +000082 auto out_dims = deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h), weights_info->dimension(idx_w), weights_info->dimension(idx_h), stride_info);
giuros0146a49a02019-04-01 13:50:22 +010083
84 const TensorShape output_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info);
85
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
87 }
88 return Status{};
89}
90
91std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info, const PadStrideInfo &deconv_info)
92{
93 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
94
Manuel Bottini8481d832019-12-10 15:28:40 +000095 const DataLayout data_layout = input_info->data_layout();
96 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
97 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Matthew Jacksonb9070a42019-08-22 16:13:27 +010098 const PadStrideInfo stride_info(deconv_info.stride().first, deconv_info.stride().second);
giuros0146a49a02019-04-01 13:50:22 +010099
Matthew Jacksonb9070a42019-08-22 16:13:27 +0100100 auto out_dims = deconvolution_output_dimensions(input_info->dimension(idx_w), input_info->dimension(idx_h), weights_info->dimension(idx_w), weights_info->dimension(idx_h), stride_info);
giuros0146a49a02019-04-01 13:50:22 +0100101
102 const TensorShape output_shape = misc::shape_calculator::compute_deconvolution_output_shape(out_dims, *input_info, *weights_info);
103
104 auto_init_if_empty(*output, input->clone()->set_tensor_shape(output_shape).set_data_layout(data_layout).set_quantization_info(input->quantization_info()));
105
106 Window win = calculate_max_window(*input);
107
108 return std::make_pair(Status{}, win);
109}
110} // namespace
111
112CLDeconvolutionReshapeOutputKernel::CLDeconvolutionReshapeOutputKernel()
113 : _add_bias(false),
114 _bias(nullptr)
115{
Giorgio Arena4a95bba2021-06-28 11:00:27 +0100116 _type = CLKernelType::ELEMENTWISE;
giuros0146a49a02019-04-01 13:50:22 +0100117}
118
119void CLDeconvolutionReshapeOutputKernel::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const ITensorInfo *input_info, const ITensorInfo *weights_info,
120 const PadStrideInfo &deconv_info)
121{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100122 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, input_info, weights_info, deconv_info);
123}
124
Manuel Bottini256c0b92020-04-21 13:29:30 +0100125void CLDeconvolutionReshapeOutputKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const ITensorInfo *input_info,
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100126 const ITensorInfo *weights_info,
127 const PadStrideInfo &deconv_info)
128{
giuros0146a49a02019-04-01 13:50:22 +0100129 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, input_info, weights_info);
130 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr ? bias->info() : nullptr), output->info(), input_info, weights_info, deconv_info));
131
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000132 auto padding_info = get_padding_info({ input, bias, output });
giuros0146a49a02019-04-01 13:50:22 +0100133 // Configure kernel window
134 auto win_config = validate_and_configure_window(input->info(), output->info(), input_info, weights_info, deconv_info);
135 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
136
137 const DataLayout data_layout = input_info->data_layout();
138 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
139 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
140 const size_t idx_b = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
141
142 _input = input;
143 _output = output;
144 _add_bias = (bias != nullptr);
145 _bias = bias;
146
147 const int filter_w = weights_info->dimension(idx_w);
148 const int filter_h = weights_info->dimension(idx_h);
149 const int filter_b = weights_info->dimension(idx_b);
150 const int img_w = input_info->dimension(idx_w);
151 const int img_h = input_info->dimension(idx_h);
152
153 CLBuildOptions build_opts;
154 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
155 build_opts.add_option("-DFILTER_WIDTH=" + support::cpp11::to_string(filter_w));
156 build_opts.add_option("-DFILTER_HEIGHT=" + support::cpp11::to_string(filter_h));
157 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(img_w));
158 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(img_h));
159 build_opts.add_option_if(data_layout == DataLayout::NCHW, "-DNUM_FILTERS=" + support::cpp11::to_string(filter_b));
160 build_opts.add_option_if(_add_bias, "-DADD_BIAS");
161
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100162 _kernel = create_kernel(compile_context, "deconvolution_reshape", build_opts.options());
giuros0146a49a02019-04-01 13:50:22 +0100163 ICLKernel::configure_internal(win_config.second);
164
165 // Set config_id for enabling LWS tuning
166 _config_id = "deconvolution_reshape_output_";
167 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
168 _config_id += "_";
169 _config_id += lower_string(string_from_data_layout(input->info()->data_layout()));
170 _config_id += "_";
171 _config_id += support::cpp11::to_string(input->info()->dimension(0));
172 _config_id += "_";
173 _config_id += support::cpp11::to_string(input->info()->dimension(1));
174 _config_id += "_";
175 _config_id += support::cpp11::to_string(output->info()->dimension(0));
176 _config_id += "_";
177 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000178 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
giuros0146a49a02019-04-01 13:50:22 +0100179}
180
181Status CLDeconvolutionReshapeOutputKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const ITensorInfo *input_info, const ITensorInfo *weights_info,
182 const PadStrideInfo &deconv_info)
183{
184 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, input_info, weights_info, deconv_info));
185 return Status{};
186}
187
188void CLDeconvolutionReshapeOutputKernel::run(const Window &window, cl::CommandQueue &queue)
189{
190 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
191 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
192 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
193
194 unsigned int idx = 0;
195 add_3D_tensor_argument(idx, _input, collapsed);
196 add_3D_tensor_argument(idx, _output, collapsed);
197 if(_add_bias)
198 {
199 add_1D_tensor_argument(idx, _bias, collapsed);
200 }
201 enqueue(queue, *this, collapsed, lws_hint());
202}
203} // namespace arm_compute