Elffuss BraidHub

kikocis/kleidiai public

main · 15.8 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 "test/nextgen/operators/matmul/matmul_operator.hpp"
8
9#include <array>
10#include <memory>
11#include <optional>
12
13#include "test/common/cpu_info.hpp"
14#include "test/common/data_type.hpp"
15#include "test/common/span.hpp"
16#include "test/nextgen/functions/round.hpp"
17#include "test/nextgen/operators/matmul/matmul/matmul_wrapper_registry.hpp"
18#include "test/nextgen/operators/matmul/matmul_bias_mode.hpp"
19#include "test/nextgen/operators/matmul/pack_lhs/matmul_pack_lhs_wrapper_registry.hpp"
20#include "test/nextgen/operators/matmul/pack_rhs/matmul_pack_rhs_wrapper_registry.hpp"
21#include "test/nextgen/quantization/asymm_linear_quantizer.hpp"
22#include "test/nextgen/quantization/symm_linear_quantizer.hpp"
23
24namespace kai::test {
25
26namespace {
27
28/// Combine several functions, and return true of all return true
29template <auto... Functions>
30constexpr auto all_true = [](auto... args) -> bool { return (Functions(args...) && ...); };
31
32bool is_shape_suitable_lhs_vector(
33 size_t shape_m, [[maybe_unused]] size_t shape_n, [[maybe_unused]] size_t shape_k,
34 [[maybe_unused]] const MatrixPortion& portion) {
35 return shape_m == 1;
36}
37
38bool is_shape_suitable_lhs_direct(
39 size_t shape_m, [[maybe_unused]] size_t shape_n, size_t shape_k, [[maybe_unused]] const MatrixPortion& portion) {
40 return shape_m > 0 && shape_k > 0;
41}
42
43/// Common bias format sets.
44const MatMulBiasModeSet no_bias;
45const MatMulBiasModeSet acc_bias_per_n{MatMulBiasMode::ACCUMULATION_PER_N};
46const MatMulBiasModeSet acc_bias_per_m_per_n{MatMulBiasMode::ACCUMULATION_PER_M, MatMulBiasMode::ACCUMULATION_PER_N};
47const MatMulBiasModeSet acc_bias_per_m_per_n_scale_bias_per_n{
48 MatMulBiasMode::ACCUMULATION_PER_M, MatMulBiasMode::ACCUMULATION_PER_N, MatMulBiasMode::SCALE_BIAS_PER_N};
49
50} // namespace
51
52Span<const MatMulOperator> get_available_matmul_operators() {
53 static std::array<MatMulOperator, 12> operators;
54
55 // matmul_clamp_f32_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa
56 operators[0].name = "matmul_clamp_f32_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa";
57
58 operators[0].is_cpu_supported = cpu_has_sme2;
59 operators[0].is_shape_suitable = all_true< //
60 is_shape_suitable_lhs_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa, //
61 is_shape_suitable_rhs_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa>;
62
63 operators[0].supported_bias_mode_sets = {no_bias, acc_bias_per_n};
64 operators[0].clamp_mode = MatMulClampMode::REQUIRED;
65
66 operators[0].lhs_quant = std::make_unique<AsymmLinearQuantizer>(
67 DataType::I8, DataType::FP32, DataType::I32, RoundMode::TIE_AWAY, RoundMode::CURRENT, 1, 0);
68 operators[0].rhs_quant =
69 std::make_unique<SymmLinearQuantizer>(DataType::U4, DataType::FP32, RoundMode::CURRENT, 1, 0);
70 operators[0].bias_quant = std::nullopt;
71
72 operators[0].lhs_dtype = DataType::FP32;
73 operators[0].rhs_dtype = DataType::FP32;
74 operators[0].bias_dtype = DataType::FP32;
75 operators[0].acc_dtype = DataType::FP32;
76 operators[0].dst_dtype = DataType::FP32;
77
78 operators[0].pack_lhs = create_matmul_lhs_quant_pack_qai8dxp1vlx4_f32();
79 operators[0].pack_rhs = create_matmul_rhs_pack_nxk_qsi4cxp4vlx4s1s0_qsu4cxs1s0_neon();
80 operators[0].matmul = create_matmul_clamp_f32_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa();
81
82 // matmul_clamp_f32_qai8dxp1x4_qsi4cxp4vlx4_1x4vl_sme2_sdot
83 operators[1].name = "matmul_clamp_f32_qai8dxp1x4_qsi4cxp4vlx4_1x4vl_sme2_sdot";
84
85 operators[1].is_cpu_supported = cpu_has_sme2;
86 operators[1].is_shape_suitable = all_true< //
87 is_shape_suitable_lhs_qai8dxp1x4_qsi4cxp4vlx4_1x4vl_sme2_sdot, //
88 is_shape_suitable_rhs_qai8dxp1x4_qsi4cxp4vlx4_1x4vl_sme2_sdot>;
89
90 operators[1].supported_bias_mode_sets = {no_bias, acc_bias_per_n};
91 operators[1].clamp_mode = MatMulClampMode::REQUIRED;
92
93 operators[1].lhs_quant = std::make_unique<AsymmLinearQuantizer>(
94 DataType::I8, DataType::FP32, DataType::I32, RoundMode::TIE_AWAY, RoundMode::CURRENT, 1, 0);
95 operators[1].rhs_quant =
96 std::make_unique<SymmLinearQuantizer>(DataType::U4, DataType::FP32, RoundMode::CURRENT, 1, 0);
97 operators[1].bias_quant = std::nullopt;
98
99 operators[1].lhs_dtype = DataType::FP32;
100 operators[1].rhs_dtype = DataType::FP32;
101 operators[1].bias_dtype = DataType::FP32;
102 operators[1].acc_dtype = DataType::FP32;
103 operators[1].dst_dtype = DataType::FP32;
104
105 operators[1].pack_lhs = create_matmul_lhs_quant_pack_qai8dxp1x4_f32();
106 operators[1].pack_rhs = create_matmul_rhs_pack_nxk_qsi4cxp4vlx4s1s0_qsu4cxs1s0_neon();
107 operators[1].matmul = create_matmul_clamp_f32_qai8dxp1x4_qsi4cxp4vlx4_1x4vl_sme2_sdot();
108
109 // matmul_clamp_f32_f32p2vlx1_f32p2vlx1biasf32_sme2_mopa
110 operators[2].name = "matmul_clamp_f32_f32p2vlx1_f32p2vlx1biasf32_sme2_mopa";
111
112 operators[2].is_cpu_supported = cpu_has_sme2;
113 operators[2].is_shape_suitable = all_true< //
114 is_shape_suitable_lhs_f32p2vlx1_f32p2vlx1biasf32_sme2_mopa, //
115 is_shape_suitable_rhs_f32p2vlx1_f32p2vlx1biasf32_sme2_mopa>;
116
117 operators[2].supported_bias_mode_sets = {acc_bias_per_n};
118 operators[2].clamp_mode = MatMulClampMode::REQUIRED;
119
120 operators[2].lhs_quant = std::nullopt;
121 operators[2].rhs_quant = std::nullopt;
122 operators[2].bias_quant = std::nullopt;
123
124 operators[2].lhs_dtype = DataType::FP32;
125 operators[2].rhs_dtype = DataType::FP32;
126 operators[2].bias_dtype = DataType::FP32;
127 operators[2].acc_dtype = DataType::FP32;
128 operators[2].dst_dtype = DataType::FP32;
129
130 operators[2].pack_lhs = create_matmul_lhs_pack_f32p2vlx1_f32_sme();
131 operators[2].pack_rhs = create_matmul_rhs_pack_kxn_f32p2vlx1biasf32_f32_f32_sme();
132 operators[2].matmul = create_matmul_clamp_f32_f32p2vlx1_f32p2vlx1biasf32_sme2_mopa();
133
134 // kai_matmul_clamp_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa -non transposed
135 operators[3].name = "matmul_clamp_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa";
136
137 operators[3].is_cpu_supported = cpu_has_sme2;
138 operators[3].is_shape_suitable = all_true< //
139 is_shape_suitable_lhs_x32p4vsx1_x32_sme, //
140 is_shape_suitable_rhs_kxn_x32p4vsx1bx32_x32_x32_sme>;
141 operators[3].supported_bias_mode_sets = {acc_bias_per_n};
142 operators[3].clamp_mode = MatMulClampMode::OPTIONAL;
143 operators[3].lhs_quant = std::nullopt;
144 operators[3].rhs_quant = std::nullopt;
145 operators[3].bias_quant = std::nullopt;
146 operators[3].lhs_dtype = DataType::FP32;
147 operators[3].rhs_dtype = DataType::FP32;
148 operators[3].bias_dtype = DataType::FP32;
149 operators[3].acc_dtype = DataType::FP32;
150 operators[3].dst_dtype = DataType::FP32;
151
152 operators[3].pack_lhs = create_matmul_pack_lhs_mxk_x32p4vsx1_x32_sme();
153 operators[3].pack_rhs = create_matmul_pack_rhs_kxn_x32p4vsx1bx32_x32_x32_sme();
154 operators[3].matmul = create_matmul_clamp_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa();
155
156 // kai_matmul_clamp_t_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa - transposed
157 operators[4].name = "matmul_clamp_t_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa";
158
159 operators[4].is_cpu_supported = cpu_has_sme2;
160 operators[4].is_shape_suitable = all_true< //
161 is_shape_suitable_lhs_x32p4vsx1_x32_sme, //
162 is_shape_suitable_rhs_nxk_x32p4vsx1bx32_x32_x32_sme>;
163 operators[4].supported_bias_mode_sets = {acc_bias_per_n};
164 operators[4].clamp_mode = MatMulClampMode::OPTIONAL;
165 operators[4].lhs_quant = std::nullopt;
166 operators[4].rhs_quant = std::nullopt;
167 operators[4].bias_quant = std::nullopt;
168 operators[4].lhs_dtype = DataType::FP32;
169 operators[4].rhs_dtype = DataType::FP32;
170 operators[4].bias_dtype = DataType::FP32;
171 operators[4].acc_dtype = DataType::FP32;
172 operators[4].dst_dtype = DataType::FP32;
173
174 operators[4].pack_lhs = create_matmul_pack_lhs_mxk_x32p4vsx1_x32_sme();
175 operators[4].pack_rhs = create_matmul_pack_rhs_nxk_x32p4vsx1bx32_x32_x32_sme();
176 operators[4].matmul = create_matmul_clamp_f32_f32p4vsx1_f32p4vsx1b_8vsx8vs_elastic_sme2_mopa();
177
178 // kai_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa
179 operators[5].name = "matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa";
180
181 operators[5].is_cpu_supported = cpu_has_sme2;
182 operators[5].is_shape_suitable = all_true< //
183 is_shape_suitable_lhs_x8p4vsx4_x8_sme, //
184 is_shape_suitable_rhs_kxn_x8p4vsx4_x8_sme>;
185 operators[5].supported_bias_mode_sets = {acc_bias_per_m_per_n};
186 operators[5].clamp_mode = MatMulClampMode::UNSUPPORTED;
187 operators[5].lhs_quant = std::nullopt;
188 operators[5].rhs_quant = std::nullopt;
189 operators[5].bias_quant = std::nullopt;
190 operators[5].lhs_dtype = DataType::U8;
191 operators[5].rhs_dtype = DataType::U8;
192 operators[5].bias_dtype = DataType::I32;
193 operators[5].acc_dtype = DataType::I32;
194 operators[5].dst_dtype = DataType::I32;
195
196 operators[5].pack_lhs = create_matmul_pack_lhs_mxk_x8p4vsx4_x8_sme();
197 operators[5].pack_rhs = create_matmul_pack_rhs_kxn_x8p4vsx4_x8_sme();
198 operators[5].matmul = create_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa();
199
200 // kai_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa - NxK RHS pack
201 operators[6].name = "matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa_rhs_nxk";
202
203 operators[6].is_cpu_supported = cpu_has_sme2;
204 operators[6].is_shape_suitable = all_true< //
205 is_shape_suitable_lhs_x8p4vsx4_x8_sme, //
206 is_shape_suitable_rhs_nxk_x8p4vsx4_x8_sme>;
207 operators[6].supported_bias_mode_sets = {acc_bias_per_m_per_n};
208 operators[6].clamp_mode = MatMulClampMode::UNSUPPORTED;
209 operators[6].lhs_quant = std::nullopt;
210 operators[6].rhs_quant = std::nullopt;
211 operators[6].bias_quant = std::nullopt;
212 operators[6].lhs_dtype = DataType::U8;
213 operators[6].rhs_dtype = DataType::U8;
214 operators[6].bias_dtype = DataType::I32;
215 operators[6].acc_dtype = DataType::I32;
216 operators[6].dst_dtype = DataType::I32;
217
218 operators[6].pack_lhs = create_matmul_pack_lhs_mxk_x8p4vsx4_x8_sme();
219 operators[6].pack_rhs = create_matmul_pack_rhs_nxk_x8p4vsx4_x8_sme();
220 operators[6].matmul = create_matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa();
221
222 // kai_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa - KxN RHS pack
223 operators[7].name = "matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa_rhs_kxn";
224
225 operators[7].is_cpu_supported = cpu_has_sme2;
226 operators[7].is_shape_suitable = all_true< //
227 is_shape_suitable_lhs_x8p4vsx4_x8_sme, //
228 is_shape_suitable_rhs_kxn_x8p4vsx4_x8_sme>;
229 operators[7].supported_bias_mode_sets = {acc_bias_per_m_per_n_scale_bias_per_n};
230 operators[7].clamp_mode = MatMulClampMode::OPTIONAL;
231 operators[7].lhs_quant = std::nullopt;
232 operators[7].rhs_quant = std::nullopt;
233 operators[7].bias_quant = std::nullopt;
234 operators[7].lhs_dtype = DataType::U8;
235 operators[7].rhs_dtype = DataType::U8;
236 operators[7].bias_dtype = DataType::I32;
237 operators[7].acc_dtype = DataType::I32;
238 operators[7].dst_dtype = DataType::FP32;
239
240 operators[7].pack_lhs = create_matmul_pack_lhs_mxk_x8p4vsx4_x8_sme();
241 operators[7].pack_rhs = create_matmul_pack_rhs_kxn_x8p4vsx4_x8_sme();
242 operators[7].matmul = create_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa();
243
244 // kai_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa - NxK RHS pack
245 operators[8].name = "matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa_rhs_nxk";
246
247 operators[8].is_cpu_supported = cpu_has_sme2;
248 operators[8].is_shape_suitable = all_true< //
249 is_shape_suitable_lhs_x8p4vsx4_x8_sme, //
250 is_shape_suitable_rhs_nxk_x8p4vsx4_x8_sme>;
251 operators[8].supported_bias_mode_sets = {acc_bias_per_m_per_n_scale_bias_per_n};
252 operators[8].clamp_mode = MatMulClampMode::OPTIONAL;
253 operators[8].lhs_quant = std::nullopt;
254 operators[8].rhs_quant = std::nullopt;
255 operators[8].bias_quant = std::nullopt;
256 operators[8].lhs_dtype = DataType::U8;
257 operators[8].rhs_dtype = DataType::U8;
258 operators[8].bias_dtype = DataType::I32;
259 operators[8].acc_dtype = DataType::I32;
260 operators[8].dst_dtype = DataType::FP32;
261
262 operators[8].pack_lhs = create_matmul_pack_lhs_mxk_x8p4vsx4_x8_sme();
263 operators[8].pack_rhs = create_matmul_pack_rhs_nxk_x8p4vsx4_x8_sme();
264 operators[8].matmul = create_matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa();
265
266 // kai_matmul_clamp_f32_f32_f32p4vsx1b_1x32vs_sme2_mla - non-transposed RHS.
267 operators[9].name = "matmul_clamp_f32_f32_f32p4vsx1b_1x32vs_sme2_mla";
268
269 operators[9].is_cpu_supported = cpu_has_sme2;
270 operators[9].is_shape_suitable = all_true< //
271 is_shape_suitable_lhs_vector, //
272 is_shape_suitable_lhs_x32p4vsx1_x32_sme, //
273 is_shape_suitable_rhs_kxn_x32p4vsx1bx32_x32_x32_sme>;
274 operators[9].supported_bias_mode_sets = {acc_bias_per_n};
275 operators[9].clamp_mode = MatMulClampMode::OPTIONAL;
276 operators[9].lhs_quant = std::nullopt;
277 operators[9].rhs_quant = std::nullopt;
278 operators[9].bias_quant = std::nullopt;
279 operators[9].lhs_dtype = DataType::FP32;
280 operators[9].rhs_dtype = DataType::FP32;
281 operators[9].bias_dtype = DataType::FP32;
282 operators[9].acc_dtype = DataType::FP32;
283 operators[9].dst_dtype = DataType::FP32;
284
285 operators[9].pack_lhs = std::nullopt;
286 operators[9].pack_rhs = create_matmul_pack_rhs_kxn_x32p4vsx1bx32_x32_x32_sme();
287 operators[9].matmul = create_matmul_clamp_f32_f32_f32p4vsx1bf32_1x32vs_sme2_mla();
288
289 // kai_matmul_clamp_f32_f32_f32p4vsx1b_1x32vs_sme2_mla - transposed RHS.
290 operators[10].name = "matmul_clamp_t_f32_f32_f32p4vsx1b_1x32vs_sme2_mla";
291
292 operators[10].is_cpu_supported = cpu_has_sme2;
293 operators[10].is_shape_suitable = all_true< //
294 is_shape_suitable_lhs_vector, //
295 is_shape_suitable_lhs_x32p4vsx1_x32_sme, //
296 is_shape_suitable_rhs_kxn_x32p4vsx1bx32_x32_x32_sme>;
297 operators[10].supported_bias_mode_sets = {acc_bias_per_n};
298 operators[10].clamp_mode = MatMulClampMode::OPTIONAL;
299 operators[10].lhs_quant = std::nullopt;
300 operators[10].rhs_quant = std::nullopt;
301 operators[10].bias_quant = std::nullopt;
302 operators[10].lhs_dtype = DataType::FP32;
303 operators[10].rhs_dtype = DataType::FP32;
304 operators[10].bias_dtype = DataType::FP32;
305 operators[10].acc_dtype = DataType::FP32;
306 operators[10].dst_dtype = DataType::FP32;
307
308 operators[10].pack_lhs = std::nullopt;
309 operators[10].pack_rhs = create_matmul_pack_rhs_nxk_x32p4vsx1bx32_x32_x32_sme();
310 operators[10].matmul = create_matmul_clamp_f32_f32_f32p4vsx1bf32_1x32vs_sme2_mla();
311
312 // kai_matmul_clamp_f16_f16_f16p16vsx2bf16_6x16vs_sve2p1_dot
313 operators[11].name = "matmul_clamp_f16_f16_f16p16vsx2bf16_6x16vs_sve2p1_dot";
314
315 operators[11].is_cpu_supported = cpu_check<cpu_has_sve2p1, cpu_has_fp16>;
316 operators[11].is_shape_suitable = all_true< //
317 is_shape_suitable_lhs_direct, //
318 is_shape_suitable_rhs_kxn_x16p16vsx2bx16_x16_x16_sve>;
319 operators[11].supported_bias_mode_sets = {no_bias, acc_bias_per_n};
320 operators[11].clamp_mode = MatMulClampMode::OPTIONAL;
321 operators[11].lhs_quant = std::nullopt;
322 operators[11].rhs_quant = std::nullopt;
323 operators[11].bias_quant = std::nullopt;
324 operators[11].lhs_dtype = DataType::FP16;
325 operators[11].rhs_dtype = DataType::FP16;
326 operators[11].bias_dtype = DataType::FP16;
327 operators[11].acc_dtype = DataType::FP32;
328 operators[11].dst_dtype = DataType::FP16;
329
330 operators[11].pack_lhs = std::nullopt;
331 operators[11].pack_rhs = create_matmul_rhs_pack_kxn_x16p16vsx2bx16_x16_x16_sve();
332 operators[11].matmul = create_matmul_clamp_f16_f16_f16p16vsx2bf16_6x16vs_sve2p1_dot();
333
334 return operators;
335}
336
337} // namespace kai::test