MTP coupling on qwen35_fp8's 2-bit draft head; libxe2 rows for the coupling ops

- arch: under --draft-coupling the MtpBlockFP8 draft head keeps the 2-bit logits' top 8,
  rescores them against the trunk's head (logit_rerank) and runs one mtp_couple_propose a
  round, as the Flash-Next head does; qwen35_fp8 declares the draw/state planes and a
  pitch-8 proposal. Off, nothing changes.
- libxe2: device rows for mtp_couple_propose / mtp_couple_verify, a work-item a row over
  spechub's own row helpers and argument readers (built into xe2_couple.cpp).
- spechub: mtp_couple_propose takes an optional q_temp (Q = softmax(score / q_temp)); exact at
  any value. RADIANCE_MTP_Q_TEMP sets it on qwen35_fp8 (default 1: 1.0 > 0.7 > 0.5 here).

w4a16-rtn-mtp4, depth 7 adaptive, temperature 0.7: decode proxy 76.9 -> 78.8 tok/s,
BetterBench decode 80.5 -> 81.7; greedy 8/8 identical with coupling on. Host tests
(spechub|couple|abi|mtp) 7/7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-02 05:45:50 +01:00
1 parent bfb1ebd1dd
commit 175f0bbd33
8 files changed
+160 -15

No files matched your search

+60 -5
View File
@@ -90,6 +90,7 @@
#define RAD_BLOCK_MTP_FP8_H
#include "rad_block_attn_gated_fp8.h"
#include "rad_spechub.h" /* RAD_MTP_COUPLE_*: the coupling wire */
#include "rad_block_mlp_fp8.h"
#include "rad_block_vit.h"
@@ -134,8 +135,16 @@ struct MtpBlockFP8 {
rad_buf ds = 0; /* [max_logit_rows, 1] f32, one scale a row */
rad_buf dsum = 0; /* [n_embd/group, mpad] f32, group-major (quant_act_i8's asum) */
rad_buf dlog = 0; /* [max_logit_rows, n_vocab] bf16, the coarse logits */
rad_buf didx = 0; /* [max_logit_rows, 1] i32, the proposed token */
rad_buf dval = 0; /* [max_logit_rows, 1] f32, its logit */
rad_buf didx = 0; /* [max_logit_rows, topr] i32, the proposed token (column 0) */
rad_buf dval = 0; /* [max_logit_rows, topr] f32, its logit */
/* --draft-coupling: the shortlist is the top kCoupleR of the 2-bit logits, rescored
* against the trunk's head (`logit_rerank`), and each round's `mtp_couple_propose` draws
* x ~ Q from it into column 0: [max_seqs] draws and [depth, max_seqs] states, PERSIST
* (abi/rad_spechub.h). Zero for the ordinary argmax at topr 1. */
rad_buf couple_draws = 0, couple_state = 0;
int64_t topr = 1;
/* Q's temperature over the rescored scores: exact at any value, it moves acceptance only */
double q_temp = 1.0;
/* The layer reuses the trunk's working set, which is dead by the time the head runs (a
* second set would be 150 MiB). */
AttnGatedFP8::Wire attn{};
@@ -171,7 +180,10 @@ struct MtpBlockFP8 {
bool media = false;
MediaRows mrows{};
rad_op op_norm = 0, op_dsel = 0, op_dback = 0, op_logits = 0;
rad_op op_dquant = 0, op_dlogits = 0, op_dtopk = 0;
rad_op op_dquant = 0, op_dlogits = 0, op_dtopk = 0, op_drerank = 0;
/* --draft-coupling: one op a round (the ABI freezes `round` as a parameter). */
rad_op op_couple[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
int depth = 0;
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, rad_kvgroup kv,
int64_t rotary_dim, rad_weight tok_embd, rad_weight lm_head, float out_wadd,
@@ -334,19 +346,47 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
RAD_WEIGHTS(w_dh, w_dhs)),
{w.dq, w.ds, w.dsum}, {w.dlog});
const bool couple = w.couple_draws || w.couple_state;
if (couple && (!w.couple_draws || !w.couple_state || w.topr < 2 || g.world > 1 ||
g.max_spec < 1 || g.max_spec > RAD_MTP_COUPLE_MAX_ROUNDS))
return RAD_E_UNSUPPORTED;
op_dtopk = rw(b, RAD_OP(b, "row_topk",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("R", 1),
RAD_INT("N", mtp_draft_rows(g.n_vocab_all)), RAD_INT("R", w.topr),
RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),
{w.dlog}, {w.didx, w.dval});
if (couple) {
/* The coarse shortlist rescored exactly against the trunk's head (already resident),
* so Q is the softmax of the target's own logits over it; in place over idx/val. */
op_drerank = RAD_OP(b, "logit_rerank",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("R", w.topr),
RAD_INT("n_vocab", g.n_vocab), RAD_INT("n_embd", g.n_embd),
RAD_INT("vocab_off", g.vocab_off), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_lm));
if (!op_drerank) return RAD_E_INVAL;
const rad_buf rd[2] = { w.hout, w.didx };
const rad_buf wr[2] = { w.didx, w.dval };
RAD_ARCH_TRY(rad_op_reads(b, op_drerank, rd, 2));
RAD_ARCH_TRY(rad_op_writes(b, op_drerank, wr, 2));
depth = (int)g.max_spec;
for (int round = 1; round <= depth; ++round) {
op_couple[round - 1] = rw(b, RAD_OP(b, "mtp_couple_propose",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("n_vocab", g.n_vocab_all),
RAD_INT("round", round), RAD_INT("max_rounds", depth),
RAD_F64("q_temp", w.q_temp)),
RAD_NOWEIGHTS),
{ w.didx, w.dval, w.couple_draws }, { w.didx, w.couple_state });
if (!op_couple[round - 1]) return RAD_E_INVAL;
}
}
}
/* Keep `hout` live through the head's last op: it is the next round's input, and if only the
* lm_head's first op read it, the planner could give its bytes to later ops (silently lowering
* acceptance). */
{
const rad_op last = q2 ? op_dtopk : op_logits;
const rad_op last = !q2 ? op_logits : op_couple[0] ? op_couple[depth - 1] : op_dtopk;
if (rad_op_reads(b, last, &w.hout, 1) < 0) return RAD_E_INVAL;
}
return RAD_OK;
@@ -413,6 +453,21 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
brows(w.dlog, L), RAD_B(w.dsum), RAD_NONE);
RAD_ISSUE_N(c, op_dtopk, L, brows(w.dlog, L), brows(w.didx, L), brows(w.dval, L),
RAD_NONE); /* the `pairs` slot, absent: operands are positional */
if (op_drerank)
RAD_ISSUE_N(c, op_drerank, L, brows(w.hout, L), RAD_W(w_lm), brows(w.didx, L),
brows(w.didx, L), brows(w.dval, L), RAD_NONE);
/* After the rescored shortlist: column 0 is what the engine reads, and a sampled row
* replaces it with its x. Round 1 runs in the history pass (r < 0). */
if (op_couple[0]) {
const int round = r < 0 ? 1 : r;
if (round < 1 || round > depth) {
rad_step_fail(c, "MTP coupled round exceeds the declared draft depth");
return;
}
RAD_ISSUE_N(c, op_couple[round - 1], L, brows(w.didx, L), brows(w.dval, L),
praw(batch->seq_ids, RAD_I32, batch->n_seq),
brows(w.couple_draws, L), RAD_B(w.couple_state));
}
} else {
RAD_ISSUE_N(c, op_logits, L, brows(w.hout, L), RAD_W(w_lm), brows(w.logits, L));
}
+27 -3
View File
@@ -193,6 +193,8 @@ struct Model {
rad_buf b_mtp_e = 0, b_mtp_c = 0, b_mtp_x = 0, b_mtp_h = 0;
rad_buf b_mtp_dq = 0, b_mtp_ds = 0, b_mtp_dsum = 0;
rad_buf b_mtp_dlog = 0, b_mtp_didx = 0, b_mtp_dval = 0;
rad_buf b_mtp_couple_draws = 0, b_mtp_couple_state = 0;
int64_t mtp_topr = 1;
/* Auxiliary hidden-state taps, declared only when the container carries a drafter that wants
* them. A DFlash2 drafter reads the residual stream at several intermediate layers and
@@ -1169,8 +1171,27 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype, {g.max_logit_rows, mtp_draft_rows(g.n_vocab_all)});
/* A draft round's proposal comes out of row_topk here, never through the sampler's
* token buffer. */
m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, 1});
m.b_mtp_dval = decl_b(b, nm.f("mtp_dval"), RAD_F32, {g.max_logit_rows, 1});
/* --draft-coupling: a top-8 shortlist, rescored against the trunk's head, that each
* round's mtp_couple_propose samples x ~ Q from into column 0 (rad_block_mtp_fp8.h) */
m.mtp_topr = ctx->draft_coupling ? 8 : 1;
m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, m.mtp_topr});
m.b_mtp_dval = decl_b(b, nm.f("mtp_dval"), RAD_F32, {g.max_logit_rows, m.mtp_topr});
if (ctx->draft_coupling) {
if (ctx->max_spec < 1 || ctx->max_spec > RAD_MTP_COUPLE_MAX_ROUNDS) {
rad_note(b, "--draft-coupling on: the MTP head couples at most %d rounds",
RAD_MTP_COUPLE_MAX_ROUNDS);
return RAD_E_UNSUPPORTED;
}
m.b_mtp_couple_draws = decl_b(b, nm.f("mtp_couple_draws"), RAD_U32,
{g.max_seqs, RAD_MTP_COUPLE_DRAW_WORDS}, RAD_BUF_PERSIST);
m.b_mtp_couple_state = decl_b(b, nm.f("mtp_couple_state"), RAD_U32,
{ctx->max_spec, g.max_seqs, RAD_MTP_COUPLE_STATE_WORDS},
RAD_BUF_PERSIST);
if (!m.b_mtp_couple_draws || !m.b_mtp_couple_state) return RAD_E_INVAL;
rad_note(b, "MTP coupled: x~Q from the 2-bit head's top 8 rescored against the "
"trunk's head, %d round(s); fixed-seed tokens are NOT preserved",
ctx->max_spec);
}
}
/* The head reuses the trunk's working buffers (`attn_qg`, `attn_k`, the ffn planes, `h`),
@@ -1185,6 +1206,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
tw.e = m.b_mtp_e; tw.c = m.b_mtp_c; tw.x = m.b_mtp_x;
tw.dq = m.b_mtp_dq; tw.ds = m.b_mtp_ds; tw.dsum = m.b_mtp_dsum;
tw.dlog = m.b_mtp_dlog; tw.didx = m.b_mtp_didx; tw.dval = m.b_mtp_dval;
tw.topr = m.mtp_topr;
if (const char* qt = std::getenv("RADIANCE_MTP_Q_TEMP")) tw.q_temp = std::atof(qt);
tw.couple_draws = m.b_mtp_couple_draws; tw.couple_state = m.b_mtp_couple_state;
tw.attn = tw_attn; tw.mlp = tw_mlp;
MtpHead::Src ts{};
@@ -1238,7 +1262,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
dd.kind = RAD_DRAFT_SERIAL;
dd.depth = ctx->max_spec;
dd.proposal = m.b_mtp_didx;
dd.proposal_pitch = 1;
dd.proposal_pitch = m.mtp_topr; /* the pick is column 0 */
dd.mask_token = -1;
RAD_ARCH_TRY(rad_declare_drafter(b, &dd));
}
+3 -2
View File
@@ -2794,7 +2794,7 @@ resolutions of one op. The one exception is `sample_chain` (below).
| `sample_argmax` | `M`(range) `n_vocab` | `logits`, `params` → `token` |
| `sample_merge_topk` | `M`(range) `n_cand` | `gathered`, `params` → `cand_idx`, `cand_val` |
| `sample_logprobs` | `M`(range) `n_vocab` `k` `vocab_off`/opt | `logits`, `params` → `stats` |
| `mtp_couple_propose` | `M`(range) `n_vocab` `round` `max_rounds` | `idx`:inout, `val`, `seq_ids`, `draw` → `state`:inout |
| `mtp_couple_propose` | `M`(range) `n_vocab` `round` `max_rounds` `q_temp`:f64/opt | `idx`:inout, `val`, `seq_ids`, `draw` → `state`:inout |
| `mtp_couple_verify` | `M`(range) `n_cand` `n_vocab` | `cand_idx`, `cand_val`, `params`, `state` → `token`:inout |
| `dflash_couple_propose` | `M`(range) `steps` `top_k` `rank` `n_vocab` `anchor_stride` `max_rounds` `q_temp`:f64/opt | `cand`, `unary`, `hp`?, `anchor`, `pred`w, `succ`w, `seq_ids`, `draw`, `tokens`:inout → `state`:inout |
@@ -2810,7 +2810,8 @@ if it is set, it must name `spechub` or the coupled ops are not loaded at all.
`mtp_couple_propose` runs on a draft round's
final reranked top-8 and samples the proposal `x ~ Q`, where `Q` is the FP64 softmax of those
scores at temperature 1; it writes `x` into column 0 of `idx` and one state row per round. A row
scores divided by `q_temp` (absent: 1, the scores' own law; any value keeps the coupling exact,
since `Q` is stored as drawn, and moves acceptance only); it writes `x` into column 0 of `idx` and one state row per round. A row
whose `seq_ids` does not name the card its draw was staged for, or whose shortlist is malformed,
leaves column 0 alone with `status = -1`. `mtp_couple_verify` runs after `sample_pick`, over the same
final candidate plane, and applies the maximal coupling: the draft is accepted when it equals the
+37
View File
@@ -0,0 +1,37 @@
/* xe2_couple.cpp -- the linear-MTP coupling ops (`--draft-coupling on`) on the card.
*
* `mtp_couple_propose` and `mtp_couple_verify` (docs/OPS.md) through the spechub plugin's own row
* helpers (plugin/spechub/spechub.h over abi/rad_spechub.h) and argument readers, one work-item a
* row: the rows are a step's sampled positions (at most the draft window), and the sampler's
* candidate plane holds ~top_k live entries, so the serial walks are short. The arithmetic is the
* host reference's, in its order, in FP64 (libxe2 builds with -ffp-contract=off).
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include "../plugin/spechub/spechub.cpp"
namespace xe2 {
int mtp_couple_propose(const RadArgs* a, RadStream st) {
RadMtpCoupleProposeCall c{};
if (int rc = rad_mtp_couple_propose_args(a, &c); rc != RAD_OK) return rc;
if (c.M <= 0) return RAD_OK;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
return submit([&] {
q->parallel_for(sycl::range<1>{(size_t)c.M}, [=](sycl::id<1> i) { rad_mtp_couple_propose_row(c, (int64_t)i[0]); });
});
}
int mtp_couple_verify(const RadArgs* a, RadStream st) {
RadMtpCoupleVerifyCall c{};
if (int rc = rad_mtp_couple_verify_args(a, &c); rc != RAD_OK) return rc;
if (c.M <= 0) return RAD_OK;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
return submit([&] {
q->parallel_for(sycl::range<1>{(size_t)c.M}, [=](sycl::id<1> i) { rad_mtp_couple_verify_row(c, (int64_t)i[0]); });
});
}
} // namespace xe2
+6
View File
@@ -2,6 +2,10 @@
#pragma once
#include "rad_abi.h"
/* spechub's operand shapes for the coupling ops (plugin/spechub/spechub.cpp, built into xe2_couple.cpp) */
int rad_mtp_couple_propose_shape(const RadParam*, int, int, RadOpdDesc*);
int rad_mtp_couple_verify_shape(const RadParam*, int, int, RadOpdDesc*);
namespace xe2 {
int add(const RadArgs* a, RadStream s);
@@ -13,6 +17,8 @@ int grid_embed(const RadArgs* a, RadStream s);
int rmsnorm(const RadArgs* a, RadStream s);
int add_rmsnorm(const RadArgs* a, RadStream s);
int mtp_couple_propose(const RadArgs* a, RadStream s);
int mtp_couple_verify(const RadArgs* a, RadStream s);
int rmsnorm_quant_fp8(const RadArgs* a, RadStream s);
int quant_act_fp8(const RadArgs* a, RadStream s);
int gated_quant_fp8(const RadArgs* a, RadStream s);
+13
View File
@@ -86,6 +86,19 @@ const RadKernelInfo kKernels[] = {
.shape = "any n; bf16 rows wider than 512, 8-aligned, in one pass, else the pair",
.dtypes = "float x, a, gain and y (bf16 in the recipes), f32 arithmetic",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::add_rmsnorm },
{ .name = "xe2_mtp_couple_propose", .op = "mtp_couple_propose", .family = "draft",
.computes = "x~Q over the final top8, FP64 T1 softmax: spechub's row helper, a work-item a row",
.shape = "M,n_vocab,round,max_rounds<=8",
.dtypes = "f32,i32,u32",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::mtp_couple_propose,
.opd_shape = rad_mtp_couple_propose_shape },
{ .name = "xe2_mtp_couple_verify", .op = "mtp_couple_verify", .family = "draft",
.computes = "maximal coupling over the surviving target candidates, FP64 P: spechub's row "
"helper, a work-item a row",
.shape = "M,n_cand,n_vocab",
.dtypes = "f32,i32,u32",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::mtp_couple_verify,
.opd_shape = rad_mtp_couple_verify_shape },
{ .name = "xe2_rmsnorm_quant_fp8", .op = "rmsnorm_quant_fp8", .family = "norm",
.computes = "residual add (optional), RMS norm, bf16 narrowing, E4M3 codes and an f32 scale "
"per group; int8 codes at amax/127 for an int8 q",
+6 -3
View File
@@ -13,7 +13,7 @@ namespace {
#define PI(k) { k, RAD_P_INT, RAD_REQUIRED, 0 }
const RadParamSpec kProposeParams[] = {
PI("M"), PI("n_vocab"), PI("round"), PI("max_rounds"),
PI("M"), PI("n_vocab"), PI("round"), PI("max_rounds"), { "q_temp", RAD_P_F64, RAD_OPTIONAL, 0 },
};
const RadParamSpec kVerifyParams[] = { PI("M"), PI("n_cand"), PI("n_vocab") };
const RadParamSpec kDflashParams[] = {
@@ -58,8 +58,9 @@ const RadOperandSpec kDflashOperands[] = {
};
const RadOpSchema kSchemas[] = {
{ "mtp_couple_propose", kProposeParams, 4, kProposeOperands, 5,
"Linear MTP round `round`: x~Q, Q = FP64 T1 softmax of the final top8 scores; x into idx "
{ "mtp_couple_propose", kProposeParams, 5, kProposeOperands, 5,
"Linear MTP round `round`: x~Q, Q = FP64 softmax of the final top8 scores / q_temp (default "
"1); x into idx "
"column 0 and the state into state[round-1][row]. mode0 rows keep column 0 (status 0); bad "
"ids/scores or seq_ids != draw.card keep it too (status -1)." },
{ "mtp_couple_verify", kVerifyParams, 3, kVerifyOperands, 5,
@@ -230,6 +231,8 @@ int rad_mtp_couple_propose_args(const RadArgs* a, RadMtpCoupleProposeCall* out)
c.M = M;
c.vocab = v;
c.round = (int)rad_param_geti(a->p, a->n_p, "round", 0);
c.q_temp = (float)rad_param_getf(a->p, a->n_p, "q_temp", 1.0);
if (!(c.q_temp > 0.0f) || c.q_temp != c.q_temp || c.q_temp > 1e6f) return RAD_E_INVAL;
c.idx = (int32_t*)a->t[0].data;
c.val = (const float*)a->t[1].data;
c.card = (const int32_t*)a->t[2].data;
+8 -2
View File
@@ -31,6 +31,7 @@ struct RadMtpCoupleProposeCall {
int64_t vocab;
int64_t ld_idx, ld_val, ld_draw, st_round, st_row;
int round;
float q_temp = 1.0f; /* Q = softmax(val / q_temp); 1: the scores' own law */
};
/* The verify's operands: the sampler's own candidate plane, its parameter rows, the bound state
@@ -100,8 +101,13 @@ static inline RAD_SPECHUB_HD void rad_mtp_couple_propose_row(const RadMtpCoupleP
for (int i = 0; i < 8; ++i) s.ids[i] = -1;
if (s.draw.mode == 1) {
s.status = -1;
if (s.draw.card == c.card[row] &&
rad_mtp_couple_build_q(idx, c.val + row * c.ld_val, c.vocab, &s)) {
const float* v = c.val + row * c.ld_val;
float vt[8];
if (c.q_temp != 1.0f) {
for (int i = 0; i < 8; ++i) vt[i] = v[i] / c.q_temp;
v = vt;
}
if (s.draw.card == c.card[row] && rad_mtp_couple_build_q(idx, v, c.vocab, &s)) {
const double u = rad_spechub_uniform(s.draw.seed, s.draw.base,
RAD_MTP_COUPLE_KEY_DRAFT + (uint64_t)c.round);
s.x = s.ids[rad_mtp_couple_pick(s, u)];