blob: aaed1d990d5b25fa1131c54c269bf6ae00f1281a [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"
Jakub Sujak8ae57142022-12-02 16:09:06 +000030#include "src/core/helpers/WindowHelpers.h"
31#include "src/core/utils/ScaleUtils.h"
32#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
33
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40ClTemplateResize::ClTemplateResize(ComponentId id, const ArgumentPack<ITensorInfo> &tensors, const ClTemplateResize::Attributes &attributes)
41 : IGpuTemplateComponentWriter{ id, tensors }, _src{}, _dst{}, _attributes{ attributes }
42{
43 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
44 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
45
46 ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _dst);
47}
48
49std::string ClTemplateResize::get_name() const
50{
51 return _attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "resize_bilinear" : "resize_nearest";
52}
53
54std::string ClTemplateResize::get_component_code(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
55{
56 ARM_COMPUTE_UNUSED(comp_group);
57
58 std::string code = R"_(
59//------------------ START KERNEL {{meta_kernel_id}} ---------------------
Jakub Sujak8ae57142022-12-02 16:09:06 +000060TILE(uint, 1, 1, g_dst_indirect_y);
61{
62 const int yo = g_ind_2 % {{arg_dst}}_h;
63 const int bout = g_ind_2 / {{arg_dst}}_h;
64)_";
65
66 if(_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR)
67 {
68 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
69 {
70 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +000071 float xi_f = (g_ind_1 * {{SCALE_X}});
72 float yi_f = (yo * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +000073)_";
74 }
75 else
76 {
77 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +000078 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}});
79 float yi_f = ((yo + 0.5f) * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +000080)_";
81 }
82
83 if(_attributes.align_corners())
84 {
85 code += R"_(
86 xi_f = round(xi_f);
87 yi_f = round(yi_f);
88)_";
89 }
90
91 code += R"_(
92 const int xi0 = clamp((int)xi_f, 0, (int){{src}}_w - 1);
93 const int yi0 = clamp((int)yi_f, 0, (int){{src}}_h - 1);
94
95 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}});
96)_";
97 }
98 else if(_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR)
99 {
100 if(_attributes.sampling_policy() == SamplingPolicy::TOP_LEFT)
101 {
102 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +0000103 float xi_f = (g_ind_1 * {{SCALE_X}});
104 float yi_f = (yo * {{SCALE_Y}});
Jakub Sujak8ae57142022-12-02 16:09:06 +0000105)_";
106 }
107 else
108 {
109 code += R"_(
Gunes Bayirb7e86262022-12-26 16:24:04 +0000110 float xi_f = ((g_ind_1 + 0.5f) * {{SCALE_X}} - 0.5f);
111 float yi_f = ((yo + 0.5f) * {{SCALE_Y}} - 0.5f);
Jakub Sujak8ae57142022-12-02 16:09:06 +0000112)_";
113 }
114
115 code += R"_(
116 const int xi = (int)floor(xi_f);
117 const int yi = (int)floor(yi_f);
118
119 TILE({{SRC_DATA_TYPE}}, 1, N0, in00);
120 TILE({{SRC_DATA_TYPE}}, 1, N0, in01);
121 TILE({{SRC_DATA_TYPE}}, 1, N0, in10);
122 TILE({{SRC_DATA_TYPE}}, 1, N0, in11);
123
124 in00[0].v = {{CONSTANT_VALUE}};
125 in01[0].v = {{CONSTANT_VALUE}};
126 in10[0].v = {{CONSTANT_VALUE}};
127 in11[0].v = {{CONSTANT_VALUE}};
128
129 const int xi0 = clamp(xi, 0, (int){{src}}_w - 1);
130 const int yi0 = clamp(yi, 0, (int){{src}}_h - 1);
131 const int xi1 = clamp(xi + 1, 0, (int){{src}}_w - 1);
132 const int yi1 = clamp(yi + 1, 0, (int){{src}}_h - 1);
133
134 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);
135 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);
136 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);
137 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);
138)_";
139
140 if(is_data_type_float(_src->data_type()))
141 {
142 code += R"_(
143 const {{SRC_DATA_TYPE}} a = ({{SRC_DATA_TYPE}})(xi_f - (float)xi);
144 const {{SRC_DATA_TYPE}} b = ({{SRC_DATA_TYPE}})(1.f - a);
145 const {{SRC_DATA_TYPE}} a1 = ({{SRC_DATA_TYPE}})(yi_f - (float)yi);
146 const {{SRC_DATA_TYPE}} b1 = ({{SRC_DATA_TYPE}})(1.f - a1);
147
148 // Calculate the output
149 {{dst}}[0].v = ((in00[0].v * b * b1) + (in01[0].v * a * b1) + (in10[0].v * b * a1) + (in11[0].v * a * a1));
150)_";
151 }
152 else
153 {
154 code += R"_(
Jakub Sujak8ae57142022-12-02 16:09:06 +0000155 const float a = (xi_f - (float)xi);
156 const float b = (1.f - a);
157 const float a1 = (yi_f - (float)yi);
158 const float b1 = (1.f - a1);
Gunes Bayirb7e86262022-12-26 16:24:04 +0000159
160 {{dst}}[0].v = CONVERT_SAT(
161 (CONVERT(in00[0].v, VEC_DATA_TYPE(float, N0)) * b * b1) +
162 (CONVERT(in01[0].v, VEC_DATA_TYPE(float, N0)) * a * b1) +
163 (CONVERT(in10[0].v, VEC_DATA_TYPE(float, N0)) * b * a1) +
164 (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 +0000165)_";
166 }
167 }
168 else
169 {
170 ARM_COMPUTE_ERROR("Unsupported interpolation policy");
171 }
172
173 code += R"_(
174 g_dst_indirect_y[0].v = g_ind_1 + (yo * (int)({{arg_dst}}_w)) + bout * (int)({{arg_dst}}_w * {{arg_dst}}_h);
175}
176//------------------ END KERNEL {{meta_kernel_id}} ---------------------
177)_";
178
179 return code;
180}
181
182void ClTemplateResize::declare_variables(GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
183{
184 vtable.declare_variable(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000185 comp_group,
Jakub Sujak8ae57142022-12-02 16:09:06 +0000186 _src,
187 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
Jakub Sujak8ae57142022-12-02 16:09:06 +0000188 "src");
189
190 vtable.declare_variable(
Viet-Hoa Do3558c582022-12-16 14:45:57 +0000191 comp_group,
Jakub Sujak8ae57142022-12-02 16:09:06 +0000192 _dst,
193 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
Jakub Sujak8ae57142022-12-02 16:09:06 +0000194 "dst");
195}
196
197TagLUT ClTemplateResize::get_tag_lut(const GpuKernelVariableTable &vtable, const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
198{
199 TagLUT lut{};
200
201 // Arguments and global shared variables
202 lut["src"] = vtable.get_variable(_src);
203 lut["dst"] = vtable.get_variable(_dst);
204
205 const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor());
206 lut["arg_dst"] = dst_argument.uniq_name;
207
208 // Local build options
209 lut["meta_kernel_id"] = id();
210 lut["SRC_DATA_TYPE"] = get_cl_type_from_data_type(_src->data_type());
211 lut["SRC_TENSOR_TYPE"] = "BUFFER";
212 lut["DST_DATA_TYPE"] = get_cl_type_from_data_type(_dst->data_type());
213 lut["CONSTANT_VALUE"] = string_from_pixel_value(0, _src->data_type());
214
Gunes Bayirb7e86262022-12-26 16:24:04 +0000215 const float scale_x = scale_utils::calculate_resize_ratio(_src->dimension(1), _dst->dimension(1), _attributes.align_corners());
216 const float scale_y = scale_utils::calculate_resize_ratio(_src->dimension(2), _dst->dimension(2), _attributes.align_corners());
Jakub Sujak8ae57142022-12-02 16:09:06 +0000217
Gunes Bayirb7e86262022-12-26 16:24:04 +0000218 lut["SCALE_X"] = float_to_string_with_full_precision(scale_x);
219 lut["SCALE_Y"] = float_to_string_with_full_precision(scale_y);
Jakub Sujak8ae57142022-12-02 16:09:06 +0000220
221 return lut;
222}
223
224CLBuildOptions ClTemplateResize::get_build_options(const IGpuTemplateComponentWriter::ComponentGroup &comp_group) const
225{
226 const Window root_window = comp_group.get_root_component()->template_writer()->get_window();
227 const unsigned int n0 = root_window.x().step();
228 const unsigned int m0 = root_window.y().step();
229 const unsigned int partial_n0 = _dst->dimension(0) % n0;
230
Jakub Sujak8ae57142022-12-02 16:09:06 +0000231 CLBuildOptions build_opts;
232
233 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
234 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
235 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_n0));
Jakub Sujak8ae57142022-12-02 16:09:06 +0000236
237 return build_opts;
238}
239
240std::string ClTemplateResize::get_config_id() const
241{
242 std::string config_id{};
243
244 config_id += "resize_";
245 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::NEAREST_NEIGHBOR ? "NEAREST_NEIGHBOR" : "");
246 config_id += (_attributes.interpolation_policy() == InterpolationPolicy::BILINEAR ? "BILINEAR" : "");
247 config_id += "_";
248 config_id += (_attributes.sampling_policy() == SamplingPolicy::CENTER ? "center" : "topleft");
249 config_id += "_";
250 config_id += support::cpp11::to_string(_dst->dimension(0));
251 config_id += "_";
252 config_id += support::cpp11::to_string(_dst->dimension(1));
253 config_id += "_";
254 config_id += support::cpp11::to_string(_dst->dimension(2));
255 config_id += "_";
256 config_id += support::cpp11::to_string(_dst->dimension(3));
257
258 return config_id;
259}
260
261std::set<std::string> ClTemplateResize::get_headers_list() const
262{
263 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
264}
265
266Window ClTemplateResize::get_window() const
267{
268 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
269
270 const unsigned int n0 = adjust_vec_size(16 / _src->element_size(), _src->dimension(0));
271 Window win = calculate_max_window(*_dst, Steps(n0));
272 return win.collapse(win, Window::DimZ);
273}
274
275} // namespace dynamic_fusion
276} // namespace experimental
277} // namespace arm_compute