mirror of
https://github.com/lightseekorg/tokenspeed.git
synced 2026-12-04 10:26:50 +08:00
perf(scheduler): reuse forward resources during decode admission
Resolve forward resources once per decode admission and reuse the reference for the permit check, block tables, and cache progress. Keep the reference within the admission attempt so it cannot cross a request state transition. Document the reference lifetime and cover decode page growth, reserve updates, and handing execution capacity to a waiting request. Signed-off-by: sunshengqi <69040952+simple-sun@users.noreply.github.com>
This commit is contained in:
@@ -35,6 +35,10 @@ capacity failure. Only a failed KV admission enters the existing capacity
|
||||
recovery policy. The P role hands execution to its peer and does not consume
|
||||
a local decode permit.
|
||||
|
||||
Forward resources belong to the request's current FSM state. Admission may
|
||||
reuse a reference to those resources within one attempt; the reference must
|
||||
not cross a state transition or outlive the request.
|
||||
|
||||
## 1. Admission is per chunk
|
||||
|
||||
An optional `--chunk-prefill-kv-size` also bounds the KV read by a local
|
||||
|
||||
@@ -978,13 +978,14 @@ std::optional<fsm::SchedulePrefillEvent> Scheduler::schedulePrefill(
|
||||
std::optional<fsm::ScheduleDecodeEvent> Scheduler::scheduleDecode(ExecutionPlan& plan, AdmissionFeedback& feedback,
|
||||
Request* request) {
|
||||
DecodeSlotIndex decode_slot;
|
||||
if (config_.role != Role::kP && !request->HasDecodeSlot()) {
|
||||
fsm::ForwardResources& resources = request->ForwardResourcesRef();
|
||||
if (config_.role != Role::kP && !resources.decode_slot.valid()) {
|
||||
if (decode_slots_.AvailableSlots() == 0) {
|
||||
return std::nullopt; // Execution admission, not a KV capacity failure.
|
||||
}
|
||||
decode_slot = decode_slots_.Allocate();
|
||||
}
|
||||
std::vector<BlockTable>& tables = request->BlockTablesRef();
|
||||
std::vector<BlockTable>& tables = resources.block_tables;
|
||||
const std::int32_t num_computed_tokens = request->NumComputedTokens();
|
||||
std::int32_t reserve_tokens = request->ReserveNumTokensInNextScheduleEvent();
|
||||
if (config_.decode_input_tokens > 1) {
|
||||
@@ -1034,7 +1035,7 @@ std::optional<fsm::ScheduleDecodeEvent> Scheduler::scheduleDecode(ExecutionPlan&
|
||||
// copy-in block for it, so it is skipped even if that checkpoint has left
|
||||
// the cache since.
|
||||
const bool first_decode = request->Is<fsm::PrefillDone>();
|
||||
fsm::CacheProgress& cache_progress = request->CacheProgressRef();
|
||||
fsm::CacheProgress& cache_progress = resources.cache_progress;
|
||||
const bool prompt_end = first_decode && firstDecodePublishesPromptEnd(*request, cache_progress.resume_tokens);
|
||||
const StateStep state_step{prompt_end ? StateStep::Kind::kRotate
|
||||
: first_decode && config_.role == Role::kD ? StateStep::Kind::kKeep
|
||||
|
||||
@@ -297,6 +297,9 @@ public:
|
||||
|
||||
bool HasDecodeSlot() const { return forwardResources("HasDecodeSlot").decode_slot.valid(); }
|
||||
|
||||
// The reference must not outlive the request or cross Request::Apply.
|
||||
fsm::ForwardResources& ForwardResourcesRef() { return forwardResources("ForwardResourcesRef"); }
|
||||
|
||||
const std::vector<BlockTable>& BlockTablesRef() const { return forwardResources("BlockTablesRef").block_tables; }
|
||||
|
||||
std::vector<BlockTable>& BlockTablesRef() { return forwardResources("BlockTablesRef").block_tables; }
|
||||
|
||||
@@ -127,6 +127,55 @@ def test_late_older_prefills_do_not_displace_streaming_requests():
|
||||
assert _forward_ids(scheduler.next_execution_plan()) == ["a", "d"]
|
||||
|
||||
|
||||
def test_decode_page_growth_keeps_its_slot_and_hands_capacity_to_a_waiter():
|
||||
scheduler = ts.Scheduler(_config(1, 2))
|
||||
_submit(scheduler, "running", "waiting")
|
||||
slots = _admit_all(scheduler, ("running", "waiting"))
|
||||
_ready(scheduler, "running", "waiting")
|
||||
first = _find_forward_op(scheduler.next_execution_plan())
|
||||
assert list(first.request_ids) == ["running"]
|
||||
assert list(first.request_pool_indices) == [slots[0]]
|
||||
pages = [int(page) for page in first.block_tables["history"][0] if page > 0]
|
||||
_advance(scheduler, "running", [43])
|
||||
|
||||
for token, reserve in enumerate((1, 6, 1, 1), start=44):
|
||||
_send(
|
||||
scheduler,
|
||||
_event(
|
||||
"UpdateReserveNumTokens",
|
||||
"running",
|
||||
reserve_num_tokens_in_next_schedule_event=reserve,
|
||||
),
|
||||
)
|
||||
plan = scheduler.next_execution_plan()
|
||||
op = _find_forward_op(plan)
|
||||
assert list(op.request_ids) == ["running"]
|
||||
assert list(op.request_pool_indices) == [slots[0]]
|
||||
next_pages = [int(page) for page in op.block_tables["history"][0] if page > 0]
|
||||
assert next_pages[: len(pages)] == pages
|
||||
if reserve == 6:
|
||||
assert len(next_pages) > len(pages)
|
||||
assert scheduler.active_decode_slots() == 1
|
||||
assert scheduler.allocated_request_slots() == 2
|
||||
assert scheduler.ready_waiting_size() == 1
|
||||
assert not plan.aborts
|
||||
pages = next_pages
|
||||
_advance(scheduler, "running", [token])
|
||||
|
||||
_finish(scheduler, "running")
|
||||
next_op = _find_forward_op(scheduler.next_execution_plan())
|
||||
assert list(next_op.request_ids) == ["waiting"]
|
||||
assert list(next_op.request_pool_indices) == [slots[1]]
|
||||
assert scheduler.active_decode_slots() == 1
|
||||
assert scheduler.allocated_request_slots() == 1
|
||||
assert scheduler.ready_waiting_size() == 0
|
||||
_advance(scheduler, "waiting", [99])
|
||||
_finish(scheduler, "waiting")
|
||||
assert _forward_ids(scheduler.next_execution_plan()) == []
|
||||
assert scheduler.active_decode_slots() == 0
|
||||
assert scheduler.allocated_request_slots() == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("terminal", ["Finish", "Abort"])
|
||||
def test_terminal_request_keeps_its_permit_until_overlap_results_land(terminal):
|
||||
scheduler = ts.Scheduler(_config(1, 2))
|
||||
|
||||
Reference in New Issue
Block a user