blob: 846c712cebe1f0f4de04cec362c7347e43e3e0b9 [file] [log] [blame]
Jakub Sujak8ae57142022-12-02 16:09:06 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2022-2023 Arm Limited.
Jakub Sujak8ae57142022-12-02 16:09:06 +00003 *
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
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
29#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Jakub Sujak8ae57142022-12-02 16:09:06 +000031#include "src/core/helpers/WindowHelpers.h"
32#include "src/core/utils/ScaleUtils.h"
33#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
34
35namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041ClTemplateResize::ClTemplateResize(ComponentId id,
42 const ArgumentPack<ITensorInfo> &tensors,
43 const ClTemplateResize::Attributes &attributes)
44 : IGpuTemplateComponentWriter{id, tensors}, _src{}, _dst{}, _attributes{attributes}
Jakub Sujak8ae57142022-12-02 16:09:06 +000045{
46 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
47 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
48
49 ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
50}
51
52std::string ClTemplateResize::get_name() const
53{
54 return _attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "resize_bilinear" : "resize_nearest";
55}
56
57std::string ClTemplateResize::get_component_code(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
58{
59 ARM_COMPUTE_UNUSED(comp_group);
60
61 std::string code = R"_(
62//------------------ START KERNEL {{meta_kernel_id}} ---------------------
Jakub Sujak8ae57142022-12-02 16:09:06 +000063TILE(uint, 1, 1, g_dst_indirect_y);
64{
65 const int yo = g_ind_2 % {{arg_dst}}_h;
66 const int bout = g_ind_2 / {{arg_dst}}_h;
67)_";
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 if (_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR)
Jakub Sujak8ae57142022-12-02 16:09:06 +000070 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
Jakub Sujak8ae57142022-12-02 16:09:06 +000072 {
73 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +000074 float xi_f = (g_ind_1 * {{SCALE_X}});
75 float yi_f = (yo * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +000076)_";
77 }
78 else
79 {
80 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +000081 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}});
82 float yi_f = ((yo + 0.5f) * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +000083)_";
84 }
85
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 if (_attributes.align_corners())
Jakub Sujak8ae57142022-12-02 16:09:06 +000087 {
88 code += R"_(
89 xi_f = round(xi_f);
90 yi_f = round(yi_f);
91)_";
92 }
93
94 code += R"_(
95 const int xi0 = clamp((int)xi_f, 0, (int){{src}}_w - 1);
96 const int yi0 = clamp((int)yi_f, 0, (int){{src}}_h - 1);
97
98 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}});
99)_";
100 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100101 else if (_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR)
Jakub Sujak8ae57142022-12-02 16:09:06 +0000102 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
Jakub Sujak8ae57142022-12-02 16:09:06 +0000104 {
105 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +0000106 float xi_f = (g_ind_1 * {{SCALE_X}});
107 float yi_f = (yo * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +0000108)_";
109 }
110 else
111 {
112 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +0000113 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}} - 0.5f);
114 float yi_f = ((yo + 0.5f) * {{SCALE_Y}} - 0.5f);
Jakub Sujak8ae57142022-12-02 16:09:06 +0000115)_";
116 }
117
118 code += R"_(
119 const int xi = (int)floor(xi_f);
120 const int yi = (int)floor(yi_f);
121
122 TILE({{SRC_DATA_TYPE}}, 1, N0, in00);
123 TILE({{SRC_DATA_TYPE}}, 1, N0, in01);
124 TILE({{SRC_DATA_TYPE}}, 1, N0, in10);
125 TILE({{SRC_DATA_TYPE}}, 1, N0, in11);
126
127 in00[0].v = {{CONSTANT_VALUE}};
128 in01[0].v = {{CONSTANT_VALUE}};
129 in10[0].v = {{CONSTANT_VALUE}};
130 in11[0].v = {{CONSTANT_VALUE}};
131
132 const int xi0 = clamp(xi, 0, (int){{src}}_w - 1);
133 const int yi0 = clamp(yi, 0, (int){{src}}_h - 1);
134 const int xi1 = clamp(xi + 1, 0, (int){{src}}_w - 1);
135 const int yi1 = clamp(yi + 1, 0, (int){{src}}_h - 1);
136
137 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);
138 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);
139 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);
140 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);
141)_";
142
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 if (is_data_type_float(_src->data_type()))
Jakub Sujak8ae57142022-12-02 16:09:06 +0000144 {
145 code += R"_(
146 const {{SRC_DATA_TYPE}} a = ({{SRC_DATA_TYPE}})(xi_f - (float)xi);
147 const {{SRC_DATA_TYPE}} b = ({{SRC_DATA_TYPE}})(1.f - a);
148 const {{SRC_DATA_TYPE}} a1 = ({{SRC_DATA_TYPE}})(yi_f - (float)yi);
149 const {{SRC_DATA_TYPE}} b1 = ({{SRC_DATA_TYPE}})(1.f - a1);
150
151 // Calculate the output
152 {{dst}}[0].v = ((in00[0].v * b * b1) + (in01[0].v * a * b1) + (in10[0].v * b * a1) + (in11[0].v * a * a1));
153)_";
154 }
155 else
156 {
157 code += R"_(
Jakub Sujak8ae57142022-12-02 16:09:06 +0000158 const float a = (xi_f - (float)xi);
159 const float b = (1.f - a);
160 const float a1 = (yi_f - (float)yi);
161 const float b1 = (1.f - a1);
Gunes Bayirb7e86262022-12-26 16:24:04 +0000162
163 {{dst}}[0].v = CONVERT_SAT(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 (CONVERT(in00[0].v, VEC_DATA_TYPE(float, N0)) * b * b1) +
Gunes Bayirb7e86262022-12-26 16:24:04 +0000165 (CONVERT(in01[0].v, VEC_DATA_TYPE(float, N0)) * a * b1) +
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100166 (CONVERT(in10[0].v, VEC_DATA_TYPE(float, N0)) * b * a1) +
Gunes Bayirb7e86262022-12-26 16:24:04 +0000167 (CONVERT(in11[0].v, VEC_DATA_TYPE(float, N0)) * a * a1), VEC_DATA_TYPE({{DST_DATA_TYPE}}, N0));
Jakub Sujak8ae57142022-12-02 16:09:06 +0000168)_";
169 }
170 }
171 else
172 {
173 ARM_COMPUTE_ERROR("Unsupported interpolation policy");
174 }
175
176 code += R"_(
177 g_dst_indirect_y[0].v = g_ind_1 + (yo * (int)({{arg_dst}}_w)) + bout * (int)({{arg_dst}}_w * {{arg_dst}}_h);
178}
179//------------------ END KERNEL {{meta_kernel_id}} ---------------------
180)_";
181
182 return code;
183}
184
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100185void ClTemplateResize::declare_variables(GpuKernelVariableTable &vtable,
186 const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
Jakub Sujak8ae57142022-12-02 16:09:06 +0000187{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 vtable.declare_variable(comp_group, _src, GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
189 "src");
Jakub Sujak8ae57142022-12-02 16:09:06 +0000190
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 vtable.declare_variable(comp_group, _dst, GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
192 "dst");
Jakub Sujak8ae57142022-12-02 16:09:06 +0000193}
194
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195TagLUT ClTemplateResize::get_tag_lut(const GpuKernelVariableTable &vtable,
196 const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
Jakub Sujak8ae57142022-12-02 16:09:06 +0000197{
198 TagLUT lut{};
199
200 // Arguments and global shared variables
201 lut["src"] = vtable.get_variable(_src);
202 lut["dst"] = vtable.get_variable(_dst);
203
204 const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor());
205 lut["arg_dst"] = dst_argument.uniq_name;
206
207 // Local build options
208 lut["meta_kernel_id"] = id();
209 lut["SRC_DATA_TYPE"] = get_cl_type_from_data_type(_src->data_type());
210 lut["SRC_TENSOR_TYPE"] = "BUFFER";
211 lut["DST_DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type());
212 lut["CONSTANT_VALUE"] = string_from_pixel_value(0, _src->data_type());
213
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100214 const float scale_x =
215 scale_utils::calculate_resize_ratio(_src->dimension(1), _dst->dimension(1), _attributes.align_corners());
216 const float scale_y =
217 scale_utils::calculate_resize_ratio(_src->dimension(2), _dst->dimension(2), _attributes.align_corners());
Jakub Sujak8ae57142022-12-02 16:09:06 +0000218
Gunes Bayirb7e86262022-12-26 16:24:04 +0000219 lut["SCALE_X"] = float_to_string_with_full_precision(scale_x);
220 lut["SCALE_Y"] = float_to_string_with_full_precision(scale_y);
Jakub Sujak8ae57142022-12-02 16:09:06 +0000221
222 return lut;
223}
224
225CLBuildOptions ClTemplateResize::get_build_options(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
226{
227 const Window root_window = comp_group.get_root_component()->template_writer()->get_window();
228 const unsigned int n0 = root_window.x().step();
229 const unsigned int m0 = root_window.y().step();
230 const unsigned int partial_n0 = _dst->dimension(0) % n0;
231
Jakub Sujak8ae57142022-12-02 16:09:06 +0000232 CLBuildOptions build_opts;
233
234 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
235 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
236 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_n0));
Jakub Sujak8ae57142022-12-02 16:09:06 +0000237
238 return build_opts;
239}
240
241std::string ClTemplateResize::get_config_id() const
242{
243 std::string config_id{};
244
245 config_id += "resize_";
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100246 config_id +=
247 (_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR ? "NEAREST_NEIGHBOR" : "");
Jakub Sujak8ae57142022-12-02 16:09:06 +0000248 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "BILINEAR" : "");
249 config_id += "_";
250 config_id += (_attributes.sampling_policy() == SamplingPolicy::CENTER ? "center" : "topleft");
251 config_id += "_";
252 config_id += support::cpp11::to_string(_dst->dimension(0));
253 config_id += "_";
254 config_id += support::cpp11::to_string(_dst->dimension(1));
255 config_id += "_";
256 config_id += support::cpp11::to_string(_dst->dimension(2));
257 config_id += "_";
258 config_id += support::cpp11::to_string(_dst->dimension(3));
259
260 return config_id;
261}
262
263std::set<std::string> ClTemplateResize::get_headers_list() const
264{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 return std::set<std::string>{"helpers.h", "tile_helpers.h"};
Jakub Sujak8ae57142022-12-02 16:09:06 +0000266}
267
268Window ClTemplateResize::get_window() const
269{
270 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
271
272 const unsigned int n0 = adjust_vec_size(16 / _src->element_size(), _src->dimension(0));
273 Window win = calculate_max_window(*_dst, Steps(n0));
274 return win.collapse(win, Window::DimZ);
275}
276
277} // namespace dynamic_fusion
278} // namespace experimental
279} // namespace arm_compute