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