qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8

decl_gated_fold_mx declared gemm_nt_q_gated_mx over 1..8 rows only, from
before libr4d had gemm_mxfp4a8_gated_gemv_wide (9..64, byte-identical to
gemv_wide followed by gated_quant_fp8, the pair it replaces). TranslateGemma
already folds to 64 through RADIANCE_G3_FOLD_ROWS; Qwen's 16-row verify steps
(two sequences at speculation 7) still ran gate_up and gated_quant_fp8 apart.

Qwen3.8-27B MXFP4 + DFlash2 at TP2 on 2x R9700, production flags, ms a decode
step, three interleaved rounds (medians), the 1..8 band -> folding to 32:

  16 rows (x2):            1k 20.96 -> 20.78 (-0.9 %), 16k 21.96 -> 21.70 (-1.2 %)
  32 rows (x4):            1k 23.97 -> 24.10 (+0.5 %), 16k 26.06 -> 25.98
  32 rows (x8 short block): 1k 24.99 -> 25.14 (+0.6 %)

and 64 rows lost 2.1 % (16k x16) on the base before this main. So the band is
16: a taller step takes exactly main's path. Where the wide row does not
resolve (K at or under decode_shortk's corner), the declaration drops to 1..8
instead of losing the fold. Greedy output is identical to main (300 tokens,
two prompts).

arch_test: qwen35_fp8_mxfp4_feed_forward_folds_verify_steps_up_to_16_rows
(one and two sequences of 8 rows fold, three to eight take the pair).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-29 16:21:56 +01:00
1 parent 187a015bda
commit ed5f990d92
3 files changed
+71 -16

No files matched your search

+3 -3
View File
@@ -120,9 +120,9 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co
swiglu_limit));
/* The fold: decl_gated_fold (an fp8a8 tile) for block-fp8 weights, decl_gated_fold_mx for plain
* MXFP4 at one to eight rows. Other gate_up encodings (M2XFP4 included), and a clamped
* (swiglu_limit) product, take the pair. Over int8 codes only a host kernel would take it, which
* would pin the layer's weights to the host. */
* MXFP4 at one to 16 rows (eight where K is short). Other gate_up encodings (M2XFP4 included),
* and a clamped (swiglu_limit) product, take the pair. Over int8 codes only a host kernel would
* take it, which would pin the layer's weights to the host. */
const bool fold_ok = swiglu_limit <= 0 && !gate_up.f32_scale;
if (fold_ok && !gate_up.bf16 && !gate_up.mx && !gate_up.m2x && !gate_up.paro5 && !gate_up.w4 &&
!gate_up.i8)
+26 -13
View File
@@ -1742,24 +1742,37 @@ inline rad_op decl_gated_fold(RadBuilder* b, int64_t rows, int64_t N, int64_t K,
}
/* The same fold over an OCP-MXFP4 [2n, K] gate_up weight (`gemm_nt_q_gated_mx`: codes, the E8M0
* plane and that plane again as the per-row reference), up to min(rows, 8) rows, the band of
* libr4d's one-to-eight-row GEMM. Declared only when a loaded library declares the op (libref
* does not), so a CPU-only engine takes the pair. */
* plane and that plane again as the per-row reference), declared for M up to min(rows, 16):
* libr4d's gated_gemv serves 1..8 rows and gated_gemv_wide 9..64, each byte-identical to its band's
* GEMM row (gemv, gemv_wide) followed by gated_quant_fp8. 16, not the wide row's 64: Qwen3.8-27B
* at TP2 on 2x R9700 (spec 7, ms a decode step, three interleaved rounds against the 1..8 band)
* gains at 16 rows (two sequences: 1k 20.96 -> 20.78, 16k 21.96 -> 21.70) but loses at 32 (four
* sequences at 1k 23.97 -> 24.10, eight in the short block 24.99 -> 25.14), and 64 lost more
* (16k x16 +2.1 %). The wide row refuses a short K (decode_shortk's corner), so where 1..16 does
* not resolve the band drops to 1..8 rather than lose the fold. Optional like decl_gated_fold, and
* asked for only when a loaded library declares the op: libref does not, so a CPU-only engine takes
* the pair without ever naming it. */
inline rad_op decl_gated_fold_mx(RadBuilder* b, int64_t rows, int64_t N, int64_t K, rad_weight w,
rad_weight ws, rad_weight wref,
std::initializer_list<rad_buf> reads,
std::initializer_list<rad_buf> writes, int64_t* gated_rows) {
const int64_t hi = rows < 8 ? rows : 8;
if (hi <= 0 || N % 256 || K % RAD_FP8_BLOCK) return 0;
if (rows <= 0 || N % 256 || K % RAD_FP8_BLOCK) return 0;
if (!rad_op_declared(b, "gemm_nt_q_gated_mx")) return 0;
const rad_op op = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_STR("act", "silu")),
RAD_WEIGHTS(w, ws, wref)),
reads, writes);
if (op) *gated_rows = hi;
return op;
for (const int64_t band : { (int64_t)16, (int64_t)8 }) {
const int64_t hi = rows < band ? rows : band;
const rad_op op = rw(b, RAD_OP(b, "gemm_nt_q_gated_mx",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP),
RAD_STR("dtype", "mxfp4a8"), RAD_STR("act", "silu")),
RAD_WEIGHTS(w, ws, wref)),
reads, writes);
if (op) {
*gated_rows = hi;
return op;
}
if (hi <= 8) break;
}
return 0;
}
/* ================================================== a calibration tap */
+42
View File
@@ -7754,6 +7754,48 @@ TEST(qwen35_fp8_declares_fp8_linears_and_the_fused_output_gate) {
CHECK(find_b(b, "mtp_x") == nullptr);
}
/* On an MXFP4 trunk the feed-forward's gated fold (gate_up, silu_mul and down's quantiser in one
* launch) is declared over 1..16 rows, libr4d's gated_gemv (1..8) and part of gated_gemv_wide's
* (9..64) band: a verify step of one or two sequences at speculation 7 (8 or 16 rows) issues it
* once a layer at the step's height and no gated_quant_fp8; a taller one (24..64 rows) the unfused
* gate_up and gated_quant_fp8. */
TEST(qwen35_fp8_mxfp4_feed_forward_folds_verify_steps_up_to_16_rows) {
MetaKV mk = with_dflash2(q35fp8_meta());
const RadEncoding mx = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, rad::arch::RAD_MX_GROUP);
const int L = 8;
for (int rank = 0; rank < 2; ++rank) {
RadBuilder b;
b.encs = { { "blk.", mx } };
RadBuildCtx ctx = spec_ctx(rank, 2, 7);
ctx.max_tok = 2048;
REQUIRE_EQ(stderr_of_rc([&] { return qwen35_fp8::declare(&b, mk.meta(), &ctx); }), RAD_OK);
int folds = 0;
for (const auto& o : b.ops)
if (o.op == "gemm_nt_q_gated_mx" && !o.w.empty() &&
b.weights[o.w[0] - 1].name.rfind("blk.", 0) == 0) {
++folds;
CHECK_EQ(pstr(&o, "act"), std::string("silu"));
CHECK_EQ(param(&o, "M")->ihi, 16);
}
CHECK_EQ(folds, L);
for (int n = 1; n <= 8; ++n) {
const auto k = kit(b, std::vector<int>((size_t)n, 8), {});
const auto v = run_passes(b, qwen35_fp8::step, { k.get() }, rank, 2);
CHECK_PASSES(b, v);
const std::vector<std::string> ops = issued_ops(b, *v[0]);
const bool fold = 8 * n <= 16;
int gated = 0;
for (size_t i = 0; i < ops.size(); ++i)
if (ops[i] == "gemm_nt_q_gated_mx") {
++gated;
CHECK_EQ(v[0]->issues[i].n, 8 * n);
}
CHECK_EQ(gated, fold ? L : 0);
CHECK_EQ(count_issued(ops, "gated_quant_fp8"), fold ? 0 : L);
}
}
}
/* At two ranks each delta-net layer reads its out-projection into the on-die cache beside the decode
* recurrent update (GdnFP8::op_pf_out): a `prefetch` of the whole projection, issued right before
* the update on steps of up to four decode sequences and 32 rows, none on a step of five or more