From f3429d8c13e4419b41bd9852b7a8bfd1a7fa3a43 Mon Sep 17 00:00:00 2001 From: mici81 Date: Wed, 21 Oct 2026 01:48:32 +0000 Subject: [PATCH] gemm_mxfp4a8_decode_shortk: the decode kernel at 33..64 rows of a wide, short weight, above gemv_wide gemv_wide's blocks hold three or four row tiles over the whole K, so on a short K the cross-wave reduction of a block outweighs its WMMAs, and on a wide N there are many such blocks. Swept over 24 per-rank shapes (Qwen3.8 and TranslateGemma at TP1/2/4/8, 1B/4B/8B-class dense) at 24..64 rows, each row timed alone: gemv_wide is at least upstream's decode kernel everywhere except N >= 8192 with K <= 2560 past 32 rows, where it lost by 7..30 % (19456 x 2560 at 64 rows 131.8 us against upstream's 104.5; 16384 x 2048 92.8 / 76.1; 9728 x 2560 74.1 / 67.4). This decode kernel takes that corner: 93.3, 68.6 and 52.5 us. A row of its own (same entry, scratch hook and tunables as the decode row, priority 59, constraints M 33..64, N 8192..36864, K <= 2560) rather than a rule in the gemv entry, so the planner's choice stays a table lookup. Narrower N stays with gemv_wide (5120 x 2176 at 40 rows 39.2 against 52.8), as does every shape of the models we serve. r4d_selftest: five numeric cases over the corner, both activation grids and parts. Co-Authored-By: Claude Opus 5.5 (1M context) --- libr4d/README.md | 1 + libr4d/r4d_plugin.h | 3 +++ libr4d/r4d_rows.cpp | 28 +++++++++++++++++++++++++++- libr4d/r4d_selftest.cpp | 7 +++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/libr4d/README.md b/libr4d/README.md index acc2529b..c4a91b65 100644 --- a/libr4d/README.md +++ b/libr4d/README.md @@ -53,6 +53,7 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set. | `gemm_w2a8_nt` | 2-bit weight against int8 activation: the DFlash2 draft head | | `gemm_mxfp4a8_nt_m64` | OCP-MXFP4 weight, fp8 activation, skinny | | `gemm_mxfp4a8_decode` | the same operands for the decode band (M ≤ 128): split-K across workgroups with a last-arriver reduction, the partial in the op's scratch | +| `gemm_mxfp4a8_decode_shortk` | the decode kernel at 33..64 rows of a wide, short weight (N ≥ 8192, K ≤ 2560), above `gemm_mxfp4a8_gemv_wide`, which loses there | | `gemm_mxfp4a8_tiled` | the prefill sibling: the activation re-tiled into fragment order in the op's scratch and read straight into registers, the weight unpacked once per K slab for a 256-row block | | `attn_decode_h128_gqa4_fp8kv` / `..._bf16kv` | head_dim 128, 4 queries per KV head, sliding window: a block-diffusion drafter's geometry. Decode only; there is no prefill row at this geometry | | `rowtopk_bf16` / `rowtopk_bf16_chunks` | exact per-row top-K over a bf16 matrix, two-stage | diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 6fa2894e..3c9a7313 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -51,6 +51,9 @@ enum { R4D_PRIO_GEMM_MXDEC = 55, /* The MXFP4 gemv row (one to eight rows) outranks the decode row, whose band it shares there. */ R4D_PRIO_GEMM_MXGEMV = 58, + /* The decode kernel's short-K row outranks gemv_wide in the one corner of its band where the + * decode kernel is faster (33..64 rows, N >= 8192, K <= 2560). */ + R4D_PRIO_GEMM_MXDEC_SHORTK = 59, /* The skinny m64 row has no upper bound on M, so the tiled bf16 row (M > 64 only) must outrank * it, and stays below m16, whose band it never enters. */ R4D_PRIO_GEMM_BF16_TILED = 55, diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 93660471..cc5a0f1a 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4811,10 +4811,22 @@ static const RadConstraint cGemmMxfp4A8Gemv[] = { }; /* gemm_mxfp4a8_gemv_wide: the same entry at 9..64 rows, where only the WMMA form serves (K % 128). * The form's sum order for a row is its 16-row tile's chain alone, so a row's bits are the same in - * both rows at every M. Several streams with speculation (4 x 1 + 7 drafts = 32) land here. */ + * both rows at every M. Several streams with speculation (4 x 1 + 7 drafts = 32) land here, except + * the decode kernel's short-K corner below. */ static const RadConstraint cGemmMxfp4A8GemvWide[] = { C_GE("M", 9), C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"), }; +/* gemm_mxfp4a8_decode_shortk: the decode entry at 33..64 rows of a wide, short weight, above + * gemv_wide by priority. There gemv_wide's blocks hold three or four row tiles over a short K each, + * so the cross-wave reduction of a block weighs more than its WMMAs, and the decode kernel's split + * wins (R9700, each row alone, ours' gemv_wide / decode / upstream's decode, us: 19456 x 2560 at 64 + * rows 131.8 / 93.3 / 104.5, 16384 x 2048 92.8 / 68.6 / 76.1, 9728 x 2560 74.1 / 52.5 / 67.4). + * Narrower N stays with gemv_wide (5120 x 2176 at 40 rows 39.2 against the decode kernel's 52.8), + * as does K past 2560 (17408 x 5120 at 40 rows 119.3 against 129.0). */ +static const RadConstraint cGemmMxfp4A8DecodeShortK[] = { + C_GE("M", 33), C_LE("M", 64), C_GE("N", 8192), C_LE("N", 36864), C_LE("K", 2560), C_DIV("K", 64), + C_DIV("N", 16), C_IN("dtype", "mxfp4a8"), +}; /* r4d_gemm_mxfp4a8_tiled. Entry point: N % 16 (the fragment-order weight), K % 64 (its narrowest * K slab), M >= 1. The decode and skinny rows outrank it in their bands by priority, so the band * starts at 1 rather than at a hand-picked crossover. */ @@ -7554,6 +7566,20 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, nullptr, nullptr, r4d_rad_gemm_mxfp4a8_decode, r4d_rad_gemm_mxfp4a8_decode_scratch, r4d_rad_layout_mxfp4, r4d_rad_relayout_mxfp4 }, +{ "gemm_mxfp4a8_decode_shortk", "gemm_nt_q", "gemm", + "the decode row's kernel at 33 to 64 rows of a wide, short weight (N >= 8192, K <= 2560), where " + "its split-K beats gemv_wide, whose blocks there spend more on the cross-wave reduction than on " + "the WMMAs", + "33 <= M <= 64; 8192 <= N <= 36864, a multiple of 16; K <= 2560, a multiple of 64; the skinny " + "kernel's fragment-order weight, [K/32][N] E8M0 exponents and per-row reference exponent; the " + "split partial in the op's scratch", + "e4m3 A with an f32 scale per row or per (row, 128), e2m1 W with an E8M0 scale per 32 K folded " + "into the element against a per-row reference, bf16 C, fp32 accumulate", + RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_MXDEC_SHORTK, + ROW(cGemmMxfp4A8DecodeShortK), ROW(tGemmMxfp4Decode), nullptr, + nullptr, nullptr, r4d_rad_gemm_mxfp4a8_decode, r4d_rad_gemm_mxfp4a8_decode_scratch, + r4d_rad_layout_mxfp4, r4d_rad_relayout_mxfp4 }, + { "gemm_mxfp4a8_tiled", "gemm_nt_q", "gemm", "the OCP-MXFP4 GEMM tiled for PREFILL row counts: the activation re-tiled into fragment order in " "the op's scratch, its fragments read straight into registers, the weight unpacked once per K " diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index a388b789..462d38d3 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -26921,6 +26921,13 @@ static void device_phase(const Plugin& pl) { mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 64, 10288, 640, 0, false); mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 33, 16, 128, 0, true); mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 24, 34816, 5120, 0, true, {}, 17408); + /* decode_shortk: the decode kernel over its corner, both activation grids, a tile partly + * live, and parts. */ + mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode_shortk", 33, 16384, 2048, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode_shortk", 48, 9728, 2560, 0, false); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode_shortk", 64, 19456, 2560, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode_shortk", 50, 8192, 576, 0, true); + mxfp4a8_numeric(pl, "gemm_mxfp4a8_decode_shortk", 40, 19456, 2560, 0, true, {}, 9728); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 5120, 5120, true); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 1088, 576, true); mxfp4a8_m_invariant(pl, "gemm_mxfp4a8_gemv", 1056, 576, false);