SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
Gunes Bayir | 0ee13af | 2024-02-07 15:34:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2024 Arm Limited. |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 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 "ClKernelRuntime.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 25 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 27 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 28 | #include "src/core/CL/CLUtils.h" |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 29 | #include "src/dynamic_fusion/runtime/gpu/cl/ckw_driver/GpuCkwKernelArgumentsHelpers.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 30 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelSourceCode.h" |
| 31 | #include "src/gpu/cl/ClKernelLibrary.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 32 | #include "support/Cast.h" |
Gunes Bayir | 0ee13af | 2024-02-07 15:34:45 +0000 | [diff] [blame] | 33 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace experimental |
| 37 | { |
| 38 | namespace dynamic_fusion |
| 39 | { |
| 40 | using namespace arm_compute::opencl; |
| 41 | |
| 42 | void 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | _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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 52 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | inline void ClKernelRuntime::add_kernel_argument(unsigned int &idx, |
| 64 | const GpuKernelArgumentBinding &arg, |
| 65 | const ICLTensor *tensor, |
| 66 | std::vector<cl::Image2D> &cl_images) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 67 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | switch (arg.type()) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 69 | { |
| 70 | case GpuKernelArgumentBinding::Type::TensorStorage: |
| 71 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | switch (arg.tensor_storage_type()) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 73 | { |
| 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 114 | void 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 120 | |
| 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 123 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 130 | // CLImages created from tensor arguments. Need to be retained until enqueue |
| 131 | std::vector<cl::Image2D> cl_images; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 132 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | for (const auto &arg : _arguments) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 134 | { |
| 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 Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 140 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 141 | // Dispatch kernel |
| 142 | enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | } while (skip_sliding_window && window.slide_window_slice_3D(slice)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace dynamic_fusion |
| 147 | } // namespace experimental |
| 148 | } // namespace arm_compute |