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

+195 -1
View File
@@ -1,4 +1,4 @@
/* plugin/spechub/spechub.hip -- the device launches for the two coupling ops.
/* plugin/spechub/spechub.hip -- the device launches for the coupling ops.
*
* The propose kernel is one thread a row over abi/rad_spechub.h's helpers. The verify is one
* workgroup a row: the masses are computed in parallel, and the sums and walks run on one thread
@@ -152,8 +152,202 @@ __global__ void couple_verify(const int32_t* idx, const float* values, const uin
c.token[row * c.ld[4]] = out;
}
/* DFlash2's coupled walk: one workgroup a sequence. Per step the workgroup forms the
* candidate-independent half `sa` of the edge, then thread (c, j) computes part j of candidate c's
* edge (rad_dflash_edge_part, the host's serial fmaf chain over the same slice), and thread 0 adds
* the parts in order, takes the step (rad_dflash_couple_step) and publishes the token as the next
* predecessor. The arithmetic and its order are the host reference's, so tokens and states match
* rad_ref_dflash_couple bit for bit. */
constexpr int kDfThreads = RAD_DFLASH_COUPLE_MAX_K * RAD_DFLASH_EDGE_PARTS; /* 256 */
__global__ __launch_bounds__(kDfThreads) void dflash_couple(
const int32_t* cand, const float* unary, const uint16_t* hp, const int32_t* anchor,
const uint16_t* pred, const uint16_t* succ, const int32_t* card, const uint32_t* draw,
int32_t* tokens, uint32_t* state, int64_t steps, int64_t K, int64_t R, int64_t vocab,
int64_t astr, int64_t ld_cand, int64_t ld_unary, int64_t ld_hp, int64_t ld_draw,
int64_t ld_tok, int64_t st_round, int64_t st_row, float q_temp) {
__shared__ float sa[RAD_DFLASH_COUPLE_MAX_R];
__shared__ float part[kDfThreads];
__shared__ float sc[RAD_DFLASH_COUPLE_MAX_K];
__shared__ int32_t sid[RAD_DFLASH_COUPLE_MAX_K];
__shared__ int32_t spid;
/* The step's Q in the making: its live (id, score / q_temp) in selection order, their count
* (-1: no Q), the top score, the FP64 weights, their sum and the normalised q. */
__shared__ int32_t lid[8], slot_id[8];
__shared__ float lv[8], slot_v[8];
__shared__ int sn;
__shared__ float stop;
__shared__ double w[8], wq[8], ssum;
__shared__ RadMtpCoupleState sst;
const int64_t s = blockIdx.x;
const int tid = threadIdx.x;
const RadMtpCoupleDraw d = *reinterpret_cast<const RadMtpCoupleDraw*>(draw + s * ld_draw);
int32_t* tok = tokens + s * ld_tok;
uint32_t* st0 = state + s * st_row;
if (d.mode != 1) {
/* A greedy request: dflash_select's walk stands, and each round records it (status 0). */
if (tid == 0)
for (int64_t l = 0; l < steps; ++l)
(void)rad_dflash_couple_step(sc, sid, (int)K, q_temp, d, false, (int)l + 1, vocab,
tok[l],
reinterpret_cast<RadMtpCoupleState*>(st0 + l * st_round));
return;
}
const bool card_ok = d.card == card[s];
if (tid == 0) spid = anchor[s * astr];
__syncthreads();
const int c = tid / RAD_DFLASH_EDGE_PARTS, j = tid % RAD_DFLASH_EDGE_PARTS;
for (int64_t l = 0; l < steps; ++l) {
const int64_t row = s * steps + l;
const int32_t pid = spid;
const bool ok = pid >= 0 && pid < vocab;
for (int64_t r = tid; r < R; r += kDfThreads)
sa[r] = ok ? rad_dflash_bf2f(pred[(int64_t)pid * R + r]) *
(hp ? rad_dflash_bf2f(hp[row * ld_hp + r]) : 1.0f)
: 0.0f;
__syncthreads();
if (c < K) {
const int32_t id = cand[row * ld_cand + c];
float pv = 0.0f;
if (id >= 0 && id < vocab) {
const uint16_t* srow = succ + (int64_t)id * R;
if (R == 32 * RAD_DFLASH_EDGE_PARTS) {
/* DFlash2's rank 256: the slice's 32 values in four 16-byte loads issued
* together, then rad_dflash_edge_part's fmaf chain over them in its order. A
* load inside the chain would wait out one memory latency per element. */
const uint4* src = reinterpret_cast<const uint4*>(srow + j * 32);
uint4 v[4];
#pragma unroll
for (int i = 0; i < 4; ++i) v[i] = src[i];
const uint16_t* h = reinterpret_cast<const uint16_t*>(v);
const float* a = sa + j * 32;
#pragma unroll
for (int r = 0; r < 32; ++r) pv = fmaf(a[r], rad_dflash_bf2f(h[r]), pv);
} else {
pv = rad_dflash_edge_part(sa, srow, R, j);
}
}
part[tid] = pv;
if (j == 0) sid[c] = id;
}
__syncthreads();
/* Each candidate's score from its parts, in the host's order. */
if (tid < K) {
float acc = 0.0f;
if (sid[tid] >= 0 && sid[tid] < vocab)
for (int q = 0; q < RAD_DFLASH_EDGE_PARTS; ++q)
acc += part[tid * RAD_DFLASH_EDGE_PARTS + q];
sc[tid] = unary[row * ld_unary + tid] + acc;
}
if (tid < 8) slot_id[tid] = -2;
__syncthreads();
RadMtpCoupleState* so = reinterpret_cast<RadMtpCoupleState*>(st0 + l * st_round);
/* rad_dflash_couple_step, spread over the workgroup: one lane walking the selection and
* build_q's FP64 work serially waits out an LDS or FP64 latency per element. Each operation
* and its order are the helper's, so the bits are too.
*
* The eight: candidate k's place is the number of eligible candidates ahead of it (a higher
* score, or an equal one at a lower index), which is exactly the order the helper's repeated
* first-maximum scan takes them in. */
if (tid < K) {
const int32_t id = sid[tid];
const float v = sc[tid];
if (id >= 0 && id < vocab && v == v) {
int rank = 0;
for (int k = 0; k < K; ++k) {
const int32_t ik = sid[k];
const float vk = sc[k];
rank += (ik >= 0 && ik < vocab && vk == vk && (vk > v || (vk == v && k < tid)));
}
if (rank < 8) { slot_id[rank] = id; slot_v[rank] = v / q_temp; }
}
}
__syncthreads();
if (tid == 0) {
sn = -1;
if (card_ok) {
int live = 0;
bool bad = false;
for (int n = 0; n < 8 && slot_id[n] != -2; ++n) {
const float v = slot_v[n];
/* build_q: +inf refuses the whole Q, -inf carries no mass, a repeat refuses. */
if (v == INFINITY) { bad = true; break; }
if (v == -INFINITY) continue;
for (int i = 0; i < live; ++i) bad = bad || lid[i] == slot_id[n];
if (bad) break;
lid[live] = slot_id[n];
lv[live++] = v;
}
if (!bad && live > 0) {
float top = lv[0];
for (int i = 1; i < live; ++i)
if (lv[i] > top) top = lv[i];
stop = top;
sn = live;
}
}
}
__syncthreads();
if (tid < 8 && tid < sn) w[tid] = ::exp((double)lv[tid] - stop);
__syncthreads();
if (tid == 0 && sn > 0) {
double sum = 0;
for (int i = 0; i < sn; ++i) sum += w[i];
ssum = sum;
}
__syncthreads();
if (tid < 8 && tid < sn) wq[tid] = w[tid] / ssum;
__syncthreads();
/* The state is built in LDS: a private struct indexed by a runtime count lives in scratch,
* and every access to it is a memory round trip. */
if (tid == 0) {
RadMtpCoupleState& s = sst;
s.draw = d;
s.pos = 0;
s.round = (int)l + 1;
s.status = -1;
s.count = 0;
for (int i = 0; i < 8; ++i) { s.ids[i] = -1; s.q[i] = 0; }
for (int i = 0; i < sn; ++i)
if (wq[i] > 0) { s.ids[s.count] = lid[i]; s.q[s.count++] = wq[i]; }
if (s.count > 0) {
const double u = rad_spechub_uniform(d.seed, d.base,
RAD_MTP_COUPLE_KEY_DRAFT + (uint64_t)(l + 1));
s.x = s.ids[rad_mtp_couple_pick(s, u)];
s.status = 1;
} else {
s.count = 0;
for (int i = 0; i < 8; ++i) { s.ids[i] = -1; s.q[i] = 0; }
int best = 0;
float bv = -INFINITY;
for (int k = 0; k < K; ++k)
if (sc[k] > bv) { bv = sc[k]; best = k; }
s.x = sid[best];
}
tok[l] = s.x;
spid = s.x;
}
__syncthreads();
/* One word a lane out to the state plane. */
if (tid < (int)RAD_MTP_COUPLE_STATE_WORDS)
reinterpret_cast<uint32_t*>(so)[tid] = reinterpret_cast<const uint32_t*>(&sst)[tid];
/* The next step reads spid and overwrites sa, part and the Q scratch (the copy above reads
* sst, which the next step writes only after its own barriers). */
}
}
} /* namespace */
int rad_mine_dflash_couple_launch(const RadArgs* a, RadStream stream) {
RadDflashCoupleCall c{};
const int rc = rad_dflash_couple_args(a, &c);
if (rc != RAD_OK) return rc;
return R4D_EMIT(stream, dflash_couple, dim3((unsigned)c.M), dim3(kDfThreads), 0,
c.cand, c.unary, c.hp, c.anchor, c.pred, c.succ, c.card, c.draw, c.tokens,
c.state, c.steps, c.K, c.R, c.vocab, c.anchor_stride, c.ld_cand, c.ld_unary,
c.ld_hp, c.ld_draw, c.ld_tok, c.st_round, c.st_row, c.q_temp);
}
int rad_mine_mtp_couple_propose_launch(const RadArgs* a, RadStream stream) {
RadMtpCoupleProposeCall c{};
const int rc = rad_mtp_couple_propose_args(a, &c);