blob: 2e7ee36bcbc33dd14c8ccb2a8615c7591e3ede6f [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
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLKernel.h"
31#include "arm_compute/core/CL/ICLTensor.h"
32#include "arm_compute/core/CL/OpenCL.h"
33#include "arm_compute/core/Error.h"
34#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/TensorInfo.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010038#include "src/core/utils/ScaleUtils.h"
39
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <set>
41#include <string>
42
43using namespace arm_compute;
44
Michalis Spyrou46da23f2018-04-10 13:41:30 +010045namespace
46{
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +000047inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output, bool align_corners)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010048{
49 DataLayout data_layout = input.data_layout();
50 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
51 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
52
53 // Compute the ratio between source width/height and destination width/height
54 const unsigned int input_width = input.dimension(idx_width);
55 const unsigned int input_height = input.dimension(idx_height);
56 const unsigned int output_width = output.dimension(idx_width);
57 const unsigned int output_height = output.dimension(idx_height);
58
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010059 float wr = arm_compute::scale_utils::calculate_resize_ratio(input_width, output_width, align_corners);
60 float hr = arm_compute::scale_utils::calculate_resize_ratio(input_height, output_height, align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010061
62 return std::make_pair(wr, hr);
63}
64
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010065Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010066{
67 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000068 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 +010069 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000071 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010072 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010073 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 +010074
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +010075 const bool will_use_align_corners = info.align_corners;
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +000076
Michalis Spyrou46da23f2018-04-10 13:41:30 +010077 float wr = 0.f;
78 float hr = 0.f;
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010079 std::tie(wr, hr) = calculate_scale_factors(*input, *output, will_use_align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010080
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010081 ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
Michalis Spyrou46da23f2018-04-10 13:41:30 +010082
83 return Status{};
84}
85
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010086std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const ScaleKernelInfo &info, BorderSize &border)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010087{
88 Window win{};
89 bool window_changed{};
90 unsigned int num_elems_processed_per_iteration = 0;
91 DataLayout data_layout = input->data_layout();
92
93 switch(data_layout)
94 {
95 case DataLayout::NCHW:
96 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +010097 if(info.border_mode == BorderMode::UNDEFINED)
Michalis Spyrou46da23f2018-04-10 13:41:30 +010098 {
99 border = BorderSize(0);
100 }
101
102 num_elems_processed_per_iteration = 4;
103 // Configure kernel window
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000104 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100105 AccessWindowStatic input_access(input,
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000106 -border.left, -border.top,
107 input->dimension(0) + border.right,
108 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100109 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
110
111 output_access.set_valid_region(win, calculate_valid_region_scale(*(input),
112 output->tensor_shape(),
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100113 info.interpolation_policy,
114 info.sampling_policy,
115 info.border_mode == BorderMode::UNDEFINED));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100116
117 window_changed = update_window_and_padding(win, input_access, output_access);
118 }
119 break;
120 case DataLayout::NHWC:
121 {
122 num_elems_processed_per_iteration = 1;
123 // Configure kernel window
124 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000125 AccessWindowStatic input_access(input, -border.left, -border.top,
126 input->dimension(0) + border.right,
127 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100128 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
129 window_changed = update_window_and_padding(win, input_access, output_access);
130 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
131 }
132 break;
133 default:
134 ARM_COMPUTE_ERROR("Data layout not supported");
135 }
136
137 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
138 return std::make_pair(err, win);
139}
140} // namespace
141
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142BorderSize CLScaleKernel::border_size() const
143{
144 return BorderSize(1);
145}
146
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100147Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148{
Sang-Hoon Parkf025a772020-05-26 11:11:32 +0100149 BorderSize border = BorderSize(1);
150
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100151 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, info));
152 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 +0100153
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100154 return Status{};
155}
156
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100157const ICLTensor *CLScaleKernel::input() const
158{
159 return _input;
160}
161
162const ICLTensor *CLScaleKernel::output() const
163{
164 return _output;
165}
166
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100167void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100168{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100169 configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100170}
171
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100172void CLScaleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100173{
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100174 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), info));
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000175
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100176 _input = input;
177 _output = output;
178 _interpolation_policy = info.interpolation_policy;
179 _data_layout = input->info()->data_layout();
Michele Di Giorgio7a81d2a2020-07-30 14:52:16 +0100180 _align_corners = info.align_corners;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100181
182 float wr = 0.f;
183 float hr = 0.f;
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000184 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), _align_corners);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100185
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100186 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 +0100187
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000188 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
189 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
190 const bool is_nhwc = _data_layout == DataLayout::NHWC;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100191
Daniil Efremov7a49c792017-11-14 21:25:34 +0700192 // Compute actual border size
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100193 BorderSize border = border_size();
Daniil Efremov7a49c792017-11-14 21:25:34 +0700194
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100195 auto interpolation_policy_to_use = _interpolation_policy;
Daniil Efremov7a49c792017-11-14 21:25:34 +0700196 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100197 if(_interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 {
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100199 interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100201
202 // Configure kernel window
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100203 auto win_config = validate_and_configure_window(input->info(), output->info(), info, border);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100204 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100205 ICLKernel::configure_internal(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206
207 // Create kernel
Daniil Efremov7a49c792017-11-14 21:25:34 +0700208 CLBuildOptions build_opts;
209 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
210 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100211 build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000212 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 +0100213 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 +0100214 build_opts.add_option_if(_align_corners, "-DALIGN_CORNERS");
Michalis Spyrou17220e22018-09-12 13:35:38 +0100215 if(call_quantized_kernel)
216 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100217 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
218 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
219 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100220 }
Daniil Efremov7a49c792017-11-14 21:25:34 +0700221
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100222 std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100224 std::string kernel_name = "scale_" + interpolation_name;
225 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000226 kernel_name += lower_string(string_from_data_layout(_data_layout));
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100227 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000229 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 +0100230
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000231 const unsigned int input_width = input->info()->dimension(idx_width);
232 const unsigned int input_height = input->info()->dimension(idx_height);
steniu01f81652d2017-09-11 15:29:12 +0100233
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100234 _kernel.setArg<float>(idx++, input_width);
235 _kernel.setArg<float>(idx++, input_height);
Sang-Hoon Park5b7fd872020-01-09 15:27:51 +0000236 _kernel.setArg<float>(idx++, wr);
237 _kernel.setArg<float>(idx++, hr);
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000238
239 // Set config_id for enabling LWS tuning
240 _config_id = "scale_";
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100241 _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
242 _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000243 _config_id += (is_nhwc ? "nhwc" : "nchw");
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000244 _config_id += "_";
245 _config_id += support::cpp11::to_string(output->info()->dimension(0));
246 _config_id += "_";
247 _config_id += support::cpp11::to_string(output->info()->dimension(1));
248 _config_id += "_";
249 _config_id += support::cpp11::to_string(output->info()->dimension(2));
250 _config_id += "_";
251 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100253
254void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
255{
256 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
257 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
258
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000259 switch(_data_layout)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100260 {
261 case DataLayout::NCHW:
262 {
263 Window slice = window.first_slice_window_2D();
264
265 do
266 {
267 unsigned int idx = 0;
268 add_2D_tensor_argument(idx, _input, slice);
269 add_2D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100270 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100271 }
272 while(window.slide_window_slice_2D(slice));
273 break;
274 }
275 case DataLayout::NHWC:
276 {
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000277 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
278 Window slice = collapsed.first_slice_window_4D();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100279
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000280 unsigned int idx = 0;
281 add_4D_tensor_argument(idx, _input, slice);
282 add_4D_tensor_argument(idx, _output, slice);
283 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100284 break;
285 }
286 default:
287 ARM_COMPUTE_ERROR("Data layout not supported");
288 }
289}