spec: --draft-verify token|block|traversal for the coupled MTP chain

--draft-coupling verifies each row of the chain on its own. Two lossless
sequence-level verifiers accept more drafts from the same chain: Block
Verification (arXiv 2403.10444) decides the whole chain at once, Traversal
Verification (arXiv 2505.12398) walks a path from its last draft back to its
first. --draft-verify token (default) is the engine as it was; block switches
the coupling on; traversal needs it and is refused otherwise.

Block: the sampler marks the consecutive bound rows of a request with the
block's length (RadMtpCoupleState::blk) and mtp_couple_verify decides the block
on the workgroup of its round-1 row, every FP64 sum a fixed lane tree so the
result is the host reference's bit for bit. The wide-plane serial helper is a
kernel of its own, which leaves couple_verify without scratch.

Traversal: a new op mtp_trav_verify takes the step's topology (parent row, draft
token per row) and decides a request on one workgroup, 0 bytes of scratch: the
target's law as integer masses of a fixed-point exponential, the walk in FP64.
Its header and kernel also handle nodes with several children (Gumbel-top-k and
RheoSampling siblings); the serial head here drafts one, so that part has no
producer yet and is covered by the tests only.

RadMtpCoupleState grows from 152 to 168 bytes (nkids, scheme, blk), so
RAD_ABI_VERSION is 41.

Exactness: mtp_block_test enumerates the draft tree of a toy model and runs
chi-square over the real verify code; mtp_rrs_test runs chi-square and an
exhaustive enumeration of the walk (worst |joint - P| 7e-14) with negative
controls that fail; spechub_device_test compares the device rows with the host
rows. Measured on Qwen3.8-Flash-Next IQR-V4 (TP2, MTP chain 3, coupling) on our
branch: about +1% tok/s with traversal, +4.6% at T 1.0 top_k 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 21:49:45 +01:00
1 parent 9b6eba80c6
commit d0fd4aa1bb
31 files changed
+5022 -30

No files matched your search

+37
View File
@@ -216,6 +216,13 @@ static const Flag g_flags[] = {
" couples the target's draw to it: more accepted drafts on sampled\n"
" requests, the same output law, other tokens than off for a fixed\n"
" seed" },
{ "--draft-verify", "MODE", "token (default, alias rowwise) | block | traversal -- how a coupled draft chain is\n"
" verified: token decides each row on its own (maximal coupling);\n"
" block decides the whole chain at once (Block Verification, arXiv\n"
" 2403.10444; switches --draft-coupling on); traversal decides a\n"
" root-to-leaf path bottom-up (Traversal Verification, arXiv\n"
" 2505.12398; needs --draft-coupling on). The same output law in\n"
" every mode, more drafts accepted per step in block and traversal" },
{ "--draft-model", "PATH", "NOT IMPLEMENTED -- merge the drafter at convert time" },
{ "--host", "ADDR", "listen address" },
@@ -904,6 +911,19 @@ int config_parse(int argc, char** argv, Config* c) {
return RAD_E_INVAL;
}
}
else if (S("--draft-verify")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (!std::strcmp(v, "token") || !std::strcmp(v, "rowwise")) {
c->draft_verify_block = false;
c->draft_verify_traversal = false;
}
else if (!std::strcmp(v, "block")) { c->draft_verify_block = true; c->draft_verify_traversal = false; }
else if (!std::strcmp(v, "traversal")) { c->draft_verify_block = false; c->draft_verify_traversal = true; }
else {
RAD_ERR("--draft-verify takes token (rowwise), block or traversal, not '%s'", v);
return RAD_E_INVAL;
}
}
else if (S("--draft-model")) {
/* Refused, not ignored: separate draft models are not supported (spec §10), and the
* drafter comes from the container (rad-convert --draft-model merges one in; an MTP
@@ -1411,6 +1431,21 @@ int config_parse(int argc, char** argv, Config* c) {
RAD_ERR("--video-min-pixels is above --video-max-pixels"); return RAD_E_INVAL; }
if (c->video_min_frames && c->video_max_frames && *c->video_min_frames > *c->video_max_frames) {
RAD_ERR("--video-min-frames is above --video-max-frames"); return RAD_E_INVAL; }
/* --draft-verify block verifies a linear chain with the coupled drafting's states, so it
* switches the coupling on; traversal needs it and says so rather than switching it on. */
if (c->draft_verify_block) {
if (c->given.count("--draft-coupling") && !c->draft_coupling) {
RAD_ERR("--draft-verify block needs the coupled drafting (--draft-coupling on); "
"--draft-coupling off contradicts it");
return RAD_E_INVAL;
}
c->draft_coupling = true;
}
if (c->draft_verify_traversal && !c->draft_coupling) {
RAD_ERR("--draft-verify traversal requires --draft-coupling on: the walk needs the draft's "
"law Q, which only a coupled draft carries");
return RAD_E_INVAL;
}
/* --host-pool-mib is what the model pins for its weights in all; Config::host_pool_mib is a
* rank's share of it, whichever of the two options came first. */
if (pool_all) c->host_pool_mib /= std::max(c->tp, 1);
@@ -1536,6 +1571,8 @@ const ShowRow kShow[] = {
return c.draft_short_from < 0 ? std::string("auto")
: c.draft_short_from > 0 ? num(c.draft_short_from) : std::string("0 (never)"); } },
{ "--draft-coupling", [](const Config& c) { return onoff(c.draft_coupling); } },
{ "--draft-verify", [](const Config& c) {
return std::string(c.draft_verify_block ? "block" : c.draft_verify_traversal ? "traversal" : "token"); } },
{ "--draft-model", [](const Config&) { return std::string("not implemented (the container's own drafter)"); } },
{ "--host", [](const Config& c) { return c.host; } },
{ "--port", [](const Config& c) { return num(c.port); } },
+2
View File
@@ -880,6 +880,8 @@ int Engine::declare_sampler(int rank, RadBuilder* b, int64_t logits_width, bool
sc.rank = rank;
sc.world_size = cfg_.tp;
sc.draft_coupling = cfg_.draft_coupling;
sc.draft_verify_block = cfg_.draft_verify_block;
sc.draft_verify_traversal = cfg_.draft_verify_traversal;
RAD_TRY(smp.declare(b, sc));
/* Where the chain writes the sampled ids. The engine declares it because rad-kbench drives the
+5
View File
@@ -59,6 +59,11 @@ int Engine::step_state_init() {
* pinned staging beside them. A program without them is refused rather than served
* uncoupled. */
mtp_couple_ = cfg_.draft_coupling;
if (cfg_.draft_verify_traversal && !mtp_couple_) {
RAD_ERR("--draft-verify traversal requires --draft-coupling on: the walk needs the draft's "
"law Q, which only a coupled draft carries");
return RAD_E_INVAL;
}
if (mtp_couple_) {
/* Coupling is defined on a linear chain of drafts: a serial head's rounds, or a block
* drafter whose walk samples each position in turn (DFlash2, dflash_couple_propose), its
+9
View File
@@ -1038,6 +1038,15 @@ struct Config {
* (RadBuildCtx::draft_coupling, abi/rad_spechub.h).
* Off by default; on changes which tokens a fixed seed yields, not their law. */
bool draft_coupling = false;
/* --draft-verify token|block|traversal: how a coupled draft chain is verified. token (default):
* each row's maximal coupling (above). block: Block Verification (arXiv 2403.10444,
* abi/rad_mtp_block.h), the chain's drafts accepted jointly; the same output law, at least as
* many accepted tokens. block switches --draft-coupling on (the drafting half is shared).
* traversal: Traversal Verification (arXiv 2505.12398, abi/rad_mtp_traversal.h) walks the
* chain from its last draft back to its first; the same output law, never fewer accepted
* tokens than token-wise, and it needs --draft-coupling on. */
bool draft_verify_block = false;
bool draft_verify_traversal = false;
/* ---------------- KL DIVERGENCE AGAINST A REFERENCE (core/kld.h) ----------------
*
+49 -4
View File
@@ -164,6 +164,7 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
cfg_ = cfg;
couple_ = cfg.draft_coupling;
trav_ = cfg.draft_coupling && cfg.draft_verify_traversal;
if (cfg_.n_vocab_local <= 0) cfg_.n_vocab_local = cfg_.n_vocab / cfg_.world_size;
/* The staged mask covers this rank's shard; the grammar's covers the whole vocabulary.
@@ -223,6 +224,10 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
buf_couple_ = decl("sampler.mtp_couple", RAD_U32, rows, RAD_MTP_COUPLE_STATE_WORDS,
RAD_BUF_PERSIST);
if (!buf_couple_) return RAD_E_NOMEM;
if (trav_) {
buf_trav_ = decl("sampler.mtp_trav", RAD_I32, rows, 2, RAD_BUF_PERSIST);
if (!buf_trav_) return RAD_E_NOMEM;
}
}
buf_cand_i_ = decl("sampler.cand_idx", RAD_I32, rows, ncand, RAD_BUF_TRANSIENT);
buf_cand_v_ = decl("sampler.cand_val", RAD_F32, rows, ncand, RAD_BUF_TRANSIENT);
@@ -304,8 +309,13 @@ int Sampler::declare(RadBuilder* b, const SamplerConfig& cfg) {
RadParam{ "n_cand", RAD_P_INT, (long long)ncand, 0, nullptr },
RadParam{ "n_vocab", RAD_P_INT, (long long)cfg_.n_vocab, 0, nullptr },
};
op_couple_ = rad_decl_op(b, "mtp_couple_verify", pc, 3, nullptr, 0);
if (!op_couple_) return RAD_E_NOKERNEL;
if (trav_) {
op_trav_ = rad_decl_op(b, "mtp_trav_verify", pc, 3, nullptr, 0);
if (!op_trav_) return RAD_E_NOKERNEL;
} else {
op_couple_ = rad_decl_op(b, "mtp_couple_verify", pc, 3, nullptr, 0);
if (!op_couple_) return RAD_E_NOKERNEL;
}
/* No rad_op_reads: the other sampler ops declare no lists, and a list on this op alone
* would narrow the candidate planes' lifetime to it and refuse the first sample_topk. */
}
@@ -391,6 +401,8 @@ int Sampler::stage_alloc() {
if (couple_)
couple_stage_ = (RadMtpCoupleState*)rad_dev_alloc(
rows * (int64_t)sizeof(RadMtpCoupleState), RAD_MEM_HOST_PINNED);
if (trav_)
trav_stage_ = (int32_t*)rad_dev_alloc(rows * 2 * (int64_t)sizeof(int32_t), RAD_MEM_HOST_PINNED);
/* Logprobs readback; only rank 0 reads it. */
if (cfg_.rank == 0) {
@@ -407,7 +419,8 @@ int Sampler::stage_alloc() {
}
if (!params_stage_ || !greedy_stage_ || !late_stage_ || !hist_stage_ || !brk_stage_ ||
!ngram_stage_ || !ngram_wl_stage_ || !mask_stage_ || (couple_ && !couple_stage_)) {
!ngram_stage_ || !ngram_wl_stage_ || !mask_stage_ || (couple_ && !couple_stage_) ||
(trav_ && !trav_stage_)) {
RAD_ERR("sampler: could not pin %s of staging",
humanb(rows * (3 * (int64_t)sizeof(DeviceSampleParams) + cfg_.max_hist * 5 +
mask_words_ * 8 + 8)).c_str());
@@ -428,6 +441,8 @@ void Sampler::stage_free() {
if (mask_stage_) rad_dev_free(mask_stage_, RAD_MEM_HOST_PINNED);
if (couple_stage_) rad_dev_free(couple_stage_, RAD_MEM_HOST_PINNED);
couple_stage_ = nullptr;
if (trav_stage_) rad_dev_free(trav_stage_, RAD_MEM_HOST_PINNED);
trav_stage_ = nullptr;
if (lp_host_) rad_dev_free(lp_host_, RAD_MEM_HOST_PINNED);
if (lp_ci_host_) rad_dev_free(lp_ci_host_, RAD_MEM_HOST_PINNED);
if (lp_cv_host_) rad_dev_free(lp_cv_host_, RAD_MEM_HOST_PINNED);
@@ -790,6 +805,11 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
think = st->think;
}
const ReasoningCount think0 = think;
/* --draft-verify block: the chain rows bound from round 1 on, consecutively, are one block
* (abi/rad_mtp_block.h); each carries the block's length in `blk` so the verify's round-1
* row decides them together. */
int64_t blk_row0 = -1;
int blk_len = 0;
for (int k = 0; k < n_pos; ++k) {
/* Row k's drafts before it: 0..k-1 on a chain, its ancestors (root first) on a tree. */
@@ -822,6 +842,17 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
think.push(sp.reasoning, r->draft[k - 1]);
}
const int64_t row = n_seq_++;
/* The traversal verify's tree: this row's parent row in the step (-1 for the
* request's first row) and its own draft token. A chain's row k hangs off row k - 1. */
if (trav_) {
int32_t pk = -1;
if (k > 0) {
pk = k - 1;
if (tpar) { const int32_t pj = tpar[k - 1]; if (pj >= 0 && pj < k) pk = pj; }
}
trav_stage_[2 * row] = pk < 0 ? -1 : (int32_t)(row - k + pk);
trav_stage_[2 * row + 1] = k > 0 && r->draft ? r->draft[k - 1] : 0;
}
/* Row k verifies draft k against the state its round left. A row with no bound state
* (the bonus row, a greedy row, an uncoupled step) gets a zeroed one, and the verify
* leaves the pick's token alone. */
@@ -836,6 +867,13 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
if (rad_mtp_couple_binds(c, r->id, sp.seed, (uint64_t)n_out, k, r->draft[k],
cfg_.n_vocab)) {
couple_stage_[row] = c;
if (cfg_.draft_verify_block && !tpar) {
if (k == 0) { blk_row0 = row; blk_len = 0; }
if (blk_row0 >= 0 && k == blk_len) {
++blk_len;
for (int64_t j = blk_row0; j <= row; ++j) couple_stage_[j].blk = blk_len;
}
}
any_couple_ = true;
++couple_build_.rows;
auto& q = couple_build_.seqs;
@@ -1219,6 +1257,9 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
if (any_couple_)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_couple_), couple_stage_,
n_seq_ * (int64_t)sizeof(RadMtpCoupleState), s));
if (any_couple_ && trav_)
RAD_TRY(rad_memcpy_async(rad_buf_ptr(c, buf_trav_), trav_stage_,
n_seq_ * 2 * (int64_t)sizeof(int32_t), s));
const RadOperand o_logits = opd_rows(logits, n_seq_);
const RadOperand o_params = opd_rows(buf_params_, n_seq_);
@@ -1317,7 +1358,11 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
/* After the pick, over the same plane: a coupled row replaces the pick's token and an unbound
* row keeps it. The issued step is recorded for the evidence line. */
if (any_couple_) {
RAD_TRY(issue(op_couple_, { o_ci, o_cv, o_params, opd_rows(buf_couple_, n_seq_), o_tok }));
if (trav_)
RAD_TRY(issue(op_trav_, { o_ci, o_cv, o_params, opd_rows(buf_couple_, n_seq_), o_tok,
opd_rows(buf_trav_, n_seq_) }));
else
RAD_TRY(issue(op_couple_, { o_ci, o_cv, o_params, opd_rows(buf_couple_, n_seq_), o_tok }));
couple_issued_step_ = couple_build_.step;
}
+11 -1
View File
@@ -127,6 +127,12 @@ struct SamplerConfig {
/* --draft-coupling: declare the coupled verify (`mtp_couple_verify`) behind the pick and the
* per-row state plane it reads. */
bool draft_coupling = false;
/* --draft-verify block: the chain rows of a coupled request are verified as one block
* (abi/rad_mtp_block.h) instead of one at a time. Needs draft_coupling. */
bool draft_verify_block = false;
/* --draft-verify traversal: `mtp_trav_verify` replaces the row-wise verify behind the pick
* (needs draft_coupling) and a per-row topology plane rides with the state plane. */
bool draft_verify_traversal = false;
};
/* ================================================================== the driver */
@@ -234,7 +240,7 @@ public:
* verify op at all. Cleared by build_greedy: a gate for run(), not for evidence. */
bool staged_any_couple() const { return any_couple_; }
/* The verify op the last build resolved, for naming its kernel in a log line. */
rad_op staged_couple_op() const { return op_couple_; }
rad_op staged_couple_op() const { return trav_ ? op_trav_ : op_couple_; }
/* What build_step staged for the coupled verify. build_greedy, which runs between that build
* and the step's wait, clears any_couple_ but leaves this snapshot. */
@@ -303,6 +309,10 @@ private:
rad_op op_allgather_ = RAD_NULL_HANDLE;
rad_op op_merge_ = RAD_NULL_HANDLE; /* the candidate merge, under tensor parallel */
rad_op op_couple_ = RAD_NULL_HANDLE; /* --draft-coupling only */
rad_op op_trav_ = RAD_NULL_HANDLE; /* --draft-verify traversal: replaces op_couple_ */
rad_buf buf_trav_ = RAD_NULL_HANDLE; /* [rows, 2] i32: parent row (-1 root), draft token */
int32_t* trav_stage_ = nullptr; /* pinned, 2 words a sampled row */
bool trav_ = false;
rad_op op_logprobs_ = RAD_NULL_HANDLE; /* summary of the raw distribution */
rad_op op_lp_gather_ = RAD_NULL_HANDLE; /* every rank's summary, under a split vocabulary */