• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020 Arm Limited.
3  *
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 "src/core/CL/kernels/CLScaleKernel.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #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"
33 #include "src/core/AccessWindowStatic.h"
34 #include "src/core/CL/CLValidate.h"
35 #include "src/core/CL/ICLKernel.h"
36 #include "src/core/helpers/WindowHelpers.h"
37 #include "support/StringSupport.h"
38 
39 #include "src/core/utils/ScaleUtils.h"
40 
41 #include <set>
42 #include <string>
43 
44 using namespace arm_compute;
45 
46 namespace
47 {
calculate_scale_factors(const ITensorInfo & input,const ITensorInfo & output,bool align_corners)48 inline std::pair<float, float> calculate_scale_factors(const ITensorInfo &input, const ITensorInfo &output, bool align_corners)
49 {
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 
60     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);
62 
63     return std::make_pair(wr, hr);
64 }
65 
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const ScaleKernelInfo & info)66 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
67 {
68     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
69     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);
70     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
71     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
72     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
73     ARM_COMPUTE_RETURN_ERROR_ON(output == input);
74     ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
75 
76     const bool will_use_align_corners = info.align_corners;
77 
78     float wr = 0.f;
79     float hr = 0.f;
80     std::tie(wr, hr) = calculate_scale_factors(*input, *output, will_use_align_corners);
81 
82     ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (wr > 1.f || hr > 1.f));
83 
84     return Status{};
85 }
86 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output,const ScaleKernelInfo & info,BorderSize & border)87 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const ScaleKernelInfo &info, BorderSize &border)
88 {
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         {
98             if(info.border_mode == BorderMode::UNDEFINED)
99             {
100                 border = BorderSize(0);
101             }
102 
103             num_elems_processed_per_iteration = 4;
104             // Configure kernel window
105             win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
106             AccessWindowStatic input_access(input,
107                                             -border.left, -border.top,
108                                             input->dimension(0) + border.right,
109                                             input->dimension(1) + border.bottom);
110             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(),
114                                                                              info.interpolation_policy,
115                                                                              info.sampling_policy,
116                                                                              info.border_mode == BorderMode::UNDEFINED));
117 
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));
126             AccessWindowStatic input_access(input, -border.left, -border.top,
127                                             input->dimension(0) + border.right,
128                                             input->dimension(1) + border.bottom);
129             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 
border_size() const143 BorderSize CLScaleKernel::border_size() const
144 {
145     return BorderSize(1);
146 }
147 
validate(const ITensorInfo * input,const ITensorInfo * output,const ScaleKernelInfo & info)148 Status CLScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ScaleKernelInfo &info)
149 {
150     BorderSize border = BorderSize(1);
151 
152     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);
154 
155     return Status{};
156 }
157 
input() const158 const ICLTensor *CLScaleKernel::input() const
159 {
160     return _input;
161 }
162 
output() const163 const ICLTensor *CLScaleKernel::output() const
164 {
165     return _output;
166 }
167 
configure(const ICLTensor * input,ICLTensor * output,const ScaleKernelInfo & info)168 void CLScaleKernel::configure(const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
169 {
170     configure(CLKernelLibrary::get().get_compile_context(), input, output, info);
171 }
172 
configure(const CLCompileContext & compile_context,const ICLTensor * input,ICLTensor * output,const ScaleKernelInfo & info)173 void CLScaleKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const ScaleKernelInfo &info)
174 {
175     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), info));
176 
177     _input                = input;
178     _output               = output;
179     _interpolation_policy = info.interpolation_policy;
180     _data_layout          = input->info()->data_layout();
181     _align_corners        = info.align_corners;
182 
183     float wr = 0.f;
184     float hr = 0.f;
185     std::tie(wr, hr) = calculate_scale_factors(*input->info(), *output->info(), _align_corners);
186 
187     const bool call_quantized_kernel = is_data_type_quantized_asymmetric(input->info()->data_type()) && _interpolation_policy == InterpolationPolicy::BILINEAR;
188 
189     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;
192 
193     // Compute actual border size
194     BorderSize border = border_size();
195 
196     auto interpolation_policy_to_use = _interpolation_policy;
197     // Area interpolation behaves as Nearest Neighbour in case of up-sampling
198     if(_interpolation_policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
199     {
200         interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
201     }
202 
203     // Configure kernel window
204     auto win_config = validate_and_configure_window(input->info(), output->info(), info, border);
205     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
206     ICLKernel::configure_internal(win_config.second);
207 
208     // Create kernel
209     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));
212     build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
213     build_opts.add_option_if(is_nhwc, "-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
214     build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
215     build_opts.add_option_if(_align_corners, "-DALIGN_CORNERS");
216     if(call_quantized_kernel)
217     {
218         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));
221     }
222 
223     std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
224     std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
225     std::string kernel_name = "scale_" + interpolation_name;
226     kernel_name += call_quantized_kernel ? "_quantized_" : "_";
227     kernel_name += lower_string(string_from_data_layout(_data_layout));
228     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
229 
230     unsigned int idx = is_nhwc ? 2 * num_arguments_per_4D_tensor() : 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
231 
232     const unsigned int input_width  = input->info()->dimension(idx_width);
233     const unsigned int input_height = input->info()->dimension(idx_height);
234 
235     _kernel.setArg<float>(idx++, input_width);
236     _kernel.setArg<float>(idx++, input_height);
237     _kernel.setArg<float>(idx++, wr);
238     _kernel.setArg<float>(idx++, hr);
239 
240     // Set config_id for enabling LWS tuning
241     _config_id = "scale_";
242     _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
243     _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
244     _config_id += (is_nhwc ? "nhwc" : "nchw");
245     _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));
253 }
254 
run(const Window & window,cl::CommandQueue & queue)255 void 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 
260     switch(_data_layout)
261     {
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);
271                 enqueue(queue, *this, slice, lws_hint());
272             }
273             while(window.slide_window_slice_2D(slice));
274             break;
275         }
276         case DataLayout::NHWC:
277         {
278             Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
279             Window slice     = collapsed.first_slice_window_4D();
280 
281             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());
285             break;
286         }
287         default:
288             ARM_COMPUTE_ERROR("Data layout not supported");
289     }
290 }
291