vllm.v1.core.sched.scheduler
¶
Classes:
Scheduler
¶
Bases: SchedulerInterface
Methods:
-
finish_requests–Handles the finish signal from outside the scheduler.
-
get_kv_cache_usage–Returns the fraction of the KV cache currently in use (0.0-1.0).
-
get_request_counts–Returns (num_running_reqs, num_waiting_reqs).
-
reset_encoder_cache–Reset the encoder cache to invalidate all cached encoder outputs.
-
reset_prefix_cache–Reset the KV prefix cache.
_admits_inflight_kv_load()
¶
Whether a new async KV load stays within --max-num-inflight-kv-loads.
_connector_finished(request)
¶
Invoke the KV connector request_finished() method if applicable.
Returns optional kv transfer parameters to be included with the request outputs.
_drafts_unused(request, num_computed_tokens, num_new_tokens)
¶
Whether the drafts proposed for request after this step are sure to
go unused.
Drafts proposed after a step are verified in the request's next decode step. They go unused when no such step follows, in two cases:
- This step is a non-final prefill chunk. It samples no token, and the scheduler drops drafts proposed after a prefill chunk.
- This step samples the request's last output token: even if every
draft is rejected, the request reaches
max_tokensand finishes.
Stop tokens and stop strings also end requests, but they cannot be known in advance. This returns True only when it is sure.
The model runner proposes no drafts (K=0) in a step where this is True for every scheduled request on every DP rank.
Parameters:
_drain_deferred_frees()
¶
Return deferred blocks whose fence step has completed.
Fences are appended in near-monotonic order (a CoW retention fence can lead request-free fences by one step), so stop at the first pending one; any satisfied entry behind it is merely freed later.
_drain_deferred_loads()
¶
Start deferred async loads whose fence step has completed.
_ec_transfer_pending(request, num_computed_tokens)
¶
Whether an encoder input this request needs is still in transit.
_free_cow_retained_blocks(blocks, fence_seq)
¶
Release CoW copy retentions, deferring their return to the block pool while the step that runs the copy may still be in flight.
_free_request_blocks(request)
¶
Free the request's KV blocks, deferring the return to the block pool when an in-flight GPU step may still write them.
_handle_blocked_waiting_request(request)
¶
Returns True if scheduling of this request should proceed, False if it should be skipped for this step.
_handle_failed_recving(failed_req_ids)
¶
Fail closed for layouts whose block IDs are not globally unique.
_handle_invalid_blocks(invalid_block_ids, num_scheduled_tokens)
¶
_handle_stopped_request(request)
¶
Return True if finished (can be False for resumable requests).
_has_decode_awaiting_cadence()
¶
Whether a running request still needs a decode but awaits the cadence.
_inflight_prefill_reserved_blocks()
¶
Num blocks in-flight prefills still need to finish (their reservation).
_load_restores_replay_window(request)
¶
Whether the KV connector's load for request restores every DSV41
SWA bounded-replay group, as a P/D transfer of the request's own blocks
does; a prefix-cache store holds no window.
_mamba_block_aligned_split(request, num_new_tokens, num_new_local_computed_tokens=0, num_external_computed_tokens=0)
¶
Clip a prefill chunk so it ends where Mamba state must be cached.
With prefix caching, reusable SSM states are materialized at block boundaries, plus mandatory early stops (the prompt's partial-tail hash boundary, a detected shared-prefix junction). If a block is larger than the configured prefill chunk limit, intermediate chunks keep private running state until they reach the next cacheable position.
_mark_prefix_replay(request, num_hit_tokens, window_loaded)
¶
Record where a prefix hit's replay starts and return the number of hit tokens to recompute; the caller rewinds the computed count by it.
The replayed tokens are the hit's last window (see
Request.replay_start): the worker rebuilds their sliding-window KV
and leaves their cached KV alone. A hit whose KV load carries the
window replays nothing.
_min_in_flight_output_tokens(request)
¶
Min output tokens added by in-flight steps, even if all drafts are rejected. At least one, for diffusion (no bonus token per step).
_next_waiting_queue(take_async_kv)
¶
self.waiting or self.async_kv_waiting, whichever head goes first.
_preempt_request(request, timestamp, drop_stale_output=False)
¶
Preempt a request and put it back to the waiting queue.
NOTE: The request should be popped from the running queue outside of this method.
drop_stale_output: drop (rather than deliver) any in-flight output; used by reset_prefix_cache, whose same-step resume would otherwise deliver tokens out of order, and for connectors with a pending KV hand-off, which the preemption's block free would leave without valid KV.
_publish_ec_events(ec_events)
¶
Publish EC events together with the ones queued by the EC connector.
_refresh_queued_workload(request)
¶
Recount a request whose status or token counts changed while it may be in a waiting queue. The queues count what enters and leaves them.
_reject_on_missing_encoder_input(request, input_id)
¶
Fail a request whose item would be encoded here with no input.
A metadata-only item (the consumer side of EPD) can only be served from the EC connector or from transferred KV. When neither covers it, such as an EC miss or a KV consumer recomputing a failed load, scheduling a local encode would hand the model no pixels and kill the engine core.
_reorder_waiting_by_cached_prefix()
¶
Move waiting requests with a cached prefix to the front of the window.
Reordering is confined to the slots held by plain waiting requests, so a request in a blocked status or one already partially computed keeps its exact position and nothing is moved past it. That keeps this a reordering among peers rather than a change in priority, and leaves the queue beyond the window untouched.
_request_remaining_blocks(request)
¶
Blocks request still needs to hold its full sequence and be promoted.
_reserve_parked(request)
¶
Cache a parked async KV load's reservation. Set when parked and refreshed at promotion, the only points where the load's held blocks change.
_reserve_prefill_lookahead(request, num_computed_tokens, num_new_tokens)
¶
Never end a prefill chunk within num_prefill_lookahead of the prefill end.
At a chunked-prefill boundary, the multi-module MTP drafter consumes the next num_prefill_lookahead known prefill tokens as draft inputs. A boundary closer to the end than that would make it fall back to sampled drafts, permanently polluting the trailing modules' KV caches. Either finish the prefill or leave at least num_prefill_lookahead for the next chunk. No-op for eagle-family drafters (lookahead 1).
_try_schedule_encoder_inputs(request, num_computed_tokens, num_new_tokens, encoder_compute_budget, shift_computed_tokens=0)
¶
Determine which encoder inputs need to be scheduled in the current step,
and update num_new_tokens and encoder token budget accordingly.
An encoder input will be scheduled if: - Its output tokens overlap with the range of tokens being computed in this step, i.e., [num_computed_tokens, num_computed_tokens + num_new_tokens). - It is not already computed and stored in the encoder cache. - It is not exist on remote encoder cache (via ECConnector) - There is sufficient encoder token budget to process it. - The encoder cache has space to store it.
If an encoder input cannot be scheduled due to cache or budget
limitations, the method adjusts num_new_tokens to schedule only the
decoder tokens up to just before the unschedulable encoder input.
Note that num_computed_tokens includes both locally cached blocks and externally cached blocks (via KVConnector).
_update_from_kv_xfer_finished(kv_connector_output)
¶
KV Connector: update the scheduler state based on the output.
The Worker side connectors add finished_recving and finished_sending reqs to the output. * if finished_sending: free the blocks
if finished_recving: add to state so we can¶
schedule the request during the next step.
_update_request_as_session(session, update)
¶
Updates the waiting session with the next streaming update.
Discards the last sampled output token from the prior input chunk. Returns False if the update was dropped for exceeding max_model_len.
_update_requests_with_invalid_blocks(requests, invalid_block_ids, num_scheduled_tokens, evict_blocks=True)
¶
Identify and update requests affected by invalid KV cache blocks.
This method scans the given requests, detects those with invalid blocks
and adjusts their num_computed_tokens to the longest valid prefix.
For observability, it also accumulates the total number of tokens that
will need to be recomputed across all affected requests.
Parameters:
-
(requests¶Iterable[Request]) –The set of requests to scan for invalid blocks.
-
(invalid_block_ids¶set[int]) –IDs of invalid blocks.
-
(num_scheduled_tokens¶dict[str, int]) –req_id -> number of scheduled tokens.
-
(evict_blocks¶bool, default:True) –Whether to collect blocks for eviction (False for async requests which aren't cached yet).
Returns:
-
tuple(tuple[set[str], int, set[int]]) –- affected_req_ids (set[str]): IDs of requests impacted by invalid blocks.
- total_affected_tokens (int): Total number of tokens that must be recomputed across all affected requests.
- blocks_to_evict (set[int]): Block IDs to evict from cache, including invalid blocks and downstream dependent blocks.
_update_waiting_for_remote_kv(request)
¶
KV Connector: update request state after async recv is finished.
When the kv transfer is ready, we cache the blocks and the request state will be moved back to WAITING from WAITING_FOR_REMOTE_KV.
finish_requests(request_ids, finished_status)
¶
Handles the finish signal from outside the scheduler.
For example, the API server can abort a request when the client disconnects.
If request_ids is None, all requests will be finished.
Returns:
get_kv_cache_usage()
¶
Returns the fraction of the KV cache currently in use (0.0-1.0).
get_request_counts()
¶
Returns (num_running_reqs, num_waiting_reqs).
reset_encoder_cache()
¶
Reset the encoder cache to invalidate all cached encoder outputs.
This should be called when model weights are updated to ensure stale vision embeddings are not reused.
reset_prefix_cache(reset_running_requests=False, reset_connector=False)
¶
Reset the KV prefix cache.
If reset_running_requests is True, all the running requests will be preempted and moved to the waiting queue. Otherwise, this method will only reset the KV prefix cache when there is no running requests taking KV cache.