diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index bfb8679a..75280b9b 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1448,7 +1448,8 @@ bool Scheduler::preempted_can_prefill(const SchedReq& s) const { 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; + need += 1; /* the step after this one keeps its block */ + if (extra > 0) need += extra; /* and the block its next token already needs */ } if (need > kv_->available_blocks((int32_t)g)) return false; } diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index d11d2ed5..92aee63d 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -1676,24 +1676,30 @@ TEST(preemption_takes_the_lowest_priority_victim_and_requeues_it_to_the_front) { * 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 */ + f.build(/*blocks=*/26, /*states=*/64); /* 26 x 16 = 416 tokens */ CHECK_OK(f.start()); - uint64_t a = f.submit(320, 64, /*priority=*/5); - uint64_t b = f.submit(160, 64, /*priority=*/0); + uint64_t a = f.submit(287, 64, /*priority=*/5); + uint64_t b = f.submit(128, 64, /*priority=*/0); - /* A takes 256 tokens (16 blocks), B 160 (10): the pool is full. */ + /* A takes 287 tokens (18 blocks), B 128 (8): 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. */ + /* B's next token needs a new block; it is the lowest-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(); + /* One block the fake pool has free and no sequence holds: the victim's own blocks plus one. + * The gate still wants one block of headroom beside the running A. */ + f.kv.total[0] += 1; + f.kv.refcnt[0].push_back(0); + f.kv.freelist[0].push_back(26); + /* 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]; @@ -1703,6 +1709,8 @@ TEST(a_preempted_prefill_waits_for_the_whole_prompt_and_running_headroom) { for (int i = 0; i < 80 && f.sch.find(a)->state != ReqState::Finished; ++i) { 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); f.commit_plain(); } CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Finished);