blob: eab5cddd07df17492d6f542618fcdaeed6c08fd4 [file] [log] [blame]
SiCong Lif44bbc52022-08-29 18:25:51 +01001/*
Gunes Bayir0ee13af2024-02-07 15:34:45 +00002 * Copyright (c) 2022-2024 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#include "src/dynamic_fusion/runtime/gpu/cl/ckw_driver/GpuCkwKernelArgumentsHelpers.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010030#include "src/dynamic_fusion/sketch/gpu/GpuKernelSourceCode.h"
31#include "src/gpu/cl/ClKernelLibrary.h"
SiCong Lif44bbc52022-08-29 18:25:51 +010032#include "support/Cast.h"
Gunes Bayir0ee13af2024-02-07 15:34:45 +000033
SiCong Lif44bbc52022-08-29 18:25:51 +010034namespace arm_compute
35{
36namespace experimental
37{
38namespace dynamic_fusion
39{
40using namespace arm_compute::opencl;
41
42void ClKernelRuntime::configure(const ClCompileContext &compile_ctx, const GpuKernelSourceCode &code)
43{
44 // Create kernel from kernel source string
45 opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 _kernel = static_cast<cl::Kernel>(compile_ctx.create_kernel(
47 code.name(),
48 code.name(), // program name has to be provided to differentiate between different unfusable components' kernels.
49 // Each program contains exactly one kernel
50 code.code(), klib.kernel_path() /* Kernel path: Used in cases of embedded kernels */,
51 code.build_options().options(), false /* Is source binary */));
SiCong Lif44bbc52022-08-29 18:25:51 +010052
53 // Configure execution window
54 IClKernel::configure_internal(code.window());
55
56 // Set config id for lws tuning
57 _config_id = code.config_id();
58
59 // Set kernel arguments
60 _arguments = code.arguments();
61}
62
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063inline void ClKernelRuntime::add_kernel_argument(unsigned int &idx,
64 const GpuKernelArgumentBinding &arg,
65 const ICLTensor *tensor,
66 std::vector<cl::Image2D> &cl_images)
SiCong Li23882a92023-06-28 09:49:45 +010067{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 switch (arg.type())
SiCong Li23882a92023-06-28 09:49:45 +010069 {
70 case GpuKernelArgumentBinding::Type::TensorStorage:
71 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 switch (arg.tensor_storage_type())
SiCong Li23882a92023-06-28 09:49:45 +010073 {
74 case TensorStorageType::ClBufferUint8Ptr:
75 {
76 cl_add_buffer_argument(_kernel, idx, tensor->cl_buffer());
77 break;
78 }
79 case TensorStorageType::ClImage2dReadOnly:
80 {
81 cl::Image2D tensor_image2d = create_image2d_from_tensor(tensor, CLImage2DType::ReadOnly);
82 cl_images.push_back(tensor_image2d);
83 cl_add_texture_argument(_kernel, idx, tensor_image2d);
84 break;
85 }
86 case TensorStorageType::ClImage2dWriteOnly:
87 {
88 cl::Image2D tensor_image2d = create_image2d_from_tensor(tensor, CLImage2DType::WriteOnly);
89 cl_images.push_back(tensor_image2d);
90 cl_add_texture_argument(_kernel, idx, tensor_image2d);
91 break;
92 }
93 default:
94 {
95 ARM_COMPUTE_ERROR("Do not accept other TensorStorageType");
96 break;
97 }
98 }
99 break;
100 }
101 case GpuKernelArgumentBinding::Type::TensorComponent:
102 {
103 cl_add_tensor_component_argument(_kernel, idx, tensor, arg.tensor_component_type());
104 break;
105 }
106 default:
107 {
108 ARM_COMPUTE_ERROR("Do not accept other types of kernel arguments");
109 break;
110 }
111 }
112}
113
SiCong Lif44bbc52022-08-29 18:25:51 +0100114void ClKernelRuntime::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
115{
116 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
117 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
118
119 Window slice = window.first_slice_window_3D();
SiCong Lif44bbc52022-08-29 18:25:51 +0100120
121 /// NOTE: Parameters extracted from old kernels. So far they seem to be constant
122 /// 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 +0100123 constexpr bool skip_sliding_window = false;
124 constexpr bool use_dummy_work_items = false;
125
126 unsigned int idx = 0;
127 do
128 {
129 // Set kernel arguments
SiCong Lif44bbc52022-08-29 18:25:51 +0100130 // CLImages created from tensor arguments. Need to be retained until enqueue
131 std::vector<cl::Image2D> cl_images;
SiCong Lif44bbc52022-08-29 18:25:51 +0100132
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100133 for (const auto &arg : _arguments)
SiCong Li23882a92023-06-28 09:49:45 +0100134 {
135 auto tensor = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(arg.id()));
136 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
137 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor->info());
138 add_kernel_argument(idx, arg, tensor, cl_images);
139 }
SiCong Li23882a92023-06-28 09:49:45 +0100140
SiCong Lif44bbc52022-08-29 18:25:51 +0100141 // Dispatch kernel
142 enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100143 } while (skip_sliding_window && window.slide_window_slice_3D(slice));
SiCong Lif44bbc52022-08-29 18:25:51 +0100144}
145
146} // namespace dynamic_fusion
147} // namespace experimental
148} // namespace arm_compute