|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from vllm.lora.request import LoRARequest |
| 8 | + from vllm.multimodal import MultiModalKwargs |
| 9 | + from vllm.multimodal.base import PlaceholderRange |
| 10 | + from vllm.sampling_params import SamplingParams |
| 11 | + from vllm.v1.request import Request |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class NewRequestData: |
| 16 | + |
| 17 | + req_id: str |
| 18 | + prompt_token_ids: List[int] |
| 19 | + prompt: Optional[str] |
| 20 | + mm_inputs: List["MultiModalKwargs"] |
| 21 | + mm_hashes: List[str] |
| 22 | + mm_positions: List["PlaceholderRange"] |
| 23 | + sampling_params: "SamplingParams" |
| 24 | + block_ids: List[int] |
| 25 | + num_computed_tokens: int |
| 26 | + lora_request: Optional["LoRARequest"] |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def from_request( |
| 30 | + cls, |
| 31 | + request: "Request", |
| 32 | + block_ids: List[int], |
| 33 | + num_computed_tokens: int, |
| 34 | + ) -> "NewRequestData": |
| 35 | + return cls( |
| 36 | + req_id=request.request_id, |
| 37 | + prompt_token_ids=request.prompt_token_ids, |
| 38 | + prompt=request.prompt, |
| 39 | + mm_inputs=request.mm_inputs, |
| 40 | + mm_hashes=request.mm_hashes, |
| 41 | + mm_positions=request.mm_positions, |
| 42 | + sampling_params=request.sampling_params, |
| 43 | + block_ids=block_ids, |
| 44 | + num_computed_tokens=num_computed_tokens, |
| 45 | + lora_request=request.lora_request, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class CachedRequestData: |
| 51 | + |
| 52 | + req_id: str |
| 53 | + # If resumed_from_preemption is False, new_block_ids will be appended to |
| 54 | + # the request's block IDs. If True, new_block_ids will be used as the |
| 55 | + # request's block IDs instead of appending to the existing block IDs. |
| 56 | + resumed_from_preemption: bool |
| 57 | + new_block_ids: List[int] |
| 58 | + num_computed_tokens: int |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def from_request( |
| 62 | + cls, |
| 63 | + request: "Request", |
| 64 | + resumed_from_preemption: bool, |
| 65 | + new_block_ids: List[int], |
| 66 | + num_computed_tokens: int, |
| 67 | + ) -> "CachedRequestData": |
| 68 | + return cls( |
| 69 | + req_id=request.request_id, |
| 70 | + resumed_from_preemption=resumed_from_preemption, |
| 71 | + new_block_ids=new_block_ids, |
| 72 | + num_computed_tokens=num_computed_tokens, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class SchedulerOutput: |
| 78 | + |
| 79 | + # List of the requests that are scheduled for the first time. |
| 80 | + # We cache the request's data in each worker process, so that we don't |
| 81 | + # need to re-send it every scheduling step. |
| 82 | + scheduled_new_reqs: List[NewRequestData] |
| 83 | + # List of the requests that have been scheduled before. |
| 84 | + # Since the request's data is already cached in the worker processes, |
| 85 | + # we only send the diff to minimize the communication cost. |
| 86 | + scheduled_cached_reqs: List[CachedRequestData] |
| 87 | + |
| 88 | + # req_id -> num_scheduled_tokens |
| 89 | + # Number of tokens scheduled for each request. |
| 90 | + num_scheduled_tokens: Dict[str, int] |
| 91 | + # Total number of tokens scheduled for all requests. |
| 92 | + # Equal to sum(num_scheduled_tokens.values()) |
| 93 | + total_num_scheduled_tokens: int |
| 94 | + # req_id -> encoder input indices that need processing. |
| 95 | + # E.g., if a request has [0, 1], it could mean the vision encoder needs |
| 96 | + # to process that the request's 0-th and 1-th images in the current step. |
| 97 | + scheduled_encoder_inputs: Dict[str, List[int]] |
| 98 | + # Number of common prefix blocks for all requests. |
| 99 | + # This can be used for cascade attention. |
| 100 | + num_common_prefix_blocks: int |
| 101 | + |
| 102 | + # Request IDs that are finished in between the previous and the current |
| 103 | + # steps. This is used to notify the workers about the finished requests |
| 104 | + # so that they can free the cached states for those requests. |
| 105 | + finished_req_ids: Set[str] |
| 106 | + # List of (req_id, encoder_input_index) tuples. |
| 107 | + # Used to free the encoder cache. |
| 108 | + free_encoder_input_ids: List[Tuple[str, int]] |
0 commit comments