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

+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;
}