Repository navigation
Conversation
…ntext admission The KV cache V2 scheduler admits pending context requests in arrival order, and each takes as much of the per-rank max_num_tokens budget as it can. A started long prompt (hundreds of thousands of tokens) therefore takes the whole budget every iteration until it finishes, and short prompts queued on the same rank wait for all of its chunks, which dominates their TTFT tail on long-context multi-turn workloads. With TLLM_CTX_SHORTEST_FIRST=1, pending context requests are ordered by their estimated uncached tokens: a started request uses its remaining length; a new one uses its prompt length minus the reusable prefix from probe_context_reuse, probed once per request. A request that gets no tokens for TLLM_CTX_SHORTEST_FIRST_MAX_WAITS consecutive iterations (default 8) moves to the front, and a long prompt still takes whatever budget the short requests leave in every iteration, so it cannot starve. Default behaviour is unchanged. Signed-off-by: Iman Tabrizian <[email protected]>
|
Thanks for this. We've been working on the same problem: long uncached prefills blocking short follow-up turns on multi-turn agentic disaggregated serving. The same notes apply to #20600, which reorders context requests too. Context-admission ordering is no longer part of #20736, to avoid a third overlapping change. Two notes from validating a similar ordering on GB300: 1. Reordering + attention DP + SWA scratch reuse (DeepSeek-V4) can corrupt the sliding-window page table Under attention DP, a scheduling pass can skip the forward on every rank. This happens when one rank has an empty batch and can't allocate its padding dummy. By then the context requests scheduled in that pass have already been resized to If the next pass reorders, for example a newly arrived short request sorts ahead, the long request gets a shorter chunk than the capacity it already holds. With We hit this on DeepSeek-V4 Pro with attention-DP context servers at concurrency 240–308. FCFS never triggers it, because a replayed chunk always equals the grown capacity. Any order that can change between passes does trigger it, and shortest-first is one of them. The fix is scheduler-only and independent of the admission order. A context request whose capacity has grown past its position runs exactly that chunk, or waits for a pass with enough budget. Such requests are also placed first, after any reordering, so they replay the skipped pass before anyone else takes the budget. It is a no-op unless SWA scratch reuse is on and a forward was skipped. With it, our reordering runs went clean for the full hour; without it, they crashed within minutes. The bug and the fix are reproduced in a unit test with the real diff (scheduler_v2.py)@@ class KVCacheV2Scheduler(RequestScheduler): __init__
+ # Pre-grown context capacity only matters with SWA scratch reuse.
+ self._swa_scratch_reuse = (
+ getattr(kv_cache_manager, "enable_swa_scratch_reuse", False) is True
+ )
+
+ def _pregrown_context_tokens(self, req: LlmRequest) -> int:
+ """KV capacity a context request already holds past its position.
+
+ Under attention DP a pass can skip the forward on every rank while the
+ context requests scheduled in it stay resized (resize never shrinks).
+ With SWA scratch reuse, a shorter chunk afterwards leaves blocks of the
+ next chunk's window without pages, so the request must run exactly the
+ chunk it was grown for. Returns 0 when nothing is pre-grown.
+ """
+ if not self._swa_scratch_reuse:
+ return 0
+ kv_cache = self.kv_cache_manager.kv_cache_map.get(req.py_request_id)
+ if kv_cache is None or not kv_cache.is_active:
+ return 0
+ grown = (
+ kv_cache.capacity
+ - self.kv_cache_manager.num_extra_kv_tokens
+ - req.context_current_position
+ )
+ return max(0, min(grown, req.context_remaining_length))
+
+ def _pregrown_first(self, pending_ctx: RequestList) -> RequestList:
+ """Stable partition: context requests with pre-grown capacity first."""
+ pregrown, rest = [], []
+ for req in pending_ctx:
+ if (
+ req.state_value == self._context_init_state_value
+ and self._pregrown_context_tokens(req) > 0
+ ):
+ pregrown.append(req)
+ else:
+ rest.append(req)
+ return pregrown + rest if pregrown else pending_ctx
@@ def _schedule_... (after any reordering of pending_ctx, before phase-2 admission)
+ if self._swa_scratch_reuse:
+ pending_ctx = self._pregrown_first(pending_ctx)
+
for req in pending_ctx:
if budget.requests_full:
break
@@ def _try_schedule_context_chunked(...) (after rounding chunk_size to chunk_unit_size)
+ # Run exactly the pre-grown chunk, or wait for a pass with enough budget.
+ pregrown = self._pregrown_context_tokens(req)
+ if pregrown > 0:
+ if chunk_size < pregrown:
+ return ScheduleAction.SKIP, 0, False
+ chunk_size = pregrown
+
if chunk_size <= 0:2. Starvation bound in iterations Eight iterations is about 30 s when context steps take 3–4 s, but under a second when they take tens of ms, and there the policy mostly falls back to FCFS. A wall-clock bound behaves more predictably across loads. One way is to convert seconds to iterations through an EMA of the step time, taken from rank 0's clock under TP/CP so all ranks agree. Two smaller points:
|
Description
The KV cache V2 scheduler (
KVCacheV2Scheduler) admits pending context requests in arrival order, and each request takes as much of the per-rankmax_num_tokensbudget as it can. On long-context multi-turn workloads, one started long prompt (hundreds of thousands of tokens) therefore takes the whole budget every iteration until all of its chunks finish. Short prompts queued on the same rank wait behind every one of those chunks, and that wait dominates their TTFT tail.ContextChunkingPolicy.EQUAL_PROGRESSdoes not change this, because the V2 scheduler only distinguishesFORCE_CHUNK.This PR adds an opt-in admission order, enabled with
TLLM_CTX_SHORTEST_FIRST=1. Pending context requests are sorted by their estimated uncached tokens before phase-2 admission:context_remaining_length.prompt_lenminus the reusable prefix fromKVCacheManagerV2.probe_context_reuse, probed once per request. Before it claims its cache,context_remaining_lengthstill counts the reusable prefix, so a mostly-cached follow-up turn would otherwise sort as long.TLLM_CTX_SHORTEST_FIRST_MAX_WAITSconsecutive iterations (default 8) moves to the front. A long prompt also keeps whatever budget the short requests leave in every iteration.The per-request state lives in two scheduler-owned dicts keyed by request id, rebuilt from the pending set each iteration. The default behaviour is unchanged.
Measured on a long-context multi-turn serving benchmark: one DEP4 context worker, seven TP4 generation workers, 66 concurrent conversations, prompts up to about 700k tokens. With the flag on, TTFT p90 went from 7.2 s to 3.3 s and p95 from 10.8 s to 4.7 s, with throughput up 6% (900 s window). Over a 3600 s window, p90 was 3.4 s.
Test Coverage
tests/unittest/_torch/executor/kv_cache/test_kv_cache_v2_scheduler.py::TestContextShortestFirst:test_default_admits_in_arrival_order: with the flag unset, a started long prompt still takes the whole budget.test_short_request_admitted_ahead_of_started_long_one: the short prompt is admitted in full, and the long one gets the remaining budget rounded to the chunk unit.test_reusable_prefix_is_not_counted: a mostly-cached request sorts ahead of a shorter uncached one.test_starved_request_moves_to_front: afterMAX_WAITSiterations without tokens, the long prompt is admitted first.PR Checklist