feat(qwen4exp): support calibrated U5 gate/W5 down recipe

This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-16 17:58:41 +01:00
1 parent 157959fd80
commit ba3ebd380f
7 files changed
+401 -17

No files matched your search

+20 -10
View File
@@ -810,7 +810,10 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
fmt_of(e, enc, fe, nmv);
if (fe[0] == 3 && fe[1] == 3) continue;
for (int i = 0; i < 2; ++i) {
if (family(want) && family(fe[i])) fmt[i] = fe[i];
/* A trellis gate_up reads rotated int8, but the down projection can use the
* W5NL E4M3 kernel: its gated intermediate is quantised independently below. */
const bool mixed_w5_down = a8f && want == 11 && i == 1 && fe[i] == 7;
if (family(want) && (family(fe[i]) || mixed_w5_down)) fmt[i] = fe[i];
fenc[i] = enc[i];
}
break;
@@ -1079,9 +1082,12 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
"architecture declared the routed arm's codes E4M3\n", l);
return RAD_E_UNSUPPORTED;
}
const bool down_w5_fp8 = a8 && fmt[0] == 11 && fmt[1] == 7;
if (a8) {
const bool pair = w.eff.q8 && w.eff.s8 && w.eff.q8_rot && w.eff.g8 == RAD_FP8_BLOCK;
if (!pair || !c.expert_rot || !kServed8[fmt[0]] || !kServed8[fmt[1]] || c.calib ||
const bool down_supported = down_w5_fp8 ? (w.eff.q && w.eff.s && kServed[fmt[1]])
: kServed8[fmt[1]] != nullptr;
if (!pair || !c.expert_rot || !kServed8[fmt[0]] || !down_supported || c.calib ||
c.swiglu_limit > 0) {
fprintf(stderr, "radiance: layer %d: int8 activations meet rotated experts (int8, "
"four-bit, or the codebook at E4M3 scales a 64 or 32 of K) through "
@@ -1105,7 +1111,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
* experts' encoding or the kernel is not a candidate. */
gu_trellis = fmt[0] == 11;
const char* const edtype[2] = { a8 ? kServed8[fmt[0]] : kServed[fmt[0]],
a8 ? kServed8[fmt[1]] : kServed[fmt[1]] };
a8 && !down_w5_fp8 ? kServed8[fmt[1]] : kServed[fmt[1]] };
/* This class's slice of each expert: gate and up rows [lo, hi), down columns [lo, hi), scale
* planes the same in their own units. */
auto span = [&](rad_weight h, int q, int64_t unit) -> int {
@@ -1302,9 +1308,13 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
* which takes the codes alone (no bf16 product is written), so the gated quantiser resolves to
* the rotated op at no extra launch. */
gq_exp.fuse_clamped = c.swiglu_quant != 0;
/* Fed int8, `eff` names the rotating int8 quantiser itself (ActFP8::q8_rot). */
RAD_ARCH_TRY(gq_exp.declare(b, g, w.egu, w.eff, c.n_ff_exp, gact, c.rows * c.top_k,
false, rot && !a8, c.swiglu_limit, c.swiglu_step != 0));
/* Gate_up and down are separate GEMMs. A trellis gate uses int8 input while W5NL down
* reads rotated E4M3: select the latter pair only for the gated intermediate. */
ActFP8 down_eff = w.eff;
if (down_w5_fp8) down_eff.q8_fed = down_eff.q8_rot = false;
RAD_ARCH_TRY(gq_exp.declare(b, g, w.egu, down_eff, c.n_ff_exp, gact, c.rows * c.top_k,
false, rot && (!a8 || down_w5_fp8), c.swiglu_limit,
c.swiglu_step != 0));
{
const std::vector<rad_weight> tab = tables(w_dn, w_dns);
const RadParam p4[] = { RAD_RANGE("M", 1, c.rows), RAD_INT("N", g.n_embd),
@@ -1321,13 +1331,13 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
RAD_STR("a_order", "sorted"), RAD_STR("dtype", edtype[1]),
RAD_INT("N_odd", g.n_embd), RAD_INT("K_odd", wq[1]),
RAD_INT("parts", 1), RAD_INT("a_group", RAD_FP8_BLOCK) };
const int cut = a8 ? 0 : 1;
const int cut = a8 && !down_w5_fp8 ? 0 : 1;
rad_op h = ncls == 4
? rad_decl_op(b, "moe_gemm_q_mod4", p4, (int)(sizeof p4 / sizeof *p4) - cut,
tab.data(), (int)tab.size())
: rad_decl_op(b, "moe_gemm_q", p2, (int)(sizeof p2 / sizeof *p2) - cut,
tab.data(), (int)tab.size());
op_dn = rw(b, h, {w.eff.cq(), w.eff.cs(), w.sorted, w.eoff}, {w.edn});
op_dn = rw(b, h, {down_eff.cq(), down_eff.cs(), w.sorted, w.eoff}, {w.edn});
}
/* ---- The protected experts' own GEMM pair. Their weights belong to the layer group, not to
* expert slots: each container expert unit has a fixed size and these are four times larger, so
@@ -1925,7 +1935,7 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
brows(w.sorted, rows * k), brows(w.eoff, ne + 1), brows(w.edn, rows * k));
else if (ncls == 4)
RAD_ISSUE_N(c_, op_dn, rows,
brows(w.eff.cq(), rows * k), brows(w.eff.cs(), rows * k),
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
RAD_WTAB(w_dn[0], n4[0]), RAD_WTAB(w_dns[0], n4[0]),
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
RAD_WTAB(w_dn[1], n4[1]), RAD_WTAB(w_dns[1], n4[1]),
@@ -1934,7 +1944,7 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
brows(w.edn, rows * k));
else
RAD_ISSUE_N(c_, op_dn, rows,
brows(w.eff.cq(), rows * k), brows(w.eff.cs(), rows * k),
brows(gq_exp.a.cq(), rows * k), brows(gq_exp.a.cs(), rows * k),
RAD_WTAB(w_dn[0], n_reg / 2), RAD_WTAB(w_dns[0], n_reg / 2),
brows(w.sorted, rows * k), brows(w.eoff, n_reg + 1),
RAD_WTAB(w_dn[1], n_reg / 2), RAD_WTAB(w_dns[1], n_reg / 2),
+59 -2
View File
@@ -152,6 +152,13 @@ struct MtpHcBlock {
MoeFP8 mlp{};
rad_op op_gather = 0, op_embed = 0, op_embed_ar = 0, op_enter = 0, op_carry = 0;
/* The selected draft calibration also records the bf16 attention input shared by
* qg/k/v, independently of the expert gate's later input. */
rad_buf b_attn_gram = 0;
rad_op op_attn_gram = 0;
const char* attn_gram_stem = nullptr;
mutable int64_t attn_gram_rows = 0;
mutable bool attn_gram_done = false;
/* Media rows over the head's embedding (see MtpBlockFP8::media). Set before declare when the
* program has an encoder. */
@@ -258,6 +265,20 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
* order. */
RAD_ARCH_TRY(hc_attn.declare(b, nm, g, nm.f("mtp.attn_hc"), hc, HyperConn::Wire{w.hw, w.x, w.inj},
src.hc_attn));
if (src.mlp.cfg.calib) {
/* Only a BF16 source supplies domain-0 inputs for this calibration. The input
* is replicated, so each TP rank records the same width and GPTQ sums them. */
if (!src.mlp.cfg.expert_bf16 || !w.attn.h.x) return RAD_E_UNSUPPORTED;
attn_gram_stem = nm.f("blk.%d.attn_qg", l);
b_attn_gram = decl_b(b, nm.f("blk.%d.mtp_attn_gram", l), RAD_F32,
{g.n_embd, g.n_embd}, RAD_BUF_PERSIST);
op_attn_gram = rw(b, RAD_OP(b, "gram_accum",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", g.n_embd),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{w.attn.h.x, b_attn_gram}, {b_attn_gram});
if (!b_attn_gram || !op_attn_gram) return RAD_E_INVAL;
}
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, kv));
attn.ar_out_take = hc_attn.ar_take();
RAD_ARCH_TRY(attn.declare(b, nm, g, l, kv, rotary_dim, w.attn, src.attn,
@@ -471,6 +492,17 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
const int64_t T = batch->n_tok;
const int r = batch->draft_pass;
/* Expert calibration needs real draft-layer inputs, not the trunk's Hessian. In a
* selected calibration layer, run the history's teacher-forced rows through the full
* draft block before selecting the rows needed by the drafter. Normal inference and
* a tap that has reached its target retain the ordinary history-only fast path. */
const bool tap_history = r < 0 && mlp.c.calib && !mlp.calib_done;
/* serial_batch names one trunk hidden row per history token. Refuse an incomplete
* gather instead of silently calibrating stale hin rows if that contract changes. */
if (tap_history && (batch->n_out != T || !batch->out_ids)) {
rad_step_fail(c, "MTP calibration needs one trunk hidden row per history token");
return;
}
/* The history pass continues from the trunk's wide stream; later rounds from the head's,
* which the carry left in the same buffer. */
@@ -493,11 +525,26 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
RAD_W(w_fc_h), RAD_W(w_fc_e), brows(w.hw, T));
hc_attn.read_pieces(c, T);
if (tap_history && op_attn_gram && !attn_gram_done) {
if (attn_gram_rows == 0)
rad_memset_async(rad_buf_ptr(c, b_attn_gram), 0,
g.n_embd * g.n_embd * 4, rad_stream(c));
RAD_ISSUE_N(c, op_attn_gram, T, brows(w.attn.h.x, T), RAD_NONE,
brows(b_attn_gram, g.n_embd));
attn_gram_rows += T;
if (attn_gram_rows >= calib_target()) {
if (!calib_write(c, b_attn_gram, g.n_embd, attn_gram_rows, attn_gram_stem, 0)) {
rad_step_fail(c, "MTP calibration could not write its attention input Gram");
return;
}
attn_gram_done = true;
}
}
/* A history pass with no drafting rows (every prefill chunk but a prompt's last) only stores
* the head's state: K and V, and the indexer's block keys and tail. `n_draft_out` is the same
* on every rank, so skipped collectives match. */
if (r < 0 && batch->n_draft_out <= 0) {
if (r < 0 && batch->n_draft_out <= 0 && !tap_history) {
qsa.store(c, attn.w.h, batch);
attn.store_kv(c, batch);
return;
@@ -513,7 +560,7 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
/* A history pass with drafting rows continues on those rows alone (round 1): they are moved
* to the front of `hw` and the rest of the head runs over them. */
int64_t L = T; /* the rows the stream goes on with */
if (r < 0) {
if (r < 0 && !tap_history) {
L = batch->n_draft_out;
RAD_ISSUE_N(c, op_dsel, L, RAD_B(w.hw), praw(batch->draft_out_ids, RAD_I32, L),
brows(w.hin, L));
@@ -524,6 +571,16 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
mlp.step_rows(c, L);
hc_mlp.write(c, L, 0, L);
if (tap_history) {
/* History-only chunks have no proposal. For the last chunk, select the same
* round-1 rows the ordinary path would have selected before its feed-forward. */
L = batch->n_draft_out;
if (L <= 0) return;
RAD_ISSUE_N(c, op_dsel, L, RAD_B(w.hw), praw(batch->draft_out_ids, RAD_I32, L),
brows(w.hin, L));
RAD_ISSUE_N(c, op_dback, L, brows(w.hin, L), brows(w.hw, L));
}
/* The carry, only if a later round reads it: a history pass is round 1, followed by round 2
* when depth > 1; the last round is followed by a trunk step. */
if (r < 0 ? depth > 1 : r < depth)
+8 -2
View File
@@ -936,8 +936,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
if (m.arm8) {
/* The int8 routed arm's two pairs, rotated at a scale a 128 and written by their
* producers: the gated product's by gated_had_quant_i8g, the input's by the read. */
RAD_ARCH_TRY(m.a_eff.declare_qs(b, nm, g, "moe_eff", m.moecfg.n_ff_exp, ER, true, false,
RAD_FP8_BLOCK));
/* A trellis gate_up may be paired with W5NL down. Keep its E4M3 pair as well so that
* the gated intermediate can use the down projection's own activation format. */
RAD_ARCH_TRY(m.a_eff.declare_qs(b, nm, g, "moe_eff", m.moecfg.n_ff_exp, ER, true,
m.moecfg.expert_iqr != 0, RAD_FP8_BLOCK));
m.a_eff.q8_fed = m.a_eff.q8_rot = true;
if (!m.trunk_rot) {
RAD_ARCH_TRY(m.a_hr.declare_qs(b, nm, g, "moe_hr", g.n_embd, 0, true, false,
@@ -1238,6 +1240,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
MoeFP8::Src ms{};
ms.cfg = m.moecfg;
/* The legacy expert tap can be limited to selected layers, including the MTP
* layer below. Keep the whole-expert placement selected by m.moecfg.calib. */
ms.cfg.calib = m.moecfg.calib && calib_layer_on((int)l);
ms.router = nm.ckpt("%s.%lld.mlp.gate.weight", pre, (long long)l);
RAD_ARCH_TRY(moe_expert_sources(b, nm, "layer",
nm.ckpt("%s.%lld.mlp.experts", pre, (long long)l), &ms));
@@ -1462,6 +1467,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
ts.attn.o = nm.ckpt("%s.layers.0.self_attn.o_proj.weight", mpre);
ts.mlp.cfg = m.moecfg;
ts.mlp.cfg.calib = m.moecfg.calib && calib_layer_on((int)g.n_layer);
ts.mlp.router = nm.ckpt("%s.layers.0.mlp.gate.weight", mpre);
RAD_ARCH_TRY(moe_expert_sources(b, nm, "mtp", nm.ckpt("%s.layers.0.mlp.experts", mpre),
&ts.mlp));