blob: 82c5c8a446a4393c1f74ef4294144cc5dbd16550 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +00002 * Copyright (c) 2016-2019 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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <set>
38#include <string>
39
40using namespace arm_compute;
41
Michalis Spyrou46da23f2018-04-10 13:41:30 +010042namespace
43{
44inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output)
45{
46 DataLayout data_layout = input.data_layout();
47 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
48 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
49
50 // Compute the ratio between source width/height and destination width/height
51 const unsigned int input_width = input.dimension(idx_width);
52 const unsigned int input_height = input.dimension(idx_height);
53 const unsigned int output_width = output.dimension(idx_width);
54 const unsigned int output_height = output.dimension(idx_height);
55
56 float wr = static_cast<float>(input_width) / static_cast<float>(output_width);
57 float hr = static_cast<float>(input_height) / static_cast<float>(output_height);
58
59 return std::make_pair(wr, hr);
60}
61
62Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy)
63{
64 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michalis Spyrou17220e22018-09-12 13:35:38 +010065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010066 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000068 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Michalis Spyrou46da23f2018-04-10 13:41:30 +010069 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
70
71 float wr = 0.f;
72 float hr = 0.f;
73 std::tie(wr, hr) = calculate_scale_factors(*input, *output);
74
75 ARM_COMPUTE_RETURN_ERROR_ON(policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
76
77 return Status{};
78}
79
80std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, BorderSize &border)
81{
82 Window win{};
83 bool window_changed{};
84 unsigned int num_elems_processed_per_iteration = 0;
85 DataLayout data_layout = input->data_layout();
86
87 switch(data_layout)
88 {
89 case DataLayout::NCHW:
90 {
91 if(border_mode == BorderMode::UNDEFINED)
92 {
93 border = BorderSize(0);
94 }
95
96 num_elems_processed_per_iteration = 4;
97 // Configure kernel window
Michele Di Giorgio17a01a32019-01-03 15:12:27 +000098 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michalis Spyrou46da23f2018-04-10 13:41:30 +010099 AccessWindowStatic input_access(input,
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000100 -border.left, -border.top,
101 input->dimension(0) + border.right,
102 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100103 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
104
105 output_access.set_valid_region(win, calculate_valid_region_scale(*(input),
106 output->tensor_shape(),
107 policy,
108 sampling_policy,
109 border_mode == BorderMode::UNDEFINED));
110
111 window_changed = update_window_and_padding(win, input_access, output_access);
112 }
113 break;
114 case DataLayout::NHWC:
115 {
116 num_elems_processed_per_iteration = 1;
117 // Configure kernel window
118 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000119 AccessWindowStatic input_access(input, -border.left, -border.top,
120 input->dimension(0) + border.right,
121 input->dimension(1) + border.bottom);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100122 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
123 window_changed = update_window_and_padding(win, input_access, output_access);
124 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
125 }
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{
138 return BorderSize(1);
139}
140
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100141Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy,
142 BorderMode border_mode, SamplingPolicy sampling_policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143{
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100144 BorderSize border = BorderSize(1);
145 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, policy));
146 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), policy, border_mode, sampling_policy, border).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100148 return Status{};
149}
150
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100151const ICLTensor *CLScaleKernel::input() const
152{
153 return _input;
154}
155
156const ICLTensor *CLScaleKernel::output() const
157{
158 return _output;
159}
160
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100161void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
162{
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000163 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy));
164
Georgios Pinitas6c95c2d2018-08-20 16:06:58 +0100165 _input = input;
166 _output = output;
167 _interpolationPolicy = policy;
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000168 _data_layout = input->info()->data_layout();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100169
170 float wr = 0.f;
171 float hr = 0.f;
172 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info());
173
Michalis Spyrou17220e22018-09-12 13:35:38 +0100174 const bool call_quantized_kernel = is_data_type_quantized_asymmetric(input->info()->data_type()) && policy == InterpolationPolicy::BILINEAR;
175
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000176 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
177 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
178 const bool is_nhwc = _data_layout == DataLayout::NHWC;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100179
Daniil Efremov7a49c792017-11-14 21:25:34 +0700180 // Compute the ratio between source width/height and destination width/height
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100181 const unsigned int input_width = input->info()->dimension(idx_width);
182 const unsigned int input_height = input->info()->dimension(idx_height);
183 const unsigned int output_width = output->info()->dimension(idx_width);
184 const unsigned int output_height = output->info()->dimension(idx_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +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
189 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
191 {
192 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
193 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100194
195 // Configure kernel window
196 auto win_config = validate_and_configure_window(input->info(), output->info(), policy, border_mode, sampling_policy, border);
197 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()));
203 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100204 build_opts.add_option_if(border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000205 build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700206 build_opts.add_option_if_else(sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
Michalis Spyrou17220e22018-09-12 13:35:38 +0100207 if(call_quantized_kernel)
208 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100209 const UniformQuantizationInfo qinfo = input->info()->quantization_info().uniform();
210 build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
211 build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100212 }
Daniil Efremov7a49c792017-11-14 21:25:34 +0700213
214 std::string interpolation_name = string_from_interpolation_policy(policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100216 std::string kernel_name = "scale_" + interpolation_name;
217 kernel_name += call_quantized_kernel ? "_quantized_" : "_";
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000218 kernel_name += lower_string(string_from_data_layout(_data_layout));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100219 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000221 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 +0100222
223 // Set static kernel arguments
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100224 const float scale_x = static_cast<float>(input_width) / output_width;
225 const float scale_y = static_cast<float>(input_height) / output_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);
steniu01f81652d2017-09-11 15:29:12 +0100229 _kernel.setArg<float>(idx++, scale_x);
230 _kernel.setArg<float>(idx++, scale_y);
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000231
232 // Set config_id for enabling LWS tuning
233 _config_id = "scale_";
Michele Di Giorgio17a01a32019-01-03 15:12:27 +0000234 _config_id += (border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
Vidhya Sudhan Loganathan7a63e792019-01-14 16:29:20 +0000235 _config_id += (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));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100246
247void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
248{
249 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
250 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
251
Georgios Pinitas14d9d982019-12-13 12:33:09 +0000252 switch(_data_layout)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100253 {
254 case DataLayout::NCHW:
255 {
256 Window slice = window.first_slice_window_2D();
257
258 do
259 {
260 unsigned int idx = 0;
261 add_2D_tensor_argument(idx, _input, slice);
262 add_2D_tensor_argument(idx, _output, slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100263 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100264 }
265 while(window.slide_window_slice_2D(slice));
266 break;
267 }
268 case DataLayout::NHWC:
269 {
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000270 Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
271 Window slice = collapsed.first_slice_window_4D();
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100272
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000273 unsigned int idx = 0;
274 add_4D_tensor_argument(idx, _input, slice);
275 add_4D_tensor_argument(idx, _output, slice);
276 enqueue(queue, *this, slice, lws_hint());
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100277 break;
278 }
279 default:
280 ARM_COMPUTE_ERROR("Data layout not supported");
281 }
282}