blob: 7ee79e82af4bed91dfe8a47d2317fc139af1a4b3 [file] [log] [blame]
Jakub Sujak8ae57142022-12-02 16:09:06 +00001/*
2 * Copyright (c) 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
25#include "ClTemplateResize.h"
26
27#include "src/core/helpers/WindowHelpers.h"
28#include "src/core/utils/ScaleUtils.h"
29#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
30
31namespace arm_compute
32{
33namespace experimental
34{
35namespace dynamic_fusion
36{
37ClTemplateResize::ClTemplateResize(ComponentId id, const ArgumentPack<ITensorInfo> &tensors, const ClTemplateResize::Attributes &attributes)
38 : IGpuTemplateComponentWriter{ id, tensors }, _src{}, _dst{}, _attributes{ attributes }
39{
40 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
41 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
42
43 ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
44}
45
46std::string ClTemplateResize::get_name() const
47{
48 return _attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "resize_bilinear" : "resize_nearest";
49}
50
51std::string ClTemplateResize::get_component_code(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
52{
53 ARM_COMPUTE_UNUSED(comp_group);
54
55 std::string code = R"_(
56//------------------ START KERNEL {{meta_kernel_id}} ---------------------
57TILE({{DST_DATA_TYPE}}, 1, N0, {{dst}});
58TILE(uint, 1, 1, g_dst_indirect_y);
59{
60 const int yo = g_ind_2 % {{arg_dst}}_h;
61 const int bout = g_ind_2 / {{arg_dst}}_h;
62)_";
63
64 if(_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR)
65 {
66 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
67 {
68 code += R"_(
69 float xi_f = (g_ind_1 * SCALE_X);
70 float yi_f = (yo * SCALE_Y);
71)_";
72 }
73 else
74 {
75 code += R"_(
76 float xi_f = ((g_ind_1 + 0.5f) * SCALE_X);
77 float yi_f = ((yo + 0.5f) * SCALE_Y);
78)_";
79 }
80
81 if(_attributes.align_corners())
82 {
83 code += R"_(
84 xi_f = round(xi_f);
85 yi_f = round(yi_f);
86)_";
87 }
88
89 code += R"_(
90 const int xi0 = clamp((int)xi_f, 0, (int){{src}}_w - 1);
91 const int yi0 = clamp((int)yi_f, 0, (int){{src}}_h - 1);
92
93 T_LOAD_NHWC_WITH_DILATION({{SRC_DATA_TYPE}}, 1, 1, N0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yi0, xi0, g_ind_0, {{src}}_w, {{src}}_h, 1, 1, false, {{dst}});
94)_";
95 }
96 else if(_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR)
97 {
98 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
99 {
100 code += R"_(
101 float xi_f = (g_ind_1 * SCALE_X);
102 float yi_f = (yo * SCALE_Y);
103)_";
104 }
105 else
106 {
107 code += R"_(
108 float xi_f = ((g_ind_1 + 0.5f) * SCALE_X - 0.5f);
109 float yi_f = ((yo + 0.5f) * SCALE_Y - 0.5f);
110)_";
111 }
112
113 code += R"_(
114 const int xi = (int)floor(xi_f);
115 const int yi = (int)floor(yi_f);
116
117 TILE({{SRC_DATA_TYPE}}, 1, N0, in00);
118 TILE({{SRC_DATA_TYPE}}, 1, N0, in01);
119 TILE({{SRC_DATA_TYPE}}, 1, N0, in10);
120 TILE({{SRC_DATA_TYPE}}, 1, N0, in11);
121
122 in00[0].v = {{CONSTANT_VALUE}};
123 in01[0].v = {{CONSTANT_VALUE}};
124 in10[0].v = {{CONSTANT_VALUE}};
125 in11[0].v = {{CONSTANT_VALUE}};
126
127 const int xi0 = clamp(xi, 0, (int){{src}}_w - 1);
128 const int yi0 = clamp(yi, 0, (int){{src}}_h - 1);
129 const int xi1 = clamp(xi + 1, 0, (int){{src}}_w - 1);
130 const int yi1 = clamp(yi + 1, 0, (int){{src}}_h - 1);
131
132 T_LOAD_NHWC_WITH_DILATION({{SRC_DATA_TYPE}}, 1, 1, N0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yi0, xi0, g_ind_0, {{src}}_w, {{src}}_h, 1, 1, false, in00);
133 T_LOAD_NHWC_WITH_DILATION({{SRC_DATA_TYPE}}, 1, 1, N0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yi0, xi1, g_ind_0, {{src}}_w, {{src}}_h, 1, 1, false, in01);
134 T_LOAD_NHWC_WITH_DILATION({{SRC_DATA_TYPE}}, 1, 1, N0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yi1, xi0, g_ind_0, {{src}}_w, {{src}}_h, 1, 1, false, in10);
135 T_LOAD_NHWC_WITH_DILATION({{SRC_DATA_TYPE}}, 1, 1, N0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yi1, xi1, g_ind_0, {{src}}_w, {{src}}_h, 1, 1, false, in11);
136)_";
137
138 if(is_data_type_float(_src->data_type()))
139 {
140 code += R"_(
141 const {{SRC_DATA_TYPE}} a = ({{SRC_DATA_TYPE}})(xi_f - (float)xi);
142 const {{SRC_DATA_TYPE}} b = ({{SRC_DATA_TYPE}})(1.f - a);
143 const {{SRC_DATA_TYPE}} a1 = ({{SRC_DATA_TYPE}})(yi_f - (float)yi);
144 const {{SRC_DATA_TYPE}} b1 = ({{SRC_DATA_TYPE}})(1.f - a1);
145
146 // Calculate the output
147 {{dst}}[0].v = ((in00[0].v * b * b1) + (in01[0].v * a * b1) + (in10[0].v * b * a1) + (in11[0].v * a * a1));
148)_";
149 }
150 else
151 {
152 code += R"_(
153 TILE(float, 1, N0, out_f);
154 TILE(float, 1, N0, in00_f);
155 TILE(float, 1, N0, in01_f);
156 TILE(float, 1, N0, in10_f);
157 TILE(float, 1, N0, in11_f);
158
159 const float a = (xi_f - (float)xi);
160 const float b = (1.f - a);
161 const float a1 = (yi_f - (float)yi);
162 const float b1 = (1.f - a1);
163)_"
164 // Dequantize
165 R"_(
166 LOOP_UNROLLING(int, n0, 0, 1, N0,
167 {
168 in00_f[0].s[n0] = ((float)in00[0].s[n0] - (float){{OFFSET}}) * (float){{SCALE}};
169 in01_f[0].s[n0] = ((float)in01[0].s[n0] - (float){{OFFSET}}) * (float){{SCALE}};
170 in10_f[0].s[n0] = ((float)in10[0].s[n0] - (float){{OFFSET}}) * (float){{SCALE}};
171 in11_f[0].s[n0] = ((float)in11[0].s[n0] - (float){{OFFSET}}) * (float){{SCALE}};
172 })
173)_"
174 // Calculate the output in the floating-point domain
175 R"_(
176 out_f[0].v = ((in00_f[0].v * b * b1) + (in01_f[0].v * a * b1) + (in10_f[0].v * b * a1) + (in11_f[0].v * a * a1));
177)_"
178 // Quantize
179 R"_(
180 LOOP_UNROLLING(int, n0, 0, 1, N0,
181 {
182 {{dst}}[0].s[n0] = CONVERT_SAT(out_f[0].s[n0] / (float){{SCALE}} + (float){{OFFSET}}, {{DST_DATA_TYPE}});
183 })
184)_";
185 }
186 }
187 else
188 {
189 ARM_COMPUTE_ERROR("Unsupported interpolation policy");
190 }
191
192 code += R"_(
193 g_dst_indirect_y[0].v = g_ind_1 + (yo * (int)({{arg_dst}}_w)) + bout * (int)({{arg_dst}}_w * {{arg_dst}}_h);
194}
195//------------------ END KERNEL {{meta_kernel_id}} ---------------------
196)_";
197
198 return code;
199}
200
201void ClTemplateResize::declare_variables(GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
202{
203 vtable.declare_variable(
204 _src,
205 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
206 comp_group.is_intermediate_tensor(_src),
207 "src");
208
209 vtable.declare_variable(
210 _dst,
211 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
212 comp_group.is_intermediate_tensor(_dst),
213 "dst");
214}
215
216TagLUT ClTemplateResize::get_tag_lut(const GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
217{
218 TagLUT lut{};
219
220 // Arguments and global shared variables
221 lut["src"] = vtable.get_variable(_src);
222 lut["dst"] = vtable.get_variable(_dst);
223
224 const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor());
225 lut["arg_dst"] = dst_argument.uniq_name;
226
227 // Local build options
228 lut["meta_kernel_id"] = id();
229 lut["SRC_DATA_TYPE"] = get_cl_type_from_data_type(_src->data_type());
230 lut["SRC_TENSOR_TYPE"] = "BUFFER";
231 lut["DST_DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type());
232 lut["CONSTANT_VALUE"] = string_from_pixel_value(0, _src->data_type());
233
234 const bool is_qasymm_bilinear = is_data_type_quantized_asymmetric(_src->data_type())
235 && _attributes.interpolation_policy() == InterpolationPolicy::BILINEAR;
236
237 if(is_qasymm_bilinear)
238 {
239 const UniformQuantizationInfo qinfo = _src->quantization_info().uniform();
240 lut["SCALE"] = support::cpp11::to_string(qinfo.scale);
241 lut["OFFSET"] = support::cpp11::to_string(qinfo.offset);
242 }
243 else
244 {
245 lut["SCALE"] = support::cpp11::to_string(1);
246 lut["OFFSET"] = support::cpp11::to_string(0);
247 }
248
249 return lut;
250}
251
252CLBuildOptions ClTemplateResize::get_build_options(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
253{
254 const Window root_window = comp_group.get_root_component()->template_writer()->get_window();
255 const unsigned int n0 = root_window.x().step();
256 const unsigned int m0 = root_window.y().step();
257 const unsigned int partial_n0 = _dst->dimension(0) % n0;
258
259 const float scale_x = scale_utils::calculate_resize_ratio(_src->dimension(1), _dst->dimension(1), _attributes.align_corners());
260 const float scale_y = scale_utils::calculate_resize_ratio(_src->dimension(2), _dst->dimension(2), _attributes.align_corners());
261
262 CLBuildOptions build_opts;
263
264 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
265 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
266 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_n0));
267 build_opts.add_option("-DSCALE_X=" + float_to_string_with_full_precision(scale_x));
268 build_opts.add_option("-DSCALE_Y=" + float_to_string_with_full_precision(scale_y));
269
270 return build_opts;
271}
272
273std::string ClTemplateResize::get_config_id() const
274{
275 std::string config_id{};
276
277 config_id += "resize_";
278 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR ? "NEAREST_NEIGHBOR" : "");
279 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "BILINEAR" : "");
280 config_id += "_";
281 config_id += (_attributes.sampling_policy() == SamplingPolicy::CENTER ? "center" : "topleft");
282 config_id += "_";
283 config_id += support::cpp11::to_string(_dst->dimension(0));
284 config_id += "_";
285 config_id += support::cpp11::to_string(_dst->dimension(1));
286 config_id += "_";
287 config_id += support::cpp11::to_string(_dst->dimension(2));
288 config_id += "_";
289 config_id += support::cpp11::to_string(_dst->dimension(3));
290
291 return config_id;
292}
293
294std::set<std::string> ClTemplateResize::get_headers_list() const
295{
296 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
297}
298
299Window ClTemplateResize::get_window() const
300{
301 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
302
303 const unsigned int n0 = adjust_vec_size(16 / _src->element_size(), _src->dimension(0));
304 Window win = calculate_max_window(*_dst, Steps(n0));
305 return win.collapse(win, Window::DimZ);
306}
307
308} // namespace dynamic_fusion
309} // namespace experimental
310} // namespace arm_compute