Archived
MXFP4 gated fold: review fixes and docs
The parts of the series' review rounds that concern the fold, carried over when the series was split: - gemm_mxfp4a8_gated_gemv: operand dtypes checked, the activation scale grid read as the gemm_nt_q rows read it (mx_ascale_grid), row pitches checked against the widths; the arrival counters are zeroed before any launch reads them (the memset is on the null stream, which the engine's streams need not wait for). - The layout hooks refuse an operand the gated row does not have; the GeGLU chain refuses out_bf16 (its bf16 plane is a temporary there). - r4d_selftest: the reference plane through the gemv row's own hook must be the same bytes; the small gated cases at K 256. - libr4d/README.md and docs/TOOLS.md: the row, and --perfmxd's fold timing. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cbc299417e
commit
2721f7c951
8 files changed
+63
-25
No files matched your search
@@ -733,6 +733,7 @@ int Gemma3Mlp::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
// Not under calibration either: the down tap reads the bf16 product, which the fold never writes.
|
||||
{
|
||||
const int64_t hi = g.max_tok < 8 ? g.max_tok : 8;
|
||||
// G3Linear's mx is linear_is_mxfp4, never a rotated (ParoQuant) weight, so one part.
|
||||
if (ln_gu.mx && ln_down.mx && !ln_down.shared_in && !calib_dir() && hi > 0 &&
|
||||
ln_gu.N % 256 == 0 && ln_gu.K % RAD_FP8_BLOCK == 0 &&
|
||||
rad_op_declared(b, "gemm_nt_q_gated_mx")) {
|
||||
|
||||
+1
-1
@@ -743,7 +743,7 @@ The `--perf*` modes time rows at one shape on a card. Two that cover the dense G
|
||||
| | |
|
||||
|---|---|
|
||||
| `--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 |
|
||||
| `--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; at a gate_up shape (N a multiple of 256, K of 128) also `gated_quant_fp8` and the fold that replaces the pair, `gemm_mxfp4a8_gated_gemv` |
|
||||
|
||||
`--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.
|
||||
|
||||
+16
-2
@@ -54,6 +54,7 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set.
|
||||
| `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_gated_gemv` | `gemm_nt_q_gated_mx`: the gemv WMMA form over a gate_up weight with `gated_quant_fp8`'s pass (or GeGLU's `gelu_mul` and `quant_act_fp8`) in its epilogue, M ≤ 8; the [M, 2n] product is never written |
|
||||
| `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 |
|
||||
@@ -197,8 +198,21 @@ rows their outputs differ from the decode row's in the last bf16 place on a few
|
||||
thousand (within the selftest's bound). A greedy text can therefore differ from one produced
|
||||
with the decode row at those rows. Within the family a row's bits do not depend on M, as above.
|
||||
|
||||
**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
|
||||
**`gemm_mxfp4a8_gated_gemv`** (`gemm_nt_q_gated_mx`, M ≤ 8, K a multiple of 128, N of 256, `act`
|
||||
`silu`, or `gelu_tanh` without `out_bf16`). The `gemv` WMMA form over a gate_up weight, a block per
|
||||
16-column tile pair of the gated output (eight K waves on the gate tile, eight on the up tile, in
|
||||
`gemv`'s K order), the bf16 product and row amaxes in the op's scratch; the last of a 128-column
|
||||
group's blocks to arrive (a counter keyed on the output) writes the group's codes and scales. Byte
|
||||
for byte `gemm_mxfp4a8_gemv` followed by `gated_quant_fp8`, or by `gelu_mul` and `quant_act_fp8`
|
||||
(`r4d_selftest`, M = 1..8, Qwen's and TranslateGemma's gate_up). A decode step of Qwen3.8-27B
|
||||
at TP2 issues 631 kernels a card, and a dependent kernel costs 3.5-6.4 µs in a replayed graph on
|
||||
gfx1201 however little it does, so the launch it saves is the point. TranslateGemma 27B alone,
|
||||
one stream at speculation 7: 144.5 / 144.9 → 151.5 / 150.5 tok/s, measured with the first form of
|
||||
this row (a block per 128-column group, the same bytes); the tile-pair form, Qwen3.8-27B without
|
||||
speculation x1 / x4 / x8 +1.4 / +1.8 / +1.5 %.
|
||||
|
||||
**Tunables.** `gemv`, `gemv_wide` and `gated_gemv` 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.
|
||||
|
||||
|
||||
@@ -531,6 +531,9 @@ extern "C" int r4d_fuse_gemm_nt_q_gated_mx(const RadParam* p, int n_p, uint64_t
|
||||
if (!dt || std::strcmp(dt, "mxfp4a8")) return RAD_E_UNSUPPORTED;
|
||||
const bool gelu = act_is(p, n_p, "silu", "gelu_tanh");
|
||||
if (!gelu && !act_is(p, n_p, "silu", "silu")) return RAD_E_UNSUPPORTED;
|
||||
/* The GeGLU chain leaves its bf16 plane in a temporary, so a caller asking for out_bf16 there
|
||||
* has no chain (the shim refuses the operand too). */
|
||||
if (gelu && RAD_FUSE_HAS(present, 7)) return RAD_E_UNSUPPORTED;
|
||||
|
||||
if (step == 0) {
|
||||
Step s(out, "gemm_nt_q");
|
||||
|
||||
@@ -520,7 +520,10 @@ unsigned* mxgg_out_ctr(const void* out) {
|
||||
if (auto it = blocks.find(key); it != blocks.end()) return it->second;
|
||||
unsigned* p = nullptr;
|
||||
if (hipMalloc((void**)&p, R4D_MXGG_MAX_GROUPS * sizeof(unsigned)) != hipSuccess) return nullptr;
|
||||
if (hipMemset(p, 0, R4D_MXGG_MAX_GROUPS * sizeof(unsigned)) != hipSuccess) {
|
||||
// Zero before any launch reads it: the memset is on the null stream, which the engine's streams
|
||||
// need not wait for, so wait here (as mxd_out_ctr and r4d_moe.hip's router counters do).
|
||||
if (hipMemset(p, 0, R4D_MXGG_MAX_GROUPS * sizeof(unsigned)) != hipSuccess ||
|
||||
hipDeviceSynchronize() != hipSuccess) {
|
||||
(void)hipFree(p);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -569,7 +572,10 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* sc = r4d_opt(a, MXGG_S);
|
||||
const RadTensor* y = r4d_opt(a, MXGG_Y);
|
||||
if (!x || !xs || !w || !ws || !wr || !q || !sc) return R4D_NO(RAD_E_INVAL);
|
||||
if (q->rank != 2 || sc->rank != 2 || x->rank < 2) return R4D_NO(RAD_E_SHAPE);
|
||||
if (x->dtype != RAD_F8E4M3 || xs->dtype != RAD_F32 || q->dtype != RAD_F8E4M3 ||
|
||||
sc->dtype != RAD_F32 || (y && y->dtype != RAD_BF16))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
if (q->rank != 2 || sc->rank != 2 || x->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
||||
if (y && y->rank != 2) return R4D_NO(RAD_E_SHAPE);
|
||||
if (r4d_geti_or(a, "group", 32) != 32) return R4D_NO(RAD_E_SHAPE);
|
||||
/* silu (the SwiGLU MLP) or gelu_tanh (GeGLU, gelu_mul's). The GeGLU chain's bf16 plane is
|
||||
@@ -590,21 +596,20 @@ extern "C" int r4d_rad_gemm_mxfp4a8_gated(const RadArgs* a, RadStream s) {
|
||||
if (x->shape[0] != M || r4d_numel(x) != M * K) return R4D_NO(RAD_E_SHAPE);
|
||||
if (sc->shape[0] != M || sc->shape[1] != nout / 128) return R4D_NO(RAD_E_SHAPE);
|
||||
if (y && (y->shape[0] != M || y->shape[1] != nout)) return R4D_NO(RAD_E_SHAPE);
|
||||
/* The activation scale per (row, 128 of K), or per row. */
|
||||
bool ablk;
|
||||
long long as_rs;
|
||||
if (xs->rank == 2 && xs->shape[0] == M && xs->shape[1] == K / 128) {
|
||||
ablk = true;
|
||||
as_rs = r4d_row_pitch(xs);
|
||||
} else if (xs->rank == 1 && xs->shape[0] == M) {
|
||||
ablk = false;
|
||||
as_rs = 1;
|
||||
} else {
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
}
|
||||
/* The activation scale grid as the gemm_nt_q rows read it (mx_ascale_grid), so the fold and the
|
||||
* GEMM it replaces take every scale plane the same way. */
|
||||
GemmOps go{};
|
||||
go.ascale = xs; go.M = M; go.K = K;
|
||||
int as_group = 0, as_rs_i = 1;
|
||||
if (mx_ascale_grid(go, &as_group, &as_rs_i) != RAD_OK) return R4D_NO(RAD_E_SHAPE);
|
||||
const bool ablk = as_group == 128;
|
||||
const long long as_rs = as_rs_i;
|
||||
/* Row pitches; r4d_row_pitch is -1 for a tensor not contiguous past its leading axis. A is read
|
||||
* a dword at a time. */
|
||||
const long long lda = r4d_row_pitch(x), qp = r4d_row_pitch(q), sp = r4d_row_pitch(sc);
|
||||
const long long yp = y ? r4d_row_pitch(y) : 0;
|
||||
if (lda < 0 || as_rs < 0 || qp < 0 || sp < 0 || yp < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (lda < K || lda % 4 || qp < nout || sp < nout / 128 || (y && yp < nout))
|
||||
return R4D_NO(RAD_E_STRIDE);
|
||||
if (!r4d_aligned(x->data, 16) || !r4d_aligned(w->data, 16) || !r4d_aligned(q->data, 4) ||
|
||||
!r4d_aligned(ws->data, 4) || !r4d_aligned(wr->data, 4) || qp % 4)
|
||||
return R4D_NO(RAD_E_ALIGN);
|
||||
|
||||
@@ -942,20 +942,24 @@ extern "C" int r4d_rad_relayout_mxfp4(const RadParam* p, int n_p, int operand,
|
||||
/* gemm_nt_q_gated_mx's weights are gemm_nt_q's mxfp4a8 planes in the same stored forms, but its
|
||||
* `b_ref` is operand 4, where gemm_nt_q has `y`: mapped to gemm_nt_q's index, so both ops ask for
|
||||
* the reference plane in one form and the loader stores it once. */
|
||||
static int mx_gated_operand(int operand) { return operand == 4 ? Q_BREF : operand; }
|
||||
static int mx_gated_operand(int operand) {
|
||||
return operand == Q_B || operand == Q_BSCALE ? operand : operand == 4 ? Q_BREF : -1;
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_layout_mxfp4_gated(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, RadLayout* out) {
|
||||
return r4d_rad_layout_mxfp4(p, n_p, mx_gated_operand(operand), enc, sel, pl, n, out);
|
||||
const int o = mx_gated_operand(operand);
|
||||
return o < 0 ? RAD_E_UNSUPPORTED : r4d_rad_layout_mxfp4(p, n_p, o, enc, sel, pl, n, out);
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_relayout_mxfp4_gated(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* pl, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
return r4d_rad_relayout_mxfp4(p, n_p, mx_gated_operand(operand), enc, sel, pl, n, dst,
|
||||
dst_bytes);
|
||||
const int o = mx_gated_operand(operand);
|
||||
return o < 0 ? RAD_E_UNSUPPORTED
|
||||
: r4d_rad_relayout_mxfp4(p, n_p, o, enc, sel, pl, n, dst, dst_bytes);
|
||||
}
|
||||
|
||||
/* ---- the MoE experts. The weight is staged through LDS, so it is not permuted: the int4 plane is
|
||||
|
||||
+2
-1
@@ -4827,7 +4827,8 @@ static const RadConstraint cGemmNtQGatedFp8[] = {
|
||||
C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "fp8a8"), C_IN("act", "silu"),
|
||||
};
|
||||
/* r4d_gemm_mxfp4a8_gated_gemv: the gemv row's WMMA form (K a multiple of 128, M <= 8) with a block
|
||||
* per 128-column group of the gated output (N/2 a multiple of 128). */
|
||||
* per 16-column tile pair of the gated output; N/2 a multiple of 128, so each 128-column group's eight
|
||||
* blocks meet at its last arriver, which quantises the group. */
|
||||
static const RadConstraint cGemmNtQGatedMx[] = {
|
||||
C_LE("M", 8), C_DIV("K", 128), C_DIV("N", 256), C_EQ("group", 32), C_IN("dtype", "mxfp4a8"),
|
||||
C_IN("act", "silu gelu_tanh"),
|
||||
|
||||
+12
-2
@@ -6121,6 +6121,16 @@ static void mxfp4a8_gated_case(const Plugin& pl, int64_t M, int64_t N, int64_t K
|
||||
ok(nm, false, "relayout refused");
|
||||
return;
|
||||
}
|
||||
/* The reference plane through the gemv row's own hook (its operand 6) must be the same bytes:
|
||||
* a remap that handed the gated row's b_ref another form would otherwise pass, both kernels
|
||||
* reading the same wrong plane. */
|
||||
{
|
||||
std::vector<uint8_t> wr_gemv;
|
||||
if (stored(rv, lp, 6, p, { "scale" }, &wr_gemv) != RAD_OK || wr_gemv != wr) {
|
||||
ok(nm, false, "b_ref stored differently by the gated and gemv rows");
|
||||
return;
|
||||
}
|
||||
}
|
||||
const int64_t nkb = K / 128, n = N / 2, gkb = n / 128;
|
||||
std::vector<uint8_t> ha((size_t)(M * K));
|
||||
std::vector<float> as((size_t)(M * (ablk ? nkb : 1)));
|
||||
@@ -27388,10 +27398,10 @@ static void device_phase(const Plugin& pl) {
|
||||
* K, and the per-row activation scale. */
|
||||
for (const int64_t m : { 1, 2, 3, 4, 5, 6, 7, 8 }) mxfp4a8_gated_case(pl, m, 17408, 5120, true);
|
||||
mxfp4a8_gated_case(pl, 8, 21504, 5376, true);
|
||||
mxfp4a8_gated_case(pl, 3, 256, 128, true);
|
||||
mxfp4a8_gated_case(pl, 3, 256, 256, true);
|
||||
mxfp4a8_gated_case(pl, 5, 1024, 640, false);
|
||||
for (const int64_t m : { 1, 4, 8 }) mxfp4a8_gated_case(pl, m, 21504, 5376, true, true);
|
||||
mxfp4a8_gated_case(pl, 3, 256, 128, true, true);
|
||||
mxfp4a8_gated_case(pl, 3, 256, 256, true, true);
|
||||
mxfp4a8_gated_case(pl, 2, 1024, 640, false, true);
|
||||
/* Parts (ParoQuant): each weight part against its own activation copy, with bounds off any
|
||||
* kernel's tile, two and three parts, at every band. */
|
||||
|
||||
Reference in new issue
Block a user