blob: f3d2fa12d506738ce1564050d9cddeb40dda8a47 [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 */
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
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 {
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100123 // Configure kernel window
Manuel Bottini0c58f992020-12-03 16:26:35 +0000124 win = calculate_max_window(*output, Steps());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100125 }
126 break;
127 default:
128 ARM_COMPUTE_ERROR("Data layout not supported");
129 }
130
131 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
132 return std::make_pair(err, win);
133}
134} // namespace
135
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136BorderSize CLScaleKernel::border_size() const
137{
Manuel Bottini0c58f992020-12-03 16:26:35 +0000138 return BorderSize(static_cast<size_t>(_data_layout == DataLayout::NCHW));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}
140
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100141Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000144 BorderSize border = BorderSize(static_cast<size_t>(input->data_layout() == DataLayout::NCHW));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100145 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 +0100146
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100147 return Status{};
148}
149
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100150const ICLTensor *CLScaleKernel::input() const
151{
152 return _input;
153}
154
155const ICLTensor *CLScaleKernel::output() const
156{
157 return _output;
158}
159
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100160void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100161{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100162 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100163}
164
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100165void CLScaleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100166{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100167 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), info));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000168 auto padding_info = get_padding_info({ input, output });
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000169
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100170 _input = input;
171 _output = output;
172 _interpolation_policy = info.interpolation_policy;
173 _data_layout = input->info()->data_layout();
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100174 _align_corners = info.align_corners;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100175
176 float wr = 0.f;
177 float hr = 0.f;
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000178 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), _align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100179
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100180 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 +0100181
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000182 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
183 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
184 const bool is_nhwc = _data_layout == DataLayout::NHWC;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100185
Daniil Efremov7a49c792017-11-14 21:25:34 +0700186 // Compute actual border size
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100187 BorderSize border = border_size();
Daniil Efremov7a49c792017-11-14 21:25:34 +0700188
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100189 auto interpolation_policy_to_use = _interpolation_policy;
Daniil Efremov7a49c792017-11-14 21:25:34 +0700190 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100191 if(_interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100193 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100195
196 // Configure kernel window
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100197 auto win_config = validate_and_configure_window(input->info(), output->info(), info, border);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100198 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100199 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200
201 // Create kernel
Daniil Efremov7a49c792017-11-14 21:25:34 +0700202 CLBuildOptions build_opts;
203 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000204 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 +0700205 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100206 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000207 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 +0100208 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 +0100209 build_opts.add_option_if(_align_corners, "-DALIGN_CORNERS");
Michalis Spyrou17220e22018-09-12 13:35:38 +0100210 if(call_quantized_kernel)
211 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100212 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
213 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
214 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100215 }
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100216 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100218 std::string kernel_name = "scale_" + interpolation_name;
219 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000220 kernel_name += lower_string(string_from_data_layout(_data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100221 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000223 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 +0100224
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000225 const unsigned int input_width = input->info()->dimension(idx_width);
226 const unsigned int input_height = input->info()->dimension(idx_height);
steniu01f81652d2017-09-11 15:29:12 +0100227
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100228 _kernel.setArg<float>(idx++, input_width);
229 _kernel.setArg<float>(idx++, input_height);
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000230 _kernel.setArg<float>(idx++, wr);
231 _kernel.setArg<float>(idx++, hr);
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000232
233 // Set config_id for enabling LWS tuning
234 _config_id = "scale_";
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100235 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
236 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000237 _config_id += (is_nhwc ? "nhwc" : "nchw");
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000238 _config_id += "_";
239 _config_id += support::cpp11::to_string(output->info()->dimension(0));
240 _config_id += "_";
241 _config_id += support::cpp11::to_string(output->info()->dimension(1));
242 _config_id += "_";
243 _config_id += support::cpp11::to_string(output->info()->dimension(2));
244 _config_id += "_";
245 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Manuel Bottini0c58f992020-12-03 16:26:35 +0000246 if(is_nhwc)
247 {
248 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
249 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100251
252void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
253{
254 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
255 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000256 switch(_data_layout)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100257 {
258 case DataLayout::NCHW:
259 {
260 Window slice = window.first_slice_window_2D();
261
262 do
263 {
264 unsigned int idx = 0;
265 add_2D_tensor_argument(idx, _input, slice);
266 add_2D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100267 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100268 }
269 while(window.slide_window_slice_2D(slice));
270 break;
271 }
272 case DataLayout::NHWC:
273 {
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000274 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
275 Window slice = collapsed.first_slice_window_4D();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100276
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000277 unsigned int idx = 0;
278 add_4D_tensor_argument(idx, _input, slice);
279 add_4D_tensor_argument(idx, _output, slice);
280 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100281 break;
282 }
283 default:
284 ARM_COMPUTE_ERROR("Data layout not supported");
285 }
286}