From 8df9240d65dded5624e296f308a85f1a9920d865 Mon Sep 17 00:00:00 2001 From: mici81 Date: Fri, 30 Oct 2026 11:59:35 +0100 Subject: [PATCH] sched: the preempted-prefill gate skips a KV group that holds no blocks Every program carries a sentinel KV group at index 0. KVGeom reads its decl.kind 0 as RAD_KV_FULL with block size 1, while the manager's plan for it pages nothing and owns no block. The gate (e668d49) walked every group KVGeom calls paged, so for the sentinel it wanted the whole prompt, one block a token, out of a pool of none: a preempted request was never readmitted. The bound on the wait (33108e4) cannot help, since preempting running requests frees nothing in that group. engine_e2e_test's qwen35_preemption_by_recompute_changes_no_logit hung on it (the KL recording of a small pool: one preempted document, nothing left running); with the group skipped it passes in 0.9 s. It may also be the live request in #346 that waited 710 s with gen=0. The gate now skips a group with no blocks (total_blocks 0), as KVManager::ensure skips a group its plan does not page. sched_test: a_preempted_prefill_ignores_a_kv_group_that_holds_no_blocks (a group the scheduler sees as full and the manager as empty; the preempted request stayed Preempted after the pool emptied before the fix). 173 cases pass. Co-Authored-By: Claude Opus 5.5 --- core/sched/scheduler.cpp | 5 ++++- tests/sched_test.cpp | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 396eaef8..5951520c 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1509,7 +1509,10 @@ bool Scheduler::preempted_can_prefill(const SchedReq& s) const { for (size_t g = 0; g < kvgeom_.g.size(); ++g) { const KVGeom::G& gg = kvgeom_.g[g]; - if (!gg.paged) continue; + /* A group the manager gives no blocks (the sentinel at index 0, which KVGeom reads as a + * full group of block size 1) asks nothing: ensure() skips it the same way. Counting it + * wanted the whole prompt from a pool of none and kept a preempted request out for good. */ + if (!gg.paged || kv_->total_blocks((int32_t)g) <= 0) continue; int64_t need = (target + gg.block_size - 1) / gg.block_size - (int64_t)kv_->block_table(r.id, (int32_t)g).size(); if (need < 0) need = 0; diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index a47ef509..21031990 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -1725,6 +1725,48 @@ TEST(a_preempted_prefill_waits_for_the_whole_prompt_and_running_headroom) { f.commit_plain(); } +/* Every program carries a sentinel KV group at index 0: decl.kind 0 (RAD_KV_FULL), block size 1, no + * layers, and the manager's plan for it pages nothing, so it holds no blocks. The gate must not ask + * that group for the prompt's blocks: it has none to give, and the preempted request would wait + * for ever (engine_e2e_test's preemption case hung on it, with nothing left running). Here the + * group is appended, not at index 0, so the fixture's bindings stay where they are. */ +TEST(a_preempted_prefill_ignores_a_kv_group_that_holds_no_blocks) { + Fixture f; + f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */ + KVGroupInfo none; /* as the sentinel: kind 0, no block size, no layers */ + none.name = "none"; + f.prog.kv_groups.push_back(none); + f.kv.setup(f.prog.kv_groups, f.cfg.max_ctx, 26, 64); + const size_t ng = f.prog.kv_groups.size() - 1; + f.kv.geom.g[ng].paged = false; /* the manager pages nothing there ... */ + f.kv.total[ng] = 0; /* ... and owns no block of it */ + f.kv.freelist[ng].clear(); + f.kv.refcnt[ng].clear(); + for (int k = 63; k >= 0; --k) f.kv.state_free[ng].push_back(k); + f.cfg.preempt_wait_ms = 60000; + CHECK_OK(f.start()); + + uint64_t a = f.submit(287, 64, /*priority=*/5); + uint64_t b = f.submit(128, 64, /*priority=*/0); + CHECK(f.sch.step() != nullptr); + CHECK_EQ(f.sch.plan().e.size(), 2); + f.commit_plain(); + CHECK(f.sch.step() != nullptr); /* B's next block: B is preempted */ + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + f.commit_plain(); + + for (int i = 0; i < 80 && f.sch.find(a)->state != ReqState::Finished; ++i) { + CHECK(f.sch.step() != nullptr); + f.commit_plain(); + } + CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Finished); + + /* A's blocks are back and nothing runs: B prefills its whole prompt again. */ + CHECK(f.sch.step() != nullptr); + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Running); + f.commit_plain(); +} + /* The gate's wait is bounded: after --preempt-wait-ms an overdue preempted prefill gets room by * preempting the lowest-priority running request before the step is planned, so it cannot queue * forever behind a long decode (the 710 s stall in the daily's log). */