__init__.py 4.92 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
import enum
4
import time
5
from collections.abc import Sequence
6
from typing import Any, Optional, Union
7
8
9

import msgspec

10
11
12
13
from vllm.lora.request import LoRARequest
from vllm.multimodal import MultiModalKwargs
from vllm.multimodal.inputs import PlaceholderRange
from vllm.sampling_params import SamplingParams
14
from vllm.v1.metrics.stats import SchedulerStats
15
from vllm.v1.outputs import LogprobsLists, LogprobsTensors
16

17
18
19
# These are possible values of RequestOutput.finish_reason,
# so form part of the external API.
FINISH_REASON_STRINGS = ("stop", "length", "abort")
20

21
22

class FinishReason(enum.IntEnum):
23
24
25
    """
    Reason a request finished - stop, length, or abort.

26
27
    Int rather than Str for more compact serialization.

28
29
30
31
32
33
34
35
36
37
    stop - a stop string was emitted
    length - max_tokens was consumed, or max_model_len was reached
    abort - aborted for another reason

    """
    STOP = 0
    LENGTH = 1
    ABORT = 2

    def __str__(self):
38
        return FINISH_REASON_STRINGS[self.value]
39
40


41
42
43
44
45
class EngineCoreRequest(
        msgspec.Struct,
        array_like=True,  # type: ignore[call-arg]
        omit_defaults=True,  # type: ignore[call-arg]
        gc=False):  # type: ignore[call-arg]
46
47
48
49
50
51

    # NOTE: prompt and prompt_token_ids should be DecoderOnlyInput,
    # but this object is currently not playing well with msgspec
    # due to circular imports and typing we have in data.py

    request_id: str
52
    prompt_token_ids: list[int]
53
    mm_inputs: Optional[Sequence[Optional[MultiModalKwargs]]]
54
55
    mm_hashes: Optional[list[str]]
    mm_placeholders: Optional[list[PlaceholderRange]]
56
    sampling_params: SamplingParams
57
58
    eos_token_id: Optional[int]
    arrival_time: float
59
    lora_request: Optional[LoRARequest]
60

61
62
63
64
65
    # Used in DP case to indicate which wave of requests this is expected to
    # belong to, to cover a race condition where the request is sent before
    # a wave finished notification is received.
    current_wave: int = 0

66

67
68
69
70
class EngineCoreEventType(enum.IntEnum):
    """The type of engine core request event."""
    QUEUED = 1
    SCHEDULED = 2
71
    PREEMPTED = 3
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91


class EngineCoreEvent(msgspec.Struct):
    """A timestamped engine core event associated with a request.

    The timestamp is a monotonic timestamps and is used for by the engine
    frontend to calculate intervals between engine core events. These
    timestamps should not be compared with timestamps from other processes.
    """
    type: EngineCoreEventType
    timestamp: float

    @classmethod
    def new_event(cls,
                  event_type: EngineCoreEventType,
                  timestamp: Optional[float] = None) -> "EngineCoreEvent":
        timestamp = time.monotonic() if timestamp is None else timestamp
        return cls(event_type, timestamp)


92
93
94
95
96
class EngineCoreOutput(
        msgspec.Struct,
        array_like=True,  # type: ignore[call-arg]
        omit_defaults=True,  # type: ignore[call-arg]
        gc=False):  # type: ignore[call-arg]
97
98

    request_id: str
99
    new_token_ids: list[int]
100
101
102
103

    new_logprobs: Optional[LogprobsLists] = None
    new_prompt_logprobs_tensors: Optional[LogprobsTensors] = None

104
    finish_reason: Optional[FinishReason] = None
105
    stop_reason: Union[int, str, None] = None
106
    events: Optional[list[EngineCoreEvent]] = None
107

108
109
110
111
    @property
    def finished(self) -> bool:
        return self.finish_reason is not None

112

113
114
115
116
117
118
119
120
121
122
123
124
class UtilityOutput(
        msgspec.Struct,
        array_like=True,  # type: ignore[call-arg]
        gc=False):  # type: ignore[call-arg]

    call_id: int

    # Non-None implies the call failed, result should be None.
    failure_message: Optional[str] = None
    result: Any = None


125
126
127
128
129
class EngineCoreOutputs(
        msgspec.Struct,
        array_like=True,  # type: ignore[call-arg]
        omit_defaults=True,  # type: ignore[call-arg]
        gc=False):  # type: ignore[call-arg]
130
131

    #NOTE(Nick): We could consider ways to make this more compact,
132
    # e.g. columnwise layout
133

134
135
    engine_index: int = 0

136
    # [num_reqs]
137
    outputs: list[EngineCoreOutput] = []
138
    scheduler_stats: Optional[SchedulerStats] = None
139
140
    timestamp: float = 0.0

141
    utility_output: Optional[UtilityOutput] = None
142
143
    finished_requests: Optional[set[str]] = None

144
145
146
147
148
149
    # In DP case, used to signal that the current wave of requests
    # has finished and the engines are paused.
    wave_complete: Optional[int] = None
    # In DP case, used to signal that a request was received for an
    # "old" wave, so the next wave needs to be started in other engines.
    start_wave: Optional[int] = None
150

151
152
153
    def __post_init__(self):
        if self.timestamp == 0.0:
            self.timestamp = time.monotonic()
154
155
156
157
158
159
160
161
162


class EngineCoreRequestType(enum.Enum):
    """
    Request types defined as hex byte strings, so it can be sent over sockets
    without separate encoding step.
    """
    ADD = b'\x00'
    ABORT = b'\x01'
163
    START_DP_WAVE = b'\x02'
164
    UTILITY = b'\x03'
165
166
    # Sentinel used within EngineCoreProc.
    EXECUTOR_FAILED = b'\x04'