data.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from typing import (TYPE_CHECKING, Generic, Iterable, List, Optional, Tuple,
  2. Union)
  3. from typing_extensions import NotRequired, TypedDict, TypeVar
  4. if TYPE_CHECKING:
  5. from aphrodite.multimodal import MultiModalDataDict
  6. class TextPrompt(TypedDict):
  7. """Schema for a text prompt."""
  8. prompt: str
  9. """The input text to be tokenized before passing to the model."""
  10. multi_modal_data: NotRequired["MultiModalDataDict"]
  11. """
  12. Optional multi-modal data to pass to the model,
  13. if the model supports it.
  14. """
  15. class TokensPrompt(TypedDict):
  16. """Schema for a tokenized prompt."""
  17. prompt_token_ids: List[int]
  18. """A list of token IDs to pass to the model."""
  19. multi_modal_data: NotRequired["MultiModalDataDict"]
  20. """
  21. Optional multi-modal data to pass to the model,
  22. if the model supports it.
  23. """
  24. SingletonPrompt = Union[str, TextPrompt, TokensPrompt]
  25. """
  26. Set of possible schemas for a single LLM input:
  27. - A text prompt (:class:`str` or :class:`TextPrompt`)
  28. - A tokenized prompt (:class:`TokensPrompt`)
  29. Note that "singleton" is as opposed to a data structure
  30. which encapsulates multiple prompts, i.e. of the sort
  31. which may be utilized for encoder/decoder models when
  32. the user desires to express both the encoder & decoder
  33. prompts explicitly, i.e. :class:`ExplicitEncoderDecoderPrompt`
  34. A prompt of type :class:`SingletonPromptType` may be employed
  35. as (1) input to a decoder-only model, (2) input to
  36. the encoder of an encoder/decoder model, in the scenario
  37. where the decoder-prompt is not specified explicitly, or
  38. (3) as a member of a larger data structure encapsulating
  39. more than one prompt, i.e. :class:`ExplicitEncoderDecoderPrompt`
  40. """
  41. _T1_co = TypeVar("_T1_co",
  42. bound=SingletonPrompt,
  43. default=SingletonPrompt,
  44. covariant=True)
  45. _T2_co = TypeVar("_T2_co",
  46. bound=SingletonPrompt,
  47. default=SingletonPrompt,
  48. covariant=True)
  49. # TODO: Make fields ReadOnly once mypy supports it
  50. class ExplicitEncoderDecoderPrompt(TypedDict, Generic[_T1_co, _T2_co]):
  51. """Represents an encoder/decoder model input prompt,
  52. comprising an explicit encoder prompt and a
  53. decoder prompt.
  54. The encoder and decoder prompts, respectively,
  55. may formatted according to any of the
  56. :class:`SingletonPromptType` schemas, and are not
  57. required to have the same schema.
  58. Only the encoder prompt may have multi-modal data.
  59. Note that an :class:`ExplicitEncoderDecoderPrompt` may not
  60. be used as an input to a decoder-only model,
  61. and that the `encoder_prompt` and `decoder_prompt`
  62. fields of this data structure themselves must be
  63. :class:`SingletonPromptType` instances.
  64. """
  65. encoder_prompt: _T1_co
  66. decoder_prompt: Optional[_T2_co]
  67. PromptType = Union[SingletonPrompt, ExplicitEncoderDecoderPrompt]
  68. """
  69. Set of possible schemas for an LLM input, including
  70. both decoder-only and encoder/decoder input types:
  71. - A text prompt (:class:`str` or :class:`TextPrompt`)
  72. - A tokenized prompt (:class:`TokensPrompt`)
  73. - A single data structure containing both an encoder and a decoder prompt
  74. (:class:`ExplicitEncoderDecoderPrompt`)
  75. """
  76. class LLMInputs(TypedDict):
  77. """
  78. The inputs in :class:`~vllm.LLMEngine` before they are
  79. passed to the model executor.
  80. This specifies the data required for decoder-only models.
  81. """
  82. prompt_token_ids: List[int]
  83. """The token IDs of the prompt."""
  84. prompt: NotRequired[Optional[str]]
  85. """
  86. The original prompt text corresponding to the token IDs, if available.
  87. """
  88. multi_modal_data: NotRequired[Optional["MultiModalDataDict"]]
  89. """
  90. Optional multi-modal data to pass to the model,
  91. if the model supports it.
  92. """
  93. class EncoderDecoderLLMInputs(LLMInputs):
  94. """
  95. The inputs in :class:`~vllm.LLMEngine` before they are
  96. passed to the model executor.
  97. This specifies the required data for encoder-decoder models.
  98. """
  99. encoder_prompt_token_ids: List[int]
  100. """The token IDs of the encoder prompt."""
  101. encoder_prompt: NotRequired[Optional[str]]
  102. """
  103. The original encoder prompt text corresponding to the token IDs, if
  104. available.
  105. """
  106. _T1 = TypeVar("_T1", bound=SingletonPrompt, default=SingletonPrompt)
  107. _T2 = TypeVar("_T2", bound=SingletonPrompt, default=SingletonPrompt)
  108. def build_explicit_enc_dec_prompt(
  109. encoder_prompt: _T1,
  110. decoder_prompt: Optional[_T2],
  111. ) -> ExplicitEncoderDecoderPrompt[_T1, _T2]:
  112. return ExplicitEncoderDecoderPrompt(encoder_prompt=encoder_prompt,
  113. decoder_prompt=decoder_prompt)
  114. def zip_enc_dec_prompts(
  115. enc_prompts: Iterable[_T1],
  116. dec_prompts: Iterable[Optional[_T2]],
  117. ) -> List[ExplicitEncoderDecoderPrompt[_T1, _T2]]:
  118. """
  119. Zip encoder and decoder prompts together into a list of
  120. :class:`ExplicitEncoderDecoderPrompt` instances.
  121. """
  122. return [
  123. build_explicit_enc_dec_prompt(encoder_prompt, decoder_prompt)
  124. for (encoder_prompt, decoder_prompt) in zip(enc_prompts, dec_prompts)
  125. ]
  126. def to_enc_dec_tuple_list(
  127. enc_dec_prompts: Iterable[ExplicitEncoderDecoderPrompt[_T1, _T2]],
  128. ) -> List[Tuple[_T1, Optional[_T2]]]:
  129. return [(enc_dec_prompt["encoder_prompt"],
  130. enc_dec_prompt["decoder_prompt"])
  131. for enc_dec_prompt in enc_dec_prompts]