blob: 0882f291356c2603b903e189c9199539120ba961 [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 */
24#include "src/core/gpu/cl/kernels/ClScaleKernel.h"
25
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
53 float wr = arm_compute::scale_utils::calculate_resize_ratio(src_width, dst_width, align_corners);
54 float hr = arm_compute::scale_utils::calculate_resize_ratio(src_height, dst_height, align_corners);
55
56 return std::make_pair(wr, hr);
57}
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));
68
69 float wr = 0.f;
70 float hr = 0.f;
71 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
72 std::tie(wr, hr) = calculate_scale_factors(src, dst, data_layout, info.align_corners);
73
74 ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
75
76 return Status{};
77}
78
79std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info, BorderSize &border)
80{
81 Window win{};
82 bool window_changed{};
83 unsigned int num_elems_processed_per_iteration = 0;
84 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
85
86 switch(data_layout)
87 {
88 case DataLayout::NCHW:
89 {
90 if(info.border_mode == BorderMode::UNDEFINED)
91 {
92 border = BorderSize(0);
93 }
94
95 num_elems_processed_per_iteration = 4;
96 // Configure kernel window
97 win = calculate_max_window(*dst, Steps(num_elems_processed_per_iteration));
98 AccessWindowStatic input_access(src,
99 -border.left, -border.top,
100 src->dimension(0) + border.right,
101 src->dimension(1) + border.bottom);
102 AccessWindowHorizontal output_access(dst, 0, num_elems_processed_per_iteration);
103
104 output_access.set_valid_region(win, calculate_valid_region_scale(*src,
105 dst->tensor_shape(),
106 info.interpolation_policy,
107 info.sampling_policy,
108 info.border_mode == BorderMode::UNDEFINED));
109
110 window_changed = update_window_and_padding(win, input_access, output_access);
111 }
112 break;
113 case DataLayout::NHWC:
114 {
115 // Configure kernel window
116 win = calculate_max_window(*dst, Steps());
117 }
118 break;
119 default:
120 ARM_COMPUTE_ERROR("Data layout not supported");
121 }
122
123 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
124 return std::make_pair(err, win);
125}
126} // namespace
127
128BorderSize ClScaleKernel::border_size() const
129{
130 return BorderSize(static_cast<size_t>(_data_layout == DataLayout::NCHW));
131}
132
133Status ClScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
134{
135 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, info));
136 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
137 BorderSize border = BorderSize(static_cast<size_t>(data_layout == DataLayout::NCHW));
138 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get(), info, border).first);
139
140 return Status{};
141}
142
143void ClScaleKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
144{
145 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, info));
146 auto padding_info = get_padding_info({ src, dst });
147
148 // Info required for the static tuning
149 _info = info;
150 _data_type = src->data_type();
151 _data_layout = _info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : _info.data_layout;
152
153 float wr = 0.f;
154 float hr = 0.f;
155 std::tie(wr, hr) = calculate_scale_factors(src, dst, _data_layout, _info.align_corners);
156 const bool call_quantized_kernel = is_data_type_quantized_asymmetric(src->data_type()) && _info.interpolation_policy == InterpolationPolicy::BILINEAR;
157
158 // Compute actual border size
159 BorderSize border = border_size();
160 const bool is_nhwc = _data_layout == DataLayout::NHWC;
161
162 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
163 auto interpolation_policy_to_use = _info.interpolation_policy;
164 if(_info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
165 {
166 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
167 }
168
169 // Configure kernel window
170 auto win_config = validate_and_configure_window(src, dst, _info, border);
171 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
172 ICLKernel::configure_internal(win_config.second);
173
174 // Create kernel
175 CLBuildOptions build_opts;
176 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
177 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
178 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
179 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
180 build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(dst->dimension(2)));
181 build_opts.add_option_if_else(_info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
182 build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
183 if(call_quantized_kernel)
184 {
185 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
186 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
187 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
188 }
189 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
190 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
191 std::string kernel_name = "scale_" + interpolation_name;
192 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
193 kernel_name += lower_string(string_from_data_layout(_data_layout));
194
195 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
196 if(is_nhwc)
197 {
198 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
199 }
200
201 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
202 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
203 unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
204 const unsigned int src_width = src->dimension(idx_width);
205 const unsigned int dst_height = src->dimension(idx_height);
206
207 _kernel.setArg<float>(idx++, src_width);
208 _kernel.setArg<float>(idx++, dst_height);
209 _kernel.setArg<float>(idx++, wr);
210 _kernel.setArg<float>(idx++, hr);
211
212 // Set to enable static tuning
213 _output_x_dim = dst->dimension(0);
214
215 // Set config_id for enabling LWS tuning
216 _config_id = "scale_";
217 _config_id += (_info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
218 _config_id += (_info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
219 _config_id += (is_nhwc ? "nhwc" : "nchw");
220 _config_id += "_";
221 _config_id += support::cpp11::to_string(dst->dimension(0));
222 _config_id += "_";
223 _config_id += support::cpp11::to_string(dst->dimension(1));
224 _config_id += "_";
225 _config_id += support::cpp11::to_string(dst->dimension(2));
226 _config_id += "_";
227 _config_id += support::cpp11::to_string(dst->dimension(3));
228}
229
230void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
231{
232 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
233 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
234
235 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
236 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
237
238 switch(_data_layout)
239 {
240 case DataLayout::NCHW:
241 {
242 Window slice = window.first_slice_window_2D();
243
244 do
245 {
246 unsigned int idx = 0;
247 add_2D_tensor_argument(idx, src, slice);
248 add_2D_tensor_argument(idx, dst, slice);
249 enqueue(queue, *this, slice, lws_hint());
250 }
251 while(window.slide_window_slice_2D(slice));
252 break;
253 }
254 case DataLayout::NHWC:
255 {
256 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
257 Window slice = collapsed.first_slice_window_4D();
258
259 unsigned int idx = 0;
260 add_4D_tensor_argument(idx, src, slice);
261 add_4D_tensor_argument(idx, dst, slice);
262 enqueue(queue, *this, slice, lws_hint());
263 break;
264 }
265 default:
266 ARM_COMPUTE_ERROR("Data layout not supported");
267 }
268}
269} // namespace kernels
270} // namespace opencl
271} // namespace arm_compute