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

+16
View File
@@ -167,6 +167,11 @@ static const Flag g_flags[] = {
" one, for each of --max-num-seqs, within an eighth of host\n"
" memory. N fixes the total; 0 keeps none" },
{ "--no-prefix-cache", nullptr, "disable prefix caching" },
{ "--prefix-resume", "MODE", "where a prefix hit resumes: exact (default) only where\n"
" an idle server's cold prefill of the prompt starts a\n"
" chunk, so the hit answers bit-identically to a cold run;\n"
" nearest at the deepest snapshot, up to a chunk less to\n"
" re-prefill" },
{ "--checkpoint-policy", "NAME", "which snapshots a session retains: geometric-backoff "
"(default), tip-only, keep-all" },
@@ -682,6 +687,15 @@ int config_parse(int argc, char** argv, Config* c) {
}
}
else if (S("--no-prefix-cache")) c->prefix_cache = false;
else if (S("--prefix-resume")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (!std::strcmp(v, "exact")) c->prefix_exact = true;
else if (!std::strcmp(v, "nearest")) c->prefix_exact = false;
else {
RAD_ERR("--prefix-resume '%s' is not one of exact, nearest", v);
return RAD_E_INVAL;
}
}
else if (S("--checkpoint-policy")) {
if (!(v = need(i))) return RAD_E_INVAL;
/* Validated, so an unknown name does not silently fall back to the default. */
@@ -1182,6 +1196,8 @@ const ShowRow kShow[] = {
return c.checkpoint_slots < 0 ? std::string("auto") : num(c.checkpoint_slots); } },
{ "--no-prefix-cache", [](const Config& c) {
return std::string(c.prefix_cache ? "not given: prefix caching on" : "prefix caching off"); } },
{ "--prefix-resume", [](const Config& c) {
return std::string(c.prefix_exact ? "exact" : "nearest"); } },
{ "--checkpoint-policy", [](const Config& c) { return c.checkpoint_policy; } },
{ "--num-speculative-tokens", [](const Config& c) { return c.n_spec < 0 ? std::string("auto") : num(c.n_spec); } },
{ "--draft-window", [](const Config& c) { return std::string(draft_window_name(c.draft_window)); } },
+8
View File
@@ -898,6 +898,14 @@ struct Config {
* store is opened (checkpoint_slots_auto in core/mem/prefix.h). */
int64_t checkpoint_slots = -1;
bool prefix_cache = true;
/* --prefix-resume exact|nearest. Exact (the default) resumes a prefix hit only on the grid an
* idle server's cold prefill chunks the same prompt on (ChunkGeometry::grid), and takes a
* prompt's tail snapshot where its last chunk starts: the hit recomputes the cold run's own
* chunks, so its output is the cold run's bit for bit, where a kernel rounds a row differently
* at another chunk length. Nearest resumes at the deepest snapshot or block, a prompt's tail
* snapshot at its last quantum boundary: up to a chunk less re-prefilled a turn, and a hit that
* can differ from a cold run in the last bits (a near-tie in greedy text). */
bool prefix_exact = true;
/* geometric-backoff | tip-only | keep-all. §19.1 records the default as unmeasured;
* core/mem/prefix.h ships the two extremes so they can be compared. */
std::string checkpoint_policy = "geometric-backoff";
+43 -2
View File
@@ -143,15 +143,49 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
* checkpoints land wherever a step ends (§7.3). */
g.min_max_tok = g.quantum;
*out = g;
if (cfg.max_tok < g.min_max_tok) {
*out = g;
RAD_ERR("--max-num-batched-tokens is %lld but this model's chunk geometry needs at least "
"%lld, so a chunk can end on a linear-state boundary: %s",
(long long)cfg.max_tok, (long long)g.min_max_tok, g.why.c_str());
return RAD_E_INVAL;
}
/* The resume grid (--prefix-resume exact): where an idle server's prefill chunks start. A
* prefill alone in its step takes the budget, rounded down to the quantum, and stops at every
* interval boundary it reaches where chunks split at the interval (linear state, or periodic
* window snapshots); a span carries h intervals from the second one on. An interval past the
* budget must then be a multiple of the budget's chunk, or the chunk after a checkpoint would
* start off the grid every cold run cuts: rounded up, as the quantum rounds it, since a longer
* interval costs only retention. */
const int64_t I0 = g.checkpoint_interval;
const bool splits = I0 > 0 && (!g.window_ckpt || g.window_periodic);
const int64_t budget_chunk = cfg.max_tok / g.quantum * g.quantum;
g.exact = cfg.prefix_exact && cfg.prefix_cache;
if (g.exact && splits && I0 > budget_chunk && I0 % budget_chunk != 0) {
g.checkpoint_interval = align_up(I0, budget_chunk);
RAD_WARN("--checkpoint-interval %lld resolves to %lld: --prefix-resume exact needs it a "
"multiple of the %lld-token prefill chunk (--max-num-batched-tokens %lld) so "
"every checkpoint is where a cold prefill's chunk ends; give a multiple of %lld, "
"or --prefix-resume nearest to keep %lld", (long long)cfg.checkpoint_interval,
(long long)g.checkpoint_interval, (long long)budget_chunk, (long long)cfg.max_tok,
(long long)budget_chunk, (long long)I0);
}
if (g.exact) {
const int64_t I = g.checkpoint_interval;
if (splits && I <= budget_chunk) {
const int64_t h = g.span > 1 ? std::max<int64_t>(1, std::min(g.span, cfg.max_tok / I))
: 1;
g.grid = h * I;
g.grid_origin = h > 1 ? I : 0;
} else {
g.grid = budget_chunk;
g.grid_origin = 0;
}
}
*out = g;
/* Log the effective chunk length. `plan_chunk` splits at the checkpoint interval first, so
* when the interval is below --max-num-batched-tokens, raising the budget does not change the
* chunk; the log line makes that visible. tip-only window snapshots do not
@@ -167,6 +201,13 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry
eff >= cfg.max_tok ? ""
: g.span > 1 ? " (the checkpoint interval times the span, not the budget)"
: " (the checkpoint interval, not the budget)");
if (g.exact)
RAD_INFO("prefix resume: exact -- a hit resumes only where an idle server's cold prefill "
"of the same prompt starts a chunk (every %lld tokens%s) and a prompt's tail "
"snapshot is taken where its last chunk starts, so the hit recomputes the cold "
"run's own chunks and answers bit-identically; a turn re-prefills up to one chunk "
"more than --prefix-resume nearest", (long long)g.grid,
g.grid_origin > 0 ? fmt(" from %lld", (long long)g.grid_origin).c_str() : "");
if (cfg.prefill_span > 1) {
/* What the span does here, said once: the intervals the budget holds can be fewer than
* the flag asks for, and a model without an interval grid spans nothing. */
+22
View File
@@ -60,7 +60,29 @@ struct ChunkGeometry {
* tip-only, which keeps the tail alone and so splits no chunk at the interval. */
bool window_periodic = false;
/* --prefix-resume exact (Config::prefix_exact): a prefix hit resumes only where a cold prefill
* of the same prompt starts a chunk, so the tokens a hit recomputes are cut into the chunks a
* cold run cuts them into. A kernel may round a row differently at another chunk length (a
* split-K count chosen from M, a small-M path); computed in the same chunks, the hit's output
* is the cold run's, bit for bit, on an idle server. The grid is where an idle server's prefill
* chunks start: 0, then grid_origin + k * grid. `grid` is the checkpoint interval where chunks
* split at it and it fits the budget, times the span from the second interval on (origin = the
* interval), and otherwise --max-num-batched-tokens rounded down to the quantum (the interval
* is then rounded up to a multiple of it). Zero when not exact. */
bool exact = false;
int64_t grid = 0;
int64_t grid_origin = 0;
bool checkpoints() const { return checkpoint_interval > 0; }
/* The deepest grid position at or below `pos`; `pos` itself when not exact. */
int64_t grid_floor(int64_t pos) const {
if (pos <= 0) return 0;
if (!exact || grid <= 0) return pos;
if (pos < grid_origin) return 0;
return grid_origin + (pos - grid_origin) / grid * grid;
}
bool on_grid(int64_t pos) const { return grid_floor(pos) == pos; }
};
/* The budget of a step a span does not take (ChunkGeometry::shared_tok), from the configuration
+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`,