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,
+15 -15
View File
@@ -9192,7 +9192,7 @@ struct ArchCases {
qwen35_fp8::step, 3, {}, 0x7c3df775e4091829ull);
add("qwen35_fp8 + MTP, two ranks", [this, sc](RadBuilder* b) {
auto c = sc(2, 3); return qwen35_fp8::declare(b, q35.meta(), &c); },
nullptr, 3, { "ar_rmsnorm_quant_fp8" }, 0x7c11e3c8243c955full);
nullptr, 3, { "ar_rmsnorm_quant_fp8" }, 0x2c36db278c52b14dull);
add("qwen35_fp8 + DFlash2", [this, sc](RadBuilder* b) {
auto c = sc(1, 7); return qwen35_fp8::declare(b, q35df.meta(), &c); },
qwen35_fp8::step, 7, {}, 0xa980fd70dfca7f04ull);
@@ -9201,20 +9201,20 @@ struct ArchCases {
qwen35_w8a8::step, 7, {}, 0xb01f0bb2b9898c1dull);
add("qwen35_w8a8 + DFlash2, two ranks", [this, sc](RadBuilder* b) {
auto c = sc(2, 7); return w8a8_declare(b, q35w8df.meta(), &c); },
nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0xdc5540151b0d6684ull);
nullptr, 7, { "ar_rmsnorm_quant_fp8" }, 0xe1a076a88fee5f9eull);
add("qwen35moe_fp8 + MTP", [this, sc](RadBuilder* b) {
auto c = sc(1, 3); return qwen35moe_fp8::declare(b, q35moe.meta(), &c); },
qwen35moe_fp8::step, 3, {}, 0xb5aaea840d6bf6c2ull);
add("qwen4exp_fp8", [this](RadBuilder* b) {
auto c = q4_ctx(0, 1); return qwen4exp_fp8::declare(b, &q4, &c); },
qwen4exp_fp8::step, 3, {}, 0x43048d3448778280ull);
qwen4exp_fp8::step, 3, {}, 0x4a12ebade34d845bull);
add("qwen4exp_fp8, two ranks", [this](RadBuilder* b) {
auto c = q4_ctx(0, 2); return qwen4exp_fp8::declare(b, &q4, &c); },
nullptr, 3, { "ar_hc_write" }, 0x43d3be2b6d76e470ull);
nullptr, 3, { "ar_hc_write" }, 0xeb856cde42ac3e3bull);
add("qwen4exp_fp8, four-bit experts", [this](RadBuilder* b) {
b->encs = { { "ffn_gate_up_exps", w4 }, { "ffn_down_exps", w4 } };
auto c = q4_ctx(0, 1); return qwen4exp_fp8::declare(b, &q4, &c); },
qwen4exp_fp8::step, 3, {}, 0x08d082fcbef37889ull);
qwen4exp_fp8::step, 3, {}, 0xa9458e439054bfa0ull);
/* The cache dtype stated rather than left to the plugin's default. The streamed store and
* the prologue that folds it are declared only where the vocabulary has them, and both are
* written at the KV group's dtype -- bf16 being the copy-only one, and it is what
@@ -9226,7 +9226,7 @@ struct ArchCases {
auto c = q4_ctx(0, 1);
c.kv_dtype = RAD_BF16;
return qwen4exp_fp8::declare(b, &q4, &c); },
qwen4exp_fp8::step, 3, {}, 0x0000000000000000ull);
qwen4exp_fp8::step, 3, {}, 0x4a12ebade34d845bull);
add("llama_fp8 + DSpark", [this](RadBuilder* b) {
auto c = dspark_ctx(0, 1); return llama_fp8::declare(b, lla.meta(), &c); },
llama_fp8::step, 7, {}, 0xdcf36a1d31aba73cull);
@@ -9942,15 +9942,15 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) {
uint64_t graph, steps;
};
const Case cases[] = {
{ "qwen4exp_fp8", 1, 0, 0x23ec76c443e2cb6full, 0x26a9e2c23cf85696ull },
{ "qwen4exp_fp8", 2, 0, 0x88290524cfd7714full, 0x889c6b36c014609full },
{ "qwen4exp_fp8", 2, 1, 0xa4268bda72739be8ull, 0x959ef6b7c6f57d8bull },
{ "qwen4exp_fp8", 4, 0, 0xaa5abead708b106full, 0xfbe74f2b9d7020bfull },
{ "qwen4exp_fp8", 4, 3, 0x9ffc4cf1b074a153ull, 0x36df57cac613c6cfull },
{ "qwen35_fp8", 1, 0, 0xcfc82ca915ec549dull, 0x4d6fca823d1e97efull },
{ "qwen35_fp8", 2, 1, 0xcaf28466af9e917dull, 0x0bcf5833fce11a86ull },
{ "qwen35moe_fp8", 1, 0, 0x7fa7b015e1bc8873ull, 0x3fcf71676e558d79ull },
{ "qwen35moe_fp8", 2, 0, 0xf60c9508dd197370ull, 0x05e0778848bf416cull },
{ "qwen4exp_fp8", 1, 0, 0xc38f2b3063262a80ull, 0x26a9e2c23cf85696ull },
{ "qwen4exp_fp8", 2, 0, 0xab53806937d83c1aull, 0x889c6b36c014609full },
{ "qwen4exp_fp8", 2, 1, 0x983f38c3a7ee5a29ull, 0x959ef6b7c6f57d8bull },
{ "qwen4exp_fp8", 4, 0, 0x780814049d36bebcull, 0xfbe74f2b9d7020bfull },
{ "qwen4exp_fp8", 4, 3, 0x0723ed824fef82d4ull, 0x36df57cac613c6cfull },
{ "qwen35_fp8", 1, 0, 0xf5ea9e1795b08134ull, 0x4d6fca823d1e97efull },
{ "qwen35_fp8", 2, 1, 0xb99b0cc0002db22cull, 0x0bcf5833fce11a86ull },
{ "qwen35moe_fp8", 1, 0, 0x3979c928377bd5fcull, 0x3fcf71676e558d79ull },
{ "qwen35moe_fp8", 2, 0, 0x5ef9a8b68cb03cf1ull, 0x05e0778848bf416cull },
};
for (const Case& cs : cases) {
RadBuilder b;