Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [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 | */ |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClMatMulNativeKernel.h" |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensorPack.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/ActivationFunctionUtils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [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" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 34 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 35 | #include "src/common/utils/Log.h" |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 36 | #include "src/core/CL/CLUtils.h" |
| 37 | #include "src/core/helpers/AutoConfiguration.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 38 | #include "src/core/helpers/WindowHelpers.h" |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 39 | #include "src/gpu/cl/kernels/gemm/ClGemmHelpers.h" |
Gunes Bayir | e87fa66 | 2023-09-07 12:20:33 +0100 | [diff] [blame] | 40 | #include "src/gpu/cl/kernels/helpers/MatMulKernelHelpers.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 41 | #include "support/Cast.h" |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 42 | #include "support/StringSupport.h" |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 43 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | namespace opencl |
| 47 | { |
| 48 | namespace kernels |
| 49 | { |
| 50 | namespace |
| 51 | { |
| 52 | Status validate_matmul_kernel_info(const MatMulKernelInfo &matmul_kernel_info) |
| 53 | { |
| 54 | const bool adj_lhs = matmul_kernel_info.adj_lhs; |
| 55 | const bool adj_rhs = matmul_kernel_info.adj_rhs; |
| 56 | const int m0 = matmul_kernel_info.m0; |
| 57 | const int n0 = matmul_kernel_info.n0; |
| 58 | const int k0 = matmul_kernel_info.k0; |
| 59 | |
| 60 | // Validate M0 |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(m0 < 1, "Only positive integers are supported for M0"); |
| 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 63 | if (adj_lhs) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 64 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((m0 & (m0 - 1)) && (m0 != 3)) || (m0 > 16), |
| 66 | "Only 1,2,3,4,8,16 are supported for M0 for Lhs transposed"); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Validate N0 |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 70 | 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^] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((n0 & (n0 - 1)) && (n0 != 3)) || (n0 > 16), |
| 72 | "Only 1,2,3,4,8,16 are supported for N0"); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 73 | |
| 74 | // Validate K0 |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(k0 < 1, "Only positive integers are supported for K0"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 76 | if (!adj_lhs || adj_rhs) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 77 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((k0 & (k0 - 1)) && (k0 != 3)) || (k0 > 16), |
| 79 | "Only 1,2,3,4,8,16 are supported for K0"); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 80 | } |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 81 | |
| 82 | return Status{}; |
| 83 | } |
| 84 | |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 85 | Status validate_export_to_cl_image(const ITensorInfo *rhs, const MatMulKernelInfo &matmul_kernel_info) |
| 86 | { |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON(matmul_kernel_info.export_rhs_to_cl_image && rhs->lock_paddings()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 88 | if (matmul_kernel_info.export_rhs_to_cl_image) |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 89 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 90 | if (matmul_kernel_info.adj_rhs) |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 91 | { |
| 92 | const int k0 = matmul_kernel_info.k0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 93 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(k0 != 4 && k0 != 8 && k0 != 16, |
| 94 | "K0 can only be: 4, 8, and 16 for Rhs transposed"); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
| 98 | const int n0 = matmul_kernel_info.n0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 99 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(n0 != 4 && n0 != 8 && n0 != 16, |
| 100 | "N0 can only be: 4, 8, and 16 for Rhs non-transposed"); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 101 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 102 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!export_to_cl_image(rhs), |
| 103 | "Export to CLImage is not supported for this device/configuration"); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 106 | return Status{}; |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 107 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 108 | } // namespace |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 109 | ClMatMulNativeKernel::ClMatMulNativeKernel() |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 110 | { |
| 111 | _type = CLKernelType::GEMM; |
| 112 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 113 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 114 | Status ClMatMulNativeKernel::validate(const ITensorInfo *lhs, |
| 115 | const ITensorInfo *rhs, |
| 116 | const ITensorInfo *bias, |
| 117 | const ITensorInfo *dst, |
| 118 | const MatMulKernelInfo &matmul_kernel_info, |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 119 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 120 | { |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_UNUSED(act_info); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 123 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lhs, 1, DataType::F32, DataType::F16); |
| 124 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, rhs); |
| 125 | ARM_COMPUTE_RETURN_ON_ERROR(validate_matmul_kernel_info(matmul_kernel_info)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 126 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 127 | validate_matmul_input_shapes(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info)); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 128 | ARM_COMPUTE_RETURN_ON_ERROR(validate_export_to_cl_image(rhs, matmul_kernel_info)); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 129 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 130 | const TensorShape expected_output_shape = |
| 131 | misc::shape_calculator::compute_matmul_shape(lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 132 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 133 | if (dst->total_size() != 0) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 134 | { |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 135 | const TensorInfo tensor_info_dst = dst->clone()->set_tensor_shape(expected_output_shape); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 136 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst); |
| 137 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(lhs, dst); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 138 | } |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 139 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 140 | if (bias != nullptr) |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 141 | { |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(bias, lhs); |
| 143 | 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^] | 144 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(bias->dimension(0) != expected_output_shape[0], |
| 145 | "First dimension of bias and output tensors must match."); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 148 | return Status{}; |
| 149 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 150 | void ClMatMulNativeKernel::configure(const ClCompileContext &compile_context, |
| 151 | ITensorInfo *lhs, |
| 152 | ITensorInfo *rhs, |
| 153 | ITensorInfo *bias, |
| 154 | ITensorInfo *dst, |
| 155 | const MatMulKernelInfo &matmul_kernel_info, |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 156 | const ActivationLayerInfo &act_info) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 157 | { |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 158 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst, &compile_context, &matmul_kernel_info); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 159 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst, matmul_kernel_info); |
| 160 | ARM_COMPUTE_ERROR_THROW_ON(validate(lhs, rhs, bias, dst, matmul_kernel_info)); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 161 | |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 162 | // dst tensor auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 163 | auto_init_if_empty(*dst, lhs->clone()->set_tensor_shape(misc::shape_calculator::compute_matmul_shape( |
| 164 | lhs->tensor_shape(), rhs->tensor_shape(), matmul_kernel_info))); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 165 | |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 166 | const int m = dst->dimension(1); |
| 167 | const int n = dst->dimension(0); |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 168 | const int k = matmul_kernel_info.adj_lhs ? lhs->tensor_shape().y() : lhs->tensor_shape().x(); |
| 169 | const bool adj_lhs = matmul_kernel_info.adj_lhs; |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 170 | |
Gunes Bayir | 8918b23 | 2023-03-17 13:52:21 +0000 | [diff] [blame] | 171 | int m0 = adj_lhs ? adjust_vec_size(matmul_kernel_info.m0, m) : std::min(matmul_kernel_info.m0, m); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 172 | int n0 = adjust_vec_size(matmul_kernel_info.n0, n); |
| 173 | |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 174 | _export_rhs_to_cl_image = matmul_kernel_info.export_rhs_to_cl_image && !rhs->lock_paddings(); |
| 175 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 176 | // Configure kernel window |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 177 | Window win = calculate_max_window(*dst, Steps(n0, m0)); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 178 | win = win.collapse(win, Window::DimZ); |
| 179 | IClKernel::configure_internal(win); |
| 180 | |
| 181 | // 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. |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 182 | const unsigned int partial_store_m0 = m % m0; |
| 183 | const unsigned int partial_store_n0 = n % n0; |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 184 | |
| 185 | CLBuildOptions build_opts; |
| 186 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(lhs->data_type())); |
| 187 | build_opts.add_option("-DM0=" + support::cpp11::to_string(m0)); |
| 188 | build_opts.add_option("-DN0=" + support::cpp11::to_string(n0)); |
| 189 | build_opts.add_option("-DK0=" + support::cpp11::to_string(matmul_kernel_info.k0)); |
| 190 | build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0)); |
| 191 | build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0)); |
| 192 | build_opts.add_option("-DK=" + support::cpp11::to_string(k)); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 193 | build_opts.add_option_if(bias != nullptr, "-DBIAS"); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 194 | build_opts.add_option_if_else(_export_rhs_to_cl_image, "-DRHS_TENSOR_TYPE=IMAGE", "-DRHS_TENSOR_TYPE=BUFFER"); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 195 | |
Mohammed Suhail Munshi | 94abde4 | 2023-05-25 16:48:43 +0100 | [diff] [blame] | 196 | // Define values for activation function |
| 197 | build_opts.add_option(("-DA_VAL=" + float_to_string_with_full_precision(act_info.a()))); |
| 198 | build_opts.add_option(("-DB_VAL=" + float_to_string_with_full_precision(act_info.b()))); |
| 199 | build_opts.add_option("-DACTIVATION_TYPE=" + lower_string(string_from_activation_func(act_info.activation()))); |
| 200 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 201 | std::string kernel_name("mat_mul_native"); |
| 202 | kernel_name += matmul_kernel_info.adj_lhs ? "_t" : "_nt"; |
| 203 | kernel_name += matmul_kernel_info.adj_rhs ? "_t" : "_nt"; |
| 204 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 205 | // A macro guard to compile ONLY the kernel of interest |
| 206 | build_opts.add_option("-D" + upper_string(kernel_name)); |
| 207 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 208 | if (_export_rhs_to_cl_image) |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 209 | { |
| 210 | gemm::update_padding_for_cl_image(rhs); |
| 211 | } |
| 212 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 213 | // Create kernel |
| 214 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 215 | |
| 216 | // Set config_id for enabling LWS tuning |
| 217 | _config_id = kernel_name; |
| 218 | _config_id += "_"; |
| 219 | _config_id += lower_string(string_from_data_type(lhs->data_type())); |
| 220 | _config_id += "_"; |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 221 | _config_id += support::cpp11::to_string(m); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 222 | _config_id += "_"; |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 223 | _config_id += support::cpp11::to_string(n); |
| 224 | _config_id += "_"; |
| 225 | _config_id += support::cpp11::to_string(k); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 226 | _config_id += "_"; |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 227 | _config_id += support::cpp11::to_string(dst->dimension(2)); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 228 | _config_id += "_"; |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 229 | _config_id += support::cpp11::to_string(_export_rhs_to_cl_image); |
| 230 | _config_id += "_"; |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 231 | _config_id += support::cpp11::to_string(m0); |
| 232 | _config_id += "_"; |
| 233 | _config_id += support::cpp11::to_string(n0); |
| 234 | _config_id += "_"; |
| 235 | _config_id += support::cpp11::to_string(matmul_kernel_info.k0); |
| 236 | } |
| 237 | |
Jakub Sujak | 1ed6a14 | 2023-04-13 21:14:42 +0100 | [diff] [blame] | 238 | void ClMatMulNativeKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 239 | { |
| 240 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 241 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 242 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 243 | const ICLTensor *lhs = |
| 244 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 245 | const ICLTensor *rhs = |
| 246 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 247 | const ICLTensor *bias = utils::cast::polymorphic_downcast<const ICLTensor *>( |
| 248 | tensors.get_const_tensor(TensorType::ACL_SRC_2)); // nullptr if bias is not present |
| 249 | ICLTensor *dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 250 | ARM_COMPUTE_ERROR_ON_NULLPTR(lhs, rhs, dst); |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 251 | ARM_COMPUTE_LOG_PARAMS(lhs, rhs, bias, dst); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 252 | |
| 253 | unsigned int idx = 0; |
| 254 | Window window_collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 255 | |
| 256 | add_3d_tensor_nhw_argument(idx, lhs); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 257 | |
| 258 | cl::Image2D rhs_cl_image; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 259 | if (_export_rhs_to_cl_image) |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 260 | { |
| 261 | const size_t image_w = rhs->info()->dimension(0) / 4; |
| 262 | const size_t image_h = rhs->info()->tensor_shape().total_size() / rhs->info()->dimension(0); |
| 263 | const TensorShape shape2d(image_w, image_h); |
| 264 | const size_t image_row_pitch = rhs->info()->strides_in_bytes()[1]; |
| 265 | |
| 266 | // Export cl_buffer to cl_image |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 267 | rhs_cl_image = create_image2d_from_buffer(CLKernelLibrary::get().context(), rhs->cl_buffer(), shape2d, |
| 268 | rhs->info()->data_type(), image_row_pitch, CLImage2DType::ReadOnly); |
Gunes Bayir | bbeef72 | 2023-03-20 10:19:10 +0000 | [diff] [blame] | 269 | _kernel.setArg(idx++, rhs_cl_image); |
| 270 | } |
| 271 | |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 272 | add_3d_tensor_nhw_argument(idx, rhs); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 273 | if (bias != nullptr) |
Mohammed Suhail Munshi | 8e2dede | 2023-06-27 14:25:58 +0100 | [diff] [blame] | 274 | { |
| 275 | add_3d_tensor_nhw_argument(idx, bias); |
| 276 | } |
Jakub Sujak | e9b3ee2 | 2023-04-17 12:08:48 +0100 | [diff] [blame] | 277 | add_3d_tensor_nhw_argument(idx, dst); |
Ramy Elgammal | 2b6ebfe | 2023-03-09 21:15:37 +0000 | [diff] [blame] | 278 | |
| 279 | enqueue(queue, *this, window_collapsed, lws_hint()); |
| 280 | } |
| 281 | |
| 282 | } // namespace kernels |
| 283 | } // namespace opencl |
| 284 | } // namespace arm_compute |