dflash2: lossless coupled acceptance at T > 0 (--draft-coupling on)

DFlash2's drafts were the greedy walk over its candidate lattice, accepted
only when they equal the target's own sampled draw: P(y == x) instead of
rejection sampling's sum min(P, Q). --draft-coupling already did the latter
for a linear MTP head; this extends it to the block drafter.

- dflash_couple_propose (spechub plugin, device row + host oracle): after
  dflash_select, the same lattice walked again, sampled. Position l scores
  each candidate as dflash_select does (unary + low-rank edge from the
  previous token), Q_l = FP64 softmax of score / q_temp over the best eight
  in-vocabulary candidates, x_l ~ Q_l at (seed, base, KEY_DRAFT + l + 1),
  x_l is the next predecessor. Position l writes round l+1's state row, so
  the engine binds it and the sampler's mtp_couple_verify (maximal coupling,
  residual (P - Q)+) verifies it exactly as an MTP round. Q depends on the
  committed text and on x_0..x_{l-1}, never on the verify, and is stored
  exactly. Greedy requests keep dflash_select's tokens (status 0); a draw
  for another card or a Q that cannot be built takes the walk's argmax and
  the ordinary verify (status -1). The shared arithmetic is in
  abi/rad_spechub.h (rad_dflash_couple_step / _walk).
- The block driver stages one draw per drafting row before the query pass,
  reads the states back with the slots and binds them after the commit
  (Engine::bind_couple, shared with the serial driver).
- qwen35_fp8 declares the planes and the op under --draft-coupling;
  RADIANCE_DFLASH_Q_TEMP sets Q's temperature (default 1, exact at any).
- Default (flag off): no op, no plane, no change.

Gates:
- spechub_dflash_couple_test (CPU): emitted token vs P by chi-square over 3
  seeds each, Fisher-combined p 0.135..0.919, for Q = P, Q on fewer tokens
  than P, Q mostly outside P, top_k 20 + top_p 0.95, top_p 0.6, top_k 3 at
  T 0.7, peaked Q; acceptance = sum min(P, Q) within 5 sigma; the walk's
  stored Q equals the softmax of its scores (1e-5) and x follows it (p
  0.993); the whole draft/verify/bonus loop over 3 tokens matches the
  target's autoregressive law (combined p 0.62 at q_temp 1, 0.67 at 0.5,
  0.024 with every 2nd step on the ordinary verify).
- Device row vs host oracle, 40 random lattices at the production geometry
  (V 248320, K 16, R 256, steps 7/3, M 1..16): tokens, x, ids, status
  identical; q differs only by FP64 exp ulps (<= 4.3e-16 relative).
  91 us a launch at M 16, steps 7.
- Engine, Qwen3.8-27B MXFP4 TP2, our serving flags, 4-6 tokens of two chat
  prompts at T 1 / top_p 0.95 / top_k 20, 5000-11000 samples per arm:
  coupled vs ordinary speculation chi-square p 0.23..0.81 (tokens 1-4 and
  pair 1-2); both differ from no speculation on one prompt by the same
  amount (the verify-vs-decode logits, not the rule).
- Flag off, old build (up12 5dbb5dc) vs this: greedy top-5 logprob traces
  identical over 300 tokens (spec 7 and 0, two requests each), 6/6
  fixed-seed sampled texts identical.
- ctest (the environment-only tests excluded) 105/105.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-27 18:37:28 +01:00
1 parent 72a58d0c0c
commit cb0abe9ac7
18 files changed
+1330 -115

No files matched your search

+78 -47
View File
@@ -60,12 +60,15 @@ int Engine::step_state_init() {
* uncoupled. */
mtp_couple_ = cfg_.draft_coupling;
if (mtp_couple_) {
/* Coupling is defined on a linear serial chain. A block drafter proposes a whole block
* from one pass, so it has no per-round draw to couple and no per-round state. */
if (drafter_.kind != RAD_DRAFT_SERIAL || drafter_.depth <= 0) {
RAD_ERR("--draft-coupling on requires the linear serial draft chain (RAD_DRAFT_SERIAL "
"with depth > 0); this model declares drafter '%s' kind %d depth %d, which has "
"no per-round draw to 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
* position l standing for round l + 1. Either way one draw a drafting row and one state a
* (round, row). */
if ((drafter_.kind != RAD_DRAFT_SERIAL && drafter_.kind != RAD_DRAFT_BLOCK) ||
drafter_.depth <= 0) {
RAD_ERR("--draft-coupling on requires a linear draft chain (a serial head or a block "
"drafter with a coupled walk, depth > 0); this model declares drafter '%s' "
"kind %d depth %d, which has no per-round draw to couple",
drafter_.name ? drafter_.name : "(none)", drafter_.kind, drafter_.depth);
return RAD_E_UNSUPPORTED;
}
@@ -85,7 +88,8 @@ int Engine::step_state_init() {
return RAD_E_STATE;
}
const RadBufDecl& sd = prog.buffers[couple_state_buf_].decl;
if (sd.rank != 3 || sd.shape[2] != (int64_t)RAD_MTP_COUPLE_STATE_WORDS) {
if (sd.rank != 3 || sd.shape[2] != (int64_t)RAD_MTP_COUPLE_STATE_WORDS ||
sd.shape[0] < (int64_t)drafter_.depth) {
RAD_ERR("--draft-coupling: 'mtp_couple_state' is not the [rounds, rows, %d]-word "
"plane the coupling reads", (int)RAD_MTP_COUPLE_STATE_WORDS);
return RAD_E_STATE;
@@ -99,8 +103,13 @@ int Engine::step_state_init() {
(int64_t)drafter_.depth * couple_rows_ * (int64_t)sizeof(RadMtpCoupleState),
RAD_MEM_HOST_PINNED));
if (!couple_draw_stage_ || !couple_readback_) return RAD_E_NOMEM;
RAD_INFO("MTP coupled LIVE: %d linear round(s), x~Q from the final reranked top8, maximal "
"coupling at the verify; fixed-seed tokens are NOT preserved", drafter_.depth);
if (drafter_.kind == RAD_DRAFT_BLOCK)
RAD_INFO("DFlash coupled LIVE: %d position(s), x~Q along the candidate walk, maximal "
"coupling at the verify; fixed-seed tokens are NOT preserved", drafter_.depth);
else
RAD_INFO("MTP coupled LIVE: %d linear round(s), x~Q from the final reranked top8, "
"maximal coupling at the verify; fixed-seed tokens are NOT preserved",
drafter_.depth);
}
RAD_TRY(rad_event_create_as(&step_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
@@ -206,7 +215,7 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
serial_rounds_ = rounds;
serial_rows_ = n;
/* Before any serial pass reads a draw; round 1 is the history pass below. */
if (mtp_couple_ && n > 0) RAD_TRY(stage_couple_draws(n));
if (mtp_couple_ && n > 0) RAD_TRY(stage_couple_draws(serial_row_.data(), n));
/* RADIANCE_DEBUG_STAGE: running means of a draft pass's host time: batch + advance, greedy
* sampler chain, barrier release and issue. They cost the step nothing until their sum exceeds
@@ -329,38 +338,45 @@ int Engine::finish_serial_drafter(const StepOut& so) {
if (!r0.sampler.allows_speculation(serial_row_[(size_t)i].seq)) continue;
const uint64_t seq = serial_row_[(size_t)i].seq;
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], done);
if (!mtp_couple_) continue;
/* Bound only for a request still running: one this commit finished, or whose draft the
* scheduler refused, binds nothing, and set_mtp_couple(..., 0) clears an earlier step's
* states (RAD_E_NOTFOUND for a request the sampler already dropped).
*
* The position is the committed output length after this commit, the n_out the next
* build_step reads. It comes from the scheduler's commit snapshot, not the plan's Request
* pointer, which reap() may already have freed (sched/mtp_couple_bind.h). */
MtpCoupleBindPoint bp;
bp.found = sched_.committed_seq(seq, &bp.n_out, &bp.live);
if (!bp.found)
RAD_WARN("MTP coupled: serial row for sequence %llu is not in the committed step; "
"clearing its coupling", (unsigned long long)seq);
int n_bound = 0, n_malformed = 0;
std::string why;
const int bs = mtp_couple_bind_rounds(
bp, couple_draw_stage_[(size_t)i], &couple_readback_[(size_t)i], (int)couple_rows_,
done, slots_host_[serial_row_[(size_t)i].card], meta_.n_vocab, couple_bound_,
&n_bound, &why, &n_malformed);
if (bs < 0) { RAD_ERR("%s", why.c_str()); return bs; }
if (n_malformed > 0 && !couple_malformed_note_) {
couple_malformed_note_ = true;
RAD_WARN("MTP coupled: sequence %llu: %d draft round(s) had a shortlist with no valid "
"Q (non-finite score, duplicate id or no live entry); those rounds keep the "
"greedy proposal and the ordinary verify (logged once)",
(unsigned long long)seq, n_malformed);
}
for (auto& rk : ranks_) {
const int st = rk->sampler.set_mtp_couple(seq, couple_bound_,
rc == RAD_OK ? n_bound : 0);
if (st < 0 && st != RAD_E_NOTFOUND) return st;
}
if (mtp_couple_)
RAD_TRY(bind_couple(seq, i, done, rc == RAD_OK, serial_row_[(size_t)i].card));
}
return RAD_OK;
}
/* --draft-coupling, after the commit: bind drafting row `i`'s `done` states (its readback column)
* to the positions the next build samples, and hand them to every rank's sampler.
*
* Bound only for a request still running: one this commit finished, or whose draft the scheduler
* refused (`drafted` false), binds nothing, and set_mtp_couple(..., 0) clears an earlier step's
* states (RAD_E_NOTFOUND for a request the sampler already dropped).
*
* The position is the committed output length after this commit, the n_out the next build_step
* reads. It comes from the scheduler's commit snapshot, not the plan's Request pointer, which
* reap() may already have freed (sched/mtp_couple_bind.h). */
int Engine::bind_couple(uint64_t seq, int i, int done, bool drafted, int32_t card) {
MtpCoupleBindPoint bp;
bp.found = sched_.committed_seq(seq, &bp.n_out, &bp.live);
if (!bp.found)
RAD_WARN("coupled draft: row for sequence %llu is not in the committed step; clearing its "
"coupling", (unsigned long long)seq);
int n_bound = 0, n_malformed = 0;
std::string why;
const int bs = mtp_couple_bind_rounds(bp, couple_draw_stage_[(size_t)i],
&couple_readback_[(size_t)i], (int)couple_rows_, done,
slots_host_[card], meta_.n_vocab, couple_bound_,
&n_bound, &why, &n_malformed);
if (bs < 0) { RAD_ERR("%s", why.c_str()); return bs; }
if (n_malformed > 0 && !couple_malformed_note_) {
couple_malformed_note_ = true;
RAD_WARN("coupled draft: sequence %llu: %d draft position(s) had a shortlist with no valid "
"Q (non-finite score, duplicate id or no live entry); those keep the greedy "
"proposal and the ordinary verify (logged once)",
(unsigned long long)seq, n_malformed);
}
for (auto& rk : ranks_) {
const int st = rk->sampler.set_mtp_couple(seq, couple_bound_, drafted ? n_bound : 0);
if (st < 0 && st != RAD_E_NOTFOUND) return st;
}
return RAD_OK;
}
@@ -391,18 +407,18 @@ void Engine::couple_verify_evidence(int step) {
}
}
/* --draft-coupling: the draw for every drafting row, before any serial pass reads it.
/* --draft-coupling: the draw for every drafting row, before any draft pass reads it.
*
* The coordinate is the pre-commit output size, which strictly grows between two steps that draft
* one request, so (base, round) never repeats a draft stream. The card is the slot the advance
* writes; the propose op keeps the greedy proposal for a row whose draw names another card. The
* rule is sched/mtp_couple_plan.h. */
int Engine::stage_couple_draws(int n) {
template <class Row>
int Engine::stage_couple_draws(const Row* rows, int n) {
std::string why;
const int stg = mtp_couple_plan_stage(sched_.plan(),
(int)(cfg_.max_seqs > 0 ? cfg_.max_seqs : 1),
serial_row_.data(), n, &couple_by_card_,
couple_draw_stage_, &why);
rows, n, &couple_by_card_, couple_draw_stage_, &why);
if (stg < 0) {
RAD_ERR("MTP coupled: %s", why.c_str());
return stg;
@@ -505,6 +521,9 @@ int Engine::issue_block_drafter(int step, StepOut& so) {
const int n = sched_.block_begin(block, steps, (int)drafter_.window, drafter_.mask_token,
block_row_.data(), (int)block_row_.size(), /*on_card=*/true);
/* --draft-coupling: the draws before the query pass reads them (stage_couple_draws). */
if (mtp_couple_ && n > 0) RAD_TRY(stage_couple_draws(block_row_.data(), n));
/* The context pass runs even when nothing drafts: a mid-prompt sequence has hidden states the
* drafter will need, and skipping would leave a permanent hole in its cache. */
if (const RadBatch* cb = sched_.block_context(step)) RAD_TRY(block_pass(cb, "context", step));
@@ -515,6 +534,15 @@ int Engine::issue_block_drafter(int step, StepOut& so) {
RAD_TRY(block_pass(qb, "query", step));
RAD_TRY(advance_all(ADV_BTAKE, steps, true, n));
Rank& r0 = *ranks_[0];
if (mtp_couple_) {
/* Positions 1..steps of the drafting rows, on the stream of the slots below. */
const void* state = r0.ctx.buf_ptr(couple_state_buf_);
if (!state || steps > (int)drafter_.depth) return RAD_E_STATE;
const int64_t pitch = couple_rows_ * (int64_t)sizeof(RadMtpCoupleState);
RAD_TRY(rad_memcpy_2d_async(couple_readback_, pitch, state, pitch,
(int64_t)n * (int64_t)sizeof(RadMtpCoupleState), steps,
r0.ctx.stream()));
}
RAD_TRY(rad_memcpy_async(slots_host_, r0.step_slots,
(int64_t)cfg_.max_seqs * (int64_t)sizeof(StepSlot), r0.ctx.stream()));
block_rows_ = n;
@@ -557,7 +585,10 @@ int Engine::finish_block_drafter(const StepOut& so) {
/* As in finish_serial_drafter: the step issued ahead only carries a request whose answer
* is fixed (Scheduler::step_next_locked), so this agrees with the count it verifies. */
if (!r0.sampler.allows_speculation(block_row_[(size_t)i].seq)) continue;
sched_.set_draft(block_row_[(size_t)i].seq, &draft_out_[(size_t)i * (size_t)steps], steps);
const uint64_t seq = block_row_[(size_t)i].seq;
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)steps], steps);
if (mtp_couple_)
RAD_TRY(bind_couple(seq, i, steps, rc == RAD_OK, block_row_[(size_t)i].card));
}
return RAD_OK;
}