| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | #pragma once |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <cstdint> |
| 10 | #include <functional> |
| 11 | #include <iosfwd> |
| 12 | #include <optional> |
| 13 | #include <string> |
| 14 | #include <string_view> |
| 15 | #include <tuple> |
| 16 | |
| 17 | #include "kai/kai_common.h" |
| 18 | #include "test/common/buffer.hpp" |
| 19 | #include "test/common/data_format.hpp" |
| 20 | #include "test/common/data_type.hpp" |
| 21 | #include "test/common/float16.hpp" |
| 22 | #include "test/common/matrix_portion.hpp" |
| 23 | #include "test/common/range.hpp" |
| 24 | #include "test/common/seed.hpp" |
| 25 | |
| 26 | namespace kai::test { |
| 27 | /// Matrix multiplication shape. |
| 28 | struct MatMulShape { |
| 29 | size_t m; ///< LHS height. |
| 30 | size_t n; ///< RHS width. |
| 31 | size_t k; ///< LHS width and RHS height. |
| 32 | |
| 33 | struct Hash { |
| 34 | size_t operator()(const MatMulShape& shape) const { |
| 35 | return // |
| 36 | (std::hash<size_t>{}(shape.m) << 0) ^ // |
| 37 | (std::hash<size_t>{}(shape.n) << 1) ^ // |
| 38 | (std::hash<size_t>{}(shape.k) << 2); // |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | private: |
| 43 | friend bool operator==(const MatMulShape& lhs, const MatMulShape& rhs) { |
| 44 | return // |
| 45 | lhs.m == rhs.m && // |
| 46 | lhs.n == rhs.n && // |
| 47 | lhs.k == rhs.k; |
| 48 | } |
| 49 | friend std::ostream& operator<<(std::ostream& os, const MatMulShape& shape); |
| 50 | }; |
| 51 | |
| 52 | // NOLINTBEGIN(misc-non-private-member-variables-in-classes) |
| 53 | |
| 54 | enum class MatMulMethodCapability : uint8_t { |
| 55 | NONE = 0, |
| 56 | PACKED_LHS = 1U << 0, |
| 57 | PACKED_RHS = 1U << 1, |
| 58 | PACKED_TRANSPOSED_RHS = 1U << 2, |
| 59 | OUTPUT = 1U << 3, |
| 60 | }; |
| 61 | |
| 62 | [[nodiscard]] constexpr MatMulMethodCapability operator|( |
| 63 | const MatMulMethodCapability lhs, const MatMulMethodCapability rhs) { |
| 64 | return static_cast<MatMulMethodCapability>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs)); |
| 65 | } |
| 66 | |
| 67 | /// Matrix multiplication method. |
| 68 | struct MatMulMethod { |
| 69 | std::string_view name{}; ///< Name of matmul method. |
| 70 | MatMulMethodCapability capabilities{MatMulMethodCapability::NONE}; ///< Expected test capabilities. |
| 71 | |
| 72 | size_t m0{0}; ///< Block size in M dimension. |
| 73 | size_t n0{0}; ///< Block size in N dimension. |
| 74 | size_t k0{0}; ///< Block size in K dimension. |
| 75 | |
| 76 | DataFormat dst_format{}; ///< Data format of the destination matrix. |
| 77 | DataFormat lhs_format{}; ///< Data format of the LHS matrix. |
| 78 | DataFormat packed_lhs_format{}; ///< Data format of the packed LHS matrix. |
| 79 | DataFormat rhs_format{}; ///< Data format of the RHS matrix. |
| 80 | DataFormat packed_rhs_format{}; ///< Data format of the packed RHS matrix. |
| 81 | DataFormat bias_format{}; ///< Data format of the bias vector. |
| 82 | DataType acc_dt{DataType::UNKNOWN}; ///< Reference accumulator data type, if specified. |
| 83 | bool nb_support{}; ///< Does the kernel support null_bias. |
| 84 | |
| 85 | [[nodiscard]] bool has_capability(const MatMulMethodCapability capability) const { |
| 86 | return (static_cast<uint8_t>(capabilities) & static_cast<uint8_t>(capability)) != 0; |
| 87 | } |
| 88 | |
| 89 | /// Generate LHS matrix. |
| 90 | /// |
| 91 | /// @param[in] m Number of rows in the LHS matrix. |
| 92 | /// @param[in] k Number of columns in the LHS matrix. |
| 93 | /// @param[in] seed_feed Seed feed for random number generation. |
| 94 | /// |
| 95 | /// @return LHS matrix data buffer. |
| 96 | std::function<Buffer(size_t, size_t, SeedFeed&)> fn_generate_lhs{nullptr}; |
| 97 | |
| 98 | /// Generate RHS matrix. |
| 99 | /// |
| 100 | /// @param[in] k Number of rows in the RHS matrix. |
| 101 | /// @param[in] n Number of columns in the RHS matrix. |
| 102 | /// @param[in] seed_feed Seed feed for random number generation. |
| 103 | /// |
| 104 | /// @return RHS matrix data buffer. |
| 105 | std::function<Buffer(size_t, size_t, SeedFeed&)> fn_generate_rhs{nullptr}; |
| 106 | |
| 107 | /// Generate bias. |
| 108 | /// |
| 109 | /// @param[in] n Number of rows in the bias. |
| 110 | /// @param[in] k Number of columns in the bias. |
| 111 | /// @param[in] seed_feed Seed feed for random number generation. |
| 112 | /// @param[in] null_bias_mode Whether to generate null bias (true) or real bias (false). |
| 113 | /// |
| 114 | /// @return Bias data buffer. |
| 115 | std::function<Buffer(size_t, size_t, SeedFeed&, bool)> fn_generate_bias{nullptr}; |
| 116 | |
| 117 | /// Check if CPU supports required features. |
| 118 | /// |
| 119 | /// @return Supported (true) or not supported (false). |
| 120 | std::function<bool(void)> fn_is_supported{nullptr}; |
| 121 | |
| 122 | /// Gets mr value. |
| 123 | /// |
| 124 | /// This is the packing parameter which must be used to pack the LHS matrix (if necessary). |
| 125 | /// |
| 126 | /// @return The mr value. |
| 127 | std::function<size_t(void)> fn_get_mr{nullptr}; |
| 128 | |
| 129 | /// Gets nr value. |
| 130 | /// |
| 131 | /// This is the packing parameter which must be used to pack the RHS matrix (if necessary). |
| 132 | /// |
| 133 | /// @return The nr value. |
| 134 | std::function<size_t(void)> fn_get_nr{nullptr}; |
| 135 | |
| 136 | /// Gets kr value. |
| 137 | /// |
| 138 | /// This is the packing parameter which must be used to pack the LHS and RHS matrix (if necessary). |
| 139 | /// |
| 140 | /// @return The kr value. |
| 141 | std::function<size_t(void)> fn_get_kr{nullptr}; |
| 142 | |
| 143 | /// Gets sr value. |
| 144 | /// |
| 145 | /// This is the packing parameter which must be used to pack the RHS matrix. |
| 146 | /// |
| 147 | /// @return The sr value. |
| 148 | std::function<size_t(void)> fn_get_sr{nullptr}; |
| 149 | |
| 150 | /// Gets m step value for main kernel. |
| 151 | /// |
| 152 | /// The starting row index must be divisible by `m_step`. |
| 153 | /// |
| 154 | /// @return The m step value. |
| 155 | std::function<size_t(void)> fn_get_main_m_step{nullptr}; |
| 156 | |
| 157 | /// Gets n step value for RHS packing micro-kernel. |
| 158 | /// |
| 159 | /// The starting row index must be divisible by `n_step`. |
| 160 | /// |
| 161 | /// @return The n step value. |
| 162 | std::function<size_t(void)> fn_get_pack_rhs_n_step{nullptr}; |
| 163 | |
| 164 | /// Gets n step value for main kernel. |
| 165 | /// |
| 166 | /// The starting column index must be divisible by `n_step`. |
| 167 | /// |
| 168 | /// @return The n step value. |
| 169 | std::function<size_t(void)> fn_get_main_n_step{nullptr}; |
| 170 | |
| 171 | /// Gets the offset in bytes of the LHS matrix. |
| 172 | /// |
| 173 | /// @param[in] m_idx Coordinate of the matrix in M dimension. |
| 174 | /// @param[in] stride Row stride in bytes. |
| 175 | /// |
| 176 | /// @return The offset in bytes. |
| 177 | std::function<size_t(size_t m_idx, size_t stride)> fn_get_lhs_offset{nullptr}; |
| 178 | |
| 179 | /// Gets the size in bytes of the packed LHS matrix. |
| 180 | /// |
| 181 | /// @param[in] m Number of rows in the unpacked LHS matrix. |
| 182 | /// @param[in] k Number of columns in the unpacked LHS matrix. |
| 183 | /// @param[in] mr Number of rows to be interleaved. |
| 184 | /// @param[in] kr Unused. Must be 1. |
| 185 | /// @param[in] sr Unused. Must be 1. |
| 186 | /// |
| 187 | /// @return The size in bytes. |
| 188 | std::function<size_t(size_t m, size_t k, size_t mr, size_t kr, size_t sr)> fn_get_packed_lhs_size{nullptr}; |
| 189 | |
| 190 | /// Gets the offset in bytes of the packed LHS matrix. |
| 191 | /// |
| 192 | /// @param[in] m_idx Coordinate of the matrix in M dimension. |
| 193 | /// @param[in] k Size of the matrix in K dimension. |
| 194 | /// |
| 195 | /// @return The offset in bytes. |
| 196 | std::function<size_t(size_t m_idx, size_t k)> fn_get_packed_lhs_offset{nullptr}; |
| 197 | |
| 198 | /// Preprocesses the LHS matrix. |
| 199 | /// |
| 200 | /// @param[in] m Number of rows of the unpacked LHS matrix. |
| 201 | /// @param[in] k Common dimension between the LHS and RHS matrix. |
| 202 | /// @param[in] mr Block size in M dimension. It must be {{ kernel.interleave_by }}VL. |
| 203 | /// @param[in] kr Block size in K dimension. It must be {{ kernel.block_by }}. |
| 204 | /// @param[in] sr Number of kr splits. It must be 1. |
| 205 | /// @param[in] m_idx_start Unused. Must be 0. |
| 206 | /// @param[in] lhs LHS matrix data buffer. |
| 207 | /// @param[in] lhs_stride Row stride in bytes of the LHS matrix. |
| 208 | /// @param[out] lhs_packed Packed RHS matrix. |
| 209 | std::function<void( |
| 210 | size_t m, size_t k, size_t mr, size_t kr, size_t sr, size_t m_idx_start, const void* lhs, size_t lhs_stride, |
| 211 | void* lhs_packed)> |
| 212 | fn_pack_lhs{nullptr}; |
| 213 | |
| 214 | /// Gets a value indicating whether LHS packing is needed. |
| 215 | [[nodiscard]] bool is_pack_lhs_needed() const { |
| 216 | return fn_pack_lhs != nullptr; |
| 217 | } |
| 218 | |
| 219 | /// Gets the offset in bytes of the RHS matrix. |
| 220 | /// |
| 221 | /// @param[in] n_idx Coordinate of the matrix in N dimension. |
| 222 | /// |
| 223 | /// @return The offset in bytes. |
| 224 | std::function<size_t(size_t n_idx)> fn_get_rhs_offset{nullptr}; |
| 225 | |
| 226 | /// Gets the size in bytes of the packed RHS matrix. |
| 227 | /// |
| 228 | /// @param[in] n Size of the matrix in N dimension. |
| 229 | /// @param[in] k Size of the matrix in K dimension. |
| 230 | /// |
| 231 | /// @return The size in bytes. |
| 232 | std::function<size_t(size_t n, size_t k)> fn_get_packed_rhs_size{nullptr}; |
| 233 | |
| 234 | /// Gets the size in bytes of the packed RHS matrix. |
| 235 | /// |
| 236 | /// @param[in] n Size of the matrix in N dimension. |
| 237 | /// @param[in] k Size of the matrix in K dimension. |
| 238 | /// @param[in] nr Block size in N dimension. |
| 239 | /// @param[in] kr Block size in K dimension. |
| 240 | /// |
| 241 | /// @return The size in bytes. |
| 242 | std::function<size_t(size_t n, size_t k, size_t nr, size_t kr)> fn_get_packed_rhs_size_generic_block_size = nullptr; |
| 243 | |
| 244 | /// Gets the offset in bytes of the packed RHS matrix in the RHS packing micro-kernel |
| 245 | /// |
| 246 | /// @param[in] n_idx Coordinate of the matrix in N dimension. |
| 247 | /// @param[in] k Size of the matrix in K dimension. |
| 248 | /// |
| 249 | /// @return The offset in bytes. |
| 250 | std::function<size_t(size_t n_idx, size_t k)> fn_get_pack_rhs_packed_rhs_offset{nullptr}; |
| 251 | |
| 252 | /// Gets the offset in bytes of the packed RHS matrix in the main kernel. |
| 253 | /// |
| 254 | /// @param[in] n_idx Coordinate of the matrix in N dimension. |
| 255 | /// @param[in] k Size of the matrix in K dimension. |
| 256 | /// |
| 257 | /// @return The offset in bytes. |
| 258 | std::function<size_t(size_t n_idx, size_t k)> fn_get_main_packed_rhs_offset{nullptr}; |
| 259 | |
| 260 | std::function<void( |
| 261 | size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t rhs_stride, const void* rhs, |
| 262 | const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes, const void* params)> |
| 263 | fn_pack_rhs{nullptr}; |
| 264 | |
| 265 | /// Gets n step value. |
| 266 | /// |
| 267 | /// The starting row index must be divisible by `n_step`. |
| 268 | /// |
| 269 | /// @return The n step value. |
| 270 | std::function<size_t()> fn_pack_rhs_nxk_get_n_step{nullptr}; |
| 271 | |
| 272 | /// Gets the offset in bytes to the data element in the RHS matrix buffer. |
| 273 | /// |
| 274 | /// @param[in] n_idx Column index. |
| 275 | /// @param[in] rhs_offset Row stride in bytes of the RHS matrix. |
| 276 | /// |
| 277 | /// @return The offset in bytes to the data element. |
| 278 | std::function<size_t(size_t n_idx, size_t rhs_stride)> fn_pack_rhs_nxk_get_rhs_offset{nullptr}; |
| 279 | |
| 280 | /// Gets the offset in bytes to the data element in the bias buffer. |
| 281 | /// |
| 282 | /// @param[in] n_idx Column index. |
| 283 | /// |
| 284 | /// @return The offset in bytes to the data element. |
| 285 | std::function<size_t(size_t n_idx)> fn_pack_rhs_nxk_get_bias_offset{nullptr}; |
| 286 | |
| 287 | /// Gets the offset in bytes to the data element in the packed RHS buffer. |
| 288 | /// |
| 289 | /// @param[in] n_idx Row index. |
| 290 | /// @param[in] k Number of columns. |
| 291 | /// |
| 292 | /// @return The offset in bytes to the data element. |
| 293 | std::function<size_t(size_t n_idx, size_t k)> fn_pack_rhs_nxk_get_packed_rhs_offset{nullptr}; |
| 294 | |
| 295 | /// Gets the size in bytes of the packed RHS buffer. |
| 296 | /// |
| 297 | /// @param[in] n Number of rows. |
| 298 | /// @param[in] k Number of columns. |
| 299 | /// |
| 300 | /// @return The size in bytes of the packed RHS buffer. |
| 301 | std::function<size_t(size_t n, size_t k)> fn_pack_rhs_nxk_get_packed_rhs_size{nullptr}; |
| 302 | |
| 303 | /// Runs the RHS packing micro-kernel for matrix multiplication. |
| 304 | /// |
| 305 | /// The pointer of each buffers (RHS, bias and packed RHS) needs to be added with offset |
| 306 | /// calculated using the following functions: |
| 307 | /// |
| 308 | /// * RHS: @ref kai_get_rhs_offset_rhs_pack_nxk_f32p2vlx1b_f32_f32_sme. |
| 309 | /// * Bias: @ref kai_get_bias_offset_rhs_pack_nxk_f32p2vlx1b_f32_f32_sme. |
| 310 | /// * Output: @ref kai_get_rhs_packed_offset_rhs_pack_nxk_f32p2vlx1b_f32_f32_sme. |
| 311 | /// |
| 312 | /// @param[in] num_groups Number of groups. It must be 1. |
| 313 | /// @param[in] n Number of columns of the output matrix. |
| 314 | /// @param[in] k Common dimension between the LHS and RHS matrix. |
| 315 | /// @param[in] nr Block size in N dimension. It must be 2 * kai_get_sme_vector_length_u32(). |
| 316 | /// @param[in] kr Block size in K dimension. It must be 1. |
| 317 | /// @param[in] sr Number of kr splits. It must be 1. |
| 318 | /// @param[in] rhs_stride Row stride in bytes of the RHS matrix. |
| 319 | /// @param[in] rhs RHS matrix data buffer. |
| 320 | /// @param[in] bias Bias matrix data buffer. |
| 321 | /// @param[in] scale Scale data buffer. It must be NULL. |
| 322 | /// @param[out] rhs_packed Packed RHS matrix. |
| 323 | /// @param[in] extra_bytes Extra bytes to append to the end of each row of the packed RHS matrix. It must be 0. |
| 324 | /// @param[in] params Extra packing parameters. It must be NULL. |
| 325 | std::function<void( |
| 326 | size_t num_groups, size_t n, size_t k, size_t nr, size_t kr, size_t sr, size_t rhs_stride, const void* rhs, |
| 327 | const void* bias, const void* scale, void* rhs_packed, size_t extra_bytes, const void* params)> |
| 328 | fn_pack_rhs_nxk{nullptr}; |
| 329 | |
| 330 | /// Gets the offset in bytes to the data element in the bias buffer. |
| 331 | /// |
| 332 | /// @param[in] n_idx Column index. |
| 333 | /// |
| 334 | /// @return The offset in bytes to the data element. |
| 335 | std::function<size_t(size_t n_idx)> fn_get_bias_offset{nullptr}; |
| 336 | |
| 337 | /// Gets the offset in bytes to the data element in the destination matrix buffer. |
| 338 | /// |
| 339 | /// @param[in] m_idx Row index. |
| 340 | /// @param[in] n_idx Column index. |
| 341 | /// @param[in] stride Row stride in bytes. |
| 342 | /// |
| 343 | /// @return The offset in bytes to the data element. |
| 344 | std::function<size_t(size_t m_idx, size_t n_idx, size_t stride)> fn_get_dst_offset{nullptr}; |
| 345 | |
| 346 | /// Gets the size in bytes of the destination matrix buffer. |
| 347 | /// |
| 348 | /// @param[in] m Number of rows. |
| 349 | /// @param[in] n Number of columns. |
| 350 | /// |
| 351 | /// @return The size in bytes of the destination matrix buffer. |
| 352 | std::function<size_t(size_t m, size_t n)> fn_get_dst_size{nullptr}; |
| 353 | |
| 354 | /// Performs F16 or F32 matrix multiplication with RHS packing |
| 355 | /// followed by clamp operation. |
| 356 | /// |
| 357 | /// @param[in] m Size of the matrix in M dimension. |
| 358 | /// @param[in] n Size of the matrix in N dimension. |
| 359 | /// @param[in] k Size of the matrix in K dimension. |
| 360 | /// @param[in] lhs LHS data buffer. |
| 361 | /// @param[in] packed_rhs Packed RHS data buffer. |
| 362 | /// @param[out] dst Output data buffer. |
| 363 | /// @param[in] lhs_stride LHS row stride. |
| 364 | /// @param[in] dst_stride_row Output row stride. |
| 365 | /// @param[in] dst_stride_col Output column stride. |
| 366 | /// @param[in] clamp_min Lower bound of the output data. |
| 367 | /// @param[in] clamp_max Upper bound of the output data. |
| 368 | std::function<void( |
| 369 | size_t m, size_t n, size_t k, // |
| 370 | const void* lhs, size_t lhs_stride, // |
| 371 | const void* packed_rhs, // |
| 372 | void* dst, size_t dst_stride_row, size_t dst_stride_col, // |
| 373 | float clamp_min, float clamp_max)> |
| 374 | fn_matmul_f16_f16_f16p = nullptr; |
| 375 | |
| 376 | std::function<void( |
| 377 | size_t m, size_t n, size_t k, // |
| 378 | const void* lhs, size_t lhs_stride, // |
| 379 | const void* packed_rhs, // |
| 380 | void* dst, size_t dst_stride_row, size_t dst_stride_col, // |
| 381 | float clamp_min, float clamp_max)> |
| 382 | fn_matmul_f32_f32_f32p = nullptr; |
| 383 | |
| 384 | /// Performs BF16 matrix multiplication with LHS and RHS packing |
| 385 | /// followed by clamp operation. |
| 386 | /// |
| 387 | /// @param[in] m Size of the matrix in M dimension. |
| 388 | /// @param[in] n Size of the matrix in N dimension. |
| 389 | /// @param[in] k Size of the matrix in K dimension. |
| 390 | /// @param[in] packed_lhs Packed LHS data buffer. |
| 391 | /// @param[in] packed_rhs Packed RHS data buffer. |
| 392 | /// @param[out] dst Output data buffer. |
| 393 | /// @param[in] dst_stride_row Output row stride. |
| 394 | /// @param[in] dst_stride_col Output column stride. |
| 395 | /// @param[in] clamp_min Lower bound of the output data. |
| 396 | /// @param[in] clamp_max Upper bound of the output data. |
| 397 | std::function<void( |
| 398 | size_t m, size_t n, size_t k, // |
| 399 | const void* packed_lhs, // |
| 400 | const void* packed_rhs, // |
| 401 | void* dst, size_t dst_stride_row, size_t dst_stride_col, // |
| 402 | float clamp_min, float clamp_max)> |
| 403 | fn_matmul_f32_bf16p_bf16p = nullptr; |
| 404 | |
| 405 | std::function<void( |
| 406 | size_t m, size_t n, size_t k, // |
| 407 | const void* packed_lhs, // |
| 408 | const void* packed_rhs, // |
| 409 | void* dst, size_t dst_stride_row, size_t dst_stride_col, // |
| 410 | float clamp_min, float clamp_max)> |
| 411 | fn_matmul_f16_bf16p_bf16p = nullptr; |
| 412 | |
| 413 | /// Performs F16 or F32 matrix multiplication with LHS & RHS packing |
| 414 | /// followed by clamp operation. |
| 415 | /// |
| 416 | /// @param[in] m Number of output rows to be computed. |
| 417 | /// @param[in] n Number of output columns to be computed. |
| 418 | /// @param[in] k Common dimension of the LHS and RHS operands. |
| 419 | /// @param[in] packed_lhs Packed LHS matrix buffer. |
| 420 | /// @param[in] packed_rhs Packed RHS matrix buffer. |
| 421 | /// @param[out] dst Output matrix buffer. |
| 422 | /// @param[in] dst_stride_row Row stride in bytes of the output matrix. |
| 423 | /// @param[in] dst_stride_col Column stride in bytes of the output matrix. |
| 424 | /// @param[in] clamp_min Minimum value to clamp the final result. |
| 425 | /// @param[in] clamp_max Maximum value to clamp the final result. |
| 426 | std::function<void( |
| 427 | size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, void* dst, size_t dst_stride_row, |
| 428 | size_t dst_stride_col, float clamp_min, float clamp_max)> |
| 429 | fn_matmul_f16_f16p_f16p = nullptr; |
| 430 | |
| 431 | std::function<void( |
| 432 | size_t m, size_t n, size_t k, const void* lhs_packed, const void* rhs_packed, void* dst, size_t dst_stride_row, |
| 433 | size_t dst_stride_col, float clamp_min, float clamp_max)> |
| 434 | fn_matmul_f32_f32p_f32p = nullptr; |
| 435 | |
| 436 | /// Gets a value indicating whether pre-processing the RHS matrix is needed. |
| 437 | [[nodiscard]] bool is_pack_rhs_needed() const { |
| 438 | return fn_pack_rhs != nullptr; |
| 439 | } |
| 440 | |
| 441 | /// Gets a value indicating whether pre-processing the transposed RHS matrix is needed. |
| 442 | [[nodiscard]] bool is_pack_rhs_nxk_needed() const { |
| 443 | return fn_pack_rhs_nxk != nullptr; |
| 444 | } |
| 445 | |
| 446 | /// Preprocesses the RHS matrix. |
| 447 | /// |
| 448 | /// @param[in] n Size of the matrix in N dimension. |
| 449 | /// @param[in] k Size of the matrix in K dimension. |
| 450 | /// @param[in] rhs RHS data buffer. |
| 451 | /// @param[in] rhs_row_stride RHS row stride. |
| 452 | /// @param[in] bias Bias data buffer. |
| 453 | /// @param[in] scale Quantization scales data buffer. |
| 454 | /// @param[out] packed_rhs Packed RHS data buffer. |
| 455 | void pack_rhs( |
| 456 | size_t n, size_t k, const void* rhs, size_t rhs_row_stride, const void* bias, const void* scale, |
| 457 | void* packed_rhs) const { |
| 458 | KAI_UNUSED(scale); |
| 459 | |
| 460 | if (fn_pack_rhs != nullptr) { |
| 461 | fn_pack_rhs( |
| 462 | 1, n, k, fn_get_nr(), fn_get_kr(), fn_get_sr(), rhs_row_stride, rhs, bias, nullptr, packed_rhs, 0, |
| 463 | nullptr); |
| 464 | } else { |
| 465 | KAI_ERROR("RHS pre-processing is not supported!"); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /// Preprocesses the transposed RHS matrix. |
| 470 | /// |
| 471 | /// @param[in] n Size of the matrix in N dimension. |
| 472 | /// @param[in] k Size of the matrix in K dimension. |
| 473 | /// @param[in] rhs RHS data buffer. |
| 474 | /// @param[in] rhs_row_stride RHS row stride. |
| 475 | /// @param[in] bias Bias data buffer. |
| 476 | /// @param[in] scale Quantization scales data buffer. |
| 477 | /// @param[out] packed_rhs Packed RHS data buffer. |
| 478 | void pack_rhs_nxk( |
| 479 | size_t n, size_t k, const void* rhs, size_t rhs_row_stride, const void* bias, const void* scale, |
| 480 | void* packed_rhs) const { |
| 481 | KAI_UNUSED(scale); |
| 482 | |
| 483 | if (fn_pack_rhs_nxk != nullptr) { |
| 484 | fn_pack_rhs_nxk( |
| 485 | 1, n, k, fn_get_nr(), fn_get_kr(), fn_get_sr(), rhs_row_stride, rhs, bias, nullptr, packed_rhs, 0, |
| 486 | nullptr); |
| 487 | } else { |
| 488 | KAI_ERROR("RHS pre-processing is not supported!"); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | [[nodiscard]] bool has_main_kernel() const { |
| 493 | return fn_matmul_f16_f16_f16p != nullptr || // |
| 494 | fn_matmul_f16_f16p_f16p != nullptr || // |
| 495 | fn_matmul_f32_f32p_f32p != nullptr || // |
| 496 | fn_matmul_f32_f32_f32p != nullptr || // |
| 497 | fn_matmul_f32_bf16p_bf16p != nullptr || // |
| 498 | fn_matmul_f16_bf16p_bf16p != nullptr; |
| 499 | } |
| 500 | |
| 501 | void main_kernel( |
| 502 | size_t m, size_t n, size_t k, const void* lhs, const void* rhs, const void* bias, void* dst, size_t lhs_stride, |
| 503 | size_t rhs_stride, size_t dst_stride, float clamp_min, float clamp_max) const { |
| 504 | KAI_UNUSED(bias); |
| 505 | KAI_UNUSED(rhs_stride); |
| 506 | |
| 507 | if (fn_matmul_f16_f16_f16p) { |
| 508 | fn_matmul_f16_f16_f16p( |
| 509 | m, n, k, lhs, lhs_stride, rhs, dst, dst_stride, sizeof(uint16_t), clamp_min, clamp_max); |
| 510 | } else if (fn_matmul_f32_f32_f32p) { |
| 511 | fn_matmul_f32_f32_f32p(m, n, k, lhs, lhs_stride, rhs, dst, dst_stride, sizeof(float), clamp_min, clamp_max); |
| 512 | } else if (fn_matmul_f16_f16p_f16p) { |
| 513 | fn_matmul_f16_f16p_f16p(m, n, k, lhs, rhs, dst, dst_stride, sizeof(Float16), clamp_min, clamp_max); |
| 514 | } else if (fn_matmul_f32_f32p_f32p) { |
| 515 | fn_matmul_f32_f32p_f32p(m, n, k, lhs, rhs, dst, dst_stride, sizeof(float), clamp_min, clamp_max); |
| 516 | } else if (fn_matmul_f32_bf16p_bf16p) { |
| 517 | fn_matmul_f32_bf16p_bf16p( |
| 518 | m, n, k, reinterpret_cast<const uint16_t*>(lhs), rhs, reinterpret_cast<float*>(dst), dst_stride, |
| 519 | sizeof(float), clamp_min, clamp_max); |
| 520 | } else if (fn_matmul_f16_bf16p_bf16p) { |
| 521 | fn_matmul_f16_bf16p_bf16p(m, n, k, lhs, rhs, dst, dst_stride, sizeof(uint16_t), clamp_min, clamp_max); |
| 522 | } else { |
| 523 | KAI_ERROR("Main kernel is not available!"); |
| 524 | } |
| 525 | } |
| 526 | }; |
| 527 | |
| 528 | // NOLINTEND(misc-non-private-member-variables-in-classes) |
| 529 | |
| 530 | /// Describes bias handling |
| 531 | enum class BiasMode { |
| 532 | INTERNAL, // Zero bias internally generated in kernel |
| 533 | PROVIDED, // Bias provided by kernel caller |
| 534 | }; |
| 535 | |
| 536 | /// Matrix multiplication test information. |
| 537 | using MatMulClampTestParams = std::tuple<MatMulMethod, MatMulShape, MatrixPortion, BiasMode, std::optional<float>>; |
| 538 | using MatMulClampTestPortionedParams = std::tuple<size_t, MatMulShape, MatrixPortion, std::optional<float>>; |
| 539 | using MatMulClampTestPortionedParamsWithBias = |
| 540 | std::tuple<size_t, MatMulShape, MatrixPortion, std::optional<float>, bool>; |
| 541 | using MatMulClampTestPortionedParamsWithBias_WithBL = |
| 542 | std::tuple<size_t, MatMulShape, size_t, MatrixPortion, std::optional<float>, bool>; |
| 543 | |
| 544 | /// Prints the test information. |
| 545 | void PrintTo(const MatMulClampTestParams& param, std::ostream* os); |
| 546 | void PrintTo(const MatMulShape& shape, std::ostream* os); |
| 547 | void PrintTo(const MatrixPortion& portion, std::ostream* os); |
| 548 | void PrintTo(const BiasMode& bias_mode, std::ostream* os); |
| 549 | |
| 550 | /// Generate test information |
| 551 | std::string test_description( |
| 552 | const std::string_view& name, const MatMulShape& shape, const MatrixPortion& portion, bool bias, |
| 553 | std::optional<float> clamp_keep_ratio); |
| 554 | |
| 555 | } // namespace kai::test |
| 556 | |
| 557 | template <> |
| 558 | struct std::hash<kai::test::MatMulShape> { |
| 559 | size_t operator()(const kai::test::MatMulShape& ms) const { |
| 560 | return kai::test::MatMulShape::Hash{}(ms); |
| 561 | } |
| 562 | }; |