blob: b1655d5cc190f18781d76624dd4db3ed8e89874e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recasff860cf2018-02-22 13:08:01 +00002 * Copyright (c) 2016-2018 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);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
66 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
68 ARM_COMPUTE_RETURN_ERROR_ON(output == input);
69
70 float wr = 0.f;
71 float hr = 0.f;
72 std::tie(wr, hr) = calculate_scale_factors(*input, *output);
73
74 ARM_COMPUTE_RETURN_ERROR_ON(policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
75
76 return Status{};
77}
78
79std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, BorderSize &border)
80{
81 Window win{};
82 bool window_changed{};
83 unsigned int num_elems_processed_per_iteration = 0;
84 DataLayout data_layout = input->data_layout();
85
86 switch(data_layout)
87 {
88 case DataLayout::NCHW:
89 {
90 if(border_mode == BorderMode::UNDEFINED)
91 {
92 border = BorderSize(0);
93 }
94
95 num_elems_processed_per_iteration = 4;
96 // Configure kernel window
97 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
98 const ValidRegion &input_valid_region = input->valid_region();
99
100 // Reads can occur within the valid region of the input
101 AccessWindowStatic input_access(input,
102 input_valid_region.anchor[0] - border.left, input_valid_region.anchor[1] - border.top,
103 input_valid_region.anchor[0] + input_valid_region.shape[0] + border.right,
104 input_valid_region.anchor[1] + input_valid_region.shape[1] + border.bottom);
105 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
106
107 output_access.set_valid_region(win, calculate_valid_region_scale(*(input),
108 output->tensor_shape(),
109 policy,
110 sampling_policy,
111 border_mode == BorderMode::UNDEFINED));
112
113 window_changed = update_window_and_padding(win, input_access, output_access);
114 }
115 break;
116 case DataLayout::NHWC:
117 {
118 num_elems_processed_per_iteration = 1;
119 // Configure kernel window
120 win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
121 AccessWindowRectangle input_access(input, -border.left, -border.top, num_elems_processed_per_iteration, num_elems_processed_per_iteration);
122 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
151void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
152{
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 _input = input;
154 _output = output;
155
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100156 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy));
157
158 float wr = 0.f;
159 float hr = 0.f;
160 std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info());
161
162 DataLayout data_layout = input->info()->data_layout();
163 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
164 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
165
Daniil Efremov7a49c792017-11-14 21:25:34 +0700166 // Compute the ratio between source width/height and destination width/height
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100167 const unsigned int input_width = input->info()->dimension(idx_width);
168 const unsigned int input_height = input->info()->dimension(idx_height);
169 const unsigned int output_width = output->info()->dimension(idx_width);
170 const unsigned int output_height = output->info()->dimension(idx_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171
Daniil Efremov7a49c792017-11-14 21:25:34 +0700172 // Compute actual border size
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100173 BorderSize border = border_size();
Daniil Efremov7a49c792017-11-14 21:25:34 +0700174
175 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
177 {
178 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
179 }
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100180
181 // Configure kernel window
182 auto win_config = validate_and_configure_window(input->info(), output->info(), policy, border_mode, sampling_policy, border);
183 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
184 ICLKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185
186 // Create kernel
Daniil Efremov7a49c792017-11-14 21:25:34 +0700187 CLBuildOptions build_opts;
188 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
189 build_opts.add_option("-DBORDER_SIZE=" + support::cpp11::to_string(border.right));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100190 build_opts.add_option_if(border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700191 build_opts.add_option_if_else(sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
Daniil Efremov7a49c792017-11-14 21:25:34 +0700192
193 std::string interpolation_name = string_from_interpolation_policy(policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100195 std::string kernel_name = "scale_" + interpolation_name + "_" + lower_string(string_from_data_layout(data_layout));
Daniil Efremov7a49c792017-11-14 21:25:34 +0700196 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100198 unsigned int idx = data_layout == DataLayout::NHWC ? 2 * num_arguments_per_3D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
200 // Set static kernel arguments
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100201 const float scale_x = static_cast<float>(input_width) / output_width;
202 const float scale_y = static_cast<float>(input_height) / output_height;
steniu01f81652d2017-09-11 15:29:12 +0100203
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100204 _kernel.setArg<float>(idx++, input_width);
205 _kernel.setArg<float>(idx++, input_height);
steniu01f81652d2017-09-11 15:29:12 +0100206 _kernel.setArg<float>(idx++, scale_x);
207 _kernel.setArg<float>(idx++, scale_y);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100209
210void CLScaleKernel::run(const Window &window, cl::CommandQueue &queue)
211{
212 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
213 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
214
215 switch(_input->info()->data_layout())
216 {
217 case DataLayout::NCHW:
218 {
219 Window slice = window.first_slice_window_2D();
220
221 do
222 {
223 unsigned int idx = 0;
224 add_2D_tensor_argument(idx, _input, slice);
225 add_2D_tensor_argument(idx, _output, slice);
226 enqueue(queue, *this, slice, _lws_hint);
227 }
228 while(window.slide_window_slice_2D(slice));
229 break;
230 }
231 case DataLayout::NHWC:
232 {
233 Window slice = window.first_slice_window_3D();
234
235 do
236 {
237 unsigned int idx = 0;
238 add_3D_tensor_argument(idx, _input, slice);
239 add_3D_tensor_argument(idx, _output, slice);
240 enqueue(queue, *this, slice, _lws_hint);
241 }
242 while(window.slide_window_slice_3D(slice));
243 break;
244 }
245 default:
246 ARM_COMPUTE_ERROR("Data layout not supported");
247 }
248}