M2XFP4 (MXFP4v2): quantiser, gfx1201 WMMA kernels, host rows, df2 wiring, .rad packer (Import::ParoM2), stability fixes (counters-in-scratch x3, aql publish fences), kbench rows, runbook (REMAINING.md)

This commit is contained in:
hugypufy committed 2026-10-23 12:25:59 +01:00
1 parent 9eaf10da6e
commit ea0908596b
60 files changed
+4619 -193

No files matched your search

+92
View File
@@ -4390,3 +4390,95 @@ A sanitised replay of the whole fixture. Host code only -- see the note in the
- 0 report(s)
## The M2XFP4 GEMMs
Hand-written section, 2026-10-20, beside the generated report above. The M2
analogue of the OCP-MXFP4 x fp8 stack (arXiv 2601.19213 §4.4.2, the paper's
weight-side Sg-EM): two rows, `gemm_m2xfp4a8_decode` (M <= 128, the decode band)
and `gemm_m2xfp4a8_tiled` (prefill), at the quantiser-own encoding `m2xfp4` --
one more plane than MXFP4, everything else the OCP rows' plumbing.
**Encoding and decode.** Per group of 32 columns the weight carries the MXFP4
planes -- e2m1 codes and an E8M0 exponent whose adaptive bias b in {-1,0,1} the
quantiser already folded into the stored byte -- plus one meta byte holding four
two-bit subgroup multipliers, subgroup s covering columns [8s, 8s+8). The exact
weight is `e2m1mag[c&7] . sign(c) . 2^(scale-127) . (1 + k_s/4)`. The fold of the
OCP rows applies unchanged (`r4d_mxfp4.h`): `d = Wref[n] - E8M0[n][blk]` against
the row-maximum reference goes through a table, `2^(Wref[n]-127)` once in the
epilogue -- Wref is still the row's largest exponent byte, so d >= 0 holds for
every block, and the largest element any table cell then holds is the x1.75
magnitude 10.5 . 2^-d, an e4m3 normal at every d >= 0. `r4d_m2xfp4.h`'s
`kM2xfp4Mag[16][4][2]` is the mxfp4 table with the second index: its k = 0 rows
are `kMxfp4Mag` byte for byte; k = 1, 2 are exact wherever the product is normal;
k = 3 rounds the odd-mantissa magnitudes (1.5, 3, 6 land on the tie's even side,
2.5, 5, 10 scaled) -- at most 2^-4.4 relative on those elements, accepted here
because the CPU decode and the reference stay exact. The unpack is the same four
v_perm_b32 per eight weights, one table row [d][k_s].
**Memory organization.** The relayout (`r4d_layout.cpp`, m2xfp4) stores the codes
in the skinny kernels' WMMA fragment order and appends the meta plane to the same
stored blob in the exponent plane's [K/32][N] order and row pitch -- one byte per
(weight row, 32 of K), read beside the exponent byte with the staging geometry
the scales already use, so the weight stream grows by one byte per sixteen. The
weight operand takes the container's `codes` and `meta` planes; `bscale` and
`bref` are the OCP rows' own arrangements and derivations. Split-K, the
last-arriver reduction, the scratch contract, the concurrency promise (counters
keyed on the output) and the ks/bk/nt and tn tunables are the OCP rows', with
this row's own state.
**Coverage.** `r4d_emu_m2xfp4` holds the table to arithmetic over every
(d, k, magnitude) cell and the unpack to it over every code, then runs both
kernels against an f64 reference of the exact decode at the FP8 WMMA's tolerance
(both activation scale grids, a zero block, both K slabs, the one-fragment form
and the split law). `r4d_selftest --case gemm_m2xfp4a8_decode` (and `_tiled`)
runs the rows on the card through their own relayout, including parts and both
grids, at the tolerance above plus the k = 3 margin (4e-2 rel_l2).
The same encoding is served on the host by libref's and libavx's `gemm_nt_q`
m2 arms (the concatenated canonical planes; ref_gemm.cpp's gemm_m2), which the
row checker holds to the quantiser's own decode.
**Speed.** rad-kbench reaches the rows: `data/kernels_m2xfp4_m{1,8,16,128}.rkb`
are recorded from a skeleton of an m2xfp4 27B container (weights drawn from
seeds, one file a --max-tok pass, replayed together; the mx side comes from
`--record-rows` over the same cases, which re-encodes the weight between the
mx/m2 families -- tools/rad_kbench.cpp's reencode_gemmx_weight). The numbers
below are `--timing-only --report` cold medians, each launch reading its rotated
weights from memory, gfx1201:
| shape | M | mxfp4 us | m2 us | delta |
|---|---|---|---|---|
| N=1024 K=5120 | 1 | 11.3 | 13.3 | +17.5% |
| N=1024 K=5120 | 16 | 13.9 | 15.8 | +13.8% |
| N=1024 K=5120 | 128 | 44.3 | 44.7 | +0.8% |
| N=5120 K=6144 | 1 | 32.1 | 38.3 | +19.2% |
| N=5120 K=6144 | 16 | 34.1 | 40.2 | +17.9% |
| N=5120 K=6144 | 128 | 100.9 | 104.5 | +3.6% |
| N=5120 K=17408 | 1 | 78.5 | 86.7 | +10.5% |
| N=5120 K=17408 | 16 | 89.7 | 100.5 | +12.0% |
| N=5120 K=17408 | 128 | 211.2 | 222.8 | +5.5% |
| N=12288 K=5120 | 1 | 56.4 | 59.3 | +5.3% |
| N=12288 K=5120 | 128 | 113.8 | 124.7 | +9.6% |
| N=16384 K=5120 | 64 | 94.6 | 103.0 | +8.9% |
| N=16384 K=5120 | 128 | 156.8 | 169.1 | +7.8% |
| N=34816 K=5120 | 1 | 150.1 | 167.8 | +11.8% |
| N=34816 K=5120 | 16 | 157.1 | 173.8 | +10.7% |
| N=34816 K=5120 | 64 | 206.2 | 223.1 | +8.2% |
| N=34816 K=5120 | 128 | 354.7 | 370.5 | +4.4% |
An earlier harness that drove the row's own ABI launch with the meta plane as a
separate 5-6 MiB buffer measured +0.7-3.6% at M <= 16: that buffer sat warm in
L2, and in the engine -- and in a cold kbench pass, where the meta rides the
weight blob and rotates with it -- the M <= 16 premium is 5-19% instead. Past
the DTM 1 -> 2 band switch at M = 17 the extra byte hides again (0.4-10%); the
whole-M-ladder fixtures carry 1, 2, 4, 8, 9, 16, 18, 36, 64, 65, 128.
One tuning attempt was made at the M >= 64 points where the delta exceeds 8%:
staging the slab's exponent and meta bytes into LDS once (wide coalesced loads
replacing the duplicated scalar global reads the unpack loop makes). It was
reverted: the W-stage's paired-row store writes one row past sW for the last
row pair -- a latent out-of-bounds LDS store the original layout absorbed --
and beside newly staged LDS it corrupts the staged bytes; guarding that slot
changed the error without removing it, and the residual mismatch stayed
undiagnosed. The numbers above are the unmodified kernel.