1 /*
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
31 namespace arm_compute
32 {
33 namespace experimental
34 {
35 namespace dynamic_fusion
36 {
ClTemplateResize(ComponentId id,const ArgumentPack<ITensorInfo> & tensors,const ClTemplateResize::Attributes & attributes)37 ClTemplateResize::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
get_name() const46 std::string ClTemplateResize::get_name() const
47 {
48 return _attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "resize_bilinear" : "resize_nearest";
49 }
50
get_component_code(const IGpuTemplateComponentWriter::ComponentGroup & comp_group) const51 std::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}} ---------------------
57 TILE(uint, 1, 1, g_dst_indirect_y);
58 {
59 const int yo = g_ind_2 % {{arg_dst}}_h;
60 const int bout = g_ind_2 / {{arg_dst}}_h;
61 )_";
62
63 if(_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR)
64 {
65 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
66 {
67 code += R"_(
68 float xi_f = (g_ind_1 * {{SCALE_X}});
69 float yi_f = (yo * {{SCALE_Y}});
70 )_";
71 }
72 else
73 {
74 code += R"_(
75 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}});
76 float yi_f = ((yo + 0.5f) * {{SCALE_Y}});
77 )_";
78 }
79
80 if(_attributes.align_corners())
81 {
82 code += R"_(
83 xi_f = round(xi_f);
84 yi_f = round(yi_f);
85 )_";
86 }
87
88 code += R"_(
89 const int xi0 = clamp((int)xi_f, 0, (int){{src}}_w - 1);
90 const int yi0 = clamp((int)yi_f, 0, (int){{src}}_h - 1);
91
92 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}});
93 )_";
94 }
95 else if(_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR)
96 {
97 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
98 {
99 code += R"_(
100 float xi_f = (g_ind_1 * {{SCALE_X}});
101 float yi_f = (yo * {{SCALE_Y}});
102 )_";
103 }
104 else
105 {
106 code += R"_(
107 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}} - 0.5f);
108 float yi_f = ((yo + 0.5f) * {{SCALE_Y}} - 0.5f);
109 )_";
110 }
111
112 code += R"_(
113 const int xi = (int)floor(xi_f);
114 const int yi = (int)floor(yi_f);
115
116 TILE({{SRC_DATA_TYPE}}, 1, N0, in00);
117 TILE({{SRC_DATA_TYPE}}, 1, N0, in01);
118 TILE({{SRC_DATA_TYPE}}, 1, N0, in10);
119 TILE({{SRC_DATA_TYPE}}, 1, N0, in11);
120
121 in00[0].v = {{CONSTANT_VALUE}};
122 in01[0].v = {{CONSTANT_VALUE}};
123 in10[0].v = {{CONSTANT_VALUE}};
124 in11[0].v = {{CONSTANT_VALUE}};
125
126 const int xi0 = clamp(xi, 0, (int){{src}}_w - 1);
127 const int yi0 = clamp(yi, 0, (int){{src}}_h - 1);
128 const int xi1 = clamp(xi + 1, 0, (int){{src}}_w - 1);
129 const int yi1 = clamp(yi + 1, 0, (int){{src}}_h - 1);
130
131 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);
132 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);
133 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);
134 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);
135 )_";
136
137 if(is_data_type_float(_src->data_type()))
138 {
139 code += R"_(
140 const {{SRC_DATA_TYPE}} a = ({{SRC_DATA_TYPE}})(xi_f - (float)xi);
141 const {{SRC_DATA_TYPE}} b = ({{SRC_DATA_TYPE}})(1.f - a);
142 const {{SRC_DATA_TYPE}} a1 = ({{SRC_DATA_TYPE}})(yi_f - (float)yi);
143 const {{SRC_DATA_TYPE}} b1 = ({{SRC_DATA_TYPE}})(1.f - a1);
144
145 // Calculate the output
146 {{dst}}[0].v = ((in00[0].v * b * b1) + (in01[0].v * a * b1) + (in10[0].v * b * a1) + (in11[0].v * a * a1));
147 )_";
148 }
149 else
150 {
151 code += R"_(
152 const float a = (xi_f - (float)xi);
153 const float b = (1.f - a);
154 const float a1 = (yi_f - (float)yi);
155 const float b1 = (1.f - a1);
156
157 {{dst}}[0].v = CONVERT_SAT(
158 (CONVERT(in00[0].v, VEC_DATA_TYPE(float, N0)) * b * b1) +
159 (CONVERT(in01[0].v, VEC_DATA_TYPE(float, N0)) * a * b1) +
160 (CONVERT(in10[0].v, VEC_DATA_TYPE(float, N0)) * b * a1) +
161 (CONVERT(in11[0].v, VEC_DATA_TYPE(float, N0)) * a * a1), VEC_DATA_TYPE({{DST_DATA_TYPE}}, N0));
162 )_";
163 }
164 }
165 else
166 {
167 ARM_COMPUTE_ERROR("Unsupported interpolation policy");
168 }
169
170 code += R"_(
171 g_dst_indirect_y[0].v = g_ind_1 + (yo * (int)({{arg_dst}}_w)) + bout * (int)({{arg_dst}}_w * {{arg_dst}}_h);
172 }
173 //------------------ END KERNEL {{meta_kernel_id}} ---------------------
174 )_";
175
176 return code;
177 }
178
declare_variables(GpuKernelVariableTable & vtable,const IGpuTemplateComponentWriter::ComponentGroup & comp_group) const179 void ClTemplateResize::declare_variables(GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
180 {
181 vtable.declare_variable(
182 comp_group,
183 _src,
184 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
185 "src");
186
187 vtable.declare_variable(
188 comp_group,
189 _dst,
190 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
191 "dst");
192 }
193
get_tag_lut(const GpuKernelVariableTable & vtable,const IGpuTemplateComponentWriter::ComponentGroup & comp_group) const194 TagLUT ClTemplateResize::get_tag_lut(const GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
195 {
196 TagLUT lut{};
197
198 // Arguments and global shared variables
199 lut["src"] = vtable.get_variable(_src);
200 lut["dst"] = vtable.get_variable(_dst);
201
202 const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor());
203 lut["arg_dst"] = dst_argument.uniq_name;
204
205 // Local build options
206 lut["meta_kernel_id"] = id();
207 lut["SRC_DATA_TYPE"] = get_cl_type_from_data_type(_src->data_type());
208 lut["SRC_TENSOR_TYPE"] = "BUFFER";
209 lut["DST_DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type());
210 lut["CONSTANT_VALUE"] = string_from_pixel_value(0, _src->data_type());
211
212 const float scale_x = scale_utils::calculate_resize_ratio(_src->dimension(1), _dst->dimension(1), _attributes.align_corners());
213 const float scale_y = scale_utils::calculate_resize_ratio(_src->dimension(2), _dst->dimension(2), _attributes.align_corners());
214
215 lut["SCALE_X"] = float_to_string_with_full_precision(scale_x);
216 lut["SCALE_Y"] = float_to_string_with_full_precision(scale_y);
217
218 return lut;
219 }
220
get_build_options(const IGpuTemplateComponentWriter::ComponentGroup & comp_group) const221 CLBuildOptions ClTemplateResize::get_build_options(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
222 {
223 const Window root_window = comp_group.get_root_component()->template_writer()->get_window();
224 const unsigned int n0 = root_window.x().step();
225 const unsigned int m0 = root_window.y().step();
226 const unsigned int partial_n0 = _dst->dimension(0) % n0;
227
228 CLBuildOptions build_opts;
229
230 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
231 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
232 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_n0));
233
234 return build_opts;
235 }
236
get_config_id() const237 std::string ClTemplateResize::get_config_id() const
238 {
239 std::string config_id{};
240
241 config_id += "resize_";
242 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR ? "NEAREST_NEIGHBOR" : "");
243 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "BILINEAR" : "");
244 config_id += "_";
245 config_id += (_attributes.sampling_policy() == SamplingPolicy::CENTER ? "center" : "topleft");
246 config_id += "_";
247 config_id += support::cpp11::to_string(_dst->dimension(0));
248 config_id += "_";
249 config_id += support::cpp11::to_string(_dst->dimension(1));
250 config_id += "_";
251 config_id += support::cpp11::to_string(_dst->dimension(2));
252 config_id += "_";
253 config_id += support::cpp11::to_string(_dst->dimension(3));
254
255 return config_id;
256 }
257
get_headers_list() const258 std::set<std::string> ClTemplateResize::get_headers_list() const
259 {
260 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
261 }
262
get_window() const263 Window ClTemplateResize::get_window() const
264 {
265 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
266
267 const unsigned int n0 = adjust_vec_size(16 / _src->element_size(), _src->dimension(0));
268 Window win = calculate_max_window(*_dst, Steps(n0));
269 return win.collapse(win, Window::DimZ);
270 }
271
272 } // namespace dynamic_fusion
273 } // namespace experimental
274 } // namespace arm_compute
275