From b632c72f5dcac69b1329d31ec276093ba00dec0e Mon Sep 17 00:00:00 2001 From: mici81 Date: Thu, 5 Nov 2026 10:24:18 +0100 Subject: [PATCH] sched: an overdue preempted prefill keeps its place ahead of the queue A preempted request waiting for its whole-recompute gate (kAdmitWait) let the admission walk ask the next request in the queue. For an overdue one that undoes the overdue path: the room it made in this step went to a request queued behind it. With preempted prompts published, the victim forced off for it needs almost nothing to come back, so it was re-admitted at once and forced off again on the next step. Under KV pressure the same victim was forced off and re-admitted 26 times in 16 s while the overdue request waited. An overdue preempted request that is still refused now stops the walk, as a refused fresh request already does. 2x R9700, Qwen3.8-Flash-Next TP2, production flags with the KV cache cut to 6.45 GiB a card (--gpu-headroom-mib 3072), 16 concurrent 58.6k-token prompts with 4000-token answers: - without publishing preempted prompts: wall 390.6 s, mean 338.5 s, 9 preemptions, 1 forced - publishing them: wall 346.4 / 344.1 s, mean 306.6 / 299.8 s, 36 / 32 preemptions, 28 / 26 forced - publishing them, with this change: wall 331.0 / 324.4 s, mean 289.4 / 284.0 s, 8 / 9 preemptions, 1 / 1 forced Co-Authored-By: Claude Opus 5.5 --- core/sched/scheduler.cpp | 12 ++++++++++- tests/sched_test.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 1cad5a22..54f99381 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -2076,7 +2076,17 @@ const RadBatch* Scheduler::step() { admit_full_ = 0; admit_next_step_ = false; const int as = admit_one(s, budget, areserve); - if (as == kAdmitWait) { ++at; continue; } + /* A preempted request waiting for its whole-recompute gate lets the next one be asked; + * an overdue one does not. The room the overdue path made this step is for it, and a + * request queued behind it would take that room back: a victim just forced off for it, + * its prompt published, needs almost nothing to come back, and under KV pressure the + * same victim was forced off and re-admitted 26 times in 16 s while the overdue request + * waited. */ + if (as == kAdmitWait) { + if (s.req->state == ReqState::Preempted && preempt_wait_overdue(s)) break; + ++at; + continue; + } /* An overdue preempted prefill on an idle engine that still cannot be admitted will * not become admissible by waiting: nothing is running to finish and free the pool. * Fail it (spec ยง17) and ask the next request, instead of parking the step loop. Not diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index bed70235..7fcaa39b 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -1996,6 +1996,49 @@ TEST(a_preempted_prefill_falls_back_to_a_running_victim_after_the_wait) { f.commit_plain(); } +/* An overdue preempted prefill the pool still refuses keeps its place: nothing queued behind it is + * admitted in that step, since the room the overdue path makes is for it. Asking the next request + * instead let a request just forced off for it come straight back (its prompt published, so its + * recompute needed almost nothing) and take that room again: under KV pressure the same victim was + * forced off and re-admitted 26 times in 16 s while the overdue request waited. */ +TEST(an_overdue_preempted_prefill_keeps_its_place_ahead_of_the_queue) { + Fixture f; + f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */ + f.cfg.preempt_wait_ms = 0; /* overdue from the next step */ + 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); + f.commit_plain(); + CHECK(f.sch.step() != nullptr); + REQUIRE_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + f.commit_plain(); + + /* B is overdue and does not fit beside A. C, queued behind it, would fit; it must wait. */ + uint64_t c = f.submit(16, 4, /*priority=*/0); + for (int i = 0; i < 3; ++i) { + REQUIRE(f.sch.step() != nullptr); + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + CHECK_EQ((int)f.sch.find(c)->state, (int)ReqState::Waiting); + f.commit_plain(); + } + /* Once A finishes, B goes in first. */ + for (int i = 0; i < 80 && f.sch.find(a) && f.sch.find(a)->state != ReqState::Finished; ++i) { + if (!f.sch.step()) break; + f.commit_plain(); + } + bool b_first = false; + for (int i = 0; i < 4; ++i) { + if (!f.sch.step()) continue; + const ReqState bs = f.sch.find(b)->state, cs = f.sch.find(c)->state; + if (bs == ReqState::Running) { b_first = true; f.commit_plain(); break; } + CHECK(cs != ReqState::Running); + f.commit_plain(); + } + CHECK(b_first); +} + /* A lower-priority overdue prefill may stay queued while a higher-priority runner keeps its * blocks. Forcing that runner off just swaps who is at zero. The priority-5 request must not be * the one left in Preempted while someone else still runs. */