data.py 5.83 KB
Newer Older
1
2
from typing import (TYPE_CHECKING, Generic, Iterable, List, Optional, Tuple,
                    Union)
3

4
from typing_extensions import NotRequired, TypedDict, TypeVar
5
6

if TYPE_CHECKING:
7
    from vllm.multimodal import MultiModalDataDict
8
9
10
11
12
13
14
15


class TextPrompt(TypedDict):
    """Schema for a text prompt."""

    prompt: str
    """The input text to be tokenized before passing to the model."""

16
    multi_modal_data: NotRequired["MultiModalDataDict"]
17
18
19
20
21
22
23
24
25
26
27
28
    """
    Optional multi-modal data to pass to the model,
    if the model supports it.
    """


class TokensPrompt(TypedDict):
    """Schema for a tokenized prompt."""

    prompt_token_ids: List[int]
    """A list of token IDs to pass to the model."""

29
    multi_modal_data: NotRequired["MultiModalDataDict"]
30
31
32
33
34
35
    """
    Optional multi-modal data to pass to the model,
    if the model supports it.
    """


36
SingletonPrompt = Union[str, TextPrompt, TokensPrompt]
37
"""
38
Set of possible schemas for a single LLM input:
39
40
41

- A text prompt (:class:`str` or :class:`TextPrompt`)
- A tokenized prompt (:class:`TokensPrompt`)
42
43
44
45
46

Note that "singleton" is as opposed to a data structure
which encapsulates multiple prompts, i.e. of the sort
which may be utilized for encoder/decoder models when
the user desires to express both the encoder & decoder
47
prompts explicitly, i.e. :class:`ExplicitEncoderDecoderPrompt`
48

49
A prompt of type :class:`SingletonPrompt` may be employed
50
51
52
53
as (1) input to a decoder-only model, (2) input to
the encoder of an encoder/decoder model, in the scenario
where the decoder-prompt is not specified explicitly, or
(3) as a member of a larger data structure encapsulating
54
more than one prompt, i.e. :class:`ExplicitEncoderDecoderPrompt`
55
56
"""

57
_T1_co = TypeVar("_T1_co",
58
59
                 bound=SingletonPrompt,
                 default=SingletonPrompt,
60
61
                 covariant=True)
_T2_co = TypeVar("_T2_co",
62
63
                 bound=SingletonPrompt,
                 default=SingletonPrompt,
64
                 covariant=True)
65

66
67
68

# TODO: Make fields ReadOnly once mypy supports it
class ExplicitEncoderDecoderPrompt(TypedDict, Generic[_T1_co, _T2_co]):
69
70
71
    """
    Represents an encoder/decoder model input prompt,
    comprising an explicit encoder prompt and a decoder prompt.
72

73
74
75
    The encoder and decoder prompts, respectively, may be formatted
    according to any of the :class:`SingletonPrompt` schemas,
    and are not required to have the same schema.
76
77
78

    Only the encoder prompt may have multi-modal data.

79
    Note that an :class:`ExplicitEncoderDecoderPrompt` may not
80
    be used as an input to a decoder-only model,
81
    and that the :code:`encoder_prompt` and :code:`decoder_prompt`
82
    fields of this data structure themselves must be
83
    :class:`SingletonPrompt` instances.
84
85
    """

86
    encoder_prompt: _T1_co
87

88
    decoder_prompt: Optional[_T2_co]
89
90


91
PromptType = Union[SingletonPrompt, ExplicitEncoderDecoderPrompt]
92
93
94
95
96
97
98
99
100
101
102
"""
Set of possible schemas for an LLM input, including
both decoder-only and encoder/decoder input types:

- A text prompt (:class:`str` or :class:`TextPrompt`)
- A tokenized prompt (:class:`TokensPrompt`)
- A single data structure containing both an encoder and a decoder prompt
  (:class:`ExplicitEncoderDecoderPrompt`)
"""


103
class LLMInputs(TypedDict):
104
105
106
    """
    The inputs in :class:`~vllm.LLMEngine` before they are
    passed to the model executor.
107
108

    This specifies the data required for decoder-only models.
109
    """
110
    prompt_token_ids: List[int]
111
112
    """The token IDs of the prompt."""

113
    prompt: NotRequired[Optional[str]]
114
115
116
117
    """
    The original prompt text corresponding to the token IDs, if available.
    """

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    multi_modal_data: NotRequired[Optional["MultiModalDataDict"]]
    """
    Optional multi-modal data to pass to the model,
    if the model supports it.
    """


class EncoderDecoderLLMInputs(LLMInputs):
    """
    The inputs in :class:`~vllm.LLMEngine` before they are
    passed to the model executor.

    This specifies the required data for encoder-decoder models.
    """
    encoder_prompt_token_ids: List[int]
133
134
135
136
137
138
139
140
    """The token IDs of the encoder prompt."""

    encoder_prompt: NotRequired[Optional[str]]
    """
    The original encoder prompt text corresponding to the token IDs, if
    available.
    """

141
142
143
144
145
146
    encoder_multi_modal_data: NotRequired[Optional["MultiModalDataDict"]]
    """
    Optional multi-modal data to pass to the encoder model,
    if the model supports it.
    """

147

148
149
_T1 = TypeVar("_T1", bound=SingletonPrompt, default=SingletonPrompt)
_T2 = TypeVar("_T2", bound=SingletonPrompt, default=SingletonPrompt)
150
151


152
153
154
155
156
157
158
159
160
161
162
163
def build_explicit_enc_dec_prompt(
    encoder_prompt: _T1,
    decoder_prompt: Optional[_T2],
) -> ExplicitEncoderDecoderPrompt[_T1, _T2]:
    return ExplicitEncoderDecoderPrompt(encoder_prompt=encoder_prompt,
                                        decoder_prompt=decoder_prompt)


def zip_enc_dec_prompts(
    enc_prompts: Iterable[_T1],
    dec_prompts: Iterable[Optional[_T2]],
) -> List[ExplicitEncoderDecoderPrompt[_T1, _T2]]:
164
    """
165
166
    Zip encoder and decoder prompts together into a list of
    :class:`ExplicitEncoderDecoderPrompt` instances.
167
    """
168
169
170
171
172
    return [
        build_explicit_enc_dec_prompt(encoder_prompt, decoder_prompt)
        for (encoder_prompt, decoder_prompt) in zip(enc_prompts, dec_prompts)
    ]

173

174
175
176
177
178
179
def to_enc_dec_tuple_list(
    enc_dec_prompts: Iterable[ExplicitEncoderDecoderPrompt[_T1, _T2]],
) -> List[Tuple[_T1, Optional[_T2]]]:
    return [(enc_dec_prompt["encoder_prompt"],
             enc_dec_prompt["decoder_prompt"])
            for enc_dec_prompt in enc_dec_prompts]
180
181
182
183
184
185
186
187
188
189
190
191
192
193


def __getattr__(name: str):
    if name == "PromptInput":
        import warnings

        msg = ("PromptInput has been renamed to PromptType. "
               "The original name will be removed in an upcoming version.")

        warnings.warn(DeprecationWarning(msg), stacklevel=2)

        return PromptType

    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")