Elffuss BraidHub

kikocis/kleidiai public

main · 16.3 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 <string>
14#include <tuple>
15#include <vector>
16
17#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa.h"
18#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot.h"
19#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod.h"
20#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod.h"
21#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod.h"
22#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm.h"
23#include "kai/ukernels/matmul/matmul_clamp_f16_qai8dxp_qsi8cxp/kai_matmul_clamp_f16_qai8dxp_qsi8cxp_interface.h"
24#include "kai/ukernels/matmul/pack/kai_lhs_quant_pack_qai8dxp_f16_neon.h"
25#include "kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi8cxp_qsi8cx_neon.h"
26#include "test/common/buffer.hpp"
27#include "test/common/cache.hpp"
28#include "test/common/compare.hpp"
29#include "test/common/cpu_info.hpp"
30#include "test/common/data_format.hpp"
31#include "test/common/float16.hpp"
32#include "test/common/matmul_test_common.hpp"
33#include "test/common/matrix_portion.hpp"
34#include "test/common/memory.hpp"
35#include "test/common/seed.hpp"
36#include "test/common/test_suite.hpp"
37#include "test/reference/cast.hpp"
38#include "test/reference/clamp.hpp"
39#include "test/reference/fill.hpp"
40#include "test/reference/matmul.hpp"
41#include "test/reference/pack.hpp"
42#include "test/reference/quantize.hpp"
43
44namespace kai::test {
45
46using F16Qai8Qsi8CacheDataId = std::tuple<
47 MatMulShape, //
48 DataFormat, // lhs format
49 DataFormat, // rhs format
50 DataFormat, // bias format
51 std::optional<float> // clamp_keep_ratio
52 >;
53
54struct F16Qai8Qsi8CacheData {
55 Buffer ref_dst;
56 Buffer ref_rhs_qsi8;
57 Buffer ref_rhs_scales;
58 Buffer ref_lhs_f16;
59 Buffer ref_biases;
60 Range<float> clamp;
61};
62
63template <>
64F16Qai8Qsi8CacheData ReferenceGenerator<F16Qai8Qsi8CacheDataId, F16Qai8Qsi8CacheData>::generate_reference(
65 const F16Qai8Qsi8CacheDataId& data_id) {
66 auto [shape, lhs_format, rhs_format, bias_format, clamp_keep_ratio] = data_id;
67
68 const size_t M = shape.m;
69 const size_t N = shape.n;
70 const size_t K = shape.k;
71
72 // Seed the random generator.
73 const auto key = std::string("F16Qai8Qsi8_cache:") + std::to_string(M) + "x" + std::to_string(N) + "x" +
74 std::to_string(K) + ":" + std::to_string(static_cast<uint32_t>(lhs_format.data_type())) + ":" +
75 std::to_string(static_cast<uint32_t>(rhs_format.data_type())) + ":" +
76 std::to_string(static_cast<uint32_t>(bias_format.data_type())) + ":" +
77 (clamp_keep_ratio.has_value() ? std::to_string(clamp_keep_ratio.value()) : "noclamp");
78 auto& feed = seed_stream(key);
79
80 bool has_bias = bias_format.data_type() != DataType::UNKNOWN;
81 Buffer lhs = fill_matrix_random(shape.m, shape.k, lhs_format, feed());
82 Buffer rhs = fill_matrix_random(shape.n, shape.k, rhs_format, feed());
83 Buffer bias = has_bias ? fill_matrix_random(1, shape.n, bias_format, feed()) : Buffer();
84
85 const auto ref_lhs = cast<float, Float16>(lhs.data(), lhs.size() * 8 / size_in_bits<Float16>);
86
87 QuantizationInfo lhs_qinfo{};
88 lhs_qinfo.quant_width = K;
89 lhs_qinfo.dst_type = DataType::QAI8;
90 lhs_qinfo.scale_type = DataType::FP32;
91 lhs_qinfo.zero_point_type = DataType::I32;
92 const auto [ref_lhs_quant, lhs_qoutputs] = quantize_dynamic(ref_lhs.data(), DataType::FP32, M, K, lhs_qinfo);
93
94 QuantizationInfo rhs_qinfo{};
95 rhs_qinfo.quant_width = K;
96 rhs_qinfo.dst_type = DataType::QSI8;
97 rhs_qinfo.scale_type = DataType::FP32;
98 auto [ref_rhs_quant, rhs_qoutputs] = quantize_dynamic(rhs.data(), DataType::FP32, N, K, rhs_qinfo);
99
100 const auto ref_dst_no_clamp =
101 matmul_nt_t_quantized<int8_t, float, int32_t, int8_t, float, int32_t, float, float, int32_t, float>(
102 M, N, K, ref_lhs_quant.data(), lhs_qoutputs.scales.data(), lhs_qoutputs.zero_points.data(), 1, K,
103 ref_rhs_quant.data(), rhs_qoutputs.scales.data(), nullptr, 1, K, has_bias ? bias.data() : nullptr, nullptr,
104 nullptr, 1);
105
106 const auto [clamp_min, clamp_max] = find_clamp_range<float>(ref_dst_no_clamp.data(), M * N, clamp_keep_ratio);
107 const auto ref_dst_float = clamp<float>(ref_dst_no_clamp.data(), M * N, clamp_min, clamp_max);
108 auto ref_dst = cast<Float16, float>(ref_dst_float.data(), ref_dst_float.size() * 8 / size_in_bits<float>);
109
110 F16Qai8Qsi8CacheData out;
111 out.ref_dst = std::move(ref_dst);
112 out.ref_rhs_qsi8 = std::move(ref_rhs_quant);
113 out.ref_rhs_scales = std::move(rhs_qoutputs.scales);
114 out.ref_lhs_f16 = std::move(lhs);
115 out.ref_biases = std::move(bias);
116 out.clamp = {clamp_min, clamp_max};
117
118 return out;
119}
120
121static const std::array<UkernelVariant<kai_matmul_clamp_f16_qai8dxp_qsi8cxp_ukernel>, 6>
122 variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp = {
123 {{UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod),
124 "kai_matmul_clamp_f16_qai8dxp1x4_qsi8cxp4x4_1x4_neon_dotprod", cpu_has_dotprod_and_fp16},
125 {UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod),
126 "kai_matmul_clamp_f16_qai8dxp4x4_qsi8cxp4x4_16x4_neon_dotprod", cpu_has_dotprod_and_fp16},
127 {UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod),
128 "kai_matmul_clamp_f16_qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod", cpu_has_dotprod_and_fp16},
129 {UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm),
130 "kai_matmul_clamp_f16_qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm", cpu_has_i8mm_and_fp16},
131 {UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa),
132 "kai_matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa", cpu_has_sme2},
133 {UKERNEL_MATMUL_VARIANT(clamp_f16_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot),
134 "kai_matmul_clamp_f16_qai8dxp1x4_qsi8cxp4vlx4_1x4vl_sme2_dot", cpu_has_sme2}}};
135
136class MatMulTest_f16_qai8dxp_qsi8cxp : public ::testing::TestWithParam<MatMulClampTestPortionedParamsWithBias> {};
137GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MatMulTest_f16_qai8dxp_qsi8cxp);
138
139TEST_P(MatMulTest_f16_qai8dxp_qsi8cxp, EndToEnd) {
140 const auto& [variant_index, matmul_shape, portion, clamp_keep_ratio, has_bias] = GetParam();
141 const auto& ukernel_variant = variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.at(variant_index);
142
143 if (ukernel_variant.fn_is_supported && !ukernel_variant.fn_is_supported()) {
144 GTEST_SKIP() << "Unsupported CPU feature";
145 }
146
147 const size_t M = matmul_shape.m;
148 const size_t N = matmul_shape.n;
149 const size_t K = matmul_shape.k;
150
151 const auto mr = ukernel_variant.interface.get_mr();
152 const auto nr = ukernel_variant.interface.get_nr();
153 const auto kr = ukernel_variant.interface.get_kr();
154 const auto sr = ukernel_variant.interface.get_sr();
155
156 ASSERT_FALSE(mr == 1 && M > 1);
157
158 auto m_step = ukernel_variant.interface.get_m_step();
159 ASSERT_TRUE(m_step % mr == 0);
160
161 auto n_step = ukernel_variant.interface.get_n_step();
162 ASSERT_TRUE(n_step % nr == 0);
163
164 const auto rect = portion.compute_portion(M, N, m_step, n_step);
165 if (rect.height() == 0 || rect.width() == 0) {
166 GTEST_SKIP() << "Empty dimension of matrix(" << rect.width() << "," << rect.height() << ")";
167 }
168
169 const auto lhs_format = DataFormat(DataType::FP16);
170 const auto rhs_format = DataFormat(DataType::FP32);
171 const auto bias_format = has_bias ? DataFormat(DataType::FP32) : DataFormat(DataType::UNKNOWN);
172
173 const F16Qai8Qsi8CacheDataId testdata_id = {matmul_shape, lhs_format, rhs_format, bias_format, clamp_keep_ratio};
174 const F16Qai8Qsi8CacheData& testdata = getV<F16Qai8Qsi8CacheDataId, F16Qai8Qsi8CacheData>(testdata_id);
175 const auto& ref_lhs_f16 = testdata.ref_lhs_f16;
176 const auto& ref_rhs_qsi8 = testdata.ref_rhs_qsi8;
177 const auto& ref_biases = testdata.ref_biases;
178 const auto& ref_rhs_scales = testdata.ref_rhs_scales;
179 const auto& ref_dst = testdata.ref_dst;
180 auto [clamp_min, clamp_max] = testdata.clamp;
181
182 // Runs the LHS packing micro-kernel.
183 const auto lhs_start_row = rect.start_row();
184 const auto imp_packed_lhs_size = kai_get_lhs_packed_size_lhs_quant_pack_qai8dxp_f16_neon(M, K, mr, kr, sr);
185 Buffer imp_packed_lhs(imp_packed_lhs_size);
186
187 auto lhs_stride = K * sizeof(uint16_t);
188 auto lhs_offset = kai_get_lhs_offset_lhs_quant_pack_qai8dxp_f16_neon(lhs_start_row, lhs_stride);
189 auto lhs_packed_offset = kai_get_lhs_packed_offset_lhs_quant_pack_qai8dxp_f16_neon(lhs_start_row, K, mr, kr, sr);
190 auto lhs_matmul_offset = ukernel_variant.interface.get_lhs_packed_offset(lhs_start_row, K);
191
192 ASSERT_EQ(lhs_packed_offset, lhs_matmul_offset);
193
194 kai_run_lhs_quant_pack_qai8dxp_f16_neon(
195 rect.height() /* m */, K, mr, kr, sr, 0, ref_lhs_f16.data() + lhs_offset, lhs_stride,
196 imp_packed_lhs.data() + lhs_packed_offset);
197
198 const auto imp_packed_rhs_size = kai_get_rhs_packed_size_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(N, K, nr, kr, sr);
199 Buffer imp_packed_rhs(imp_packed_rhs_size);
200 const auto rhs_start_row = rect.start_col();
201 auto rhs_packed_offset = kai_get_rhs_packed_offset_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(rhs_start_row, K, nr, kr, sr);
202 auto rhs_matmul_offset = ukernel_variant.interface.get_rhs_packed_offset(rhs_start_row, K);
203 ASSERT_EQ(rhs_packed_offset, rhs_matmul_offset);
204
205 // Runs the RHS packing micro-kernel.
206 const kai_rhs_pack_qsi8cx_params params{.lhs_zero_point = 1, .scale_multiplier = 1.0f};
207 kai_run_rhs_pack_nxk_qsi8cxp_qsi8cx_neon(
208 1, N, K, nr, kr, sr, reinterpret_cast<const int8_t*>(ref_rhs_qsi8.data()),
209 has_bias ? reinterpret_cast<const float*>(ref_biases.data()) : nullptr,
210 reinterpret_cast<const float*>(ref_rhs_scales.data()), imp_packed_rhs.data(), 0, &params);
211
212 const auto dst_stride_row = N * sizeof(uint16_t);
213 const auto dst_stride_col = sizeof(uint16_t);
214 const auto dst_offset =
215 ukernel_variant.interface.get_dst_offset(rect.start_row(), rect.start_col(), dst_stride_row);
216 const auto ref_dst_offset = rect.start_row() * dst_stride_row + rect.start_col() * dst_stride_col;
217 ASSERT_EQ(dst_offset, ref_dst_offset);
218
219 // Runs the GEMM micro-kernel.
220 const auto imp_dst_size = ukernel_variant.interface.get_dst_size(M, N);
221 ASSERT_EQ(imp_dst_size, ref_dst.size());
222 Buffer imp_dst(imp_dst_size);
223 ukernel_variant.interface.run_matmul(
224 rect.height(), rect.width(), K, imp_packed_lhs.data() + lhs_matmul_offset,
225 imp_packed_rhs.data() + rhs_matmul_offset, imp_dst.data() + dst_offset, dst_stride_row, dst_stride_col,
226 clamp_min, clamp_max);
227
228 // Compares the output of the micro-kernels against the output of the reference implementation for the portion
229 // tested.
230 DefaultMismatchHandler handler(0, 0.02, 0, 0.05);
231 DataFormat dst_format = DataFormat(DataType::FP16);
232 const auto success = compare(imp_dst.data(), ref_dst.data(), dst_format, M, N, rect, handler);
233 ASSERT_TRUE(success);
234}
235
236static std::vector<size_t> variant_indices_with_mr(const size_t mr) {
237 std::vector<size_t> indices;
238 for (size_t variant_idx = 0; variant_idx < variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.size(); ++variant_idx) {
239 const auto& variant = variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.at(variant_idx);
240 if (variant.fn_is_supported && !variant.fn_is_supported()) {
241 continue;
242 }
243 if (variant.interface.get_mr() == mr) {
244 indices.emplace_back(variant_idx);
245 }
246 }
247 return indices;
248}
249
250static std::vector<size_t> variant_indices_without_mr(const size_t mr) {
251 std::vector<size_t> indices;
252 for (size_t variant_idx = 0; variant_idx < variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.size(); ++variant_idx) {
253 const auto& variant = variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.at(variant_idx);
254 if (variant.fn_is_supported && !variant.fn_is_supported()) {
255 continue;
256 }
257 if (variant.interface.get_mr() != mr) {
258 indices.emplace_back(variant_idx);
259 }
260 }
261 return indices;
262}
263
264static std::vector<size_t> unsupported_variant_indices() {
265 std::vector<size_t> indices;
266 for (size_t variant_idx = 0; variant_idx < variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.size(); ++variant_idx) {
267 const auto& variant = variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.at(variant_idx);
268 if (variant.fn_is_supported && !variant.fn_is_supported()) {
269 indices.emplace_back(variant_idx);
270 }
271 }
272 return indices;
273}
274
275static constexpr std::array<MatMulShape, 8> single_row_shapes = {{
276 MatMulShape{1, 2, 32}, //
277 MatMulShape{1, 3, 32}, //
278 MatMulShape{1, 4, 32}, //
279 MatMulShape{1, 5, 31}, //
280 MatMulShape{1, 71, 32}, //
281 MatMulShape{1, 64, 32}, //
282 MatMulShape{1, 35, 65}, //
283 MatMulShape{1, 128, 32}, //
284}};
285
286static constexpr std::array<MatMulShape, 18> all_shapes = {{
287 MatMulShape{1, 2, 32}, //
288 MatMulShape{1, 3, 32}, //
289 MatMulShape{1, 4, 32}, //
290 MatMulShape{1, 5, 31}, //
291 MatMulShape{1, 71, 32}, //
292 MatMulShape{3, 3, 32}, //
293 MatMulShape{4, 4, 32}, //
294 MatMulShape{5, 5, 31}, //
295 MatMulShape{16, 64, 32}, //
296 MatMulShape{1, 64, 32}, //
297 MatMulShape{16, 32, 36}, //
298 MatMulShape{15, 35, 65}, //
299 MatMulShape{8, 32, 64}, //
300 MatMulShape{15, 31, 45}, //
301 MatMulShape{1, 35, 65}, //
302 MatMulShape{1, 128, 32}, //
303 MatMulShape{64, 128, 32}, //
304 MatMulShape{77, 99, 64},
305}};
306
307static const std::array<MatrixPortion, 7> portions = {{
308 MatrixPortion(0, 0, 1, 1), // Full matrix.
309 MatrixPortion(0, 0, 1, 0.25), // Leftmost portion.
310 MatrixPortion(0, 0.75, 1, 1), // Rightmost portion.
311 MatrixPortion(0, 0.5, 1, 0.8), // Somewhere Middle
312 MatrixPortion(0.75, 0.75, 1, 1), // Bottom-right corner.
313 MatrixPortion(0.75, 0, 1, 1), // Partial rows
314 MatrixPortion(0.4, 0.5, 0.6, 0.8) // Somewhere Middle
315}};
316
317static const auto test_name = [](const auto& info) {
318 const auto variant_idx = std::get<0>(info.param);
319 const std::string name{variants_kai_matmul_clamp_f16_qai8dxp_qsi8cxp.at(variant_idx).name};
320 const auto shape = std::get<MatMulShape>(info.param);
321 const auto portion = std::get<2>(info.param);
322 const auto clamp_keep_ratio = std::get<3>(info.param);
323 const auto has_bias = std::get<4>(info.param);
324
325 return test_description(name, shape, portion, has_bias, clamp_keep_ratio);
326};
327
328// Keep one skipped test per unavailable kernel in JUnit without calling get_mr() on unsupported hardware.
329INSTANTIATE_TEST_SUITE_P(
330 UnsupportedCPU, MatMulTest_f16_qai8dxp_qsi8cxp,
331 testing::Combine(
332 testing::ValuesIn(unsupported_variant_indices()), testing::Values(MatMulShape{1, 2, 32}),
333 testing::Values(MatrixPortion(0, 0, 1, 1)), testing::Values(std::optional<float>{}), testing::Values(false)),
334 test_name);
335
336INSTANTIATE_TEST_SUITE_P(
337 MatMulMr1, MatMulTest_f16_qai8dxp_qsi8cxp,
338 testing::Combine(
339 testing::ValuesIn(variant_indices_with_mr(1)), testing::ValuesIn(single_row_shapes),
340 testing::ValuesIn(portions),
341 testing::ValuesIn(std::initializer_list<std::optional<float>>{
342 std::nullopt, // Disable clamping
343 1.0f, // Clamp to full range
344 0.9f, // Clamp to 90% range
345 0.5f}), // Clamp to 50% range
346 testing::Bool()),
347 test_name);
348
349INSTANTIATE_TEST_SUITE_P(
350 MatMulMrN, MatMulTest_f16_qai8dxp_qsi8cxp,
351 testing::Combine(
352 testing::ValuesIn(variant_indices_without_mr(1)), testing::ValuesIn(all_shapes), testing::ValuesIn(portions),
353 testing::ValuesIn(std::initializer_list<std::optional<float>>{
354 std::nullopt, // Disable clamping
355 1.0f, // Clamp to full range
356 0.9f, // Clamp to 90% range
357 0.5f}), // Clamp to 50% range
358 testing::Bool()),
359 test_name);
360
361} // namespace kai::test