Elffuss BraidHub

kikocis/kleidiai public

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