From 192fe102782838daf4079b58f946721d1c561b4e Mon Sep 17 00:00:00 2001 From: John Date: Thu, 22 Oct 2026 09:46:35 -0400 Subject: [PATCH] sched: checkpoint sliding windows at prefill intervals Keep intermediate window snapshots as well as the prompt-tail snapshot so a tail edit can resume below its divergence instead of re-prefilling the entire matched prefix. Reuse the existing checkpoint retention policy and preserve the pre-template-divergence tail boundary. Add scheduler and cache regression tests for interval snapshots, aligned prompt tails, constrained chunk budgets, and edited-prefix lookup. Update the geometry report and guide to describe periodic window snapshots. --- core/sched/geometry.cpp | 10 ++++----- core/sched/geometry.h | 7 +++---- core/sched/scheduler.cpp | 12 +++++++++-- docs/GUIDE.md | 8 ++++--- tests/mem_test.cpp | 23 ++++++++++++++++++++ tests/sched_test.cpp | 45 ++++++++++++++++++++++++++++++++++++++-- 6 files changed, 89 insertions(+), 16 deletions(-) diff --git a/core/sched/geometry.cpp b/core/sched/geometry.cpp index 94916bc4..9bacca31 100644 --- a/core/sched/geometry.cpp +++ b/core/sched/geometry.cpp @@ -126,14 +126,14 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry * retention. * * Without linear state there are no checkpoints, except for a trunk window under a prefix - * cache, which snapshots the window behind a prompt's tail (window_ckpt) without splitting - * chunks. */ + * cache, which snapshots the window at interval boundaries and the prompt's tail + * (window_ckpt). */ if ((has_linear || (has_window && cfg.prefix_cache)) && cfg.checkpoint_interval > 0) g.checkpoint_interval = align_up(cfg.checkpoint_interval, g.quantum); else g.checkpoint_interval = 0; g.window_ckpt = !has_linear && g.checkpoint_interval > 0; - /* A window snapshot splits no chunk (plan_chunk), so there is no interval grid to span. */ + /* Window checkpoints do not support skipping snapshots inside a prefill span. */ g.span = (g.checkpoint_interval > 0 && !g.window_ckpt && cfg.prefill_span > 1) ? cfg.prefill_span : 1; g.shared_tok = prefill_shared_tok(cfg); @@ -153,9 +153,9 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry /* 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. A window snapshot splits nothing. */ + * chunk; the log line makes that visible. */ const int64_t span_tok = g.checkpoint_interval * g.span; - const int64_t eff = (g.checkpoint_interval > 0 && !g.window_ckpt && span_tok < cfg.max_tok) + const int64_t eff = (g.checkpoint_interval > 0 && span_tok < cfg.max_tok) ? span_tok : cfg.max_tok; RAD_INFO("chunk geometry: quantum %lld (attn block %lld, state chunk %lld), checkpoint " "interval %lld, max_tok %lld -> prefill chunks of at most %lld%s", diff --git a/core/sched/geometry.h b/core/sched/geometry.h index a0bb327c..c36ad459 100644 --- a/core/sched/geometry.h +++ b/core/sched/geometry.h @@ -51,10 +51,9 @@ struct ChunkGeometry { /* Every contributor by name, for the startup line and the refusal. */ std::string why; - /* No linear state but a sliding window: the checkpoint snapshots only the window behind a - * prompt's tail (KVManager::has_window_snapshot). Chunks are not split at the interval, since - * a mid-prompt window snapshot gains nothing; the interval is only the shortest prompt worth - * a snapshot. */ + /* No linear state but a sliding window: checkpoints snapshot the window behind an + * interval boundary or the prompt's tail (KVManager::has_window_snapshot). Intermediate + * snapshots let an edited prompt resume below its divergence instead of from zero. */ bool window_ckpt = false; bool checkpoints() const { return checkpoint_interval > 0; } diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 939613e6..45ed1bef 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -751,7 +751,14 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg 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) next = INT64_MAX; /* a window snapshot: the tail only */ + if (geo_.window_ckpt) { + /* A tail-only window snapshot is unreachable after a prompt edit. + * Keep interval checkpoints too; the ordinary retention policy bounds + * their storage. Preserve the tail before the template's changed suffix, + * even when the prompt ends exactly on an interval boundary. */ + const int64_t tail = tail_checkpoint(n_target); + if (tail > n_computed) next = std::min(next, tail); + } /* A span (ChunkGeometry::span) starts only on an interval boundary past the first * interval, and its chunk is the span's intervals in a row, each the piece a span of 1 * makes a chunk of its own. Not from a prompt's start: the first interval may be served on @@ -765,7 +772,8 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg want = far - n_computed; else if (n_computed + want > next && n_computed + want < n_target) want = (n_computed + want) / I * I - n_computed; - if (!geo_.window_ckpt && (n_computed + want) % I == 0) { + if ((geo_.window_ckpt && n_computed + want == next) || + (!geo_.window_ckpt && (n_computed + want) % I == 0)) { if (checkpoint_pos) *checkpoint_pos = n_computed + want; } else if (n_computed + want == n_target) { /* Also one at the prompt's tail. The next turn resumes from the deepest snapshot diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a438b49e..79881c14 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1037,9 +1037,11 @@ What it costs: are read from host memory, and the arena a spanned step takes is room they lose for it ``` -A model without an interval grid (no linear state, or window snapshots at a prompt's end, as on -Gemma 4) spans nothing; there the flag only plans every step but a lone prefill at the shared -budget, and startup warns. +A model without checkpoints, or one whose checkpoints hold sliding windows (as on Gemma 4), +does not support spans; there the flag only plans every step but a lone prefill at the shared +budget, and startup warns. Window snapshots are written at `--checkpoint-interval` boundaries +as well as just before the prompt's end. The intermediate snapshots let an edited prompt resume +below its first changed token; `--checkpoint-policy` bounds the snapshots kept. **Sharing a step** (`--busy-prefill-chunk`, default `auto`). When other work is pending, a prefill chunk leaves room for one row plus the draft window per decode row behind it, and a share of the diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index eb70145b..6feb14ca 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -1104,6 +1104,29 @@ TEST(kv_window_snapshot_only_where_a_hit_resumes_from_it) { } } +TEST(window_prefix_tail_edit_resumes_at_an_earlier_interval_snapshot) { + Fixture f; + f.cfg.checkpoint_interval = 256; + f.cfg.checkpoint_slots = 8; + auto groups = hybrid_groups(/*with_linear=*/false); + groups.push_back(paged_group("swa", RAD_KV_WINDOW, 2, 16, { 2 }, 64)); + REQUIRE_EQ(f.build_groups(groups), RAD_OK); + CHECK(f.kv.has_window_snapshot()); + auto tokens = tok_run(1000, 1100); + publish(f, 1, tokens, 1100); + const auto hashes = chain_of(tokens); + for (int64_t p : { 256, 512, 768, 1024, 1072 }) ckpt_at(f, 1, hashes, p); + f.pc.retain(1); + + /* Replacing the final 123 tokens invalidates both the tail and 1024 snapshot, + * but the unchanged prefix still reaches the snapshot at 768. */ + for (size_t i = 977; i < tokens.size(); ++i) tokens[i] += 10000; + const CacheHit hit = f.pc.lookup(tokens, {}); + CHECK_EQ(hit.n_attn_tokens, 976); + CHECK_EQ(hit.n_linear_tokens, 768); + CHECK(hit.checkpoint_slot >= 0); +} + TEST(kv_window_group_recycles_below_the_window) { std::vector g; g.push_back(null_group()); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index c3f5a2a4..54839978 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -343,6 +343,47 @@ TEST(chunk_geometry_refuses_a_budget_it_cannot_serve) { CHECK_EQ(f.start(), RAD_E_INVAL); } +/* Gemma-like FULL + WINDOW, with no recurrent state. A prompt-tail snapshot alone + * cannot serve a request whose last few hundred tokens changed: it is past the + * divergence. Keep periodic snapshots as well, so lookup can resume below it. */ +TEST(window_prefill_writes_interval_snapshots_before_the_tail) { + Fixture f; + f.build(4096, 64, 512, 4096, /*windowed=*/true); + f.prog.kv_groups.erase(f.prog.kv_groups.begin() + 1, f.prog.kv_groups.begin() + 3); + f.prog.ops.resize(1); /* remove the recurrent scan and its chunk constraint */ + f.prog.buffers.resize(1); /* its derived conv/state indices do not apply either */ + f.kv.setup(f.prog.kv_groups, f.cfg.max_ctx, 4096, 64); + f.pc.groups = { 0 }; + REQUIRE_EQ(f.start(), RAD_OK); + CHECK(f.sch.geometry().window_ckpt); + + int64_t pos = 0; + std::vector snapshots; + while (pos < 1100) { + int64_t ckpt = -1; + const int64_t n = f.sch.plan_chunk(pos, 1100, 512, &ckpt); + REQUIRE(n > 0); + pos += n; + if (ckpt > 0) { + CHECK_EQ(ckpt, pos); + snapshots.push_back(ckpt); + } + } + const std::vector expected = { 256, 512, 768, 1024, 1072 }; + CHECK(snapshots == expected); + + /* An aligned prompt must still snapshot before its last block: that block's + * generation marker changes when the client replays the assistant reply. */ + int64_t ckpt = -1; + CHECK_EQ(f.sch.plan_chunk(768, 1024, 512, &ckpt), 240ll); + CHECK_EQ(ckpt, 1008ll); + /* A shared step may end before the boundary; the next chunk must reach it. */ + CHECK_EQ(f.sch.plan_chunk(0, 1100, 160, &ckpt), 160ll); + CHECK_EQ(ckpt, -1ll); + CHECK_EQ(f.sch.plan_chunk(160, 1100, 160, &ckpt), 96ll); + CHECK_EQ(ckpt, 256ll); +} + TEST(prefill_chunks_split_at_the_checkpoint_interval) { Fixture f; f.build(); @@ -609,8 +650,8 @@ TEST(a_shared_step_under_a_span_is_planned_as_without_one) { CHECK(plans[0] == plans[1]); } -/* No interval grid, no span: a window snapshot splits no chunk, and a model without checkpoints - * has no interval. The shared budget still follows the flag, as the engine states it to the plugin +/* Window snapshots do not support spans, and a model without checkpoints has no interval. + * The shared budget still follows the flag, as the engine states it to the plugin * (RadBuildCtx::max_mixed_tok), and steps that share it admit as before. */ TEST(a_span_needs_an_interval_grid) { Program prog;