sched: gate preempted prefill on whole recompute plus running headroom

This commit is contained in:
Bakon authored and StillDeadcode committed 2026-10-29 13:16:07 +01:00
1 parent 4a61d7296b
commit e668d493d9
7 files changed
+156 -2

No files matched your search

+18
View File
@@ -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<int32_t>& block_table(uint64_t id, int32_t g) const override {
auto it = seqs.find(id);
+44
View File
@@ -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();