blob: c2c78c8f6d71556630b9a3c608bb4a6ae0e750bd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio655e8c62021-01-28 12:51:02 +00002 * Copyright (c) 2016-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLScaleKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/AccessWindowStatic.h"
34#include "src/core/CL/CLValidate.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035#include "src/core/CL/ICLKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000037#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010039#include "src/core/utils/ScaleUtils.h"
40
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <set>
42#include <string>
43
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000044namespace arm_compute
45{
Michalis Spyrou46da23f2018-04-10 13:41:30 +010046namespace
47{
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000048inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010049{
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000050 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? input.data_layout() : info.data_layout;
51 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
52 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010053
54 // Compute the ratio between source width/height and destination width/height
55 const unsigned int input_width = input.dimension(idx_width);
56 const unsigned int input_height = input.dimension(idx_height);
57 const unsigned int output_width = output.dimension(idx_width);
58 const unsigned int output_height = output.dimension(idx_height);
59
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000060 float wr = arm_compute::scale_utils::calculate_resize_ratio(input_width, output_width, info.align_corners);
61 float hr = arm_compute::scale_utils::calculate_resize_ratio(input_height, output_height, info.align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010062
63 return std::make_pair(wr, hr);
64}
65
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010066Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010067{
68 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000069 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010070 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010073 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010074 ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
Michalis Spyrou46da23f2018-04-10 13:41:30 +010075
76 float wr = 0.f;
77 float hr = 0.f;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000078 std::tie(wr, hr) = calculate_scale_factors(*input, *output, info);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010079
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010080 ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
Michalis Spyrou46da23f2018-04-10 13:41:30 +010081
82 return Status{};
83}
84
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010085std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const ScaleKernelInfo &info, BorderSize &border)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010086{
Michele Di Giorgio655e8c62021-01-28 12:51:02 +000087 Window win{};
88 bool window_changed{};
89 unsigned int num_elems_processed_per_iteration = 0;
90 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : info.data_layout;
Michalis Spyrou46da23f2018-04-10 13:41:30 +010091
92 switch(data_layout)
93 {
94 case DataLayout::NCHW:
95 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010096 if(info.border_mode == BorderMode::UNDEFINED)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010097 {
98 border = BorderSize(0);
99 }
100
101 num_elems_processed_per_iteration = 4;
102 // Configure kernel window
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000103 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100104 AccessWindowStatic input_access(input,
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000105 -border.left, -border.top,
106 input->dimension(0) + border.right,
107 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100108 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
109
110 output_access.set_valid_region(win, calculate_valid_region_scale(*(input),
111 output->tensor_shape(),
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100112 info.interpolation_policy,
113 info.sampling_policy,
114 info.border_mode == BorderMode::UNDEFINED));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100115
116 window_changed = update_window_and_padding(win, input_access, output_access);
117 }
118 break;
119 case DataLayout::NHWC:
120 {
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100121 // Configure kernel window
Manuel Bottini0c58f992020-12-03 16:26:35 +0000122 win = calculate_max_window(*output, Steps());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100123 }
124 break;
125 default:
126 ARM_COMPUTE_ERROR("Data layout not supported");
127 }
128
129 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
130 return std::make_pair(err, win);
131}
132} // namespace
133
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134BorderSize CLScaleKernel::border_size() const
135{
Manuel Bottini0c58f992020-12-03 16:26:35 +0000136 return BorderSize(static_cast<size_t>(_data_layout == DataLayout::NCHW));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137}
138
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100139Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100141 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000142 const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : info.data_layout;
143 BorderSize border = BorderSize(static_cast<size_t>(data_layout == DataLayout::NCHW));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100144 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), info, border).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100146 return Status{};
147}
148
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100149const ICLTensor *CLScaleKernel::input() const
150{
151 return _input;
152}
153
154const ICLTensor *CLScaleKernel::output() const
155{
156 return _output;
157}
158
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100159void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100160{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100161 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100162}
163
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100164void CLScaleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100165{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100166 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), info));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000167 auto padding_info = get_padding_info({ input, output });
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000168
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100169 _input = input;
170 _output = output;
171 _interpolation_policy = info.interpolation_policy;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000172 _data_layout = info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : info.data_layout;
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100173 _align_corners = info.align_corners;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100174
175 float wr = 0.f;
176 float hr = 0.f;
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000177 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), info);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100178
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100179 const bool call_quantized_kernel = is_data_type_quantized_asymmetric(input->info()->data_type()) && _interpolation_policy == InterpolationPolicy::BILINEAR;
Michalis Spyrou17220e22018-09-12 13:35:38 +0100180
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000181 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
182 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
183 const bool is_nhwc = _data_layout == DataLayout::NHWC;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100184
Daniil Efremov7a49c792017-11-14 21:25:34 +0700185 // Compute actual border size
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100186 BorderSize border = border_size();
Daniil Efremov7a49c792017-11-14 21:25:34 +0700187
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100188 auto interpolation_policy_to_use = _interpolation_policy;
Daniil Efremov7a49c792017-11-14 21:25:34 +0700189 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100190 if(_interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100192 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100194
195 // Configure kernel window
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100196 auto win_config = validate_and_configure_window(input->info(), output->info(), info, border);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100197 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100198 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
200 // Create kernel
Daniil Efremov7a49c792017-11-14 21:25:34 +0700201 CLBuildOptions build_opts;
202 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000203 build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, input->info()->data_type()));
Daniil Efremov7a49c792017-11-14 21:25:34 +0700204 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100205 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000206 build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100207 build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
Sang-Hoon Park94d50512020-07-02 10:49:39 +0100208 build_opts.add_option_if(_align_corners, "-DALIGN_CORNERS");
Michalis Spyrou17220e22018-09-12 13:35:38 +0100209 if(call_quantized_kernel)
210 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100211 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
212 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
213 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100214 }
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100215 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100217 std::string kernel_name = "scale_" + interpolation_name;
218 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000219 kernel_name += lower_string(string_from_data_layout(_data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100220 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000222 unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000224 const unsigned int input_width = input->info()->dimension(idx_width);
225 const unsigned int input_height = input->info()->dimension(idx_height);
steniu01f81652d2017-09-11 15:29:12 +0100226
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100227 _kernel.setArg<float>(idx++, input_width);
228 _kernel.setArg<float>(idx++, input_height);
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000229 _kernel.setArg<float>(idx++, wr);
230 _kernel.setArg<float>(idx++, hr);
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000231
232 // Set config_id for enabling LWS tuning
233 _config_id = "scale_";
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100234 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
235 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000236 _config_id += (is_nhwc ? "nhwc" : "nchw");
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000237 _config_id += "_";
238 _config_id += support::cpp11::to_string(output->info()->dimension(0));
239 _config_id += "_";
240 _config_id += support::cpp11::to_string(output->info()->dimension(1));
241 _config_id += "_";
242 _config_id += support::cpp11::to_string(output->info()->dimension(2));
243 _config_id += "_";
244 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000245 if(is_nhwc)
246 {
247 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
248 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100250
251void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
252{
253 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
254 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000255 switch(_data_layout)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100256 {
257 case DataLayout::NCHW:
258 {
259 Window slice = window.first_slice_window_2D();
260
261 do
262 {
263 unsigned int idx = 0;
264 add_2D_tensor_argument(idx, _input, slice);
265 add_2D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100266 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100267 }
268 while(window.slide_window_slice_2D(slice));
269 break;
270 }
271 case DataLayout::NHWC:
272 {
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000273 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
274 Window slice = collapsed.first_slice_window_4D();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100275
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000276 unsigned int idx = 0;
277 add_4D_tensor_argument(idx, _input, slice);
278 add_4D_tensor_argument(idx, _output, slice);
279 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100280 break;
281 }
282 default:
283 ARM_COMPUTE_ERROR("Data layout not supported");
284 }
285}
Michele Di Giorgio655e8c62021-01-28 12:51:02 +0000286} // namespace arm_compute