From 824e2389961b737305ce6e309bff84dae3365423 Mon Sep 17 00:00:00 2001 From: mici81 Date: Thu, 22 Oct 2026 12:23:51 +0000 Subject: [PATCH] MXFP4 tiled GEMM: review fixes, tests and docs Carried from the series' review rounds when it was split: numeric cases for the plain kernel at a whole row of blocks (TN=4 forced and by the rule from 1024 rows, TN=2 at the 64-wide slab), the plain kernel's comment, and libr4d/README.md's account of the row with the measurements behind TN=4 from 1024 rows, the slab-ahead weight load and the thin last row. Co-Authored-By: Claude Opus 5.5 (1M context) --- libr4d/README.md | 22 ++++++++++++++++++++++ libr4d/r4d_gemm_mxfp4a8_tiled.hip | 5 +++-- libr4d/r4d_selftest.cpp | 6 ++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libr4d/README.md b/libr4d/README.md index acc2529b..6eaca490 100644 --- a/libr4d/README.md +++ b/libr4d/README.md @@ -110,6 +110,28 @@ diverge by about 1 ULP. 6. Add a case to `libr4d/r4d_selftest.cpp` so the row is called at least once; a row without one reports `no smoke case`. +## The OCP-MXFP4 tiled GEMM (prefill) + +**`gemm_mxfp4a8_tiled`** (`gemm_nt_q` at `mxfp4a8`, the rows past the decode band). TN = 4 (256 × +128 blocks) from 1024 rows (it was 2048), unless its blocks would number under 128: at the +crossover Qwen3.8-27B's gate_up -2 %, down -3 %, TranslateGemma 27B's gate_up -9 %, its down +1 %, +and at 1536 rows all four better; TranslateGemma's 1024-row chunks run there. The weight words a +thread stages are loaded into registers a slab ahead, right after the staging barrier, so their +DRAM round trip runs under the slab's WMMAs (+1 VGPR at TN = 4, no spills). At 1024 rows Qwen +gate_up 804 → 768 µs, down 443 → 417, ssm_in 396 → 384, ssm_out 249 → 231, q|gate 306 → 318; +TranslateGemma gate_up 1063 → 1029, down 651 → 617; at 2048 rows 0..-4 %; a 41k-token Qwen3.8-27B +prefill on two cards 8.57 → 8.28 s. A thin last row of blocks (1..160 rows, M under 1024) runs a +second kernel whose blocks of that row skip the WMMAs of fragments past M, where the plain kernel +computes them on clamped copies (half the only block's math at 129 rows); every other M runs the +plain kernel, whose code is instruction for instruction the single kernel's from before the split +(a guard inside the one kernel cost full blocks 1..15 %). Same-session A/B over 12 shapes: M +129..160 -8..-20 %, 320..384 -2..-16 % (Qwen gate_up at 320 rows 402 → 339 µs, TranslateGemma's 513 +→ 441). Each of the three keeps the fold, the WMMAs and their order per output byte. Times are one +rank's weight at TP2 on an R9700, the row alone, clocks warmed; over 20 shapes, M 129..2048: median +-7.4 % over 140 cells, none slower beyond run-to-run noise. + +`tn` (0 the rule, 2, 4) pins the TN choice through `RADIANCE_TUNE`. + ## Notes on the kernels Three properties of the architecture are assumed throughout and not restated per kernel: diff --git a/libr4d/r4d_gemm_mxfp4a8_tiled.hip b/libr4d/r4d_gemm_mxfp4a8_tiled.hip index 65238eee..691d7dce 100644 --- a/libr4d/r4d_gemm_mxfp4a8_tiled.hip +++ b/libr4d/r4d_gemm_mxfp4a8_tiled.hip @@ -34,7 +34,7 @@ // // The weight words and exponents a thread stages are loaded a slab ahead, into registers, right // after the staging barrier, so their DRAM round trip runs under the slab's WMMAs instead of in -// front of the next unpack (+1 VGPR at TN=4; r4d_selftest --perfmx, warmed: Qwen3.8 gate_up at +// front of the next unpack (+1 VGPR at TN=4; the row alone, clocks warmed: Qwen3.8 gate_up at // 1024 rows 804 -> 768 us, down 443 -> 417, TranslateGemma down 651 -> 617, attention q|gate // 6144 x 5120 306 -> 318; at 2048 rows 0..-4 %). Loading the A fragments a slab ahead too (+38 // VGPRs) is slower everywhere. @@ -323,7 +323,8 @@ __device__ __forceinline__ void r4d_gemm_mxfp4a8_tiled_body( } } -// The plain kernel: every block the unguarded body, over the body's own LDS (the kernel as it always was). +// The plain kernel: every block the unguarded body, over the body's own LDS (the single kernel's code +// from before the part kernel split off). template __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel( const unsigned char* __restrict__ AT, const float* __restrict__ As, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 0fb90270..298c91ef 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -25992,6 +25992,12 @@ static void device_phase(const Plugin& pl) { mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 640, 2, true); mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 400, 640, 4, true); mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 2100, 512, 2560, 0, true); + /* The plain kernel at a whole row of blocks (not the part kernel, which takes a last row of + * 1..160 under 1024 rows): TN=4 forced and by the rule from 1024 rows, and TN=2 at the + * 64-wide slab (K not a multiple of 128). */ + mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 256, 400, 640, 4, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 1024, 4096, 640, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 512, 272, 576, 2, true); /* K not a multiple of 128: the TN=2 kernel takes the 64-wide slab. */ mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 576, 2); mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 576, 2, true);