blob: 221addb7b5116fbb3c6b358f53df56c86f099b17 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
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#include "ClTemplateDirectConv2d.h"
25
26#include "src/dynamic_fusion/sketch/gpu/GpuKernelComponentGroup.h"
27#include "src/dynamic_fusion/sketch/gpu/components/cl/ClComponentDirectConv2d.h"
28
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "src/core/helpers/WindowHelpers.h"
31
32#include "support/StringSupport.h"
33
34namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40ClTemplateDirectConv2d::ClTemplateDirectConv2d(ComponentId id,
41 const ArgumentPack<ITensorInfo> &tensors,
42 const Attributes &attributes,
43 const Settings &settings)
44 : IGpuTemplateComponentWriter{ id, tensors },
45 _src{},
46 _weight{},
47 _bias{},
48 _dst{},
49 _attributes{ attributes },
50 _settings{ settings }
51{
52 _src = this->tensors().get_const_tensor(TensorType::ACL_SRC_0);
53 _weight = this->tensors().get_const_tensor(TensorType::ACL_SRC_1);
54 if(this->tensors().get_const_tensor(TensorType::ACL_SRC_2))
55 {
56 _bias = this->tensors().get_const_tensor(TensorType::ACL_SRC_2);
57 }
58 _dst = this->tensors().get_const_tensor(TensorType::ACL_DST_0);
59 ARM_COMPUTE_ERROR_ON_NULLPTR(_src, _weight, _dst);
60}
61
62std::string ClTemplateDirectConv2d::get_name() const
63{
64 return "direct_conv2d";
65}
66
67std::string ClTemplateDirectConv2d::get_component_code(const ComponentGroup &comp_group) const
68{
69 ARM_COMPUTE_UNUSED(comp_group);
70
71 const auto channel_idx = get_data_layout_dimension_index(_src->data_layout(), DataLayoutDimension::CHANNEL);
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +000072 const auto k0 = adjust_vec_size(_settings.direct_conv_descriptor().k0, _src->dimension(channel_idx));
SiCong Lif44bbc52022-08-29 18:25:51 +010073 const bool leftover_loop = (_src->dimension(channel_idx) % k0) != 0;
74
75 std::string code = R"_(
76//------------------ START KERNEL {{meta_kernel_id}} ---------------------
77// IN_0(src) {{src}}
78// IN_1(wei) {{weight}}
79)_";
80 if(_bias && _bias->has_valid_id())
81 {
82 code += R"_(
83// IN_1(bia) {{bias}}
84)_";
85 }
86 code += R"_(
87// OUT(dst, accum) {{dst}}
88
SiCong Lif44bbc52022-08-29 18:25:51 +010089TILE({{ACC_DATA_TYPE}}, M0, N0, {{dst}});
Gunes Bayir7dc02342022-11-21 21:46:50 +000090TILE(uint, M0, 1, g_dst_indirect_y);
91
SiCong Lif44bbc52022-08-29 18:25:51 +010092{
SiCong Lif44bbc52022-08-29 18:25:51 +010093#define _IWEI_WIDTH {{WEI_WIDTH}}
94#define _IWEI_HEIGHT {{WEI_HEIGHT}}
95#define _ISRC_WIDTH {{src}}_w
96#define _ISRC_HEIGHT {{src}}_h
97#define _ISRC_CHANNELS {{src}}_c
98#define _IDST_WIDTH {{arg_dst}}_w
99#define _IDST_HEIGHT {{arg_dst}}_h
100#define _IDST_CHANNELS {{arg_dst}}_c
101#define _IY_MULTIPLIER (_IWEI_WIDTH * _IWEI_HEIGHT)
102
SiCong Lif44bbc52022-08-29 18:25:51 +0100103 TILE(int, M0, 1, xi);
104 TILE(int, M0, 1, yi);
105
106 // Convert the linear index to coordinate
107 LOOP_UNROLLING(int, i, 0, 1, M0,
108 {
Gian Marco Iodice85260d82022-12-16 15:34:27 +0000109 xi[0].s[i] = ((g_ind_1 + i) % _IDST_WIDTH) * {{STRIDE_X}};
110 yi[0].s[i] = ((g_ind_1 + i) / _IDST_WIDTH) * {{STRIDE_Y}};
111 xi[0].s[i] -= {{PAD_LEFT}};
112 yi[0].s[i] -= {{PAD_TOP}};
SiCong Lif44bbc52022-08-29 18:25:51 +0100113 })
114
115 LOOP_UNROLLING(int, i, 0, 1, M0,
116 {
117 {{dst}}[i].v = 0;
118 })
119
120 for(int i = 0; i < (_IWEI_WIDTH * _IWEI_HEIGHT); ++i)
121 {
SiCong Lif44bbc52022-08-29 18:25:51 +0100122 int xk = i % _IWEI_WIDTH;
123 int yk = i / _IWEI_WIDTH;
124
Gian Marco Iodice85260d82022-12-16 15:34:27 +0000125 TILE(int, 1, M0, my);
126
127 LOOP_UNROLLING(int, i, 0, 1, M0,
128 {
129 int x_s = xi[0].s[i] + xk;
130 int y_s = yi[0].s[i] + yk;
131 my[0].s[i] = x_s + y_s *_ISRC_WIDTH;
132 my[0].s[i] = my[0].s[i] + g_ind_2 * (int)(_ISRC_WIDTH * _ISRC_HEIGHT);
133 my[0].s[i] = select(-1, my[0].s[i], x_s >= 0);
134 my[0].s[i] = select(-1, my[0].s[i], x_s < _ISRC_WIDTH);
135 my[0].s[i] = select(-1, my[0].s[i], y_s >= 0);
136 my[0].s[i] = select(-1, my[0].s[i], y_s < _ISRC_HEIGHT);
137 })
138
139 int ck = 0;
140 for(; ck <= (_ISRC_CHANNELS - K0); ck += K0)
SiCong Lif44bbc52022-08-29 18:25:51 +0100141 {
142 TILE({{SRC_DATA_TYPE}}, M0, K0, a);
143 TILE({{WEI_DATA_TYPE}}, N0, K0, b);
144
SiCong Lif44bbc52022-08-29 18:25:51 +0100145 LOOP_UNROLLING(int, i, 0, 1, M0,
146 {
147 a[i].v = {{ZERO_VALUE}};
148 })
149
150 LOOP_UNROLLING(int, i, 0, 1, N0,
151 {
152 b[i].v = {{ZERO_VALUE}};
153 })
154
Gian Marco Iodice85260d82022-12-16 15:34:27 +0000155 T_LOAD2D_INDIRECT({{SRC_DATA_TYPE}}, M0, K0, {{SRC_TENSOR_TYPE}}, {{src}}, ck, {{src}}_stride_y, my, a);
SiCong Lif44bbc52022-08-29 18:25:51 +0100156
SiCong Lif44bbc52022-08-29 18:25:51 +0100157 T_LOAD({{WEI_DATA_TYPE}}, N0, K0, {{WEI_TENSOR_TYPE}}, {{weight}}, ck, g_ind_0 * _IY_MULTIPLIER + i, _IY_MULTIPLIER, {{weight}}_stride_y, b);
158
SiCong Lif44bbc52022-08-29 18:25:51 +0100159 T_MMUL({{SRC_DATA_TYPE}}, {{WEI_DATA_TYPE}}, {{ACC_DATA_TYPE}}, M0, N0, K0, NT, T, a, b, {{dst}});
SiCong Lif44bbc52022-08-29 18:25:51 +0100160 }
SiCong Lif44bbc52022-08-29 18:25:51 +0100161)_";
162
163 if(leftover_loop)
164 {
165 code += R"_(
Gian Marco Iodice85260d82022-12-16 15:34:27 +0000166 for(; ck < _ISRC_CHANNELS; ++ck)
SiCong Lif44bbc52022-08-29 18:25:51 +0100167 {
168 TILE({{SRC_DATA_TYPE}}, M0, 1, a);
169 TILE({{WEI_DATA_TYPE}}, N0, 1, b);
170
SiCong Lif44bbc52022-08-29 18:25:51 +0100171 LOOP_UNROLLING(int, i, 0, 1, M0,
172 {
173 a[i].v = {{ZERO_VALUE}};
174 })
175
176 LOOP_UNROLLING(int, i, 0, 1, N0,
177 {
178 b[i].v = {{ZERO_VALUE}};
179 })
180
Gian Marco Iodice85260d82022-12-16 15:34:27 +0000181 T_LOAD2D_INDIRECT({{SRC_DATA_TYPE}}, M0, 1, {{SRC_TENSOR_TYPE}}, {{src}}, ck, {{src}}_stride_y, my, a);
SiCong Lif44bbc52022-08-29 18:25:51 +0100182
SiCong Lif44bbc52022-08-29 18:25:51 +0100183 T_LOAD({{WEI_DATA_TYPE}}, N0, 1, BUFFER, {{weight}}, ck, g_ind_0 * _IY_MULTIPLIER + i, _IY_MULTIPLIER, {{weight}}_stride_y, b);
184
SiCong Lif44bbc52022-08-29 18:25:51 +0100185 T_MMUL({{SRC_DATA_TYPE}}, {{WEI_DATA_TYPE}}, {{ACC_DATA_TYPE}}, M0, N0, 1, NT, T, a, b, {{dst}});
SiCong Lif44bbc52022-08-29 18:25:51 +0100186 }
187 )_";
188}
189
190code += R"_(
191#undef _I_WEI_WIDTH
192#undef _I_WEI_HEIGHT
193#undef _ISRC_WIDTH
194#undef _ISRC_HEIGHT
195#undef _ISRC_CHANNELS
196#undef _IDST_WIDTH
197#undef _IDST_HEIGHT
198#undef _IDST_CHANNELS
199#undef _IY_MULTIPLIER
200
201 }
202)_";
203
204 if(_bias && _bias->has_valid_id())
205 {
206 code += R"_(
207 TILE({{BIA_DATA_TYPE}}, 1, N0, bias0);
208
209 T_LOAD({{BIA_DATA_TYPE}}, 1, N0, BUFFER, {{bias}}, g_ind_0, 0, 1, 0, bias0);
210
SiCong Lif44bbc52022-08-29 18:25:51 +0100211 T_ELTWISE_BROADCAST_ADD_X({{ACC_DATA_TYPE}}, M0, N0, {{dst}}, bias0, {{dst}});
212 )_";
213}
214
215code += R"_(
Gunes Bayir7dc02342022-11-21 21:46:50 +0000216 LOOP_UNROLLING(int, i, 0, 1, M0,
217 {
218 g_dst_indirect_y[i].v = (uint)min(g_ind_1 + i, (int)({{arg_dst}}_w * {{arg_dst}}_h) - 1);
219 g_dst_indirect_y[i].v += g_ind_2 * (int)({{arg_dst}}_w * {{arg_dst}}_h);
220 })
SiCong Lif44bbc52022-08-29 18:25:51 +0100221}
222//------------------ END KERNEL {{meta_kernel_id}} ---------------------
223)_";
224 return code;
225}
226
227void ClTemplateDirectConv2d::declare_variables(GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
228{
229 vtable.declare_variable(
230 _src,
231 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer),
232 comp_group.is_intermediate_tensor(_src),
233 "src");
234
235 const GpuKernelArgumentInfo::Type weight_type = _settings.export_to_cl_image() ? GpuKernelArgumentInfo::Type::Tensor_4D_t_Image : GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer;
236 vtable.declare_variable(
237 _weight,
238 GpuKernelArgumentInfo(weight_type),
239 comp_group.is_intermediate_tensor(_weight),
240 "weight");
241
242 if(_bias && _bias->has_valid_id()) // optional bias
243 {
244 vtable.declare_variable(
245 _bias,
246 GpuKernelArgumentInfo(GpuKernelArgumentInfo::Type::Vector),
247 comp_group.is_intermediate_tensor(_bias),
248 "bias");
249 }
250 vtable.declare_variable(
251 _dst,
Ramy Elgammal404462a2022-11-08 02:14:46 +0000252 GpuKernelArgumentInfo(common_tensor_type),
SiCong Lif44bbc52022-08-29 18:25:51 +0100253 comp_group.is_intermediate_tensor(_dst),
254 "dst");
255}
256
257TagLUT ClTemplateDirectConv2d::get_tag_lut(const GpuKernelVariableTable &vtable, const ComponentGroup &comp_group) const
258{
259 TagLUT lut{};
260 // Arguments and global shared variables
261 lut["src"] = vtable.get_variable(_src);
262 lut["weight"] = vtable.get_variable(_weight);
263
264 if(_bias && _bias->has_valid_id()) // optional bias
265 {
266 lut["bias"] = vtable.get_variable(_bias);
267 lut["BIA_DATA_TYPE"] = get_cl_type_from_data_type(_bias->data_type());
268 }
269 lut["dst"] = vtable.get_variable(_dst);
270
Viet-Hoa Do04f46202022-12-14 14:49:56 +0000271 const auto dst_argument = vtable.get_variable(comp_group.get_any_dst_tensor());
SiCong Lif44bbc52022-08-29 18:25:51 +0100272 lut["arg_dst"] = dst_argument.uniq_name;
273
274 // Local build options
275 lut["meta_kernel_id"] = id();
276 lut["ACC_DATA_TYPE"] = _src->data_type();
277 lut["SRC_DATA_TYPE"] = _src->data_type();
278 lut["WEI_DATA_TYPE"] = _weight->data_type();
279
280 lut["SRC_TENSOR_TYPE"] = "BUFFER";
281 switch(vtable.get_variable(_weight).kernel_argument_info.type)
282 {
283 case GpuKernelArgumentInfo::Type::Image_Export_To_ClImage2D:
284 case GpuKernelArgumentInfo::Type::Image_3D_Export_To_ClImage2D:
285 case GpuKernelArgumentInfo::Type::Tensor_4D_t_Image:
286 {
287 lut["WEI_TENSOR_TYPE"] = "IMAGE";
288 break;
289 }
290 default:
291 {
292 lut["WEI_TENSOR_TYPE"] = "BUFFER";
293 break;
294 }
295 }
296 const auto width_idx = 1;
297 const auto height_idx = 2;
298 lut["WEI_WIDTH"] = _weight->dimension(width_idx);
299 lut["WEI_HEIGHT"] = _weight->dimension(height_idx);
300
301 lut["STRIDE_X"] = _attributes.stride().x();
302 lut["STRIDE_Y"] = _attributes.stride().y();
303
304 lut["PAD_LEFT"] = _attributes.pad().left;
305 lut["PAD_TOP"] = _attributes.pad().top;
306
307 lut["ZERO_VALUE"] = 0;
308
309 return lut;
310}
311
312CLBuildOptions ClTemplateDirectConv2d::get_build_options(const ComponentGroup &comp_group) const
313{
314 const unsigned int channel_idx = get_data_layout_dimension_index(_src->data_layout(), DataLayoutDimension::CHANNEL);
SiCong Lif44bbc52022-08-29 18:25:51 +0100315
SiCong Lif44bbc52022-08-29 18:25:51 +0100316 const auto root_window = comp_group.get_root_component()->template_writer()->get_window();
317 const unsigned int n0 = root_window.x().step();
318 const unsigned int m0 = root_window.y().step();
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000319 const unsigned int k0 = adjust_vec_size(_settings.direct_conv_descriptor().k0, _src->dimension(channel_idx));
SiCong Lif44bbc52022-08-29 18:25:51 +0100320 const unsigned int partial_store_n0 = _dst->dimension(0) % n0;
321
322 CLBuildOptions build_opts{};
323 if(_settings.fast_relaxed_math())
324 {
325 build_opts.add_option("-cl-fast-relaxed-math");
326 }
327 else
328 {
329 // -cl-fast-relaxed-math also sets -cl-finite-math-only and -cl-unsafe-math-optimizations
330 // to disable -cl-finite-math-only, we only include -cl-unsafe-math-optimizations
331 build_opts.add_option("-cl-unsafe-math-optimizations");
332 }
333 build_opts.add_option("-DIS_TILED");
334 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
335 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
336 build_opts.add_option("-DK0=" + support::cpp11::to_string(k0));
337 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
338
339 return build_opts;
340}
341
342std::string ClTemplateDirectConv2d::get_config_id() const
343{
344 const DataType data_type = _src->data_type();
345 const DataLayout data_layout = _src->data_layout();
346
347 const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
348 const unsigned int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
349
350 const unsigned int kernel_size = _weight->dimension(width_idx);
351
352 std::string config_id{};
353 config_id += lower_string(string_from_data_type(data_type));
354 config_id += "_";
355 config_id += support::cpp11::to_string(kernel_size);
356 config_id += "_";
357 config_id += support::cpp11::to_string(_attributes.stride().x());
358 config_id += "_";
359 config_id += support::cpp11::to_string(_attributes.stride().y());
360 config_id += "_";
361 config_id += support::cpp11::to_string(_dst->dimension(width_idx));
362 config_id += "_";
363 config_id += support::cpp11::to_string(_dst->dimension(height_idx));
364 config_id += "_";
365 config_id += lower_string(string_from_data_layout(data_layout));
366 return config_id;
367}
368
369std::set<std::string> ClTemplateDirectConv2d::get_headers_list() const
370{
371 return std::set<std::string>{ "helpers.h", "tile_helpers.h" };
372}
373
374Window ClTemplateDirectConv2d::get_window() const
375{
376 ARM_COMPUTE_ERROR_ON_MSG(_dst->tensor_shape().total_size() == 0U, "Destination tensor is not initialized");
377
378 const auto output_shape = _dst->tensor_shape();
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000379 const auto desc = _settings.direct_conv_descriptor();
SiCong Lif44bbc52022-08-29 18:25:51 +0100380
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000381 const unsigned int n0 = adjust_vec_size(desc.n0, output_shape[0]);
382 const unsigned int m0 = adjust_vec_size(desc.m0, output_shape[1] * output_shape[2]);
SiCong Lif44bbc52022-08-29 18:25:51 +0100383
384 // Create and configure kernel window
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000385 Window win = calculate_max_window(output_shape, Steps(n0, m0));
SiCong Lif44bbc52022-08-29 18:25:51 +0100386
Ramy Elgammaldf6a3b02022-11-30 16:23:10 +0000387 const size_t dim_y_collapsed = ceil_to_multiple(output_shape[1] * output_shape[2], m0);
388 win.set(Window::DimY, Window::Dimension(0, dim_y_collapsed, m0));
SiCong Lif44bbc52022-08-29 18:25:51 +0100389 win.set(Window::DimZ, Window::Dimension(0, output_shape.total_size_upper(3), 1));
390
391 return win;
392}
393
394} // namespace dynamic_fusion
395} // namespace experimental
396} // namespace arm_compute