1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2016-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClScaleKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
28*c217d954SCole Faust #include "src/core/AccessWindowStatic.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
31*c217d954SCole Faust #include "src/core/utils/ScaleUtils.h"
32*c217d954SCole Faust #include "support/Cast.h"
33*c217d954SCole Faust
34*c217d954SCole Faust namespace arm_compute
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace opencl
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace kernels
39*c217d954SCole Faust {
40*c217d954SCole Faust namespace
41*c217d954SCole Faust {
calculate_scale_factors(const ITensorInfo * src,const ITensorInfo * dst,DataLayout data_layout,bool align_corners)42*c217d954SCole Faust inline std::pair<float, float> calculate_scale_factors(const ITensorInfo *src, const ITensorInfo *dst, DataLayout data_layout, bool align_corners)
43*c217d954SCole Faust {
44*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
45*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
46*c217d954SCole Faust
47*c217d954SCole Faust // Compute the ratio between source width/height and destination width/height
48*c217d954SCole Faust const unsigned int src_width = src->dimension(idx_width);
49*c217d954SCole Faust const unsigned int src_height = src->dimension(idx_height);
50*c217d954SCole Faust const unsigned int dst_width = dst->dimension(idx_width);
51*c217d954SCole Faust const unsigned int dst_height = dst->dimension(idx_height);
52*c217d954SCole Faust
53*c217d954SCole Faust float scale_x = arm_compute::scale_utils::calculate_resize_ratio(src_width, dst_width, align_corners);
54*c217d954SCole Faust float scale_y = arm_compute::scale_utils::calculate_resize_ratio(src_height, dst_height, align_corners);
55*c217d954SCole Faust
56*c217d954SCole Faust return std::make_pair(scale_x, scale_y);
57*c217d954SCole Faust }
58*c217d954SCole Faust
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const ScaleKernelInfo & info)59*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
60*c217d954SCole Faust {
61*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
62*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
63*c217d954SCole Faust 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);
64*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
65*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
66*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
67*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
68*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(src->data_type()) && !is_data_type_quantized_asymmetric(src->data_type()));
69*c217d954SCole Faust
70*c217d954SCole Faust float scale_x = 0.f;
71*c217d954SCole Faust float scale_y = 0.f;
72*c217d954SCole Faust const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
73*c217d954SCole Faust std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, data_layout, info.align_corners);
74*c217d954SCole Faust
75*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (scale_x > 1.f || scale_y > 1.f));
76*c217d954SCole Faust
77*c217d954SCole Faust return Status{};
78*c217d954SCole Faust }
79*c217d954SCole Faust } // namespace
80*c217d954SCole Faust
validate(const ITensorInfo * src,const ITensorInfo * dst,const ScaleKernelInfo & info)81*c217d954SCole Faust Status ClScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
82*c217d954SCole Faust {
83*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, info));
84*c217d954SCole Faust return Status{};
85*c217d954SCole Faust }
86*c217d954SCole Faust
ClScaleKernel()87*c217d954SCole Faust ClScaleKernel::ClScaleKernel()
88*c217d954SCole Faust {
89*c217d954SCole Faust _type = CLKernelType::ELEMENTWISE;
90*c217d954SCole Faust }
91*c217d954SCole Faust
configure(const CLCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const ScaleKernelInfo & info)92*c217d954SCole Faust void ClScaleKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
93*c217d954SCole Faust {
94*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, info));
95*c217d954SCole Faust auto padding_info = get_padding_info({ src, dst });
96*c217d954SCole Faust
97*c217d954SCole Faust // Info required for the static tuning
98*c217d954SCole Faust _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
99*c217d954SCole Faust
100*c217d954SCole Faust const bool is_nhwc = _data_layout == DataLayout::NHWC;
101*c217d954SCole Faust
102*c217d954SCole Faust float scale_x = 0.f;
103*c217d954SCole Faust float scale_y = 0.f;
104*c217d954SCole Faust std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, _data_layout, info.align_corners);
105*c217d954SCole Faust
106*c217d954SCole Faust // Area interpolation behaves as Nearest Neighbour in case of up-sampling
107*c217d954SCole Faust auto interpolation_policy_to_use = info.interpolation_policy;
108*c217d954SCole Faust if(info.interpolation_policy == InterpolationPolicy::AREA && scale_x <= 1.f && scale_y <= 1.f)
109*c217d954SCole Faust {
110*c217d954SCole Faust interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
111*c217d954SCole Faust }
112*c217d954SCole Faust
113*c217d954SCole Faust // Create kernel
114*c217d954SCole Faust const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
115*c217d954SCole Faust const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
116*c217d954SCole Faust const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
117*c217d954SCole Faust const unsigned int src_width = src->dimension(idx_width);
118*c217d954SCole Faust const unsigned int src_height = src->dimension(idx_height);
119*c217d954SCole Faust const unsigned int dst_width = dst->dimension(idx_width);
120*c217d954SCole Faust const unsigned int dst_channels = dst->dimension(idx_channel);
121*c217d954SCole Faust unsigned int vec_size = 0;
122*c217d954SCole Faust unsigned int vec_size_leftover = 0;
123*c217d954SCole Faust
124*c217d954SCole Faust CLBuildOptions build_opts;
125*c217d954SCole Faust if(_data_layout == DataLayout::NHWC)
126*c217d954SCole Faust {
127*c217d954SCole Faust vec_size = adjust_vec_size(src->data_type() == DataType::F32 ? 4 : 8, dst_channels);
128*c217d954SCole Faust vec_size_leftover = dst_channels % vec_size;
129*c217d954SCole Faust build_opts.add_option("-DSRC_TENSOR_TYPE=BUFFER");
130*c217d954SCole Faust build_opts.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
131*c217d954SCole Faust build_opts.add_option("-DDST_TENSOR_TYPE=BUFFER");
132*c217d954SCole Faust build_opts.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
133*c217d954SCole Faust build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
134*c217d954SCole Faust build_opts.add_option("-DN0=" + support::cpp11::to_string(vec_size));
135*c217d954SCole Faust build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(vec_size_leftover));
136*c217d954SCole Faust build_opts.add_option("-DSCALE_" + string_from_interpolation_policy(interpolation_policy_to_use));
137*c217d954SCole Faust build_opts.add_option_if(src->num_dimensions() > 3, "-DBATCHED_EXECUTION");
138*c217d954SCole Faust build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
139*c217d954SCole Faust build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
140*c217d954SCole Faust build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
141*c217d954SCole Faust build_opts.add_option_if(is_data_type_float(src->data_type()), "-DIS_FLOATING_POINT");
142*c217d954SCole Faust build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
143*c217d954SCole Faust }
144*c217d954SCole Faust else if(_data_layout == DataLayout::NCHW)
145*c217d954SCole Faust {
146*c217d954SCole Faust vec_size = adjust_vec_size(4, dst_width);
147*c217d954SCole Faust vec_size_leftover = dst_width % vec_size;
148*c217d954SCole Faust build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
149*c217d954SCole Faust build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
150*c217d954SCole Faust build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
151*c217d954SCole Faust build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
152*c217d954SCole Faust build_opts.add_option("-DSCALE_X=" + float_to_string_with_full_precision(scale_x));
153*c217d954SCole Faust build_opts.add_option("-DSCALE_Y=" + float_to_string_with_full_precision(scale_y));
154*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
155*c217d954SCole Faust build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + ((vec_size_leftover == 0) ? support::cpp11::to_string(vec_size) : support::cpp11::to_string(vec_size_leftover)));
156*c217d954SCole Faust build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
157*c217d954SCole Faust build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
158*c217d954SCole Faust build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
159*c217d954SCole Faust build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
160*c217d954SCole Faust
161*c217d954SCole Faust const bool is_qasymm_bilinear = is_data_type_quantized_asymmetric(src->data_type()) && info.interpolation_policy == InterpolationPolicy::BILINEAR;
162*c217d954SCole Faust if(is_qasymm_bilinear)
163*c217d954SCole Faust {
164*c217d954SCole Faust const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
165*c217d954SCole Faust build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
166*c217d954SCole Faust build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
167*c217d954SCole Faust }
168*c217d954SCole Faust }
169*c217d954SCole Faust else
170*c217d954SCole Faust {
171*c217d954SCole Faust ARM_COMPUTE_ERROR_ON("Unsupported data layout");
172*c217d954SCole Faust }
173*c217d954SCole Faust
174*c217d954SCole Faust std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
175*c217d954SCole Faust std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
176*c217d954SCole Faust std::string kernel_name = "scale_" + interpolation_name + "_";
177*c217d954SCole Faust kernel_name += lower_string(string_from_data_layout(_data_layout));
178*c217d954SCole Faust
179*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
180*c217d954SCole Faust
181*c217d954SCole Faust // Configure kernel window
182*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(vec_size));
183*c217d954SCole Faust ICLKernel::configure_internal(win);
184*c217d954SCole Faust
185*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
186*c217d954SCole Faust
187*c217d954SCole Faust // Pass scale kernel arguments
188*c217d954SCole Faust if(is_nhwc)
189*c217d954SCole Faust {
190*c217d954SCole Faust unsigned int idx = 2 * num_arguments_per_4d_tensor_nhwc();
191*c217d954SCole Faust _kernel.setArg<cl_float>(idx++, scale_x);
192*c217d954SCole Faust _kernel.setArg<cl_float>(idx++, scale_y);
193*c217d954SCole Faust }
194*c217d954SCole Faust // Set config_id for enabling LWS tuning
195*c217d954SCole Faust _config_id = "scale_";
196*c217d954SCole Faust _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
197*c217d954SCole Faust _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
198*c217d954SCole Faust _config_id += (is_nhwc ? "nhwc" : "nchw");
199*c217d954SCole Faust _config_id += "_";
200*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(0));
201*c217d954SCole Faust _config_id += "_";
202*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(1));
203*c217d954SCole Faust _config_id += "_";
204*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(2));
205*c217d954SCole Faust _config_id += "_";
206*c217d954SCole Faust _config_id += support::cpp11::to_string(dst->dimension(3));
207*c217d954SCole Faust }
208*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)209*c217d954SCole Faust void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
210*c217d954SCole Faust {
211*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
212*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
213*c217d954SCole Faust
214*c217d954SCole Faust auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
215*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
216*c217d954SCole Faust
217*c217d954SCole Faust switch(_data_layout)
218*c217d954SCole Faust {
219*c217d954SCole Faust case DataLayout::NCHW:
220*c217d954SCole Faust {
221*c217d954SCole Faust Window slice = window.first_slice_window_2D();
222*c217d954SCole Faust
223*c217d954SCole Faust do
224*c217d954SCole Faust {
225*c217d954SCole Faust unsigned int idx = 0;
226*c217d954SCole Faust add_2D_tensor_argument(idx, src, slice);
227*c217d954SCole Faust add_2D_tensor_argument(idx, dst, slice);
228*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
229*c217d954SCole Faust }
230*c217d954SCole Faust while(window.slide_window_slice_2D(slice));
231*c217d954SCole Faust break;
232*c217d954SCole Faust }
233*c217d954SCole Faust case DataLayout::NHWC:
234*c217d954SCole Faust {
235*c217d954SCole Faust Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
236*c217d954SCole Faust Window slice = collapsed.first_slice_window_4D();
237*c217d954SCole Faust
238*c217d954SCole Faust unsigned int idx = 0;
239*c217d954SCole Faust add_4d_tensor_nhwc_argument(idx, src);
240*c217d954SCole Faust add_4d_tensor_nhwc_argument(idx, dst);
241*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
242*c217d954SCole Faust break;
243*c217d954SCole Faust }
244*c217d954SCole Faust default:
245*c217d954SCole Faust ARM_COMPUTE_ERROR("Data layout not supported");
246*c217d954SCole Faust }
247*c217d954SCole Faust }
248*c217d954SCole Faust } // namespace kernels
249*c217d954SCole Faust } // namespace opencl
250*c217d954SCole Faust } // namespace arm_compute
251