sched: a prefix hit on a windowed TRUNK re-prefills — reuse cannot cover a group the cache does not index

prefix.cpp indexes the RAD_KV_FULL groups and skips RAD_KV_WINDOW (its own
words: "a hit's blocks do not describe a cache whose floor moves" — the
blocks below the window are recycled). admit_one clamped a hit for the
linear/checkpoint cases; a model whose own attention includes a group the
cache skips — every windowed+full pair — passed every clamp, reused, and
adopted blocks covering the CACHED groups only. The windowed trunk layers
take used = ctx_len + n_tokens (batch.cpp) and read positions below
ctx_len this sequence never wrote; grab_block's min-heap (kv.cpp) hands
out the most recently freed blocks, i.e. the previous tenant's. The
sequence attends to the PREVIOUS REQUEST'S KV as its own prefix: fluent
text carrying the last request's content. Reproduced on a live server
with sequential prompts (A-B-C-A-B: the re-run of A reads C's prompt),
and it is why a windowed model with the prefix cache on cannot currently
serve more than one conversation at a time.

The fix is a second predicate next to the drafter floor, computed once
in init(): a paged group bound to a TRUNK layer (below meta.n_layers —
a drafter borrows from base_layer and above, per rad_bind_layer_kv) that
cached_groups() does not name. In admit_one, a hit with such a hole
reuses nothing: the prefix re-prefills (windowed prompts are small;
milliseconds). The drafter's own hole stays the refill design's —
HitCap ends the hit one drafter window before the prompt's end so the
tail-recompute covers the drafter's window while the trunk reuses the
rest (the e95d397 tests keep passing, all of them); no tail-recompute
can repair a trunk hole, which is why zero is the honest answer there.

sched_test gains an opt-in WINDOW group in the fixture (bound to a
trunk layer) and a_hit_on_a_model_with_a_windowed_group_reuses_nothing:
a deep, checkpoint-covered hit — legal under every other clamp — on a
cache indexing only the FULL group. Reuse lands at 0, nothing is
adopted, and attn_cached_tokens still reports the 304-token match: the
cache hit and the scheduler refused it. Disable the clamp and the hit
adopts 16 blocks of the seeded (previous tenant's) KV — n_computed 256,
the leak as shipped; that is the regression net.

Box: ctest lane + A-B-C-A-B battery green (evidence in the PR body).
This commit is contained in:
sfjes committed 2026-10-09 23:13:26 -07:00
1 parent 04405089cb
commit 73620ef00e
3 files changed
+119 -2

No files matched your search

+38
View File
@@ -87,6 +87,21 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
n_paged, pc->cached_groups().size(),
hit_holes_draft_ ? "LEAVES a hole in a drafter's own KV" : "leaves no hole");
}
/* THE TRUNK'S SHARE of that question: a paged group a TRUNK layer reads (a bound layer below
* the trunk's layer count — a drafter borrows from base_layer and above) that the cache does
* not index. Its hole is not repairable by recomputing the tail; see scheduler.h. */
hit_holes_trunk_ = false;
for (size_t i = 0; i < prog.kv_groups.size(); ++i) {
if (kv->block_tokens((int32_t)i) <= 0) continue;
bool trunk = false;
for (int l : prog.kv_groups[i].layers) if (l < prog.meta.n_layers) { trunk = true; break; }
if (!trunk) continue;
const std::vector<int32_t>& cg = pc->cached_groups();
if (std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end()) { hit_holes_trunk_ = true; break; }
}
if (hit_holes_trunk_)
RAD_INFO("prefix cache: a TRUNK paged group is not prefix-cached — a hit re-prefills, "
"it cannot be capped");
draft_refill_ = 0;
if (cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_BLOCK && prog.drafter.window > 0 &&
hit_holes_draft_) {
@@ -964,6 +979,29 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
* slot last held, and it reads as fluent text rather than as a failure. */
if (reuse > 0 && has_linear_state_ && h.checkpoint_slot < 0) reuse = 0;
/* THE WINDOWED-GROUP CLAMP — a prefix hit on a model whose OWN (trunk) KV includes a
* paged group the cache does not index. prefix.cpp skips RAD_KV_WINDOW groups — their
* blocks below the window are recycled, so no block list describes them; the comment
* there says a hit "costs the sequences ... a windowed layer whose cache starts where
* the hit ended" — this is that cost in KV corruption. The adopted blocks cover the
* cached groups ONLY, while `used = ctx_len + n_tokens` (batch.cpp) makes every
* windowed trunk layer read the positions below ctx_len that this sequence NEVER WROTE
* — and grab_block's min-heap (kv.cpp) hands this sequence the very blocks the most
* recent tenant wrote. The model then attends to the PREVIOUS REQUEST'S KV as its own
* prefix: fluent text carrying the last request's CONTENT. Zero the reuse; the prefix
* re-prefills (windowed prefixes are small; the re-prefill is milliseconds).
*
* NOT the drafter's hole. A drafter's own group (bound at base_layer and above — see
* scheduler.h) is repaired by draft refill, which CAPS the hit so the recompute covers
* the drafter's window while the trunk reuses the rest; zeroing here would delete that
* design. The trunk's hole, which no tail-recompute can fix, is this clamp's own. */
if (reuse > 0 && hit_holes_trunk_) {
reuse = 0;
RAD_DEBUG("admit %llu: prefix hit %lld zeroed — a trunk paged group is not "
"prefix-cached (windowed); re-prefilling", (unsigned long long)r.id,
(long long)h.n_attn_tokens);
}
/* Claim the cached blocks. Without this the hit is only an accounting entry: the sequence
* would skip computing those tokens and then attend to blocks it does not own. adopt()
* takes a whole block multiple, which `reuse` is -- a whole number of blocks, or a
+12
View File
@@ -501,6 +501,18 @@ private:
* also skips on a block-size mismatch. A bound like this lives in exactly one place; a second
* copy is free to drift out of step with the first. */
bool hit_holes_draft_ = true;
/* DOES A HIT HOLE THE TRUNK ITSELF? hit_holes_draft_ is the drafter's guard, and draft_refill_
* repairs a drafter hole by recomputing the prompt's TAIL. A TRUNK group the cache does not
* index admits no such repair: the adopted blocks cover the cached groups only, while the
* windowed trunk layers read the positions below ctx_len this sequence never wrote — the KV of
* whoever held these blocks last. So a trunk hole refuses the hit outright.
*
* THE TRUNK IS WHICH GROUPS? The one a drafter borrows is bound at `base_layer` and above —
* the drafter's layers run after the trunk's (rad_bind_layer_kv at the drafter declare), so a
* group with a bound layer below meta.n_layers belongs to the trunk and a hit must cover it;
* a drafter's own group is the refill's business, not this clamp's. Read off the Program,
* which carries both the bindings and the trunk's layer count. */
bool hit_holes_trunk_ = false;
/* DRAFT REFILL. Where a hit leaves a hole in a WINDOWED block drafter's cache, the drafter is
* declined until its window has slid past the hole, which on the follow-up turn of a chat --
* the turn a prefix hit is for -- is most or all of the reply. So the hit is ended one window
+69 -2
View File
@@ -492,7 +492,7 @@ struct Fixture {
~Fixture() { sch.fini(); for (void* p : owned) std::free(p); }
void build(int64_t blocks = 4096, int64_t states = 64, int64_t max_tok = 512,
int64_t max_ctx = 4096) {
int64_t max_ctx = 4096, bool windowed = false) {
g_gdn_ki = RadKernelInfo{};
g_gdn_ki.name = "gdn_fake";
g_gdn_ki.op = "gdn_chunked";
@@ -524,6 +524,25 @@ struct Fixture {
conv.decl.conv_width = 4;
prog.kv_groups.push_back(conv);
/* A WINDOWED paged group — the second half of the pair a sliding-window model declares
* (windowed layers + a full group; qwen35/llama windowed checkpoints declare these).
* Paged, block-aligned, and deliberately NOT prefix-cached: prefix.cpp indexes only the
* groups whose blocks below the window stay put. Tests that want a prefix cache over a
* WINDOW+FULL pair trim pc.groups to the cached ones. */
if (windowed) {
KVGroupInfo win;
win.name = "swa";
win.decl.kind = RAD_KV_WINDOW;
win.decl.window = 64;
win.block_size = 16;
win.layers = { 2 }; /* bound to a TRUNK layer — the group a trunk attention reads;
* a drafter's own group is bound at the trunk's layer count and
* above (rad_bind_layer_kv at the drafter declare) */
prog.kv_groups.push_back(win);
prog.meta.n_layers = 16; /* trunk layers 0..15; the drafter tests' appended group
* stays unbound, and unbound is not trunk-owned */
}
OpInfo op;
op.op = "gdn_chunk_scan";
op.schema = &g_gdn_schema;
@@ -926,6 +945,50 @@ TEST(a_hit_that_no_checkpoint_covers_replays_from_zero) {
CHECK_EQ(f.sch.metrics().linear_cached_tokens, 0);
}
/* THE PREFIX CACHE CANNOT COVER A MODEL WITH A PAGED GROUP IT DOES NOT INDEX. The fixture's
* groups are FULL, LINEAR, CONV, WINDOW — the windowed group is paged, bound to a trunk layer,
* and a real cache skips it (prefix.cpp: its blocks below the window are recycled, so no block
* list describes them), which is what pc.groups = {0} reproduces: three paged groups, one
* cached. The cache below is seeded to a DEEP, checkpoint-covered hit — 304 attention blocks,
* the linear state at 256, a slot to restore from — the most legitimate hit there is: every
* clamp above the trunk one lets it through untouched. The adopted blocks cover group 0 ONLY,
* while used = ctx_len + n_tok makes the windowed trunk layers read the positions below ctx_len
* this sequence never wrote — and the pool hands out the most recently freed blocks, i.e. the
* previous tenant's. That is reading ANOTHER REQUEST'S KV AS YOUR OWN PREFIX: fluent text
* carrying the last request's content. No tail-recompute repairs a trunk hole (a drafter's own
* hole is another matter, and the refill tests above keep theirs); the hit is refused. */
TEST(a_hit_on_a_model_with_a_windowed_group_reuses_nothing) {
Fixture f;
f.build(4096, 64, 512, 4096, /*windowed=*/true);
f.pc.groups = {0}; /* a cache that indexes the FULL group only — windowed groups are not
* prefix-cacheable; must precede start(), which reads it once */
CHECK_OK(f.start());
f.pc.seed(304); /* a deep, block-aligned attention hit: 19 blocks */
f.pc.lin = 256; /* its linear state checkpoints at 256 — the resume point exists */
f.pc.src = 3; /* and names a snapshot, so no checkpoint clamp applies */
uint64_t a = f.submit(300, 8);
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
/* The whole hit is refused, not trimmed: the request runs from zero. */
CHECK_EQ(f.sch.find(a)->n_computed, 0);
CHECK_EQ(f.sch.find(a)->n_cached, 0);
CHECK_EQ(b->ctx_lens[0], 0);
CHECK_EQ(f.sch.plan().e[0].ckpt_restore, -1);
/* NOTHING IS ADOPTED — the 16 blocks covering the 256-token chunk are all freshly grabbed. */
CHECK_EQ((int)f.kv.block_table(a, 0).size(), 16);
CHECK(std::find(f.pc.seeded[0].begin(), f.pc.seeded[0].end(),
f.kv.block_table(a, 0)[0]) == f.pc.seeded[0].end());
/* THE CACHE STILL REPORTED THE MATCH, and the metrics say so: this is a real hit the
* scheduler refused, not a lookup that found nothing. */
SchedMetrics m = f.sch.metrics();
CHECK_EQ(m.attn_cached_tokens, 304);
CHECK_EQ(m.linear_cached_tokens, 0);
}
/* ================================================================== preemption */
/* A REQUEST WHOSE CONTEXT IS STILL BEING READ OFF DISK WAITS, AND ONLY IT DOES. It is not admitted
* short -- the hit length is what its prefill is shaped by -- and it does not hold up the queue:
@@ -1291,7 +1354,11 @@ RefillRun refill_run(int n_prompt, int32_t seeded, int64_t ckpt_lo = 0) {
f.cfg.n_spec = 7;
f.prog.drafter.kind = RAD_DRAFT_BLOCK;
f.prog.drafter.window = 512;
KVGroupInfo w; /* the drafter's own group: paged, windowed, not prefix-cached */
KVGroupInfo w; /* the drafter's own group: paged, windowed, not prefix-cached.
* UNBOUND, and a drafter group bound at the trunk's layer count
* and above is the same — the trunk clamp reads bound TRUNK
* layers (scheduler.h, hit_holes_trunk_) and does not fire on a
* drafter hole: the refill below is exactly its repair. */
w.name = "draft";
w.decl.kind = RAD_KV_WINDOW;
w.decl.window = 512;