• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2022 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/gpu/cl/kernels/ClScaleKernel.h"
25 
26 #include "arm_compute/core/CL/ICLTensor.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "src/core/AccessWindowStatic.h"
29 #include "src/core/CL/CLValidate.h"
30 #include "src/core/helpers/WindowHelpers.h"
31 #include "src/core/utils/ScaleUtils.h"
32 #include "support/Cast.h"
33 
34 namespace arm_compute
35 {
36 namespace opencl
37 {
38 namespace kernels
39 {
40 namespace
41 {
calculate_scale_factors(const ITensorInfo * src,const ITensorInfo * dst,DataLayout data_layout,bool align_corners)42 inline std::pair<float, float> calculate_scale_factors(const ITensorInfo *src, const ITensorInfo *dst, DataLayout data_layout, bool align_corners)
43 {
44     const int idx_width  = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
45     const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
46 
47     // Compute the ratio between source width/height and destination width/height
48     const unsigned int src_width  = src->dimension(idx_width);
49     const unsigned int src_height = src->dimension(idx_height);
50     const unsigned int dst_width  = dst->dimension(idx_width);
51     const unsigned int dst_height = dst->dimension(idx_height);
52 
53     float scale_x = arm_compute::scale_utils::calculate_resize_ratio(src_width, dst_width, align_corners);
54     float scale_y = arm_compute::scale_utils::calculate_resize_ratio(src_height, dst_height, align_corners);
55 
56     return std::make_pair(scale_x, scale_y);
57 }
58 
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst,const ScaleKernelInfo & info)59 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
60 {
61     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
62     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
63     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::U8, DataType::S16, DataType::F16, DataType::F32);
64     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
65     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
66     ARM_COMPUTE_RETURN_ERROR_ON(dst == src);
67     ARM_COMPUTE_RETURN_ERROR_ON(info.align_corners && !arm_compute::scale_utils::is_align_corners_allowed_sampling_policy(info.sampling_policy));
68     ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized(src->data_type()) && !is_data_type_quantized_asymmetric(src->data_type()));
69 
70     float            scale_x     = 0.f;
71     float            scale_y     = 0.f;
72     const DataLayout data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
73     std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, data_layout, info.align_corners);
74 
75     ARM_COMPUTE_RETURN_ERROR_ON(info.interpolation_policy == InterpolationPolicy::AREA && (scale_x > 1.f || scale_y > 1.f));
76 
77     return Status{};
78 }
79 } // namespace
80 
validate(const ITensorInfo * src,const ITensorInfo * dst,const ScaleKernelInfo & info)81 Status ClScaleKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const ScaleKernelInfo &info)
82 {
83     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, info));
84     return Status{};
85 }
86 
ClScaleKernel()87 ClScaleKernel::ClScaleKernel()
88 {
89     _type = CLKernelType::ELEMENTWISE;
90 }
91 
configure(const CLCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const ScaleKernelInfo & info)92 void ClScaleKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const ScaleKernelInfo &info)
93 {
94     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, info));
95     auto padding_info = get_padding_info({ src, dst });
96 
97     // Info required for the static tuning
98     _data_layout = info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : info.data_layout;
99 
100     const bool is_nhwc = _data_layout == DataLayout::NHWC;
101 
102     float scale_x = 0.f;
103     float scale_y = 0.f;
104     std::tie(scale_x, scale_y) = calculate_scale_factors(src, dst, _data_layout, info.align_corners);
105 
106     // Area interpolation behaves as Nearest Neighbour in case of up-sampling
107     auto interpolation_policy_to_use = info.interpolation_policy;
108     if(info.interpolation_policy == InterpolationPolicy::AREA && scale_x <= 1.f && scale_y <= 1.f)
109     {
110         interpolation_policy_to_use = InterpolationPolicy::NEAREST_NEIGHBOR;
111     }
112 
113     // Create kernel
114     const int          idx_width         = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
115     const int          idx_height        = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
116     const int          idx_channel       = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
117     const unsigned int src_width         = src->dimension(idx_width);
118     const unsigned int src_height        = src->dimension(idx_height);
119     const unsigned int dst_width         = dst->dimension(idx_width);
120     const unsigned int dst_channels      = dst->dimension(idx_channel);
121     unsigned int       vec_size          = 0;
122     unsigned int       vec_size_leftover = 0;
123 
124     CLBuildOptions build_opts;
125     if(_data_layout == DataLayout::NHWC)
126     {
127         vec_size          = adjust_vec_size(src->data_type() == DataType::F32 ? 4 : 8, dst_channels);
128         vec_size_leftover = dst_channels % vec_size;
129         build_opts.add_option("-DSRC_TENSOR_TYPE=BUFFER");
130         build_opts.add_option("-DSRC_DATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
131         build_opts.add_option("-DDST_TENSOR_TYPE=BUFFER");
132         build_opts.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst->data_type()));
133         build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
134         build_opts.add_option("-DN0=" + support::cpp11::to_string(vec_size));
135         build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(vec_size_leftover));
136         build_opts.add_option("-DSCALE_" + string_from_interpolation_policy(interpolation_policy_to_use));
137         build_opts.add_option_if(src->num_dimensions() > 3, "-DBATCHED_EXECUTION");
138         build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
139         build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
140         build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
141         build_opts.add_option_if(is_data_type_float(src->data_type()), "-DIS_FLOATING_POINT");
142         build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
143     }
144     else if(_data_layout == DataLayout::NCHW)
145     {
146         vec_size          = adjust_vec_size(4, dst_width);
147         vec_size_leftover = dst_width % vec_size;
148         build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
149         build_opts.add_option("-DCONSTANT_VALUE=" + string_from_pixel_value(info.constant_border_value, src->data_type()));
150         build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
151         build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
152         build_opts.add_option("-DSCALE_X=" + float_to_string_with_full_precision(scale_x));
153         build_opts.add_option("-DSCALE_Y=" + float_to_string_with_full_precision(scale_y));
154         build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size));
155         build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + ((vec_size_leftover == 0) ? support::cpp11::to_string(vec_size) : support::cpp11::to_string(vec_size_leftover)));
156         build_opts.add_option_if(info.border_mode == BorderMode::REPLICATE, "-DBORDER_MODE_REPLICATE");
157         build_opts.add_option_if(info.border_mode == BorderMode::CONSTANT, "-DBORDER_MODE_CONSTANT");
158         build_opts.add_option_if(info.align_corners, "-DALIGN_CORNERS");
159         build_opts.add_option_if_else(info.sampling_policy == SamplingPolicy::CENTER, "-DSAMPLING_POLICY_CENTER", "-DSAMPLING_POLICY_TOP_LEFT");
160 
161         const bool is_qasymm_bilinear = is_data_type_quantized_asymmetric(src->data_type()) && info.interpolation_policy == InterpolationPolicy::BILINEAR;
162         if(is_qasymm_bilinear)
163         {
164             const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
165             build_opts.add_option("-DSCALE=" + support::cpp11::to_string(qinfo.scale));
166             build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(qinfo.offset));
167         }
168     }
169     else
170     {
171         ARM_COMPUTE_ERROR_ON("Unsupported data layout");
172     }
173 
174     std::string interpolation_name = string_from_interpolation_policy(interpolation_policy_to_use);
175     std::transform(interpolation_name.begin(), interpolation_name.end(), interpolation_name.begin(), ::tolower);
176     std::string kernel_name = "scale_" + interpolation_name + "_";
177     kernel_name += lower_string(string_from_data_layout(_data_layout));
178 
179     _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
180 
181     // Configure kernel window
182     Window win = calculate_max_window(*dst, Steps(vec_size));
183     ICLKernel::configure_internal(win);
184 
185     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
186 
187     // Pass scale kernel arguments
188     if(is_nhwc)
189     {
190         unsigned int idx = 2 * num_arguments_per_4d_tensor_nhwc();
191         _kernel.setArg<cl_float>(idx++, scale_x);
192         _kernel.setArg<cl_float>(idx++, scale_y);
193     }
194     // Set config_id for enabling LWS tuning
195     _config_id = "scale_";
196     _config_id += (info.border_mode == BorderMode::REPLICATE ? "Bord_rep" : "");
197     _config_id += (info.sampling_policy == SamplingPolicy::CENTER ? "center" : "topleft");
198     _config_id += (is_nhwc ? "nhwc" : "nchw");
199     _config_id += "_";
200     _config_id += support::cpp11::to_string(dst->dimension(0));
201     _config_id += "_";
202     _config_id += support::cpp11::to_string(dst->dimension(1));
203     _config_id += "_";
204     _config_id += support::cpp11::to_string(dst->dimension(2));
205     _config_id += "_";
206     _config_id += support::cpp11::to_string(dst->dimension(3));
207 }
208 
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)209 void ClScaleKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
210 {
211     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
212     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
213 
214     auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
215     auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
216 
217     switch(_data_layout)
218     {
219         case DataLayout::NCHW:
220         {
221             Window slice = window.first_slice_window_2D();
222 
223             do
224             {
225                 unsigned int idx = 0;
226                 add_2D_tensor_argument(idx, src, slice);
227                 add_2D_tensor_argument(idx, dst, slice);
228                 enqueue(queue, *this, slice, lws_hint());
229             }
230             while(window.slide_window_slice_2D(slice));
231             break;
232         }
233         case DataLayout::NHWC:
234         {
235             Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ);
236             Window slice     = collapsed.first_slice_window_4D();
237 
238             unsigned int idx = 0;
239             add_4d_tensor_nhwc_argument(idx, src);
240             add_4d_tensor_nhwc_argument(idx, dst);
241             enqueue(queue, *this, slice, lws_hint());
242             break;
243         }
244         default:
245             ARM_COMPUTE_ERROR("Data layout not supported");
246     }
247 }
248 } // namespace kernels
249 } // namespace opencl
250 } // namespace arm_compute
251