SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 Arm Limited. |
| 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 "src/gpu/cl/kernels/ClMatMulNativeMMULKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/ITensorPack.h" |
| 29 | #include "arm_compute/core/KernelDescriptors.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame^] | 31 | #include "arm_compute/core/utils/StringUtils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 34 | |
| 35 | #include "src/common/utils/Log.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame^] | 38 | #include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h" |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 39 | |
| 40 | #include "support/Cast.h" |
| 41 | #include "support/StringSupport.h" |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | namespace opencl |
| 46 | { |
| 47 | namespace kernels |
| 48 | { |
| 49 | namespace |
| 50 | { |
| 51 | // Block size dimensions for the MMUL extension |
| 52 | constexpr int mmul_m0 = 4; |
| 53 | constexpr int mmul_n0 = 4; |
| 54 | constexpr int mmul_k0 = 4; |
| 55 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 56 | Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info) |
| 57 | { |
| 58 | const bool adj_lhs = matmul_kernel_info.adj_lhs; |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 59 | const int m0 = matmul_kernel_info.m0; |
| 60 | const int n0 = matmul_kernel_info.n0; |
| 61 | const int k0 = matmul_kernel_info.k0; |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 62 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 63 | // Validate M0 |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0"); |
| 65 | |
Gunes Bayir | 00474e9 | 2023-06-19 21:33:51 +0100 | [diff] [blame] | 66 | if(adj_lhs) |
| 67 | { |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((m0 != 1) && (m0 != 2) && (m0 != 3) && (m0 != 4) && (m0 != 8) && (m0 != 16), "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed"); |
| 69 | } |
| 70 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 71 | // Validate N0 |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 < 1, "Only positive integers are supported for N0"); |
Gunes Bayir | 00474e9 | 2023-06-19 21:33:51 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((n0 != 1) && (n0 != 2) && (n0 != 3) && (n0 != 4) && (n0 != 8) && (n0 != 16), "Only 1,2,3,4,8,16 are supported for N0"); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 74 | |
| 75 | // Validate K0 |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((k0 != 1), "Only 1 is supported for k0"); |
| 77 | |
| 78 | return Status{}; |
| 79 | } |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 80 | } |
| 81 | ClMatMulNativeMMULKernel::ClMatMulNativeMMULKernel() |
| 82 | { |
| 83 | _type = CLKernelType::GEMM; |
| 84 | } |
| 85 | |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 86 | Status ClMatMulNativeMMULKernel::validate(const ITensorInfo *lhs, const ITensorInfo *rhs, const ITensorInfo *bias, const ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info) |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 87 | { |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst); |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!arm_matrix_multiply_supported(CLKernelLibrary::get().get_device()), "The extension cl_arm_matrix_multiply is not supported on the target platform"); |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs); |
| 92 | ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info)); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 93 | |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame^] | 94 | const TensorShape &lhs_shape = lhs->tensor_shape(); |
| 95 | ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_input_shapes(lhs_shape, rhs->tensor_shape(), matmul_kernel_info)); |
| 96 | |
| 97 | const size_t lhs_k = matmul_kernel_info.adj_lhs ? lhs_shape.y() : lhs_shape.x(); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR((lhs_k % mmul_k0) != 0, "K dimension must be a multiple of %d", mmul_k0); |
| 99 | |
| 100 | const TensorShape expected_output_shape = misc::shape_calculator::compute_matmul_shape(lhs_shape, rhs->tensor_shape(), matmul_kernel_info); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 101 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 102 | if(dst->total_size() != 0) |
| 103 | { |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 104 | const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(expected_output_shape); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
| 107 | } |
| 108 | |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 109 | if(bias != nullptr) |
| 110 | { |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((bias->num_dimensions() > 1), "Multi dimensional bias is unsupported."); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(bias->dimension(0) != expected_output_shape[0], "First dimension of bias and output tensors must match."); |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, bias); |
| 114 | } |
| 115 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 116 | return Status{}; |
| 117 | } |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 118 | void ClMatMulNativeMMULKernel::configure(const ClCompileContext &compile_context, ITensorInfo *lhs, ITensorInfo *rhs, ITensorInfo *bias, ITensorInfo *dst, const MatMulKernelInfo &matmul_kernel_info) |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 119 | { |
| 120 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info); |
| 122 | ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info)); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 123 | |
| 124 | // dst tensor auto initialization if not yet initialized |
| 125 | auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info))); |
| 126 | |
| 127 | const int m = dst->dimension(1); |
| 128 | const int n = dst->dimension(0); |
Ramy Elgammal | c952596 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 129 | const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x(); |
| 130 | |
| 131 | _m = m; |
| 132 | _n = n; |
| 133 | _k = k; |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 134 | |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame^] | 135 | const int m0 = std::min(matmul_kernel_info.m0, m); |
| 136 | const int n0 = adjust_vec_size(matmul_kernel_info.n0, n); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 137 | |
| 138 | // Configure kernel window |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame^] | 139 | const auto win_config = validate_and_configure_window_for_mmul_kernels(lhs, rhs, dst, matmul_kernel_info, mmul_m0, |
| 140 | mmul_n0); |
| 141 | |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 143 | IClKernel::configure_internal(win_config.second); |
| 144 | |
| 145 | // Calculate partial (store instead of load) M0 and partial N0 for the partial blocks at the end of a row/column if any. This is to avoid padding. |
| 146 | const unsigned int m0_leftover = m % m0; |
| 147 | const unsigned int n0_leftover = n % n0; |
| 148 | |
| 149 | CLBuildOptions build_opts; |
| 150 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type())); |
| 151 | build_opts.add_option_if(lhs->data_type() == DataType::F16, "-DHALF_PRECISION"); |
| 152 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 153 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 154 | build_opts.add_option("-DM0_LEFTOVER=" + support::cpp11::to_string(m0_leftover)); |
| 155 | build_opts.add_option("-DN0_LEFTOVER=" + support::cpp11::to_string(n0_leftover)); |
| 156 | build_opts.add_option("-DMMUL_M0=" + support::cpp11::to_string(mmul_m0)); |
| 157 | build_opts.add_option("-DMMUL_N0=" + support::cpp11::to_string(mmul_n0)); |
| 158 | build_opts.add_option("-DMMUL_K0=" + support::cpp11::to_string(mmul_k0)); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 159 | build_opts.add_option_if(bias != nullptr, "-DBIAS"); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 160 | |
Ramy Elgammal | c952596 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 161 | std::string kernel_name("mat_mul_native_mmul"); |
| 162 | kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt"; |
| 163 | kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt"; |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 164 | |
| 165 | // A macro guard to compile ONLY the kernel of interest |
| 166 | build_opts.add_option("-D" + upper_string(kernel_name)); |
| 167 | |
| 168 | // Create kernel |
| 169 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 170 | |
| 171 | // Set config_id for enabling LWS tuning |
| 172 | _config_id = kernel_name; |
| 173 | _config_id += "_"; |
| 174 | _config_id += lower_string(string_from_data_type(lhs->data_type())); |
| 175 | _config_id += "_"; |
| 176 | _config_id += support::cpp11::to_string(k); |
| 177 | _config_id += "_"; |
| 178 | _config_id += support::cpp11::to_string(dst->dimension(2)); |
| 179 | _config_id += "_"; |
| 180 | _config_id += support::cpp11::to_string(m0); |
| 181 | _config_id += "_"; |
| 182 | _config_id += support::cpp11::to_string(n0); |
| 183 | _config_id += "_"; |
| 184 | _config_id += support::cpp11::to_string(matmul_kernel_info.k0); |
| 185 | } |
| 186 | |
| 187 | void ClMatMulNativeMMULKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 190 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 191 | |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 192 | const ICLTensor *lhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 193 | const ICLTensor *rhs = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 194 | const ICLTensor *bias = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present |
| 195 | ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 196 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 197 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 198 | unsigned int idx = 0; |
| 199 | |
| 200 | add_3d_tensor_nhw_argument(idx, lhs); |
| 201 | add_3d_tensor_nhw_argument(idx, rhs); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 202 | if(bias != nullptr) |
| 203 | { |
| 204 | add_3d_tensor_nhw_argument(idx, bias); |
| 205 | } |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 206 | add_3d_tensor_nhw_argument(idx, dst); |
| 207 | |
| 208 | // Pass m and n at runtime as signed ints, to ensure results of any subtractions they could be operand in, would still be signed. |
| 209 | _kernel.setArg<cl_int>(idx++, _m); |
| 210 | _kernel.setArg<cl_int>(idx++, _n); |
Ramy Elgammal | c952596 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 211 | _kernel.setArg<cl_int>(idx++, _k); |
SiCong Li | a8d8058 | 2023-05-19 14:23:37 +0100 | [diff] [blame] | 212 | |
| 213 | // LWS_x should be multiple of 16 at least. (32, 2) has been chosen to have more work-items on a single core |
| 214 | // LWS also enforces the order of execution of the work items which improves cache utilization |
| 215 | enqueue(queue, *this, window, cl::NDRange(32, 2), false); |
| 216 | } |
| 217 | |
| 218 | } // namespace kernels |
| 219 | } // namespace opencl |
| 220 | } // namespace arm_compute |