blob: 9ca20fa152d588bd0b086c9905f76b876fb59c11 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2022-2023 Arm Limited.
SiCong Lif44bbc52022-08-29 18:25:51 +01003 *
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 "ClKernelRuntime.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
SiCong Lif44bbc52022-08-29 18:25:51 +010026#include "arm_compute/core/CL/ICLTensor.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
SiCong Lif44bbc52022-08-29 18:25:51 +010028#include "src/core/CL/CLUtils.h"
SiCong Li23882a92023-06-28 09:49:45 +010029#ifdef ACL_INTERNAL_TEST_CKW_IN_DF
30#include "src/dynamic_fusion/runtime/gpu/cl/ckw_driver/GpuCkwKernelArgumentsHelpers.h"
31#endif // ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +010032#include "src/dynamic_fusion/sketch/gpu/GpuKernelSourceCode.h"
33#include "src/gpu/cl/ClKernelLibrary.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010034#include "support/Cast.h"
35namespace arm_compute
36{
37namespace experimental
38{
39namespace dynamic_fusion
40{
41using namespace arm_compute::opencl;
42
43void ClKernelRuntime::configure(const ClCompileContext &compile_ctx, const GpuKernelSourceCode &code)
44{
45 // Create kernel from kernel source string
46 opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 _kernel = static_cast<cl::Kernel>(compile_ctx.create_kernel(
48 code.name(),
49 code.name(), // program name has to be provided to differentiate between different unfusable components' kernels.
50 // Each program contains exactly one kernel
51 code.code(), klib.kernel_path() /* Kernel path: Used in cases of embedded kernels */,
52 code.build_options().options(), false /* Is source binary */));
SiCong Lif44bbc52022-08-29 18:25:51 +010053
54 // Configure execution window
55 IClKernel::configure_internal(code.window());
56
57 // Set config id for lws tuning
58 _config_id = code.config_id();
59
60 // Set kernel arguments
61 _arguments = code.arguments();
62}
63
SiCong Li23882a92023-06-28 09:49:45 +010064#ifndef ACL_INTERNAL_TEST_CKW_IN_DF
65
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066inline void ClKernelRuntime::add_tensor_argument(unsigned int &idx,
67 const GpuKernelArgumentInfo &arg,
68 const ICLTensor *tensor,
69 const Window &arg_slice,
70 std::vector<cl::Image2D> &cl_images)
SiCong Lif44bbc52022-08-29 18:25:51 +010071{
SiCong Li19844f62023-05-16 16:46:34 +010072 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
73
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 switch (arg.type)
SiCong Lif44bbc52022-08-29 18:25:51 +010075 {
76 case GpuKernelArgumentInfo::Type::Scalar:
77 {
78 ARM_COMPUTE_ERROR("Unsupported yet");
79 break;
80 }
81
82 case GpuKernelArgumentInfo::Type::Vector:
83 {
84 add_1D_tensor_argument(idx, tensor, arg_slice);
85 break;
86 }
87
88 case GpuKernelArgumentInfo::Type::Image:
89 {
90 add_2D_tensor_argument(idx, tensor, arg_slice);
91 break;
92 }
93 case GpuKernelArgumentInfo::Type::Image_Reinterpret_As_3D:
94 {
95 add_2D_tensor_argument(idx, tensor, arg_slice);
96 const unsigned int total_cross_plane_pad = tensor->info()->padding().top + tensor->info()->padding().bottom;
97 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad));
98 break;
99 }
100 case GpuKernelArgumentInfo::Type::Image_Export_To_ClImage2D:
101 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) *
103 tensor->info()->dimension(2) *
104 tensor->info()->dimension(3));
SiCong Lif44bbc52022-08-29 18:25:51 +0100105 const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1];
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 cl::Image2D tensor_image2d =
107 create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d,
108 tensor->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +0100109 cl_images.push_back(tensor_image2d);
110 _kernel.setArg(idx++, tensor_image2d);
111 break;
112 }
113
114 case GpuKernelArgumentInfo::Type::Image_3D:
115 {
116 add_2D_tensor_argument(idx, tensor, arg_slice);
117 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(tensor->info()->strides_in_bytes()[2]));
118 break;
119 }
120 case GpuKernelArgumentInfo::Type::Image_3D_Export_To_ClImage2D:
121 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) *
123 tensor->info()->dimension(2) *
124 tensor->info()->dimension(3));
SiCong Lif44bbc52022-08-29 18:25:51 +0100125 const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1];
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 cl::Image2D tensor_image2d =
127 create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d,
128 tensor->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +0100129 cl_images.push_back(tensor_image2d);
130 _kernel.setArg(idx++, tensor_image2d);
131 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(tensor->info()->strides_in_bytes()[2]));
132 break;
133 }
134
135 case GpuKernelArgumentInfo::Type::Tensor_3D:
136 {
137 add_3D_tensor_argument(idx, tensor, arg_slice);
138 break;
139 }
140
141 case GpuKernelArgumentInfo::Type::Tensor_4D:
142 {
143 add_4D_tensor_argument(idx, tensor, arg_slice);
144 break;
145 }
146 case GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer:
147 {
148 add_4d_tensor_nhwc_argument(idx, tensor);
149 break;
150 }
151 case GpuKernelArgumentInfo::Type::Tensor_4D_t_Image:
152 {
153 const size_t image_w = tensor->info()->dimension(0) / 4;
154 const size_t image_h = tensor->info()->tensor_shape().total_size_upper(1);
155 const size_t image_stride_y = tensor->info()->strides_in_bytes()[1];
156
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 cl::Image2D tensor_image2d = create_image2d_from_buffer(
158 CLKernelLibrary::get().context(), tensor->cl_buffer(), TensorShape(image_w, image_h),
159 tensor->info()->data_type(), image_stride_y, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +0100160 cl_images.push_back(tensor_image2d);
161
162 _kernel.setArg(idx++, tensor_image2d);
163 add_4d_tensor_nhwc_argument(idx, tensor);
164 break;
165 }
SiCong Li19844f62023-05-16 16:46:34 +0100166 case GpuKernelArgumentInfo::Type::Tensor_Special_0:
167 {
168 const ITensorInfo *info = tensor->info();
169 const Strides &strides = info->strides_in_bytes();
170
171 _kernel.setArg(idx++, tensor->cl_buffer());
172 const size_t dim1xdim2 = info->tensor_shape()[1] * info->tensor_shape()[2];
173 _kernel.setArg<cl_int>(idx++, static_cast<int32_t>(dim1xdim2));
174 const size_t stride1 = strides[1];
175 _kernel.setArg<cl_int>(idx++, static_cast<int32_t>(stride1));
176 break;
177 }
SiCong Lif44bbc52022-08-29 18:25:51 +0100178 default:
179 {
180 ARM_COMPUTE_ERROR("Unsupported");
181 }
182 }
183}
184
SiCong Li23882a92023-06-28 09:49:45 +0100185#else // ACL_INTERNAL_TEST_CKW_IN_DF
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186inline void ClKernelRuntime::add_kernel_argument(unsigned int &idx,
187 const GpuKernelArgumentBinding &arg,
188 const ICLTensor *tensor,
189 std::vector<cl::Image2D> &cl_images)
SiCong Li23882a92023-06-28 09:49:45 +0100190{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 switch (arg.type())
SiCong Li23882a92023-06-28 09:49:45 +0100192 {
193 case GpuKernelArgumentBinding::Type::TensorStorage:
194 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100195 switch (arg.tensor_storage_type())
SiCong Li23882a92023-06-28 09:49:45 +0100196 {
197 case TensorStorageType::ClBufferUint8Ptr:
198 {
199 cl_add_buffer_argument(_kernel, idx, tensor->cl_buffer());
200 break;
201 }
202 case TensorStorageType::ClImage2dReadOnly:
203 {
204 cl::Image2D tensor_image2d = create_image2d_from_tensor(tensor, CLImage2DType::ReadOnly);
205 cl_images.push_back(tensor_image2d);
206 cl_add_texture_argument(_kernel, idx, tensor_image2d);
207 break;
208 }
209 case TensorStorageType::ClImage2dWriteOnly:
210 {
211 cl::Image2D tensor_image2d = create_image2d_from_tensor(tensor, CLImage2DType::WriteOnly);
212 cl_images.push_back(tensor_image2d);
213 cl_add_texture_argument(_kernel, idx, tensor_image2d);
214 break;
215 }
216 default:
217 {
218 ARM_COMPUTE_ERROR("Do not accept other TensorStorageType");
219 break;
220 }
221 }
222 break;
223 }
224 case GpuKernelArgumentBinding::Type::TensorComponent:
225 {
226 cl_add_tensor_component_argument(_kernel, idx, tensor, arg.tensor_component_type());
227 break;
228 }
229 default:
230 {
231 ARM_COMPUTE_ERROR("Do not accept other types of kernel arguments");
232 break;
233 }
234 }
235}
236
237#endif // ACL_INTERNAL_TEST_CKW_IN_DF
SiCong Lif44bbc52022-08-29 18:25:51 +0100238void ClKernelRuntime::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
239{
240 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
241 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
242
243 Window slice = window.first_slice_window_3D();
SiCong Lif44bbc52022-08-29 18:25:51 +0100244
245 /// NOTE: Parameters extracted from old kernels. So far they seem to be constant
246 /// but we may need to make them into another configuration passed from GpuWorkloadSourceCode if needed in the future
SiCong Lif44bbc52022-08-29 18:25:51 +0100247 constexpr bool skip_sliding_window = false;
248 constexpr bool use_dummy_work_items = false;
249
250 unsigned int idx = 0;
251 do
252 {
253 // Set kernel arguments
SiCong Lif44bbc52022-08-29 18:25:51 +0100254 // CLImages created from tensor arguments. Need to be retained until enqueue
255 std::vector<cl::Image2D> cl_images;
SiCong Li23882a92023-06-28 09:49:45 +0100256#ifndef ACL_INTERNAL_TEST_CKW_IN_DF
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 for (auto id_arg : _arguments)
SiCong Lif44bbc52022-08-29 18:25:51 +0100258 {
259 const auto arg = id_arg.second;
260 auto tensor = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(id_arg.first));
261 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
262 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor->info());
SiCong Li23882a92023-06-28 09:49:45 +0100263 add_tensor_argument(idx, *arg.kernel_argument_info(), tensor, slice, cl_images);
SiCong Lif44bbc52022-08-29 18:25:51 +0100264 }
265
SiCong Li23882a92023-06-28 09:49:45 +0100266#else // ACL_INTERNAL_TEST_CKW_IN_DF
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100267 for (const auto &arg : _arguments)
SiCong Li23882a92023-06-28 09:49:45 +0100268 {
269 auto tensor = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(arg.id()));
270 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
271 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor->info());
272 add_kernel_argument(idx, arg, tensor, cl_images);
273 }
274#endif // ACL_INTERNAL_TEST_CKW_IN_DF
275
SiCong Lif44bbc52022-08-29 18:25:51 +0100276 // Dispatch kernel
277 enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100278 } while (skip_sliding_window && window.slide_window_slice_3D(slice));
SiCong Lif44bbc52022-08-29 18:25:51 +0100279}
280
281} // namespace dynamic_fusion
282} // namespace experimental
283} // namespace arm_compute