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

+2 -2
View File
@@ -661,8 +661,8 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
{w.h}, {w.coef});
op_gu[i] = w_gu[i].gemm(b, qrows, 2 * g.n_ff, g.n_embd, {w.h2q, w.h2s}, {w.gate_up});
/* The fold saves two launches and the bf16 round trip of the [T, n_ff] product; it is an
* fp8a8 tile, so an MXFP4 gate_up takes the pair. */
if (!w_gu[i].mx)
* fp8a8 tile, so an MXFP4 or M2XFP4 gate_up takes the pair. */
if (!w_gu[i].mx && !w_gu[i].m2x)
op_gug[i] = decl_gated_fold(b, qrows, 2 * g.n_ff, g.n_embd, w_gu[i].w, w_gu[i].s,
{w.h2q, w.h2s}, {w.ffq, w.ffs}, &gated_rows);
op_dn[i] = w_dn[i].gemm(b, qrows, g.n_embd, g.n_ff, {w.ffq, w.ffs}, {w.h});
+1 -1
View File
@@ -110,7 +110,7 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
* Declared over int8 codes, only a host kernel would take it, and that would pin the layer's
* weights to the host. */
if (swiglu_limit <= 0 && !gate_up.bf16 && !gate_up.f32_scale && !gate_up.mx &&
!gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
!gate_up.m2x && !gate_up.paro5 && !gate_up.w4 && !gate_up.i8)
op_gated = decl_gated_fold(b, g.max_tok, gate_up.N, gate_up.K, gate_up.w, gate_up.ws,
{ w.h.q, w.h.s },
gq_ffn.want_x
+159 -7
View File
@@ -708,6 +708,11 @@ struct LinearFP8 {
* view whose layout hook reduces it). Read from the weight's encoding. */
bool mx = false;
rad_weight wref = 0;
/* The M2XFP4 sibling (libquant `m2xfp4`): the MXFP4 weight plus one metadata byte a group,
* read by `gemm_nt_q` at dtype m2xfp4 with the same E4M3 pair, `b_scale` and `b_ref` as MXFP4's
* are, the meta plane riding the weight operand's stored blob. Read from the weight's
* encoding. */
bool m2x = false;
/* Rotated (ParoQuant) variant of `mx`: the same GEMM over an activation that
* `givens_dense_quant` makes from the bf16 plane (channel scale, dense per-group rotation and
* E4M3 quantiser in one pass), once per independently rotated part of a fused projection
@@ -870,6 +875,34 @@ inline bool linear_is_mxfp4(const RadEncoding& e) {
sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP;
}
/* The M2XFP4 sibling (arXiv 2601.19213, the paper's weight-side Sg-EM): the MXFP4 weight plus one
* metadata byte per group of 32, four 2-bit subgroup extra mantissas that refine the exponent to
* (1 + k/4) . 2^E (libquant's `m2xfp4` quantiser; libr4d's m2xfp4 layout hook). The exponent byte
* carries the adaptive bias; the GEMM applies nothing beyond the planes. */
inline bool linear_is_m2xfp4(const RadEncoding& e) {
if (!rad_enc_is(&e, "m2xfp4") || e.n_planes != 3 || e.transform[0]) return false;
const RadEncPlane *sc = rad_enc_plane(&e, "scale"), *mt = rad_enc_plane(&e, "meta");
return e.plane[0].dtype == RAD_FP4E2M1 && e.plane[0].block[0] == 1 && e.plane[0].block[1] == 1 &&
sc && sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP && mt &&
mt->dtype == RAD_U8 && mt->block[0] == 1 && mt->block[1] == RAD_MX_GROUP;
}
/* A rotated weight with M2XFP4 planes (ParoQuant's `m2xfp4` grid): the scheme's three planes in the
* basis of a "givens" transform, whose tables are views of the weight's planes. Read exactly like
* linear_is_paro_mxfp4 -- the meta plane rides the weight operand's stored blob -- over an
* activation the rotation makes. */
inline bool linear_is_paro_m2xfp4(const RadEncoding& e) {
if (!rad_enc_is(&e, "m2xfp4") || std::strncmp(e.transform, "givens", RAD_ENC_STR) != 0) return false;
if (!rad_enc_valid(&e) || e.n_planes < 6) return false;
const RadEncPlane *sc = rad_enc_plane(&e, "scale"), *mt = rad_enc_plane(&e, "meta"),
*pr = rad_enc_plane(&e, "t.pairs"), *an = rad_enc_plane(&e, "t.angle"),
*rs = rad_enc_plane(&e, "t.scale");
return pr && an && rs && pr->dtype == RAD_I32 && an->dtype == RAD_F32 && rs->dtype == RAD_F32 &&
e.plane[0].dtype == RAD_FP4E2M1 && e.plane[0].block[0] == 1 && e.plane[0].block[1] == 1 &&
sc && sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == RAD_MX_GROUP && mt &&
mt->dtype == RAD_U8 && mt->block[0] == 1 && mt->block[1] == RAD_MX_GROUP;
}
/* A rotated weight (ParoQuant): an affine encoding in the basis of a "givens" transform, with the
* rotation tables as planes (int32 column pairs, f32 angles, f32 channel scales; one set per part
* of a fused projection). The GEMM reads the codes as stored; the activation is rotated first. */
@@ -924,7 +957,8 @@ inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char*
probed_i8 = known && (linear_is_w8(enc) || linear_is_w8h(enc));
/* A rotated weight rotates and quantises the bf16 plane itself; a 4-bit one casts or
* quantises it. */
const bool rotated = known && (linear_is_paro_mxfp4(enc) || linear_is_paro_w5(enc));
const bool rotated = known && (linear_is_paro_mxfp4(enc) || linear_is_paro_w5(enc) ||
linear_is_paro_m2xfp4(enc));
reads_x = g.w_bf16 || plain || probed_i8 || rotated || (known && linear_is_w4(enc));
return RAD_OK;
}
@@ -1277,6 +1311,91 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
return RAD_E_DTYPE;
}
/* ---- M2XFP4 weight over the E4M3 pair: the MXFP4 weight's views plus the metadata plane,
* which rides the weight operand -- the declared view takes codes and meta together, and the
* stored blob carries the meta in its tail (libr4d's m2xfp4 layout). The exponent plane is
* `b_scale` and `b_ref` as MXFP4's is; the row parts, the shape bounds and the column-shard
* rule are the MXFP4 ones.
*
* A rotated weight (ParoQuant) carries the same planes in the basis its "givens" tables name,
* so it is declared exactly like the rotated MXFP4 one: `givens_dense_quant` makes the E4M3
* pair from the input's bf16 plane, once per rotated part, and the GEMM meets copy p with part
* p's rows. `paro` makes step() issue that rotation first. */
const bool m2rot = known && linear_is_paro_m2xfp4(enc);
const bool m2 = known && (linear_is_m2xfp4(enc) || m2rot);
if (m2) {
m2x = true;
paro = m2rot;
if (m2rot) {
if (!a.x) {
fprintf(stderr, "radiance: '%s' is a rotated M2XFP4 weight, rotated and quantised "
"from its input's bf16 plane, and its input has none\n", wn);
return RAD_E_DTYPE;
}
if (g.world > 1 || span_hi > 0) {
fprintf(stderr, "radiance: '%s' is a rotated M2XFP4 weight; its rotation is per "
"projection and is not cut by a tensor-parallel shard (world %d)\n",
wn, g.world);
return RAD_E_UNSUPPORTED;
}
} else if (!a.q || !a.s) {
fprintf(stderr, "radiance: '%s' is an M2XFP4 weight read against its input's E4M3 "
"pair, and its input has none\n", wn);
return RAD_E_DTYPE;
}
if (N % 16 || K % RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an M2XFP4 weight takes rows in "
"16s and K in whole %d-column activation scale blocks\n",
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
return RAD_E_INVAL;
}
RowParts rp;
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "M2XFP4 tiles"));
int64_t pb1 = 0, pb2 = 0;
const int64_t hi = max_rows > 0 ? max_rows : g.max_tok;
if (m2rot) {
std::vector<int64_t> cut;
for (int64_t p : rot_part_rows) cut.push_back(p);
if (cut.empty()) for (int64_t p : row_parts) cut.push_back(p);
RAD_ARCH_TRY(declare_rotation(b, nm, base, wn, enc, N, K, grp, cut, &pb1, &pb2));
rot_q = decl_b(b, nm.f("%s.rot_q", base), RAD_F8E4M3, { hi, rot_parts * K });
rot_s = decl_b(b, nm.f("%s.rot_s", base), RAD_F32,
{ hi, rot_parts * (K / RAD_FP8_BLOCK) });
if (!rot_q || !rot_s) return RAD_E_INVAL;
rot_dense = decl_dense(b, nm, base, wn, enc, K, grp);
if (!rot_dense) return RAD_E_INVAL;
op_rot = decl_dense_op(b, g, a, "fp8", 1, hi, { rot_q, rot_s });
if (!op_rot) return RAD_E_INVAL;
}
const char* sn = nm.f("%s.scale", base);
const char* rn = nm.f("%s.ref", base);
w = decl_view(b, wn, nullptr, "codes,meta", RAD_FP4E2M1, { N, K }, access, shard, grp,
rp.w, rp.n);
ws = decl_view(b, sn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
access, shard, grp, rp.w, rp.n);
wref = decl_view(b, rn, wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
access, shard, grp, rp.w, rp.n);
if (!w || !ws || !wref) return RAD_E_INVAL;
if (span_hi > 0) {
const bool col = shard == RAD_SHARD_COL;
if (col && (span_lo % RAD_FP8_BLOCK || span_hi % RAD_FP8_BLOCK)) {
fprintf(stderr, "radiance: '%s' is sliced [%lld, %lld) on this rank, which cuts a "
"%d-column activation scale block\n", base, (long long)span_lo,
(long long)span_hi, RAD_FP8_BLOCK);
return RAD_E_INVAL;
}
const int64_t lo = col ? span_lo / RAD_MX_GROUP : span_lo;
const int64_t hi_n = col ? span_hi / RAD_MX_GROUP : span_hi;
RAD_ARCH_TRY(rad_weight_shard_span(b, w, span_lo, span_hi));
RAD_ARCH_TRY(rad_weight_shard_span(b, ws, lo, hi_n));
RAD_ARCH_TRY(rad_weight_shard_span(b, wref, lo, hi_n));
}
/* A rotated weight reads its own rotated pair, a plain one the producer's. */
op = decl_gemm_q(b, 1, hi, RAD_MX_GROUP, "m2xfp4", pb1, pb2, { w, ws, wref },
{ m2rot ? rot_q : a.q, m2rot ? rot_s : a.s }, y);
return op ? RAD_OK : RAD_E_INVAL;
}
/* ---- OCP-MXFP4 weight over the E4M3 pair. Three views: codes, exponents as `b_scale`, and
* the same plane as `b_ref`. Row parts apply as is (the exponent grid is one row tall); column
* slices must be whole 128s, the activation's scale block. */
@@ -1476,9 +1595,10 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (mx) {
if (mx || m2x) {
/* A rotated weight first rotates its input into its own codes, `rot_parts` copies side by
* side; a plain weight reads the producer's. */
* side; a plain weight reads the producer's. An M2XFP4 weight runs the same issue: it
* takes the operands MXFP4's does, its meta riding the weight operand's stored blob. */
const rad_buf aq = paro ? rot_q : a.q, as = paro ? rot_s : a.s;
const int64_t ak = rot_parts * K;
if (paro)
@@ -1513,6 +1633,11 @@ struct DenseFP8 {
* works against the same E4M3 activation pair. */
rad_weight ref = 0;
bool mx = false;
/* Or M2XFP4 (libquant `m2xfp4`): the MXFP4 weight plus one metadata byte a group, read by
* `gemm_nt_q` at dtype m2xfp4 with the same views and the same `b_ref`. The meta plane rides
* the weight operand's stored blob (libr4d's m2xfp4 layout), so the declared weight takes the
* codes and meta planes together. */
bool m2x = false;
/* The caller made the name map (a row range of a checkpoint tensor); declare() does not. */
bool mapped = false;
@@ -1545,11 +1670,18 @@ struct DenseFP8 {
RAD_ARCH_TRY(rad_weight_shard_span(b, w, lo, hi));
return rad_weight_shard_span(b, s, lo / RAD_FP8_BLOCK, hi / RAD_FP8_BLOCK);
}
/* `gemm_nt_q` over this weight, `fp8a8` or `mxfp4a8` as it was declared, ranged M 1..hi.
* `split_n`, when set, is the stored width a row slice takes its K split from (docs/OPS.md). */
/* `gemm_nt_q` over this weight, `fp8a8`, `mxfp4a8` or `m2xfp4` as it was declared, ranged
* M 1..hi. `split_n`, when set, is the stored width a row slice takes its K split from
* (docs/OPS.md). */
rad_op gemm(RadBuilder* b, int64_t hi, int64_t N, int64_t K,
std::initializer_list<rad_buf> reads, std::initializer_list<rad_buf> writes,
int64_t split_n = 0) const {
if (m2x)
return rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "m2xfp4")),
RAD_WEIGHTS(w, s, ref)),
reads, writes);
if (mx)
return rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
@@ -1569,8 +1701,8 @@ struct DenseFP8 {
RAD_WEIGHTS(w, s)),
reads, writes);
}
/* The op's trailing `b_ref` operand: the reference view for MXFP4, absent for fp8. */
RadOperand bref() const { return mx ? RAD_W(ref) : RAD_NONE; }
/* The op's trailing `b_ref` operand: the reference view for MXFP4 and M2XFP4, absent for fp8. */
RadOperand bref() const { return mx || m2x ? RAD_W(ref) : RAD_NONE; }
};
inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t N, int64_t K,
@@ -1586,6 +1718,26 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t
if (!mapped) RAD_ARCH_TRY(map_linear(b, wn, src));
RadEncoding enc{};
mx = weight_enc(b, wn, &enc) && linear_is_mxfp4(enc);
/* ---- M2XFP4: the MXFP4 views plus the metadata plane riding the weight operand, the
* same bounds. */
m2x = weight_enc(b, wn, &enc) && linear_is_m2xfp4(enc);
if (m2x) {
if (N % 16 || K % RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an M2XFP4 weight takes rows "
"in 16s and K in whole %d-column activation scale blocks\n",
base, (long long)N, (long long)K, RAD_FP8_BLOCK);
return RAD_E_INVAL;
}
RowParts rp;
RAD_ARCH_TRY(rp.set(row_parts, 16, 1, base, "M2XFP4 tiles"));
w = decl_view(b, wn, nullptr, "codes,meta", RAD_FP4E2M1, { N, K }, access, shard, grp,
rp.w, rp.n);
s = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
access, shard, grp, rp.w, rp.n);
ref = decl_view(b, nm.f("%s.ref", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP },
access, shard, grp, rp.w, rp.n);
return w && s && ref ? RAD_OK : RAD_E_INVAL;
}
if (mx) {
if (N % 16 || K % RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an MXFP4 weight takes rows "