core/sched: a prefix hit resumes on the cold run's chunk grid, so it recomputes the cold run's own chunks

A hit recomputes the tokens past where it resumes. Kernels may round a row differently at another
chunk length (spec §17: the fp8 GEMM's split-K count is chosen from M), so a hit that recomputes
its tail in other chunks than a cold prefill of the same prompt can flip a near-tie in greedy text.
On the DeepSeek-V4.1 daily (TP4) a 24k-token prompt that hit a 16k one's tail snapshot resumed at
15856, ran a 528-token chunk to the next boundary and answered differently from its cold run.

--prefix-resume exact, the default:

- ChunkGeometry carries the grid an idle server's cold prefill chunks on: --max-num-batched-tokens
  rounded down to the quantum, or the checkpoint interval where chunks split at it and it is the
  smaller, times the span from the second interval on.
- plan_chunk writes checkpoints only on that grid. A prompt's tail snapshot is taken where its last
  chunk starts (sliding-window snapshots keep their one-block margin) instead of at its last
  quantum boundary. The chunk already ends there, so a long cold prompt no longer runs a short
  extra step at its end.
- A checkpoint interval past the chunk must be a multiple of it; otherwise it is rounded up, and
  startup says so.
- admit_one steps a hit with no snapshot to resume from (full attention) back to the grid. A
  request's own checkpoint (Request::checkpoint_at, /v1/systemone's shared head) is honored where
  it falls.

--prefix-resume nearest keeps the previous rule.

The cost is re-prefill: an exact repeat runs the cold run's last chunk again, and an agent's next
turn resumes where the previous prompt's last chunk started, about half a chunk more than nearest
(2048 tokens at a 4096-token budget). The property holds on an idle server; a prefill that shares
its steps with decode rows or other requests (--busy-prefill-chunk, --prefill-interleave) is cut
differently, as batching already changes greedy text.

Tests: sched_test covers the grid for six geometries (GDN hybrid, interval past the budget, span,
periodic and tip-only window snapshots, full attention), the interval rounding, the tail snapshot
at the last chunk start, admission stepping back to the grid, and a property over prompt lengths:
every checkpoint is a chunk start of the cold run of its prompt and of any longer prompt it
prefixes, and a hit from it cuts the cold run's chunks. config_test covers the flag.
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-28 12:50:54 +01:00
1 parent f43929ccb8
commit 662b7720b5
9 files changed
+449 -4

No files matched your search

+246
View File
@@ -166,6 +166,10 @@ struct Fixture {
cfg.max_ctx = max_ctx;
cfg.checkpoint_interval = 256;
cfg.prefix_cache = true;
/* The tests below were written for the nearest resume rule (a tail snapshot at the last
* quantum boundary, a hit at any snapshot or block); the exact rule's own tests set
* prefix_exact (--prefix-resume exact, the default). */
cfg.prefix_exact = false;
/* Config::n_spec defaults to -1 (`auto`), which engine_bringup resolves; this fixture
* skips that, and DraftController::configure refuses a negative depth. Speculation tests
* set their own depth. */
@@ -795,6 +799,248 @@ TEST(a_span_needs_an_interval_grid) {
CHECK_EQ((int64_t)f.sch.plan().e.size(), 2ll);
}
/* ================================================================== exact resume */
/* --prefix-resume exact (the default): a hit resumes only where an idle server's cold prefill of
* the same prompt starts a chunk, and a prompt's tail snapshot is where its last chunk starts. A
* kernel may round a row differently at another chunk length (spec §17: the split-K count is
* chosen from M), so a hit is bit-identical to a cold run only when it recomputes the cold run's
* own chunks. */
/* The chunk ends and checkpoints an idle server plans for an `L`-token prompt from `from`. */
struct Cut { std::vector<int64_t> ends, ckpts; };
static Cut cut_from(const Scheduler& s, int64_t from, int64_t L, int64_t budget) {
Cut c;
for (int64_t pos = from; pos < L;) {
int64_t ck = -1;
const int64_t n = s.plan_chunk(pos, L, budget, &ck);
if (n <= 0) break;
pos += n;
c.ends.push_back(pos);
if (ck > 0) c.ckpts.push_back(ck);
}
return c;
}
static std::vector<int64_t> ends_after(const Cut& c, int64_t from) {
std::vector<int64_t> v;
for (int64_t e : c.ends) if (e > from) v.push_back(e);
return v;
}
/* Which geometry: 0 the GDN hybrid at interval 256 / budget 512 (chunks split at the interval);
* 1 interval 1024 past a 512 budget; 2 a span of 2; 3 a sliding-window model with periodic
* snapshots; 4 the same, tip-only (no interval split); 5 full attention alone (no snapshots). */
static void exact_fixture(Fixture& f, int kind) {
const bool window = kind == 3 || kind == 4, attn_only = kind == 5;
f.build(4096, 64, 512, 8192, window, !attn_only);
if (window) {
/* As the window tests above: drop the GDN hybrid's linear and conv groups and its scan. */
f.prog.kv_groups.erase(f.prog.kv_groups.begin() + 1, f.prog.kv_groups.begin() + 3);
f.prog.ops.resize(1);
f.prog.buffers.resize(1);
f.kv.setup(f.prog.kv_groups, f.cfg.max_ctx, 4096, 64);
f.pc.groups = { 0 };
if (kind == 4) f.cfg.checkpoint_policy = "tip-only";
}
if (kind == 1) f.cfg.checkpoint_interval = 1024;
if (kind == 2) f.cfg.prefill_span = 2;
f.cfg.prefix_exact = true;
}
TEST(exact_resume_grid_is_the_chunk_an_idle_prefill_cuts) {
struct Want { int kind; int64_t interval, grid, origin; };
for (const Want w : { Want{ 0, 256, 256, 0 }, Want{ 1, 1024, 512, 0 }, Want{ 2, 256, 512, 256 },
Want{ 3, 256, 256, 0 }, Want{ 4, 256, 512, 0 }, Want{ 5, 0, 512, 0 } }) {
Fixture f;
exact_fixture(f, w.kind);
REQUIRE_EQ(f.start(), RAD_OK);
const ChunkGeometry& g = f.sch.geometry();
CHECK(g.exact);
CHECK_EQ(g.checkpoint_interval, w.interval);
CHECK_EQ(g.grid, w.grid);
CHECK_EQ(g.grid_origin, w.origin);
}
/* Nearest, or no prefix cache: no grid, every position its own floor. */
for (int off = 0; off < 2; ++off) {
Fixture f;
exact_fixture(f, 0);
if (off) f.cfg.prefix_cache = false; else f.cfg.prefix_exact = false;
REQUIRE_EQ(f.start(), RAD_OK);
CHECK(!f.sch.geometry().exact);
CHECK_EQ(f.sch.geometry().grid_floor(300), 300);
}
Fixture f;
exact_fixture(f, 2);
REQUIRE_EQ(f.start(), RAD_OK);
const ChunkGeometry& g = f.sch.geometry(); /* 0, 256, 768, 1280, ... */
CHECK_EQ(g.grid_floor(255), 0);
CHECK_EQ(g.grid_floor(256), 256);
CHECK_EQ(g.grid_floor(767), 256);
CHECK_EQ(g.grid_floor(1000), 768);
CHECK(!g.on_grid(512));
}
/* An interval past the budget that is not a multiple of the budget's chunk would put the chunk
* after every checkpoint off the cold run's grid: exact rounds it up, with a warning that names
* both ways out; nearest keeps it. */
TEST(exact_rounds_an_interval_off_the_chunk_up_to_a_multiple_of_it) {
for (int exact = 0; exact < 2; ++exact) {
Fixture f;
f.build(); /* quantum 64 */
f.cfg.checkpoint_interval = 768; /* past the 512 budget, not a multiple of it */
f.cfg.prefix_exact = exact != 0;
log_set_level(Log::Error);
REQUIRE_EQ(f.start(), RAD_OK);
CHECK_EQ(f.sch.geometry().checkpoint_interval, exact ? 1024 : 768);
if (exact) CHECK_EQ(f.sch.geometry().grid, 512);
}
/* A budget off the quantum chunks at the quantum below it: 500 -> 448, and the interval goes
* to the next multiple of 448. */
Fixture f;
f.build(4096, 64, /*max_tok=*/500);
f.cfg.checkpoint_interval = 1024;
f.cfg.prefix_exact = true;
log_set_level(Log::Error);
REQUIRE_EQ(f.start(), RAD_OK);
CHECK_EQ(f.sch.geometry().checkpoint_interval, 1344);
CHECK_EQ(f.sch.geometry().grid, 448);
int64_t ck = -1;
CHECK_EQ(f.sch.plan_chunk(0, 5000, 500, &ck), 448);
CHECK_EQ(f.sch.plan_chunk(896, 5000, 500, &ck), 448);
CHECK_EQ(ck, 1344);
}
/* The tail snapshot is where the prompt's last chunk starts, a boundary an idle chunk already
* ends on: no extra step, and an exact repeat re-prefills that last chunk whole. */
TEST(an_exact_tail_snapshot_is_where_the_last_chunk_starts) {
Fixture f;
exact_fixture(f, 1); /* interval 1024, chunks of 512 */
REQUIRE_EQ(f.start(), RAD_OK);
Cut c = cut_from(f.sch, 0, 1800, 512);
CHECK((c.ends == std::vector<int64_t>{ 512, 1024, 1536, 1800 }));
CHECK((c.ckpts == std::vector<int64_t>{ 1024, 1536 }));
/* Its last chunk starting on the interval: that checkpoint is the tail's. */
c = cut_from(f.sch, 0, 1100, 512);
CHECK((c.ends == std::vector<int64_t>{ 512, 1024, 1100 }));
CHECK((c.ckpts == std::vector<int64_t>{ 1024 }));
/* One ending on the grid: the last chunk is [1536, 2048), and its end is an interval. */
c = cut_from(f.sch, 0, 2048, 512);
CHECK((c.ckpts == std::vector<int64_t>{ 1024, 1536, 2048 }));
/* Inside the first interval nothing (as before, cheap to prefill again). */
c = cut_from(f.sch, 0, 900, 512);
CHECK(c.ckpts.empty());
/* A step sharing its budget may straddle the tail: split there, the snapshot written. */
int64_t ck = -1;
CHECK_EQ(f.sch.plan_chunk(1400, 1800, 512, &ck), 136);
CHECK_EQ(ck, 1536);
/* A budget short of a quantum boundary past it leaves it for the next chunk. */
CHECK_EQ(f.sch.plan_chunk(1024, 1800, 100, &ck), 64);
CHECK_EQ(ck, -1);
/* Nearest cuts the same prompt's last chunk at its last quantum boundary instead. */
Fixture n;
n.build();
n.cfg.checkpoint_interval = 1024;
REQUIRE_EQ(n.start(), RAD_OK);
c = cut_from(n.sch, 0, 1800, 512);
CHECK((c.ends == std::vector<int64_t>{ 512, 1024, 1536, 1792, 1800 }));
CHECK((c.ckpts == std::vector<int64_t>{ 1024, 1792 }));
}
/* The property itself, over every geometry and many prompt lengths: every checkpoint is on the
* grid and is a chunk start of the cold run of the prompt that wrote it and of any longer prompt
* it prefixes (an agent's next turn), and from it the hit cuts exactly the cold run's chunks. */
TEST(an_exact_hit_from_any_checkpoint_recomputes_the_cold_runs_own_chunks) {
for (int kind = 0; kind < 6; ++kind) {
Fixture f;
exact_fixture(f, kind);
REQUIRE_EQ(f.start(), RAD_OK);
const ChunkGeometry& g = f.sch.geometry();
int checked = 0;
for (int64_t L = 100; L <= 3000; L += 37) {
const Cut cold = cut_from(f.sch, 0, L, 512);
for (int64_t c : cold.ckpts) {
CHECK(g.on_grid(c));
if (c >= L) continue;
CHECK(std::find(cold.ends.begin(), cold.ends.end(), c) != cold.ends.end());
CHECK(cut_from(f.sch, c, L, 512).ends == ends_after(cold, c));
for (int64_t L2 : { L + 1, L + 300, L + 1500 }) {
const Cut cold2 = cut_from(f.sch, 0, L2, 512);
CHECK(std::find(cold2.ends.begin(), cold2.ends.end(), c) != cold2.ends.end());
CHECK(cut_from(f.sch, c, L2, 512).ends == ends_after(cold2, c));
}
++checked;
}
}
CHECK(kind == 5 ? checked == 0 : checked > 20);
}
/* What nearest gives up: its tail snapshot at a 1000-token prompt's last quantum boundary
* (960) is no chunk start of the 1500-token turn after it, which a cold run cuts at 768 and
* 1024, so a hit there recomputes [960, 1024) as a chunk of its own. */
Fixture n;
n.build();
REQUIRE_EQ(n.start(), RAD_OK);
const Cut c1000 = cut_from(n.sch, 0, 1000, 512);
CHECK(std::find(c1000.ckpts.begin(), c1000.ckpts.end(), 960) != c1000.ckpts.end());
const Cut c1500 = cut_from(n.sch, 0, 1500, 512);
CHECK(std::find(c1500.ends.begin(), c1500.ends.end(), 960) == c1500.ends.end());
CHECK(std::find(c1500.ends.begin(), c1500.ends.end(), 768) != c1500.ends.end());
CHECK_EQ(cut_from(n.sch, 960, 1500, 512).ends.front(), 1024);
}
/* At admission: a hit at a snapshot resumes there (plan_chunk put it on the grid); a hit with no
* snapshot to resume from (full attention alone) steps back to the grid, the blocks above it
* left to the cache and recomputed in the cold run's chunk. */
TEST(an_exact_hit_steps_back_to_the_grid_and_prefills_the_cold_chunks) {
for (int exact = 0; exact < 2; ++exact) {
Fixture f;
exact_fixture(f, 5); /* chunks of 512, no snapshots */
f.cfg.prefix_exact = exact != 0;
REQUIRE_EQ(f.start(), RAD_OK);
f.pc.seed(784); /* 49 blocks */
const uint64_t a = f.submit(1300, 8);
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
CHECK_EQ(f.sch.find(a)->n_computed, exact ? 512 : 784);
CHECK_EQ(b->ctx_lens[0], exact ? 512 : 784);
CHECK_EQ(b->n_tok, 512); /* [512, 1024) as the cold run, or [784, 1296) */
CHECK_EQ((int)f.kv.block_table(a, 0).size(), exact ? 64 : 81);
CHECK_EQ(f.sch.metrics().hit_cut_tokens, exact ? 272 : 0);
}
/* With snapshots the hit is at one: kept as is, restored from its slot. */
Fixture f;
exact_fixture(f, 1);
REQUIRE_EQ(f.start(), RAD_OK);
f.pc.seed(1600);
f.pc.lin = 1536; /* a tail snapshot, on the 512 grid */
f.pc.src = 3;
const uint64_t a = f.submit(1800, 8);
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
CHECK_EQ(f.sch.find(a)->n_computed, 1536);
CHECK_EQ(b->n_tok, 264); /* the cold run's last chunk, whole */
CHECK_EQ(f.sch.plan().e[0].ckpt_restore, 3);
}
/* A cold prompt at the scheduler: the last chunk is not split at the end, and an exact repeat's
* hit at the tail snapshot is that chunk. */
TEST(an_exact_cold_prefill_keeps_its_last_chunk_whole) {
Fixture f;
exact_fixture(f, 0); /* interval 256, chunks of 256 */
REQUIRE_EQ(f.start(), RAD_OK);
f.submit(1000, 1);
std::vector<int64_t> toks, ckpts;
for (int i = 0; i < 8; ++i) {
const RadBatch* b = f.sch.step();
if (!b) break;
toks.push_back(b->n_tok);
for (int k = 0; k < b->n_checkpoints; ++k)
ckpts.push_back(b->ctx_lens[0] + b->checkpoint_tok[k] + 1);
f.commit_plain();
}
CHECK((toks == std::vector<int64_t>{ 256, 256, 256, 232 }));
CHECK((ckpts == std::vector<int64_t>{ 256, 512, 768 }));
}
TEST(a_chunk_ending_on_a_checkpoint_carries_it_into_the_batch) {
Fixture f;
f.build();