SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 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 | #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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 32 | #include "src/dynamic_fusion/sketch/gpu/GpuKernelSourceCode.h" |
| 33 | #include "src/gpu/cl/ClKernelLibrary.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 34 | #include "support/Cast.h" |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace experimental |
| 38 | { |
| 39 | namespace dynamic_fusion |
| 40 | { |
| 41 | using namespace arm_compute::opencl; |
| 42 | |
| 43 | void 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 47 | _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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 53 | |
| 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 Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 64 | #ifndef ACL_INTERNAL_TEST_CKW_IN_DF |
| 65 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 66 | inline 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 71 | { |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR_ON_NULLPTR(tensor); |
| 73 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 74 | switch (arg.type) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 75 | { |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 102 | const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * |
| 103 | tensor->info()->dimension(2) * |
| 104 | tensor->info()->dimension(3)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 105 | const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 106 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 109 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 122 | const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * |
| 123 | tensor->info()->dimension(2) * |
| 124 | tensor->info()->dimension(3)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 125 | const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 126 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 129 | 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 157 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 160 | 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 Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 166 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 178 | default: |
| 179 | { |
| 180 | ARM_COMPUTE_ERROR("Unsupported"); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 185 | #else // ACL_INTERNAL_TEST_CKW_IN_DF |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 186 | inline void ClKernelRuntime::add_kernel_argument(unsigned int &idx, |
| 187 | const GpuKernelArgumentBinding &arg, |
| 188 | const ICLTensor *tensor, |
| 189 | std::vector<cl::Image2D> &cl_images) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 190 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 191 | switch (arg.type()) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 192 | { |
| 193 | case GpuKernelArgumentBinding::Type::TensorStorage: |
| 194 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 195 | switch (arg.tensor_storage_type()) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 196 | { |
| 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 238 | void 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 244 | |
| 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 247 | 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 254 | // CLImages created from tensor arguments. Need to be retained until enqueue |
| 255 | std::vector<cl::Image2D> cl_images; |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 256 | #ifndef ACL_INTERNAL_TEST_CKW_IN_DF |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 257 | for (auto id_arg : _arguments) |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 258 | { |
| 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 Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 263 | add_tensor_argument(idx, *arg.kernel_argument_info(), tensor, slice, cl_images); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 264 | } |
| 265 | |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 266 | #else // ACL_INTERNAL_TEST_CKW_IN_DF |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 267 | for (const auto &arg : _arguments) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 268 | { |
| 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 Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 276 | // Dispatch kernel |
| 277 | enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 278 | } while (skip_sliding_window && window.slide_window_slice_3D(slice)); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | } // namespace dynamic_fusion |
| 282 | } // namespace experimental |
| 283 | } // namespace arm_compute |