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