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

+56 -1
View File
@@ -871,7 +871,38 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
int64_t want = remaining < budget ? remaining : budget;
if (geo_.checkpoints()) {
if (geo_.checkpoints() && geo_.exact) {
/* --prefix-resume exact: checkpoints only where an idle cold prefill of this prompt starts
* a chunk (ChunkGeometry::grid), so a hit that resumes at one recomputes the cold run's
* own chunks. Splits as below at the interval and a span's end; the tail snapshot is at
* the start of the prompt's last chunk (tail_checkpoint), a boundary an idle chunk already
* ends on, so it costs a copy and no step. A step that shares the budget (another
* request, decode rows) may straddle it, and is split there. */
const int64_t I = geo_.checkpoint_interval;
const bool periodic = !geo_.window_ckpt || geo_.window_periodic;
const int64_t next = periodic ? ((n_computed / I) + 1) * I : INT64_MAX;
int64_t far = next;
if (geo_.span > 1 && span_now_ && n_computed >= I && n_computed % I == 0)
far = n_computed + geo_.span * I;
if (n_computed + want > far)
want = far - n_computed;
else if (n_computed + want > next && n_computed + want < n_target)
want = (n_computed + want) / I * I - n_computed;
const int64_t tail = tail_checkpoint(n_target);
if (tail > n_computed && n_computed + want >= tail) {
want = tail - n_computed;
if (checkpoint_pos) *checkpoint_pos = tail;
} else if (periodic && (n_computed + want) % I == 0 && geo_.on_grid(n_computed + want)) {
if (checkpoint_pos) *checkpoint_pos = n_computed + want;
}
/* A request's own checkpoint (Request::checkpoint_at) as below, wherever it falls: the
* head the request shares with others resumes there by design. */
const int64_t keep = keep_at / geo_.quantum * geo_.quantum;
if (keep > n_computed && keep <= n_computed + want && keep <= n_target) {
want = keep - n_computed;
if (checkpoint_pos) *checkpoint_pos = keep;
}
} else if (geo_.checkpoints()) {
const int64_t I = geo_.checkpoint_interval;
int64_t next = ((n_computed / I) + 1) * I; /* strictly after n_computed */
if (geo_.window_ckpt && !geo_.window_periodic) {
@@ -955,6 +986,16 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg
int64_t Scheduler::tail_checkpoint(int64_t target) const {
const int64_t I = geo_.checkpoint_interval, Q = geo_.quantum;
if (I <= 0 || Q <= 0) return -1;
if (geo_.exact) {
/* --prefix-resume exact: where the prompt's last chunk starts on the resume grid, so an
* exact repeat re-prefills that chunk as the cold run did, and an appended turn that chunk
* and what it added. A window snapshot keeps its margin below (next comment): the grid
* point under the end less a block. Past the first interval, as below. */
const int64_t margin = geo_.window_ckpt ? std::max<int64_t>(Q, 16) : 1;
const int64_t t = geo_.grid_floor(target - margin);
if (geo_.window_ckpt) return t >= I ? t : -1;
return t > I ? t : -1;
}
const int64_t tail = target / Q * Q;
/* A window snapshot is taken at the tail of any prompt of an interval or more, a block short
* of the end: the next turn re-renders the last few tokens (the generation prompt is replaced
@@ -1157,6 +1198,20 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
if (reuse > last) reuse = last;
if (reuse < 0) reuse = 0;
/* --prefix-resume exact: resume only on the grid an idle cold prefill chunks this prompt
* on (ChunkGeometry::grid), so the tokens recomputed below are cut into the cold run's own
* chunks. A snapshot is already on it (plan_chunk writes none off it, but for a request's
* own Request::checkpoint_at); a hit at a block (no snapshot needed) steps back to it. */
if (geo_.exact && reuse > 0 &&
!(geo_.checkpoints() && h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens)) {
const int32_t g = (int32_t)geo_.grid_floor(reuse);
if (g < reuse)
RAD_DEBUG("admit %llu: prefix hit of %lld tokens resumes at %lld, on the "
"%lld-token chunk grid (--prefix-resume exact)", (unsigned long long)r.id,
(long long)reuse, (long long)g, (long long)geo_.grid);
reuse = g;
}
/* A hit on a hybrid model needs a checkpoint. The adopted blocks cover the paged
* attention groups, but nothing covers a recurrent group, whose state lives in a
* per-sequence slot; skipping tokens also requires restoring that state (`ckpt_src`,