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

+47
View File
@@ -98,6 +98,7 @@
#include "rad_arch.h"
#include "rad_fp8.h"
#include "rad_spechub.h" /* RAD_MTP_COUPLE_*: the coupling wire (--draft-coupling) */
namespace rad {
namespace arch {
@@ -135,6 +136,10 @@ struct Dflash2 {
* architecture that does not call rad_drafter_short_block leaves it off and its graph as
* it was. */
int short_block = 0;
/* --draft-coupling: the temperature of the draft law Q the coupled walk samples from
* (dflash_couple_propose's `q_temp`). Any value keeps the output exact; it only moves
* acceptance, sum min(P, Q). */
double q_temp = 1.0;
int64_t steps() const { return block - 1; }
int64_t ng(int64_t n_embd) const { return group ? n_embd / group : 0; }
/* The kernel projection emits [2, taps, NG] per token (prepare's set, then finish's);
@@ -190,6 +195,9 @@ struct Dflash2 {
rad_buf dpair = 0; /* [srows, sel_topk, 2] f32 */
rad_buf dgath = 0; /* [srows, world * sel_topk, 2] f32 */
rad_buf tokens = 0; /* [max_seqs, steps] i32 the draft; the engine reads it by name */
/* --draft-coupling: [max_seqs, DRAW_WORDS] draws the engine stages, and [steps, max_seqs,
* STATE_WORDS] states it reads back (abi/rad_spechub.h). Both or neither. */
rad_buf couple_draws = 0, couple_state = 0;
};
/* Whether the 2-bit draft head is vocab-sharded (rad_arch.h: vocab_head_sharded), so whoever
@@ -296,6 +304,9 @@ struct Dflash2 {
static constexpr int64_t kRerankR = 32;
int64_t top_w = 0; /* the coarse top-K's width: kRerankR or sel_topk */
rad_op op_hp = 0, op_sel = 0;
/* --draft-coupling: the sampled walk after the greedy one (dflash_couple_propose), for the
* drafter's block and its short one. 0 when the coupling is off. */
rad_op op_couple = 0, op_couple_lo = 0;
/* The short block: kShortBlock rows (depth 3, the window that pays at many sequences), when
* the drafter's own block is longer (rad_drafter_short_block). */
static constexpr int64_t kShortBlock = 4;
@@ -949,6 +960,35 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
RAD_WEIGHTS(w_pred, w_succ)),
{w.cand, w.unary, w.hp}, {w.tokens});
/* --draft-coupling: the walk again, sampled (abi/rad_spechub.h, rad_dflash_couple_step), over
* the same lattice right after the greedy walk. A greedy request's row keeps the selector's
* tokens; a sampled one gets x ~ Q at every position and the Q it was drawn from, which the
* engine reads back and binds to the verify. The tokens stay global ids, so the engine's take
* (ADV_BTAKE) is unchanged. */
if (w.couple_draws || w.couple_state) {
if (!w.couple_draws || !w.couple_state || cfg.steps() > RAD_MTP_COUPLE_MAX_ROUNDS ||
cfg.sel_topk > RAD_DFLASH_COUPLE_MAX_K)
return RAD_E_UNSUPPORTED;
auto couple = [&](int64_t steps, int64_t stride) {
return rw(b, RAD_OP(b, "dflash_couple_propose",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", steps),
RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank),
RAD_INT("n_vocab", g.n_vocab_all),
RAD_INT("anchor_stride", stride),
RAD_INT("max_rounds", cfg.steps()),
RAD_F64("q_temp", cfg.q_temp)),
RAD_WEIGHTS(w_pred, w_succ)),
{w.cand, w.unary, w.hp, w.couple_draws, w.tokens},
{w.tokens, w.couple_state});
};
op_couple = couple(cfg.steps(), cfg.block);
if (!op_couple) return RAD_E_INVAL;
if (block_lo) {
op_couple_lo = couple(block_lo - 1, block_lo);
if (!op_couple_lo) return RAD_E_INVAL;
}
}
/* The reads beside the all-reduces, after every weight they read is declared. */
if (op_ar && cfg.prefetch > 0)
for (int64_t l = 0; l < g.n_layer; ++l) {
@@ -1161,6 +1201,13 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
bcol(w.unary, 0, cfg.sel_topk, S), brows(w.hp, S),
praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred), RAD_W(w_succ),
brows(w.tokens, batch->n_seq));
if (op_couple)
RAD_ISSUE_N(c, lo ? op_couple_lo : op_couple, batch->n_seq,
bcol(w.cand, 0, cfg.sel_topk, S), bcol(w.unary, 0, cfg.sel_topk, S),
brows(w.hp, S), praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred),
RAD_W(w_succ), praw(batch->seq_ids, RAD_I32, batch->n_seq),
brows(w.couple_draws, batch->n_seq), brows(w.tokens, batch->n_seq),
RAD_B(w.couple_state));
}
} /* namespace arch */