blob: 8233f210b45849364cedf23950283acccb1969ec [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 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 */
24#include "arm_compute/core/CL/kernels/CLScaleKernel.h"
25
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLKernel.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#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
44using namespace arm_compute;
45
Michalis Spyrou46da23f2018-04-10 13:41:30 +010046namespace
47{
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +000048inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output, bool align_corners)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010049{
50 DataLayout data_layout = input.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);
53
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
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010060 float wr = arm_compute::scale_utils::calculate_resize_ratio(input_width, output_width, align_corners);
61 float hr = arm_compute::scale_utils::calculate_resize_ratio(input_height, output_height, 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
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +010076 const bool will_use_align_corners = info.align_corners;
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +000077
Michalis Spyrou46da23f2018-04-10 13:41:30 +010078 float wr = 0.f;
79 float hr = 0.f;
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010080 std::tie(wr, hr) = calculate_scale_factors(*input, *output, will_use_align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010081
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010082 ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
Michalis Spyrou46da23f2018-04-10 13:41:30 +010083
84 return Status{};
85}
86
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010087std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const ScaleKernelInfo &info, BorderSize &border)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010088{
89 Window win{};
90 bool window_changed{};
91 unsigned int num_elems_processed_per_iteration = 0;
92 DataLayout data_layout = input->data_layout();
93
94 switch(data_layout)
95 {
96 case DataLayout::NCHW:
97 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010098 if(info.border_mode == BorderMode::UNDEFINED)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010099 {
100 border = BorderSize(0);
101 }
102
103 num_elems_processed_per_iteration = 4;
104 // Configure kernel window
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000105 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100106 AccessWindowStatic input_access(input,
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000107 -border.left, -border.top,
108 input->dimension(0) + border.right,
109 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100110 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
111
112 output_access.set_valid_region(win, calculate_valid_region_scale(*(input),
113 output->tensor_shape(),
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100114 info.interpolation_policy,
115 info.sampling_policy,
116 info.border_mode == BorderMode::UNDEFINED));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100117
118 window_changed = update_window_and_padding(win, input_access, output_access);
119 }
120 break;
121 case DataLayout::NHWC:
122 {
123 num_elems_processed_per_iteration = 1;
124 // Configure kernel window
125 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000126 AccessWindowStatic input_access(input, -border.left, -border.top,
127 input->dimension(0) + border.right,
128 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100129 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
130 window_changed = update_window_and_padding(win, input_access, output_access);
131 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
132 }
133 break;
134 default:
135 ARM_COMPUTE_ERROR("Data layout not supported");
136 }
137
138 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
139 return std::make_pair(err, win);
140}
141} // namespace
142
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143BorderSize CLScaleKernel::border_size() const
144{
145 return BorderSize(1);
146}
147
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100148Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149{
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100150 BorderSize border = BorderSize(1);
151
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100152 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
153 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 +0100154
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100155 return Status{};
156}
157
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100158const ICLTensor *CLScaleKernel::input() const
159{
160 return _input;
161}
162
163const ICLTensor *CLScaleKernel::output() const
164{
165 return _output;
166}
167
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100168void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100169{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100170 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100171}
172
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100173void CLScaleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100174{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100175 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), info));
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000176
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100177 _input = input;
178 _output = output;
179 _interpolation_policy = info.interpolation_policy;
180 _data_layout = input->info()->data_layout();
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100181 _align_corners = info.align_corners;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100182
183 float wr = 0.f;
184 float hr = 0.f;
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000185 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), _align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100186
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100187 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 +0100188
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000189 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
190 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
191 const bool is_nhwc = _data_layout == DataLayout::NHWC;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100192
Daniil Efremov7a49c792017-11-14 21:25:34 +0700193 // Compute actual border size
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100194 BorderSize border = border_size();
Daniil Efremov7a49c792017-11-14 21:25:34 +0700195
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100196 auto interpolation_policy_to_use = _interpolation_policy;
Daniil Efremov7a49c792017-11-14 21:25:34 +0700197 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100198 if(_interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100200 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100202
203 // Configure kernel window
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100204 auto win_config = validate_and_configure_window(input->info(), output->info(), info, border);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100205 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100206 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207
208 // Create kernel
Daniil Efremov7a49c792017-11-14 21:25:34 +0700209 CLBuildOptions build_opts;
210 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
211 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100212 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000213 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 +0100214 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 +0100215 build_opts.add_option_if(_align_corners, "-DALIGN_CORNERS");
Michalis Spyrou17220e22018-09-12 13:35:38 +0100216 if(call_quantized_kernel)
217 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100218 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
219 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
220 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100221 }
Daniil Efremov7a49c792017-11-14 21:25:34 +0700222
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100223 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100225 std::string kernel_name = "scale_" + interpolation_name;
226 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000227 kernel_name += lower_string(string_from_data_layout(_data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100228 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000230 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 +0100231
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000232 const unsigned int input_width = input->info()->dimension(idx_width);
233 const unsigned int input_height = input->info()->dimension(idx_height);
steniu01f81652d2017-09-11 15:29:12 +0100234
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100235 _kernel.setArg<float>(idx++, input_width);
236 _kernel.setArg<float>(idx++, input_height);
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000237 _kernel.setArg<float>(idx++, wr);
238 _kernel.setArg<float>(idx++, hr);
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000239
240 // Set config_id for enabling LWS tuning
241 _config_id = "scale_";
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100242 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
243 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000244 _config_id += (is_nhwc ? "nhwc" : "nchw");
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000245 _config_id += "_";
246 _config_id += support::cpp11::to_string(output->info()->dimension(0));
247 _config_id += "_";
248 _config_id += support::cpp11::to_string(output->info()->dimension(1));
249 _config_id += "_";
250 _config_id += support::cpp11::to_string(output->info()->dimension(2));
251 _config_id += "_";
252 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100254
255void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
256{
257 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
258 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
259
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000260 switch(_data_layout)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100261 {
262 case DataLayout::NCHW:
263 {
264 Window slice = window.first_slice_window_2D();
265
266 do
267 {
268 unsigned int idx = 0;
269 add_2D_tensor_argument(idx, _input, slice);
270 add_2D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100271 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100272 }
273 while(window.slide_window_slice_2D(slice));
274 break;
275 }
276 case DataLayout::NHWC:
277 {
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000278 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
279 Window slice = collapsed.first_slice_window_4D();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100280
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000281 unsigned int idx = 0;
282 add_4D_tensor_argument(idx, _input, slice);
283 add_4D_tensor_argument(idx, _output, slice);
284 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100285 break;
286 }
287 default:
288 ARM_COMPUTE_ERROR("Data layout not supported");
289 }
290}