| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
| 12 | #include <cstdlib> |
| 13 | #include <sstream> |
| 14 | #include <string> |
| 15 | #include <tuple> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa.h" |
| 19 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qai4c32p4vlx4_1x4vl_sme2_dot.h" |
| 20 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qai4c32p4x4_1x4_neon_dotprod.h" |
| 21 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod.h" |
| 22 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod.h" |
| 23 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm.h" |
| 24 | #include "kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qai4c32p/kai_matmul_clamp_f32_qsi8d32p_qai4c32p_interface.h" |
| 25 | #include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32pscalef32_f32_neon.h" |
| 26 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon.h" |
| 27 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s0s1_f32_f32_f32_neon.h" |
| 28 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon.h" |
| 29 | #include "test/common/abi_checker.hpp" |
| 30 | #include "test/common/buffer.hpp" |
| 31 | #include "test/common/compare.hpp" |
| 32 | #include "test/common/cpu_info.hpp" |
| 33 | #include "test/common/int4.hpp" |
| 34 | #include "test/common/matmul_test_common.hpp" |
| 35 | #include "test/common/matrix_portion.hpp" |
| 36 | #include "test/common/round.hpp" |
| 37 | #include "test/common/seed.hpp" |
| 38 | #include "test/common/test_suite.hpp" |
| 39 | #include "test/reference/cast.hpp" |
| 40 | #include "test/reference/clamp.hpp" |
| 41 | #include "test/reference/fill.hpp" |
| 42 | #include "test/reference/matmul.hpp" |
| 43 | #include "test/reference/pack.hpp" |
| 44 | #include "test/reference/quantize.hpp" |
| 45 | |
| 46 | namespace kai::test { |
| 47 | // Interface for the LHS and RHS packed size and packing micro-kernels |
| 48 | using kai_get_lhs_packed_size_func_t = decltype(&kai_get_lhs_packed_size_lhs_quant_pack_qsi8d32pscalef32_f32_neon); |
| 49 | using kai_get_rhs_packed_size_func_t = |
| 50 | decltype(&kai_get_rhs_packed_size_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon); |
| 51 | using kai_get_lhs_packed_offset_func_t = decltype(&kai_get_lhs_packed_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon); |
| 52 | using kai_get_rhs_packed_offset_func_t = |
| 53 | decltype(&kai_get_rhs_packed_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon); |
| 54 | using kai_get_lhs_offset_func_t = decltype(&kai_get_lhs_offset_lhs_quant_pack_qsi8d32pscalef32_f32_neon); |
| 55 | using kai_get_rhs_offset_func_t = decltype(&kai_get_rhs_offset_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon); |
| 56 | using kai_run_lhs_pack_func_t = decltype(&kai_run_lhs_quant_pack_qsi8d32pscalef32_f32_neon); |
| 57 | using kai_run_rhs_pack_func_t = decltype(&kai_run_rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon); |
| 58 | |
| 59 | // Micro-kernel interface |
| 60 | struct kai_qai4c32p_pack_functions { |
| 61 | kai_get_rhs_packed_size_func_t packed_size; |
| 62 | kai_get_rhs_packed_offset_func_t get_packed_offset; |
| 63 | kai_get_rhs_offset_func_t get_offset; |
| 64 | kai_run_rhs_pack_func_t run_pack; |
| 65 | }; |
| 66 | |
| 67 | struct kai_qsi8d32p_pack_functions { |
| 68 | kai_get_lhs_packed_size_func_t packed_size; |
| 69 | kai_get_lhs_packed_offset_func_t get_packed_offset; |
| 70 | kai_get_lhs_offset_func_t get_offset; |
| 71 | kai_run_lhs_pack_func_t run_pack; |
| 72 | }; |
| 73 | |
| 74 | static const std::array< |
| 75 | UkernelMatmulPackVariant< |
| 76 | kai_matmul_clamp_f32_qsi8d32p_qai4c32p_ukernel, kai_qsi8d32p_pack_functions, kai_qai4c32p_pack_functions>, |
| 77 | 8> |
| 78 | variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p = { |
| 79 | {UKERNEL_MATMUL_PACK_VARIANT( |
| 80 | clamp_f32_qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod, cpu_has_dotprod, |
| 81 | lhs_quant_pack_qsi8d32pscalef32_f32_neon, rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon, true), |
| 82 | UKERNEL_MATMUL_PACK_VARIANT( |
| 83 | clamp_f32_qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm, cpu_has_i8mm, lhs_quant_pack_qsi8d32pscalef32_f32_neon, |
| 84 | rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon, true), |
| 85 | UKERNEL_MATMUL_PACK_VARIANT( |
| 86 | clamp_f32_qsi8d32p4x4_qai4c32p4x4_8x4_neon_dotprod, cpu_has_dotprod, |
| 87 | lhs_quant_pack_qsi8d32pscalef32_f32_neon, rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon, true), |
| 88 | UKERNEL_MATMUL_PACK_VARIANT( |
| 89 | clamp_f32_qsi8d32p1x4_qai4c32p4x4_1x4_neon_dotprod, cpu_has_dotprod, |
| 90 | lhs_quant_pack_qsi8d32pscalef32_f32_neon, rhs_pack_nxk_qai4c32p_qau4c32s0s1_f32_f32_f32_neon, true), |
| 91 | UKERNEL_MATMUL_PACK_VARIANT( |
| 92 | clamp_f32_qsi8d32p1x4_qai4c32p4vlx4_1x4vl_sme2_dot, cpu_has_sme2, lhs_quant_pack_qsi8d32pscalef32_f32_neon, |
| 93 | rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon, false), |
| 94 | UKERNEL_MATMUL_PACK_VARIANT( |
| 95 | clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa, cpu_has_sme2, |
| 96 | lhs_quant_pack_qsi8d32pscalef32_f32_neon, rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s1s0_f32_f32_f32_neon, |
| 97 | false), |
| 98 | UKERNEL_MATMUL_PACK_VARIANT( |
| 99 | clamp_f32_qsi8d32p1x4_qai4c32p4vlx4_1x4vl_sme2_dot, cpu_has_sme2, lhs_quant_pack_qsi8d32pscalef32_f32_neon, |
| 100 | rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s0s1_f32_f32_f32_neon, true), |
| 101 | UKERNEL_MATMUL_PACK_VARIANT( |
| 102 | clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa, cpu_has_sme2, |
| 103 | lhs_quant_pack_qsi8d32pscalef32_f32_neon, rhs_pack_nxk_qai4c32ps1s0nrx4_qau4c32s0s1_f32_f32_f32_neon, |
| 104 | true)}}; |
| 105 | |
| 106 | // Executes the LHS packing micro-kernel. |
| 107 | static inline std::tuple<Buffer, size_t> pack_lhs_qsi8d32p( |
| 108 | const kai_qsi8d32p_pack_functions& pack_interface, size_t M, size_t K, size_t bl, size_t mr, size_t kr, size_t sr, |
| 109 | const Buffer& lhs_values_qsi8, size_t stride, size_t rect_start_row, size_t rect_height) { |
| 110 | const auto imp_packed_lhs_size = pack_interface.packed_size(M, K, bl, mr, kr, sr); |
| 111 | Buffer imp_packed_lhs(imp_packed_lhs_size, 0); |
| 112 | |
| 113 | auto lhs_offset = pack_interface.get_offset(rect_start_row, stride); |
| 114 | auto lhs_packed_offset = pack_interface.get_packed_offset(rect_start_row, K, bl, mr, kr, sr); |
| 115 | |
| 116 | abi_check( |
| 117 | pack_interface.run_pack, rect_height, K, bl, mr, kr, sr, 0, |
| 118 | reinterpret_cast<const float*>(lhs_values_qsi8.data() + lhs_offset), stride, |
| 119 | imp_packed_lhs.data() + lhs_packed_offset); |
| 120 | |
| 121 | return {std::move(imp_packed_lhs), lhs_packed_offset}; |
| 122 | } |
| 123 | |
| 124 | // Executes the RHS packing micro-kernel. |
| 125 | static inline std::tuple<Buffer, size_t> pack_rhs_qai4c32p( |
| 126 | const kai_qai4c32p_pack_functions& pack_interface, size_t N, size_t K, size_t bl, size_t nr, size_t kr, size_t sr, |
| 127 | const Buffer& rhs_values_qai4, const bool has_bias, const Buffer& biases, const Buffer& rhs_scales, |
| 128 | const Buffer& rhs_zp, bool s0s1_input, size_t rect_start_row) { |
| 129 | // Cast to unsigned int |
| 130 | auto rhs_qau4s1s0 = cast_qsu4_qsi4(rhs_values_qai4.data(), N * K); |
| 131 | |
| 132 | const auto imp_packed_rhs_size = pack_interface.packed_size(N, K, nr, kr, bl); |
| 133 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 134 | auto rhs_packed_offset = pack_interface.get_packed_offset(rect_start_row, K, nr, kr, bl); |
| 135 | |
| 136 | // Runs the RHS packing micro-kernel. |
| 137 | kai_rhs_pack_nxk_qai4c32p_params params{}; |
| 138 | params.lhs_zero_point = 1; |
| 139 | params.rhs_zero_point = 8; |
| 140 | |
| 141 | abi_check( |
| 142 | pack_interface.run_pack, 1, N, K, nr, kr, sr, bl, |
| 143 | reinterpret_cast<const uint8_t*>(s0s1_input ? convert_s0s1_s1s0(rhs_qau4s1s0).data() : rhs_qau4s1s0.data()), |
| 144 | rhs_zp.data(), has_bias ? biases.data() : nullptr, rhs_scales.data(), imp_packed_rhs.data(), 0, ¶ms); |
| 145 | |
| 146 | return {std::move(imp_packed_rhs), rhs_packed_offset}; |
| 147 | } |
| 148 | |
| 149 | using MatMulTestClampPortionedParamsWithBias_WithBL = |
| 150 | std::tuple<size_t, MatMulShape, size_t, MatrixPortion, std::optional<float>, bool>; |
| 151 | class MatMulTest_f32_qsi8d32p_qai4c32p_LhsPacked |
| 152 | : public ::testing::TestWithParam<MatMulTestClampPortionedParamsWithBias_WithBL> {}; |
| 153 | class MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd |
| 154 | : public ::testing::TestWithParam<MatMulTestClampPortionedParamsWithBias_WithBL> {}; |
| 155 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd); |
| 156 | |
| 157 | TEST_P(MatMulTest_f32_qsi8d32p_qai4c32p_LhsPacked, LhsPackedWithSameBlockdepth) { |
| 158 | // Verify LHS quant and pack int8 kernel behaves same for int4 and int8 matmul kernels, |
| 159 | // when the block-depth is same for different values of kr, sr. |
| 160 | |
| 161 | const auto& [variant_index, matmul_shape, bl, portion, clamp_keep_ratio, has_bias] = GetParam(); |
| 162 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_index); |
| 163 | |
| 164 | if (ukernel_variant.ukernel.fn_is_supported && !ukernel_variant.ukernel.fn_is_supported()) { |
| 165 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 166 | } |
| 167 | |
| 168 | const size_t M = matmul_shape.m; |
| 169 | const size_t N = matmul_shape.n; |
| 170 | const size_t K = matmul_shape.k; |
| 171 | |
| 172 | ASSERT_EQ(K % bl, size_t{0}); |
| 173 | |
| 174 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 175 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 176 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 177 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 178 | |
| 179 | auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 180 | ASSERT_TRUE(m_step % mr == 0); |
| 181 | |
| 182 | auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 183 | ASSERT_TRUE(n_step % nr == 0); |
| 184 | |
| 185 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 186 | |
| 187 | // Generates input data. |
| 188 | const auto ref_lhs = fill_random<float>(M * K, seed_stream(current_test_key())()); |
| 189 | |
| 190 | // Runs the LHS packing micro-kernel. |
| 191 | const auto lhs_start_row = rect.start_row(); |
| 192 | auto lhs_stride = K * sizeof(float); |
| 193 | |
| 194 | auto [imp_packed_lhs, lhs_packed_offset] = pack_lhs_qsi8d32p( |
| 195 | ukernel_variant.lhs_pack_interface, M, K, bl, mr, kr, sr, ref_lhs, lhs_stride, lhs_start_row, rect.height()); |
| 196 | |
| 197 | const size_t kr_qsi8 = kr / sr; |
| 198 | const size_t sr_qsi8 = 1; |
| 199 | |
| 200 | auto [imp_packed_lhs_qsi8, lhs_qsi8_packed_offset] = pack_lhs_qsi8d32p( |
| 201 | ukernel_variant.lhs_pack_interface, M, K, bl, mr, kr_qsi8, sr_qsi8, ref_lhs, lhs_stride, lhs_start_row, |
| 202 | rect.height()); |
| 203 | |
| 204 | ASSERT_EQ(lhs_qsi8_packed_offset, lhs_packed_offset); |
| 205 | |
| 206 | auto* imp_packed_lhs_ptr = reinterpret_cast<const uint8_t*>(imp_packed_lhs.data()); |
| 207 | auto* imp_packed_lhs_qsi8_ptr = reinterpret_cast<const uint8_t*>(imp_packed_lhs_qsi8.data()); |
| 208 | for (size_t i = 0; i < ukernel_variant.lhs_pack_interface.packed_size(M, K, bl, mr, kr, sr); i++) { |
| 209 | ASSERT_EQ(imp_packed_lhs_ptr[i], imp_packed_lhs_qsi8_ptr[i]); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | TEST_P(MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, EndToEnd) { |
| 214 | const auto& [variant_index, matmul_shape, bl, portion, clamp_keep_ratio, has_bias] = GetParam(); |
| 215 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_index); |
| 216 | |
| 217 | if (ukernel_variant.ukernel.fn_is_supported && !ukernel_variant.ukernel.fn_is_supported()) { |
| 218 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 219 | } |
| 220 | |
| 221 | const size_t M = matmul_shape.m; |
| 222 | const size_t N = matmul_shape.n; |
| 223 | const size_t K = matmul_shape.k; |
| 224 | |
| 225 | ASSERT_EQ(K % bl, size_t{0}); |
| 226 | |
| 227 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 228 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 229 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 230 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 231 | |
| 232 | ASSERT_TRUE(!(mr == 1 && M > 1)); |
| 233 | |
| 234 | auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 235 | ASSERT_TRUE(m_step % mr == 0); |
| 236 | |
| 237 | auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 238 | ASSERT_TRUE(n_step % nr == 0); |
| 239 | |
| 240 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 241 | if (rect.height() == 0 || rect.width() == 0) { |
| 242 | GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")"; |
| 243 | } |
| 244 | |
| 245 | // Seed the random generator. |
| 246 | auto& feed = seed_stream(current_test_key()); |
| 247 | |
| 248 | // Generates input data. |
| 249 | const auto ref_lhs = fill_random<float>(M * K, feed()); |
| 250 | const auto ref_rhs = fill_random<float>(N * K, feed()); |
| 251 | Buffer ref_biases; |
| 252 | |
| 253 | if (has_bias) { |
| 254 | ref_biases = fill_random<float>(N, feed()); |
| 255 | } |
| 256 | // Runs the reference implementation. |
| 257 | // * Quantizes the LHS matrix using 8-bit symmetric quantization. |
| 258 | // * Quantizes the RHS matrix using 4-bit asymmetric quantization. |
| 259 | // * Performs GEMM. |
| 260 | QuantizationInfo lhs_qinfo{}; |
| 261 | lhs_qinfo.quant_width = bl; |
| 262 | lhs_qinfo.dst_type = DataType::QSI8; |
| 263 | lhs_qinfo.scale_type = DataType::FP32; |
| 264 | const auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref_lhs.data(), DataType::FP32, M, K, lhs_qinfo); |
| 265 | |
| 266 | QuantizationInfo rhs_qinfo{}; |
| 267 | rhs_qinfo.quant_width = bl; |
| 268 | rhs_qinfo.dst_type = DataType::QAI4; |
| 269 | rhs_qinfo.scale_type = DataType::FP32; |
| 270 | rhs_qinfo.zero_point_type = DataType::I32; |
| 271 | const auto [ref_rhs_quant, rhs_qoutputs] = quantize_dynamic(ref_rhs.data(), DataType::FP32, N, K, rhs_qinfo); |
| 272 | |
| 273 | const auto ref_dst_no_clamp = |
| 274 | matmul_nt_t_quantized<int8_t, float, int32_t, Int4, float, int32_t, float, float, int32_t, float>( |
| 275 | M, N, K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), nullptr, 1, bl, ref_rhs_quant.data(), |
| 276 | rhs_qoutputs.scales.data(), rhs_qoutputs.zero_points.data(), 1, bl, has_bias ? ref_biases.data() : nullptr, |
| 277 | nullptr, nullptr, 1); |
| 278 | |
| 279 | // Clamps the reference output. |
| 280 | const auto [clamp_min, clamp_max] = find_clamp_range<float>(ref_dst_no_clamp.data(), M * N, clamp_keep_ratio); |
| 281 | const auto ref_dst = clamp<float>(ref_dst_no_clamp.data(), M * N, clamp_min, clamp_max); |
| 282 | |
| 283 | // Runs the LHS packing micro-kernel. |
| 284 | const auto lhs_start_row = rect.start_row(); |
| 285 | auto [imp_packed_lhs, lhs_packed_offset] = pack_lhs_qsi8d32p( |
| 286 | ukernel_variant.lhs_pack_interface, M, K, bl, mr, kr, sr, ref_lhs, K * sizeof(float), lhs_start_row, |
| 287 | rect.height()); |
| 288 | auto lhs_matmul_offset = ukernel_variant.ukernel.interface.get_lhs_packed_offset(lhs_start_row, K, bl); |
| 289 | |
| 290 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 291 | |
| 292 | // Prepare the offsets as the RHS packing micro-kernel expects the scaled zero-points in float. |
| 293 | const size_t num_blocks_per_row = round_up_division(K, bl); |
| 294 | const size_t ref_zp_size = N * num_blocks_per_row; |
| 295 | const size_t ref_zp_size_in_bytes = ref_zp_size * sizeof(float); |
| 296 | Buffer ref_rhs_zp_f32(ref_zp_size_in_bytes); |
| 297 | for (size_t i = 0; i < ref_zp_size; ++i) { |
| 298 | reinterpret_cast<float*>(ref_rhs_zp_f32.data())[i] = |
| 299 | -reinterpret_cast<const int32_t*>(rhs_qoutputs.zero_points.data())[i] * |
| 300 | reinterpret_cast<const float*>(rhs_qoutputs.scales.data())[i]; |
| 301 | } |
| 302 | |
| 303 | const auto rhs_start_row = rect.start_col(); |
| 304 | auto [imp_packed_rhs, rhs_packed_offset] = pack_rhs_qai4c32p( |
| 305 | ukernel_variant.rhs_pack_interface, N, K, bl, nr, kr, sr, ref_rhs_quant, has_bias, ref_biases, |
| 306 | rhs_qoutputs.scales, ref_rhs_zp_f32, ukernel_variant.rhs_s0s1_input, rhs_start_row); |
| 307 | |
| 308 | auto rhs_matmul_offset = ukernel_variant.ukernel.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 309 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 310 | |
| 311 | const auto dst_stride_row = N * sizeof(float); |
| 312 | const auto dst_stride_col = sizeof(float); |
| 313 | const auto dst_offset = |
| 314 | ukernel_variant.ukernel.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row); |
| 315 | const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col; |
| 316 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 317 | |
| 318 | // Runs the GEMM micro-kernel. |
| 319 | const auto imp_dst_size = ukernel_variant.ukernel.interface.get_dst_size(M, N); |
| 320 | ASSERT_EQ(imp_dst_size, ref_dst.size()); |
| 321 | Buffer imp_dst(imp_dst_size); |
| 322 | abi_check( |
| 323 | ukernel_variant.ukernel.interface.run_matmul, rect.height(), rect.width(), K, bl, |
| 324 | imp_packed_lhs.data() + lhs_matmul_offset, imp_packed_rhs.data() + rhs_matmul_offset, |
| 325 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), dst_stride_row, dst_stride_col, clamp_min, clamp_max); |
| 326 | |
| 327 | // Compares the output of the micro-kernels against the output of the reference implementation for the portion |
| 328 | // tested. |
| 329 | DefaultMismatchHandler handler(0, 0.1, 0, 0.05); |
| 330 | DataFormat dst_format = DataFormat(DataType::FP32); |
| 331 | const auto success = compare(imp_dst.data(), ref_dst.data(), dst_format, M, N, rect, handler); |
| 332 | ASSERT_TRUE(success); |
| 333 | } |
| 334 | static const auto test_name = [](const auto& info) { |
| 335 | const auto variant_idx = std::get<0>(info.param); |
| 336 | const std::string name{variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_idx).ukernel.name}; |
| 337 | const auto shape = std::get<MatMulShape>(info.param); |
| 338 | const auto bl = std::get<2>(info.param); |
| 339 | const auto portion = std::get<3>(info.param); |
| 340 | const auto clamp_keep_ratio = std::get<4>(info.param); |
| 341 | const auto has_bias = std::get<5>(info.param); |
| 342 | |
| 343 | std::ostringstream sstream; |
| 344 | sstream << name << "__"; |
| 345 | PrintTo(shape, &sstream); |
| 346 | sstream << "__BL_" << bl << "_"; |
| 347 | sstream << "__clamp_keep_ratio_" |
| 348 | << (clamp_keep_ratio.has_value() ? std::to_string(static_cast<int>(clamp_keep_ratio.value() * 100)) |
| 349 | : "noclamp"); |
| 350 | |
| 351 | if (has_bias) { |
| 352 | sstream << "_withBias_"; |
| 353 | } else { |
| 354 | sstream << "_noBias_"; |
| 355 | } |
| 356 | if (variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_idx).rhs_s0s1_input) { |
| 357 | sstream << "_RHS_s0s1__"; |
| 358 | } else { |
| 359 | sstream << "_RHS_s1s0__"; |
| 360 | } |
| 361 | PrintTo(portion, &sstream); |
| 362 | |
| 363 | return sstream.str(); |
| 364 | }; |
| 365 | |
| 366 | static constexpr std::array<MatMulShape, 26> test_matmul_shapes = {{ |
| 367 | MatMulShape{1, 64, 32}, // |
| 368 | MatMulShape{1, 63, 32}, // |
| 369 | MatMulShape{1, 65, 32}, // |
| 370 | MatMulShape{1, 64, 64}, // |
| 371 | MatMulShape{1, 64, 128}, // |
| 372 | MatMulShape{1, 128, 32}, // |
| 373 | MatMulShape{1, 128, 128}, // |
| 374 | MatMulShape{1, 2, 32}, // |
| 375 | MatMulShape{1, 3, 32}, // |
| 376 | MatMulShape{1, 4, 32}, // |
| 377 | MatMulShape{1, 5, 32}, // |
| 378 | MatMulShape{1, 71, 32}, // |
| 379 | MatMulShape{3, 3, 32}, // |
| 380 | MatMulShape{4, 4, 32}, // |
| 381 | MatMulShape{5, 5, 32}, // |
| 382 | MatMulShape{32, 128, 32}, // |
| 383 | MatMulShape{15, 64, 64}, // |
| 384 | MatMulShape{17, 64, 64}, // |
| 385 | MatMulShape{16, 63, 64}, // |
| 386 | MatMulShape{16, 64, 64}, // |
| 387 | MatMulShape{16, 65, 64}, // |
| 388 | MatMulShape{32, 64, 64}, // |
| 389 | MatMulShape{16, 32, 64}, // |
| 390 | MatMulShape{8, 32, 64}, // |
| 391 | MatMulShape{15, 32, 32}, // |
| 392 | MatMulShape{77, 99, 64}, |
| 393 | }}; |
| 394 | |
| 395 | static const std::array<MatrixPortion, 7> test_portions = {{ |
| 396 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 397 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 398 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 399 | MatrixPortion(0, 0.5, 1, 0.8), // Somewhere Middle |
| 400 | MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner. |
| 401 | MatrixPortion(0.75, 0, 1, 1), // Partial rows |
| 402 | MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle |
| 403 | }}; |
| 404 | |
| 405 | static std::vector<size_t> variant_indices_with_mr(const bool mr_is_1) { |
| 406 | std::vector<size_t> indices; |
| 407 | for (size_t variant_idx = 0; variant_idx < variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.size(); ++variant_idx) { |
| 408 | const auto& variant = variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_idx).ukernel; |
| 409 | if (variant.fn_is_supported && !variant.fn_is_supported()) { |
| 410 | continue; |
| 411 | } |
| 412 | const auto mr = variant.interface.get_mr(); |
| 413 | if ((mr == 1) == mr_is_1) { |
| 414 | indices.emplace_back(variant_idx); |
| 415 | } |
| 416 | } |
| 417 | return indices; |
| 418 | } |
| 419 | |
| 420 | static std::vector<size_t> unsupported_variant_indices() { |
| 421 | std::vector<size_t> indices; |
| 422 | for (size_t variant_idx = 0; variant_idx < variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.size(); ++variant_idx) { |
| 423 | const auto& variant = variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.at(variant_idx).ukernel; |
| 424 | if (variant.fn_is_supported && !variant.fn_is_supported()) { |
| 425 | indices.emplace_back(variant_idx); |
| 426 | } |
| 427 | } |
| 428 | return indices; |
| 429 | } |
| 430 | |
| 431 | static std::vector<MatMulShape> shapes_with_block_length(const size_t bl, const bool single_row_only) { |
| 432 | std::vector<MatMulShape> shapes; |
| 433 | for (const auto shape : test_matmul_shapes) { |
| 434 | if (shape.k % bl == 0 && (!single_row_only || shape.m == 1)) { |
| 435 | shapes.emplace_back(shape); |
| 436 | } |
| 437 | } |
| 438 | return shapes; |
| 439 | } |
| 440 | |
| 441 | // Keep one skipped test per unavailable kernel in JUnit without calling get_mr() on unsupported hardware. |
| 442 | INSTANTIATE_TEST_SUITE_P( |
| 443 | UnsupportedCPU, MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, |
| 444 | testing::Combine( |
| 445 | testing::ValuesIn(unsupported_variant_indices()), testing::Values(MatMulShape{1, 64, 32}), testing::Values(32), |
| 446 | testing::Values(MatrixPortion(0, 0, 1, 1)), testing::Values(std::optional<float>{}), testing::Values(false)), |
| 447 | test_name); |
| 448 | |
| 449 | INSTANTIATE_TEST_SUITE_P( |
| 450 | MatMulBL32, MatMulTest_f32_qsi8d32p_qai4c32p_LhsPacked, |
| 451 | testing::Combine( |
| 452 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.size()), |
| 453 | testing::ValuesIn(shapes_with_block_length(32, false)), testing::Values(32), testing::ValuesIn(test_portions), |
| 454 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 455 | std::nullopt, // Disable clamping |
| 456 | 1.0f, // Clamp to full range |
| 457 | 0.9f, // Clamp to 90% range |
| 458 | 0.5f}), // Clamp to 50% range |
| 459 | testing::Bool()), |
| 460 | test_name); |
| 461 | |
| 462 | INSTANTIATE_TEST_SUITE_P( |
| 463 | MatMulBL64, MatMulTest_f32_qsi8d32p_qai4c32p_LhsPacked, |
| 464 | testing::Combine( |
| 465 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_qsi8d32p_qai4c32p.size()), |
| 466 | testing::ValuesIn(shapes_with_block_length(64, false)), testing::Values(64), testing::ValuesIn(test_portions), |
| 467 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 468 | std::nullopt, // Disable clamping |
| 469 | 1.0f, // Clamp to full range |
| 470 | 0.9f, // Clamp to 90% range |
| 471 | 0.5f}), // Clamp to 50% range |
| 472 | testing::Bool()), |
| 473 | test_name); |
| 474 | |
| 475 | INSTANTIATE_TEST_SUITE_P( |
| 476 | MatMulBL32Mr1, MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, |
| 477 | testing::Combine( |
| 478 | testing::ValuesIn(variant_indices_with_mr(true)), testing::ValuesIn(shapes_with_block_length(32, true)), |
| 479 | testing::Values(32), testing::ValuesIn(test_portions), |
| 480 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 481 | std::nullopt, // Disable clamping |
| 482 | 1.0f, // Clamp to full range |
| 483 | 0.9f, // Clamp to 90% range |
| 484 | 0.5f}), // Clamp to 50% range |
| 485 | testing::Bool()), |
| 486 | test_name); |
| 487 | |
| 488 | INSTANTIATE_TEST_SUITE_P( |
| 489 | MatMulBL32MrN, MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, |
| 490 | testing::Combine( |
| 491 | testing::ValuesIn(variant_indices_with_mr(false)), testing::ValuesIn(shapes_with_block_length(32, false)), |
| 492 | testing::Values(32), testing::ValuesIn(test_portions), |
| 493 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 494 | std::nullopt, // Disable clamping |
| 495 | 1.0f, // Clamp to full range |
| 496 | 0.9f, // Clamp to 90% range |
| 497 | 0.5f}), // Clamp to 50% range |
| 498 | testing::Bool()), |
| 499 | test_name); |
| 500 | |
| 501 | INSTANTIATE_TEST_SUITE_P( |
| 502 | MatMulBL64Mr1, MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, |
| 503 | testing::Combine( |
| 504 | testing::ValuesIn(variant_indices_with_mr(true)), testing::ValuesIn(shapes_with_block_length(64, true)), |
| 505 | testing::Values(64), testing::ValuesIn(test_portions), |
| 506 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 507 | std::nullopt, // Disable clamping |
| 508 | 1.0f, // Clamp to full range |
| 509 | 0.9f, // Clamp to 90% range |
| 510 | 0.5f}), // Clamp to 50% range |
| 511 | testing::Bool()), |
| 512 | test_name); |
| 513 | |
| 514 | INSTANTIATE_TEST_SUITE_P( |
| 515 | MatMulBL64MrN, MatMulTest_f32_qsi8d32p_qai4c32p_EndToEnd, |
| 516 | testing::Combine( |
| 517 | testing::ValuesIn(variant_indices_with_mr(false)), testing::ValuesIn(shapes_with_block_length(64, false)), |
| 518 | testing::Values(64), testing::ValuesIn(test_portions), |
| 519 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 520 | std::nullopt, // Disable clamping |
| 521 | 1.0f, // Clamp to full range |
| 522 | 0.9f, // Clamp to 90% range |
| 523 | 0.5f}), // Clamp to 50% range |
| 524 | testing::Bool()), |
| 525 | test_name); |
| 526 | |
| 527 | } // namespace kai::test |