Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "src/runtime/heuristics/matmul_native/ClMatMulNativeHelpers.h" |
| 25 | |
| 26 | #include "arm_compute/core/KernelDescriptors.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/TensorShape.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 30 | #include "src/gpu/cl/kernels/ClMatMulNativeKernel.h" |
| 31 | |
| 32 | #include <limits> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace cl_matmul |
| 38 | { |
| 39 | MatMulKernelInfo select_info(const MatMulKernelInfo &info0, |
| 40 | const MatMulKernelInfo &info1, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | unsigned int m, |
| 42 | unsigned int n, |
| 43 | unsigned int k, |
| 44 | unsigned int b, |
| 45 | DataType data_type, |
| 46 | bool rhs_lock_padding) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 47 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_MSG(info1.export_rhs_to_cl_image == true, |
| 49 | "The fallback MatMul configuration cannot have export_to_cl_image = true"); |
| 50 | ARM_COMPUTE_ERROR_ON_MSG(info0.adj_lhs != info1.adj_lhs, |
| 51 | "The MatMul configurations must have the same adj_lhs value"); |
| 52 | ARM_COMPUTE_ERROR_ON_MSG(info0.adj_rhs != info1.adj_rhs, |
| 53 | "The MatMul configurations must have the same adj_rhs value"); |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 54 | |
| 55 | const bool adj_lhs = info0.adj_lhs; |
| 56 | const bool adj_rhs = info0.adj_rhs; |
| 57 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | TensorInfo lhs_info = |
| 59 | !adj_lhs ? TensorInfo(TensorShape(k, m, b), 1, data_type) : TensorInfo(TensorShape(m, k, b), 1, data_type); |
| 60 | TensorInfo rhs_info = |
| 61 | !adj_rhs ? TensorInfo(TensorShape(n, k, b), 1, data_type) : TensorInfo(TensorShape(k, n, b), 1, data_type); |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 62 | TensorInfo dst_info; |
| 63 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | if (rhs_lock_padding == false) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 65 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | if (bool(opencl::kernels::ClMatMulNativeKernel::validate(&lhs_info, &rhs_info, nullptr, &dst_info, info0))) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 67 | { |
| 68 | return info0; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | return info1; |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | return info1; |
| 78 | } |
| 79 | } |
| 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | MatMulKernelInfo find_info(const MatMulNativeConfigsMatrix &configs, |
| 82 | bool adj_lhs, |
| 83 | bool adj_rhs, |
| 84 | unsigned int m, |
| 85 | unsigned int n, |
| 86 | unsigned int k, |
| 87 | unsigned int b) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 88 | { |
| 89 | size_t min_acc = std::numeric_limits<size_t>::max(); |
| 90 | size_t min_idx = 0; |
| 91 | |
| 92 | ARM_COMPUTE_ERROR_ON(configs.size() == 0); |
| 93 | const size_t num_rows = configs.size(); |
| 94 | const size_t num_cols = configs[0].size(); |
| 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_ERROR_ON_MSG(num_cols != 8U, |
| 97 | "The entry should have 8 integer values representing: M, N, K, B, M0, N0. K0, IMG_RHS"); |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 98 | ARM_COMPUTE_UNUSED(num_cols); |
| 99 | |
| 100 | // Find nearest GeMM workload |
| 101 | // Note: the workload does not depend on the K dimension |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | for (size_t y = 0; y < num_rows; ++y) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 103 | { |
| 104 | size_t mc0 = static_cast<size_t>(configs[y][0]); |
| 105 | size_t nc0 = static_cast<size_t>(configs[y][1]); |
| 106 | size_t kc0 = static_cast<size_t>(configs[y][2]); |
| 107 | size_t bc0 = static_cast<size_t>(configs[y][3]); |
| 108 | |
| 109 | size_t acc = 0; |
| 110 | acc += (m - mc0) * (m - mc0); |
| 111 | acc += (n - nc0) * (n - nc0); |
| 112 | acc += (k - kc0) * (k - kc0); |
| 113 | acc += (b - bc0) * (b - bc0); |
| 114 | acc = std::sqrt(acc); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | if (acc < min_acc) |
Gian Marco Iodice | 352c07d | 2023-05-03 12:21:38 +0100 | [diff] [blame] | 116 | { |
| 117 | min_acc = acc; |
| 118 | min_idx = y; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Get the configuration from the nearest GeMM shape |
| 123 | MatMulKernelInfo desc; |
| 124 | desc.adj_lhs = adj_lhs; |
| 125 | desc.adj_rhs = adj_rhs; |
| 126 | desc.m0 = configs[min_idx][4]; |
| 127 | desc.n0 = configs[min_idx][5]; |
| 128 | desc.k0 = configs[min_idx][6]; |
| 129 | desc.export_rhs_to_cl_image = configs[min_idx][7]; |
| 130 | |
| 131 | return desc; |
| 132 | } |
| 133 | } // namespace cl_matmul |
| 134 | } // namespace arm_compute |