blob: 4c4373a215381d88616d3364e0676995609b4628 [file] [log] [blame]
Manuel Bottini3b131ab2021-02-19 18:16:44 +00001/*
SiCong Lif9a547c2023-03-24 15:50:51 +00002 * Copyright (c) 2016-2023 Arm Limited.
Manuel Bottini3b131ab2021-02-19 18:16:44 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/kernels/ClScaleKernel.h"
Manuel Bottini3b131ab2021-02-19 18:16:44 +000025
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/TensorInfo.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
30#include "arm_compute/core/utils/StringUtils.h"
31#include "arm_compute/core/utils/InterpolationPolicyUtils.h"
Manuel Bottini3b131ab2021-02-19 18:16:44 +000032#include "src/core/AccessWindowStatic.h"
33#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/WindowHelpers.h"
35#include "src/core/utils/ScaleUtils.h"
36#include "support/Cast.h"
37
38namespace arm_compute
39{
40namespace opencl
41{
42namespace kernels
43{
44namespace
45{
SiCong Lif9a547c2023-03-24 15:50:51 +000046inline std::tuple<float, float> calculate_scale_factors(const ITensorInfo *src, const ITensorInfo *dst, DataLayout data_layout, bool align_corners)
Manuel Bottini3b131ab2021-02-19 18:16:44 +000047{
48 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
49 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
50
51 // Compute the ratio between source width/height and destination width/height
52 const unsigned int src_width = src->dimension(idx_width);
53 const unsigned int src_height = src->dimension(idx_height);
54 const unsigned int dst_width = dst->dimension(idx_width);
55 const unsigned int dst_height = dst->dimension(idx_height);
56
Giorgio Arena511771f2021-08-19 13:04:56 +010057 float scale_x = arm_compute::scale_utils::calculate_resize_ratio(src_width, dst_width, align_corners);
58 float scale_y = arm_compute::scale_utils::calculate_resize_ratio(src_height, dst_height, align_corners);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000059
SiCong Lif9a547c2023-03-24 15:50:51 +000060 return std::make_tuple(scale_x, scale_y);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000061}
62
63Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
64{
65 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
66 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
67 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
70 ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
Pablo Marquez Tello48c0ed92023-05-05 15:02:32 +010071 ARM_COMPUTE_RETURN_ERROR_ON(src->num_channels()!=1);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000072 ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
Giorgio Arena511771f2021-08-19 13:04:56 +010073 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(src->data_type()) && !is_data_type_quantized_asymmetric(src->data_type()));
Manuel Bottini3b131ab2021-02-19 18:16:44 +000074
Giorgio Arena511771f2021-08-19 13:04:56 +010075 float scale_x = 0.f;
76 float scale_y = 0.f;
Manuel Bottini3b131ab2021-02-19 18:16:44 +000077 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Giorgio Arena511771f2021-08-19 13:04:56 +010078 std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, data_layout, info.align_corners);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000079
Giorgio Arena511771f2021-08-19 13:04:56 +010080 ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (scale_x > 1.f || scale_y > 1.f));
Manuel Bottini3b131ab2021-02-19 18:16:44 +000081
82 return Status{};
83}
Manuel Bottini3b131ab2021-02-19 18:16:44 +000084} // namespace
85
Manuel Bottini3b131ab2021-02-19 18:16:44 +000086Status ClScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
87{
88 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, info));
Manuel Bottini3b131ab2021-02-19 18:16:44 +000089 return Status{};
90}
91
Giorgio Arena4a95bba2021-06-28 11:00:27 +010092ClScaleKernel::ClScaleKernel()
93{
94 _type = CLKernelType::ELEMENTWISE;
95}
96
Manuel Bottini3b131ab2021-02-19 18:16:44 +000097void ClScaleKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
98{
99 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, info));
100 auto padding_info = get_padding_info({ src, dst });
101
102 // Info required for the static tuning
Sheri Zhang1efed922021-03-10 22:43:38 +0000103 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000104
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000105 const bool is_nhwc = _data_layout == DataLayout::NHWC;
106
Giorgio Arena511771f2021-08-19 13:04:56 +0100107 float scale_x = 0.f;
108 float scale_y = 0.f;
109 std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, _data_layout, info.align_corners);
Giorgio Arena511771f2021-08-19 13:04:56 +0100110
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000111 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang1efed922021-03-10 22:43:38 +0000112 auto interpolation_policy_to_use = info.interpolation_policy;
Giorgio Arena511771f2021-08-19 13:04:56 +0100113 if(info.interpolation_policy == InterpolationPolicy::AREA && scale_x <= 1.f && scale_y <= 1.f)
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000114 {
115 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
116 }
117
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000118 // Create kernel
Giorgio Arena511771f2021-08-19 13:04:56 +0100119 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
120 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100121 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
Giorgio Arena511771f2021-08-19 13:04:56 +0100122 const unsigned int src_width = src->dimension(idx_width);
123 const unsigned int src_height = src->dimension(idx_height);
124 const unsigned int dst_width = dst->dimension(idx_width);
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100125 const unsigned int dst_channels = dst->dimension(idx_channel);
126 unsigned int vec_size = 0;
127 unsigned int vec_size_leftover = 0;
Giorgio Arena511771f2021-08-19 13:04:56 +0100128
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000129 CLBuildOptions build_opts;
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100130 if(_data_layout == DataLayout::NHWC)
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000131 {
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100132 vec_size = adjust_vec_size(src->data_type() == DataType::F32 ? 4 : 8, dst_channels);
133 vec_size_leftover = dst_channels % vec_size;
134 build_opts.add_option("-DSRC_TENSOR_TYPE=BUFFER");
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100135 build_opts.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
136 build_opts.add_option("-DDST_TENSOR_TYPE=BUFFER");
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100137 build_opts.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
138 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100139 build_opts.add_option("-DN0=" + support::cpp11::to_string(vec_size));
140 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(vec_size_leftover));
Adnan AlSinan17975a62021-11-08 17:46:39 +0000141 build_opts.add_option("-DSCALE_" + string_from_interpolation_policy(interpolation_policy_to_use));
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100142 build_opts.add_option_if(src->num_dimensions() > 3, "-DBATCHED_EXECUTION");
143 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
144 build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
145 build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
146 build_opts.add_option_if(is_data_type_float(src->data_type()), "-DIS_FLOATING_POINT");
147 build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000148 }
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100149 else if(_data_layout == DataLayout::NCHW)
150 {
151 vec_size = adjust_vec_size(4, dst_width);
152 vec_size_leftover = dst_width % vec_size;
153 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
154 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
155 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
156 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
157 build_opts.add_option("-DSCALE_X=" + float_to_string_with_full_precision(scale_x));
158 build_opts.add_option("-DSCALE_Y=" + float_to_string_with_full_precision(scale_y));
159 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
160 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + ((vec_size_leftover == 0) ? support::cpp11::to_string(vec_size) : support::cpp11::to_string(vec_size_leftover)));
161 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
162 build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
163 build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
164 build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
Gunes Bayirb7e86262022-12-26 16:24:04 +0000165
166 const bool is_qasymm_bilinear = is_data_type_quantized_asymmetric(src->data_type()) && info.interpolation_policy == InterpolationPolicy::BILINEAR;
Gian Marco Iodice7bc1a772021-09-08 17:14:19 +0100167 if(is_qasymm_bilinear)
168 {
169 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
170 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
171 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
172 }
173 }
174 else
175 {
176 ARM_COMPUTE_ERROR_ON("Unsupported data layout");
177 }
178
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000179 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
180 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Giorgio Arena511771f2021-08-19 13:04:56 +0100181 std::string kernel_name = "scale_" + interpolation_name + "_";
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000182 kernel_name += lower_string(string_from_data_layout(_data_layout));
183
184 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000185
Giorgio Arena511771f2021-08-19 13:04:56 +0100186 // Configure kernel window
187 Window win = calculate_max_window(*dst, Steps(vec_size));
188 ICLKernel::configure_internal(win);
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000189
Giorgio Arena511771f2021-08-19 13:04:56 +0100190 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000191
Adnan AlSinan17975a62021-11-08 17:46:39 +0000192 // Pass scale kernel arguments
193 if(is_nhwc)
194 {
195 unsigned int idx = 2 * num_arguments_per_4d_tensor_nhwc();
196 _kernel.setArg<cl_float>(idx++, scale_x);
197 _kernel.setArg<cl_float>(idx++, scale_y);
198 }
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000199 // Set config_id for enabling LWS tuning
200 _config_id = "scale_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000201 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
202 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000203 _config_id += (is_nhwc ? "nhwc" : "nchw");
204 _config_id += "_";
205 _config_id += support::cpp11::to_string(dst->dimension(0));
206 _config_id += "_";
207 _config_id += support::cpp11::to_string(dst->dimension(1));
208 _config_id += "_";
209 _config_id += support::cpp11::to_string(dst->dimension(2));
210 _config_id += "_";
211 _config_id += support::cpp11::to_string(dst->dimension(3));
212}
213
214void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
215{
216 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
217 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
218
219 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
220 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
221
222 switch(_data_layout)
223 {
224 case DataLayout::NCHW:
225 {
226 Window slice = window.first_slice_window_2D();
227
228 do
229 {
230 unsigned int idx = 0;
231 add_2D_tensor_argument(idx, src, slice);
232 add_2D_tensor_argument(idx, dst, slice);
233 enqueue(queue, *this, slice, lws_hint());
234 }
235 while(window.slide_window_slice_2D(slice));
236 break;
237 }
238 case DataLayout::NHWC:
239 {
240 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
241 Window slice = collapsed.first_slice_window_4D();
242
243 unsigned int idx = 0;
Adnan AlSinan17975a62021-11-08 17:46:39 +0000244 add_4d_tensor_nhwc_argument(idx, src);
245 add_4d_tensor_nhwc_argument(idx, dst);
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000246 enqueue(queue, *this, slice, lws_hint());
247 break;
248 }
249 default:
250 ARM_COMPUTE_ERROR("Data layout not supported");
251 }
252}
253} // namespace kernels
254} // namespace opencl
255} // namespace arm_compute