| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 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 <cstdlib> |
| 14 | #include <limits> |
| 15 | #include <sstream> |
| 16 | #include <string> |
| 17 | #include <tuple> |
| 18 | |
| 19 | #include "kai/ukernels/matmul/matmul_clamp_f32_f16p_qsi4c32p/kai_matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa.h" |
| 20 | #include "kai/ukernels/matmul/matmul_clamp_f32_f16p_qsi4c32p/kai_matmul_clamp_f32_f16p_qsi4c32p_interface.h" |
| 21 | #include "kai/ukernels/matmul/pack/kai_lhs_pack_f16pmrx2_f32_neon.h" |
| 22 | #include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.h" |
| 23 | #include "test/common/abi_checker.hpp" |
| 24 | #include "test/common/buffer.hpp" |
| 25 | #include "test/common/cache.hpp" |
| 26 | #include "test/common/compare.hpp" |
| 27 | #include "test/common/cpu_info.hpp" |
| 28 | #include "test/common/float16.hpp" |
| 29 | #include "test/common/int4.hpp" |
| 30 | #include "test/common/matmul_test_common.hpp" |
| 31 | #include "test/common/matrix_portion.hpp" |
| 32 | #include "test/common/memory.hpp" |
| 33 | #include "test/common/round.hpp" |
| 34 | #include "test/common/seed.hpp" |
| 35 | #include "test/common/test_suite.hpp" |
| 36 | #include "test/reference/cast.hpp" |
| 37 | #include "test/reference/clamp.hpp" |
| 38 | #include "test/reference/fill.hpp" |
| 39 | #include "test/reference/matmul.hpp" |
| 40 | #include "test/reference/pack.hpp" |
| 41 | #include "test/reference/quantize.hpp" |
| 42 | |
| 43 | namespace kai::test { |
| 44 | using F32F16pQsi4c32pCacheDataId = std::tuple<MatMulShape, DataFormat, DataFormat, size_t>; |
| 45 | |
| 46 | struct F32F16pQsi4c32pCacheData { |
| 47 | Buffer lhs; |
| 48 | Buffer rhs; |
| 49 | Buffer ref_dst; |
| 50 | Buffer ref_rhs_qsu4_scale_f16; |
| 51 | }; |
| 52 | |
| 53 | template <> |
| 54 | F32F16pQsi4c32pCacheData ReferenceGenerator<F32F16pQsi4c32pCacheDataId, F32F16pQsi4c32pCacheData>::generate_reference( |
| 55 | const F32F16pQsi4c32pCacheDataId& cache_data_id) { |
| 56 | const auto& [shape, lhs_format, rhs_format, bl] = cache_data_id; |
| 57 | |
| 58 | // Seed the random generator. |
| 59 | const auto key = std::string("F16PQsi4x32P") + "_" + std::to_string(shape.m) + "x" + std::to_string(shape.n) + "x" + |
| 60 | std::to_string(shape.k) + ":" + std::to_string(static_cast<uint32_t>(lhs_format.data_type())) + ":" + |
| 61 | std::to_string(static_cast<uint32_t>(rhs_format.data_type())) + ":" + std::to_string(bl); |
| 62 | |
| 63 | auto& feed = seed_stream(key); |
| 64 | |
| 65 | Buffer lhs = fill_matrix_random(shape.m, shape.k, lhs_format, feed(), -3, 2); |
| 66 | Buffer rhs = fill_matrix_random(shape.k, shape.n, rhs_format, feed(), -3, 2); |
| 67 | |
| 68 | const auto ref_lhs_qvalues = cast<Float16, float>(lhs.data(), shape.m * shape.k); |
| 69 | QuantizationInfo rhs_qinfo{}; |
| 70 | rhs_qinfo.quant_width = bl; |
| 71 | rhs_qinfo.dst_type = DataType::QSI4; |
| 72 | rhs_qinfo.scale_type = DataType::FP16; |
| 73 | const auto [ref_rhs_qsi4, ref_rhs_qoutputs] = |
| 74 | quantize_dynamic(rhs.data(), DataType::FP32, shape.n, shape.k, rhs_qinfo); |
| 75 | |
| 76 | const auto ref_rhs_qsu4 = cast_qsu4_qsi4(ref_rhs_qsi4.data(), shape.n * shape.k); |
| 77 | auto ref_rhs_qsu4_scale_f16 = pack_data_scales_interleave_block<UInt4, Float16>( |
| 78 | ref_rhs_qsu4.data(), ref_rhs_qoutputs.scales.data(), shape.n, shape.k, bl); |
| 79 | |
| 80 | Buffer ref_dst = |
| 81 | matmul_nt_t_quantized<Float16, float, int32_t, Int4, Float16, int32_t, float, float, int32_t, float>( |
| 82 | shape.m, shape.n, shape.k, ref_lhs_qvalues.data(), nullptr, nullptr, 1, shape.k, ref_rhs_qsi4.data(), |
| 83 | ref_rhs_qoutputs.scales.data(), nullptr, 1, bl, nullptr, nullptr, nullptr, 1); |
| 84 | |
| 85 | F32F16pQsi4c32pCacheData test_reference; |
| 86 | test_reference.lhs = std::move(lhs); |
| 87 | test_reference.rhs = std::move(rhs); |
| 88 | test_reference.ref_dst = std::move(ref_dst); |
| 89 | test_reference.ref_rhs_qsu4_scale_f16 = std::move(ref_rhs_qsu4_scale_f16); |
| 90 | |
| 91 | return test_reference; |
| 92 | } |
| 93 | |
| 94 | using MatMulTestParams_f32_f16p_qsi4c32p = std::tuple<size_t, MatMulShape, MatrixPortion, std::optional<float>, size_t>; |
| 95 | |
| 96 | [[maybe_unused]] static void PrintTo(const MatMulTestParams_f32_f16p_qsi4c32p& param, std::ostream* os) { |
| 97 | const auto variant_idx = std::get<0>(param); |
| 98 | const auto shape = std::get<1>(param); |
| 99 | const auto portion = std::get<2>(param); |
| 100 | const auto clamp_keep_ratio = std::get<3>(param); |
| 101 | const auto bl = std::get<4>(param); |
| 102 | |
| 103 | *os << "variant_" << variant_idx << "__"; |
| 104 | PrintTo(shape, os); |
| 105 | *os << "__"; |
| 106 | PrintTo(portion, os); |
| 107 | *os << "__clamp_keep_ratio_" |
| 108 | << (clamp_keep_ratio.has_value() ? std::to_string(static_cast<int>(clamp_keep_ratio.value() * 100)) |
| 109 | : "noclamp"); |
| 110 | *os << "__Bias"; |
| 111 | *os << "__bl_" << bl; |
| 112 | } |
| 113 | |
| 114 | namespace { |
| 115 | // Interface for the LHS and RHS packed size and packing functions |
| 116 | using kai_get_lhs_packed_size_func_t = decltype(&kai_get_lhs_packed_size_lhs_pack_f16pmrx2_f32_neon); |
| 117 | using kai_get_rhs_packed_size_func_t = |
| 118 | decltype(&kai_get_rhs_packed_size_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon); |
| 119 | using kai_get_lhs_packed_offset_func_t = decltype(&kai_get_lhs_packed_offset_lhs_pack_f16pmrx2_f32_neon); |
| 120 | using kai_get_rhs_packed_offset_func_t = |
| 121 | decltype(&kai_get_rhs_packed_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon); |
| 122 | using kai_get_lhs_offset_func_t = decltype(&kai_get_lhs_offset_lhs_pack_f16pmrx2_f32_neon); |
| 123 | using kai_get_rhs_offset_func_t = decltype(&kai_get_rhs_offset_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon); |
| 124 | using kai_run_lhs_pack_func_t = decltype(&kai_run_lhs_pack_f16pmrx2_f32_neon); |
| 125 | using kai_run_rhs_pack_func_t = decltype(&kai_run_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon); |
| 126 | |
| 127 | // Micro-kernel interface |
| 128 | struct kai_f16p_pack_functions { |
| 129 | kai_get_lhs_packed_size_func_t packed_size; |
| 130 | kai_get_lhs_packed_offset_func_t get_packed_offset; |
| 131 | kai_get_lhs_offset_func_t get_offset; |
| 132 | kai_run_lhs_pack_func_t run_pack; |
| 133 | }; |
| 134 | struct kai_qsi4c32p_pack_functions { |
| 135 | kai_get_rhs_packed_size_func_t packed_size; |
| 136 | kai_get_rhs_packed_offset_func_t get_packed_offset; |
| 137 | kai_get_rhs_offset_func_t get_offset; |
| 138 | kai_run_rhs_pack_func_t run_pack; |
| 139 | }; |
| 140 | |
| 141 | using Variant = UkernelMatmulPackVariant< |
| 142 | kai_matmul_clamp_f32_f16p_qsi4c32p_ukernel, kai_f16p_pack_functions, kai_qsi4c32p_pack_functions>; |
| 143 | |
| 144 | const std::array<Variant, 1> variants_kai_matmul_clamp_f32_f16p_qsi4c32p = { |
| 145 | { |
| 146 | UKERNEL_MATMUL_PACK_VARIANT( |
| 147 | clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa, (cpu_check<cpu_has_sme2, cpu_has_fp16>), |
| 148 | lhs_pack_f16pmrx2_f32_neon, rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon, false), |
| 149 | }, |
| 150 | }; |
| 151 | |
| 152 | class MatMulTest_f32_f16p_qsi4c32p : public ::testing::TestWithParam<MatMulTestParams_f32_f16p_qsi4c32p> {}; |
| 153 | |
| 154 | Buffer pack_f16pmrx2_ref(const void* src, size_t nb_rows, size_t nb_cols, size_t offset_bytes = 0, size_t mr = 16) { |
| 155 | // Source: raw FP16 (no packing) |
| 156 | DataFormat src_fmt{ |
| 157 | DataType::FP16, // data_type |
| 158 | 0, // block_height (unused for raw src) |
| 159 | 0, // block_width (unused for raw src) |
| 160 | DataFormat::PackFormat::NONE}; |
| 161 | |
| 162 | // Destination: FP16, packed by MR x 2 sub-blocks |
| 163 | DataFormat dst_fmt{ |
| 164 | DataType::FP16, // data_type |
| 165 | mr, // block_height |
| 166 | nb_cols, // block_width (prevents width padding) |
| 167 | DataFormat::PackFormat::NONE, |
| 168 | DataType::UNKNOWN, // zero_point_dt (unused) |
| 169 | DataType::UNKNOWN, // scale_dt (unused) |
| 170 | mr, // subblock_height |
| 171 | 2 // subblock_width |
| 172 | }; |
| 173 | |
| 174 | Buffer packed = pack(dst_fmt, src, /*scales*/ nullptr, /*bias*/ nullptr, src_fmt, nb_rows, nb_cols); |
| 175 | |
| 176 | if (offset_bytes == 0) { |
| 177 | return packed; |
| 178 | } |
| 179 | |
| 180 | // Reproduce the 'offset' behavior of the scalar helper |
| 181 | Buffer out(offset_bytes + packed.size(), 0); |
| 182 | std::memcpy(out.data() + offset_bytes, packed.data(), packed.size()); |
| 183 | return out; |
| 184 | } |
| 185 | |
| 186 | TEST_P(MatMulTest_f32_f16p_qsi4c32p, Offset_RHS_LHS) { |
| 187 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl] = GetParam(); |
| 188 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_f16p_qsi4c32p.at(variant_index); |
| 189 | |
| 190 | if (!ukernel_variant.ukernel.fn_is_supported()) { |
| 191 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 192 | } |
| 193 | |
| 194 | const size_t M = matmul_shape.m; |
| 195 | const size_t N = matmul_shape.n; |
| 196 | const size_t K = matmul_shape.k; |
| 197 | |
| 198 | ASSERT_TRUE(K % bl == 0); |
| 199 | |
| 200 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 201 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 202 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 203 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 204 | |
| 205 | const auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 206 | const auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 207 | const auto tile_m = std::max(m_step, mr); |
| 208 | const auto tile_n = std::max(n_step, nr); |
| 209 | |
| 210 | const auto rect = portion.compute_portion(M, N, tile_m, tile_n); |
| 211 | |
| 212 | const auto rhs_start_row = rect.start_col(); |
| 213 | const auto lhs_start_row = rect.start_row(); |
| 214 | |
| 215 | auto rhs_packed_offset = ukernel_variant.rhs_pack_interface.get_packed_offset(rhs_start_row, K, nr, kr, bl); |
| 216 | auto rhs_matmul_offset = ukernel_variant.ukernel.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 217 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 218 | |
| 219 | auto lhs_packed_offset = ukernel_variant.lhs_pack_interface.get_packed_offset(lhs_start_row, K, bl, mr, kr, sr); |
| 220 | auto lhs_matmul_offset = ukernel_variant.ukernel.interface.get_lhs_packed_offset(lhs_start_row, K, bl); |
| 221 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 222 | } |
| 223 | |
| 224 | TEST_P(MatMulTest_f32_f16p_qsi4c32p, LHS) { |
| 225 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl] = GetParam(); |
| 226 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_f16p_qsi4c32p.at(variant_index); |
| 227 | |
| 228 | if (!ukernel_variant.ukernel.fn_is_supported()) { |
| 229 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 230 | } |
| 231 | const size_t M = matmul_shape.m; |
| 232 | const size_t K = matmul_shape.k; |
| 233 | |
| 234 | ASSERT_TRUE(K % bl == 0); |
| 235 | |
| 236 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 237 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 238 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 239 | |
| 240 | auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 241 | const auto rect = portion.compute_portion(M, K, m_step, K); |
| 242 | |
| 243 | const F32F16pQsi4c32pCacheDataId id = { |
| 244 | matmul_shape, // |
| 245 | DataFormat(DataType::FP32), // |
| 246 | DataFormat(DataType::FP32), |
| 247 | bl, |
| 248 | }; |
| 249 | const F32F16pQsi4c32pCacheData& test_data = getV<F32F16pQsi4c32pCacheDataId, F32F16pQsi4c32pCacheData>(id); |
| 250 | |
| 251 | const auto lhs_start_row = rect.start_row(); |
| 252 | auto lhs_stride = K * sizeof(float); |
| 253 | auto lhs_offset = ukernel_variant.lhs_pack_interface.get_offset(lhs_start_row, lhs_stride); |
| 254 | auto lhs_packed_offset = ukernel_variant.lhs_pack_interface.get_packed_offset(lhs_start_row, K, bl, mr, kr, sr); |
| 255 | const auto imp_packed_lhs_size = ukernel_variant.lhs_pack_interface.packed_size(M, K, bl, mr, kr, sr); |
| 256 | Buffer imp_packed_lhs(imp_packed_lhs_size); |
| 257 | const auto ref_lhs_qvalues = cast<Float16, float>(test_data.lhs.data() + lhs_offset, rect.height() * K); |
| 258 | |
| 259 | auto ref_lhs_packed = pack_f16pmrx2_ref(ref_lhs_qvalues.data(), rect.height(), K, lhs_packed_offset, mr); |
| 260 | abi_check( |
| 261 | ukernel_variant.lhs_pack_interface.run_pack, rect.height() /* m */, K, bl, mr, kr, sr, 0, |
| 262 | reinterpret_cast<const float*>(test_data.lhs.data() + lhs_offset), lhs_stride, |
| 263 | imp_packed_lhs.data() + lhs_packed_offset); |
| 264 | |
| 265 | DefaultMismatchHandler handler(0, 0.0001, 0, 0.001); |
| 266 | DataFormat dst_format = DataFormat(DataType::FP16); |
| 267 | const auto success = |
| 268 | compare(imp_packed_lhs.data(), ref_lhs_packed.data(), dst_format, rect.height(), K, rect, handler); |
| 269 | ASSERT_TRUE(success); |
| 270 | } |
| 271 | |
| 272 | TEST_P(MatMulTest_f32_f16p_qsi4c32p, EndToEnd) { |
| 273 | const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, bl] = GetParam(); |
| 274 | const auto& ukernel_variant = variants_kai_matmul_clamp_f32_f16p_qsi4c32p.at(variant_index); |
| 275 | |
| 276 | if (!ukernel_variant.ukernel.fn_is_supported()) { |
| 277 | GTEST_SKIP() << "Unsupported CPU feature"; |
| 278 | } |
| 279 | |
| 280 | const size_t M = matmul_shape.m; |
| 281 | const size_t N = matmul_shape.n; |
| 282 | const size_t K = matmul_shape.k; |
| 283 | |
| 284 | ASSERT_TRUE(K % bl == 0); |
| 285 | |
| 286 | const auto mr = ukernel_variant.ukernel.interface.get_mr(); |
| 287 | const auto nr = ukernel_variant.ukernel.interface.get_nr(); |
| 288 | const auto kr = ukernel_variant.ukernel.interface.get_kr(); |
| 289 | const auto sr = ukernel_variant.ukernel.interface.get_sr(); |
| 290 | |
| 291 | const F32F16pQsi4c32pCacheDataId id = { |
| 292 | matmul_shape, // |
| 293 | DataFormat(DataType::FP32), // |
| 294 | DataFormat(DataType::FP32), |
| 295 | bl, |
| 296 | }; |
| 297 | const F32F16pQsi4c32pCacheData& test_data = getV<F32F16pQsi4c32pCacheDataId, F32F16pQsi4c32pCacheData>(id); |
| 298 | |
| 299 | // This test should only test GEMM and not GEMV |
| 300 | ASSERT_TRUE(!(mr == 1 && M > 1)); |
| 301 | |
| 302 | auto m_step = ukernel_variant.ukernel.interface.get_m_step(); |
| 303 | ASSERT_TRUE(m_step % mr == 0); |
| 304 | |
| 305 | auto n_step = ukernel_variant.ukernel.interface.get_n_step(); |
| 306 | ASSERT_TRUE(n_step % nr == 0); |
| 307 | |
| 308 | const auto rect = portion.compute_portion(M, N, m_step, n_step); |
| 309 | |
| 310 | // Runs the LHS packing micro-kernel. |
| 311 | const auto lhs_start_row = rect.start_row(); |
| 312 | const auto imp_packed_lhs_size = ukernel_variant.lhs_pack_interface.packed_size(M, K, bl, mr, kr, sr); |
| 313 | Buffer imp_packed_lhs(imp_packed_lhs_size); |
| 314 | |
| 315 | auto lhs_stride = K * sizeof(float); |
| 316 | auto lhs_offset = ukernel_variant.lhs_pack_interface.get_offset(lhs_start_row, lhs_stride); |
| 317 | auto lhs_packed_offset = ukernel_variant.lhs_pack_interface.get_packed_offset(lhs_start_row, K, bl, mr, kr, sr); |
| 318 | auto lhs_matmul_offset = ukernel_variant.ukernel.interface.get_lhs_packed_offset(lhs_start_row, K, bl); |
| 319 | |
| 320 | ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset); |
| 321 | |
| 322 | abi_check( |
| 323 | ukernel_variant.lhs_pack_interface.run_pack, rect.height() /* m */, K, bl, mr, kr, sr, 0, |
| 324 | reinterpret_cast<const float*>(test_data.lhs.data() + lhs_offset), lhs_stride, |
| 325 | imp_packed_lhs.data() + lhs_packed_offset); |
| 326 | |
| 327 | // Runs the RHS packing micro-kernel. |
| 328 | const auto imp_packed_rhs_size = ukernel_variant.rhs_pack_interface.packed_size(N, K, nr, kr, bl); |
| 329 | Buffer imp_packed_rhs(imp_packed_rhs_size); |
| 330 | const auto rhs_start_row = rect.start_col(); |
| 331 | auto rhs_packed_offset = ukernel_variant.rhs_pack_interface.get_packed_offset(rhs_start_row, K, nr, kr, bl); |
| 332 | auto rhs_matmul_offset = ukernel_variant.ukernel.interface.get_rhs_packed_offset(rhs_start_row, K, bl); |
| 333 | ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset); |
| 334 | |
| 335 | kai_rhs_pack_qs4cxs1s0_param params{}; |
| 336 | params.lhs_zero_point = 1; |
| 337 | params.rhs_zero_point = 8; |
| 338 | abi_check( |
| 339 | ukernel_variant.rhs_pack_interface.run_pack, 1, N, K, nr, kr, sr, bl, |
| 340 | reinterpret_cast<const uint8_t*>(test_data.ref_rhs_qsu4_scale_f16.data()), nullptr, imp_packed_rhs.data(), 0, |
| 341 | ¶ms); |
| 342 | |
| 343 | const auto dst_stride_row = N * sizeof(float); |
| 344 | const auto dst_stride_col = sizeof(float); |
| 345 | const auto dst_offset = |
| 346 | ukernel_variant.ukernel.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row); |
| 347 | const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col; |
| 348 | ASSERT_EQ(dst_offset, ref_dst_offset); |
| 349 | |
| 350 | // Clamp reference output |
| 351 | const auto [min, max] = find_clamp_range(DataType::FP32, test_data.ref_dst.data(), M * N, clamp_keep_ratio); |
| 352 | const auto out_clamped = clamp(DataType::FP32, test_data.ref_dst.data(), M * N, min, max); |
| 353 | |
| 354 | // Runs the GEMM micro-kernel. |
| 355 | const auto imp_dst_size = ukernel_variant.ukernel.interface.get_dst_size(M, N); |
| 356 | ASSERT_EQ(imp_dst_size, test_data.ref_dst.size()); |
| 357 | Buffer imp_dst(imp_dst_size); |
| 358 | abi_check( |
| 359 | ukernel_variant.ukernel.interface.run_matmul, rect.height(), rect.width(), K, bl, |
| 360 | imp_packed_lhs.data() + lhs_matmul_offset, imp_packed_rhs.data() + rhs_matmul_offset, |
| 361 | reinterpret_cast<float*>(imp_dst.data() + dst_offset), dst_stride_row, dst_stride_col, min, max); |
| 362 | |
| 363 | // Compares the output of the micro-kernels against the output of the reference implementation for the portion |
| 364 | // tested. |
| 365 | DefaultMismatchHandler handler(0, 0.1, 0, 0.05); |
| 366 | DataFormat dst_format = DataFormat(DataType::FP32); |
| 367 | const auto success = compare(imp_dst.data(), out_clamped.data(), dst_format, M, N, rect, handler); |
| 368 | ASSERT_TRUE(success); |
| 369 | } |
| 370 | |
| 371 | static constexpr std::array portions{ |
| 372 | MatrixPortion(0, 0, 1, 1), // Full matrix. |
| 373 | MatrixPortion(0, 0, 1, 0.25), // Leftmost portion. |
| 374 | MatrixPortion(0, 0.75, 1, 1), // Rightmost portion. |
| 375 | MatrixPortion(0, 0.5, 1, 0.8), // Somewhere Middle |
| 376 | MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner. |
| 377 | MatrixPortion(0.75, 0, 1, 1), // Partial rows |
| 378 | MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle |
| 379 | }; |
| 380 | static constexpr std::array shapes_k32{ |
| 381 | MatMulShape{1, 64, 32}, // |
| 382 | MatMulShape{1, 63, 32}, // |
| 383 | MatMulShape{1, 65, 32}, // |
| 384 | MatMulShape{1, 128, 32}, // |
| 385 | MatMulShape{1, 2, 32}, // |
| 386 | MatMulShape{1, 3, 32}, // |
| 387 | MatMulShape{1, 4, 32}, // |
| 388 | MatMulShape{1, 5, 32}, // |
| 389 | MatMulShape{1, 71, 32}, // |
| 390 | MatMulShape{3, 3, 32}, // |
| 391 | MatMulShape{4, 4, 32}, // |
| 392 | MatMulShape{5, 5, 32}, // |
| 393 | MatMulShape{32, 128, 32}, // |
| 394 | MatMulShape{15, 32, 32}, // |
| 395 | MatMulShape{1, 64, 64}, // |
| 396 | MatMulShape{16, 64, 64}, // |
| 397 | MatMulShape{32, 64, 64}, // |
| 398 | MatMulShape{1, 64, 128}, // |
| 399 | MatMulShape{32, 64, 128}, // |
| 400 | MatMulShape{32, 64, 256}, // |
| 401 | MatMulShape{77, 99, 256}, |
| 402 | }; |
| 403 | static constexpr std::array shapes_k64{ |
| 404 | MatMulShape{1, 64, 64}, // |
| 405 | MatMulShape{15, 64, 64}, // |
| 406 | MatMulShape{17, 64, 64}, // |
| 407 | MatMulShape{16, 63, 64}, // |
| 408 | MatMulShape{16, 64, 64}, // |
| 409 | MatMulShape{16, 65, 64}, // |
| 410 | MatMulShape{32, 64, 64}, // |
| 411 | MatMulShape{16, 32, 64}, // |
| 412 | MatMulShape{8, 32, 64}, // |
| 413 | MatMulShape{77, 99, 64}, // |
| 414 | MatMulShape{1, 64, 128}, // |
| 415 | MatMulShape{32, 64, 128}, // |
| 416 | MatMulShape{32, 64, 256}, // |
| 417 | MatMulShape{77, 99, 256}, |
| 418 | }; |
| 419 | static constexpr std::array shapes_k128{ |
| 420 | MatMulShape{1, 64, 128}, // |
| 421 | MatMulShape{1, 128, 128}, // |
| 422 | MatMulShape{32, 64, 128}, // |
| 423 | MatMulShape{16, 32, 128}, // |
| 424 | MatMulShape{8, 32, 128}, // |
| 425 | MatMulShape{77, 99, 128}, // |
| 426 | MatMulShape{32, 64, 256}, // |
| 427 | MatMulShape{77, 99, 256}, |
| 428 | }; |
| 429 | static constexpr std::array shapes_k256{ |
| 430 | MatMulShape{32, 64, 256}, // |
| 431 | MatMulShape{16, 32, 256}, // |
| 432 | MatMulShape{8, 32, 256}, // |
| 433 | MatMulShape{77, 99, 256}, |
| 434 | }; |
| 435 | |
| 436 | INSTANTIATE_TEST_SUITE_P( |
| 437 | MatMul_bl32, MatMulTest_f32_f16p_qsi4c32p, |
| 438 | testing::Combine( |
| 439 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_f16p_qsi4c32p.size()), // |
| 440 | testing::ValuesIn(shapes_k32), // |
| 441 | testing::ValuesIn(portions), // |
| 442 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 443 | std::nullopt, // Disable clamping |
| 444 | 1.0F, // Clamp to full range |
| 445 | 0.9F, // Clamp to 90% range |
| 446 | 0.5F, // Clamp to 50% range |
| 447 | }), |
| 448 | testing::Values(32)), // |
| 449 | testing::PrintToStringParamName()); |
| 450 | |
| 451 | INSTANTIATE_TEST_SUITE_P( |
| 452 | MatMul_bl64, MatMulTest_f32_f16p_qsi4c32p, |
| 453 | testing::Combine( |
| 454 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_f16p_qsi4c32p.size()), // |
| 455 | testing::ValuesIn(shapes_k64), // |
| 456 | testing::ValuesIn(portions), // |
| 457 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 458 | std::nullopt, // Disable clamping |
| 459 | 1.0F, // Clamp to full range |
| 460 | 0.9F, // Clamp to 90% range |
| 461 | 0.5F, // Clamp to 50% range |
| 462 | }), |
| 463 | testing::Values(64)), // |
| 464 | testing::PrintToStringParamName()); |
| 465 | |
| 466 | INSTANTIATE_TEST_SUITE_P( |
| 467 | MatMul_bl128, MatMulTest_f32_f16p_qsi4c32p, |
| 468 | testing::Combine( |
| 469 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_f16p_qsi4c32p.size()), // |
| 470 | testing::ValuesIn(shapes_k128), // |
| 471 | testing::ValuesIn(portions), // |
| 472 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 473 | std::nullopt, // Disable clamping |
| 474 | 1.0F, // Clamp to full range |
| 475 | 0.9F, // Clamp to 90% range |
| 476 | 0.5F, // Clamp to 50% range |
| 477 | }), |
| 478 | testing::Values(128)), // |
| 479 | testing::PrintToStringParamName()); |
| 480 | |
| 481 | INSTANTIATE_TEST_SUITE_P( |
| 482 | MatMul_bl256, MatMulTest_f32_f16p_qsi4c32p, |
| 483 | testing::Combine( |
| 484 | testing::Range<size_t>(0, variants_kai_matmul_clamp_f32_f16p_qsi4c32p.size()), // |
| 485 | testing::ValuesIn(shapes_k256), // |
| 486 | testing::ValuesIn(portions), // |
| 487 | testing::ValuesIn(std::initializer_list<std::optional<float>>{ |
| 488 | std::nullopt, // Disable clamping |
| 489 | 1.0F, // Clamp to full range |
| 490 | 0.9F, // Clamp to 90% range |
| 491 | 0.5F, // Clamp to 50% range |
| 492 | }), |
| 493 | testing::Values(128)), // |
| 494 | testing::PrintToStringParamName()); |
| 495 | } // namespace |
| 496 | } // namespace kai::test |