data.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. SingletonPromptInputs = 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. ExplicitEncoderDecoderPrompt
  34. A prompt of type SingletonPromptInputs 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. ExplicitEncoderDecoderPrompt
  40. """
  41. _T1_co = TypeVar("_T1_co",
  42. bound=SingletonPromptInputs,
  43. default=SingletonPromptInputs,
  44. covariant=True)
  45. _T2_co = TypeVar("_T2_co",
  46. bound=SingletonPromptInputs,
  47. default=SingletonPromptInputs,
  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. SingletonPromptInputs 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 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 may not themselves
  63. must be SingletonPromptInputs instances.
  64. """
  65. encoder_prompt: _T1_co
  66. decoder_prompt: Optional[_T2_co]
  67. PromptInputs = Union[SingletonPromptInputs, 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:`~aphrodite.AphroditeEngine` 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:`~aphrodite.AphroditeEngine` 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",
  107. bound=SingletonPromptInputs,
  108. default=SingletonPromptInputs)
  109. _T2 = TypeVar("_T2",
  110. bound=SingletonPromptInputs,
  111. default=SingletonPromptInputs)
  112. def build_explicit_enc_dec_prompt(
  113. encoder_prompt: _T1,
  114. decoder_prompt: Optional[_T2],
  115. ) -> ExplicitEncoderDecoderPrompt[_T1, _T2]:
  116. return ExplicitEncoderDecoderPrompt(encoder_prompt=encoder_prompt,
  117. decoder_prompt=decoder_prompt)
  118. def zip_enc_dec_prompts(
  119. enc_prompts: Iterable[_T1],
  120. dec_prompts: Iterable[Optional[_T2]],
  121. ) -> List[ExplicitEncoderDecoderPrompt[_T1, _T2]]:
  122. """
  123. Zip encoder and decoder prompts together into a list of
  124. :class:`ExplicitEncoderDecoderPrompt` instances.
  125. """
  126. return [
  127. build_explicit_enc_dec_prompt(encoder_prompt, decoder_prompt)
  128. for (encoder_prompt, decoder_prompt) in zip(enc_prompts, dec_prompts)
  129. ]
  130. def to_enc_dec_tuple_list(
  131. enc_dec_prompts: Iterable[ExplicitEncoderDecoderPrompt[_T1, _T2]],
  132. ) -> List[Tuple[_T1, Optional[_T2]]]:
  133. return [(enc_dec_prompt["encoder_prompt"],
  134. enc_dec_prompt["decoder_prompt"])
  135. for enc_dec_prompt in enc_dec_prompts]