| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #ifndef KLEIDIAI_BENCHMARK_MATMUL_MATMUL_RUNNER_HPP |
| 8 | #define KLEIDIAI_BENCHMARK_MATMUL_MATMUL_RUNNER_HPP |
| 9 | |
| 10 | #include <cfloat> |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <test/common/data_type.hpp> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "kai/kai_common.h" |
| 17 | #include "kai/ukernels/matmul/kai_matmul_types.h" |
| 18 | #include "matmul_interface.hpp" |
| 19 | |
| 20 | namespace kai::benchmark { |
| 21 | |
| 22 | using DataType = test::DataType; |
| 23 | |
| 24 | /// Runner for the matrix multiplication micro-kernel. |
| 25 | /// |
| 26 | /// Prepares and executes the run method of the micro-kernel. |
| 27 | /// |
| 28 | /// @tparam MatMulInterface Interface of the matrix multiplication micro-kernel. |
| 29 | template <typename MatMulInterface> |
| 30 | class MatMulRunner { |
| 31 | public: |
| 32 | /// Constructs a MatMulRunner object. |
| 33 | /// |
| 34 | /// @param matmul_interface Abstraction containing the micro-kernel to run. |
| 35 | /// @param dst_type Output type of the micro-kernel. Required for the micro-kernel to make certain assumptions |
| 36 | /// internally about the stride of the data. |
| 37 | MatMulRunner(const MatMulInterface& matmul_interface, const DataType dst_type) : |
| 38 | matmul_interface_(matmul_interface), dst_type_(dst_type) { |
| 39 | } |
| 40 | |
| 41 | /// Sets the M, N and K dimensions to describe the operand and result matrices. |
| 42 | /// |
| 43 | /// @param m Rows in a non-transposed LHS and DST matrix. |
| 44 | /// @param n Columns in a non-transposed RHS and DST matrix. |
| 45 | /// @param k Columns in a non-transposed LHS matrix, and rows in a non-transposed RHS matrix. |
| 46 | void set_mnk(const size_t m, const size_t n, const size_t k) { |
| 47 | m_ = m; |
| 48 | n_ = n; |
| 49 | k_ = k; |
| 50 | |
| 51 | lhs_stride_ = k_ * data_type_size_in_bits(dst_type_) / 8; |
| 52 | dst_stride_row_ = n_ * data_type_size_in_bits(dst_type_) / 8; |
| 53 | dst_stride_col_ = data_type_size_in_bits(dst_type_) / 8; |
| 54 | } |
| 55 | |
| 56 | /// Sets the block size to use. |
| 57 | /// |
| 58 | /// @param bl Block size. Used for micro-kernels with dynamic blockwise quantization. |
| 59 | void set_bl(const size_t bl) { |
| 60 | bl_ = bl; |
| 61 | } |
| 62 | |
| 63 | /// Runs the matrix multiplication micro-kernel. |
| 64 | /// |
| 65 | /// @param lhs Buffer containing LHS matrix data. |
| 66 | /// @param rhs Buffer containing RHS matrix data. |
| 67 | /// @param dst Destination buffer to write to. |
| 68 | void run(const void* lhs, const void* rhs, void* dst); |
| 69 | |
| 70 | /// Prepares auxiliary data required by the matrix multiplication micro-kernel. |
| 71 | void prepare(); |
| 72 | |
| 73 | private: |
| 74 | MatMulInterface matmul_interface_ = {}; |
| 75 | |
| 76 | DataType dst_type_ = DataType::FP32; |
| 77 | |
| 78 | size_t m_ = 1; |
| 79 | size_t n_ = 1; |
| 80 | size_t k_ = 1; |
| 81 | size_t bl_ = 32; |
| 82 | |
| 83 | size_t lhs_stride_ = 1; |
| 84 | size_t dst_stride_row_ = 1; |
| 85 | size_t dst_stride_col_ = 1; |
| 86 | |
| 87 | std::vector<std::byte> acc_bias_m_; |
| 88 | std::vector<std::byte> acc_bias_n_; |
| 89 | std::vector<std::byte> scale_bias_n_; |
| 90 | std::vector<std::byte> acc_scale_global_; |
| 91 | std::vector<std::byte> scale_bias_global_; |
| 92 | }; |
| 93 | |
| 94 | /// Prepares auxiliary data required by the matrix multiplication micro-kernel. |
| 95 | template <typename MatMulInterface> |
| 96 | void MatMulRunner<MatMulInterface>::prepare() { |
| 97 | // Default to no-op |
| 98 | } |
| 99 | |
| 100 | /// Runs the matrix multiplication micro-kernel. |
| 101 | /// |
| 102 | /// @param lhs Buffer containing LHS matrix data. |
| 103 | /// @param rhs Buffer containing RHS matrix data. |
| 104 | /// @param dst Destination buffer to write to. |
| 105 | template <typename MatMulInterface> |
| 106 | void MatMulRunner<MatMulInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 107 | matmul_interface_.run_matmul( |
| 108 | m_, n_, k_, // |
| 109 | lhs, rhs, dst, // |
| 110 | dst_stride_row_, dst_stride_col_, // |
| 111 | -FLT_MAX, FLT_MAX // |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /// Runs the matrix multiplication micro-kernel. Specialized on the strided LHS interface. |
| 116 | /// |
| 117 | /// @param lhs Buffer containing LHS matrix data. |
| 118 | /// @param rhs Buffer containing RHS matrix data. |
| 119 | /// @param dst Destination buffer to write to. |
| 120 | template <> |
| 121 | inline void MatMulRunner<MatMulStridedLhsInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 122 | matmul_interface_.run_matmul( |
| 123 | m_, n_, k_, // |
| 124 | lhs, lhs_stride_, rhs, dst, // |
| 125 | dst_stride_row_, dst_stride_col_, // |
| 126 | -FLT_MAX, FLT_MAX // |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /// Runs the matrix multiplication micro-kernel. Specialized on the interface with a floating point destination buffer. |
| 131 | /// |
| 132 | /// @param lhs Buffer containing LHS matrix data. |
| 133 | /// @param rhs Buffer containing RHS matrix data. |
| 134 | /// @param dst Destination buffer to write to. |
| 135 | template <> |
| 136 | inline void MatMulRunner<MatMulFloatInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 137 | matmul_interface_.run_matmul( |
| 138 | m_, n_, k_, // |
| 139 | lhs, rhs, static_cast<float*>(dst), // |
| 140 | dst_stride_row_, dst_stride_col_, // |
| 141 | -FLT_MAX, FLT_MAX // |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /// Runs the matrix multiplication micro-kernel. Specialized on the static quantization interface. |
| 146 | /// |
| 147 | /// @param lhs Buffer containing LHS matrix data. |
| 148 | /// @param rhs Buffer containing RHS matrix data. |
| 149 | /// @param dst Destination buffer to write to. |
| 150 | template <> |
| 151 | inline void MatMulRunner<MatMulStaticQuantInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 152 | constexpr kai_matmul_requantize32_params params = {INT8_MIN, INT8_MAX, 0}; |
| 153 | matmul_interface_.run_matmul( |
| 154 | m_, n_, k_, // |
| 155 | lhs, rhs, dst, // |
| 156 | dst_stride_row_, dst_stride_col_, // |
| 157 | ¶ms // |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /// Runs the matrix multiplication micro-kernel. Specialized on the dynamic blockwise quantization interface with |
| 162 | /// generic destination buffer. |
| 163 | /// |
| 164 | /// @param lhs Buffer containing LHS matrix data. |
| 165 | /// @param rhs Buffer containing RHS matrix data. |
| 166 | /// @param dst Destination buffer to write to. |
| 167 | template <> |
| 168 | inline void MatMulRunner<MatMulBlockwiseDynamicQuantGenericDstInterface>::run( |
| 169 | const void* lhs, const void* rhs, void* dst) { |
| 170 | matmul_interface_.run_matmul( |
| 171 | m_, n_, k_, bl_, // |
| 172 | lhs, rhs, dst, // |
| 173 | dst_stride_row_, dst_stride_col_, // |
| 174 | -FLT_MAX, FLT_MAX // |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | /// Runs the matrix multiplication micro-kernel. Specialized on the dynamic blockwise quantization interface. |
| 179 | /// |
| 180 | /// @param lhs Buffer containing LHS matrix data. |
| 181 | /// @param rhs Buffer containing RHS matrix data. |
| 182 | /// @param dst Destination buffer to write to. |
| 183 | template <> |
| 184 | inline void MatMulRunner<MatMulBlockwiseDynamicQuantInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 185 | matmul_interface_.run_matmul( |
| 186 | m_, n_, k_, bl_, // |
| 187 | lhs, rhs, static_cast<float*>(dst), // |
| 188 | dst_stride_row_, dst_stride_col_, // |
| 189 | -FLT_MAX, FLT_MAX // |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /// Runs the matrix multiplication micro-kernel. Specialized on the dynamic blockwise quantization interface with look |
| 194 | /// up table. |
| 195 | /// |
| 196 | /// @param lhs Buffer containing LHS matrix data. |
| 197 | /// @param rhs Buffer containing RHS matrix data. |
| 198 | /// @param dst Destination buffer to write to. |
| 199 | template <> |
| 200 | inline void MatMulRunner<MatMulBlockwiseDynamicQuantLutInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 201 | matmul_interface_.run_matmul( |
| 202 | m_, n_, k_, // |
| 203 | lhs, rhs, static_cast<float*>(dst), // |
| 204 | dst_stride_row_, dst_stride_col_, // |
| 205 | -FLT_MAX, FLT_MAX, // |
| 206 | nullptr); |
| 207 | } |
| 208 | |
| 209 | /// Runs the matrix multiplication micro-kernel. Specialized on the ukernel API interface. |
| 210 | /// |
| 211 | /// @param lhs Buffer containing LHS matrix data. |
| 212 | /// @param rhs Buffer containing RHS matrix data. |
| 213 | /// @param dst Destination buffer to write to. |
| 214 | template <> |
| 215 | inline void MatMulRunner<MatMulUkernelApiInterface>::run(const void* lhs, const void* rhs, void* dst) { |
| 216 | struct ClampArgs { |
| 217 | float min; |
| 218 | float max; |
| 219 | }; |
| 220 | |
| 221 | const auto api = matmul_interface_.get_api(); |
| 222 | const auto config = matmul_interface_.get_config(); |
| 223 | |
| 224 | const ClampArgs clamp_args{-FLT_MAX, FLT_MAX}; |
| 225 | const bool has_clamp = (matmul_interface_.flags & KAI_MATMUL_UKER_FLAGS_ARGS_CLAMP) != 0; |
| 226 | |
| 227 | const kai_matmul_uker_lhs_dim_args lhs_shape = {m_, k_}; |
| 228 | const kai_matmul_uker_rhs_dim_args rhs_shape = {n_, k_}; |
| 229 | |
| 230 | kai_matmul_uker_args args = {}; |
| 231 | args.flags = matmul_interface_.flags; |
| 232 | |
| 233 | args.shape.m = m_; |
| 234 | args.shape.n = n_; |
| 235 | args.shape.k = k_; |
| 236 | |
| 237 | args.operand.lhs.ptr = lhs; |
| 238 | args.operand.lhs.stride = api.get_lhs_stride(&config, &lhs_shape); |
| 239 | |
| 240 | args.operand.rhs.ptr = rhs; |
| 241 | args.operand.rhs.stride = api.get_rhs_stride(&config, &rhs_shape); |
| 242 | |
| 243 | args.operand.dst.ptr = dst; |
| 244 | args.operand.dst.stride.m = dst_stride_row_; |
| 245 | |
| 246 | if (has_clamp) { |
| 247 | args.activation.clamp.min_ptr = &clamp_args.min; |
| 248 | args.activation.clamp.max_ptr = &clamp_args.max; |
| 249 | } |
| 250 | |
| 251 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_BIAS_M) != 0) { |
| 252 | args.operand.bias.acc_bias_m.ptr = acc_bias_m_.data(); |
| 253 | } |
| 254 | |
| 255 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_BIAS_N) != 0) { |
| 256 | args.operand.bias.acc_bias_n.ptr = acc_bias_n_.data(); |
| 257 | } |
| 258 | |
| 259 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_SCALE_BIAS_N) != 0) { |
| 260 | args.operand.bias.scale_bias_n.ptr = scale_bias_n_.data(); |
| 261 | } |
| 262 | |
| 263 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_SCALE_GLOBAL) != 0) { |
| 264 | args.operand.scale.acc_scale_global.ptr = acc_scale_global_.data(); |
| 265 | } |
| 266 | |
| 267 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_SCALE_BIAS_GLOBAL) != 0) { |
| 268 | args.operand.bias.scale_bias_global.ptr = scale_bias_global_.data(); |
| 269 | } |
| 270 | |
| 271 | api.run(&config, &args); |
| 272 | } |
| 273 | |
| 274 | /// Prepares auxiliary data required by the ukernel API interface. |
| 275 | template <> |
| 276 | inline void MatMulRunner<MatMulUkernelApiInterface>::prepare() { |
| 277 | acc_bias_m_.clear(); |
| 278 | acc_bias_n_.clear(); |
| 279 | scale_bias_n_.clear(); |
| 280 | acc_scale_global_.clear(); |
| 281 | scale_bias_global_.clear(); |
| 282 | |
| 283 | // Allocate row bias for accumulation stage |
| 284 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_BIAS_M) != 0) { |
| 285 | KAI_ASSUME(matmul_interface_.acc_bias_elem_size != 0); |
| 286 | acc_bias_m_.resize(m_ * matmul_interface_.acc_bias_elem_size); |
| 287 | } |
| 288 | |
| 289 | // Allocate column bias for accumulation stage |
| 290 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_BIAS_N) != 0) { |
| 291 | KAI_ASSUME(matmul_interface_.acc_bias_elem_size != 0); |
| 292 | acc_bias_n_.resize(n_ * matmul_interface_.acc_bias_elem_size); |
| 293 | } |
| 294 | |
| 295 | // Allocate global scale for the accumulation stage |
| 296 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_ACC_SCALE_GLOBAL) != 0) { |
| 297 | KAI_ASSUME(matmul_interface_.acc_scale_elem_size != 0); |
| 298 | acc_scale_global_.resize(matmul_interface_.acc_scale_elem_size); |
| 299 | } |
| 300 | |
| 301 | // Allocate column bias for the scaled accumulation stage |
| 302 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_SCALE_BIAS_N) != 0) { |
| 303 | KAI_ASSUME(matmul_interface_.scale_bias_elem_size != 0); |
| 304 | scale_bias_n_.resize(n_ * matmul_interface_.scale_bias_elem_size); |
| 305 | } |
| 306 | |
| 307 | // Allocate global bias for the scaled accumulation stage |
| 308 | if ((matmul_interface_.args_flags & KAI_BENCHMARK_MATMUL_UKER_ARGS_SCALE_BIAS_GLOBAL) != 0) { |
| 309 | KAI_ASSUME(matmul_interface_.scale_bias_elem_size != 0); |
| 310 | scale_bias_global_.resize(matmul_interface_.scale_bias_elem_size); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } // namespace kai::benchmark |
| 315 | |
| 316 | #endif // KLEIDIAI_BENCHMARK_MATMUL_MATMUL_RUNNER_HPP |