Skip to content

vllm.v1.core.sched.output

Classes:

CachedRequestData dataclass

Source code in vllm/v1/core/sched/output.py
@dataclass
class CachedRequestData:
    req_ids: list[str]
    # NOTE(woosuk): new_token_ids is only used for pipeline parallelism.
    # When PP is not used, new_token_ids will be empty.
    new_token_ids: list[list[int]]
    # Blocks to append to the request's existing block IDs.
    new_block_ids: list[tuple[list[int], ...] | None]
    num_computed_tokens: list[int]
    num_output_tokens: list[int]

    # Version of dataclass repr with token IDs obfuscated.
    def anon_repr(self) -> str:
        new_token_ids_lens = [len(toks) for toks in self.new_token_ids]
        return (
            f"CachedRequestData("
            f"req_ids={self.req_ids},"
            f"new_token_ids_lens={new_token_ids_lens},"
            f"new_block_ids={self.new_block_ids},"
            f"num_computed_tokens={self.num_computed_tokens},"
            f"num_output_tokens={self.num_output_tokens}"
            f")"
        )

    def __repr__(self) -> str:
        return self.anon_repr()

    @property
    def num_reqs(self) -> int:
        return len(self.req_ids)

    @cached_property
    def _req_id_to_num_output_tokens(self) -> dict[str, int]:
        """Cache mapping of req_id to num_output_tokens for O(1) lookup.

        This cached property is safe because CachedRequestData instances
        are created fresh each scheduling iteration and not mutated during
        computation of iteration details.
        """
        return dict(zip(self.req_ids, self.num_output_tokens))

    def is_context_phase(self, req_id: str) -> bool:
        num_output_tokens = self._req_id_to_num_output_tokens.get(req_id)
        return num_output_tokens is not None and num_output_tokens == 0

    @classmethod
    def make_empty(cls) -> "CachedRequestData":
        return cls(
            req_ids=[],
            new_token_ids=[],
            new_block_ids=[],
            num_computed_tokens=[],
            num_output_tokens=[],
        )

_req_id_to_num_output_tokens cached property

Cache mapping of req_id to num_output_tokens for O(1) lookup.

This cached property is safe because CachedRequestData instances are created fresh each scheduling iteration and not mutated during computation of iteration details.

IterationDetails dataclass

Scheduled prefill/decode request and input counts.

Source code in vllm/v1/core/sched/output.py
@dataclass(slots=True)
class IterationDetails:
    """Scheduled prefill/decode request and input counts."""

    num_prefill_requests: int = 0
    num_prefill_tokens: int = 0
    num_decode_requests: int = 0
    num_decode_tokens: int = 0
    num_encoder_inputs: int = 0
    num_encoder_output_tokens: int = 0

    # Temporary aliases for existing EngineCore and worker profiler consumers.
    @property
    def num_ctx_requests(self) -> int:
        return self.num_prefill_requests

    @property
    def num_ctx_tokens(self) -> int:
        return self.num_prefill_tokens

    @property
    def num_generation_requests(self) -> int:
        return self.num_decode_requests

    @property
    def num_generation_tokens(self) -> int:
        return self.num_decode_tokens

KVConnectorBlockState dataclass

Scheduler-local block state offered to a producer-side KV connector.

Source code in vllm/v1/core/sched/output.py
@dataclass(slots=True)
class KVConnectorBlockState:
    """Scheduler-local block state offered to a producer-side KV connector."""

    # Requests scheduled this step and requests with a boundary-state hand-off.
    req_ids: set[str]
    # Resolve on access to avoid copying tables the connector never reads.
    resolve_block_ids: Callable[[str], tuple[list[int], ...]]
    # Exact Mamba boundary-state hand-offs.
    boundary_state_offloads: dict[str, list[tuple[int, int, int]]]

    def get_block_ids(self, req_id: str) -> tuple[list[int], ...] | None:
        if req_id not in self.req_ids:
            return None
        return self.resolve_block_ids(req_id)

SchedulerOutput dataclass

Methods:

Source code in vllm/v1/core/sched/output.py
@dataclass
class SchedulerOutput:
    # list of the requests that are scheduled for the first time.
    # We cache the request's data in each worker process, so that we don't
    # need to re-send it every scheduling step.
    scheduled_new_reqs: list[NewRequestData]
    # list of the requests that have been scheduled before.
    # Since the request's data is already cached in the worker processes,
    # we only send the diff to minimize the communication cost.
    scheduled_cached_reqs: CachedRequestData

    # req_id -> num_scheduled_tokens
    # Number of tokens scheduled for each request.
    num_scheduled_tokens: dict[str, int]
    # Total number of tokens scheduled for all requests.
    # Equal to sum(num_scheduled_tokens.values())
    total_num_scheduled_tokens: int
    # req_id -> spec_token_ids
    # If a request does not have any spec decode tokens, it will not be
    # included in the dictionary.
    scheduled_spec_decode_tokens: dict[str, list[int]]
    # req_id -> encoder input indices that need processing.
    # E.g., if a request has [0, 1], it could mean the vision encoder needs
    # to process that the request's 0-th and 1-th images in the current step.
    scheduled_encoder_inputs: dict[str, list[int]]

    # Request IDs that are finished in between the previous and the current
    # steps. This is used to notify the workers about the finished requests
    # so that they can free the cached states for those requests.
    finished_req_ids: set[str]
    # list of mm_hash strings associated with the encoder outputs to be
    # freed from the encoder cache.
    free_encoder_mm_hashes: list[str]

    # Request-local CPU payload updates: None releases a consumed input; a
    # payload restores it for re-encoding after KV-load rollback. GPU embeddings
    # and the feature's identifier/placeholder metadata are unaffected.
    encoder_input_updates: dict[str, dict[int, "MultiModalKwargsItem | None"]] = field(
        default_factory=dict
    )

    # Optional scheduling-time snapshot, before worker-side token adjustments.
    iteration_details: IterationDetails | None = None

    # Request IDs that are preempted in this step.
    # Only used for v2 model runner.
    preempted_req_ids: set[str] | None = None

    # Whether any of the scheduled requests use structured output.
    # Set only in async scheduling case.
    has_structured_output_requests: bool = False

    # Whether any scheduled request uses structured output with speculative
    # decoding. Set only in async scheduling case.
    has_structured_output_spec_decode: bool = False

    # Whether the scheduled requests have all the output tokens they
    # need to perform grammar bitmask computation.
    pending_structured_output_tokens: bool = False

    # KV Cache Connector metadata.
    kv_connector_metadata: KVConnectorMetadata | None = None

    # Whether any scheduled request consumes KV that the connector loads
    # synchronously during this step (load_async=False).
    has_sync_kv_loads: bool = False

    # Execution-auxiliary output control metadata consumed by the worker connector.
    aux_output_connector_metadata: AuxOutputConnectorMetadata | None = None

    # EC Cache Connector metadata
    ec_connector_metadata: ECConnectorMetadata | None = None
    # Block IDs freshly allocated from the pool during this scheduling step.
    # The worker zeros the corresponding GPU memory before the blocks are used,
    # preventing stale NaN/data from corrupting attention or SSM computation.
    new_block_ids_to_zero: list[int] | None = None

    # CoW copies to apply after zeroing new blocks and before forward.
    kv_cache_block_copies: list[KVCacheBlockCopy] | None = None

    # Scheduler-local; always None by the time this reaches a worker.
    kv_connector_block_state: KVConnectorBlockState | None = None

    # Dynamic speculative decoding: optimal K chosen by scheduler.
    # Number of spec tokens to schedule for the next step.
    num_spec_tokens_to_schedule: int = 0

    # Whether no scheduled request can use the drafts proposed after this step
    # (non-final prefill chunks, or requests sure to reach max_tokens), so the
    # drafter can skip proposing them.
    skip_drafts: bool = False

    @classmethod
    def make_empty(cls) -> "SchedulerOutput":
        return cls(
            scheduled_new_reqs=[],
            scheduled_cached_reqs=CachedRequestData.make_empty(),
            num_scheduled_tokens={},
            total_num_scheduled_tokens=0,
            scheduled_spec_decode_tokens={},
            scheduled_encoder_inputs={},
            finished_req_ids=set(),
            free_encoder_mm_hashes=[],
        )

    def shares_requests_with(self, other: "SchedulerOutput") -> bool:
        """Whether any request is scheduled in both this step and `other`."""
        return not self.num_scheduled_tokens.keys().isdisjoint(
            other.num_scheduled_tokens
        )

shares_requests_with(other)

Whether any request is scheduled in both this step and other.

Source code in vllm/v1/core/sched/output.py
def shares_requests_with(self, other: "SchedulerOutput") -> bool:
    """Whether any request is scheduled in both this step and `other`."""
    return not self.num_scheduled_tokens.keys().isdisjoint(
        other.num_scheduled_tokens
    )