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