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