| 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 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <ostream> |
| 12 | #include <sstream> |
| 13 | #include <string> |
| 14 | #include <tuple> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "kai/kai_common.h" |
| 19 | #include "kai/ukernels/matmul/matmul_clamp_bf16_qai8dxp_qsi4c32p/kai_matmul_clamp_bf16_qai8dxp1x8_qsi4c32p4x8_1x4_neon_dotprod.h" |
| 20 | #include "kai/ukernels/matmul/matmul_clamp_bf16_qai8dxp_qsi4c32p/kai_matmul_clamp_bf16_qai8dxp4x8_qsi4c32p4x8_16x4_neon_i8mm.h" |
| 21 | #include "kai/ukernels/matmul/matmul_clamp_bf16_qai8dxp_qsi4c32p/kai_matmul_clamp_bf16_qai8dxp_qsi4c32p_interface.h" |
| 22 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h" |
| 23 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4vlx4_1x4vl_sme2_dot.h" |
| 24 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod.h" |
| 25 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x4_qsi4c32p8x4_1x8_neon_dotprod.h" |
| 26 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod.h" |
| 27 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8_neon_dotprod.h" |
| 28 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod.h" |
| 29 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p4x4_16x4_neon_dotprod.h" |
| 30 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x4_qsi4c32p8x4_4x8_neon_dotprod.h" |
| 31 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm.h" |
| 32 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm.h" |
| 33 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm.h" |
| 34 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm.h" |
| 35 | #include "kai/ukernels/matmul/matmul_clamp_f32_qai8dxp_qsi4c32p/kai_matmul_clamp_f32_qai8dxp_qsi4c32p_interface.h" |
| 36 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_bf16_neon.h" |
| 37 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_f32.h" |
| 38 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0.h" |
| 39 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.h" |
| 40 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0.h" |
| 41 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32pnrx4_qsu4c32s1s0_neon.h" |
| 42 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32pnrx8_qsu4c32s1s0_neon.h" |
| 43 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon.h" |
| 44 | #include "test/common/abi_checker.hpp" |
| 45 | #include "test/common/bfloat16.hpp" |
| 46 | #include "test/common/buffer.hpp" |
| 47 | #include "test/common/cache.hpp" |
| 48 | #include "test/common/compare.hpp" |
| 49 | #include "test/common/cpu_info.hpp" |
| 50 | #include "test/common/data_type.hpp" |
| 51 | #include "test/common/int4.hpp" |
| 52 | #include "test/common/matmul_test_common.hpp" |
| 53 | #include "test/common/matrix_portion.hpp" |
| 54 | #include "test/common/memory.hpp" |
| 55 | #include "test/common/round.hpp" |
| 56 | #include "test/common/seed.hpp" |
| 57 | #include "test/common/test_suite.hpp" |
| 58 | #include "test/reference/cast.hpp" |
| 59 | #include "test/reference/clamp.hpp" |
| 60 | #include "test/reference/fill.hpp" |
| 61 | #include "test/reference/matmul.hpp" |
| 62 | #include "test/reference/pad.hpp" |
| 63 | #include "test/reference/quantize.hpp" |
| 64 | #include "test/reference/transpose.hpp" |
| 65 | |
| 66 | namespace kai::test { |
| 67 | |
| 68 | namespace { |
| 69 | |
| 70 | // LHS QAI8DXP |
| 71 | using kai_get_lhs_packed_size_func_t = decltype(&kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32); |
| 72 | using kai_get_lhs_packed_offset_func_t = decltype(&kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32); |
| 73 | using kai_get_lhs_offset_func_t = decltype(&kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32); |
| 74 | using kai_run_lhs_pack_func_t = decltype(&kai_run_lhs_quant_pack_qai8dxp_f32); |
| 75 | |
| 76 | // LHS QAI8DXP pack interface |
| 77 | struct kai_qai8dxp_pack_functions { |
| 78 | kai_get_lhs_packed_size_func_t packed_size; |
| 79 | kai_get_lhs_packed_offset_func_t get_packed_offset; |
| 80 | kai_get_lhs_offset_func_t get_offset; |
| 81 | kai_run_lhs_pack_func_t run_pack; |
| 82 | }; |
| 83 | |
| 84 | // RHS QSI4C32P (nxk, BF16 block scales; sums float, bias float) |
| 85 | using kai_get_rhs_packed_size_func_t = decltype(&kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0); |
| 86 | using kai_get_rhs_packed_offset_func_t = decltype(&kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0); |
| 87 | using kai_get_rhs_offset_func_t = decltype(&kai_get_rhs_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0); |
| 88 | using kai_run_rhs_pack_func_t = decltype(&kai_run_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0); |
| 89 | |
| 90 | // RHS QSI4C32P pack interface |
| 91 | struct kai_qsi4c32p_pack_functions { |
| 92 | kai_get_rhs_packed_size_func_t packed_size; |
| 93 | kai_get_rhs_packed_offset_func_t get_packed_offset; |
| 94 | kai_get_rhs_offset_func_t get_offset; |
| 95 | kai_run_rhs_pack_func_t run_pack; |
| 96 | }; |
| 97 | |
| 98 | const auto& get_f32_gemm_variants() noexcept { |
| 99 | using Variant = UkernelMatmulPackVariant< |
| 100 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>; |
| 101 | |
| 102 | static const std::array<Variant, 12> variants = {{ |
| 103 | UKERNEL_MATMUL_PACK_VARIANT( |
| 104 | clamp_f32_qai8dxp1x4_qsi4c32p4x4_1x4_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 105 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, |
| 106 | /*rhs_s0s1_input=*/false), |
| 107 | UKERNEL_MATMUL_PACK_VARIANT( |
| 108 | clamp_f32_qai8dxp1x4_qsi4c32p8x4_1x8_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 109 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 110 | UKERNEL_MATMUL_PACK_VARIANT( |
| 111 | clamp_f32_qai8dxp1x8_qsi4c32p4x8_1x4x32_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 112 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 113 | UKERNEL_MATMUL_PACK_VARIANT( |
| 114 | clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 115 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 116 | UKERNEL_MATMUL_PACK_VARIANT( |
| 117 | clamp_f32_qai8dxp1x8_qsi4c32p8x8_1x8x32_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 118 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 119 | UKERNEL_MATMUL_PACK_VARIANT( |
| 120 | clamp_f32_qai8dxp4x4_qsi4c32p4x4_16x4_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 121 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 122 | UKERNEL_MATMUL_PACK_VARIANT( |
| 123 | clamp_f32_qai8dxp4x4_qsi4c32p8x4_4x8_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qai8dxp_f32, |
| 124 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 125 | UKERNEL_MATMUL_PACK_VARIANT( |
| 126 | clamp_f32_qai8dxp4x8_qsi4c32p4x8_8x4x32_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qai8dxp_f32, |
| 127 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 128 | UKERNEL_MATMUL_PACK_VARIANT( |
| 129 | clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8x32_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qai8dxp_f32, |
| 130 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 131 | UKERNEL_MATMUL_PACK_VARIANT( |
| 132 | clamp_f32_qai8dxp4x8_qsi4c32p4x8_16x4x32_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qai8dxp_f32, |
| 133 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 134 | UKERNEL_MATMUL_PACK_VARIANT( |
| 135 | clamp_f32_qai8dxp4x8_qsi4c32p8x8_4x8_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qai8dxp_f32, |
| 136 | rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, false), |
| 137 | // SME2 MOPA |
| 138 | UKERNEL_MATMUL_PACK_VARIANT( |
| 139 | clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa, cpu_has_sme2, lhs_quant_pack_qai8dxp_f32, |
| 140 | rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon, false), |
| 141 | }}; |
| 142 | |
| 143 | return variants; |
| 144 | } |
| 145 | |
| 146 | const auto& get_f32_gemv_variants() noexcept { |
| 147 | using Variant = UkernelMatmulPackVariant< |
| 148 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>; |
| 149 | |
| 150 | static const std::array<Variant, 1> variants = {{ |
| 151 | UKERNEL_MATMUL_PACK_VARIANT( |
| 152 | clamp_f32_qai8dxp1x4_qsi4c32p4vlx4_1x4vl_sme2_dot, cpu_has_sme2, lhs_quant_pack_qai8dxp_f32, |
| 153 | rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon, false), |
| 154 | }}; |
| 155 | |
| 156 | return variants; |
| 157 | } |
| 158 | |
| 159 | const auto& get_bf16_gemm_variants() noexcept { |
| 160 | using Variant = UkernelVariant<kai_matmul_clamp_bf16_qai8dxp_qsi4c32p_ukernel>; |
| 161 | static const std::array<Variant, 2> variants = { |
| 162 | Variant{ |
| 163 | UKERNEL_MATMUL_VARIANT(clamp_bf16_qai8dxp1x8_qsi4c32p4x8_1x4_neon_dotprod), |
| 164 | "kai_matmul_clamp_bf16_qai8dxp1x8_qsi4c32p4x8_1x4_neon_dotprod", cpu_has_dotprod_and_bf16}, |
| 165 | Variant{ |
| 166 | UKERNEL_MATMUL_VARIANT(clamp_bf16_qai8dxp4x8_qsi4c32p4x8_16x4_neon_i8mm), |
| 167 | "kai_matmul_clamp_bf16_qai8dxp4x8_qsi4c32p4x8_16x4_neon_i8mm", cpu_has_i8mm_and_bf16}, |
| 168 | }; |
| 169 | return variants; |
| 170 | } |
| 171 | |
| 172 | // Advanced SIMD / i8mm only (exclude SME2) |
| 173 | const auto& get_f32_neon_gemm_variants_only() { |
| 174 | static std::vector<UkernelMatmulPackVariant< |
| 175 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>> |
| 176 | filtered; |
| 177 | if (filtered.empty()) { |
| 178 | const auto& all = get_f32_gemm_variants(); |
| 179 | for (const auto& v : all) { |
| 180 | const char* n = v.ukernel.name.data(); |
| 181 | if (n == nullptr || std::strstr(n, "sme2") == nullptr) { |
| 182 | filtered.push_back(v); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return filtered; |
| 187 | } |
| 188 | |
| 189 | enum class RhsPackType : std::uint8_t { NxK = 0, KxN = 1 }; |
| 190 | |
| 191 | std::tuple<Buffer, size_t> pack_lhs_qai8dxp( |
| 192 | const kai_qai8dxp_pack_functions& pack_interface, const size_t M, const size_t K, const size_t mr, const size_t kr, |
| 193 | const size_t sr, const Buffer& lhs_values_f32, const size_t lhs_stride_bytes, const size_t rect_start_row, |
| 194 | const size_t rect_height) { |
| 195 | const auto lhs_packed_size = pack_interface.packed_size(M, K, mr, kr, sr); |
| 196 | Buffer lhs_packed(lhs_packed_size, 0); |
| 197 | |
| 198 | const auto lhs_offset = pack_interface.get_offset(rect_start_row, lhs_stride_bytes); |
| 199 | const auto lhs_packed_offset = pack_interface.get_packed_offset(rect_start_row, K, mr, kr, sr); |
| 200 | |
| 201 | abi_check( |
| 202 | pack_interface.run_pack, rect_height, K, mr, kr, sr, 0, |
| 203 | reinterpret_cast<const float*>(lhs_values_f32.data() + lhs_offset), lhs_stride_bytes, |
| 204 | lhs_packed.data() + lhs_packed_offset); |
| 205 | |
| 206 | return {std::move(lhs_packed), lhs_packed_offset}; |
| 207 | } |
| 208 | |
| 209 | // Executes the scalar RHS packing micro-kernel. |
| 210 | std::tuple<Buffer, size_t> pack_rhs_qsi4c32pscalebf16( |
| 211 | // clang-format off |
| 212 | const size_t N, |
| 213 | const size_t K, |
| 214 | const size_t nr, |
| 215 | const size_t kr, |
| 216 | const size_t sr, |
| 217 | const size_t bl, |
| 218 | const Buffer& rhs_values_qsi4, |
| 219 | const Buffer& biases, |
| 220 | const size_t bias_offset, |
| 221 | const Buffer& rhs_scales, |
| 222 | const RhsPackType pack_type, |
| 223 | const size_t rect_start_row, |
| 224 | const size_t rect_width, |
| 225 | const bool use_ps1s0) { |
| 226 | // clang-format on |
| 227 | const size_t width = pack_type == RhsPackType::KxN ? N : K; |
| 228 | const size_t height = pack_type == RhsPackType::KxN ? K : N; |
| 229 | constexpr kai_datatype scale_dt = kai_dt_bf16; |
| 230 | |
| 231 | const size_t rhs_stride = round_up_multiple(width, 2); |
| 232 | const size_t rhs_stride_bytes = round_up_division(width, 2); |
| 233 | const size_t scales_stride_bytes = round_up_division(K, bl) * kai_get_datatype_size_in_bytes(scale_dt); |
| 234 | |
| 235 | KAI_ASSUME_ALWAYS(rhs_values_qsi4.size() == round_up_division(height * rhs_stride, 2)); |
| 236 | |
| 237 | const auto rhs_values_qsu4 = cast_qsu4_qsi4(rhs_values_qsi4.data(), rhs_values_qsi4.size() * 2); |
| 238 | const size_t dst_bytes_total = round_up_division(height * rhs_stride, 2); |
| 239 | const size_t dst_bytes_total_safe = dst_bytes_total + rhs_stride_bytes + 8; |
| 240 | const auto rhs_qsu4 = |
| 241 | pad_row<UInt4>(rhs_values_qsu4.data(), height, width, width, rhs_stride_bytes * 2, dst_bytes_total_safe); |
| 242 | |
| 243 | const size_t scale_offset = rect_start_row * scales_stride_bytes; |
| 244 | size_t rhs_offset = 0; |
| 245 | size_t rhs_packed_offset = 0; |
| 246 | size_t imp_packed_rhs_size = 0; |
| 247 | |
| 248 | if (pack_type == RhsPackType::KxN) { |
| 249 | if (use_ps1s0) { |
| 250 | rhs_offset = |
| 251 | kai_get_rhs_offset_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(rect_start_row, rhs_stride_bytes); |
| 252 | rhs_packed_offset = kai_get_rhs_packed_offset_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon( |
| 253 | rect_start_row, K, nr, kr, sr, bl, scale_dt); |
| 254 | imp_packed_rhs_size = |
| 255 | kai_get_rhs_packed_size_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(N, K, nr, kr, sr, bl, scale_dt); |
| 256 | } else { |
| 257 | rhs_offset = kai_get_rhs_offset_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0(rect_start_row, rhs_stride_bytes); |
| 258 | rhs_packed_offset = kai_get_rhs_packed_offset_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0( |
| 259 | rect_start_row, K, nr, kr, sr, bl, scale_dt); |
| 260 | imp_packed_rhs_size = |
| 261 | kai_get_rhs_packed_size_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0(N, K, nr, kr, sr, bl, scale_dt); |
| 262 | } |
| 263 | } else { |
| 264 | rhs_offset = kai_get_rhs_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(rect_start_row, rhs_stride_bytes); |
| 265 | rhs_packed_offset = |
| 266 | kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(rect_start_row, K, nr, kr, sr, bl, scale_dt); |
| 267 | imp_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(N, K, nr, kr, sr, bl, scale_dt); |
| 268 | } |
| 269 | |
| 270 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 271 | if (pack_type == RhsPackType::KxN) { |
| 272 | kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params params{}; |
| 273 | params.lhs_zero_point = 1; |
| 274 | params.rhs_zero_point = 8; |
| 275 | params.scale_dt = scale_dt; |
| 276 | |
| 277 | if (use_ps1s0) { |
| 278 | // clang-format off |
| 279 | abi_check( |
| 280 | kai_run_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon, |
| 281 | 1, // num_groups |
| 282 | rect_width, // n |
| 283 | K, // k |
| 284 | nr, kr, sr, bl, // packing args |
| 285 | reinterpret_cast<const uint8_t*>(rhs_qsu4.data() + rhs_offset), |
| 286 | rhs_stride_bytes, |
| 287 | reinterpret_cast<const float*>(biases.data() + bias_offset), |
| 288 | reinterpret_cast<const void*>(rhs_scales.data() + scale_offset), |
| 289 | scales_stride_bytes, |
| 290 | static_cast<void*>(imp_packed_rhs.data() + rhs_packed_offset), |
| 291 | 0, |
| 292 | ¶ms); |
| 293 | // clang-format on |
| 294 | } else { |
| 295 | kai_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0_params params_kxn{}; |
| 296 | params_kxn.lhs_zero_point = 1; |
| 297 | params_kxn.rhs_zero_point = 8; |
| 298 | params_kxn.scale_dt = scale_dt; |
| 299 | |
| 300 | // clang-format off |
| 301 | abi_check( |
| 302 | kai_run_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0, |
| 303 | 1, |
| 304 | rect_width, |
| 305 | K, |
| 306 | nr, kr, sr, bl, |
| 307 | reinterpret_cast<const uint8_t*>(rhs_qsu4.data() + rhs_offset), |
| 308 | rhs_stride_bytes, |
| 309 | reinterpret_cast<const float*>(biases.data() + bias_offset), |
| 310 | reinterpret_cast<const void*>(rhs_scales.data() + scale_offset), |
| 311 | scales_stride_bytes, |
| 312 | static_cast<void*>(imp_packed_rhs.data() + rhs_packed_offset), |
| 313 | 0, |
| 314 | ¶ms_kxn); |
| 315 | // clang-format on |
| 316 | } |
| 317 | } else { |
| 318 | kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params params{}; |
| 319 | params.lhs_zero_point = 1; |
| 320 | params.rhs_zero_point = 8; |
| 321 | params.scale_dt = scale_dt; |
| 322 | |
| 323 | abi_check( |
| 324 | // clang-format off |
| 325 | kai_run_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0, |
| 326 | 1, |
| 327 | rect_width, |
| 328 | K, |
| 329 | nr, kr, sr, bl, |
| 330 | reinterpret_cast<const uint8_t*>(rhs_qsu4.data() + rhs_offset), |
| 331 | rhs_stride_bytes, |
| 332 | reinterpret_cast<const float*>(biases.data() + bias_offset), |
| 333 | reinterpret_cast<const void*>(rhs_scales.data() + scale_offset), |
| 334 | scales_stride_bytes, |
| 335 | static_cast<void*>(imp_packed_rhs.data() + rhs_packed_offset), |
| 336 | 0, |
| 337 | ¶ms); |
| 338 | // clang-format on |
| 339 | } |
| 340 | |
| 341 | return {std::move(imp_packed_rhs), rhs_packed_offset}; |
| 342 | } |
| 343 | |
| 344 | /// Executes RHS NxK packing helper |
| 345 | std::tuple<Buffer, size_t> pack_rhs_qsi4c32p_nxk( |
| 346 | const kai_qsi4c32p_pack_functions& pack_iface, const size_t N, const size_t K, const size_t nr, const size_t kr, |
| 347 | const size_t sr, const size_t bl, const Buffer& rhs_values_qsi4, const float* bias, const Buffer& rhs_scales, |
| 348 | const size_t rect_start_row, const size_t rect_width, const bool rhs_s0s1_input) { |
| 349 | // Convert signed int4 -> unsigned int4, preserving any row padding in the source buffer. |
| 350 | const auto rhs_qsu4s1s0 = cast_qsu4_qsi4(rhs_values_qsi4.data(), rhs_values_qsi4.size() * 2); |
| 351 | |
| 352 | const auto rhs_packed_size = pack_iface.packed_size(N, K, nr, kr, sr, bl, kai_dt_bf16); |
| 353 | Buffer rhs_packed(rhs_packed_size); |
| 354 | const auto rhs_packed_offset = pack_iface.get_packed_offset(rect_start_row, K, nr, kr, sr, bl, kai_dt_bf16); |
| 355 | |
| 356 | const size_t rhs_stride_bytes = round_up_division(K, 2); // bytes per row |
| 357 | const size_t scales_stride_bytes = round_up_division(K, bl) * kai_get_datatype_size_in_bytes(kai_dt_bf16); |
| 358 | const size_t scale_offset = rect_start_row * scales_stride_bytes; |
| 359 | const size_t rhs_offset = pack_iface.get_offset(rect_start_row, rhs_stride_bytes); |
| 360 | |
| 361 | kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params params{}; |
| 362 | params.lhs_zero_point = 1; |
| 363 | params.rhs_zero_point = 8; |
| 364 | params.scale_dt = kai_dt_bf16; |
| 365 | |
| 366 | // Apply optional s0s1 -> s1s0 nibble swap. |
| 367 | const Buffer* rhs_qsu4_ptr = &rhs_qsu4s1s0; |
| 368 | Buffer rhs_qsu4_converted; |
| 369 | if (rhs_s0s1_input) { |
| 370 | rhs_qsu4_converted = convert_s0s1_s1s0(rhs_qsu4s1s0); |
| 371 | rhs_qsu4_ptr = &rhs_qsu4_converted; |
| 372 | } |
| 373 | |
| 374 | abi_check( |
| 375 | pack_iface.run_pack, 1, rect_width, K, nr, kr, sr, bl, |
| 376 | reinterpret_cast<const uint8_t*>(rhs_qsu4_ptr->data() + rhs_offset), rhs_stride_bytes, bias, |
| 377 | rhs_scales.data() + scale_offset, scales_stride_bytes, rhs_packed.data() + rhs_packed_offset, 0, ¶ms); |
| 378 | |
| 379 | return {std::move(rhs_packed), rhs_packed_offset}; |
| 380 | } |
| 381 | |
| 382 | // Executes F32-only RHS KxN packing helper (wrapper around BF16-scaled helper for clarity) |
| 383 | std::tuple<Buffer, size_t> pack_rhs_qsi4c32p_kxn( |
| 384 | const size_t N, const size_t K, const size_t nr, const size_t kr, const size_t sr, const size_t bl, |
| 385 | const Buffer& rhs_values_qsi4, const Buffer& biases, const size_t bias_offset, const Buffer& rhs_scales, |
| 386 | const size_t rect_start_row, const size_t rect_width, const bool use_ps1s0) { |
| 387 | return pack_rhs_qsi4c32pscalebf16( |
| 388 | N, K, nr, kr, sr, bl, rhs_values_qsi4, biases, bias_offset, rhs_scales, RhsPackType::KxN, rect_start_row, |
| 389 | rect_width, use_ps1s0); |
| 390 | } |
| 391 | |
| 392 | /// Executes the vectorized RHS packing micro-kernels for block length of 4 bytes or 8 bytes |
| 393 | std::tuple<Buffer, size_t> pack_rhs_qsi4c32pscalebf16_neon( |
| 394 | const size_t N, const size_t K, const size_t nr, const size_t kr, const size_t sr, const size_t bl, |
| 395 | const Buffer& rhs_values_qsi4, const Buffer& biases, const size_t bias_offset, const Buffer& rhs_scales, |
| 396 | const RhsPackType pack_type, const size_t rect_start_row, const size_t rect_width) { |
| 397 | KAI_ASSUME_ALWAYS(kr / sr == 8 || kr / sr == 4); |
| 398 | const size_t width = pack_type == RhsPackType::KxN ? N : K; |
| 399 | const size_t height = pack_type == RhsPackType::KxN ? K : N; |
| 400 | constexpr kai_datatype scale_dt = kai_dt_bf16; |
| 401 | |
| 402 | const size_t rhs_stride = round_up_multiple(width, 2); |
| 403 | const size_t rhs_stride_bytes = round_up_division(width, 2); |
| 404 | const size_t scales_stride_bytes = round_up_division(K, bl) * kai_get_datatype_size_in_bytes(scale_dt); |
| 405 | |
| 406 | KAI_ASSUME_ALWAYS(rhs_values_qsi4.size() == round_up_division(height * rhs_stride, 2)); |
| 407 | |
| 408 | const auto rhs_values_qsu4 = cast_qsu4_qsi4(rhs_values_qsi4.data(), rhs_values_qsi4.size() * 2); |
| 409 | const size_t dst_bytes_total = round_up_division(height * rhs_stride, 2); |
| 410 | const size_t dst_bytes_total_safe = dst_bytes_total + rhs_stride_bytes + 8; |
| 411 | const auto rhs_qsu4 = |
| 412 | pad_row<UInt4>(rhs_values_qsu4.data(), height, width, width, rhs_stride_bytes * 2, dst_bytes_total_safe); |
| 413 | |
| 414 | const size_t scale_offset = rect_start_row * scales_stride_bytes; |
| 415 | |
| 416 | size_t imp_packed_rhs_size_neon = 0; |
| 417 | size_t rhs_packed_offset_neon = 0; |
| 418 | size_t rhs_offset_neon = 0; |
| 419 | |
| 420 | if (kr / sr == 8) { |
| 421 | imp_packed_rhs_size_neon = |
| 422 | kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32pnrx8_qsu4c32s1s0_neon(N, K, nr, kr, sr, bl, scale_dt); |
| 423 | rhs_packed_offset_neon = kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32pnrx8_qsu4c32s1s0_neon( |
| 424 | rect_start_row, K, nr, kr, sr, bl, scale_dt); |
| 425 | rhs_offset_neon = |
| 426 | kai_get_rhs_offset_rhs_pack_nxk_qsi4c32pnrx8_qsu4c32s1s0_neon(rect_start_row, rhs_stride_bytes); |
| 427 | } else { |
| 428 | imp_packed_rhs_size_neon = |
| 429 | kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32pnrx4_qsu4c32s1s0_neon(N, K, nr, kr, sr, bl, scale_dt); |
| 430 | rhs_packed_offset_neon = kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32pnrx4_qsu4c32s1s0_neon( |
| 431 | rect_start_row, K, nr, kr, sr, bl, scale_dt); |
| 432 | rhs_offset_neon = |
| 433 | kai_get_rhs_offset_rhs_pack_nxk_qsi4c32pnrx4_qsu4c32s1s0_neon(rect_start_row, rhs_stride_bytes); |
| 434 | } |
| 435 | |
| 436 | kai_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0_params params{}; |
| 437 | params.lhs_zero_point = 1; |
| 438 | params.rhs_zero_point = 8; |
| 439 | params.scale_dt = scale_dt; |
| 440 | |
| 441 | Buffer imp_packed_rhs_neon(imp_packed_rhs_size_neon); |
| 442 | if (kr / sr == 8) { |
| 443 | kai_run_rhs_pack_nxk_qsi4c32pnrx8_qsu4c32s1s0_neon( |
| 444 | 1, rect_width /* n */, K, nr, kr, sr, bl, |
| 445 | reinterpret_cast<const uint8_t*>(rhs_qsu4.data() + rhs_offset_neon), rhs_stride_bytes, |
| 446 | reinterpret_cast<const float*>(biases.data() + bias_offset), rhs_scales.data() + scale_offset, |
| 447 | scales_stride_bytes, imp_packed_rhs_neon.data() + rhs_packed_offset_neon, 0, ¶ms); |
| 448 | } else { |
| 449 | kai_run_rhs_pack_nxk_qsi4c32pnrx4_qsu4c32s1s0_neon( |
| 450 | 1, rect_width /* n */, K, nr, kr, sr, bl, |
| 451 | reinterpret_cast<const uint8_t*>(rhs_qsu4.data() + rhs_offset_neon), rhs_stride_bytes, |
| 452 | reinterpret_cast<const float*>(biases.data() + bias_offset), rhs_scales.data() + scale_offset, |
| 453 | scales_stride_bytes, imp_packed_rhs_neon.data() + rhs_packed_offset_neon, 0, ¶ms); |
| 454 | } |
| 455 | return {std::move(imp_packed_rhs_neon), rhs_packed_offset_neon}; |
| 456 | } |
| 457 | |
| 458 | std::string test_description( |
| 459 | const std::string& name, const RhsPackType rhs_pack_type, const MatMulShape& shape, const size_t bl, |
| 460 | const MatrixPortion& portion, const std::optional<float> clamp_keep_ratio) { |
| 461 | // Remove redundant prefix to make output easier to read |
| 462 | std::string clean_name = name; |
| 463 | const std::string prefix = "kai_matmul_clamp_"; |
| 464 | if (clean_name.rfind(prefix, 0) == 0) { // starts with prefix |
| 465 | clean_name.erase(0, prefix.length()); |
| 466 | } |
| 467 | |
| 468 | std::ostringstream sstream; |
| 469 | sstream << test_description(clean_name, shape, portion, /*bias=*/false, clamp_keep_ratio) << "__BL_" << bl << "__" |
| 470 | << ((rhs_pack_type == RhsPackType::NxK) ? "NxK" : "KxN"); |
| 471 | |
| 472 | return sstream.str(); |
| 473 | } |
| 474 | |
| 475 | struct TestData { |
| 476 | size_t M{}, N{}, K{}, bl{}; |
| 477 | |
| 478 | Rect rect{0, 0, 0, 0}; |
| 479 | |
| 480 | Buffer lhs; |
| 481 | Buffer rhs; |
| 482 | Buffer bias; |
| 483 | |
| 484 | Buffer rhs_quant; |
| 485 | Buffer rhs_scales; |
| 486 | |
| 487 | Buffer lhs_packed; |
| 488 | size_t lhs_packed_offset{}; |
| 489 | |
| 490 | Buffer ref_dst_clamped; |
| 491 | Range<float> clamp; |
| 492 | }; |
| 493 | |
| 494 | using BF16QMatMulRefKey = std::tuple< |
| 495 | MatMulShape, // shape |
| 496 | size_t, // bl |
| 497 | size_t, // mr |
| 498 | size_t, // nr |
| 499 | size_t, // kr |
| 500 | size_t, // sr |
| 501 | size_t, size_t, size_t, size_t, // rect.start_row, rect.start_col, rect.height, rect.width |
| 502 | RhsPackType, // rhs_pack_type |
| 503 | std::optional<float> // clamp_keep_ratio |
| 504 | >; |
| 505 | |
| 506 | struct BF16TestData { |
| 507 | size_t M{}, N{}, K{}, bl{}; |
| 508 | Rect rect{0, 0, 0, 0}; |
| 509 | |
| 510 | Buffer lhs_bf16; // Original BF16 LHS (kept for completeness) |
| 511 | Buffer bias; // Biases (FP32) |
| 512 | Buffer rhs_quant; // QSI4 quantized RHS (possibly transposed to match pack type) |
| 513 | Buffer rhs_scales; // BF16 per-block scales |
| 514 | |
| 515 | Buffer lhs_packed; // Packed LHS buffer (BF16 dynamic quant + pack) |
| 516 | size_t lhs_packed_offset{}; // Offset for rect.start_row |
| 517 | |
| 518 | Range<float> clamp{}; // Clamp range used for matmul |
| 519 | Buffer ref_dst_bf16; // Reference DST in BF16 (clamped) |
| 520 | }; |
| 521 | |
| 522 | } // anonymous namespace |
| 523 | |
| 524 | using QMatmulClampF32ParamT = |
| 525 | std::tuple<size_t, bool, MatMulShape, size_t, MatrixPortion, RhsPackType, std::optional<float>>; |
| 526 | |
| 527 | class QMatMulClampF32Test : public ::testing::TestWithParam<QMatmulClampF32ParamT> { |
| 528 | struct TestParams { |
| 529 | const UkernelMatmulPackVariant< |
| 530 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>* |
| 531 | variant; |
| 532 | size_t variant_index; |
| 533 | MatMulShape matmul_shape; |
| 534 | size_t bl; |
| 535 | MatrixPortion portion; |
| 536 | RhsPackType rhs_pack_type; |
| 537 | Rect rect; |
| 538 | std::optional<float> clamp_keep_ratio; |
| 539 | bool is_sme2; |
| 540 | |
| 541 | TestParams() : |
| 542 | variant(nullptr), |
| 543 | variant_index(0), |
| 544 | matmul_shape{0, 0, 0}, |
| 545 | bl(32), |
| 546 | portion(0, 0, 1, 1), |
| 547 | rhs_pack_type(RhsPackType::NxK), |
| 548 | rect(0, 0, 0, 0), |
| 549 | clamp_keep_ratio(std::optional<float>(0.8F)), |
| 550 | is_sme2(false) { |
| 551 | } |
| 552 | |
| 553 | TestParams( |
| 554 | const UkernelMatmulPackVariant< |
| 555 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>* |
| 556 | variant, |
| 557 | const size_t v_idx, const MatMulShape& shape, const size_t bl, const MatrixPortion p, const RhsPackType r, |
| 558 | const Rect& rect, const std::optional<float> clamp_keep_ratio) : |
| 559 | variant(variant), |
| 560 | variant_index(v_idx), |
| 561 | matmul_shape(shape), |
| 562 | bl(bl), |
| 563 | portion(p), |
| 564 | rhs_pack_type(r), |
| 565 | rect(rect), |
| 566 | clamp_keep_ratio(clamp_keep_ratio), |
| 567 | is_sme2(false) { |
| 568 | } |
| 569 | }; |
| 570 | |
| 571 | TestParams params; |
| 572 | |
| 573 | protected: |
| 574 | static const TestData& test_data(); |
| 575 | void SetupCommonForParam() { |
| 576 | TestWithParam::SetUp(); |
| 577 | if (std::get<1>(GetParam())) { // is_gemm |
| 578 | SetupCommon(get_f32_gemm_variants()); |
| 579 | } else { |
| 580 | SetupCommon(get_f32_gemv_variants()); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | [[nodiscard]] const TestParams& GetParams() const { |
| 585 | return params; |
| 586 | } |
| 587 | TestParams& GetParams() { |
| 588 | return params; |
| 589 | } |
| 590 | |
| 591 | void SetUp() override { |
| 592 | // Gate CPU features before computing kernel interface params (which may touch unsupported instructions). |
| 593 | const auto& param = GetParam(); |
| 594 | const size_t variant_index = std::get<0>(param); |
| 595 | const bool is_gemm = std::get<1>(param); |
| 596 | const auto& variant = |
| 597 | is_gemm ? get_f32_gemm_variants().at(variant_index) : get_f32_gemv_variants().at(variant_index); |
| 598 | |
| 599 | if (variant.ukernel.fn_is_supported && !variant.ukernel.fn_is_supported()) { |
| 600 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | // Safe to compute aligned params/rect now. |
| 605 | SetupCommonForParam(); |
| 606 | const auto& p = GetParams(); |
| 607 | |
| 608 | // GEMV vs GEMM constraints (after params are set) |
| 609 | if (!is_gemm) { |
| 610 | if (p.matmul_shape.m != 1) { |
| 611 | GTEST_SKIP() << "GEMV requires M=1"; |
| 612 | return; |
| 613 | } |
| 614 | if (p.rect.height() != 1 || p.rect.start_row() != 0) { |
| 615 | GTEST_SKIP() << "GEMV portion invalid, rect height != 1 or start_row != 0"; |
| 616 | return; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | template <size_t ArrN> |
| 622 | void SetupCommon( |
| 623 | const std::array< |
| 624 | UkernelMatmulPackVariant< |
| 625 | kai_matmul_clamp_f32_qai8dxp_qsi4c32p_ukernel, kai_qai8dxp_pack_functions, kai_qsi4c32p_pack_functions>, |
| 626 | ArrN>& variants) { |
| 627 | const auto& [variant_index, is_gemm, shape, bl, portion, rhs_dir, clamp_keep_ratio] = GetParam(); |
| 628 | const auto& variant = variants.at(variant_index); |
| 629 | |
| 630 | params.variant = &variant; |
| 631 | params.variant_index = variant_index; |
| 632 | |
| 633 | // Compute aligned portion rect once |
| 634 | const size_t m_step = variant.ukernel.interface.get_m_step(); |
| 635 | const size_t n_step = variant.ukernel.interface.get_n_step(); |
| 636 | const Rect rect = portion.compute_portion(shape.m, shape.n, m_step, n_step); |
| 637 | |
| 638 | params.matmul_shape = shape; |
| 639 | params.bl = bl; |
| 640 | params.portion = portion; |
| 641 | params.rhs_pack_type = rhs_dir; |
| 642 | params.rect = rect; |
| 643 | params.clamp_keep_ratio = clamp_keep_ratio; |
| 644 | params.is_sme2 = |
| 645 | (variant.ukernel.name.data() != nullptr && std::strstr(variant.ukernel.name.data(), "sme2") != nullptr); |
| 646 | } |
| 647 | }; |
| 648 | |
| 649 | using F32QMatMulRefKey = std::tuple< |
| 650 | MatMulShape, // shape |
| 651 | size_t, // bl |
| 652 | size_t, // mr |
| 653 | size_t, // kr |
| 654 | size_t, // sr |
| 655 | size_t, // rect_start_row |
| 656 | size_t, // rect_start_col |
| 657 | size_t, // rect_height |
| 658 | size_t, // rect_width |
| 659 | RhsPackType, // rhs_pack_type |
| 660 | std::optional<int>, // clamp_pct |
| 661 | const void* // lhs_pack_key |
| 662 | >; |
| 663 | |
| 664 | template <> |
| 665 | TestData ReferenceGenerator<F32QMatMulRefKey, TestData>::generate_reference(const F32QMatMulRefKey& test_id) { |
| 666 | TestData ref{}; |
| 667 | |
| 668 | const auto& [shape, bl, mr, kr, sr, rect_start_row, rect_start_col, rect_height, rect_width, rhs_pack_type, clamp_pct, lhs_pack_key] = |
| 669 | test_id; |
| 670 | KAI_UNUSED(lhs_pack_key); |
| 671 | const std::optional<float> clamp_keep_ratio = |
| 672 | clamp_pct.has_value() ? std::optional<float>(static_cast<float>(clamp_pct.value()) / 100.0F) : std::nullopt; |
| 673 | const Rect rect(rect_start_row, rect_start_col, rect_height, rect_width); |
| 674 | |
| 675 | ref.M = shape.m; |
| 676 | ref.N = shape.n; |
| 677 | ref.K = shape.k; |
| 678 | ref.bl = bl; |
| 679 | ref.rect = rect; |
| 680 | |
| 681 | // Creates a unique seed for the test data. |
| 682 | const auto key = std::string("F32QMatMulRefKey:") + std::to_string(ref.M) + "x" + std::to_string(ref.N) + "x" + |
| 683 | std::to_string(ref.K) + "_" + std::to_string(bl) + "_" + ((rhs_pack_type == RhsPackType::NxK) ? "NxK" : "KxN") + |
| 684 | "_" + (clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp"); |
| 685 | auto& feed = seed_stream(key); |
| 686 | |
| 687 | ref.lhs = fill_random<float>(ref.M * ref.K, feed()); |
| 688 | ref.rhs = fill_random<float>(ref.N * ref.K, feed()); |
| 689 | ref.bias = fill_random<float>(ref.N, feed()); |
| 690 | |
| 691 | // Dynamic LHS quantization (reference only). |
| 692 | QuantizationInfo lhs_qinfo{}; |
| 693 | lhs_qinfo.quant_width = ref.K; |
| 694 | lhs_qinfo.dst_type = DataType::QAI8; |
| 695 | lhs_qinfo.scale_type = DataType::FP32; |
| 696 | lhs_qinfo.zero_point_type = DataType::I32; |
| 697 | auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref.lhs.data(), DataType::FP32, ref.M, ref.K, lhs_qinfo); |
| 698 | |
| 699 | // Dynamic RHS quantization to QSI4 with BF16 block scales. |
| 700 | QuantizationInfo rhs_qinfo{}; |
| 701 | rhs_qinfo.quant_width = bl; |
| 702 | rhs_qinfo.dst_type = DataType::QSI4; |
| 703 | rhs_qinfo.scale_type = DataType::BF16; |
| 704 | auto [ref_rhs_quant, rhs_qoutputs] = quantize_dynamic(ref.rhs.data(), DataType::FP32, ref.N, ref.K, rhs_qinfo); |
| 705 | |
| 706 | ref.rhs_quant = std::move(ref_rhs_quant); |
| 707 | ref.rhs_scales = std::move(rhs_qoutputs.scales); |
| 708 | |
| 709 | const bool transposed = (rhs_pack_type == RhsPackType::NxK); |
| 710 | const size_t width = transposed ? ref.K : ref.N; |
| 711 | const size_t height = transposed ? ref.N : ref.K; |
| 712 | |
| 713 | const size_t qsi4_stride = round_up_multiple(width, 2); |
| 714 | const size_t qsi4_size_bytes = round_up_division(height * qsi4_stride, 2); |
| 715 | |
| 716 | if (!transposed) { |
| 717 | ref.rhs_quant = |
| 718 | transpose_with_padding<Int4>(ref.rhs_quant.data(), ref.N, ref.K, ref.K, qsi4_stride, qsi4_size_bytes); |
| 719 | } |
| 720 | |
| 721 | Buffer ref_dst_noclamp; |
| 722 | if (transposed) { |
| 723 | ref_dst_noclamp = |
| 724 | matmul_nt_t_quantized<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 725 | ref.M, ref.N, ref.K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), |
| 726 | 1, ref.K, ref.rhs_quant.data(), ref.rhs_scales.data(), nullptr, 1, bl, ref.bias.data(), nullptr, |
| 727 | nullptr, 1); |
| 728 | } else { |
| 729 | ref_dst_noclamp = matmul_nt_nt_quantized< |
| 730 | int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 731 | ref.M, ref.N, ref.K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), 1, |
| 732 | ref.K, ref.rhs_quant.data(), ref.rhs_scales.data(), nullptr, 1, bl, ref.bias.data(), nullptr, nullptr, 1); |
| 733 | } |
| 734 | |
| 735 | const auto [cmin, cmax] = find_clamp_range<float>(ref_dst_noclamp.data(), ref.M * ref.N, clamp_keep_ratio); |
| 736 | ref.clamp = {cmin, cmax}; |
| 737 | ref.ref_dst_clamped = clamp<float>(ref_dst_noclamp.data(), ref.M * ref.N, cmin, cmax); |
| 738 | |
| 739 | // Pack LHS once for this key. |
| 740 | const size_t lhs_stride_bytes = ref.K * sizeof(float); |
| 741 | constexpr kai_qai8dxp_pack_functions lhs_iface{ |
| 742 | kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f32, |
| 743 | kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32, |
| 744 | kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f32, |
| 745 | kai_run_lhs_quant_pack_qai8dxp_f32, |
| 746 | }; |
| 747 | |
| 748 | auto [lhs_packed, lhs_packed_offset] = pack_lhs_qai8dxp( |
| 749 | lhs_iface, ref.M, ref.K, mr, kr, sr, ref.lhs, lhs_stride_bytes, rect.start_row(), rect.height()); |
| 750 | |
| 751 | ref.lhs_packed = std::move(lhs_packed); |
| 752 | ref.lhs_packed_offset = lhs_packed_offset; |
| 753 | |
| 754 | return ref; |
| 755 | } |
| 756 | |
| 757 | [[maybe_unused]] static void PrintTo(const QMatmulClampF32ParamT& param, std::ostream* os) { |
| 758 | const auto& [variant_idx, is_gemm, shape, bl, portion, rhs_pack_type, clamp_keep_ratio] = param; |
| 759 | const auto name = std::string( |
| 760 | (is_gemm ? get_f32_gemm_variants().at(variant_idx).ukernel.name |
| 761 | : get_f32_gemv_variants().at(variant_idx).ukernel.name)); |
| 762 | *os << test_description(name, rhs_pack_type, shape, bl, portion, clamp_keep_ratio); |
| 763 | } |
| 764 | |
| 765 | const TestData& QMatMulClampF32Test::test_data() { |
| 766 | const auto& param = GetParam(); |
| 767 | const size_t variant_index = std::get<0>(param); |
| 768 | const bool is_gemm = std::get<1>(param); |
| 769 | const MatMulShape& shape = std::get<2>(param); |
| 770 | const size_t bl = std::get<3>(param); |
| 771 | const MatrixPortion& portion = std::get<4>(param); |
| 772 | const RhsPackType rhs_pack_type = std::get<5>(param); |
| 773 | const std::optional<float> clamp_keep_ratio = std::get<6>(param); |
| 774 | |
| 775 | const auto& variant = |
| 776 | is_gemm ? get_f32_gemm_variants().at(variant_index) : get_f32_gemv_variants().at(variant_index); |
| 777 | const auto& iface = variant.ukernel.interface; |
| 778 | |
| 779 | const size_t mr = iface.get_mr(); |
| 780 | const size_t kr = iface.get_kr(); |
| 781 | const size_t sr = iface.get_sr(); |
| 782 | const size_t m_step = iface.get_m_step(); |
| 783 | const size_t n_step = iface.get_n_step(); |
| 784 | const Rect rect = portion.compute_portion(shape.m, shape.n, m_step, n_step); |
| 785 | |
| 786 | const std::optional<int> clamp_pct = clamp_keep_ratio.has_value() |
| 787 | ? std::optional<int>(static_cast<int>(clamp_keep_ratio.value() * 100 + 0.5F)) |
| 788 | : std::nullopt; |
| 789 | |
| 790 | const F32QMatMulRefKey key{ |
| 791 | shape, |
| 792 | bl, |
| 793 | mr, |
| 794 | kr, |
| 795 | sr, |
| 796 | rect.start_row(), |
| 797 | rect.start_col(), |
| 798 | rect.height(), |
| 799 | rect.width(), |
| 800 | rhs_pack_type, |
| 801 | clamp_pct, |
| 802 | reinterpret_cast<const void*>(variant.lhs_pack_interface.run_pack)}; |
| 803 | |
| 804 | return getV<F32QMatMulRefKey, TestData>(key); |
| 805 | } |
| 806 | |
| 807 | using MatMulTestParams_withBL_withRHSPackType = |
| 808 | std::tuple<size_t, MatMulShape, size_t, MatrixPortion, RhsPackType, std::optional<float>>; |
| 809 | |
| 810 | [[maybe_unused]] static void PrintTo(const MatMulTestParams_withBL_withRHSPackType& param, std::ostream* os) { |
| 811 | const size_t variant_idx = std::get<0>(param); |
| 812 | const MatMulShape shape = std::get<1>(param); |
| 813 | const size_t bl = std::get<2>(param); |
| 814 | const MatrixPortion portion = std::get<3>(param); |
| 815 | const RhsPackType rhs_pack_type = std::get<4>(param); |
| 816 | const std::string name{get_bf16_gemm_variants().at(variant_idx).name}; |
| 817 | const std::optional<float> clamp_keep_ratio = std::get<5>(param); |
| 818 | *os << test_description(name, rhs_pack_type, shape, bl, portion, clamp_keep_ratio); |
| 819 | } |
| 820 | |
| 821 | template <> |
| 822 | BF16TestData ReferenceGenerator<BF16QMatMulRefKey, BF16TestData>::generate_reference(const BF16QMatMulRefKey& test_id) { |
| 823 | BF16TestData ref{}; |
| 824 | |
| 825 | const MatMulShape shape = std::get<0>(test_id); |
| 826 | const size_t bl = std::get<1>(test_id); |
| 827 | const size_t mr = std::get<2>(test_id); |
| 828 | const size_t nr = std::get<3>(test_id); |
| 829 | KAI_UNUSED(nr); |
| 830 | const size_t kr = std::get<4>(test_id); |
| 831 | const size_t sr = std::get<5>(test_id); |
| 832 | const size_t rect_start_row = std::get<6>(test_id); |
| 833 | const size_t rect_start_col = std::get<7>(test_id); |
| 834 | const size_t rect_height = std::get<8>(test_id); |
| 835 | const size_t rect_width = std::get<9>(test_id); |
| 836 | const RhsPackType rhs_pack_type = std::get<10>(test_id); |
| 837 | const std::optional<float> clamp_keep_ratio = std::get<11>(test_id); |
| 838 | |
| 839 | ref.M = shape.m; |
| 840 | ref.N = shape.n; |
| 841 | ref.K = shape.k; |
| 842 | ref.bl = bl; |
| 843 | ref.rect = Rect(rect_start_row, rect_start_col, rect_height, rect_width); |
| 844 | |
| 845 | // Creates a unique seed for the test data. |
| 846 | const auto key = std::string("BF16QMatMulRefKey:") + std::to_string(ref.M) + "x" + std::to_string(ref.N) + "x" + |
| 847 | std::to_string(ref.K) + "_" + std::to_string(bl) + "_" + ((rhs_pack_type == RhsPackType::NxK) ? "NxK" : "KxN") + |
| 848 | "_" + (clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp"); |
| 849 | auto& feed = seed_stream(key); |
| 850 | |
| 851 | // Inputs |
| 852 | ref.lhs_bf16 = fill_random<BFloat16<false>>(ref.M * ref.K, feed()); |
| 853 | Buffer const ref_rhs = fill_random<float>(ref.N * ref.K, feed()); |
| 854 | ref.bias = fill_random<float>(ref.N, feed()); |
| 855 | |
| 856 | // Cast BF16 LHS to FP32 for reference quantization |
| 857 | const Buffer ref_lhs = |
| 858 | cast<float, BFloat16<false>>(ref.lhs_bf16.data(), ref.lhs_bf16.size() * 8 / size_in_bits<BFloat16<false>>); |
| 859 | |
| 860 | // Reference quantizations for LHS and RHS |
| 861 | QuantizationInfo lhs_qinfo{}; |
| 862 | lhs_qinfo.quant_width = ref.K; |
| 863 | lhs_qinfo.dst_type = DataType::QAI8; |
| 864 | lhs_qinfo.scale_type = DataType::FP32; |
| 865 | lhs_qinfo.zero_point_type = DataType::I32; |
| 866 | auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref_lhs.data(), DataType::FP32, ref.M, ref.K, lhs_qinfo); |
| 867 | |
| 868 | QuantizationInfo rhs_qinfo{}; |
| 869 | rhs_qinfo.quant_width = bl; |
| 870 | rhs_qinfo.dst_type = DataType::QSI4; |
| 871 | rhs_qinfo.scale_type = DataType::BF16; |
| 872 | auto [ref_rhs_quant, rhs_qoutputs] = quantize_dynamic(ref_rhs.data(), DataType::FP32, ref.N, ref.K, rhs_qinfo); |
| 873 | |
| 874 | // Prepare RHS layout per pack type |
| 875 | const bool transposed = (rhs_pack_type == RhsPackType::NxK); |
| 876 | const size_t width = transposed ? ref.K : ref.N; |
| 877 | const size_t height = transposed ? ref.N : ref.K; |
| 878 | |
| 879 | const size_t qsi4_stride = round_up_multiple(width, 2); |
| 880 | const size_t qsi4_size_bytes = round_up_division(height * qsi4_stride, 2); |
| 881 | |
| 882 | ref.rhs_quant = std::move(ref_rhs_quant); |
| 883 | if (!transposed) { |
| 884 | ref.rhs_quant = |
| 885 | transpose_with_padding<Int4>(ref.rhs_quant.data(), ref.N, ref.K, ref.K, qsi4_stride, qsi4_size_bytes); |
| 886 | } |
| 887 | ref.rhs_scales = std::move(rhs_qoutputs.scales); |
| 888 | |
| 889 | // Compute reference destination (float), clamp, and cast to BF16 |
| 890 | Buffer ref_dst_noclamp; |
| 891 | if (transposed) { |
| 892 | ref_dst_noclamp = |
| 893 | matmul_nt_t_quantized<int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 894 | ref.M, ref.N, ref.K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), |
| 895 | 1, ref.K, ref.rhs_quant.data(), ref.rhs_scales.data(), nullptr, 1, bl, ref.bias.data(), nullptr, |
| 896 | nullptr, 1); |
| 897 | } else { |
| 898 | ref_dst_noclamp = matmul_nt_nt_quantized< |
| 899 | int8_t, float, int32_t, Int4, BFloat16<false>, int32_t, float, float, int32_t, float>( |
| 900 | ref.M, ref.N, ref.K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), 1, |
| 901 | ref.K, ref.rhs_quant.data(), ref.rhs_scales.data(), nullptr, 1, bl, ref.bias.data(), nullptr, nullptr, 1); |
| 902 | } |
| 903 | |
| 904 | const auto [clamp_min, clamp_max] = |
| 905 | find_clamp_range<float>(ref_dst_noclamp.data(), ref.M * ref.N, clamp_keep_ratio); |
| 906 | ref.clamp = {clamp_min, clamp_max}; |
| 907 | const Buffer ref_dst_float = clamp<float>(ref_dst_noclamp.data(), ref.M * ref.N, clamp_min, clamp_max); |
| 908 | ref.ref_dst_bf16 = |
| 909 | cast<BFloat16<false>, float>(ref_dst_float.data(), ref_dst_float.size() * 8 / size_in_bits<float>); |
| 910 | |
| 911 | // Pack LHS once (BF16 packer) |
| 912 | const size_t imp_packed_lhs_size = |
| 913 | kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_bf16_neon(ref.M, ref.K, mr, kr, sr); |
| 914 | ref.lhs_packed = Buffer(imp_packed_lhs_size); |
| 915 | |
| 916 | const size_t lhs_stride = ref.K * sizeof(uint16_t); |
| 917 | const size_t lhs_offset = kai_get_lhs_offset_lhs_quant_pack_qai8dxp_bf16_neon(rect_start_row, lhs_stride); |
| 918 | ref.lhs_packed_offset = |
| 919 | kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_bf16_neon(rect_start_row, ref.K, mr, kr, sr); |
| 920 | |
| 921 | kai_run_lhs_quant_pack_qai8dxp_bf16_neon( |
| 922 | rect_height, ref.K, mr, kr, sr, 0, ref.lhs_bf16.data() + lhs_offset, lhs_stride, |
| 923 | reinterpret_cast<uint8_t*>(ref.lhs_packed.data()) + ref.lhs_packed_offset); |
| 924 | |
| 925 | return ref; |
| 926 | } |
| 927 | |
| 928 | /// Verifies RHS packed offsets (KxN vs NxK) match each other and the matmul interface at nr. |
| 929 | TEST_P(QMatMulClampF32Test, OffsetRHS) { |
| 930 | const auto& p = GetParams(); |
| 931 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 932 | if (fn_supported && !fn_supported()) { |
| 933 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | const auto& ukernel = p.variant->ukernel; |
| 938 | const size_t K = p.matmul_shape.k; |
| 939 | const size_t bl = p.bl; |
| 940 | const auto nr = ukernel.interface.get_nr(); |
| 941 | const auto kr = ukernel.interface.get_kr(); |
| 942 | const auto sr = ukernel.interface.get_sr(); |
| 943 | const auto n_idx = nr; |
| 944 | |
| 945 | const auto rhs_packed_offset_kxn = |
| 946 | kai_get_rhs_packed_offset_rhs_pack_kxn_qsi4c32p_qsu4c32s1s0(n_idx, K, nr, kr, sr, bl, kai_dt_bf16); |
| 947 | const auto rhs_packed_offset_kxn_ps1s0 = |
| 948 | kai_get_rhs_packed_offset_rhs_pack_kxn_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(n_idx, K, nr, kr, sr, bl, kai_dt_bf16); |
| 949 | const auto rhs_packed_offset_nxk = |
| 950 | kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32p_qsu4c32s1s0(n_idx, K, nr, kr, sr, bl, kai_dt_bf16); |
| 951 | const auto rhs_packed_offset_nxk_ps1s0_nrx4 = |
| 952 | kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0nrx4_qsu4c32s1s0_neon(n_idx, K, nr, kr, sr, bl, kai_dt_bf16); |
| 953 | |
| 954 | ASSERT_EQ(rhs_packed_offset_kxn, rhs_packed_offset_kxn_ps1s0); |
| 955 | ASSERT_EQ(rhs_packed_offset_kxn_ps1s0, rhs_packed_offset_nxk); |
| 956 | ASSERT_EQ(rhs_packed_offset_nxk, rhs_packed_offset_nxk_ps1s0_nrx4); |
| 957 | |
| 958 | const auto rhs_matmul_offset = ukernel.interface.get_rhs_packed_offset(n_idx, K, bl); |
| 959 | ASSERT_EQ(rhs_packed_offset_kxn, rhs_matmul_offset); |
| 960 | } |
| 961 | |
| 962 | /// Verifies LHS packed offset matches the matmul interface at mr. |
| 963 | TEST_P(QMatMulClampF32Test, OffsetLHS) { |
| 964 | const auto& p = GetParams(); |
| 965 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 966 | if (fn_supported && !fn_supported()) { |
| 967 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | const auto& ukernel = p.variant->ukernel; |
| 972 | const size_t K = p.matmul_shape.k; |
| 973 | const auto mr = ukernel.interface.get_mr(); |
| 974 | const auto kr = ukernel.interface.get_kr(); |
| 975 | const auto sr = ukernel.interface.get_sr(); |
| 976 | const auto m_idx = mr; |
| 977 | |
| 978 | const auto lhs_packed_offset = kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f32(m_idx, K, mr, kr, sr); |
| 979 | const auto lhs_matmul_offset = ukernel.interface.get_lhs_packed_offset(m_idx, K); |
| 980 | |
| 981 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 982 | } |
| 983 | |
| 984 | /// Verifies the kernel’s get_dst_offset computes row/col addressing correctly at tile-aligned starts: |
| 985 | TEST_P(QMatMulClampF32Test, OffsetDst) { |
| 986 | const auto& p = GetParams(); |
| 987 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 988 | if (fn_supported && !fn_supported()) { |
| 989 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | const auto& ukernel = p.variant->ukernel; |
| 994 | const size_t M = p.matmul_shape.m; |
| 995 | const size_t N = p.matmul_shape.n; |
| 996 | |
| 997 | const auto dst_stride_row = N * sizeof(float); |
| 998 | constexpr auto dst_stride_col = sizeof(float); |
| 999 | |
| 1000 | const auto m_step = ukernel.interface.get_m_step(); |
| 1001 | const auto n_step = ukernel.interface.get_n_step(); |
| 1002 | |
| 1003 | ASSERT_TRUE(m_step % ukernel.interface.get_mr() == 0); |
| 1004 | ASSERT_TRUE(n_step % ukernel.interface.get_nr() == 0); |
| 1005 | |
| 1006 | const size_t m_idx = (M > m_step) ? m_step : 0; |
| 1007 | const size_t n_idx = (N > n_step) ? n_step : 0; |
| 1008 | |
| 1009 | const auto off00 = ukernel.interface.get_dst_offset(0, 0, dst_stride_row); |
| 1010 | ASSERT_EQ(off00, 0U); |
| 1011 | |
| 1012 | const auto off10 = ukernel.interface.get_dst_offset(m_idx, 0, dst_stride_row); |
| 1013 | ASSERT_EQ(off10, m_idx * dst_stride_row); |
| 1014 | |
| 1015 | const auto off01 = ukernel.interface.get_dst_offset(0, n_idx, dst_stride_row); |
| 1016 | ASSERT_EQ(off01, n_idx * dst_stride_col); |
| 1017 | |
| 1018 | const auto off11 = ukernel.interface.get_dst_offset(m_idx, n_idx, dst_stride_row); |
| 1019 | ASSERT_EQ(off11, m_idx * dst_stride_row + n_idx * dst_stride_col); |
| 1020 | } |
| 1021 | |
| 1022 | /// Sanity-checks kernel interface parameters (mr/nr/kr/sr and step alignment). |
| 1023 | TEST_P(QMatMulClampF32Test, KernelInvariants) { |
| 1024 | const auto& p = GetParams(); |
| 1025 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 1026 | if (fn_supported && !fn_supported()) { |
| 1027 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | const auto& ukernel = p.variant->ukernel; |
| 1032 | const auto mr = ukernel.interface.get_mr(); |
| 1033 | const auto nr = ukernel.interface.get_nr(); |
| 1034 | const auto kr = ukernel.interface.get_kr(); |
| 1035 | const auto sr = ukernel.interface.get_sr(); |
| 1036 | const auto m_step = ukernel.interface.get_m_step(); |
| 1037 | const auto n_step = ukernel.interface.get_n_step(); |
| 1038 | |
| 1039 | ASSERT_GT(mr, 0U); |
| 1040 | ASSERT_GT(nr, 0U); |
| 1041 | ASSERT_GT(kr, 0U); |
| 1042 | ASSERT_GT(sr, 0U); |
| 1043 | |
| 1044 | ASSERT_EQ(m_step % mr, 0U); |
| 1045 | ASSERT_EQ(n_step % nr, 0U); |
| 1046 | ASSERT_EQ(kr % sr, 0U); |
| 1047 | } |
| 1048 | |
| 1049 | /// Verifies RHS row stride using difference of offsets equals the layout formula. |
| 1050 | TEST_P(QMatMulClampF32Test, RhsStrideByDifference) { |
| 1051 | const auto& p = GetParams(); |
| 1052 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 1053 | if (fn_supported && !fn_supported()) { |
| 1054 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | const auto& ukernel = p.variant->ukernel; |
| 1059 | const size_t K = p.matmul_shape.k; |
| 1060 | const size_t bl = p.bl; |
| 1061 | const auto nr = ukernel.interface.get_nr(); |
| 1062 | |
| 1063 | // Stride by difference using kernel offsets at 0 and nr. |
| 1064 | const size_t off0 = ukernel.interface.get_rhs_packed_offset(0, K, bl); |
| 1065 | const size_t off1 = ukernel.interface.get_rhs_packed_offset(nr, K, bl); |
| 1066 | const size_t stride_by_diff = off1 - off0; |
| 1067 | |
| 1068 | // Expected stride formula for qsi4c32p with BF16 scales: |
| 1069 | // nr * ( num_blocks * (bl/2 + 2) + 4 /*rsum*/ + 4 /*bias*/ ) |
| 1070 | const size_t k_internal = round_up_multiple(K, 32); |
| 1071 | const size_t num_blocks = round_up_division(k_internal, bl); |
| 1072 | const size_t bytes_per_block = (bl / 2) + 2; // int4 values + BF16 scale |
| 1073 | const size_t expected_stride = nr * (num_blocks * bytes_per_block) + nr * 4 + nr * 4; |
| 1074 | |
| 1075 | ASSERT_EQ(stride_by_diff, expected_stride); |
| 1076 | } |
| 1077 | |
| 1078 | /// Validation of the packed group slice against a reconstructed reference. |
| 1079 | TEST_P(QMatMulClampF32Test, LhsPackBufferMatchesReference) { |
| 1080 | const auto& p = GetParams(); |
| 1081 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 1082 | if (fn_supported && !fn_supported()) { |
| 1083 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1084 | return; |
| 1085 | } |
| 1086 | const auto& uk = p.variant->ukernel; |
| 1087 | |
| 1088 | const size_t M = p.matmul_shape.m; |
| 1089 | const size_t K = p.matmul_shape.k; |
| 1090 | const size_t mr = uk.interface.get_mr(); |
| 1091 | const size_t kr = uk.interface.get_kr(); |
| 1092 | const size_t sr = uk.interface.get_sr(); |
| 1093 | |
| 1094 | const size_t k_block_len = kr / sr; |
| 1095 | const size_t k_internal = ((K + 31) / 32) * 32; |
| 1096 | |
| 1097 | const size_t i8_region_bytes = mr * k_internal; |
| 1098 | const size_t neg_zero_point_region_bytes = mr * sizeof(int32_t); |
| 1099 | const size_t recip_scale_region_bytes = mr * sizeof(float); |
| 1100 | const size_t group_stride = i8_region_bytes + neg_zero_point_region_bytes + recip_scale_region_bytes; |
| 1101 | |
| 1102 | constexpr size_t rect_start_row = 0; |
| 1103 | constexpr size_t rect_height = 1; |
| 1104 | |
| 1105 | const auto ref_lhs = fill_random<float>(M * K, seed_stream(current_test_key())()); |
| 1106 | |
| 1107 | const size_t lhs_stride = K * sizeof(float); |
| 1108 | std::tuple<Buffer, size_t> pack_pair = pack_lhs_qai8dxp( |
| 1109 | p.variant->lhs_pack_interface, M, K, mr, kr, sr, ref_lhs, lhs_stride, rect_start_row, rect_height); |
| 1110 | |
| 1111 | Buffer const lhs_packed = std::move(std::get<0>(pack_pair)); |
| 1112 | const size_t lhs_packed_off = std::get<1>(pack_pair); |
| 1113 | |
| 1114 | QuantizationInfo lhs_qinfo{}; |
| 1115 | lhs_qinfo.quant_width = K; |
| 1116 | lhs_qinfo.dst_type = DataType::QAI8; |
| 1117 | lhs_qinfo.scale_type = DataType::FP32; |
| 1118 | lhs_qinfo.zero_point_type = DataType::I32; |
| 1119 | auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref_lhs.data(), DataType::FP32, M, K, lhs_qinfo); |
| 1120 | |
| 1121 | Buffer const expected(group_stride, 0); |
| 1122 | std::byte* expected_bytes = expected.data(); |
| 1123 | |
| 1124 | // Build reference layout into `expected` |
| 1125 | constexpr size_t lane_row_idx = rect_start_row; |
| 1126 | const size_t lane = lane_row_idx % mr; |
| 1127 | const size_t ref_row_base = lane_row_idx * K; |
| 1128 | const auto pad_val = read_array<int8_t>(ref_lhs_quant.data(), ref_row_base + (K - 1)); |
| 1129 | |
| 1130 | size_t ref_idx = 0; |
| 1131 | const size_t num_blocks_internal = k_internal / k_block_len; |
| 1132 | |
| 1133 | for (size_t b = 0; b < num_blocks_internal; ++b) { |
| 1134 | const size_t block_base = b * mr * k_block_len; |
| 1135 | const size_t lane_offset = block_base + lane * k_block_len; |
| 1136 | |
| 1137 | for (size_t i = 0; i < k_block_len; ++i) { |
| 1138 | const size_t dst_index = lane_offset + i; |
| 1139 | const bool in_range = ref_idx < K; |
| 1140 | |
| 1141 | const int8_t val = in_range ? read_array<int8_t>(ref_lhs_quant.data(), ref_row_base + ref_idx) : pad_val; |
| 1142 | |
| 1143 | write_array<int8_t>(expected_bytes, dst_index, val); |
| 1144 | |
| 1145 | if (in_range) { |
| 1146 | ++ref_idx; |
| 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // Header (per-lane): neg_zero_point, recip_scale |
| 1152 | const size_t neg_zero_point_elem_base = i8_region_bytes / sizeof(int32_t); |
| 1153 | const size_t recip_scale_elem_base = (i8_region_bytes + neg_zero_point_region_bytes) / sizeof(float); |
| 1154 | |
| 1155 | write_array<int32_t>( |
| 1156 | expected_bytes, neg_zero_point_elem_base + lane, |
| 1157 | -read_array<int32_t>(lhs_qoutputs.zero_points.data(), lane_row_idx)); |
| 1158 | |
| 1159 | write_array<float>( |
| 1160 | expected_bytes, recip_scale_elem_base + lane, read_array<float>(lhs_qoutputs.scales.data(), lane_row_idx)); |
| 1161 | |
| 1162 | // Validate packed buffer vs reference |
| 1163 | KAI_ASSUME_ALWAYS(lhs_packed_off + group_stride <= lhs_packed.size()); |
| 1164 | |
| 1165 | // Int8 region: allow ±1 LSB |
| 1166 | for (size_t i = 0; i < i8_region_bytes; ++i) { |
| 1167 | const auto g = read_array<int8_t>(lhs_packed.data(), lhs_packed_off + i); |
| 1168 | const auto e = read_array<int8_t>(expected.data(), i); |
| 1169 | const int dq = static_cast<int>(g) - static_cast<int>(e); |
| 1170 | EXPECT_LE(std::abs(dq), 1) << "int8 mismatch at byte " << i << " (got=" << static_cast<int>(g) |
| 1171 | << ", exp=" << static_cast<int>(e) << ", dq=" << dq << ")"; |
| 1172 | } |
| 1173 | |
| 1174 | // Region offsets (in bytes) |
| 1175 | const size_t neg_zero_point_offset = i8_region_bytes; |
| 1176 | const size_t recip_scale_offset = neg_zero_point_offset + neg_zero_point_region_bytes; |
| 1177 | |
| 1178 | // neg_zero_point (exact) |
| 1179 | for (size_t hdr_lane = 0; hdr_lane < mr; ++hdr_lane) { |
| 1180 | const auto gzp = read_array<int32_t>( |
| 1181 | lhs_packed.data(), lhs_packed_off / sizeof(int32_t) + (neg_zero_point_offset / sizeof(int32_t)) + hdr_lane); |
| 1182 | const auto ezp = read_array<int32_t>(expected.data(), (neg_zero_point_offset / sizeof(int32_t)) + hdr_lane); |
| 1183 | EXPECT_EQ(gzp, ezp) << "neg_zp mismatch at lane " << hdr_lane; |
| 1184 | } |
| 1185 | |
| 1186 | // recip_scale (near-equal) |
| 1187 | for (size_t hdr_lane = 0; hdr_lane < mr; ++hdr_lane) { |
| 1188 | const auto gsc = read_array<float>( |
| 1189 | lhs_packed.data(), lhs_packed_off / sizeof(float) + (recip_scale_offset / sizeof(float)) + hdr_lane); |
| 1190 | const auto esc = read_array<float>(expected.data(), (recip_scale_offset / sizeof(float)) + hdr_lane); |
| 1191 | EXPECT_NEAR(gsc, esc, 1e-5F) << "recip_scale mismatch at lane " << hdr_lane; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | TEST_P(QMatMulClampF32Test, EndToEnd) { |
| 1196 | const auto& p = GetParams(); |
| 1197 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 1198 | if (fn_supported && !fn_supported()) { |
| 1199 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1200 | return; |
| 1201 | } |
| 1202 | const auto& ukernel = p.variant->ukernel; |
| 1203 | |
| 1204 | const size_t bl = p.bl; |
| 1205 | const RhsPackType rhs_pack_type = p.rhs_pack_type; |
| 1206 | |
| 1207 | KAI_ASSUME_ALWAYS(bl % 32 == 0); |
| 1208 | |
| 1209 | const auto nr = ukernel.interface.get_nr(); |
| 1210 | const auto kr = ukernel.interface.get_kr(); |
| 1211 | const auto sr = ukernel.interface.get_sr(); |
| 1212 | |
| 1213 | const auto n_step = ukernel.interface.get_n_step(); |
| 1214 | ASSERT_TRUE(n_step % nr == 0); |
| 1215 | |
| 1216 | const auto rect = p.rect; |
| 1217 | ASSERT_GT(rect.height(), 0U); |
| 1218 | ASSERT_GT(rect.width(), 0U); |
| 1219 | |
| 1220 | const auto& data = test_data(); |
| 1221 | |
| 1222 | const auto rhs_start_col = rect.start_col(); |
| 1223 | const size_t bias_offset_bytes = rhs_start_col * sizeof(float); |
| 1224 | |
| 1225 | Buffer imp_packed_rhs; |
| 1226 | size_t rhs_packed_offset = 0; |
| 1227 | if (rhs_pack_type == RhsPackType::NxK) { |
| 1228 | const float* bias_ptr = reinterpret_cast<const float*>(data.bias.data()) + rhs_start_col; |
| 1229 | std::tie(imp_packed_rhs, rhs_packed_offset) = pack_rhs_qsi4c32p_nxk( |
| 1230 | p.variant->rhs_pack_interface, data.N, data.K, nr, kr, sr, bl, data.rhs_quant, bias_ptr, data.rhs_scales, |
| 1231 | rhs_start_col, rect.width(), p.variant->rhs_s0s1_input); |
| 1232 | } else { |
| 1233 | if ((rhs_start_col % 2) != 0) { |
| 1234 | GTEST_SKIP() << "KxN RHS pack requires even N-start index"; |
| 1235 | return; |
| 1236 | } |
| 1237 | std::tie(imp_packed_rhs, rhs_packed_offset) = pack_rhs_qsi4c32p_kxn( |
| 1238 | data.N, data.K, nr, kr, sr, bl, data.rhs_quant, data.bias, bias_offset_bytes, data.rhs_scales, |
| 1239 | rhs_start_col, rect.width(), p.is_sme2); |
| 1240 | } |
| 1241 | |
| 1242 | ASSERT_EQ(rhs_packed_offset, ukernel.interface.get_rhs_packed_offset(rhs_start_col, data.K, bl)); |
| 1243 | |
| 1244 | // Destination buffer and offsets |
| 1245 | const auto dst_stride_row = data.N * sizeof(float); |
| 1246 | constexpr auto dst_stride_col = sizeof(float); |
| 1247 | const auto dst_offset = ukernel.interface.get_dst_offset(rect.start_row(), rhs_start_col, dst_stride_row); |
| 1248 | const auto imp_dst_size = ukernel.interface.get_dst_size(data.M, data.N); |
| 1249 | ASSERT_EQ(imp_dst_size, data.ref_dst_clamped.size()); |
| 1250 | Buffer const imp_dst(imp_dst_size); |
| 1251 | |
| 1252 | // Run matmul |
| 1253 | abi_check( |
| 1254 | ukernel.interface.run_matmul, rect.height(), rect.width(), data.K, bl, |
| 1255 | data.lhs_packed.data() + data.lhs_packed_offset, imp_packed_rhs.data() + rhs_packed_offset, |
| 1256 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), dst_stride_row, dst_stride_col, data.clamp.min, |
| 1257 | data.clamp.max); |
| 1258 | |
| 1259 | DefaultMismatchHandler handler(0, 0.1, 0, 0.05); |
| 1260 | const auto dst_format = DataFormat(DataType::FP32); |
| 1261 | const auto success = |
| 1262 | compare(imp_dst.data(), data.ref_dst_clamped.data(), dst_format, data.M, data.N, rect, handler); |
| 1263 | ASSERT_TRUE(success); |
| 1264 | } |
| 1265 | |
| 1266 | /// RHS vectorised packer format is s16s0 this is not relevant for sme2 kernels |
| 1267 | class NeonRhsPackF32Test : public QMatMulClampF32Test {}; |
| 1268 | |
| 1269 | TEST_P(NeonRhsPackF32Test, EndToEndNeonRhsPack) { |
| 1270 | const auto& p = GetParams(); |
| 1271 | const auto fn_supported = p.variant->ukernel.fn_is_supported; |
| 1272 | if (fn_supported && !fn_supported()) { |
| 1273 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1274 | return; |
| 1275 | } |
| 1276 | const auto& ukernel = p.variant->ukernel; |
| 1277 | |
| 1278 | const size_t mr = ukernel.interface.get_mr(); |
| 1279 | const size_t nr = ukernel.interface.get_nr(); |
| 1280 | const size_t kr = ukernel.interface.get_kr(); |
| 1281 | const size_t sr = ukernel.interface.get_sr(); |
| 1282 | ASSERT_EQ(ukernel.interface.get_m_step() % mr, 0U); |
| 1283 | ASSERT_EQ(ukernel.interface.get_n_step() % nr, 0U); |
| 1284 | |
| 1285 | if (p.rhs_pack_type != RhsPackType::NxK || (kr / sr != 8 && kr / sr != 4)) { |
| 1286 | GTEST_SKIP() << "RHS packers not applicable"; |
| 1287 | } |
| 1288 | ASSERT_GT(p.rect.height(), 0U); |
| 1289 | ASSERT_GT(p.rect.width(), 0U); |
| 1290 | |
| 1291 | const auto& data = test_data(); |
| 1292 | |
| 1293 | // LHS pack |
| 1294 | const size_t lhs_stride_bytes = data.K * sizeof(float); |
| 1295 | auto [imp_packed_lhs, lhs_packed_offset] = pack_lhs_qai8dxp( |
| 1296 | p.variant->lhs_pack_interface, data.M, data.K, mr, kr, sr, data.lhs, lhs_stride_bytes, p.rect.start_row(), |
| 1297 | p.rect.height()); |
| 1298 | ASSERT_EQ(lhs_packed_offset, ukernel.interface.get_lhs_packed_offset(p.rect.start_row(), data.K)); |
| 1299 | |
| 1300 | // RHS pack |
| 1301 | const size_t rhs_start_row = p.rect.start_col(); |
| 1302 | const size_t bias_offset = rhs_start_row * sizeof(float); |
| 1303 | const auto [imp_packed_rhs_neon, rhs_packed_offset_neon] = pack_rhs_qsi4c32pscalebf16_neon( |
| 1304 | data.N, data.K, nr, kr, sr, p.bl, data.rhs_quant, data.bias, bias_offset, data.rhs_scales, p.rhs_pack_type, |
| 1305 | rhs_start_row, p.rect.width()); |
| 1306 | |
| 1307 | ASSERT_EQ(rhs_packed_offset_neon, ukernel.interface.get_rhs_packed_offset(rhs_start_row, data.K, p.bl)); |
| 1308 | |
| 1309 | const auto dst_stride_row = data.N * sizeof(float); |
| 1310 | Buffer const imp_dst(ukernel.interface.get_dst_size(data.M, data.N)); |
| 1311 | const auto dst_offset = ukernel.interface.get_dst_offset(p.rect.start_row(), rhs_start_row, dst_stride_row); |
| 1312 | |
| 1313 | // Run matmul |
| 1314 | abi_check( |
| 1315 | ukernel.interface.run_matmul, p.rect.height(), p.rect.width(), data.K, p.bl, |
| 1316 | imp_packed_lhs.data() + lhs_packed_offset, imp_packed_rhs_neon.data() + rhs_packed_offset_neon, |
| 1317 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), dst_stride_row, sizeof(float), data.clamp.min, |
| 1318 | data.clamp.max); |
| 1319 | |
| 1320 | DefaultMismatchHandler handler(0, 0.1, 0, 0.05); |
| 1321 | const DataFormat dst_format(DataType::FP32); |
| 1322 | ASSERT_TRUE(compare(imp_dst.data(), data.ref_dst_clamped.data(), dst_format, data.M, data.N, p.rect, handler)); |
| 1323 | } |
| 1324 | |
| 1325 | class QMatMulClampBF16Test : public ::testing::TestWithParam<MatMulTestParams_withBL_withRHSPackType> {}; |
| 1326 | TEST_P(QMatMulClampBF16Test, EndToEnd) { |
| 1327 | const auto& [variant_index, matmul_shape, bl, portion, rhs_pack_type, clamp_keep_ratio] = GetParam(); |
| 1328 | const auto& ukernel_variant = get_bf16_gemm_variants().at(variant_index); |
| 1329 | |
| 1330 | if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) { |
| 1331 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 1332 | } |
| 1333 | |
| 1334 | const size_t M = matmul_shape.m; |
| 1335 | const size_t N = matmul_shape.n; |
| 1336 | const size_t K = matmul_shape.k; |
| 1337 | |
| 1338 | const auto mr = ukernel_variant.interface.get_mr(); |
| 1339 | const auto nr = ukernel_variant.interface.get_nr(); |
| 1340 | const auto kr = ukernel_variant.interface.get_kr(); |
| 1341 | const auto sr = ukernel_variant.interface.get_sr(); |
| 1342 | |
| 1343 | const auto m_step = ukernel_variant.interface.get_m_step(); |
| 1344 | ASSERT_TRUE(m_step % mr == 0); |
| 1345 | |
| 1346 | const auto n_step = ukernel_variant.interface.get_n_step(); |
| 1347 | ASSERT_TRUE(n_step % nr == 0); |
| 1348 | |
| 1349 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 1350 | ASSERT_GT(rect.height(), 0U); |
| 1351 | ASSERT_GT(rect.width(), 0U); |
| 1352 | |
| 1353 | // Cached reference and inputs |
| 1354 | const BF16QMatMulRefKey key{matmul_shape, |
| 1355 | bl, |
| 1356 | mr, |
| 1357 | nr, |
| 1358 | kr, |
| 1359 | sr, |
| 1360 | rect.start_row(), |
| 1361 | rect.start_col(), |
| 1362 | rect.height(), |
| 1363 | rect.width(), |
| 1364 | rhs_pack_type, |
| 1365 | clamp_keep_ratio}; |
| 1366 | const BF16TestData& data = getV<BF16QMatMulRefKey, BF16TestData>(key); |
| 1367 | |
| 1368 | // Verify LHS offsets match interface |
| 1369 | const auto lhs_start_row = rect.start_row(); |
| 1370 | const auto lhs_packed_offset = |
| 1371 | kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_bf16_neon(lhs_start_row, K, mr, kr, sr); |
| 1372 | const auto lhs_matmul_offset = ukernel_variant.interface.get_lhs_packed_offset(lhs_start_row, K); |
| 1373 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 1374 | |
| 1375 | // RHS: pack using cached quant/scales/bias |
| 1376 | const size_t rhs_start_row = rect.start_col(); |
| 1377 | const size_t bias_offset = rhs_start_row * sizeof(float); |
| 1378 | if (rhs_pack_type == RhsPackType::KxN && (rhs_start_row % 2) != 0) { |
| 1379 | GTEST_SKIP() << "KxN RHS pack requires even N-start index"; |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | auto [imp_packed_rhs, rhs_packed_offset] = pack_rhs_qsi4c32pscalebf16( |
| 1384 | N, K, nr, kr, sr, bl, data.rhs_quant, data.bias, bias_offset, data.rhs_scales, rhs_pack_type, rhs_start_row, |
| 1385 | rect.width(), false); |
| 1386 | |
| 1387 | const auto rhs_matmul_offset = ukernel_variant.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 1388 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 1389 | |
| 1390 | // Destination |
| 1391 | const auto dst_stride_row = N * sizeof(uint16_t); |
| 1392 | constexpr auto dst_stride_col = sizeof(uint16_t); |
| 1393 | const auto dst_offset = |
| 1394 | ukernel_variant.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row); |
| 1395 | const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col; |
| 1396 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 1397 | |
| 1398 | const auto imp_dst_size = ukernel_variant.interface.get_dst_size(M, N); |
| 1399 | ASSERT_EQ(imp_dst_size, data.ref_dst_bf16.size()); |
| 1400 | Buffer imp_dst(imp_dst_size); |
| 1401 | |
| 1402 | // Run matmul |
| 1403 | abi_check( |
| 1404 | ukernel_variant.interface.run_matmul, rect.height(), rect.width(), K, bl, |
| 1405 | reinterpret_cast<const uint8_t*>(data.lhs_packed.data()) + lhs_matmul_offset, |
| 1406 | reinterpret_cast<const uint8_t*>(imp_packed_rhs.data()) + rhs_matmul_offset, imp_dst.data() + dst_offset, |
| 1407 | dst_stride_row, dst_stride_col, data.clamp.min, data.clamp.max); |
| 1408 | |
| 1409 | DefaultMismatchHandler handler(0, 0.02, 0, 0.05); |
| 1410 | auto dst_format = DataFormat(DataType::BF16); |
| 1411 | const auto success = compare(imp_dst.data(), data.ref_dst_bf16.data(), dst_format, M, N, rect, handler); |
| 1412 | ASSERT_TRUE(success); |
| 1413 | |
| 1414 | // Test vectorized packing micro-kernels, if packing parameters allow |
| 1415 | if (rhs_pack_type == RhsPackType::NxK && (kr / sr == 8 || kr / sr == 4)) { |
| 1416 | const auto [imp_packed_rhs_neon, rhs_packed_offset_neon] = pack_rhs_qsi4c32pscalebf16_neon( |
| 1417 | N, K, nr, kr, sr, bl, data.rhs_quant, data.bias, bias_offset, data.rhs_scales, rhs_pack_type, rhs_start_row, |
| 1418 | rect.width()); |
| 1419 | ASSERT_EQ(rhs_packed_offset_neon, rhs_packed_offset); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | // clang-format off |
| 1424 | |
| 1425 | /// Portion categories (GEMM/GEMV) |
| 1426 | static constexpr std::array gemm_portions{ |
| 1427 | MatrixPortion(0, 0, 1, 1), // Full matrix |
| 1428 | MatrixPortion(0.4, 0.5, 0.6, 0.8), // Middle block |
| 1429 | }; |
| 1430 | static constexpr std::array gemv_portions{ |
| 1431 | MatrixPortion(0, 0, 1, 1), // Full width |
| 1432 | MatrixPortion(0, 0.5, 1, 0.5), // Right half |
| 1433 | }; |
| 1434 | |
| 1435 | /// Shape categories (GEMM/GEMV) |
| 1436 | |
| 1437 | /// Small/Odd edge coverage (odd m/n, varied K) |
| 1438 | static constexpr std::array gemm_shapes_small_odd{ |
| 1439 | MatMulShape{ 17, 25, 64}, |
| 1440 | MatMulShape{ 31, 31, 64}, |
| 1441 | MatMulShape{ 21, 53, 256}, |
| 1442 | MatMulShape{ 35, 27, 320}, |
| 1443 | }; |
| 1444 | |
| 1445 | /// Aligned squares (cache-friendly, power-of-two-ish) |
| 1446 | static constexpr std::array gemm_shapes_aligned{ |
| 1447 | MatMulShape{ 32, 32, 128}, |
| 1448 | MatMulShape{ 64, 64, 128}, |
| 1449 | MatMulShape{128, 128, 256}, |
| 1450 | MatMulShape{192, 192, 384}, |
| 1451 | }; |
| 1452 | |
| 1453 | /// Rectangular (skinny/wide), varied K |
| 1454 | static constexpr std::array gemm_shapes_rect{ |
| 1455 | MatMulShape{ 64, 128, 256}, // wide N |
| 1456 | MatMulShape{128, 64, 256}, // tall M |
| 1457 | MatMulShape{ 96, 192, 384}, |
| 1458 | MatMulShape{160, 96, 320}, |
| 1459 | }; |
| 1460 | |
| 1461 | /// Larger/stress (within reason for CI) |
| 1462 | static constexpr std::array gemm_shapes_large{ |
| 1463 | MatMulShape{128, 160, 320}, |
| 1464 | MatMulShape{160, 128, 320}, |
| 1465 | MatMulShape{224, 160, 320}, |
| 1466 | MatMulShape{160, 224, 320}, |
| 1467 | }; |
| 1468 | |
| 1469 | /// GEMV shape categories (F32) |
| 1470 | /// M = 1, RHS NxK only in instantiation |
| 1471 | |
| 1472 | /// Small/medium N, diverse K (aligned/odd N) |
| 1473 | static constexpr std::array gemv_shapes_small{ |
| 1474 | MatMulShape{ 1, 16, 64}, |
| 1475 | MatMulShape{ 1, 31, 64}, |
| 1476 | MatMulShape{ 1, 71, 64}, |
| 1477 | MatMulShape{ 1, 128, 256}, |
| 1478 | MatMulShape{ 1, 256, 256}, |
| 1479 | MatMulShape{ 1, 320, 320}, |
| 1480 | }; |
| 1481 | |
| 1482 | /// Larger N bands (bandwidth/cache stress) |
| 1483 | static constexpr std::array gemv_shapes_large{ |
| 1484 | MatMulShape{ 1, 512, 256}, |
| 1485 | MatMulShape{ 1, 640, 320}, |
| 1486 | MatMulShape{ 1, 768, 384}, |
| 1487 | MatMulShape{ 1, 1024, 256}, |
| 1488 | MatMulShape{ 1, 896, 384}, |
| 1489 | }; |
| 1490 | |
| 1491 | static constexpr std::array bf16_shapes { |
| 1492 | MatMulShape{ 32, 32, 64}, // small aligned |
| 1493 | MatMulShape{ 48, 64, 64}, // rectangular (tall K-block reuse) |
| 1494 | MatMulShape{ 64, 64, 128}, // aligned square |
| 1495 | MatMulShape{ 96, 96, 192}, // larger aligned |
| 1496 | MatMulShape{128, 64, 256}, // rectangular (tall M) |
| 1497 | MatMulShape{ 17, 25, 64}, // odd sizes (edge behavior) |
| 1498 | MatMulShape{ 33, 29, 192}, // odd sizes with larger K |
| 1499 | MatMulShape{128, 160, 320}, // larger rectangular |
| 1500 | }; |
| 1501 | |
| 1502 | /// Dedicated clamp sweep ratios |
| 1503 | static constexpr std::array<std::optional<float>, 4> clamp_keep_ratios_sweep{ |
| 1504 | std::nullopt, // Disable clamping |
| 1505 | 1.0F, // Clamp to full range |
| 1506 | 0.5F, // Clamp to 50% range |
| 1507 | 0.1F, // Clamp to 10% range |
| 1508 | }; |
| 1509 | |
| 1510 | INSTANTIATE_TEST_SUITE_P( |
| 1511 | MatMulGemm_SmallOdd, QMatMulClampF32Test, |
| 1512 | testing::Combine( |
| 1513 | testing::Range<size_t>(0, get_f32_gemm_variants().size()), |
| 1514 | testing::Values(true), |
| 1515 | testing::ValuesIn(gemm_shapes_small_odd), |
| 1516 | testing::Values(32), |
| 1517 | testing::ValuesIn(gemm_portions), |
| 1518 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1519 | testing::Values(0.5F)), |
| 1520 | testing::PrintToStringParamName()); |
| 1521 | |
| 1522 | INSTANTIATE_TEST_SUITE_P( |
| 1523 | MatMulGemm_Aligned, QMatMulClampF32Test, |
| 1524 | testing::Combine( |
| 1525 | testing::Range<size_t>(0, get_f32_gemm_variants().size()), |
| 1526 | testing::Values(true), |
| 1527 | testing::ValuesIn(gemm_shapes_aligned), |
| 1528 | testing::Values(32), |
| 1529 | testing::ValuesIn(gemm_portions), |
| 1530 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1531 | testing::Values(0.5F)), |
| 1532 | testing::PrintToStringParamName()); |
| 1533 | |
| 1534 | INSTANTIATE_TEST_SUITE_P( |
| 1535 | MatMulGemm_Rect, QMatMulClampF32Test, |
| 1536 | testing::Combine( |
| 1537 | testing::Range<size_t>(0, get_f32_gemm_variants().size()), |
| 1538 | testing::Values(true), |
| 1539 | testing::ValuesIn(gemm_shapes_rect), |
| 1540 | testing::Values(32, 64), |
| 1541 | testing::ValuesIn(gemm_portions), |
| 1542 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1543 | testing::Values(0.5F)), |
| 1544 | testing::PrintToStringParamName()); |
| 1545 | |
| 1546 | INSTANTIATE_TEST_SUITE_P( |
| 1547 | MatMulGemm_Large, QMatMulClampF32Test, |
| 1548 | testing::Combine( |
| 1549 | testing::Range<size_t>(0, get_f32_gemm_variants().size()), |
| 1550 | testing::Values(true), |
| 1551 | testing::ValuesIn(gemm_shapes_large), |
| 1552 | testing::Values(32), |
| 1553 | testing::ValuesIn(gemm_portions), |
| 1554 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1555 | testing::Values(0.5F)), |
| 1556 | testing::PrintToStringParamName()); |
| 1557 | |
| 1558 | INSTANTIATE_TEST_SUITE_P( |
| 1559 | MatMulGemv_Small, QMatMulClampF32Test, |
| 1560 | testing::Combine( |
| 1561 | testing::Range<size_t>(0, get_f32_gemv_variants().size()), |
| 1562 | testing::Values(false), |
| 1563 | testing::ValuesIn(gemv_shapes_small), |
| 1564 | testing::Values(32), |
| 1565 | testing::ValuesIn(gemv_portions), |
| 1566 | testing::Values(RhsPackType::NxK), |
| 1567 | testing::Values(0.5F)), |
| 1568 | testing::PrintToStringParamName()); |
| 1569 | |
| 1570 | INSTANTIATE_TEST_SUITE_P( |
| 1571 | MatMulGemv_Large, QMatMulClampF32Test, |
| 1572 | testing::Combine( |
| 1573 | testing::Range<size_t>(0, get_f32_gemv_variants().size()), |
| 1574 | testing::Values(false), |
| 1575 | testing::ValuesIn(gemv_shapes_large), |
| 1576 | testing::Values(32), |
| 1577 | testing::ValuesIn(gemv_portions), |
| 1578 | testing::Values(RhsPackType::NxK), |
| 1579 | testing::Values(0.5F)), |
| 1580 | testing::PrintToStringParamName()); |
| 1581 | |
| 1582 | INSTANTIATE_TEST_SUITE_P( |
| 1583 | MatMulNeonRhsPackGemm_SmallOdd, NeonRhsPackF32Test, |
| 1584 | testing::Combine( |
| 1585 | testing::Range<size_t>(0, get_f32_neon_gemm_variants_only().size()), |
| 1586 | testing::Values(true), |
| 1587 | testing::ValuesIn(gemm_shapes_small_odd), |
| 1588 | testing::Values(32), |
| 1589 | testing::ValuesIn(gemm_portions), |
| 1590 | testing::Values(RhsPackType::NxK), |
| 1591 | testing::Values(0.5F)), |
| 1592 | testing::PrintToStringParamName()); |
| 1593 | |
| 1594 | INSTANTIATE_TEST_SUITE_P( |
| 1595 | MatMulNeonRhsPackGemm_Aligned, NeonRhsPackF32Test, |
| 1596 | testing::Combine( |
| 1597 | testing::Range<size_t>(0, get_f32_neon_gemm_variants_only().size()), |
| 1598 | testing::Values(true), |
| 1599 | testing::ValuesIn(gemm_shapes_aligned), |
| 1600 | testing::Values(32), |
| 1601 | testing::ValuesIn(gemm_portions), |
| 1602 | testing::Values(RhsPackType::NxK), |
| 1603 | testing::Values(0.5F)), |
| 1604 | testing::PrintToStringParamName()); |
| 1605 | |
| 1606 | static constexpr std::array clamp_sweep_shapes{ |
| 1607 | MatMulShape{ 64, 64, 128 }, |
| 1608 | MatMulShape{ 64, 128, 256 }, |
| 1609 | }; |
| 1610 | |
| 1611 | INSTANTIATE_TEST_SUITE_P( |
| 1612 | MatMulGemm_ClampSweep, QMatMulClampF32Test, |
| 1613 | testing::Combine( |
| 1614 | testing::Range<size_t>(0, get_f32_gemm_variants().size()), |
| 1615 | testing::Values(true), |
| 1616 | testing::ValuesIn(clamp_sweep_shapes), |
| 1617 | testing::Values(32), |
| 1618 | testing::Values(MatrixPortion(0, 0, 1, 1)), |
| 1619 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1620 | testing::ValuesIn(clamp_keep_ratios_sweep)), |
| 1621 | testing::PrintToStringParamName()); |
| 1622 | |
| 1623 | INSTANTIATE_TEST_SUITE_P( |
| 1624 | MatMulBF16_SingleSet, QMatMulClampBF16Test, |
| 1625 | testing::Combine( |
| 1626 | testing::Range<size_t>(0, get_bf16_gemm_variants().size()), |
| 1627 | testing::ValuesIn(bf16_shapes), |
| 1628 | testing::Values(32), |
| 1629 | testing::Values(MatrixPortion(0, 0, 1, 1)), |
| 1630 | testing::Values(RhsPackType::NxK, RhsPackType::KxN), |
| 1631 | testing::ValuesIn(clamp_keep_ratios_sweep) |
| 1632 | ), |
| 1633 | testing::PrintToStringParamName()); |
| 1634 | |
| 1635 | } // namespace kai::test |