kv streaming: a speculative verify pass must not take the dense path on a ring

TENTATIVE FIX for a silent wrong-output bug: with --kv-resident N and
--num-speculative-tokens > 0, any prompt under qsa_exact_to (~2051 tokens)
streams digit spam at 65-140 tok/s instead of text, while the same prompt with
speculation off is correct. Measured, and traced:

  * the attention chooses "sparse (resolve) or dense (read the block table)"
    with `T == batch->n_seq || qsa_exact_to <= 0 || reach > qsa_exact_to`
    (arch/common/rad_block_attn_gated_fp8.h). `T == n_seq` is the decode-step
    test, and a verify step is n_seq x (1 + n_spec) rows, so it is false.
  * short context, so `reach <= qsa_exact_to` is also false: the pass takes the
    DENSE branch, which reads a block id straight out of the block table as a
    pool address. That is right for a staged pass (identity view) and
    meaningless on the ring, where the pool is a window of slots.
  * the dense branch's only guard, kv_stage_layer(), is a no-op on the ring: it
    returns 0 when the binding has no stage, and the call discards it.
  * so the verify pass reads ring stripes holding another layer's blocks. Fluent
    garbage, never a fault -- and the drafter's own rounds take the sparse path,
    so acceptance still looks plausible.

The dense path is now refused whenever the group is ring-addressed -- a stream
whose ring is smaller than its id space and whose pass is not staged. A stream
whose ring covers the context keeps the dense path, which is correct there
(ids and slots coincide); the arch test that pins "below qsa_exact_to is dense"
still passes for that shape, which is how the predicate was narrowed.

Verified on 2x16 GB gfx1201, qwen3.8-next-flash-fp8-iq4r-moe, --kv-resident
32768 --num-speculative-tokens 3 --max-num-seqs 2:
  63-token prompt   coherent, 63-78 tok/s (was digit spam)
  4 923-token prompt coherent, 67 tok/s (unchanged)
  arch_test 181 cases, mem_test 184, sched_test 142, kv_window_test 8,
  p4b_kv_ring_test 4 -- all green.

Known gaps, deliberately left for the follow-up: no byte-exact spec-on vs
spec-off gate has been run (only read-back quality), GLM-5.3's MLA path is
untested, and the draft window crossing a page boundary is untested.
This commit is contained in:
C_Tamas committed 2026-10-25 17:40:49 +07:00
1 parent 36b131f039
commit ac8b3a5017
2 files changed
+44 -18

No files matched your search

+29 -3
View File
@@ -758,8 +758,23 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
* `q_len` (rows = max_seqs * q_len_max * heads); more rows are refused as RAD_E_SCRATCH at
* issue. Rows of q_len 1 up to the declared max_seqs always fit. */
const int64_t reach = (int64_t)batch->max_ctx_len + batch->max_q_len;
/* A streamed group whose ring is *smaller than its id space* must never take the dense path:
* there a block id is not a pool address, so the attention would read ring slots as pages
* (fluent wrong output, not a fault). A stream whose ring covers the context is the ordinary
* resident plan -- ids and slots coincide, and the dense path is exactly right.
*
* Probe the stage -- a no-op unless this pass is staged -- because the choice cannot be made
* from the row shape alone: with speculation T = n_seq x (1 + n_spec), so `T == n_seq` is false
* and a context under qsa_exact_to would otherwise fall through to the dense branch
* (docs/KV-STREAMING.md §5.2: `T == n_seq` is about a *decode step*, not about every pass that
* carries more rows). */
const RadKVStream* st = kvb ? kvb->stream : nullptr;
const int64_t st_blocks = (st && st->block_bytes > 0) ? st->host_layer_stride / st->block_bytes : 0;
const bool ring_addressed = st && st->slots > 0 && st_blocks > 0 && st->slots < st_blocks;
const bool ring_bound = ring_addressed && !kv_stage_layer(c, kv, kvb, layer);
const bool sparse = qsa_sel && qsa_sequ &&
(T == batch->n_seq || qsa_exact_to <= 0 || reach > qsa_exact_to);
(ring_bound || T == batch->n_seq || qsa_exact_to <= 0 ||
reach > qsa_exact_to);
const bool gated = sparse && op_attn_gq;
/* Make this launch's rows resident and rewrite their `sel` entries before the attention reads
* them. The store above must come first: a block this step writes may not be in the ring yet,
@@ -860,8 +875,19 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
/* Below qsa_exact_to the dense path reads a block id straight out of the block table and
* never resolves. A staged pass addresses blocks by their id, so this layer must be staged
* here too or a short prompt on a small ring would read slots out of an unstaged pool
* (docs/KV-STREAMING.md §3.5). A no-op for a group with no stage view. */
if (kvb && kvb->stream) (void)kv_stage_layer(c, kv, kvb, layer);
* (docs/KV-STREAMING.md §3.5). A no-op for a group with no stage view.
*
* A ring-bound pass has no such addressing: if we are here with a streamed group and no
* stage, there is no `sel` to resolve either, and issuing the dense attention would read
* ring slots as block ids. Fail the step by name rather than serve the result. */
if (ring_bound) {
rad_step_fail(c, "kv streaming: this pass is on the streamed group's ring, so a block "
"id is not a pool address, but it has no selection to resolve into "
"ring slots; the attention cannot read it. Report this: a decode pass "
"wider than one row a sequence must reach `resolve_sel`");
} else if (kvb && kvb->stream) {
(void)kv_stage_layer(c, kv, kvb, layer);
}
RAD_ISSUE_N(c, op_attn, batch->max_q_len,
brows(w.q, T), kv_cache(kv, layer),
praw2(table, RAD_I32, batch->n_seq,