| 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 <algorithm> |
| 10 | #include <array> |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <limits> |
| 14 | #include <string> |
| 15 | #include <tuple> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.h" |
| 19 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme_mopa.h" |
| 20 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme2_sdot.h" |
| 21 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme_dot.h" |
| 22 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p4x4_1x4_neon_dotprod.h" |
| 23 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p8x4_1x8_sve_dotprod.h" |
| 24 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod.h" |
| 25 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p8x8_1x8_sve_dotprod.h" |
| 26 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x4_qsi4c32p4x4_16x4_neon_dotprod.h" |
| 27 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm.h" |
| 28 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm.h" |
| 29 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p8x8_16x8_sve_i8mm.h" |
| 30 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_interface.h" |
| 31 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p4x8sb_f32_neon.h" |
| 32 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p_f32.h" |
| 33 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p_f32_neon.h" |
| 34 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h" |
| 35 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon.h" |
| 36 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0.h" |
| 37 | #include "test/common/abi_checker.hpp" |
| 38 | #include "test/common/buffer.hpp" |
| 39 | #include "test/common/compare.hpp" |
| 40 | #include "test/common/cpu_info.hpp" |
| 41 | #include "test/common/float16.hpp" |
| 42 | #include "test/common/int4.hpp" |
| 43 | #include "test/common/matmul_test_common.hpp" |
| 44 | #include "test/common/matrix_portion.hpp" |
| 45 | #include "test/common/memory.hpp" |
| 46 | #include "test/common/test_suite.hpp" |
| 47 | #include "test/reference/cast.hpp" |
| 48 | #include "test/reference/clamp.hpp" |
| 49 | #include "test/reference/fill.hpp" |
| 50 | #include "test/reference/matmul.hpp" |
| 51 | #include "test/reference/pack.hpp" |
| 52 | #include "test/reference/quantize.hpp" |
| 53 | |
| 54 | namespace kai::test { |
| 55 | |
| 56 | // Interface for the LHS and RHS packed size and packing micro-kernels |
| 57 | using kai_get_lhs_packed_size_func_t = decltype(&kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32p_f32); |
| 58 | using kai_get_rhs_packed_size_func_t = decltype(&kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0); |
| 59 | using kai_get_lhs_packed_offset_func_t = decltype(&kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32p_f32); |
| 60 | using kai_get_rhs_packed_offset_func_t = |
| 61 | decltype(&kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0); |
| 62 | using kai_get_lhs_offset_func_t = decltype(&kai_get_lhs_offset_lhs_quant_pack_qsi8d32p_f32); |
| 63 | using kai_get_rhs_offset_func_t = decltype(&kai_get_rhs_offset_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0); |
| 64 | using kai_run_lhs_pack_func_t = decltype(&kai_run_lhs_quant_pack_qsi8d32p_f32); |
| 65 | using kai_run_rhs_pack_func_t = decltype(&kai_run_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0); |
| 66 | |
| 67 | // Micro-kernel interface |
| 68 | struct kai_qsi8d32p_pack_functions { |
| 69 | kai_get_lhs_packed_size_func_t packed_size; |
| 70 | kai_get_lhs_packed_offset_func_t get_packed_offset; |
| 71 | kai_get_lhs_offset_func_t get_offset; |
| 72 | kai_run_lhs_pack_func_t run_pack; |
| 73 | }; |
| 74 | struct kai_qsi4c32p_pack_functions { |
| 75 | kai_get_rhs_packed_size_func_t packed_size; |
| 76 | kai_get_rhs_packed_offset_func_t get_packed_offset; |
| 77 | kai_get_rhs_offset_func_t get_offset; |
| 78 | kai_run_rhs_pack_func_t run_pack; |
| 79 | }; |
| 80 | |
| 81 | struct UKernelVariants { |
| 82 | UkernelMatmulPackVariant< |
| 83 | kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_ukernel, kai_qsi8d32p_pack_functions, kai_qsi4c32p_pack_functions> |
| 84 | variant; |
| 85 | }; |
| 86 | |
| 87 | // clang-format off |
| 88 | static const std::array<UKernelVariants, 7> |
| 89 | variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p = { |
| 90 | { |
| 91 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 92 | clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa, cpu_has_sme2, lhs_quant_pack_qsi8d32p_f32_neon, |
| 93 | rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon, false)}, |
| 94 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 95 | clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme_mopa, cpu_has_sme, lhs_quant_pack_qsi8d32p_f32_neon, |
| 96 | rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon, false)}, |
| 97 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 98 | clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme2_sdot, cpu_has_sme2, lhs_quant_pack_qsi8d32p_f32_neon, |
| 99 | rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon, false)}, |
| 100 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 101 | clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme_dot, cpu_has_sme, lhs_quant_pack_qsi8d32p_f32_neon, |
| 102 | rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon, false)}, |
| 103 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 104 | clamp_f32_qsi8d32p1x4_qsi4c32p8x4_1x8_sve_dotprod, (cpu_check<cpu_has_sve_vl256, cpu_has_dotprod>), lhs_quant_pack_qsi8d32p_f32, |
| 105 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 106 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 107 | clamp_f32_qsi8d32p1x8_qsi4c32p8x8_1x8_sve_dotprod, (cpu_check<cpu_has_sve_vl256, cpu_has_dotprod>), lhs_quant_pack_qsi8d32p_f32, |
| 108 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 109 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 110 | clamp_f32_qsi8d32p4x8_qsi4c32p8x8_16x8_sve_i8mm, (cpu_check<cpu_has_sve_vl256, cpu_has_i8mm>), lhs_quant_pack_qsi8d32p_f32, |
| 111 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}}}; |
| 112 | |
| 113 | static const std::array<UKernelVariants, 10> |
| 114 | variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl = { |
| 115 | { |
| 116 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 117 | clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa, cpu_has_sme2, lhs_quant_pack_qsi8d32p_f32_neon, |
| 118 | rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon, false)}, |
| 119 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 120 | clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme_mopa, cpu_has_sme, lhs_quant_pack_qsi8d32p_f32_neon, |
| 121 | rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon, false)}, |
| 122 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 123 | clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme2_sdot, cpu_has_sme2, lhs_quant_pack_qsi8d32p_f32_neon, |
| 124 | rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon, false)}, |
| 125 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 126 | clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme_dot, cpu_has_sme, lhs_quant_pack_qsi8d32p_f32_neon, |
| 127 | rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon, false)}, |
| 128 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 129 | clamp_f32_qsi8d32p4x4_qsi4c32p4x4_16x4_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qsi8d32p_f32, |
| 130 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 131 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 132 | clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qsi8d32p_f32, |
| 133 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 134 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 135 | clamp_f32_qsi8d32p4x8_qsi4c32p4x8_8x4x32_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qsi8d32p_f32, |
| 136 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 137 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 138 | clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qsi8d32p_f32, |
| 139 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 140 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 141 | 4x8sb_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm, clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm, |
| 142 | cpu_has_i8mm, lhs_quant_pack_qsi8d32p4x8sb_f32_neon, rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}, |
| 143 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 144 | clamp_f32_qsi8d32p1x4_qsi4c32p4x4_1x4_neon_dotprod, cpu_has_dotprod, lhs_quant_pack_qsi8d32p_f32, |
| 145 | rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0, false)}}}; |
| 146 | // clang-format on |
| 147 | |
| 148 | using MatMulTestParams_f32_qsi8d32p_qsi4c32p = |
| 149 | std::tuple<size_t, MatMulShape, MatrixPortion, std::optional<float>, size_t, bool>; |
| 150 | |
| 151 | [[maybe_unused]] static void PrintTo(const MatMulTestParams_f32_qsi8d32p_qsi4c32p& param, std::ostream* os) { |
| 152 | const auto variant_idx = std::get<0>(param); |
| 153 | const auto shape = std::get<1>(param); |
| 154 | const auto portion = std::get<2>(param); |
| 155 | const auto clamp_keep_ratio = std::get<3>(param); |
| 156 | const auto bl = std::get<4>(param); |
| 157 | const auto variable_bl = std::get<5>(param); |
| 158 | |
| 159 | *os << "variant_" << variant_idx << "__"; |
| 160 | PrintTo(shape, os); |
| 161 | *os << "__"; |
| 162 | PrintTo(portion, os); |
| 163 | *os << "__clamp_keep_ratio_" |
| 164 | << (clamp_keep_ratio.has_value() ? std::to_string(static_cast<int>(clamp_keep_ratio.value() * 100)) |
| 165 | : "noclamp"); |
| 166 | *os << (variable_bl ? "__VarBL" : "__FixedBL"); |
| 167 | *os << "__bl_" << bl; |
| 168 | } |
| 169 | |
| 170 | class MatMulTest_f32_qsi8d32p_qsi4c32p_Offset |
| 171 | : public ::testing::TestWithParam<MatMulTestParams_f32_qsi8d32p_qsi4c32p> {}; |
| 172 | class MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd |
| 173 | : public ::testing::TestWithParam<MatMulTestParams_f32_qsi8d32p_qsi4c32p> {}; |
| 174 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd); |
| 175 | |
| 176 | static const UKernelVariants& get_variant_entry(size_t variant_index, bool variable_bl) { |
| 177 | if (!variable_bl) { |
| 178 | return variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p.at(variant_index); |
| 179 | } |
| 180 | |
| 181 | return variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.at(variant_index); |
| 182 | } |
| 183 | |
| 184 | static uint8_t get_qsu4_rhs_value(const size_t row, const size_t k_idx) { |
| 185 | return static_cast<uint8_t>((1 + (row * 5) + (k_idx * 3)) & 0x0F); |
| 186 | } |
| 187 | |
| 188 | static Float16 get_rhs_scale_value(const size_t row, const size_t block_idx) { |
| 189 | return Float16(16.0F * static_cast<float>(1 + row + block_idx)); |
| 190 | } |
| 191 | |
| 192 | static Buffer make_s4s0_rhs_with_scales(const size_t n, const size_t k, const size_t bl) { |
| 193 | const size_t num_blocks = k / bl; |
| 194 | const size_t num_bytes_per_block = (bl / 2) + sizeof(Float16); |
| 195 | const size_t rhs_stride = num_blocks * num_bytes_per_block; |
| 196 | Buffer rhs(n * rhs_stride); |
| 197 | |
| 198 | for (size_t row = 0; row < n; ++row) { |
| 199 | for (size_t block_idx = 0; block_idx < num_blocks; ++block_idx) { |
| 200 | std::byte* block = rhs.data() + (row * rhs_stride) + (block_idx * num_bytes_per_block); |
| 201 | write_array<Float16>(block, 0, get_rhs_scale_value(row, block_idx)); |
| 202 | |
| 203 | auto* values = reinterpret_cast<uint8_t*>(block + sizeof(Float16)); |
| 204 | const size_t k_block_start = block_idx * bl; |
| 205 | for (size_t idx = 0; idx < bl / 2; ++idx) { |
| 206 | const uint8_t low = get_qsu4_rhs_value(row, k_block_start + idx); |
| 207 | const uint8_t high = get_qsu4_rhs_value(row, k_block_start + idx + (bl / 2)); |
| 208 | values[idx] = static_cast<uint8_t>((high << 4) | low); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return rhs; |
| 214 | } |
| 215 | |
| 216 | static Buffer reference_pack_s4s0sf16_rhs(const size_t n, const size_t k, const size_t nr, const size_t bl) { |
| 217 | const size_t num_blocks = k / bl; |
| 218 | const size_t num_bytes_per_block = (bl / 2) + sizeof(Float16); |
| 219 | const size_t rhs_packed_stride = nr * num_blocks * num_bytes_per_block; |
| 220 | const size_t packed_size = kai_roundup(n, nr) / nr * rhs_packed_stride; |
| 221 | const size_t packets_per_block = bl / 8; |
| 222 | const size_t scales_offset = rhs_packed_stride - (nr * num_blocks * sizeof(Float16)); |
| 223 | Buffer expected(packed_size); |
| 224 | |
| 225 | for (size_t group_start = 0; group_start < n; group_start += nr) { |
| 226 | const size_t rows_in_group = KAI_MIN(n - group_start, nr); |
| 227 | std::byte* group_base = expected.data() + ((group_start / nr) * rhs_packed_stride); |
| 228 | auto* group_packets = reinterpret_cast<uint32_t*>(group_base); |
| 229 | |
| 230 | for (size_t block_idx = 0; block_idx < num_blocks; ++block_idx) { |
| 231 | for (size_t packet_idx = 0; packet_idx < packets_per_block; ++packet_idx) { |
| 232 | for (size_t row_idx = 0; row_idx < rows_in_group; ++row_idx) { |
| 233 | const size_t row = group_start + row_idx; |
| 234 | const size_t k_packet_start = (block_idx * bl) + (packet_idx * 8); |
| 235 | uint32_t packed_packet = 0; |
| 236 | |
| 237 | for (size_t idx = 0; idx < 4; ++idx) { |
| 238 | const uint8_t low = get_qsu4_rhs_value(row, k_packet_start + idx); |
| 239 | const uint8_t high = get_qsu4_rhs_value(row, k_packet_start + idx + 4); |
| 240 | const uint8_t packed_byte = static_cast<uint8_t>(((high << 4) | low) ^ 0x88U); |
| 241 | packed_packet |= static_cast<uint32_t>(packed_byte) << (idx * 8); |
| 242 | } |
| 243 | |
| 244 | group_packets[(block_idx * packets_per_block + packet_idx) * nr + row_idx] = packed_packet; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | for (size_t row_idx = 0; row_idx < rows_in_group; ++row_idx) { |
| 249 | const size_t row = group_start + row_idx; |
| 250 | write_array<Float16>( |
| 251 | group_base + scales_offset, (block_idx * nr) + row_idx, |
| 252 | Float16(static_cast<float>(get_rhs_scale_value(row, block_idx)) / 16.0F)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return expected; |
| 258 | } |
| 259 | |
| 260 | TEST(RhsPackNxkQsi4c32pS4s0Sf16Neon, ScalarFallback) { |
| 261 | constexpr size_t n = 5; |
| 262 | constexpr size_t k = 32; |
| 263 | constexpr size_t nr = 2; |
| 264 | constexpr size_t kr = 4; |
| 265 | constexpr size_t sr = 2; |
| 266 | constexpr size_t bl = 32; |
| 267 | |
| 268 | const auto rhs = make_s4s0_rhs_with_scales(n, k, bl); |
| 269 | const auto expected = reference_pack_s4s0sf16_rhs(n, k, nr, bl); |
| 270 | Buffer packed(expected.size()); |
| 271 | |
| 272 | const kai_rhs_pack_qs4cxs1s0_param params{.lhs_zero_point = 1, .rhs_zero_point = 8}; |
| 273 | abi_check( |
| 274 | kai_run_rhs_pack_nxk_qsi4c32ps4s0sf16_qsu4c32s16s0_neon, 1, n, k, nr, kr, sr, bl, |
| 275 | reinterpret_cast<const uint8_t*>(rhs.data()), nullptr, packed.data(), 0, ¶ms); |
| 276 | |
| 277 | const auto* packed_bytes = reinterpret_cast<const uint8_t*>(packed.data()); |
| 278 | const auto* expected_bytes = reinterpret_cast<const uint8_t*>(expected.data()); |
| 279 | for (size_t idx = 0; idx < expected.size(); ++idx) { |
| 280 | ASSERT_EQ(packed_bytes[idx], expected_bytes[idx]) << "Mismatch at byte " << idx; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | TEST_P(MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, Offset_RHS) { |
| 285 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl, variable_bl] = GetParam(); |
| 286 | const auto& ukernel_variant = get_variant_entry(variant_index, variable_bl).variant; |
| 287 | |
| 288 | if (ukernel_variant.ukernel.fn_is_supported && !ukernel_variant.ukernel.fn_is_supported()) { |
| 289 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 290 | } |
| 291 | |
| 292 | const size_t M = matmul_shape.m; |
| 293 | const size_t N = matmul_shape.n; |
| 294 | const size_t K = matmul_shape.k; |
| 295 | |
| 296 | ASSERT_TRUE(K % bl == 0); |
| 297 | |
| 298 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 299 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 300 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 301 | |
| 302 | const auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 303 | const auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 304 | const auto tile_m = std::max(m_step, mr); |
| 305 | const auto tile_n = std::max(n_step, nr); |
| 306 | |
| 307 | const auto rect = portion.compute_portion(M, N, tile_m, tile_n); |
| 308 | if (rect.height() == 0 || rect.width() == 0) { |
| 309 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 310 | } |
| 311 | |
| 312 | const auto rhs_start_row = rect.start_col(); |
| 313 | auto rhs_packed_offset = ukernel_variant.rhs_pack_interface.get_packed_offset(rhs_start_row, K, nr, kr, bl); |
| 314 | auto rhs_matmul_offset = ukernel_variant.ukernel.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 315 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 316 | } |
| 317 | |
| 318 | TEST_P(MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, Offset_LHS) { |
| 319 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl, variable_bl] = GetParam(); |
| 320 | const auto& ukernel_variant = get_variant_entry(variant_index, variable_bl).variant; |
| 321 | |
| 322 | if (ukernel_variant.ukernel.fn_is_supported && !ukernel_variant.ukernel.fn_is_supported()) { |
| 323 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 324 | } |
| 325 | |
| 326 | const size_t M = matmul_shape.m; |
| 327 | const size_t N = matmul_shape.n; |
| 328 | const size_t K = matmul_shape.k; |
| 329 | |
| 330 | ASSERT_TRUE(K % bl == 0); |
| 331 | |
| 332 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 333 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 334 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 335 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 336 | |
| 337 | const auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 338 | const auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 339 | const auto tile_m = std::max(m_step, mr); |
| 340 | const auto tile_n = std::max(n_step, nr); |
| 341 | |
| 342 | const auto rect = portion.compute_portion(M, N, tile_m, tile_n); |
| 343 | if (rect.height() == 0 || rect.width() == 0) { |
| 344 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 345 | } |
| 346 | |
| 347 | const auto lhs_start_row = rect.start_row(); |
| 348 | auto lhs_packed_offset = ukernel_variant.lhs_pack_interface.get_packed_offset(lhs_start_row, K, bl, mr, kr, sr); |
| 349 | auto lhs_matmul_offset = ukernel_variant.ukernel.interface.get_lhs_packed_offset(lhs_start_row, K, bl); |
| 350 | |
| 351 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 352 | } |
| 353 | |
| 354 | TEST_P(MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, EndToEnd) { |
| 355 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl, variable_bl] = GetParam(); |
| 356 | const auto& ukernel_variant = get_variant_entry(variant_index, variable_bl).variant; |
| 357 | |
| 358 | if (ukernel_variant.ukernel.fn_is_supported && !ukernel_variant.ukernel.fn_is_supported()) { |
| 359 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 360 | } |
| 361 | |
| 362 | const std::uint32_t seed = 0; |
| 363 | |
| 364 | const size_t M = matmul_shape.m; |
| 365 | const size_t N = matmul_shape.n; |
| 366 | const size_t K = matmul_shape.k; |
| 367 | |
| 368 | ASSERT_TRUE(K % bl == 0); |
| 369 | |
| 370 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 371 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 372 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 373 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 374 | |
| 375 | ASSERT_TRUE(!(mr == 1 && M > 1)); |
| 376 | |
| 377 | auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 378 | ASSERT_TRUE(m_step % mr == 0); |
| 379 | |
| 380 | auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 381 | ASSERT_TRUE(n_step % nr == 0); |
| 382 | |
| 383 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 384 | if (rect.height() == 0 || rect.width() == 0) { |
| 385 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 386 | } |
| 387 | // Generates input data. |
| 388 | const auto ref_lhs = fill_random<float>(M * K, seed + 0); |
| 389 | const auto ref_rhs = fill_random<float>(N * K, seed + 1); |
| 390 | |
| 391 | // Runs the reference implementation. |
| 392 | QuantizationInfo lhs_qinfo{}; |
| 393 | lhs_qinfo.quant_width = bl; |
| 394 | lhs_qinfo.dst_type = DataType::QSI8; |
| 395 | lhs_qinfo.scale_type = DataType::FP16; |
| 396 | const auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref_lhs.data(), DataType::FP32, M, K, lhs_qinfo); |
| 397 | |
| 398 | QuantizationInfo rhs_qinfo{}; |
| 399 | rhs_qinfo.quant_width = bl; |
| 400 | rhs_qinfo.dst_type = DataType::QSI4; |
| 401 | rhs_qinfo.scale_type = DataType::FP16; |
| 402 | const auto [ref_rhs_quant, rhs_qoutputs] = quantize_dynamic(ref_rhs.data(), DataType::FP32, N, K, rhs_qinfo); |
| 403 | |
| 404 | const auto ref_dst = matmul_clamp_nt_t<int8_t, Float16, int32_t, Int4, Float16, int32_t, float, int32_t, float>( |
| 405 | M, N, K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), nullptr, bl, ref_rhs_quant.data(), |
| 406 | rhs_qoutputs.scales.data(), nullptr, bl, nullptr, std::numeric_limits<float>::lowest(), |
| 407 | std::numeric_limits<float>::max()); |
| 408 | |
| 409 | // Clamp reference output |
| 410 | const auto clamp_range = find_clamp_range<float>(ref_dst.data(), M * N, clamp_keep_ratio); |
| 411 | const float clamp_min = std::get<0>(clamp_range); |
| 412 | const float clamp_max = std::get<1>(clamp_range); |
| 413 | const auto out_clamped = clamp<float>(ref_dst.data(), M * N, clamp_min, clamp_max); |
| 414 | |
| 415 | // Runs the LHS packing micro-kernel. |
| 416 | const auto lhs_start_row = rect.start_row(); |
| 417 | const auto imp_packed_lhs_size = ukernel_variant.lhs_pack_interface.packed_size(M, K, bl, mr, kr, sr); |
| 418 | Buffer imp_packed_lhs(imp_packed_lhs_size); |
| 419 | |
| 420 | auto lhs_stride = K * sizeof(float); |
| 421 | auto lhs_offset = ukernel_variant.lhs_pack_interface.get_offset(lhs_start_row, lhs_stride); |
| 422 | auto lhs_packed_offset = ukernel_variant.lhs_pack_interface.get_packed_offset(lhs_start_row, K, bl, mr, kr, sr); |
| 423 | auto lhs_matmul_offset = ukernel_variant.ukernel.interface.get_lhs_packed_offset(lhs_start_row, K, bl); |
| 424 | |
| 425 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 426 | |
| 427 | abi_check( |
| 428 | ukernel_variant.lhs_pack_interface.run_pack, rect.height() /* m */, K, bl, mr, kr, sr, 0, |
| 429 | reinterpret_cast<const float*>(ref_lhs.data() + lhs_offset), lhs_stride, |
| 430 | imp_packed_lhs.data() + lhs_packed_offset); |
| 431 | |
| 432 | // Runs the RHS packing micro-kernel. |
| 433 | const auto ref_rhs_qsu4 = cast_qsu4_qsi4(ref_rhs_quant.data(), N * K); |
| 434 | const auto ref_rhs_qsu4_scale_f16 = |
| 435 | pack_data_scales_interleave_block<UInt4, Float16>(ref_rhs_qsu4.data(), rhs_qoutputs.scales.data(), N, K, bl); |
| 436 | |
| 437 | const auto imp_packed_rhs_size = ukernel_variant.rhs_pack_interface.packed_size(N, K, nr, kr, bl); |
| 438 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 439 | const auto rhs_start_row = rect.start_col(); |
| 440 | auto rhs_packed_offset = ukernel_variant.rhs_pack_interface.get_packed_offset(rhs_start_row, K, nr, kr, bl); |
| 441 | auto rhs_matmul_offset = ukernel_variant.ukernel.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 442 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 443 | |
| 444 | const kai_rhs_pack_qs4cxs1s0_param params{.lhs_zero_point = 1, .rhs_zero_point = 8}; |
| 445 | abi_check( |
| 446 | ukernel_variant.rhs_pack_interface.run_pack, 1, N, K, nr, kr, sr, bl, |
| 447 | reinterpret_cast<const uint8_t*>(ref_rhs_qsu4_scale_f16.data()), nullptr, imp_packed_rhs.data(), 0, ¶ms); |
| 448 | |
| 449 | const auto dst_stride_row = N * sizeof(float); |
| 450 | const auto dst_stride_col = sizeof(float); |
| 451 | const auto dst_offset = |
| 452 | ukernel_variant.ukernel.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row); |
| 453 | |
| 454 | const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col; |
| 455 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 456 | |
| 457 | // Runs the GEMM micro-kernel. |
| 458 | const auto imp_dst_size = ukernel_variant.ukernel.interface.get_dst_size(M, N); |
| 459 | ASSERT_EQ(imp_dst_size, ref_dst.size()); |
| 460 | Buffer imp_dst(imp_dst_size); |
| 461 | abi_check( |
| 462 | ukernel_variant.ukernel.interface.run_matmul, rect.height(), rect.width(), K, bl, |
| 463 | imp_packed_lhs.data() + lhs_matmul_offset, imp_packed_rhs.data() + rhs_matmul_offset, |
| 464 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), dst_stride_row, dst_stride_col, clamp_min, clamp_max); |
| 465 | |
| 466 | DefaultMismatchHandler handler(0, 0.02, 0, 0.05); |
| 467 | const auto success = compare(imp_dst.data(), out_clamped.data(), DataType::FP32, M, N, rect, handler); |
| 468 | |
| 469 | ASSERT_TRUE(success); |
| 470 | |
| 471 | // The standard clamping test uses finite min and max values. Exercise one-sided clamping for the SME MOPA path. |
| 472 | if (variant_index == 3 && !variable_bl && M == 1 && N == 40 && K == 32 && rect.height() == M && rect.width() == N && |
| 473 | clamp_keep_ratio == 1.0F) { |
| 474 | const auto one_sided_clamp_range = find_clamp_range<float>(ref_dst.data(), M * N, 0.5F); |
| 475 | const std::array<std::tuple<float, float>, 2> one_sided_clamp_bounds = {{ |
| 476 | {std::get<0>(one_sided_clamp_range), std::numeric_limits<float>::max()}, |
| 477 | {std::numeric_limits<float>::lowest(), std::get<1>(one_sided_clamp_range)}, |
| 478 | }}; |
| 479 | |
| 480 | for (const auto& [one_sided_clamp_min, one_sided_clamp_max] : one_sided_clamp_bounds) { |
| 481 | SCOPED_TRACE( |
| 482 | "one_sided_clamp_min=" + std::to_string(one_sided_clamp_min) + |
| 483 | " one_sided_clamp_max=" + std::to_string(one_sided_clamp_max)); |
| 484 | |
| 485 | const auto one_sided_out_clamped = |
| 486 | clamp<float>(ref_dst.data(), M * N, one_sided_clamp_min, one_sided_clamp_max); |
| 487 | Buffer one_sided_imp_dst(imp_dst_size); |
| 488 | abi_check( |
| 489 | ukernel_variant.ukernel.interface.run_matmul, rect.height(), rect.width(), K, bl, |
| 490 | imp_packed_lhs.data() + lhs_matmul_offset, imp_packed_rhs.data() + rhs_matmul_offset, |
| 491 | reinterpret_cast<float*>(one_sided_imp_dst.data() + dst_offset), dst_stride_row, dst_stride_col, |
| 492 | one_sided_clamp_min, one_sided_clamp_max); |
| 493 | |
| 494 | const auto one_sided_success = |
| 495 | compare(one_sided_imp_dst.data(), one_sided_out_clamped.data(), DataType::FP32, M, N, rect, handler); |
| 496 | ASSERT_TRUE(one_sided_success); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static std::vector<size_t> variant_indices_with_mr(const bool variable_bl, const bool mr_is_1) { |
| 502 | const size_t num_variants = variable_bl ? variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size() |
| 503 | : variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p.size(); |
| 504 | std::vector<size_t> indices; |
| 505 | for (size_t variant_idx = 0; variant_idx < num_variants; ++variant_idx) { |
| 506 | const auto& variant = get_variant_entry(variant_idx, variable_bl).variant.ukernel; |
| 507 | if (variant.fn_is_supported && !variant.fn_is_supported()) { |
| 508 | continue; |
| 509 | } |
| 510 | const auto mr = variant.interface.get_mr(); |
| 511 | if ((mr == 1) == mr_is_1) { |
| 512 | indices.emplace_back(variant_idx); |
| 513 | } |
| 514 | } |
| 515 | return indices; |
| 516 | } |
| 517 | |
| 518 | static std::vector<size_t> unsupported_variant_indices(const bool variable_bl) { |
| 519 | const size_t num_variants = variable_bl ? variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size() |
| 520 | : variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p.size(); |
| 521 | std::vector<size_t> indices; |
| 522 | for (size_t variant_idx = 0; variant_idx < num_variants; ++variant_idx) { |
| 523 | const auto& variant = get_variant_entry(variant_idx, variable_bl).variant.ukernel; |
| 524 | if (variant.fn_is_supported && !variant.fn_is_supported()) { |
| 525 | indices.emplace_back(variant_idx); |
| 526 | } |
| 527 | } |
| 528 | return indices; |
| 529 | } |
| 530 | |
| 531 | template <size_t N> |
| 532 | static std::vector<MatMulShape> shapes_with_mr_support(const std::array<MatMulShape, N>& shapes, const bool mr_is_1) { |
| 533 | std::vector<MatMulShape> filtered_shapes; |
| 534 | for (const auto shape : shapes) { |
| 535 | if (!mr_is_1 || shape.m == 1) { |
| 536 | filtered_shapes.emplace_back(shape); |
| 537 | } |
| 538 | } |
| 539 | return filtered_shapes; |
| 540 | } |
| 541 | |
| 542 | static constexpr std::array fixed_shapes{ |
| 543 | MatMulShape{1, 2, 32}, // |
| 544 | MatMulShape{1, 40, 32}, // |
| 545 | MatMulShape{1, 33, 32}, // |
| 546 | MatMulShape{32, 64, 64}, // |
| 547 | MatMulShape{16, 32, 64}, // |
| 548 | MatMulShape{8, 32, 64}, // |
| 549 | MatMulShape{15, 32, 32}, // |
| 550 | MatMulShape{77, 99, 64}, |
| 551 | }; |
| 552 | |
| 553 | static const std::array fixed_portions{ |
| 554 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 555 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 556 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 557 | MatrixPortion(0, 0.5, 1, 0.8) // Somewhere Middle |
| 558 | }; |
| 559 | |
| 560 | static const auto fixed_test_name = [](const auto& info) { |
| 561 | const auto variant_idx = std::get<0>(info.param); |
| 562 | const std::string name{variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p.at(variant_idx).variant.ukernel.name}; |
| 563 | const auto shape = std::get<MatMulShape>(info.param); |
| 564 | const auto portion = std::get<2>(info.param); |
| 565 | const auto clamp_keep_ratio = std::get<3>(info.param); |
| 566 | const auto bl = std::get<4>(info.param); |
| 567 | |
| 568 | return test_description(name, shape, portion, true, clamp_keep_ratio) + "_bl" + std::to_string(bl); |
| 569 | }; |
| 570 | |
| 571 | // Keep one skipped test per unavailable kernel in JUnit without calling get_mr() on unsupported hardware. |
| 572 | INSTANTIATE_TEST_SUITE_P( |
| 573 | UnsupportedFixedCPU, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 574 | testing::Combine( |
| 575 | testing::ValuesIn(unsupported_variant_indices(false)), testing::Values(MatMulShape{1, 2, 32}), |
| 576 | testing::Values(MatrixPortion(0, 0, 1, 1)), testing::Values(std::optional<float>{}), testing::Values(32), |
| 577 | testing::Values(false)), |
| 578 | testing::PrintToStringParamName()); |
| 579 | |
| 580 | INSTANTIATE_TEST_SUITE_P( |
| 581 | UnsupportedVariableCPU, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 582 | testing::Combine( |
| 583 | testing::ValuesIn(unsupported_variant_indices(true)), testing::Values(MatMulShape{1, 2, 32}), |
| 584 | testing::Values(MatrixPortion(0, 0, 1, 1)), testing::Values(std::optional<float>{}), testing::Values(32), |
| 585 | testing::Values(true)), |
| 586 | testing::PrintToStringParamName()); |
| 587 | |
| 588 | INSTANTIATE_TEST_SUITE_P( |
| 589 | MatMul, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 590 | testing::Combine( |
| 591 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p.size()), |
| 592 | testing::ValuesIn(fixed_shapes), testing::ValuesIn(fixed_portions), |
| 593 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 594 | std::nullopt, // Disable clamping |
| 595 | 1.0f, // Clamp to full range |
| 596 | 0.9f, // Clamp to 90% range |
| 597 | 0.5f}), // Clamp to 50% range |
| 598 | testing::Values(32), testing::Values(false)), |
| 599 | fixed_test_name); |
| 600 | |
| 601 | INSTANTIATE_TEST_SUITE_P( |
| 602 | MatMulMr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 603 | testing::Combine( |
| 604 | testing::ValuesIn(variant_indices_with_mr(false, true)), |
| 605 | testing::ValuesIn(shapes_with_mr_support(fixed_shapes, true)), testing::ValuesIn(fixed_portions), |
| 606 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 607 | std::nullopt, // Disable clamping |
| 608 | 1.0f, // Clamp to full range |
| 609 | 0.9f, // Clamp to 90% range |
| 610 | 0.5f}), // Clamp to 50% range |
| 611 | testing::Values(32), testing::Values(false)), |
| 612 | fixed_test_name); |
| 613 | |
| 614 | INSTANTIATE_TEST_SUITE_P( |
| 615 | MatMulMrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 616 | testing::Combine( |
| 617 | testing::ValuesIn(variant_indices_with_mr(false, false)), testing::ValuesIn(fixed_shapes), |
| 618 | testing::ValuesIn(fixed_portions), |
| 619 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 620 | std::nullopt, // Disable clamping |
| 621 | 1.0f, // Clamp to full range |
| 622 | 0.9f, // Clamp to 90% range |
| 623 | 0.5f}), // Clamp to 50% range |
| 624 | testing::Values(32), testing::Values(false)), |
| 625 | fixed_test_name); |
| 626 | |
| 627 | // Test kernels with variable block length support |
| 628 | static constexpr std::array shapes_k32{ |
| 629 | MatMulShape{1, 2, 32}, // |
| 630 | MatMulShape{1, 40, 32}, // |
| 631 | MatMulShape{1, 33, 32}, // |
| 632 | MatMulShape{1, 71, 32}, // |
| 633 | MatMulShape{15, 32, 32}, // |
| 634 | MatMulShape{32, 64, 64}, // |
| 635 | MatMulShape{16, 32, 64}, // |
| 636 | MatMulShape{32, 64, 128}, // |
| 637 | MatMulShape{32, 64, 256}, // |
| 638 | MatMulShape{77, 99, 256}, |
| 639 | }; |
| 640 | static constexpr std::array shapes_k64{ |
| 641 | MatMulShape{1, 2, 64}, // |
| 642 | MatMulShape{32, 64, 64}, // |
| 643 | MatMulShape{16, 32, 64}, // |
| 644 | MatMulShape{8, 32, 64}, // |
| 645 | MatMulShape{77, 99, 64}, // |
| 646 | MatMulShape{32, 64, 128}, // |
| 647 | MatMulShape{16, 32, 128}, // |
| 648 | MatMulShape{32, 64, 256}, // |
| 649 | MatMulShape{77, 99, 256}, |
| 650 | }; |
| 651 | static constexpr std::array shapes_k96{ |
| 652 | MatMulShape{32, 64, 96}, // |
| 653 | MatMulShape{16, 32, 96}, // |
| 654 | MatMulShape{8, 32, 96}, // |
| 655 | MatMulShape{77, 99, 96}, // |
| 656 | MatMulShape{32, 64, 192}, // |
| 657 | MatMulShape{16, 32, 192}, // |
| 658 | MatMulShape{32, 64, 288}, // |
| 659 | MatMulShape{77, 99, 288}, |
| 660 | }; |
| 661 | static constexpr std::array shapes_k128{ |
| 662 | MatMulShape{1, 2, 128}, // |
| 663 | MatMulShape{32, 64, 128}, // |
| 664 | MatMulShape{16, 32, 128}, // |
| 665 | MatMulShape{8, 32, 128}, // |
| 666 | MatMulShape{77, 99, 128}, // |
| 667 | MatMulShape{32, 64, 256}, // |
| 668 | MatMulShape{77, 99, 256}, |
| 669 | }; |
| 670 | static constexpr std::array shapes_k256{ |
| 671 | MatMulShape{1, 2, 256}, // |
| 672 | MatMulShape{32, 64, 256}, // |
| 673 | MatMulShape{16, 32, 256}, // |
| 674 | MatMulShape{8, 32, 256}, // |
| 675 | MatMulShape{77, 99, 256}, |
| 676 | }; |
| 677 | static constexpr std::array portions{ |
| 678 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 679 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 680 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 681 | MatrixPortion(0, 0.5, 1, 0.8) // Somewhere Middle |
| 682 | }; |
| 683 | INSTANTIATE_TEST_SUITE_P( |
| 684 | MatMulVariableBL_bl32, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 685 | testing::Combine( |
| 686 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size()), |
| 687 | testing::ValuesIn(shapes_k32), testing::ValuesIn(portions), |
| 688 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 689 | std::nullopt, // Disable clamping |
| 690 | 1.0F, // Clamp to full range |
| 691 | 0.9F, // Clamp to 90% range |
| 692 | 0.5F, // Clamp to 50% range |
| 693 | }), |
| 694 | testing::Values(32), testing::Values(true)), |
| 695 | testing::PrintToStringParamName()); |
| 696 | |
| 697 | INSTANTIATE_TEST_SUITE_P( |
| 698 | MatMulVariableBL_bl64, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 699 | testing::Combine( |
| 700 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size()), |
| 701 | testing::ValuesIn(shapes_k64), testing::ValuesIn(portions), |
| 702 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 703 | std::nullopt, // Disable clamping |
| 704 | 1.0F, // Clamp to full range |
| 705 | 0.9F, // Clamp to 90% range |
| 706 | 0.5F, // Clamp to 50% range |
| 707 | }), |
| 708 | testing::Values(64), testing::Values(true)), |
| 709 | testing::PrintToStringParamName()); |
| 710 | |
| 711 | INSTANTIATE_TEST_SUITE_P( |
| 712 | MatMulVariableBL_bl96, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 713 | testing::Combine( |
| 714 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size()), |
| 715 | testing::ValuesIn(shapes_k96), testing::ValuesIn(portions), |
| 716 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 717 | std::nullopt, // Disable clamping |
| 718 | 1.0F, // Clamp to full range |
| 719 | 0.9F, // Clamp to 90% range |
| 720 | 0.5F, // Clamp to 50% range |
| 721 | }), |
| 722 | testing::Values(96), testing::Values(true)), |
| 723 | testing::PrintToStringParamName()); |
| 724 | |
| 725 | INSTANTIATE_TEST_SUITE_P( |
| 726 | MatMulVariableBL_bl128, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 727 | testing::Combine( |
| 728 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size()), |
| 729 | testing::ValuesIn(shapes_k128), testing::ValuesIn(portions), |
| 730 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 731 | std::nullopt, // Disable clamping |
| 732 | 1.0F, // Clamp to full range |
| 733 | 0.9F, // Clamp to 90% range |
| 734 | 0.5F, // Clamp to 50% range |
| 735 | }), |
| 736 | testing::Values(128), testing::Values(true)), |
| 737 | testing::PrintToStringParamName()); |
| 738 | |
| 739 | INSTANTIATE_TEST_SUITE_P( |
| 740 | MatMulVariableBL_bl256, MatMulTest_f32_qsi8d32p_qsi4c32p_Offset, |
| 741 | testing::Combine( |
| 742 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qsi4c32p_variable_bl.size()), |
| 743 | testing::ValuesIn(shapes_k256), testing::ValuesIn(portions), |
| 744 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 745 | std::nullopt, // Disable clamping |
| 746 | 1.0f, // Clamp to full range |
| 747 | 0.9f, // Clamp to 90% range |
| 748 | 0.5f}), // Clamp to 50% range |
| 749 | testing::Values(256), testing::Values(true)), |
| 750 | testing::PrintToStringParamName()); |
| 751 | |
| 752 | INSTANTIATE_TEST_SUITE_P( |
| 753 | MatMulVariableBL_bl32_Mr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 754 | testing::Combine( |
| 755 | testing::ValuesIn(variant_indices_with_mr(true, true)), |
| 756 | testing::ValuesIn(shapes_with_mr_support(shapes_k32, true)), testing::ValuesIn(portions), |
| 757 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 758 | std::nullopt, // Disable clamping |
| 759 | 1.0F, // Clamp to full range |
| 760 | 0.9F, // Clamp to 90% range |
| 761 | 0.5F}), // Clamp to 50% range |
| 762 | testing::Values(32), testing::Values(true)), |
| 763 | testing::PrintToStringParamName()); |
| 764 | |
| 765 | INSTANTIATE_TEST_SUITE_P( |
| 766 | MatMulVariableBL_bl32_MrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 767 | testing::Combine( |
| 768 | testing::ValuesIn(variant_indices_with_mr(true, false)), testing::ValuesIn(shapes_k32), |
| 769 | testing::ValuesIn(portions), |
| 770 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 771 | std::nullopt, // Disable clamping |
| 772 | 1.0F, // Clamp to full range |
| 773 | 0.9F, // Clamp to 90% range |
| 774 | 0.5F}), // Clamp to 50% range |
| 775 | testing::Values(32), testing::Values(true)), |
| 776 | testing::PrintToStringParamName()); |
| 777 | |
| 778 | INSTANTIATE_TEST_SUITE_P( |
| 779 | MatMulVariableBL_bl64_Mr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 780 | testing::Combine( |
| 781 | testing::ValuesIn(variant_indices_with_mr(true, true)), |
| 782 | testing::ValuesIn(shapes_with_mr_support(shapes_k64, true)), testing::ValuesIn(portions), |
| 783 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 784 | std::nullopt, // Disable clamping |
| 785 | 1.0F, // Clamp to full range |
| 786 | 0.9F, // Clamp to 90% range |
| 787 | 0.5F}), // Clamp to 50% range |
| 788 | testing::Values(64), testing::Values(true)), |
| 789 | testing::PrintToStringParamName()); |
| 790 | |
| 791 | INSTANTIATE_TEST_SUITE_P( |
| 792 | MatMulVariableBL_bl64_MrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 793 | testing::Combine( |
| 794 | testing::ValuesIn(variant_indices_with_mr(true, false)), testing::ValuesIn(shapes_k64), |
| 795 | testing::ValuesIn(portions), |
| 796 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 797 | std::nullopt, // Disable clamping |
| 798 | 1.0F, // Clamp to full range |
| 799 | 0.9F, // Clamp to 90% range |
| 800 | 0.5F}), // Clamp to 50% range |
| 801 | testing::Values(64), testing::Values(true)), |
| 802 | testing::PrintToStringParamName()); |
| 803 | |
| 804 | INSTANTIATE_TEST_SUITE_P( |
| 805 | MatMulVariableBL_bl96_Mr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 806 | testing::Combine( |
| 807 | testing::ValuesIn(variant_indices_with_mr(true, true)), |
| 808 | testing::ValuesIn(shapes_with_mr_support(shapes_k96, true)), testing::ValuesIn(portions), |
| 809 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 810 | std::nullopt, // Disable clamping |
| 811 | 1.0F, // Clamp to full range |
| 812 | 0.9F, // Clamp to 90% range |
| 813 | 0.5F}), // Clamp to 50% range |
| 814 | testing::Values(96), testing::Values(true)), |
| 815 | testing::PrintToStringParamName()); |
| 816 | |
| 817 | INSTANTIATE_TEST_SUITE_P( |
| 818 | MatMulVariableBL_bl96_MrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 819 | testing::Combine( |
| 820 | testing::ValuesIn(variant_indices_with_mr(true, false)), testing::ValuesIn(shapes_k96), |
| 821 | testing::ValuesIn(portions), |
| 822 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 823 | std::nullopt, // Disable clamping |
| 824 | 1.0F, // Clamp to full range |
| 825 | 0.9F, // Clamp to 90% range |
| 826 | 0.5F}), // Clamp to 50% range |
| 827 | testing::Values(96), testing::Values(true)), |
| 828 | testing::PrintToStringParamName()); |
| 829 | |
| 830 | INSTANTIATE_TEST_SUITE_P( |
| 831 | MatMulVariableBL_bl128_Mr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 832 | testing::Combine( |
| 833 | testing::ValuesIn(variant_indices_with_mr(true, true)), |
| 834 | testing::ValuesIn(shapes_with_mr_support(shapes_k128, true)), testing::ValuesIn(portions), |
| 835 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 836 | std::nullopt, // Disable clamping |
| 837 | 1.0F, // Clamp to full range |
| 838 | 0.9F, // Clamp to 90% range |
| 839 | 0.5F}), // Clamp to 50% range |
| 840 | testing::Values(128), testing::Values(true)), |
| 841 | testing::PrintToStringParamName()); |
| 842 | |
| 843 | INSTANTIATE_TEST_SUITE_P( |
| 844 | MatMulVariableBL_bl128_MrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 845 | testing::Combine( |
| 846 | testing::ValuesIn(variant_indices_with_mr(true, false)), testing::ValuesIn(shapes_k128), |
| 847 | testing::ValuesIn(portions), |
| 848 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 849 | std::nullopt, // Disable clamping |
| 850 | 1.0F, // Clamp to full range |
| 851 | 0.9F, // Clamp to 90% range |
| 852 | 0.5F}), // Clamp to 50% range |
| 853 | testing::Values(128), testing::Values(true)), |
| 854 | testing::PrintToStringParamName()); |
| 855 | |
| 856 | INSTANTIATE_TEST_SUITE_P( |
| 857 | MatMulVariableBL_bl256_Mr1, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 858 | testing::Combine( |
| 859 | testing::ValuesIn(variant_indices_with_mr(true, true)), |
| 860 | testing::ValuesIn(shapes_with_mr_support(shapes_k256, true)), testing::ValuesIn(portions), |
| 861 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 862 | std::nullopt, // Disable clamping |
| 863 | 1.0F, // Clamp to full range |
| 864 | 0.9F, // Clamp to 90% range |
| 865 | 0.5F}), // Clamp to 50% range |
| 866 | testing::Values(256), testing::Values(true)), |
| 867 | testing::PrintToStringParamName()); |
| 868 | |
| 869 | INSTANTIATE_TEST_SUITE_P( |
| 870 | MatMulVariableBL_bl256_MrN, MatMulTest_f32_qsi8d32p_qsi4c32p_EndToEnd, |
| 871 | testing::Combine( |
| 872 | testing::ValuesIn(variant_indices_with_mr(true, false)), testing::ValuesIn(shapes_k256), |
| 873 | testing::ValuesIn(portions), |
| 874 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 875 | std::nullopt, // Disable clamping |
| 876 | 1.0F, // Clamp to full range |
| 877 | 0.9F, // Clamp to 90% range |
| 878 | 0.5F}), // Clamp to 50% range |
| 879 | testing::Values(256), testing::Values(true)), |
| 880 | testing::PrintToStringParamName()); |
| 881 | |
| 882 | } // namespace kai::test |