arch/common: the MTP block's fc as a LinearFP8, so a container may store it 4-bit

mtp.fc was declared plain bf16 and run by gemm_nt. It now goes through LinearFP8 over the
bf16 concat plane, so its form follows the stored encoding like every other linear: bf16 as
before (the same gemm_nt op, greedy output identical on qwen3.8-27b-w4a16-rtn-mtp4, 8/8), or a
4-bit weight with a zero point on the w4a16 / w4a8i8 GEMMs. Applies to every arch built on
MtpBlockFP8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-02 10:30:59 +01:00
1 parent b9fdfc0a79
commit 10a99a5bf2
1 file changed
+10 -11
+10 -11
View File
@@ -168,12 +168,16 @@ struct MtpBlockFP8 {
int layer = 0; /* absolute, for the KV group's layer_slot lookup */
int64_t rows = 0; /* the widest pass: one row per token of a prefill chunk */
int64_t off_e = 0, off_h = 0; /* the two halves' column offsets inside `c` */
rad_weight w_tok = 0, w_lm = 0, w_fc = 0, w_norm = 0, w_pre_h = 0, w_pre_e = 0;
rad_weight w_tok = 0, w_lm = 0, w_norm = 0, w_pre_h = 0, w_pre_e = 0;
/* `fc` in whatever form the container stores it: plain bf16 (gemm_nt, as the checkpoint) or a
* 4-bit weight with a zero point (the w4a16 / w4a8i8 GEMMs), read from the bf16 `c` plane. */
LinearFP8 fc{};
ActFP8 a_c{};
rad_weight w_dh = 0, w_dhs = 0; /* the 2-bit lm_head and its (scale, zero) plane */
bool q2 = false; /* is the draft round reading it? */
AttnGatedFP8 attn{};
Ffn mlp{};
rad_op op_gather = 0, op_embed = 0, op_embed_ar = 0, op_pre_h = 0, op_pre_e = 0, op_fc = 0;
rad_op op_gather = 0, op_embed = 0, op_embed_ar = 0, op_pre_h = 0, op_pre_e = 0;
/* Media rows over the head's embedding: the history pass embeds the prompt too (one position
* on), and an image token's embedding comes from the encoder. Set `media` before declare when
* the program has an encoder. */
@@ -209,7 +213,6 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.pre_fc_norm_hidden.weight"), src.pre_norm_hidden));
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.pre_fc_norm_embedding.weight"), src.pre_norm_embedding));
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.norm.weight"), src.norm));
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.fc.weight"), src.fc));
w_pre_h = decl_w(b, nm.f("mtp.pre_fc_norm_hidden.weight"), RAD_F32, {g.n_embd},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
@@ -217,8 +220,6 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
w_norm = decl_w(b, nm.f("mtp.norm.weight"), RAD_F32, {g.n_embd},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
w_fc = decl_w(b, nm.f("mtp.fc.weight"), RAD_BF16, {g.n_embd, 2 * g.n_embd},
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
/* Separate gather_rows and embed_lookup ops over the trunk's buffers and weights, because
* they write different buffers and liveness comes from each op's read/write sets. */
@@ -257,11 +258,9 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
RAD_WEIGHTS(w_pre_e)),
{w.e}, {w.c});
op_fc = rw(b, RAD_OP(b, "gemm_nt",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("N", g.n_embd),
RAD_INT("K", 2 * g.n_embd), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_fc)),
{w.c}, {w.x});
a_c.x = w.c;
RAD_ARCH_TRY(fc.declare(b, nm, g, "mtp.fc", g.n_embd, 2 * g.n_embd, RAD_SHARD_NONE, grp_layer(l),
{ src.fc }, a_c, w.x, rows));
RAD_ARCH_TRY(attn.declare(b, nm, g, l, kv, rotary_dim, w.attn, src.attn));
@@ -419,7 +418,7 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
RAD_ISSUE(c, op_pre_h, brows(w.hout, T), RAD_W(w_pre_h), bcol(w.c, off_h, g.n_embd, T));
RAD_ISSUE(c, op_pre_e, brows(w.e, T), RAD_W(w_pre_e), bcol(w.c, off_e, g.n_embd, T));
RAD_ISSUE(c, op_fc, brows(w.c, T), RAD_W(w_fc), brows(w.x, T));
fc.step(c, a_c, w.x, T);
/* A history pass with no drafting rows (every prefill chunk but a prompt's last) only needs
* K and V. `n_draft_out` is the same on every rank, so skipped collectives match. */