Introduce new Kleidiai kernels with baseline and sqnr calibrated workloads
#9
by ArtificialRay7579 - opened
Summary
Adds 18 KleidiAI definitions, each with workloads, a reference-scalar solution and a baseline-kleidiai-arm solution, built with kgen (add definition -> add workload -> add solution -> calibrate). The SME2 tier now has 20 KleidiAI kernels. Every new definition passes all five kgen check legs (static, reference, reference-scalar, baseline, diagnosis) on its target hardware.
It also moves two existing quantized definitions from a hand-set min_sqnr_db to the baseline-measured baseline_sqnr_db gate, and moves gemm_bf16_n512_k512 from SQNR to an elementwise comparison.
What is in this PR
Graviton tier (5 definitions, N/K as in the name, 13 workloads each, M = 1..2048)
| definition | baseline kernel (GEMM / GEMV for M=1) | correctness |
|---|---|---|
gemm_qs8cx_n512_k512 |
qai8dxp4x8_qsi8cxp4x8_16x4_neon_i8mm / qai8dxp1x8_qsi8cxp4x8_1x4_neon_dotprod |
SQNR vs baseline |
gemm_qs4cx_n512_k512 |
qai8dxp4x8_qsi4cxp8x8_4x8x32_neon_i8mm / qai8dxp1x8_qsi4cxp8x8_1x8x32_neon_dotprod |
SQNR vs baseline |
gemm_qsu2cx_n256_k512 |
qai8dxp4x4_qsu2cxp4x4bf32sf32_8x4_neon_dotprod / qai8dxp1x4_qsu2cxp4x4bf32sf32_1x4_neon_dotprod |
SQNR vs baseline |
gemm_qai4c32_n256_k512 |
qsi8d32p4x8_qai4c32p4x8_8x4_neon_i8mm / qsi8d32p1x8_qai4c32p4x8_1x4_neon_dotprod |
SQNR vs baseline |
gemm_f32_bf16_n512_k512 |
bf16p8x4_bf16p12x4b_8x12_neon_mmla / bf16p1x4_bf16p12x4b_1x36_neon_dot |
elementwise, abs_tol 0.09 |
SME2 tier (13 definitions, 13 workloads each, M = 1..2048; conv2d has 16)
| definition | baseline kernel (kai_ prefix omitted) |
correctness |
|---|---|---|
gemm_qs8cx_sme2_n256_k512 |
matmul_clamp_f32_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_qsu2cx_sme2_n256_k512 |
matmul_clamp_f32_qai8dxp1vlx4_qsu2cxp4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_qai4c32_sme2_n256_k512 |
matmul_clamp_f32_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_qs4c32_sme2_n256_k512 |
matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_qai8dxp_qs4c32_sme2_n256_k512 |
matmul_clamp_f32_qai8dxp1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_f16act_qs4c32_sme2_n256_k512 |
matmul_clamp_f32_f16p1vlx2_qsi4c32p4vlx2_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_f16_qs8cx_sme2_n256_k512 |
matmul_clamp_f16_qai8dxp1vlx4_qsi8cxp4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_f16_qs4cx_sme2_n256_k512 |
matmul_clamp_f16_qai8dxp1vlx8_qsi4cxp4vlx8_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_f16_qai4c32_sme2_n256_k512 |
matmul_clamp_f16_qsi8d32p1vlx4_qai4c32p4vlx4_1vlx4vl_sme2_mopa |
SQNR vs baseline |
gemm_i32_u8_sme2_n256_k512 |
matmul_i32_u8p4vsx4_u8p4vsx4_i32_i32_8vsx8vs_sme2_mopa |
exact (integer accumulation) |
gemm_u8_f32_sme2_n256_k512 |
matmul_clamp_f32_u8p4vsx4_u8p4vsx4_i32_i32_f32_f32_8vsx8vs_sme2_mopa |
elementwise, abs_tol 1e-3, rel_tol 1e-5, matched 1.0 |
gemm_qai8_qsi8cx_sme2_n256_k512 |
matmul_clamp_qai8_qai8p2vlx4_qsi8cxpsb2vlx4_2vlx2vl_sme2_mopa |
int8-valued output, abs_tol 0.5, matched >= 0.99 |
conv2d_fp32_nhwc_kh3_kw3_s1_p1_c32_c64_sme2 |
imatmul_clamp_f32_f32p2vlx1_f32p2vlx1b_2vlx2vl_sme2_mopa |
elementwise (conv2d defaults) |
gemm_i32_u8 and gemm_u8_f32 use the new-style kai_matmul_uker API; the others use the per-kernel API.
Changes to existing files
workloads/gemm/gemm_qs4cx_sme2_n256_k512.jsonl: addedbaseline_sqnr_db(44.3-50.1 dB, measured on Apple M4). Its gate was a hand-set 40 dBmin_sqnr_db.workloads/gemm/gemm_qs4c32_n256_k512.jsonl: addedbaseline_sqnr_db(48.2-49.6 dB, measured on Graviton3). Its gate was a hand-set 30 dBmin_sqnr_db.definitions/gemm/gemm_bf16_n512_k512.json: dropped thecorrectness:sqnrtag; it is judged elementwise (abs_tol0.09, worst measured baseline error 0.0835). The rest of the diff iskgenreformatting the JSON.
How it was built
baseline_sqnr_dbcomes fromkgen calibrate --author baseline-kleidiai-arm --baseline-author baseline-kleidiai-armon the machine matching each baseline's ISA (Graviton4 for the four quantized Graviton definitions, M4 for SME2, Graviton3 forgemm_qs4c32). The gate is the tag minus the 1 dB margin. Calibrating withreference-scalaras the author gives ~128 dB and is not meaningful.- Output is float32 in the raw ABI: fp16 results are widened exactly, int32 and int8 results are converted. fp16/bf16 inputs are just raw 16-bit patterns. Quantization parameters that would be scalars are one-element tensors (axis
S= 1), since the raw ABI passes no scalars. - Baselines dispatch the dedicated GEMV kernel for M = 1 on the Graviton tier (the SME2 mopa kernels take any M in one call). Weight packs are cached by content comparison (issue #80); activation quantization and packing are redone on every call.
How it was verified
kgen check(all five legs) passes for every definition above, on the target hardware: Apple M4 (mac-m4.metal) for the SME2 tier and the two SME2 changes, Graviton3 (c7g, Neoverse-V1) for the Graviton tier andgemm_qs4c32. Reference-scalar and baseline both pass all workloads, and the diagnosis leg reports them consistent.kgen autovecpasses for every definition above with--max-slowdown 0.5, where the geomean speedup of baseline kernels is at least 2x faster than the autovec version of reference-scalar kernels
Notes for reviewers
- Companion code PR: the elementwise tolerance overrides (
gemm_qai8_qsi8cx_sme2,gemm_u8_f32_sme2,gemm_bf16,gemm_f32_bf16) and the removal of the two hand-setmin_sqnr_dbentries live inconfig/kernel_contracts.yamlin the code repository. Without that change these definitions fall back to the gemm default tolerance andgemm_qai8_qsi8cx_sme2would fail. Code PR: . - Gates tighten for two existing definitions (
gemm_qs4cx_sme2: 40 dB -> about 43-49 dB,gemm_qs4c32: 30 dB -> about 47-48 dB). Verdicts from earlier agent runs on these two were judged against the looser gates. - Known limitation: the weight-side
memcmpthat guards the pack cache runs inside the timed region (issue #85, the untimed setup hook, is not applied here).gemm_f16act_qs4c32_sme2also widens its fp16 activation to fp32 inside the timed region, because KleidiAI's LHS packer only reads fp32. - Nothing under
runs/is touched.
ArtificialRay7579 changed pull request status to open
ArtificialRay7579 changed pull request status to merged