blob: 9307f7d4fb920214cc7364bd473e12ae7a1a8dfd [file] [log] [blame]
Manuel Bottini3b131ab2021-02-19 18:16:44 +00001/*
2 * Copyright (c) 2016-2021 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 */
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"
28#include "src/core/AccessWindowStatic.h"
29#include "src/core/CL/CLValidate.h"
30#include "src/core/helpers/WindowHelpers.h"
31#include "src/core/utils/ScaleUtils.h"
32#include "support/Cast.h"
33
34namespace arm_compute
35{
36namespace opencl
37{
38namespace kernels
39{
40namespace
41{
42inline std::pair<float, float> calculate_scale_factors(const ITensorInfo *src, const ITensorInfo *dst, DataLayout data_layout, bool align_corners)
43{
44 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
45 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
46
47 // Compute the ratio between source width/height and destination width/height
48 const unsigned int src_width = src->dimension(idx_width);
49 const unsigned int src_height = src->dimension(idx_height);
50 const unsigned int dst_width = dst->dimension(idx_width);
51 const unsigned int dst_height = dst->dimension(idx_height);
52
Giorgio Arena511771f2021-08-19 13:04:56 +010053 float scale_x = arm_compute::scale_utils::calculate_resize_ratio(src_width, dst_width, align_corners);
54 float scale_y = arm_compute::scale_utils::calculate_resize_ratio(src_height, dst_height, align_corners);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000055
Giorgio Arena511771f2021-08-19 13:04:56 +010056 return std::make_pair(scale_x, scale_y);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000057}
58
59Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
60{
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
62 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
63 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 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
66 ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
67 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 +010068 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 +000069
Giorgio Arena511771f2021-08-19 13:04:56 +010070 float scale_x = 0.f;
71 float scale_y = 0.f;
Manuel Bottini3b131ab2021-02-19 18:16:44 +000072 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Giorgio Arena511771f2021-08-19 13:04:56 +010073 std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, data_layout, info.align_corners);
Manuel Bottini3b131ab2021-02-19 18:16:44 +000074
Giorgio Arena511771f2021-08-19 13:04:56 +010075 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 +000076
77 return Status{};
78}
Manuel Bottini3b131ab2021-02-19 18:16:44 +000079} // namespace
80
Manuel Bottini3b131ab2021-02-19 18:16:44 +000081Status ClScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
82{
83 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, info));
Manuel Bottini3b131ab2021-02-19 18:16:44 +000084 return Status{};
85}
86
Giorgio Arena4a95bba2021-06-28 11:00:27 +010087ClScaleKernel::ClScaleKernel()
88{
89 _type = CLKernelType::ELEMENTWISE;
90}
91
Manuel Bottini3b131ab2021-02-19 18:16:44 +000092void ClScaleKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
93{
94 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, info));
95 auto padding_info = get_padding_info({ src, dst });
96
97 // Info required for the static tuning
Sheri Zhang1efed922021-03-10 22:43:38 +000098 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Manuel Bottini3b131ab2021-02-19 18:16:44 +000099
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000100 const bool is_nhwc = _data_layout == DataLayout::NHWC;
101
Giorgio Arena511771f2021-08-19 13:04:56 +0100102 float scale_x = 0.f;
103 float scale_y = 0.f;
104 std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, _data_layout, info.align_corners);
105 const bool is_qasymm_bilinear = is_data_type_quantized_asymmetric(src->data_type()) && info.interpolation_policy == InterpolationPolicy::BILINEAR;
106
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000107 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang1efed922021-03-10 22:43:38 +0000108 auto interpolation_policy_to_use = info.interpolation_policy;
Giorgio Arena511771f2021-08-19 13:04:56 +0100109 if(info.interpolation_policy == InterpolationPolicy::AREA && scale_x <= 1.f && scale_y <= 1.f)
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000110 {
111 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
112 }
113
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000114 // Create kernel
Giorgio Arena511771f2021-08-19 13:04:56 +0100115 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
116 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
117 const unsigned int src_width = src->dimension(idx_width);
118 const unsigned int src_height = src->dimension(idx_height);
119 const unsigned int dst_width = dst->dimension(idx_width);
120 const unsigned int vec_size = adjust_vec_size(is_nhwc ? 1 : 4, dst_width);
121 const unsigned int vec_size_leftover = (dst_width % vec_size);
122
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000123 CLBuildOptions build_opts;
124 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
125 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
Giorgio Arena511771f2021-08-19 13:04:56 +0100126 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
127 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
128 build_opts.add_option("-DSCALE_X=" + float_to_string_with_full_precision(scale_x));
129 build_opts.add_option("-DSCALE_Y=" + float_to_string_with_full_precision(scale_y));
130
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000131 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Giorgio Arena511771f2021-08-19 13:04:56 +0100132 build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
133 build_opts.add_option_if(!is_nhwc, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
134 build_opts.add_option_if(!is_nhwc, "-DVEC_SIZE_LEFTOVER=" + ((vec_size_leftover == 0) ? support::cpp11::to_string(vec_size) : support::cpp11::to_string(vec_size_leftover)));
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000135 build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(dst->dimension(2)));
Sheri Zhang1efed922021-03-10 22:43:38 +0000136 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 +0000137 build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
Giorgio Arena511771f2021-08-19 13:04:56 +0100138 if(is_qasymm_bilinear)
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000139 {
140 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
141 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
142 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
143 }
144 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
145 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Giorgio Arena511771f2021-08-19 13:04:56 +0100146 std::string kernel_name = "scale_" + interpolation_name + "_";
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000147 kernel_name += lower_string(string_from_data_layout(_data_layout));
148
149 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000150
Giorgio Arena511771f2021-08-19 13:04:56 +0100151 // Configure kernel window
152 Window win = calculate_max_window(*dst, Steps(vec_size));
153 ICLKernel::configure_internal(win);
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000154
Giorgio Arena511771f2021-08-19 13:04:56 +0100155 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000156
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000157 // Set config_id for enabling LWS tuning
158 _config_id = "scale_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000159 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
160 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000161 _config_id += (is_nhwc ? "nhwc" : "nchw");
162 _config_id += "_";
163 _config_id += support::cpp11::to_string(dst->dimension(0));
164 _config_id += "_";
165 _config_id += support::cpp11::to_string(dst->dimension(1));
166 _config_id += "_";
167 _config_id += support::cpp11::to_string(dst->dimension(2));
168 _config_id += "_";
169 _config_id += support::cpp11::to_string(dst->dimension(3));
170}
171
172void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
173{
174 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
175 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
176
177 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
178 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
179
180 switch(_data_layout)
181 {
182 case DataLayout::NCHW:
183 {
184 Window slice = window.first_slice_window_2D();
185
186 do
187 {
188 unsigned int idx = 0;
189 add_2D_tensor_argument(idx, src, slice);
190 add_2D_tensor_argument(idx, dst, slice);
191 enqueue(queue, *this, slice, lws_hint());
192 }
193 while(window.slide_window_slice_2D(slice));
194 break;
195 }
196 case DataLayout::NHWC:
197 {
198 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
199 Window slice = collapsed.first_slice_window_4D();
200
201 unsigned int idx = 0;
202 add_4D_tensor_argument(idx, src, slice);
203 add_4D_tensor_argument(idx, dst, slice);
204 enqueue(queue, *this, slice, lws_hint());
205 break;
206 }
207 default:
208 ARM_COMPUTE_ERROR("Data layout not supported");
209 }
210}
211} // namespace kernels
212} // namespace opencl
213} // namespace arm_compute