Archived
MXFP4 decode rows: review fixes and docs
The parts of the series' review rounds that concern these rows, carried over when the series was split: - gemm_mxfp4a8_decode: the 64-wide slab is chosen by the split the launch runs (a pin or a tuned point included), as before the register kernel; the header states the split-row bound the rule applies (ks*M = 1024). - r4d_selftest: the priority order of the MXFP4 rows is checked (gemv / gemv_wide over decode, decode_shortk over gemv_wide, decode over tiled); gemv_wide's numeric cases at K 256 and at 56 rows; the M-invariance check names the decode_shortk corner it does not cover. - Comments: gemv_wide's two-tile condition (N >= 4096, K >= 3072, an even tile count), the decode_shortk corner's bit change at M = 33, decode_shortk in the list of rows whose state is keyed on the output. - libr4d/README.md: the rows by row count, with the measurements behind each band; docs/TOOLS.md: r4d_selftest and its --perfmxd mode. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
40d205929a
commit
fb880cbc2d
6 files changed
+140
-14
No files matched your search
+24
-1
@@ -18,7 +18,8 @@ live in `scripts/`:
|
||||
| [`scripts/`](#the-shell-harnesses) | bring a model up, measure it, assert on what comes back |
|
||||
|
||||
[`rad-avx-check`](#rad-avx-check), which checks a host kernel library against libref without a GPU,
|
||||
is built beside libavx rather than installed.
|
||||
is built beside libavx rather than installed, and [`r4d_selftest`](#r4d_selftest), libr4d's own
|
||||
check and kernel timer, into the build's `bin/`.
|
||||
|
||||
`rad-convert`, `rad-kbench` and `rad-tune` run the engine's startup with a different last step:
|
||||
the same plugins, in the same hierarchy, through the same declare phase, so they resolve kernels
|
||||
@@ -727,6 +728,28 @@ serves fails in either case: the selector would resolve to it and the step would
|
||||
|
||||
---
|
||||
|
||||
## r4d_selftest
|
||||
|
||||
```
|
||||
r4d_selftest <libr4d.so> [--case <row>]
|
||||
r4d_selftest <libr4d.so> --perf N K M [iters] (and the other --perf* modes in its usage text)
|
||||
```
|
||||
|
||||
With no mode it runs libr4d's catalogue and static checks, then each row's smoke and numeric cases
|
||||
in a child process of its own, the fusion claims compared byte for byte (ctest runs it as
|
||||
`r4d_selftest`, labels `slow`, `gpu`); `--case` runs one row in this process, as the reproducer.
|
||||
The `--perf*` modes time rows at one shape on a card. Two that cover the dense GEMMs:
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| `--perf N K M [iters]` | the fp8 decode GEMMs, and the fp8 head (`logits_gemm_fp8`) |
|
||||
| `--perfmxd N K M [iters]` | the dense OCP-MXFP4 rows (`gemm_mxfp4a8_gemv`, `_decode`, `_nt_m64`, `_tiled`) at one shape, with the weight bandwidth each reaches |
|
||||
|
||||
`--perfmx` is the MXFP4 expert GEMM (`M`, experts, `top_k`, skew), not the dense rows. `--cold MIB`
|
||||
times each launch alone behind a cache flush instead of back to back.
|
||||
|
||||
---
|
||||
|
||||
## rad-tokcheck
|
||||
|
||||
```
|
||||
|
||||
@@ -52,6 +52,8 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set.
|
||||
| `gemm_w8a16_nt_m64` | 8-bit weight, f16 activation, skinny |
|
||||
| `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_gemv` | the same operands for one to eight rows: the weight streamed straight to registers, no LDS staging; WMMA when K is a multiple of 128, `v_dot4` otherwise, chosen from K alone so a row's bits do not depend on M |
|
||||
| `gemm_mxfp4a8_gemv_wide` | its WMMA form at 9..64 rows: up to four 16-row tiles a wave over each weight word, two 16-column tiles a block on wide, deep weights |
|
||||
| `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 |
|
||||
@@ -111,6 +113,89 @@ 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 GEMM by row count
|
||||
|
||||
`gemm_nt_q` at `mxfp4a8` has six rows over one stored weight (the skinny kernel's fragment-order
|
||||
codes, `[K/32][N]` E8M0 exponents and a per-row reference exponent), so the selector moves between
|
||||
them by M with no second copy. Each needs N a multiple of 16 and K of 64; of those whose
|
||||
constraints hold, the highest priority serves:
|
||||
|
||||
| row | band | priority |
|
||||
| --- | --- | --- |
|
||||
| `gemm_mxfp4a8_decode_shortk` | 33 ≤ M ≤ 64, 8192 ≤ N ≤ 36864, K ≤ 2560 | 59 |
|
||||
| `gemm_mxfp4a8_gemv` | M ≤ 8 | 58 |
|
||||
| `gemm_mxfp4a8_gemv_wide` | 9 ≤ M ≤ 64, K a multiple of 128 | 58 |
|
||||
| `gemm_mxfp4a8_decode` | M ≤ 128, N ≤ 36864 | 55 |
|
||||
| `gemm_mxfp4a8_nt_m64` | M ≤ 64 | 50 |
|
||||
| `gemm_mxfp4a8_tiled` | any M | 30 |
|
||||
|
||||
One stream's decode (M = 1 + the draft depth) takes `gemv`; several streams with speculation land
|
||||
in `gemv_wide` (4 × (1 + 7) = 32 rows, 16 × (1 + 3) = 64); `decode` keeps 65..128 rows, and 9..64
|
||||
where K is not a multiple of 128; `tiled` takes the rest. Unless stated, times are one rank's
|
||||
weight of Qwen3.8-27B or TranslateGemma 27B at TP2 on an R9700, each row alone (`r4d_selftest
|
||||
--perfmxd`). Over 16 per-rank shapes of other models and card counts (Qwen3.8-27B at TP4 and TP8,
|
||||
1B-, 4B- and 8B-class dense at TP1 and TP2, a 31B-class at TP4), M 1..128, the selection below
|
||||
against the better of the decode and skinny rows before it: median -10.3%, mean -10.6% over 192
|
||||
cells.
|
||||
|
||||
**`gemm_mxfp4a8_gemv`** (M ≤ 8). One stream's decode is a pure weight stream, and staging the
|
||||
weight through LDS left narrow projections at a third to two thirds of DRAM bandwidth. This row
|
||||
loads each weight word straight into registers and folds it to e4m3 there: WMMA from register
|
||||
fragments when K is a multiple of 128, `v_dot4` otherwise. The form and the waves a block depend on
|
||||
K alone, so a row's bits do not depend on M, and a verify step's first row is the plain decode of
|
||||
it (`r4d_selftest` checks the first row's bits against M). The scale and reference planes must be
|
||||
4-byte aligned.
|
||||
|
||||
**`gemm_mxfp4a8_gemv_wide`** (9..64 rows, K a multiple of 128). The WMMA form with up to four 16-row
|
||||
tiles a wave: each weight word of a chunk is loaded and folded once and multiplied with every
|
||||
tile's activation fragment, so the weight streams once whatever the row count. A row's sum is its
|
||||
tile's WMMA chain alone, so its bits are `gemv`'s at every M (except in the `decode_shortk` corner,
|
||||
where the decode kernel serves 33..64 rows). From 25 rows, where N ≥ 4096, K ≥ 3072 and the tile
|
||||
count is even, a block carries two 16-column tiles: the activation is read per block, and with one
|
||||
tile a 64-row activation was read N/16 times (356 MB from L2 on a 17408-row gate_up, against a 47 MB
|
||||
weight). On narrower or shallower weights two tiles lost 4..37 % (fewer blocks than the part has
|
||||
units, a short K a wave), so they stay at one. Against the decode row that served 9..64 rows
|
||||
before, in µs: Qwen down 51-80 against 56-105, ssm_out 27-42 against 30-55, q|gate 40-63 against
|
||||
44-74; TranslateGemma down 61-97 against 66-123, o 22-35 against 25-51; gate_up at 24-48 rows
|
||||
110-141 against 145-170. With two tiles a block, 17408 × 5120 at 32 rows 125 → 85 µs and at 64
|
||||
157 → 122, 21504 × 5376 154 → 111 and 183 → 150.
|
||||
|
||||
**`gemm_mxfp4a8_decode_shortk`** (33..64 rows, 8192 ≤ N ≤ 36864, K ≤ 2560). The decode row's
|
||||
kernel, scratch hook and tunables at priority 59, above `gemv_wide`. On a short K a `gemv_wide`
|
||||
block's three or four row tiles make its cross-wave reduction outweigh its WMMAs, and a wide N
|
||||
makes many such blocks. Over 24 per-rank shapes (Qwen3.8 and TranslateGemma at TP1, 2, 4 and 8,
|
||||
and 1B- to 8B-class dense) at 24..64 rows, `gemv_wide` matched or beat the previous decode kernel
|
||||
everywhere but this corner, where it lost by 7..30 %; there this row wins:
|
||||
|
||||
| shape, 64 rows | `gemv_wide` | previous decode kernel | `decode_shortk` |
|
||||
| --- | --- | --- | --- |
|
||||
| 19456 × 2560 | 131.8 µs | 104.5 | 93.3 |
|
||||
| 16384 × 2048 | 92.8 | 76.1 | 68.6 |
|
||||
| 9728 × 2560 | 74.1 | 67.4 | 52.5 |
|
||||
|
||||
Narrower N stays with `gemv_wide` (5120 × 2176 at 40 rows 39.2 µs against 52.8), as do Qwen3.8-27B's
|
||||
and TranslateGemma's two-rank shapes. It is a row rather than a rule inside the gemv entry so that
|
||||
the choice stays a lookup over constraints, and `--debug-selection` names it.
|
||||
|
||||
**`gemm_mxfp4a8_decode`** (65..128 rows, and 9..64 where K is not a multiple of 128). Past 16 rows
|
||||
each wave loads its weight words straight into registers, as `gemv` does (a fragment-order word is
|
||||
the WMMA fragment's register), and only the shared activation is double-buffered in LDS, one
|
||||
barrier a slab. The fold, k-step, slab order, scale carry and epilogue are unchanged, and the whole
|
||||
output was compared byte for byte with the LDS-staged kernel's (312 cases, 17..128 rows, both scale
|
||||
grids, forced splits): 17408 × 5120 at 96 rows 205 → 159 µs, at 128 245 → 214; 5120 × 8704 at 128
|
||||
169 → 139; 6144 × 5120 at 128 121 → 92. The split and slab rule past 64 rows was then re-tuned for
|
||||
this kernel: the 128-wide slab always, and a split only while every split keeps 16 slabs, towards a
|
||||
48-block grid or, at K ≥ 8192, regardless, up to ks·M = 1024 split rows. At 80 / 96 / 128 rows:
|
||||
17408 × 5120 161 / 187 / 215 → 161 / 175 / 197 µs, 5120 × 3072 71 / 79 / 67 → 55 / 60 / 69, 21504 ×
|
||||
5376 192 / 207 / 228 → 187 / 200 / 217. A different split sums a row in a different order, so at
|
||||
65..128 rows the re-tuned rule moves output bits within the selftest's bound: the one change in
|
||||
this family that is not bit for bit. Up to 64 rows the rule is as it was.
|
||||
|
||||
**Tunables.** `gemv` and `gemv_wide` have none, so a `RADIANCE_TUNE` pin on `gemm_nt_q` passes
|
||||
them by. `decode_shortk` shares the decode row's axes (`ks`, `bk`, `nt`; 0 the rule), so a pin of
|
||||
those reaches both. A pin applies to every row serving the op, and an axis a serving row lacks
|
||||
stops startup by name.
|
||||
|
||||
## Notes on the kernels
|
||||
|
||||
Three properties of the architecture are assumed throughout and not restated per kernel:
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
// - past 64 rows, re-tuned for the register kernel (r4d_selftest --perfmxd, warmed, every split
|
||||
// 1..8 at both slabs on the 27B's and TranslateGemma's per-rank shapes at 96 and 128 rows): the
|
||||
// 128-wide slab always (it never lost to 64), and a split only while every split keeps 16 slabs,
|
||||
// towards a 48-block grid or, at K >= 8192, regardless, up to ks*M = 512 split rows. Against the
|
||||
// towards a 48-block grid or, at K >= 8192, regardless, up to ks*M = 1024 split rows. Against the
|
||||
// old rule at 96 / 128 rows: 5120 x 8704 116 / 160 -> 116 / 149 us (a split of 4), 6144 x 5120
|
||||
// 97 / 105 -> 87 / 109, 5120 x 3072 79 / 67 -> 60 / 69 (none), 17408 x 5120 187 / 215 -> 175 / 197
|
||||
// (the wide slab). A different split changes a split row's summation order, so bits move here.
|
||||
@@ -526,7 +526,6 @@ MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
|
||||
const int tm = (M + R4D_MXD_MTILE - 1) / R4D_MXD_MTILE;
|
||||
MxdPlan p;
|
||||
int ks = 1;
|
||||
bool slab64 = false;
|
||||
if (tm > 4) {
|
||||
// Past 64 rows (the register kernel; see the header): split only while every split keeps 16
|
||||
// 128-wide slabs, towards a 48-block grid, or at a deep K regardless.
|
||||
@@ -539,11 +538,12 @@ MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
|
||||
while (ks < R4D_MXD_MAX_KS && nblk * ks < fill && 2 * ks * M <= 512) ks <<= 1;
|
||||
if (K >= 8192) // a deep K: a split of 24 or more slabs still waits on them
|
||||
while (ks < R4D_MXD_MAX_KS && (K / 128) / ks >= 24 && 2 * ks * M <= 512) ks <<= 1;
|
||||
// 64-wide slabs where LDS per block would cost resident blocks: DTM 4 unsplit on 110+ column
|
||||
// blocks.
|
||||
slab64 = tm == 4 && ks == 1 && nblk >= 110;
|
||||
}
|
||||
p.ks = KS > 0 ? KS : ks;
|
||||
// 64-wide slabs where LDS per block would cost resident blocks: DTM 4 unsplit on 110+ column
|
||||
// blocks, by the split the launch runs (a pin or a tuned point included), as before the register
|
||||
// kernel; past 64 rows the 128-wide slab always.
|
||||
const bool slab64 = tm == 4 && p.ks == 1 && nblk >= 110;
|
||||
p.bk = BK > 0 ? BK : (slab64 ? 64 : 128);
|
||||
p.nt = NT == 1 || (NT == 0 && (long)N * K / 2 >= (4l << 20));
|
||||
if (K % p.bk) { // a slab may not run past K
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
// Two forms, chosen from K alone so that a row's output bits do not depend on how many rows the
|
||||
// step carries (a verify step's first row is bit-identical to a plain decode of it):
|
||||
//
|
||||
// - K a multiple of 128: the WMMA form. One 16-column tile a block (two from 25 rows), eight waves on
|
||||
// - K a multiple of 128: the WMMA form. One 16-column tile a block (two from 25 rows where N >= 4096,
|
||||
// K >= 3072 and the tile count is even), eight waves on
|
||||
// its K in chunks of one 128 block, and up to four 16-row tiles (M <= 64), each weight word
|
||||
// multiplied with every row tile's fragment once it is loaded. A lane loads its own B-fragment word of each k-step straight
|
||||
// from the fragment-order weight (the order is the fragment's), the rows sit in the A fragments
|
||||
|
||||
+7
-5
@@ -4812,7 +4812,8 @@ 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, except
|
||||
* the decode kernel's short-K corner below. */
|
||||
* in the decode kernel's short-K corner below, which sums 33..64 rows in the decode kernel's order (a
|
||||
* row's bits there change at M = 33). */
|
||||
static const RadConstraint cGemmMxfp4A8GemvWide[] = {
|
||||
C_GE("M", 9), C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_IN("dtype", "mxfp4a8"),
|
||||
};
|
||||
@@ -9453,10 +9454,11 @@ extern "C" const RadKernelInfo* rad_kernel_at(int i) {
|
||||
* every collective the peer flags and exchange buffers the all-reduces own
|
||||
*
|
||||
* gemm_fp8a8_nt_m16's finisher counters are keyed on the stream and the output;
|
||||
* gemm_mxfp4a8_decode's split partial is the op's scratch and its arrival counters are keyed on the
|
||||
* output alone; router_gemm_topk_scatter_bf16's arrival counter belongs to its op instance (taken
|
||||
* at init, one per declared op and band). Concurrent launches never share these, so those three
|
||||
* rows are concurrent. A new row with stream-ordered state belongs in the list above.
|
||||
* gemm_mxfp4a8_decode's (and gemm_mxfp4a8_decode_shortk's, the same entry) split partial is the
|
||||
* op's scratch and its arrival counters are keyed on the output alone;
|
||||
* router_gemm_topk_scatter_bf16's arrival counter belongs to its op instance (taken at init, one
|
||||
* per declared op and band). Concurrent launches never share these, so those four rows are
|
||||
* concurrent. A new row with stream-ordered state belongs in the list above.
|
||||
*
|
||||
* lora_* ordered on purpose: the core appends them behind the GEMM whose `y` they
|
||||
* update and refills the pools they read between steps with its own copies,
|
||||
|
||||
+17
-2
@@ -468,6 +468,19 @@ static void cross_check(const Plugin& pl) {
|
||||
drain(1 << 22, 2, true) == 3 && drain(1 << 22, 2, false) == 3 &&
|
||||
drain(1 << 22, 8, true) == 3 && drain(1 << 22, 3, true) == 3);
|
||||
}
|
||||
/* The OCP-MXFP4 rows by row count: the bands overlap, and the priority picks. */
|
||||
{
|
||||
auto prio = [&](const char* n) {
|
||||
for (int k = 0; k < nk; ++k)
|
||||
if (!std::strcmp(pl.kernel_at(k)->name, n)) return pl.kernel_at(k)->priority;
|
||||
return -1;
|
||||
};
|
||||
const int gv = prio("gemm_mxfp4a8_gemv"), gw = prio("gemm_mxfp4a8_gemv_wide");
|
||||
const int dc = prio("gemm_mxfp4a8_decode"), sk = prio("gemm_mxfp4a8_decode_shortk");
|
||||
const int tl = prio("gemm_mxfp4a8_tiled");
|
||||
ok("the MXFP4 rows rank gemv / gemv_wide over decode, decode_shortk over gemv_wide, decode over tiled",
|
||||
gv > 0 && gw > 0 && dc > 0 && sk > 0 && tl > 0 && gv > dc && gw > dc && sk > gw && dc > tl);
|
||||
}
|
||||
std::printf("\n");
|
||||
}
|
||||
|
||||
@@ -4395,7 +4408,8 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
|
||||
|
||||
/* A row's output bits must not depend on how many rows ride with it: a verify step's first row
|
||||
* then decodes exactly as the plain step would, and greedy text does not change with the draft
|
||||
* depth. The first row of M = 1..8 (identical inputs, mxfp4a8_numeric's activations depend on the
|
||||
* depth. (This checks the named row; on N >= 8192, K <= 2560 the selection hands 33..64 rows to
|
||||
* gemm_mxfp4a8_decode_shortk, whose order differs.) The first row of M = 1..8 (identical inputs, mxfp4a8_numeric's activations depend on the
|
||||
* row index only) compared bit for bit. */
|
||||
static void mxfp4a8_m_invariant(const Plugin& pl, const char* row, int64_t N, int64_t K, bool ablk) {
|
||||
if (!sel(row)) return;
|
||||
@@ -26919,7 +26933,8 @@ static void device_phase(const Plugin& pl) {
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 32, 17408, 5120, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 40, 5376, 10752, 0, true);
|
||||
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", 33, 16, 256, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_gemv_wide", 56, 1072, 5120, 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. */
|
||||
|
||||
Reference in new issue
Block a user