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

+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. */