sched: --draft-suffix on, SuffixDecoding as a step selector on the MTP chain

After each commit the scheduler looks in the sequence's own prompt and output
for the latest earlier occurrence of its last --draft-suffix-min-match tokens;
the next step then verifies what followed it (up to --draft-suffix-max rows,
cut where the product of the follower frequencies falls under
--draft-suffix-min-score) in place of the head's draft. The verify window
becomes the larger of the two while the head keeps its own rounds. A sequence
whose history ended in a recurring pattern is not issued ahead, since the step
is built after the commit where the lookup can read the committed end.

It works with the fixed MTP chain, with --draft-depth adaptive and with
--draft-verify traversal. A suffix chain carries no coupled row
(Request::draft_suffix): the sampler binds none of the head's staged states to
its rows, so each row is the target's plain draw and the rows are leaves of the
traversal walk; mtp_rrs_test checks the law with a negative control that fails.
--draft-verify block is refused. The window is at most 7 rows on Flash-Next (the
delta-net fold's register window).

On Flash-Next IQR-V4 (TP2) on our branch, window 7 on chain 3 with coupling:
+1.4% overall, +2.9% on repetitive tasks, +11% on quoting, free chat unchanged;
beside adaptive depth +0.2% [-1.0, +1.5] overall, code +1.0%. Off by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 committed 2026-11-17 22:11:30 +01:00
1 parent e78d484e60
commit b8bca7885a
22 files changed
+1112 -9

No files matched your search

+329
View File
@@ -8528,3 +8528,332 @@ TEST(sim_burst_of_long_prompts) {
(double)N * (double)L / tt.back(), pct(0.5), pct(0.95), tt.back(),
dec_s > 0 ? (double)dec_tok / dec_s : 0.0, t);
}
/* ================================================================== --draft-suffix (SuffixDecoding as a step selector) */
namespace {
/* Commit the step in flight with `acc` accepted drafts and the final token `tok`. */
void commit_one(Fixture& f, int32_t acc, int32_t tok) {
int32_t acc_n = acc, tk = tok;
uint8_t eos = 0;
Scheduler::StepResult res;
res.n_seq = (int)f.sch.plan().e.size();
res.n_accepted = &acc_n;
res.token = &tk;
res.eos = &eos;
CHECK_OK(f.sch.commit(res));
}
/* A request whose prompt repeats 100..107, so the lookup continues the cycle: after the commit of
* 100 it proposes 101 102 103 with the longest match the index counts. */
uint64_t ngram_cycle_request(Fixture& f) {
auto r = std::make_unique<Request>();
r->id = f.next_id++;
r->max_tokens = 2000;
r->prompt.resize(160);
for (int i = 0; i < 160; ++i) r->prompt[(size_t)i] = 100 + i % 8;
const uint64_t a = r->id;
CHECK_OK(f.sch.add(std::move(r)));
return a;
}
} // namespace
namespace {
/* A serial MTP head that drafts 3 rounds under a verify window of 8 rows (--draft-suffix on). */
void suffix_setup(Fixture& f, int head = 3, int mx = 8, int min_match = 4, bool on = true) {
f.build(4096, 64);
f.cfg.n_spec = std::max(head, mx);
f.cfg.n_spec_head = on ? head : 0;
f.cfg.draft_suffix = on;
f.cfg.draft_suffix_min_match = min_match;
f.cfg.draft_suffix_max = mx;
f.cfg.draft_suffix_min_score = 0.3f;
if (!on) f.cfg.n_spec = head;
f.prog.drafter.kind = RAD_DRAFT_SERIAL;
f.prog.drafter.depth = f.cfg.n_spec;
}
const int32_t kHeadDraft[3] = { 21, 22, 23 };
} // namespace
TEST(the_suffix_index_proposes_the_latest_recurrence_and_cuts_by_path_score) {
using namespace rad;
/* (1 2 3) was followed by 10, 11, 10: 10 two times in three. The longest recurring suffix of the
* history, (2 3 10 1 2 3), was followed by 11 at its earlier occurrence, which is what is
* proposed, at the frequency of 11 after (1 2 3): 1/3. */
const std::vector<int32_t> v = { 1, 2, 3, 10, 1, 2, 3, 11, 1, 2, 3, 10, 1, 2, 3 };
TokenSeq s;
s.a = v.data(); s.na = (int64_t)v.size();
SuffixIndex ix;
int32_t out[16];
SuffixIndex::Out o = ix.propose(s, 7, 3, 8, 0.3f, out);
CHECK(o.n >= 1);
CHECK_EQ(out[0], 11);
CHECK(o.match >= 6);
CHECK_NEAR(o.score, 1.0 / 3.0, 1e-4);
/* a stricter score keeps nothing: the first token's own path score is 1/3 */
SuffixIndex ix2;
CHECK_EQ(ix2.propose(s, 7, 3, 8, 0.4f, out).n, 0);
/* a context that never occurred before proposes nothing */
const std::vector<int32_t> u = { 5, 6, 7, 8, 9, 40, 41, 42 };
TokenSeq su;
su.a = u.data(); su.na = (int64_t)u.size();
SuffixIndex ix3;
CHECK_EQ(ix3.propose(su, 8, 3, 8, 0.3f, out).n, 0);
/* it is incremental: the sequence grows and the proposal follows */
std::vector<int32_t> w = { 5, 6, 7, 8, 9, 40, 41, 42, 5, 6, 7 };
TokenSeq sw;
sw.a = w.data(); sw.na = (int64_t)w.size();
const SuffixIndex::Out ow = ix3.propose(sw, 8, 3, 8, 0.3f, out);
CHECK(ow.n >= 3);
CHECK_EQ(out[0], 8); CHECK_EQ(out[1], 9); CHECK_EQ(out[2], 40);
CHECK_NEAR(ow.score, 1.0, 1e-4); /* a pattern seen once, followed once: D = 1 */
}
TEST(a_recurring_history_selects_a_suffix_step_in_place_of_the_heads_draft) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = ngram_cycle_request(f);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(100);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
/* the verify window is the suffix's: 1 + 8 rows, the head's 3 drafts replaced */
CHECK_EQ(b->n_spec, 8);
CHECK_EQ(b->n_tok, 9);
const StepEntry& e = f.sch.plan().e[0];
CHECK(e.suffix);
CHECK(e.ngram);
CHECK_EQ(e.n_spec, 8);
CHECK(e.draft_parent == nullptr);
const Request* rq = f.sch.find(a);
REQUIRE(rq != nullptr);
/* no coupled row: the request marks zero model nodes, so the sampler's child list is empty */
CHECK(rq->draft_suffix);
const int32_t want[8] = { 101, 102, 103, 104, 105, 106, 107, 100 };
for (int j = 0; j < 8; ++j) CHECK_EQ(rq->draft[j], want[j]);
commit_one(f, 5, 900);
const SchedMetrics m = f.sch.metrics();
CHECK_EQ(m.suffix_steps, (int64_t)1);
CHECK_EQ(m.suffix_rows, (int64_t)8);
CHECK_EQ(m.suffix_accepted, (int64_t)5);
rq = f.sch.find(a);
REQUIRE(rq->output.size() >= 7);
for (int j = 0; j < 5; ++j) CHECK_EQ(rq->output[rq->output.size() - 6 + (size_t)j], want[j]);
CHECK_EQ(rq->output.back(), 900);
CHECK(!rq->draft_suffix); /* nothing carries over */
}
TEST(without_a_recurrence_or_with_the_flag_off_the_heads_draft_is_verified_as_before) {
{ /* no recurrence in a prompt of distinct tokens */
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = f.submit(160, 2000);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(7);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
CHECK_EQ(b->n_spec, 3);
CHECK(!f.sch.plan().e[0].suffix);
CHECK(!f.sch.plan().e[0].ngram);
CHECK(!f.sch.find(a)->draft_suffix);
for (int j = 0; j < 3; ++j) CHECK_EQ(f.sch.find(a)->draft[j], kHeadDraft[j]);
}
{ /* the flag off: a recurring history changes nothing */
Fixture f;
suffix_setup(f, 3, 8, 4, /*on=*/false);
CHECK_OK(f.start());
const uint64_t a = ngram_cycle_request(f);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(100);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
CHECK_EQ(b->n_spec, 3);
CHECK(!f.sch.plan().e[0].suffix);
CHECK(!f.sch.find(a)->draft_suffix);
commit_one(f, 3, 900);
CHECK_EQ(f.sch.metrics().suffix_steps, (int64_t)0);
CHECK_EQ(f.sch.metrics().suffix_held_ahead, (int64_t)0);
}
}
/* The head runs only its own 3 rounds after a suffix step, but its history pass covers every
* position the step verified (all 1 + 8), and the watermark moves to the commit, so the next
* step's pass owes only its own rows: the head's cache has no hole behind a suffix step. */
TEST(the_head_catches_up_on_every_position_a_suffix_step_verified) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = ngram_cycle_request(f);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(100);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
REQUIRE_EQ(b->n_spec, 8);
const int32_t ctx0 = f.sch.plan().e[0].ctx_len;
int rounds = 8; /* the engine passes the declared depth; the scheduler clamps it to the head's */
Scheduler::SerialRow rows[4];
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 1);
CHECK_EQ(rounds, 3);
const RadBatch* hb = f.sch.serial_batch(0, 1);
REQUIRE(hb != nullptr);
CHECK(hb->draft_pass < 0);
CHECK_EQ(hb->n_tok, 9); /* the bonus row and all 8 proposals' positions */
CHECK_EQ(hb->n_draft_out, 1);
CHECK_EQ(hb->draft_out_ids[0], 8);
CHECK_EQ(hb->ctx_lens[0], ctx0); /* from the step's first position */
CHECK(f.sch.serial_batch(1, 1) == nullptr);
for (int rd = 2; rd <= rounds; ++rd) REQUIRE(f.sch.serial_batch(rd, 1) != nullptr);
/* the verify took all 8: the watermark is the commit */
commit_one(f, 8, 900);
f.sch.serial_end(rounds);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
const RadBatch* b2 = f.sch.step(); /* 900 broke the cycle: the head's step */
REQUIRE(b2 != nullptr);
CHECK_EQ(b2->n_spec, 3);
CHECK(!f.sch.plan().e[0].suffix);
int rounds2 = 8;
CHECK_EQ(f.sch.serial_begin(&rounds2, rows, 4), 1);
CHECK_EQ(rounds2, 3);
const RadBatch* hb2 = f.sch.serial_batch(0, 1);
REQUIRE(hb2 != nullptr);
CHECK_EQ(hb2->n_tok, 4); /* its own 1 + 3 rows: nothing left owed */
CHECK_EQ(hb2->ctx_lens[0], ctx0 + 9);
f.sch.serial_end(rounds2);
}
/* A step issued ahead is made on the card from the head's drafts, before the previous commit: the
* lookup cannot run there. A sequence whose history ended in a recurring pattern at the last commit
* is not issued ahead (its next step is built after the commit, where the suffix selector reads the
* committed end); one whose history does not recur is, as before. */
TEST(a_sequence_in_a_recurring_pattern_is_not_issued_ahead) {
for (int hot = 0; hot < 2; ++hot) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = hot ? ngram_cycle_request(f) : f.submit(160, 2000);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(hot ? 100 : 7);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
REQUIRE(f.sch.step() != nullptr);
f.sch.find(a)->speculation_fixed = true;
f.sch.expect_draft(a, hot ? 8 : 3);
const RadBatch* nb = f.sch.step_next();
if (hot) {
CHECK(nb == nullptr);
CHECK_EQ(f.sch.metrics().suffix_held_ahead, (int64_t)1);
} else {
CHECK(nb != nullptr);
CHECK_EQ(f.sch.metrics().suffix_held_ahead, (int64_t)0);
}
}
}
/* Selection is a function of the committed text alone: the same history gives the same step whatever
* the head drafted, and a request that may not speculate (a constraining grammar) is never given a
* suffix chain. */
TEST(the_suffix_choice_reads_the_history_only_and_respects_the_speculation_gate) {
for (int variant = 0; variant < 3; ++variant) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = ngram_cycle_request(f);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(100);
if (variant == 2) f.sch.find(a)->may_speculate = false;
const int32_t other[3] = { 77, 78, 79 };
CHECK_OK(f.sch.set_draft(a, variant == 1 ? other : kHeadDraft, 3));
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
if (variant == 2) {
CHECK_EQ(b->n_spec, 0);
CHECK(!f.sch.plan().e[0].suffix);
} else {
CHECK_EQ(b->n_spec, 8);
CHECK(f.sch.plan().e[0].suffix);
CHECK_EQ(f.sch.find(a)->draft[0], 101);
}
}
}
/* ================================================================== --draft-suffix beside --draft-depth adaptive */
/* One step, two sequences: the one whose history recurs verifies its suffix chain (8 rows), the other
* the head's chain at the depth the adaptive rule gave it (set_draft's count, 2 of the head's 3
* rounds): the rows of the step differ per sequence, the window is the larger of the two. Only the
* suffix row carries the Request::draft_suffix mark; the adaptive row's coupled states stay bound. */
TEST(a_suffix_row_and_an_adaptive_depth_row_share_a_step) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = ngram_cycle_request(f);
const uint64_t b = f.submit(160, 2000);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(100);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
CHECK_OK(f.sch.set_draft(b, kHeadDraft, 2)); /* the adaptive chain stopped after two rounds */
const RadBatch* bt = f.sch.step();
REQUIRE(bt != nullptr);
const StepPlan& p = f.sch.plan();
REQUIRE_EQ(p.e.size(), (size_t)2);
size_t ia = 0, ib = 1;
if (p.e[0].seq != a) { ia = 1; ib = 0; }
CHECK(p.e[ia].suffix);
CHECK_EQ(p.e[ia].n_spec, 8);
CHECK(p.e[ia].req->draft_suffix);
CHECK(!p.e[ib].suffix);
CHECK_EQ(p.e[ib].n_spec, 2);
CHECK(!p.e[ib].req->draft_suffix);
CHECK_EQ(bt->n_spec, 8);
CHECK_EQ(bt->n_tok, 9 + 3);
for (int j = 0; j < 2; ++j) CHECK_EQ(f.sch.find(b)->draft[j], kHeadDraft[j]);
/* A accepts 5 of its 8, B both: each is counted for what it carried */
CHECK_OK(f.commit(900, 0, [&](const StepEntry& e) { return e.seq == a ? 5 : 2; }));
const SchedMetrics m = f.sch.metrics();
CHECK_EQ(m.suffix_steps, (int64_t)1);
CHECK_EQ(m.suffix_rows, (int64_t)8);
CHECK_EQ(m.suffix_accepted, (int64_t)5);
CHECK_EQ(m.draft_tokens, (int64_t)10);
CHECK_EQ(m.draft_accepted, (int64_t)7);
}
/* The host's order under the adaptive depth: sequences whose history is not in a pattern are issued
* ahead with their own row counts (expect_draft of the chain each drafted) in a suffix deployment's
* wider window; one in a pattern holds the whole step back and takes its suffix step after the commit. */
TEST(adaptive_depth_rows_are_issued_ahead_in_a_suffix_deployment_unless_a_sequence_is_in_a_pattern) {
for (int hot = 0; hot < 2; ++hot) {
Fixture f;
suffix_setup(f);
CHECK_OK(f.start());
const uint64_t a = hot ? ngram_cycle_request(f) : f.submit(160, 2000);
const uint64_t b = f.submit(160, 2000);
REQUIRE(f.sch.step() != nullptr);
f.commit_plain(hot ? 100 : 7);
CHECK_OK(f.sch.set_draft(a, kHeadDraft, 3));
CHECK_OK(f.sch.set_draft(b, kHeadDraft, 2));
REQUIRE(f.sch.step() != nullptr);
f.sch.find(a)->speculation_fixed = true;
f.sch.find(b)->speculation_fixed = true;
f.sch.expect_draft(a, 3);
f.sch.expect_draft(b, 2);
const RadBatch* nb = f.sch.step_next();
if (hot) {
CHECK(nb == nullptr);
} else {
REQUIRE(nb != nullptr);
std::vector<int> q;
for (int64_t i = 0; i < nb->n_seq; ++i) q.push_back((int)nb->q_lens[i]);
std::sort(q.begin(), q.end());
REQUIRE_EQ(q.size(), (size_t)2);
CHECK_EQ(q[0], 3); /* 1 + the 2 drafts of one chain */
CHECK_EQ(q[1], 4); /* 1 + the 3 of the other */
}
}
}