blob: 6a57482bb28224058f919044be8034109053c3f5 [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"
25#include "arm_compute/core/CL/ICLTensor.h"
26#include "src/core/CL/CLUtils.h"
27#include "src/dynamic_fusion/sketch/gpu/GpuKernelSourceCode.h"
28#include "src/gpu/cl/ClKernelLibrary.h"
29
30#include "support/Cast.h"
31namespace arm_compute
32{
33namespace experimental
34{
35namespace dynamic_fusion
36{
37using namespace arm_compute::opencl;
38
39void ClKernelRuntime::configure(const ClCompileContext &compile_ctx, const GpuKernelSourceCode &code)
40{
41 // Create kernel from kernel source string
42 opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
43 _kernel = static_cast<cl::Kernel>(compile_ctx.create_kernel(code.name(),
Ramy Elgammal002e6532023-01-11 18:48:04 +000044 code.name(), // program name has to be provided to differentiate between different unfusable components' kernels.
SiCong Lif44bbc52022-08-29 18:25:51 +010045 code.code(),
46 klib.kernel_path() /* Kernel path: Used in cases of embedded kernels */,
47 code.build_options().options(),
48 false /* Is source binary */));
49
50 // Configure execution window
51 IClKernel::configure_internal(code.window());
52
53 // Set config id for lws tuning
54 _config_id = code.config_id();
55
56 // Set kernel arguments
57 _arguments = code.arguments();
58}
59
60inline void ClKernelRuntime::add_tensor_argument(unsigned int &idx, const GpuKernelArgumentInfo &arg, const ICLTensor *tensor, const Window &arg_slice, std::vector<cl::Image2D> &cl_images)
61{
SiCong Li19844f62023-05-16 16:46:34 +010062 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
63
SiCong Lif44bbc52022-08-29 18:25:51 +010064 switch(arg.type)
65 {
66 case GpuKernelArgumentInfo::Type::Scalar:
67 {
68 ARM_COMPUTE_ERROR("Unsupported yet");
69 break;
70 }
71
72 case GpuKernelArgumentInfo::Type::Vector:
73 {
74 add_1D_tensor_argument(idx, tensor, arg_slice);
75 break;
76 }
77
78 case GpuKernelArgumentInfo::Type::Image:
79 {
80 add_2D_tensor_argument(idx, tensor, arg_slice);
81 break;
82 }
83 case GpuKernelArgumentInfo::Type::Image_Reinterpret_As_3D:
84 {
85 add_2D_tensor_argument(idx, tensor, arg_slice);
86 const unsigned int total_cross_plane_pad = tensor->info()->padding().top + tensor->info()->padding().bottom;
87 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(total_cross_plane_pad));
88 break;
89 }
90 case GpuKernelArgumentInfo::Type::Image_Export_To_ClImage2D:
91 {
92 const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * tensor->info()->dimension(2) * tensor->info()->dimension(3));
93 const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1];
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +000094 cl::Image2D tensor_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d, tensor->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +010095 cl_images.push_back(tensor_image2d);
96 _kernel.setArg(idx++, tensor_image2d);
97 break;
98 }
99
100 case GpuKernelArgumentInfo::Type::Image_3D:
101 {
102 add_2D_tensor_argument(idx, tensor, arg_slice);
103 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(tensor->info()->strides_in_bytes()[2]));
104 break;
105 }
106 case GpuKernelArgumentInfo::Type::Image_3D_Export_To_ClImage2D:
107 {
108 const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * tensor->info()->dimension(2) * tensor->info()->dimension(3));
109 const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1];
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000110 cl::Image2D tensor_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d, tensor->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +0100111 cl_images.push_back(tensor_image2d);
112 _kernel.setArg(idx++, tensor_image2d);
113 _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(tensor->info()->strides_in_bytes()[2]));
114 break;
115 }
116
117 case GpuKernelArgumentInfo::Type::Tensor_3D:
118 {
119 add_3D_tensor_argument(idx, tensor, arg_slice);
120 break;
121 }
122
123 case GpuKernelArgumentInfo::Type::Tensor_4D:
124 {
125 add_4D_tensor_argument(idx, tensor, arg_slice);
126 break;
127 }
128 case GpuKernelArgumentInfo::Type::Tensor_4D_t_Buffer:
129 {
130 add_4d_tensor_nhwc_argument(idx, tensor);
131 break;
132 }
133 case GpuKernelArgumentInfo::Type::Tensor_4D_t_Image:
134 {
135 const size_t image_w = tensor->info()->dimension(0) / 4;
136 const size_t image_h = tensor->info()->tensor_shape().total_size_upper(1);
137 const size_t image_stride_y = tensor->info()->strides_in_bytes()[1];
138
139 cl::Image2D tensor_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(),
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000140 TensorShape(image_w, image_h), tensor->info()->data_type(), image_stride_y, CLImage2DType::ReadOnly);
SiCong Lif44bbc52022-08-29 18:25:51 +0100141 cl_images.push_back(tensor_image2d);
142
143 _kernel.setArg(idx++, tensor_image2d);
144 add_4d_tensor_nhwc_argument(idx, tensor);
145 break;
146 }
SiCong Li19844f62023-05-16 16:46:34 +0100147 case GpuKernelArgumentInfo::Type::Tensor_Special_0:
148 {
149 const ITensorInfo *info = tensor->info();
150 const Strides &strides = info->strides_in_bytes();
151
152 _kernel.setArg(idx++, tensor->cl_buffer());
153 const size_t dim1xdim2 = info->tensor_shape()[1] * info->tensor_shape()[2];
154 _kernel.setArg<cl_int>(idx++, static_cast<int32_t>(dim1xdim2));
155 const size_t stride1 = strides[1];
156 _kernel.setArg<cl_int>(idx++, static_cast<int32_t>(stride1));
157 break;
158 }
SiCong Lif44bbc52022-08-29 18:25:51 +0100159 default:
160 {
161 ARM_COMPUTE_ERROR("Unsupported");
162 }
163 }
164}
165
166void ClKernelRuntime::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
167{
168 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
169 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
170
171 Window slice = window.first_slice_window_3D();
172 // Don't slice matrix along the z dimension if matrix has just 2 dimensions and matrix A more than 2
173 // This scenario can happen when the matrix multiplication is used to perform a convolution operation
174 Window slice_fixed_z = slice;
175 slice_fixed_z.set(Window::DimX, Window::Dimension(0, 1, 1));
176 slice_fixed_z.set(Window::DimY, Window::Dimension(0, 1, 1));
177
178 /// NOTE: Parameters extracted from old kernels. So far they seem to be constant
179 /// but we may need to make them into another configuration passed from GpuWorkloadSourceCode if needed in the future
180 constexpr bool slide_along_dimz = true;
181 constexpr bool skip_sliding_window = false;
182 constexpr bool use_dummy_work_items = false;
183
184 unsigned int idx = 0;
185 do
186 {
187 // Set kernel arguments
188 Window arg_slice = slice;
189 // CLImages created from tensor arguments. Need to be retained until enqueue
190 std::vector<cl::Image2D> cl_images;
191 for(auto id_arg : _arguments)
192 {
193 const auto arg = id_arg.second;
194 auto tensor = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(id_arg.first));
195 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
196 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor->info());
197 if(!slide_along_dimz)
198 {
199 // The stride_z for matrix must be zero if we do not slice
200 ARM_COMPUTE_ERROR_ON(tensor->info()->strides_in_bytes()[3] != 0);
201 arg_slice = slice_fixed_z;
202 }
203 add_tensor_argument(idx, *arg.kernel_argument_info(), tensor, arg_slice, cl_images);
204 }
205
206 // Dispatch kernel
207 enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items);
208 }
209 while(skip_sliding_window && window.slide_window_slice_3D(slice));
210}
211
212} // namespace dynamic_fusion
213} // namespace experimental
214} // namespace arm_compute