From 64c652f01ee20d0d9b4d42baeae63a5395b868db Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Sun, 25 Oct 2026 13:57:25 -0500 Subject: [PATCH] speculation: an MTP head's first draft reads its cache only up to where it sits The history pass has a row for every position a verify could commit; rows past the last committed one repeat it, and the run's last row is draft round 1's input. The card gave that pass the run's whole length as its context. Attention places row j of a q_len-row run at seqused - q_len + j and reads keys up to there, so after a verify that accepted acc < n_spec drafts, round 1 also read the n_spec - acc positions after its own: slots the head's layer had not written for this sequence (last step's speculative rounds, or whatever the block held before it was handed out). Its proposal then followed those bytes. Positions 2 and on were right: each round is one row whose context ends at its own position. This fits #221 (Qwen3.8-Flash-Next, position-1 acceptance 0.01-0.16 after a few short conversations sit in the prefix cache, healthy with --no-prefix-cache, after a restart and on long conversations): with the default lending cache the pool hands out blocks it lent the expert slab once cached blocks pass the live mark, and their unwritten slots hold expert bytes; above 2051 tokens the head's QSA path bounds each query by its own position. On the reporter's model (TP2, their flags, 32 distinct short chats on a fresh engine) 1.12.0 falls to p1 0.352 from request 9; with this change p1 is 0.953, as with --no-prefix-cache (0.955), replies identical. The context now ends at the last committed position. Earlier rows of the pass count lower than their positions, but nothing reads their output (the head goes on with draft_out_ids only), and every row's K and V still store at its own position's slot. Tests: advance_test (no row of a history pass or a round counts past its own position, and the run's last row sits exactly at it, for every acceptance and lead; fails before: the last row at 204 for a head at 201; and right after a 1-2 token prompt, where the rows nothing reads count before the sequence's start, every row still stores at its own position and the last sits at the committed one); ref_test (rows of a paged-attention run that sit before the context's start return zeros, not NaN, and the rows after them stay exact). Co-Authored-By: Claude Opus 5.5 --- core/sched/advance_impl.h | 22 +++++++++--- tests/advance_test.cpp | 71 ++++++++++++++++++++++++++++++++++++--- tests/ref_test.cpp | 26 ++++++++++++++ 3 files changed, 111 insertions(+), 8 deletions(-) diff --git a/core/sched/advance_impl.h b/core/sched/advance_impl.h index 774d352e..7b8ae007 100644 --- a/core/sched/advance_impl.h +++ b/core/sched/advance_impl.h @@ -40,8 +40,12 @@ RAD_ADV_FN bool adv_is_eog(const AdvanceArgs& a, int32_t t) { * reads the -1 padding the builder fills and faults the card on a negative block; * - a position at or past ring_pos lives in the ring, at block * ring_pos/block + ((p - ring_pos)/block) % ring_blocks, not at (p - first)/block, which runs - * off the table once the ring wraps. */ -RAD_ADV_FN void adv_groups(const AdvanceArgs& a, int i, int32_t k, int32_t base, int32_t n) { + * off the table once the ring wraps. + * + * `len`, when given, is the length's count from `base` in place of `n` (ADV_HIST: rows that + * repeat a position must not lengthen the context). */ +RAD_ADV_FN void adv_groups(const AdvanceArgs& a, int i, int32_t k, int32_t base, int32_t n, + int32_t len = -1) { const AdvGroup* G = reinterpret_cast(a.slab + a.grp_off); const int32_t* pos = adv_i32(a, a.pos_off); for (int g = 0; g < a.n_groups; ++g) { @@ -51,7 +55,7 @@ RAD_ADV_FN void adv_groups(const AdvanceArgs& a, int i, int32_t k, int32_t base, const int32_t rpos = e.ring ? adv_i32(a, e.rpos_off)[i] : 0; const int32_t tlen = e.ring ? adv_i32(a, e.tlen_off)[i] : 0; const bool ring = e.ring && tlen > 0; - int64_t used = (int64_t)base + n - first; + int64_t used = (int64_t)base + (len >= 0 ? len : n) - first; if (ring) { const int64_t span = (int64_t)tlen * e.block_size; if (used > span) used = span; @@ -162,7 +166,17 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) { out[k + j] = row0 + jj; } ctx[i] = base; - adv_groups(a, i, k, base, n); + /* The context ends at the last committed position, not at the run's end. Attention places + * row j of a q_len-row run at seqused - q_len + j and reads keys up to there (attn_paged's + * contract, libref/ref_attn.cpp). The rows past `last` sit at the last committed position + * but are counted as if at base + j, so with the run's whole length the last row -- round + * 1, the one row whose output goes on -- would also read the n_spec - acc positions after + * it, which the head has not written for this sequence: last step's speculative rounds, or + * whatever the block held before it was handed out (another request's K and V, or bytes + * the expert slab left on a recalled loan). Ending it there puts the last row where it + * is; earlier rows count lower than their positions, but nothing reads their output, and + * every row's K and V still store at the slot of its own position. */ + adv_groups(a, i, k, base, n, last + 1); return; } case ADV_ROUND: { diff --git a/tests/advance_test.cpp b/tests/advance_test.cpp index 55d373e7..93e38902 100644 --- a/tests/advance_test.cpp +++ b/tests/advance_test.cpp @@ -352,13 +352,76 @@ TEST(a_history_pass_keeps_the_hosts_rows_and_repeats_the_last_committed_one) { CHECK_EQ(s.slot(0, j), pos[j]); } CHECK_EQ(s.w[Slab::CTX], 100); - CHECK_EQ(s.used(0, 0), 105); + CHECK_EQ(s.used(0, 0), 104); /* ends at the last committed position, 103 */ +} + +/* Attention places row j of a q_len-row run at seqused - q_len + j and reads keys up to there + * (attn_paged's contract, libref/ref_attn.cpp; libr4d's decode kernel computes the same `pos`). + * So on every pass the card stages, the last row of a run -- round 1's input on a history pass, + * the only row whose output the head goes on with -- must sit exactly at its own position, and no + * row may count past its own: a key past it is a position the head's layer has not written for + * this sequence. A history pass that accepted acc < n_spec drafts used to put its last row + * n_spec - acc positions too far, and MTP round 1 read whatever those slots held. */ +TEST(no_draft_pass_row_reads_keys_past_its_own_position) { + const int n_spec = 3; + for (int aux = 0; aux <= 2; ++aux) + for (int acc = 0; acc <= n_spec; ++acc) { + Slab s(ADV_HIST); + const int n = aux + 1 + n_spec, head = 200 + aux + acc; + s.row(0, true, 0, n, 0, /*card=*/3, 0, 0, aux); + s.st[3].head = head; s.st[3].acc = acc; + s.run(); + const int32_t used = s.used(0, 0); + for (int j = 0; j < n; ++j) + CHECK(used - n + j <= s.w[Slab::POS + j]); + CHECK_EQ(s.w[Slab::POS + n - 1], head); + CHECK_EQ(used - n + (n - 1), head); + } + for (int round = 2; round <= n_spec; ++round) { + Slab s(ADV_ROUND); + s.a.round = round; + s.row(0, true, 0, 1, 0, /*card=*/1, 0, 0, 0); + s.st[1].head = 300; + s.run(); + CHECK_EQ(s.used(0, 0) - 1, s.w[Slab::POS]); + CHECK_EQ(s.w[Slab::POS], 300 + round - 1); + } +} + +/* The same pass right after a one- or two-token prompt, where the run is longer than the context + * it ends at (head + 1 < aux + 1 + n_spec): its first rows then count before position 0. Nothing + * reads their output and attention gives such a row zeros (op_attn_paged_rows_before_the_context + * in ref_test); what must hold is that every row still stores at a real position of its own + * table, the context is at least one key, and the last row sits at the committed position. */ +TEST(a_history_pass_right_after_a_tiny_prompt_stores_every_row_at_a_real_position) { + const int n_spec = 3; + int negative = 0; + for (int base = 1; base <= 2; ++base) /* the bonus token's position: a 1-2 token prompt */ + for (int aux = 0; aux <= 2; ++aux) + for (int acc = 0; acc <= n_spec; ++acc) { + Slab s(ADV_HIST, /*n_groups=*/1, /*block=*/4); + const int n = aux + 1 + n_spec, head = base + aux + acc; + s.row(0, true, 0, n, 0, /*card=*/2, 0, 0, aux); + s.st[2].head = head; s.st[2].acc = acc; + s.run(); + const int32_t used = s.used(0, 0); + CHECK_EQ(used, head + 1); + CHECK(used >= 1); + for (int j = 0; j < n; ++j) { + const int32_t p = s.w[Slab::POS + j]; + CHECK(p >= base && p <= head); + CHECK_EQ(s.slot(0, j), p); /* block b at entry b: slot == position */ + } + CHECK_EQ(used - n + (n - 1), head); + if (used - n < 0) ++negative; + } + CHECK(negative > 0); /* the cases the test is for were reached */ } /* What the card writes into a draft pass, for both head shapes. A verify at positions 101..105 * accepted two drafts: committed position 103, trunk token 7003; slot p is position p. A head - * with its own cache gets the verified run, tail repeated, context to its end; a cacheless head - * gets the committed position and a context ending there, every round. */ + * with its own cache gets the verified run, tail repeated, and a cacheless head the committed + * position; both get a context ending there, every round. */ TEST(the_card_stages_a_cacheless_head_at_the_committed_position) { auto slab = [](Slab& s, int n_rows, int round, bool shared, int shift) { s.a.round = round; @@ -382,7 +445,7 @@ TEST(the_card_stages_a_cacheless_head_at_the_committed_position) { CHECK_EQ(o.w[Slab::OUT + j], 40 + (j < 2 ? j : 2)); CHECK_EQ(o.slot(0, j), pos[j]); } - CHECK_EQ(o.used(0, 0), 106); /* the verify step's whole run */ + CHECK_EQ(o.used(0, 0), 104); /* the committed positions and no more */ CHECK_EQ(o.w[Slab::CTX], 101); } for (int shift = 0; shift < 2; ++shift) { diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 7b04ffba..73b7e1e5 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -3064,6 +3064,32 @@ TEST(op_attn_paged_ragged) { CHECK_NEAR(out.at(5), 1.0f / sum, 1e-6); } +/* A run longer than its context: four query rows over two keys, as an MTP head's history pass + * right after a one-token prompt is (core/sched/advance_impl.h ADV_HIST ends the context at the + * last committed position). Rows sit at seqused - q_len + j = -2, -1, 0, 1: the first two see no + * key and are zero, not NaN, and the last two are as if the run were two rows. */ +TEST(op_attn_paged_rows_before_the_context) { + const float sc = 1.0f / 1.41421356f; + T q(RAD_F32, {4, 1, 2}); + q.f({ 1, 0, 0, 1, 1, 0, 0, 1 }); + T kv(RAD_F32, {1, 1, 2, 4}); + kv.f({ 1, 0, 1, 0, + 0, 1, 0, 1 }); + T bt(RAD_I32, {1, 1}); bt.n({0}); + T su(RAD_I32, {1}); su.n({2}); + T out(RAD_F32, {4, 1, 2}); + A a; a.t(q).t(kv).t(bt).t(su).tnull().tnull().tnull().t(out) + .i("q_len", 4).i("head_dim", 2).i("gqa", 1).i("block_size", 2) + .i("causal", 1).i("window", 0).s("q_dtype", "f32").s("kv_dtype", "f32"); + CHECK_OK(run("attn_paged", a)); + for (int e = 0; e < 4; ++e) CHECK_EQ(out.at(e), 0.0f); + /* Row 2 at position 0 sees key 0; row 3 at position 1 sees both, q = [0,1]. */ + CHECK_NEAR(out.at(4), 1.0f, 1e-6); CHECK_NEAR(out.at(5), 0.0f, 1e-6); + const float w0 = std::exp(-sc), sum = w0 + 1.0f; + CHECK_NEAR(out.at(6), w0 / sum, 1e-6); + CHECK_NEAR(out.at(7), 1.0f / sum, 1e-6); +} + TEST(op_attn_dense) { const float sc = 1.0f / 1.41421356f; T q(RAD_F32, {2, 1, 2}); q.f({1, 0, 0, 1});