/* * Copyright (c) 2022 Arm Limited. * * SPDX-License-Identifier: MIT * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #if defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION) #include "src/gpu/cl/kernels/experimental/dynamic_fusion/ClCompositeKernel.h" #include "arm_compute/core/CL/ICLTensor.h" #include "src/core/CL/CLUtils.h" #include "src/gpu/cl/ClKernelLibrary.h" namespace arm_compute { namespace experimental { namespace dynamic_fusion { using namespace arm_compute::opencl; void ClCompositeKernel::configure(const ClCompileContext &compile_ctx, const ClKernelCode &cl_code) { // Create kernel from kernel source string opencl::ClKernelLibrary &klib = opencl::ClKernelLibrary::get(); _kernel = static_cast(compile_ctx.create_kernel(cl_code.name, "" /* Program name: Used to as part of a unique string for built kernel cache. Not needed */, cl_code.code, klib.kernel_path() /* Kernel path: Used in cases of embedded kernels */, cl_code.build_options.options(), false /* Is source binary */)); // Configure execution window IClKernel::configure_internal(cl_code.window); // Set config id for lws tuning _config_id = cl_code.config_id; // Set kernel arguments _arguments = cl_code.arguments; } inline void ClCompositeKernel::add_tensor_argument(unsigned int &idx, const ClKernelArgRuntimeDescriptor &arg, ICLTensor *tensor, const Window &arg_slice) { switch(arg.tensor_arg_type) { case TensorArgType::Scalar: { ARM_COMPUTE_ERROR("Unsupported yet"); break; } case TensorArgType::Vector: { add_1D_tensor_argument(idx, tensor, arg_slice); break; } case TensorArgType::Image: { add_2D_tensor_argument(idx, tensor, arg_slice); break; } case TensorArgType::Image_Reinterpret_As_3D: { add_2D_tensor_argument(idx, tensor, arg_slice); const unsigned int total_cross_plane_pad = tensor->info()->padding().top + tensor->info()->padding().bottom; _kernel.setArg(idx++, static_cast(total_cross_plane_pad)); break; } case TensorArgType::Image_Export_To_ClImage2D: { const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * tensor->info()->dimension(2) * tensor->info()->dimension(3)); const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1]; cl::Image2D tensor_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d, tensor->info()->data_type(), image_row_pitch); _kernel.setArg(idx++, tensor_image2d); break; } case TensorArgType::Image_3D: { add_2D_tensor_argument(idx, tensor, arg_slice); _kernel.setArg(idx++, static_cast(tensor->info()->strides_in_bytes()[2])); break; } case TensorArgType::Image_3D_Export_To_ClImage2D: { const TensorShape shape2d(tensor->info()->dimension(0) / 4, tensor->info()->dimension(1) * tensor->info()->dimension(2) * tensor->info()->dimension(3)); const size_t image_row_pitch = tensor->info()->strides_in_bytes()[1]; cl::Image2D tensor_image2d = create_image2d_from_buffer(CLKernelLibrary::get().context(), tensor->cl_buffer(), shape2d, tensor->info()->data_type(), image_row_pitch); _kernel.setArg(idx++, tensor_image2d); _kernel.setArg(idx++, static_cast(tensor->info()->strides_in_bytes()[2])); break; } case TensorArgType::Tensor_3D: { add_3D_tensor_argument(idx, tensor, arg_slice); break; } case TensorArgType::Tensor_4D: { add_4D_tensor_argument(idx, tensor, arg_slice); break; } default: { ARM_COMPUTE_ERROR("Unsupported"); } } } void ClCompositeKernel::run_composite_op(TensorBinding &tensors, const Window &window, cl::CommandQueue &queue, const ClExecutionDescriptor &exec_desc) { ARM_COMPUTE_UNUSED(exec_desc); ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); Window slice = window.first_slice_window_3D(); // Don't slice matrix along the z dimension if matrix has just 2 dimensions and matrix A more than 2 // This scenario can happen when the matrix multiplication is used to perform a convolution operation Window slice_fixed_z = slice; slice_fixed_z.set(Window::DimX, Window::Dimension(0, 1, 1)); slice_fixed_z.set(Window::DimY, Window::Dimension(0, 1, 1)); unsigned int idx = 0; do { // Set kernel arguments Window arg_slice = slice; for(auto arg : _arguments) { auto tensor = tensors._binding.at(arg.arg_id); ARM_COMPUTE_ERROR_ON_NULLPTR(tensor); if(!arg.slide_along_dimz) { // The stride_z for matrix must be zero if we do not slice ARM_COMPUTE_ERROR_ON(tensor->info()->strides_in_bytes()[3] != 0); arg_slice = slice_fixed_z; } add_tensor_argument(idx, arg, tensor, arg_slice); } // Dispatch kernel bool use_dummy_work_items = false; enqueue(queue, *this, slice, lws_hint(), use_dummy_work_items); } while(window.slide_window_slice_3D(slice)); } Status bind_arguments(ITensorPack &, const ClKernelCode &, const TensorBinding &) { return Status{}; } } // namespace dynamic_fusion } // namespace experimental } // namespace arm_compute #endif // defined(ENABLE_EXPERIMENTAL_DYNAMIC_FUSION)