Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClGemmMatrixMultiplyNativeKernel.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/ActivationFunctionUtils.h" |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/utils/StringUtils.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 37 | #include "src/core/AccessWindowStatic.h" |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 38 | #include "src/core/CL/CLUtils.h" |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 39 | #include "src/core/helpers/AutoConfiguration.h" |
| 40 | #include "src/core/helpers/WindowHelpers.h" |
| 41 | #include "src/core/utils/helpers/float_ops.h" |
| 42 | #include "support/Cast.h" |
| 43 | #include "support/StringSupport.h" |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | namespace opencl |
| 48 | { |
| 49 | namespace kernels |
| 50 | { |
| 51 | namespace |
| 52 | { |
| 53 | using ElementsProcessed = Steps; |
| 54 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | Status validate_arguments(const ITensorInfo *src0, |
| 56 | const ITensorInfo *src1, |
| 57 | const ITensorInfo *src2, |
| 58 | const ITensorInfo *dst, |
| 59 | float alpha, |
| 60 | float beta, |
| 61 | const GEMMLHSMatrixInfo &lhs_info, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 62 | const GEMMRHSMatrixInfo &rhs_info, |
| 63 | const GEMMKernelInfo &gemm_info) |
| 64 | { |
| 65 | ARM_COMPUTE_UNUSED(alpha); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst); |
Gian Marco Iodice | c9cecc0 | 2021-10-15 10:23:24 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F32, DataType::F16); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->num_dimensions() > 4, |
| 70 | "The number of dimensions for the LHS matrix must be <= 4"); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 3, |
| 72 | "The number of dimensions for the RHS matrix must be <= 3"); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.k0 & (rhs_info.k0 - 1)) && rhs_info.k0 != 3), |
| 74 | "Only 2,3,4,8,16 are supported for k0"); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_RETURN_ERROR_ON(rhs_info.k0 > 16); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON(lhs_info.m0 < 1 || lhs_info.m0 > 8); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((rhs_info.n0 & (rhs_info.n0 - 1)) && rhs_info.n0 != 3), |
| 78 | "Only 2,3,4,8,16 are supported for n0"); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 80 | (gemm_info.reinterpret_input_as_3d || gemm_info.depth_output_gemm3d != 0) && (src2 != nullptr) && |
| 81 | (!gemm_info.broadcast_bias), |
| 82 | "Bias addition only supported with broadcast mode in case the input or dst has to be reinterpreted as 3D"); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 83 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(gemm_info.fp_mixed_precision, "Mixed precision not supported"); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(rhs_info.export_to_cl_image, "Export to CLImage not supported for GEMM native"); |
| 85 | |
| 86 | const unsigned int m = gemm_info.m; |
| 87 | const unsigned int n = gemm_info.n; |
| 88 | const unsigned int k = gemm_info.k; |
| 89 | |
| 90 | ARM_COMPUTE_UNUSED(m); |
| 91 | ARM_COMPUTE_UNUSED(n); |
| 92 | ARM_COMPUTE_UNUSED(k); |
| 93 | |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(0) != k); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(0) != n); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(1) != k); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 97 | if (gemm_info.reinterpret_input_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) * src0->dimension(2) != m); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(src0->dimension(1) != m); |
| 104 | } |
| 105 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 106 | if (src2 != nullptr && !(helpers::float_ops::is_zero(beta))) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 107 | { |
| 108 | const unsigned int src2_dim0 = src2->dimension(0); |
| 109 | const unsigned int src2_dim1 = src2->dimension(1); |
| 110 | |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src2, src1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | if (gemm_info.broadcast_bias) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 113 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim1 != 1 || src2_dim0 != n), |
| 115 | "Incorrect dimension of bias matrix which is to be broadcasted"); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 116 | } |
| 117 | else |
| 118 | { |
| 119 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((src2_dim0 != n || src2_dim1 != m), "Incorrect dimension of bias matrix"); |
| 120 | } |
| 121 | } |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | if (dst->total_size() != 0) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 124 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | const TensorInfo tensor_info_dst = |
| 126 | dst->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &tensor_info_dst); |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst); |
| 129 | } |
| 130 | |
| 131 | return Status{}; |
| 132 | } |
| 133 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 134 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *src0, |
| 135 | ITensorInfo *src1, |
| 136 | ITensorInfo *src2, |
| 137 | ITensorInfo *dst, |
| 138 | const GEMMLHSMatrixInfo &lhs_info, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 139 | const GEMMRHSMatrixInfo &rhs_info, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 140 | const GEMMKernelInfo &gemm_info, |
| 141 | ElementsProcessed &num_elements_processed) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 142 | { |
| 143 | unsigned int &num_elems_processed_per_iteration_x = num_elements_processed[0]; |
| 144 | unsigned int &num_elems_processed_per_iteration_y = num_elements_processed[1]; |
| 145 | bool reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d; |
| 146 | bool reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0; |
| 147 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 148 | Window win{}; |
| 149 | Window win_out{}; |
| 150 | bool window_changed = false; |
| 151 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 152 | // In case both input and dst have to be reinterpreted as 3D tensors, |
| 153 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | if (reinterpret_input_as_3d == reinterpret_output_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 155 | { |
| 156 | reinterpret_output_as_3d = false; |
| 157 | } |
| 158 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 159 | // dst tensor auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | auto_init_if_empty( |
| 161 | *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info))); |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 162 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 163 | TensorInfo tmp_info(*dst); |
| 164 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 165 | if (reinterpret_output_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 166 | { |
| 167 | // Since the dst tensor has to be reinterpreted as 3D and the execute window is based on a 2D GEMM, |
| 168 | // the window needs to be constructed on the 2D collapsed version of the tensor |
| 169 | TensorShape tmp_shape(dst->tensor_shape()); |
| 170 | tmp_shape.collapse(2U, 1U); |
| 171 | tmp_info.set_tensor_shape(tmp_shape); |
| 172 | } |
| 173 | |
| 174 | // Configure kernel window |
| 175 | num_elems_processed_per_iteration_x = rhs_info.n0; |
| 176 | num_elems_processed_per_iteration_y = lhs_info.m0; |
| 177 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | win = |
| 179 | calculate_max_window(tmp_info, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 180 | win_out = |
| 181 | calculate_max_window(*dst, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 182 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 183 | AccessWindowStatic src0_access(src0, 0, 0, src0->dimension(0), src0->dimension(1)); |
| 184 | AccessWindowStatic src1_access( |
| 185 | src1, 0, 0, ceil_to_multiple(src1->dimension(0), num_elems_processed_per_iteration_x), src1->dimension(1)); |
| 186 | AccessWindowStatic dst_access(dst, 0, 0, dst->dimension(0), dst->dimension(1)); |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 187 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | if (src2 != nullptr) |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 189 | { |
| 190 | const int bias_processed_per_iteration_x = num_elems_processed_per_iteration_x; |
| 191 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 192 | AccessWindowStatic src2_access(src2, 0, 0, ceil_to_multiple(src2->dimension(0), bias_processed_per_iteration_x), |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 193 | src2->dimension(1)); |
| 194 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 195 | window_changed = update_window_and_padding(win, src0_access, src1_access, |
| 196 | src2_access) || // window used by the execute_window_loop |
| 197 | update_window_and_padding( |
| 198 | win_out, dst_access); // window used to update the padding requirements of dst tensor |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 199 | } |
| 200 | else |
| 201 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 202 | window_changed = |
| 203 | update_window_and_padding(win, src0_access, src1_access) || // window used by the execute_window_loop |
| 204 | update_window_and_padding(win_out, |
| 205 | dst_access); // window used to update the padding requirements of dst tensor |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 206 | } |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 207 | |
| 208 | // Collapse along the Z direction |
| 209 | // This collapse needs to be here in order to tune the Z dimension of LWS |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 210 | Window collapsed = win; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 211 | const unsigned int dimension_to_collapse = std::min(static_cast<unsigned int>(dst->num_dimensions()), 2u); |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 212 | collapsed = win.collapse(win, dimension_to_collapse); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 213 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 214 | Status err = |
| 215 | (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 216 | return std::make_pair(err, collapsed); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 217 | } |
| 218 | } // namespace |
| 219 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 220 | ClGemmMatrixMultiplyNativeKernel::ClGemmMatrixMultiplyNativeKernel() |
| 221 | { |
| 222 | _type = CLKernelType::GEMM; |
| 223 | } |
| 224 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 225 | void ClGemmMatrixMultiplyNativeKernel::configure(const CLCompileContext &compile_context, |
| 226 | ITensorInfo *src0, |
| 227 | ITensorInfo *src1, |
| 228 | ITensorInfo *src2, |
| 229 | ITensorInfo *dst, |
| 230 | float alpha, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 231 | float beta, |
| 232 | const GEMMLHSMatrixInfo &lhs_info, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 233 | const GEMMRHSMatrixInfo &rhs_info, |
| 234 | const GEMMKernelInfo &gemm_info) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 235 | { |
| 236 | ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst); |
| 237 | |
SiCongLi | 48717a3 | 2021-10-28 18:42:51 +0100 | [diff] [blame] | 238 | // dst tensor auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 239 | auto_init_if_empty( |
| 240 | *dst, src0->clone()->set_tensor_shape(misc::shape_calculator::compute_mm_shape(*src0, *src1, gemm_info))); |
SiCongLi | 48717a3 | 2021-10-28 18:42:51 +0100 | [diff] [blame] | 241 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 242 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info)); |
| 243 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 244 | auto padding_info = get_padding_info({src0, dst}); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 245 | _reinterpret_input_as_3d = gemm_info.reinterpret_input_as_3d; |
| 246 | _reinterpret_output_as_3d = gemm_info.depth_output_gemm3d != 0; |
| 247 | _use_dummy_work_items = preferred_dummy_work_items_support(CLKernelLibrary::get().get_device()); |
| 248 | _add_bias = src2 != nullptr; |
| 249 | |
| 250 | // In case both input and dst have to be reinterpreted as 3D tensors, |
| 251 | // force reinterpret_input_as_3d and reinterpret_output_as_3d to be false. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 252 | if (_reinterpret_input_as_3d == _reinterpret_output_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 253 | { |
| 254 | _reinterpret_input_as_3d = false; |
| 255 | _reinterpret_output_as_3d = false; |
| 256 | } |
| 257 | |
| 258 | // Check if we need to slide the matrix B |
| 259 | const unsigned int num_dimensions_src0 = src0->num_dimensions(); |
| 260 | _slide_matrix_b = (src1->num_dimensions() >= num_dimensions_src0); |
| 261 | |
| 262 | ElementsProcessed num_elements_processed{}; |
| 263 | |
| 264 | // Configure kernel window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | auto win_config = validate_and_configure_window(src0, src1, src2 != nullptr ? src2 : nullptr, dst, lhs_info, |
| 266 | rhs_info, gemm_info, num_elements_processed); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 267 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 268 | IClKernel::configure_internal(win_config.second); |
| 269 | |
| 270 | // If _reinterpret_input_as_3d = _reinterpret_output_as_3d = true, |
| 271 | // we will dispatch a batched-GEMM to reduce the complexity of the address calculation within the OpenCL kernel. |
| 272 | // This means that the actual m used by the kernel is given by dst->dimension(1) and not by gemm_info.m |
| 273 | const unsigned int internal_m = _reinterpret_output_as_3d ? gemm_info.m : dst->dimension(1); |
| 274 | |
| 275 | const unsigned int h_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(1) : src0->dimension(1); |
| 276 | const unsigned int d_gemm_3d = _reinterpret_output_as_3d ? dst->dimension(2) : src0->dimension(2); |
| 277 | |
| 278 | // 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. |
| 279 | const unsigned int partial_store_m0 = internal_m % lhs_info.m0; |
| 280 | const unsigned int partial_store_n0 = gemm_info.n % rhs_info.n0; |
| 281 | |
| 282 | // Shrink M0 to be always <= M (internal_m) to prevent out-of-bounds reads. |
| 283 | // NOTE: This might have implications on heuristics and performance |
| 284 | const unsigned int internal_m0 = std::min(internal_m, lhs_info.m0); |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 285 | _m = internal_m; |
| 286 | _n = gemm_info.n; |
| 287 | _k = gemm_info.k; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 288 | |
| 289 | // Create build options |
| 290 | CLBuildOptions build_opts; |
| 291 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src0->data_type())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 292 | build_opts.add_option_if(!(helpers::float_ops::is_one(alpha)), |
| 293 | "-DALPHA=" + float_to_string_with_full_precision(alpha)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 294 | build_opts.add_option_if(src2 != nullptr, "-DBETA=" + float_to_string_with_full_precision(beta)); |
| 295 | build_opts.add_option_if(helpers::float_ops::is_one(beta), "-DUNIT_BETA"); |
| 296 | build_opts.add_option_if(gemm_info.broadcast_bias, "-DBROADCAST_BIAS"); |
| 297 | build_opts.add_option_if(_reinterpret_input_as_3d, "-DREINTERPRET_INPUT_AS_3D"); |
| 298 | build_opts.add_option_if(_reinterpret_output_as_3d, "-DREINTERPRET_OUTPUT_AS_3D"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 299 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, |
| 300 | "-DHEIGHT_GEMM3D=" + support::cpp11::to_string(h_gemm_3d)); |
| 301 | build_opts.add_option_if(_reinterpret_input_as_3d || _reinterpret_output_as_3d, |
| 302 | "-DDEPTH_GEMM3D=" + support::cpp11::to_string(d_gemm_3d)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 303 | build_opts.add_option_if(!_slide_matrix_b, "-DMATRIX_B_DEPTH=" + support::cpp11::to_string(src1->dimension(2))); |
| 304 | build_opts.add_option_if(_use_dummy_work_items, "-DDUMMY_WORK_ITEMS"); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 305 | build_opts.add_option("-DM0=" + support::cpp11::to_string(internal_m0)); |
| 306 | build_opts.add_option("-DN0=" + support::cpp11::to_string(rhs_info.n0)); |
| 307 | build_opts.add_option("-DK0=" + support::cpp11::to_string(rhs_info.k0)); |
| 308 | build_opts.add_option("-DPARTIAL_STORE_M0=" + support::cpp11::to_string(partial_store_m0)); |
| 309 | build_opts.add_option("-DPARTIAL_STORE_N0=" + support::cpp11::to_string(partial_store_n0)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 310 | build_opts.add_option_if(gemm_info.activation_info.enabled(), |
| 311 | "-DACTIVATION_TYPE=" + |
| 312 | lower_string(string_from_activation_func(gemm_info.activation_info.activation()))); |
| 313 | build_opts.add_option_if(gemm_info.activation_info.enabled(), |
| 314 | "-DA_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.a())); |
| 315 | build_opts.add_option_if(gemm_info.activation_info.enabled(), |
| 316 | "-DB_VAL=" + float_to_string_with_full_precision(gemm_info.activation_info.b())); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 317 | |
| 318 | std::string kernel_name("gemm_mm_native"); |
| 319 | |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 320 | // A macro guard to compile ONLY the kernel of interest |
| 321 | build_opts.add_option("-D" + upper_string(kernel_name)); |
| 322 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 323 | // Create kernel |
| 324 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 325 | |
| 326 | // Set config_id for enabling LWS tuning |
| 327 | _config_id = kernel_name; |
| 328 | _config_id += "_"; |
| 329 | _config_id += (_add_bias ? "add_bias_" : ""); |
| 330 | _config_id += (gemm_info.broadcast_bias ? "broadcast_bias_" : ""); |
| 331 | _config_id += (_reinterpret_input_as_3d ? "3di_" : ""); |
| 332 | _config_id += (_reinterpret_output_as_3d ? "3do_" : ""); |
| 333 | _config_id += (gemm_info.activation_info.enabled() ? "fused_activation_" : ""); |
| 334 | _config_id += lower_string(string_from_data_type(src0->data_type())); |
| 335 | _config_id += "_"; |
| 336 | _config_id += support::cpp11::to_string(dst->dimension(1)); |
| 337 | _config_id += "_"; |
| 338 | _config_id += support::cpp11::to_string(dst->dimension(0)); |
| 339 | _config_id += "_"; |
| 340 | _config_id += support::cpp11::to_string(gemm_info.k); |
| 341 | _config_id += "_"; |
| 342 | _config_id += support::cpp11::to_string(dst->dimension(2)); |
| 343 | _config_id += "_"; |
| 344 | _config_id += support::cpp11::to_string(lhs_info.m0); |
| 345 | _config_id += "_"; |
| 346 | _config_id += support::cpp11::to_string(rhs_info.n0); |
| 347 | _config_id += "_"; |
| 348 | _config_id += support::cpp11::to_string(rhs_info.k0); |
| 349 | |
| 350 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 351 | } |
| 352 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 353 | Status ClGemmMatrixMultiplyNativeKernel::validate(const ITensorInfo *src0, |
| 354 | const ITensorInfo *src1, |
| 355 | const ITensorInfo *src2, |
| 356 | const ITensorInfo *dst, |
| 357 | float alpha, |
| 358 | float beta, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 359 | const GEMMLHSMatrixInfo &lhs_info, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 360 | const GEMMRHSMatrixInfo &rhs_info, |
| 361 | const GEMMKernelInfo &gemm_info) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 362 | { |
| 363 | ElementsProcessed num_elements_processed{}; |
| 364 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, alpha, beta, lhs_info, rhs_info, gemm_info)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 365 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src0->clone().get(), src1->clone().get(), |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 366 | src2 != nullptr ? src2->clone().get() : nullptr, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 367 | dst->clone().get(), lhs_info, rhs_info, gemm_info, |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 368 | num_elements_processed) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 369 | .first); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 370 | |
| 371 | return Status{}; |
| 372 | } |
| 373 | |
| 374 | void ClGemmMatrixMultiplyNativeKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 375 | { |
| 376 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 377 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 378 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 379 | const auto src0 = |
| 380 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0)); |
| 381 | const auto src1 = |
| 382 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1)); |
| 383 | const auto src2 = |
| 384 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2)); |
| 385 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 386 | |
| 387 | ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst); |
| 388 | ARM_COMPUTE_ERROR_ON(_add_bias && src2 == nullptr); |
| 389 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 390 | if (src1->info()->num_dimensions() < 3) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 391 | { |
| 392 | // The stride_z for matrix B must be zero if we do not slice |
| 393 | ARM_COMPUTE_ERROR_ON(src1->info()->strides_in_bytes()[3] != 0); |
| 394 | } |
| 395 | |
| 396 | Window slice = window.first_slice_window_3D(); |
| 397 | Window slice_matrix_b = slice; |
| 398 | |
| 399 | slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 400 | slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 401 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 402 | if (_reinterpret_input_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 403 | { |
| 404 | // Pass bottom paddings to the kernel if the input has to be reinterpreted as 3D tensor |
| 405 | unsigned int idx0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 406 | if (_add_bias) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 407 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 408 | idx0 = 4 * num_arguments_per_2D_tensor() + 7; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 409 | } |
| 410 | else |
| 411 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 412 | idx0 = 3 * num_arguments_per_2D_tensor() + 6; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 413 | } |
| 414 | const unsigned int total_cross_plane_pad = src0->info()->padding().top + src0->info()->padding().bottom; |
| 415 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 416 | } |
| 417 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 418 | if (_reinterpret_output_as_3d) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 419 | { |
| 420 | // Pass bottom paddings to the kernel if the dst has to be reinterpreted as 3D tensor |
| 421 | unsigned int idx0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 422 | if (_add_bias) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 423 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 424 | idx0 = 4 * num_arguments_per_2D_tensor() + 7 + (_reinterpret_input_as_3d ? 1 : 0); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 425 | } |
| 426 | else |
| 427 | { |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 428 | idx0 = 3 * num_arguments_per_2D_tensor() + 6 + (_reinterpret_input_as_3d ? 1 : 0); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 429 | } |
| 430 | const unsigned int total_cross_plane_pad = dst->info()->padding().top + dst->info()->padding().bottom; |
| 431 | _kernel.setArg<cl_uint>(idx0, static_cast<unsigned int>(total_cross_plane_pad)); |
| 432 | } |
| 433 | |
| 434 | do |
| 435 | { |
| 436 | Window slice_b = slice; |
| 437 | // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2 |
| 438 | // This scenario can happen when the matrix multiplication is used to perform a convolution operation |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 439 | if (!_slide_matrix_b) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 440 | { |
| 441 | slice_b = slice_matrix_b; |
| 442 | } |
| 443 | |
| 444 | unsigned int idx = 0; |
| 445 | add_2D_tensor_argument(idx, src0, slice); |
| 446 | add_2D_tensor_argument(idx, src1, slice_b); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 447 | if (_add_bias) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 448 | { |
| 449 | add_2D_tensor_argument(idx, src2, slice); |
| 450 | } |
| 451 | add_2D_tensor_argument(idx, dst, slice); |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 452 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 453 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src0->info()->strides_in_bytes()[2])); |
| 454 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src1->info()->strides_in_bytes()[2])); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 455 | if (_add_bias) |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 456 | { |
| 457 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(src2->info()->strides_in_bytes()[2])); |
| 458 | } |
| 459 | _kernel.setArg<cl_uint>(idx++, static_cast<unsigned int>(dst->info()->strides_in_bytes()[2])); |
ramelg01 | 9cca592 | 2021-11-11 10:05:00 +0000 | [diff] [blame] | 460 | |
| 461 | // Pass m, n and k at runtime |
| 462 | _kernel.setArg<cl_int>(idx++, _m); |
| 463 | _kernel.setArg<cl_int>(idx++, _n); |
| 464 | _kernel.setArg<cl_int>(idx++, _k); |
| 465 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 466 | enqueue(queue, *this, slice, lws_hint(), _use_dummy_work_items); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 467 | } while (window.slide_window_slice_3D(slice)); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 468 | } |
| 469 | } // namespace kernels |
| 470 | } // namespace opencl |
| 471 | } // namespace arm_compute |