From e668d493d902b9556cd49ae252ffca24285ec260 Mon Sep 17 00:00:00 2001 From: Bakon Date: Wed, 28 Oct 2026 19:09:56 -0500 Subject: [PATCH] sched: gate preempted prefill on whole recompute plus running headroom --- core/mem/kv.cpp | 30 +++++++++++++++++++++++++++ core/mem/kv.h | 8 ++++++-- core/sched/mem_iface.h | 9 ++++++++ core/sched/scheduler.cpp | 42 ++++++++++++++++++++++++++++++++++++++ core/sched/scheduler.h | 7 +++++++ tests/sched_fakes.h | 18 ++++++++++++++++ tests/sched_test.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 156 insertions(+), 2 deletions(-) diff --git a/core/mem/kv.cpp b/core/mem/kv.cpp index 19346e7e..2841afad 100644 --- a/core/mem/kv.cpp +++ b/core/mem/kv.cpp @@ -988,6 +988,36 @@ bool KVManager::can_ever_fit(int64_t n_tokens) const { return true; } +/* Read-only counterpart of ensure(): can `seq` grow to n_tokens without taking blocks from + * another sequence? Used by the scheduler's preempted-prefill admission gate, which must not + * reserve anything or it would perturb the very pool it is trying to protect. */ +bool KVManager::can_grow(uint64_t id, int64_t n_tokens) const { + const Seq* s = find(id); + if (!s) return false; + if (n_tokens <= s->n_tokens) return true; + for (size_t g = 0; g < plans_.size(); ++g) { + const auto& p = plans_[g]; + if (p.paged()) { + const int64_t first = s->first_block_pos[g] / p.block_size; + const int64_t want = ceil_div(n_tokens, p.block_size) - first; + int64_t need = want - (int64_t)s->blocks[g].size(); + if (need < 0) need = 0; + /* ensure() takes one copy-on-write block for a partially filled block; include it + * here so the gate does not admit a sequence that then has to steal from another. */ + if (s->n_tokens > 0 && s->n_tokens % p.block_size != 0) { + const int64_t bi = s->n_tokens / p.block_size - first; + if (bi >= 0 && bi < (int64_t)s->blocks[g].size() && + refcount_[g][(size_t)s->blocks[g][(size_t)bi]] > 1) + ++need; + } + if (need > available_blocks((int32_t)g)) return false; + } else if (p.stateful() && s->state[g] < 0) { + if (free_states_[g].empty()) return false; + } + } + return true; +} + /* ------------------------------------------------------------------ sequences */ KVManager::Seq* KVManager::find(uint64_t s) { diff --git a/core/mem/kv.h b/core/mem/kv.h index 40db3715..1c7a93a6 100644 --- a/core/mem/kv.h +++ b/core/mem/kv.h @@ -451,6 +451,12 @@ public: * the request is failed rather than deadlocking the engine (spec §17). */ bool can_ever_fit(int64_t n_tokens) const; + /* Whether `seq` can grow to `n_tokens` without taking blocks from another sequence. */ + bool can_grow(uint64_t seq, int64_t n_tokens) const; + + /* Blocks a paged group can still hand out, including the lendable part. */ + int64_t available_blocks(int32_t group) const; + /* ---------------- the loan ---------------- * * The paged groups lend unheld blocks to the expert slab in place. The carve fixes and backs @@ -722,8 +728,6 @@ private: std::vector bucket_blocks_; /* per group: blocks one held_hist_ bucket covers */ std::vector> held_hist_; /* per group, per bucket: held blocks */ - int64_t available_blocks(int32_t g) const; - /* The buffer this group wants in hand, in blocks. See buffer_bytes(). */ int64_t buf_blocks(size_t i) const; /* One past the highest held block, rounded up to a bucket. */ int64_t held_top(size_t i) const; diff --git a/core/sched/mem_iface.h b/core/sched/mem_iface.h index 377decde..af5cd251 100644 --- a/core/sched/mem_iface.h +++ b/core/sched/mem_iface.h @@ -79,6 +79,13 @@ struct IKVManager { * instead of deadlocking the engine (spec §17). */ virtual bool can_ever_fit(int64_t n_tokens) const = 0; + /* Whether `seq` can be grown to `n_tokens` without taking blocks from another sequence. */ + virtual bool can_grow(uint64_t seq, int64_t n_tokens) const = 0; + + /* How many blocks a paged group can still hand out, including those lent to the expert slab + * and recallable on demand. */ + virtual int64_t available_blocks(int32_t group) const = 0; + virtual const std::vector& block_table(uint64_t seq, int32_t group) const = 0; virtual int32_t state_slot(uint64_t seq, int32_t group) const = 0; @@ -335,6 +342,7 @@ struct KVBinding final : IKVManager { } int preempt(uint64_t s) override { return m->preempt(s); } bool can_ever_fit(int64_t n) const override { return m->can_ever_fit(n); } + bool can_grow(uint64_t s, int64_t n) const override { return m->can_grow(s, n); } const std::vector& block_table(uint64_t s, int32_t g) const override { return m->block_table(s, g); } @@ -344,6 +352,7 @@ struct KVBinding final : IKVManager { } int64_t state_index_width(int32_t g) const override { return m->state_index_width(g); } int64_t free_blocks(int32_t g) const override { return m->free_blocks(g); } + int64_t available_blocks(int32_t g) const override { return m->available_blocks(g); } int64_t total_blocks(int32_t g) const override { return m->total_blocks(g); } int64_t held_blocks(int32_t g) const override { return m->held_blocks(g); } int64_t held_top_blocks(int32_t g) const override { return m->held_top_blocks(g); } diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 822b7d0a..bfb8679a 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1154,6 +1154,12 @@ void Scheduler::preempt_back() { int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { Request& r = *s.req; + /* A REQUEST THAT WAS PREEMPTED WAITS FOR ROOM FOR THE WHOLE RECOMPUTE, plus one block of + * headroom for every sequence already running. Admitting it on a shortfall is what turns + * preemption into a loop: it prefills a chunk, the pool refuses the next step, it is taken + * back to zero, and the same chunk is attempted again. */ + if (r.state == ReqState::Preempted && !preempted_can_prefill(s)) return kAdmitWait; + if (!kv_->has_sequence(r.id)) RAD_TRY(kv_->add_sequence(r.id)); /* R-SWA: the visible set becomes [0, prompt) plus a ring of W once the prompt is placed. The @@ -1422,6 +1428,41 @@ void Scheduler::undo_hit(SchedReq& s) { s.hit_attn = 0; } +/* Whether a preempted request can be admitted without becoming the next victim. The first + * requirement is the whole recompute's blocks; the second is one block per running sequence for + * the step after this one. Without that headroom, a decode step takes the block this prefill + * just reserved, the pool refuses the next chunk, and preempt_back() puts it back to zero again. */ +bool Scheduler::preempted_can_prefill(const SchedReq& s) const { + const Request& r = *s.req; + const int64_t target = s.seq_len(); + + for (size_t g = 0; g < kvgeom_.g.size(); ++g) { + const KVGeom::G& gg = kvgeom_.g[g]; + if (!gg.paged) 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; + for (int32_t slot : running_) { + const SchedReq& rs = *reqs_[(size_t)slot]; + if (!rs.req) continue; + const int64_t next = rs.req->n_computed + 1; + const int64_t rows = (next + gg.block_size - 1) / gg.block_size; + const int64_t extra = rows - (int64_t)kv_->block_table(rs.req->id, (int32_t)g).size(); + if (extra > 0) need += extra; + } + if (need > kv_->available_blocks((int32_t)g)) return false; + } + + /* Stateful groups: the recompute needs its state slots back, and every running sequence must + * keep the slot it already holds. */ + if (!kv_->can_grow(r.id, target)) return false; + for (int32_t slot : running_) { + const SchedReq& rs = *reqs_[(size_t)slot]; + if (rs.req && !kv_->can_grow(rs.req->id, rs.req->n_computed + 1)) return false; + } + return true; +} + /* ------------------------------------------------------------------ the step */ const RadBatch* Scheduler::step() { std::lock_guard lk(mu_); @@ -1713,6 +1754,7 @@ const RadBatch* Scheduler::step() { ? share_tok : 0; admit_full_ = 0; const int as = admit_one(s, budget, areserve); + if (as == kAdmitWait) { ++at; continue; } if (as < 0) break; /* head of line: do not admit behind it */ if (admit_full_ > s.sched_tokens && capped_at < 0) { capped_at = (int32_t)plan_.e.size(); diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 5f310196..864aedcc 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -764,12 +764,19 @@ private: /* Both snapshots resolve the same way; the two wrappers exist because the row types differ. */ const Request* live_req(int32_t slot, uint64_t id) const; void preempt_back(); + /* A preempted request waits until the pool can hold its WHOLE remaining recompute plus one + * block of headroom for every running sequence; otherwise admitting it now just gets it + * preempted again next step and the prefill loops. */ + bool preempted_can_prefill(const SchedReq& s) const; /* Give blocks back from the prefix cache until the pool has headroom again. */ void trim_prefix_cache(); /* RAD_OK, or a negative status when the request cannot be admitted now, nor anything behind * it. `reserve`: tokens of the step to leave for the requests queued behind this one * (--busy-prefill-chunk; busy_cap); 0 plans the chunk on the whole budget. */ int admit_one(SchedReq& s, int64_t budget, int64_t reserve = 0); + /* Positive sentinel from admit_one: the request waits (its context is still being read off + * disk, or a preempted prefill has no room yet) and the walk may ask the one behind it. */ + static constexpr int kAdmitWait = 1; /* The queue positions one step's admission took, kept to reuse the allocation. */ std::vector took_; /* A step's prefill entries while step() moves its decode entries first; reserved at init, diff --git a/tests/sched_fakes.h b/tests/sched_fakes.h index e45a27e5..1191d43b 100644 --- a/tests/sched_fakes.h +++ b/tests/sched_fakes.h @@ -262,6 +262,24 @@ struct FakeKV : IKVManager { if (geom.g[i].paged && rows_for((int)i, n) > total[i]) return false; return true; } + bool can_grow(uint64_t id, int64_t n) const override { + auto it = seqs.find(id); + if (it == seqs.end()) return false; + const Seq& q = it->second; + if (n <= q.n) return true; + for (size_t i = 0; i < gs.size(); ++i) { + if (geom.g[i].paged) { + int64_t need = rows_for((int)i, n) - (int64_t)q.blocks[i].size(); + if (need > 0 && need > (int64_t)freelist[i].size()) return false; + } else if (!geom.g[i].paged && q.state[i] < 0 && state_free[i].empty()) { + return false; + } + } + return true; + } + int64_t available_blocks(int32_t g) const override { + return g >= 0 && g < (int32_t)freelist.size() ? (int64_t)freelist[(size_t)g].size() : 0; + } const std::vector& block_table(uint64_t id, int32_t g) const override { auto it = seqs.find(id); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index e8dba134..d11d2ed5 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -1671,6 +1671,50 @@ TEST(preemption_takes_the_lowest_priority_victim_and_requeues_it_to_the_front) { } /* ================================================================== the step batch */ +/* A PREEMPTED PREFILL WAITS FOR ROOM FOR THE WHOLE RECOMPUTE plus one block per running + * sequence. Admitting it on a shortfall is what makes the prefill loop: the next decode takes + * the block it just reserved, its next chunk is refused, and preempt_back() resets it again. */ +TEST(a_preempted_prefill_waits_for_the_whole_prompt_and_running_headroom) { + Fixture f; + f.build(/*blocks=*/30, /*states=*/64); /* 30 x 16 = 480 tokens */ + CHECK_OK(f.start()); + + uint64_t a = f.submit(320, 64, /*priority=*/5); + uint64_t b = f.submit(160, 64, /*priority=*/0); + + /* A takes 256 tokens (16 blocks), B 160 (10): the pool is full. */ + CHECK(f.sch.step() != nullptr); + CHECK_EQ(f.sch.plan().e.size(), 2); + f.commit_plain(); + + /* A's next chunk cannot fit; B is the lower-priority victim and is reset. */ + CHECK(f.sch.step() != nullptr); + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + CHECK_EQ(f.sch.find(b)->n_computed, 0); + CHECK_EQ(f.sch.metrics().preemptions, 1); + f.commit_plain(); + + /* B stays in the queue while A runs; it must not be admitted into a pool that has no room + * for its whole prompt plus A's next block. */ + uint64_t q[8]; + CHECK_EQ(f.sch.queue_snapshot(q, 8), 1); + CHECK_EQ(q[0], b); + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Preempted); + + 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; B can now prefill its whole prompt. */ + CHECK(f.sch.step() != nullptr); + CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Running); + CHECK_EQ(f.sch.find(b)->n_computed, 0); + CHECK_EQ(f.sch.metrics().preemptions, 1); + f.commit_plain(); +} + TEST(a_mixed_step_has_the_right_boundaries_and_slot_mapping) { Fixture f; f.build();