blob: 7fb5d2a5d3cbdf3d4c79df566e72f2a966af50e0 [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
Sheri Zhang1efed922021-03-10 22:43:38 +0000149 _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000150
151 float wr = 0.f;
152 float hr = 0.f;
Sheri Zhang1efed922021-03-10 22:43:38 +0000153 std::tie(wr, hr) = calculate_scale_factors(src, dst, _data_layout, info.align_corners);
154 const bool call_quantized_kernel = is_data_type_quantized_asymmetric(src->data_type()) && info.interpolation_policy == InterpolationPolicy::BILINEAR;
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000155
156 // Compute actual border size
157 BorderSize border = border_size();
158 const bool is_nhwc = _data_layout == DataLayout::NHWC;
159
160 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sheri Zhang1efed922021-03-10 22:43:38 +0000161 auto interpolation_policy_to_use = info.interpolation_policy;
162 if(info.interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000163 {
164 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
165 }
166
167 // Configure kernel window
Sheri Zhang1efed922021-03-10 22:43:38 +0000168 auto win_config = validate_and_configure_window(src, dst, info, border);
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000169 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
170 ICLKernel::configure_internal(win_config.second);
171
172 // Create kernel
173 CLBuildOptions build_opts;
174 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
175 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
176 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
177 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
178 build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(dst->dimension(2)));
Sheri Zhang1efed922021-03-10 22:43:38 +0000179 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 +0000180 build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
181 if(call_quantized_kernel)
182 {
183 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
184 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
185 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
186 }
187 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
188 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
189 std::string kernel_name = "scale_" + interpolation_name;
190 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
191 kernel_name += lower_string(string_from_data_layout(_data_layout));
192
193 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
194 if(is_nhwc)
195 {
196 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
197 }
198
199 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
200 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
201 unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
202 const unsigned int src_width = src->dimension(idx_width);
203 const unsigned int dst_height = src->dimension(idx_height);
204
205 _kernel.setArg<float>(idx++, src_width);
206 _kernel.setArg<float>(idx++, dst_height);
207 _kernel.setArg<float>(idx++, wr);
208 _kernel.setArg<float>(idx++, hr);
209
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000210 // Set config_id for enabling LWS tuning
211 _config_id = "scale_";
Sheri Zhang1efed922021-03-10 22:43:38 +0000212 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
213 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Manuel Bottini3b131ab2021-02-19 18:16:44 +0000214 _config_id += (is_nhwc ? "nhwc" : "nchw");
215 _config_id += "_";
216 _config_id += support::cpp11::to_string(dst->dimension(0));
217 _config_id += "_";
218 _config_id += support::cpp11::to_string(dst->dimension(1));
219 _config_id += "_";
220 _config_id += support::cpp11::to_string(dst->dimension(2));
221 _config_id += "_";
222 _config_id += support::cpp11::to_string(dst->dimension(3));
223}
224
225void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
226{
227 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
229
230 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
231 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
232
233 switch(_data_layout)
234 {
235 case DataLayout::NCHW:
236 {
237 Window slice = window.first_slice_window_2D();
238
239 do
240 {
241 unsigned int idx = 0;
242 add_2D_tensor_argument(idx, src, slice);
243 add_2D_tensor_argument(idx, dst, slice);
244 enqueue(queue, *this, slice, lws_hint());
245 }
246 while(window.slide_window_slice_2D(slice));
247 break;
248 }
249 case DataLayout::NHWC:
250 {
251 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
252 Window slice = collapsed.first_slice_window_4D();
253
254 unsigned int idx = 0;
255 add_4D_tensor_argument(idx, src, slice);
256 add_4D_tensor_argument(idx, dst, slice);
257 enqueue(queue, *this, slice, lws_hint());
258 break;
259 }
260 default:
261 ARM_COMPUTE_ERROR("Data layout not supported");
262 }
263}
264} // namespace kernels
265} // namespace opencl
266} // namespace arm_compute