blob: b63e2167b7c08e56f3646e11f726e61e4392824d [file] [log] [blame]
Gunes Bayir16c56972022-03-28 21:32:33 +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 */
SiCong Li4e9f5682022-05-10 10:15:59 +010024#ifdef ENABLE_EXPERIMENTAL_DYNAMIC_FUSION
Gunes Bayir16c56972022-03-28 21:32:33 +010025
26#include "src/core/experimental/dynamic_fusion/ClKernelBuildingImpl/components/ClDirectConvolutionKernelComponent.h"
27
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "src/core/CL/ICLKernel.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
32#include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h"
33
SiCong Lib63b1192022-01-28 18:24:39 +000034#include "arm_compute/runtime/CL/CLScheduler.h"
Gunes Bayir16c56972022-03-28 21:32:33 +010035namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41ComponentType ClDirectConvolutionKernelComponent::get_component_type() const
42{
43 return ComponentType::Complex;
44}
45
46std::set<std::string> ClDirectConvolutionKernelComponent::get_headers_list() const
47{
SiCong Lib63b1192022-01-28 18:24:39 +000048 return std::set<std::string> { "helpers.h", "tile_helpers.h" };
Gunes Bayir16c56972022-03-28 21:32:33 +010049}
50
51Window ClDirectConvolutionKernelComponent::get_window() const
52{
53 const auto src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id);
54 const auto weight_info = _blueprint->impl().get_kernel_argument_info(_weight.arg_id);
55 auto dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id());
56
57 // Get dst shape
SiCong Lib63b1192022-01-28 18:24:39 +000058 PadStrideInfo pad_stride_info
59 {
60 static_cast<unsigned int>(_desc.conv2d.stride.x()),
61 static_cast<unsigned int>(_desc.conv2d.stride.y()),
62 static_cast<unsigned int>(_desc.conv2d.pad.left),
63 static_cast<unsigned int>(_desc.conv2d.pad.right),
64 static_cast<unsigned int>(_desc.conv2d.pad.top),
65 static_cast<unsigned int>(_desc.conv2d.pad.bottom),
66 DimensionRoundingType::FLOOR /*default rounding type*/
67 };
68 TensorShape output_shape = misc::shape_calculator::compute_deep_convolution_shape(*src_info, *weight_info, pad_stride_info);
Gunes Bayir16c56972022-03-28 21:32:33 +010069
70 // Output auto initialization if not yet initialized
71 auto_init_if_empty(*dst_info, output_shape,
72 1,
73 src_info->data_type(),
74 src_info->quantization_info());
75
76 const unsigned int vec_size = std::min(static_cast<unsigned int>(dst_info->tensor_shape()[0]), 4u);
77 const unsigned int num_rows = (dst_info->tensor_shape()[0] > 16) ? ((src_info->data_type() == DataType::F32) ? 2U : 4U) : 1U;
SiCong Lib63b1192022-01-28 18:24:39 +000078 // const unsigned int num_rows = 1;
79 // const unsigned int vec_size = tile_info.tile_dims.x();
80 // const unsigned int num_rows = tile_info.tile_dims.y();
Gunes Bayir16c56972022-03-28 21:32:33 +010081
82 // Create and configure kernel window
83 Window win = calculate_max_window(output_shape, Steps(vec_size, num_rows));
84
85 const size_t dim_y_collapsed = ceil_to_multiple(output_shape[1] * output_shape[2], num_rows);
86 win.set(Window::DimY, Window::Dimension(0, dim_y_collapsed, num_rows));
87 win.set(Window::DimZ, Window::Dimension(0, output_shape.total_size_upper(3), 1));
88
89 return win;
90}
91
92std::string ClDirectConvolutionKernelComponent::get_additional_macros() const
93{
94 return R"_()_"; // no macros
95}
96
97std::string ClDirectConvolutionKernelComponent::get_component_code() const
98{
99 const auto src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id);
100 const auto bias_info = _blueprint->impl().get_kernel_argument_info(_bias.arg_id);
101
102 ARM_COMPUTE_ERROR_ON_MSG(src_info->data_layout() != DataLayout::NHWC, "Only NHWC data layout is supported by this component.");
103
104 const auto channel_idx = get_data_layout_dimension_index(src_info->data_layout(), DataLayoutDimension::CHANNEL);
105 const auto k0 = adjust_vec_size(is_data_type_quantized(src_info->data_type()) ? 16u : 8u, src_info->dimension(channel_idx));
106 const bool leftover_loop = (src_info->dimension(channel_idx) % k0) != 0;
107
108 std::string code = R"_(
109 //------------------ START KERNEL {{meta_kernel_id}} ---------------------
110 // IN_0(src) {{src}}
111 // IN_1(wei) {{weight}}
SiCong Lib63b1192022-01-28 18:24:39 +0000112 )_";
113 if(bias_info != nullptr)
114 {
115 code += R"_(
Gunes Bayir16c56972022-03-28 21:32:33 +0100116 // IN_1(bia) {{bias}}
SiCong Lib63b1192022-01-28 18:24:39 +0000117 )_";
118 }
119 code += R"_(
Gunes Bayir16c56972022-03-28 21:32:33 +0100120 // OUT(dst, accum) {{dst}}
121
Gunes Bayir16c56972022-03-28 21:32:33 +0100122 // Initialize the accumulators
123 TILE({{ACC_DATA_TYPE}}, M0, N0, {{dst}});
124 {
125 // All the tensor dimensions are passed at compile time.
126 // In case of dynamic tensor support, the following dimensions should be passed as function argument.
SiCong Lib63b1192022-01-28 18:24:39 +0000127 #define _IWEI_WIDTH {{WEI_WIDTH}}
128 #define _IWEI_HEIGHT {{WEI_HEIGHT}}
Gunes Bayir16c56972022-03-28 21:32:33 +0100129 #define _ISRC_WIDTH {{src}}_w
130 #define _ISRC_HEIGHT {{src}}_h
131 #define _ISRC_CHANNELS {{src}}_c
SiCong Lib63b1192022-01-28 18:24:39 +0000132 #define _IDST_WIDTH {{arg_dst}}_w
133 #define _IDST_HEIGHT {{arg_dst}}_h
134 #define _IDST_CHANNELS {{arg_dst}}_c
135 #define _IY_MULTIPLIER (_IWEI_WIDTH * _IWEI_HEIGHT)
Gunes Bayir16c56972022-03-28 21:32:33 +0100136
137 // .v = access the whole vector (OpenCL vector)
138 // .s[x] = access the vector element at position x (scalar access)
139 TILE(int, M0, 1, xi);
140 TILE(int, M0, 1, yi);
141
142 // Convert the linear index to coordinate
143 LOOP_UNROLLING(int, i, 0, 1, M0,
144 {
145 xi[i].v = ((mout + i) % _IDST_WIDTH) * {{STRIDE_X}};
146 yi[i].v = ((mout + i) / _IDST_WIDTH) * {{STRIDE_Y}};
147 xi[i].v -= {{PAD_LEFT}};
148 yi[i].v -= {{PAD_TOP}};
149 })
150
151 LOOP_UNROLLING(int, i, 0, 1, M0,
152 {
153 {{dst}}[i].v = 0;
154 })
155
SiCong Lib63b1192022-01-28 18:24:39 +0000156 for(int i = 0; i < (_IWEI_WIDTH * _IWEI_HEIGHT); ++i)
Gunes Bayir16c56972022-03-28 21:32:33 +0100157 {
158 int ck = 0;
SiCong Lib63b1192022-01-28 18:24:39 +0000159 int xk = i % _IWEI_WIDTH;
160 int yk = i / _IWEI_HEIGHT;
Gunes Bayir16c56972022-03-28 21:32:33 +0100161
162 int k = 0;
163 for(; k <= (_ISRC_CHANNELS - K0); k += K0)
164 {
165 TILE({{SRC_DATA_TYPE}}, M0, K0, a);
166 TILE({{WEI_DATA_TYPE}}, N0, K0, b);
167
168 LOOP_UNROLLING(int, i, 0, 1, M0,
169 {
170 a[i].v = {{ZERO_VALUE}};
171 })
172
173 // Load tile from the src tensor
174 T_LOAD_NHWC_INDIRECT({{SRC_DATA_TYPE}}, M0, K0, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yk, xk, ck, _ISRC_WIDTH, _ISRC_HEIGHT, {{src}}_stride_y, xi, yi, a);
175
176 // Load tile from the weights tensor
177 T_LOAD({{WEI_DATA_TYPE}}, N0, K0, {{WEI_TENSOR_TYPE}}, {{weight}}, ck, cout * _IY_MULTIPLIER + i, _IY_MULTIPLIER, {{weight}}_stride_y, b);
178
179 // Compute the matrix multiplication between two tiles
180 T_MMUL({{SRC_DATA_TYPE}}, {{WEI_DATA_TYPE}}, {{ACC_DATA_TYPE}}, M0, N0, K0, NT, T, a, b, {{dst}});
181
182 ck += K0;
183 }
184
185 // We voluntarily use SRC_CHANNELS rather than _DSRC_CHANNELS
186 // This #if directive should be removed in case of dynamic tensor support
187 )_";
188
189 if(leftover_loop)
190 {
191 code += R"_(
192 // Left-over accumulations
193 for(; k < _ISRC_CHANNELS; ++k)
194 {
195 TILE({{SRC_DATA_TYPE}}, M0, 1, a);
196 TILE({{WEI_DATA_TYPE}}, N0, 1, b);
197
198 LOOP_UNROLLING(int, i, 0, 1, M0,
199 {
200 a[i].v = {{ZERO_VALUE}};
201 })
202
203 // Load tile from the src tensor
204 T_LOAD_NHWC_INDIRECT({{SRC_DATA_TYPE}}, M0, 1, {{SRC_TENSOR_TYPE}}, {{src}}, bout, yk, xk, ck, _ISRC_WIDTH, _ISRC_HEIGHT, {{src}}_stride_y, xi, yi, a);
205
206 // Load tile from the weights tensor
207 // The T_LOAD for the left-over elements can only use BUFFER because we load one element per iteration
208 T_LOAD({{WEI_DATA_TYPE}}, N0, 1, BUFFER, {{weight}}, ck, cout * _IY_MULTIPLIER + i, _IY_MULTIPLIER, {{weight}}_stride_y, b);
209
210 // Compute the matrix multiplication between two tiles
211 T_MMUL({{SRC_DATA_TYPE}}, {{WEI_DATA_TYPE}}, {{ACC_DATA_TYPE}}, M0, N0, 1, NT, T, a, b, {{dst}});
212
213 ++ck;
214 }
215 )_";
216 }
217
218 code += R"_(
SiCong Lib63b1192022-01-28 18:24:39 +0000219 #undef _I_WEI_WIDTH
220 #undef _I_WEI_HEIGHT
221 #undef _ISRC_WIDTH
222 #undef _ISRC_HEIGHT
223 #undef _ISRC_CHANNELS
224 #undef _IDST_WIDTH
225 #undef _IDST_HEIGHT
226 #undef _IDST_CHANNELS
227 #undef _IY_MULTIPLIER
228
Gunes Bayir16c56972022-03-28 21:32:33 +0100229 }
230 )_";
231
232 if(bias_info != nullptr)
233 {
234 code += R"_(
235 TILE({{BIA_DATA_TYPE}}, 1, N0, bias0);
236
237 T_LOAD({{BIA_DATA_TYPE}}, 1, N0, BUFFER, {{bias}}, cout, 0, 1, 0, bias0);
238
239 // c = c + bias[broadcasted]
240 T_ADD_BROADCAST_X({{ACC_DATA_TYPE}}, M0, N0, {{dst}}, bias0, {{dst}});
241 )_";
242 }
243
244 code += R"_(
Gunes Bayir16c56972022-03-28 21:32:33 +0100245 }
Gunes Bayir16c56972022-03-28 21:32:33 +0100246//------------------ END KERNEL {{meta_kernel_id}} ---------------------
247 )_";
248 return code.c_str();
249}
250
251bool export_to_cl_image_support(const ITensorInfo *tensor, GPUTarget gpu_target, DataLayout data_layout)
252{
253 if(tensor->tensor_shape()[0] % 4 || (data_layout != DataLayout::NHWC))
254 {
255 return false;
256 }
257
258 // If not floating point
259 if(!is_data_type_float(tensor->data_type()))
260 {
261 return false;
262 }
263
264 if(gpu_target == GPUTarget::G71 || get_arch_from_target(gpu_target) == GPUTarget::MIDGARD)
265 {
266 return false;
267 }
268
269 // Check if the cl_khr_image2d_from_buffer extension is supported on the target platform
270 if(!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()))
271 {
272 return false;
273 }
274
275 // Check cl image pitch alignment
276 if(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0)
277 {
278 return false;
279 }
280
281 const size_t image_w = tensor->tensor_shape()[0] / 4;
282 const size_t image_h = tensor->tensor_shape()[1] * tensor->tensor_shape()[2] * tensor->tensor_shape()[3];
283 const size_t max_image_w = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>();
284 const size_t max_image_h = CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>();
285
286 if(image_w > max_image_w || image_h > max_image_h)
287 {
288 return false;
289 }
290
291 return true;
292}
293
294CLBuildOptions ClDirectConvolutionKernelComponent::generate_build_options() const
295{
296 const auto src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id);
SiCong Lib63b1192022-01-28 18:24:39 +0000297 auto weight_info = _blueprint->impl().get_kernel_argument_info(_weight.arg_id);
Gunes Bayir16c56972022-03-28 21:32:33 +0100298 const auto dst_info = _blueprint->impl().get_kernel_argument_info(_blueprint->impl().get_dst_id());
SiCong Lib63b1192022-01-28 18:24:39 +0000299 // const auto tile_info = _blueprint->impl().get_tile_info();
Gunes Bayir16c56972022-03-28 21:32:33 +0100300
301 const unsigned int channel_idx = get_data_layout_dimension_index(src_info->data_layout(), DataLayoutDimension::CHANNEL);
302 const DataType data_type = src_info->data_type();
SiCong Lib63b1192022-01-28 18:24:39 +0000303 const GPUTarget gpu_target = CLScheduler::get().target();
Gunes Bayir16c56972022-03-28 21:32:33 +0100304
SiCong Lib63b1192022-01-28 18:24:39 +0000305 const unsigned int n0 = _blueprint->impl().get_execution_window().x().step();
306 const unsigned int m0 = _blueprint->impl().get_execution_window().y().step();
Gunes Bayir16c56972022-03-28 21:32:33 +0100307 const unsigned int k0 = adjust_vec_size(is_data_type_quantized(data_type) ? 16u : 8u, src_info->dimension(channel_idx));
SiCong Lib63b1192022-01-28 18:24:39 +0000308 const unsigned int partial_store_n0 = dst_info->dimension(0) % n0;
Gunes Bayir16c56972022-03-28 21:32:33 +0100309 const bool export_to_cl_image = export_to_cl_image_support(weight_info, gpu_target, src_info->data_layout());
310
311 // Update the padding for the weights tensor if we can export to cl_image
312 if(export_to_cl_image)
313 {
314 arm_compute::opencl::kernels::gemm::update_padding_for_cl_image(weight_info);
315 }
316
317 CLBuildOptions build_opts{};
318 build_opts.add_option("-cl-fast-relaxed-math");
319 build_opts.add_option("-DIS_TILED");
320 build_opts.add_option("-DN0=" + support::cpp11::to_string(n0));
321 build_opts.add_option("-DM0=" + support::cpp11::to_string(m0));
322 build_opts.add_option("-DK0=" + support::cpp11::to_string(k0));
323 build_opts.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
324
325 return build_opts;
326}
327
SiCong Lib63b1192022-01-28 18:24:39 +0000328void ClDirectConvolutionKernelComponent::allocate_shared_vars(SharedVarTable &vtable) const
329{
330 const auto src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id);
331 const auto weight_info = _blueprint->impl().get_kernel_argument_info(_weight.arg_id);
332
333 vtable.add(_src, _blueprint->impl().group(_src.arg_id), ClKernelArgDescriptor(_src.arg_id, ClKernelTensorArgType::Tensor_4D_t_Buffer), "src");
334
335 const GPUTarget gpu_target = CLScheduler::get().target();
336 const bool export_to_cl_image = export_to_cl_image_support(weight_info, gpu_target, src_info->data_layout());
337 const ClKernelTensorArgType weight_type = export_to_cl_image ? ClKernelTensorArgType::Tensor_4D_t_Image : ClKernelTensorArgType::Tensor_4D_t_Buffer;
338 vtable.add(_weight, _blueprint->impl().group(_weight.arg_id), ClKernelArgDescriptor(_weight.arg_id, weight_type), "weight");
339
340 if(!_bias.is_empty()) // optional bias
341 {
342 vtable.add(_bias, _blueprint->impl().group(_bias.arg_id), ClKernelArgDescriptor(_bias.arg_id, ClKernelTensorArgType::Vector), "bias");
343 }
344 vtable.add(_dst, _blueprint->impl().group(_dst.arg_id), ClKernelArgDescriptor(_dst.arg_id, ClKernelTensorArgType::Tensor_4D_t_Buffer), "dst");
345}
346
347ClDirectConvolutionKernelComponent::TagLUT ClDirectConvolutionKernelComponent::get_tag_lut(const SharedVarTable &vtable) const
Gunes Bayir16c56972022-03-28 21:32:33 +0100348{
349 TagLUT lut{};
350
351 const auto src_info = _blueprint->impl().get_kernel_argument_info(_src.arg_id);
352 const auto weight_info = _blueprint->impl().get_kernel_argument_info(_weight.arg_id);
353 const auto bias_info = _blueprint->impl().get_kernel_argument_info(_bias.arg_id);
Gunes Bayir16c56972022-03-28 21:32:33 +0100354
SiCong Lib63b1192022-01-28 18:24:39 +0000355 // Arguments and global shared variables
356 lut["src"] = vtable.get(_src);
357 lut["weight"] = vtable.get(_weight);
Gunes Bayir16c56972022-03-28 21:32:33 +0100358
359 if(!_bias.is_empty()) // optional bias
360 {
SiCong Lib63b1192022-01-28 18:24:39 +0000361 lut["bias"] = vtable.get(_bias);
Gunes Bayir16c56972022-03-28 21:32:33 +0100362 lut["BIA_DATA_TYPE"] = get_cl_type_from_data_type(bias_info->data_type());
363 }
SiCong Lib63b1192022-01-28 18:24:39 +0000364 lut["dst"] = vtable.get(_dst);
365
366 const auto dst_argument = _blueprint->impl().get_argument_shared_vars().get_dst_var();
367 lut["arg_dst"] = dst_argument.uniq_name;
Gunes Bayir16c56972022-03-28 21:32:33 +0100368
369 // Local build options
SiCong Lib63b1192022-01-28 18:24:39 +0000370 lut["meta_kernel_id"] = id();
371 lut["ACC_DATA_TYPE"] = src_info->data_type();
372 lut["SRC_DATA_TYPE"] = src_info->data_type();
373 lut["WEI_DATA_TYPE"] = weight_info->data_type();
Gunes Bayir16c56972022-03-28 21:32:33 +0100374
375 lut["SRC_TENSOR_TYPE"] = "BUFFER";
SiCong Lib63b1192022-01-28 18:24:39 +0000376 switch(vtable.get(_weight).desc.tensor_arg_type)
377 {
378 case ClKernelTensorArgType::Image_Export_To_ClImage2D:
379 case ClKernelTensorArgType::Image_3D_Export_To_ClImage2D:
380 case ClKernelTensorArgType::Tensor_4D_t_Image:
381 {
382 lut["WEI_TENSOR_TYPE"] = "IMAGE";
383 break;
384 }
385 default:
386 {
387 lut["WEI_TENSOR_TYPE"] = "BUFFER";
388 break;
389 }
390 }
391 const auto width_idx = get_data_layout_dimension_index(src_info->data_layout(), DataLayoutDimension::WIDTH);
392 const auto height_idx = get_data_layout_dimension_index(src_info->data_layout(), DataLayoutDimension::HEIGHT);
393 lut["WEI_WIDTH"] = weight_info->dimension(width_idx);
394 lut["WEI_HEIGHT"] = weight_info->dimension(height_idx);
Gunes Bayir16c56972022-03-28 21:32:33 +0100395
SiCong Lib63b1192022-01-28 18:24:39 +0000396 lut["STRIDE_X"] = _desc.conv2d.stride.x();
397 lut["STRIDE_Y"] = _desc.conv2d.stride.y();
Gunes Bayir16c56972022-03-28 21:32:33 +0100398
SiCong Lib63b1192022-01-28 18:24:39 +0000399 lut["PAD_LEFT"] = _desc.conv2d.pad.left;
400 lut["PAD_TOP"] = _desc.conv2d.pad.top;
Gunes Bayir16c56972022-03-28 21:32:33 +0100401
402 lut["ZERO_VALUE"] = 0;
403
404 return lut;
405}
406} // namespace dynamic_fusion
407} // namespace experimental
SiCong Li4e9f5682022-05-10 10:15:59 +0100408} // namespace arm_compute
409#endif /* ENABLE_EXPERIMENTAL_DYNAMIC_FUSION */