data.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from typing import (TYPE_CHECKING, List, Literal, Optional, Sequence,
  2. TypedDict, Union, cast, overload)
  3. from typing_extensions import NotRequired
  4. if TYPE_CHECKING:
  5. from aphrodite.multimodal import MultiModalDataDict
  6. class ParsedText(TypedDict):
  7. content: str
  8. is_tokens: Literal[False]
  9. class ParsedTokens(TypedDict):
  10. content: List[int]
  11. is_tokens: Literal[True]
  12. @overload
  13. def parse_and_batch_prompt(
  14. prompt: Union[str, List[str]]) -> Sequence[ParsedText]:
  15. ...
  16. @overload
  17. def parse_and_batch_prompt(
  18. prompt: Union[List[int], List[List[int]]]) -> Sequence[ParsedTokens]:
  19. ...
  20. def parse_and_batch_prompt(
  21. prompt: Union[str, List[str], List[int], List[List[int]]],
  22. ) -> Union[Sequence[ParsedText], Sequence[ParsedTokens]]:
  23. if isinstance(prompt, str):
  24. # case 1: a string
  25. return [ParsedText(content=prompt, is_tokens=False)]
  26. if isinstance(prompt, list):
  27. if len(prompt) == 0:
  28. raise ValueError("please provide at least one prompt")
  29. if isinstance(prompt[0], str):
  30. # case 2: array of strings
  31. return [
  32. ParsedText(content=elem, is_tokens=False)
  33. for elem in cast(List[str], prompt)
  34. ]
  35. if isinstance(prompt[0], int):
  36. # case 3: array of tokens
  37. elem = cast(List[int], prompt)
  38. return [ParsedTokens(content=elem, is_tokens=True)]
  39. if isinstance(prompt[0], list):
  40. if len(prompt[0]) == 0:
  41. raise ValueError("please provide at least one prompt")
  42. if isinstance(prompt[0][0], int):
  43. # case 4: array of token arrays
  44. return [
  45. ParsedTokens(content=elem, is_tokens=True)
  46. for elem in cast(List[List[int]], prompt)
  47. ]
  48. raise ValueError("prompt must be a string, array of strings, "
  49. "array of tokens, or array of token arrays")
  50. class TextPrompt(TypedDict):
  51. """Schema for a text prompt."""
  52. prompt: str
  53. """The input text to be tokenized before passing to the model."""
  54. multi_modal_data: NotRequired["MultiModalDataDict"]
  55. """
  56. Optional multi-modal data to pass to the model,
  57. if the model supports it.
  58. """
  59. class TokensPrompt(TypedDict):
  60. """Schema for a tokenized prompt."""
  61. prompt_token_ids: List[int]
  62. """A list of token IDs to pass to the model."""
  63. multi_modal_data: NotRequired["MultiModalDataDict"]
  64. """
  65. Optional multi-modal data to pass to the model,
  66. if the model supports it.
  67. """
  68. SingletonPromptInputs = Union[str, TextPrompt, TokensPrompt]
  69. """
  70. Set of possible schemas for a single LLM input:
  71. - A text prompt (:class:`str` or :class:`TextPrompt`)
  72. - A tokenized prompt (:class:`TokensPrompt`)
  73. Note that "singleton" is as opposed to a data structure
  74. which encapsulates multiple prompts, i.e. of the sort
  75. which may be utilized for encoder/decoder models when
  76. the user desires to express both the encoder & decoder
  77. prompts explicitly, i.e. ExplicitEncoderDecoderPrompt
  78. A prompt of type SingletonPromptInputs may be employed
  79. as (1) input to a decoder-only model, (2) input to
  80. the encoder of an encoder/decoder model, in the scenario
  81. where the decoder-prompt is not specified explicitly, or
  82. (3) as a member of a larger data structure encapsulating
  83. more than one prompt, i.e. ExplicitEncoderDecoderPrompt
  84. """
  85. class ExplicitEncoderDecoderPrompt(TypedDict):
  86. """Represents an encoder/decoder model input prompt,
  87. comprising an explicit encoder prompt and a
  88. decoder prompt.
  89. The encoder and decoder prompts, respectively,
  90. may formatted according to any of the
  91. SingletonPromptInputs schemas, and are not
  92. required to have the same schema.
  93. Only the encoder prompt may have multi-modal data.
  94. Note that an ExplicitEncoderDecoderPrompt may not
  95. be used as an input to a decoder-only model,
  96. and that the `encoder_prompt` and `decoder_prompt`
  97. fields of this data structure may not themselves
  98. must be SingletonPromptInputs instances.
  99. """
  100. encoder_prompt: SingletonPromptInputs
  101. decoder_prompt: SingletonPromptInputs
  102. PromptInputs = Union[SingletonPromptInputs, ExplicitEncoderDecoderPrompt]
  103. """
  104. Set of possible schemas for an LLM input, including
  105. both decoder-only and encoder/decoder input types:
  106. - A text prompt (:class:`str` or :class:`TextPrompt`)
  107. - A tokenized prompt (:class:`TokensPrompt`)
  108. - A single data structure containing both an encoder and a decoder prompt
  109. (:class:`ExplicitEncoderDecoderPrompt`)
  110. """
  111. def _has_required_keys(
  112. d: dict,
  113. required_keys: set,
  114. ) -> bool:
  115. return required_keys.issubset(d.keys())
  116. def get_prompt_type(prompt: Optional[PromptInputs]) -> Optional[str]:
  117. """
  118. Get the type-name of the prompt argument instance, given that
  119. isinstance() cannot apply to TypedDict subclasses directly.
  120. If the prompt is None, return 'None' as the type name.
  121. Arguments:
  122. * prompt: LLM input prompt or None
  123. Returns:
  124. * String representation of prompt type
  125. """
  126. if prompt is None:
  127. return 'None'
  128. required_keys_dict = {
  129. 'TextPrompt': {'prompt'},
  130. 'TokensPrompt': {'prompt_token_ids'},
  131. 'ExplicitEncoderDecoder': {'encoder_prompt', 'decoder_prompt'},
  132. }
  133. if isinstance(prompt, dict):
  134. for (ptype, required_keys) in required_keys_dict.items():
  135. # Ignore type checking in the conditional below because type
  136. # checker does not understand that is_dict(prompt) narrows
  137. # down the possible types
  138. if _has_required_keys(
  139. prompt, # type: ignore
  140. required_keys):
  141. return ptype
  142. raise ValueError(f"Invalid prompt {prompt}, valid types are "
  143. "required_keys_dict={required_keys_dict}")
  144. if isinstance(prompt, str):
  145. return "str"
  146. raise ValueError(f"Invalid prompt {prompt}")
  147. class LLMInputs(TypedDict):
  148. """
  149. The inputs in :class:`~aphrodite.AphroditeEngine` before they are
  150. passed to the model executor.
  151. """
  152. prompt_token_ids: List[int]
  153. """The token IDs of the prompt."""
  154. prompt: NotRequired[Optional[str]]
  155. """
  156. The original prompt text corresponding to the token IDs, if available.
  157. """
  158. encoder_prompt_token_ids: NotRequired[List[int]]
  159. """The token IDs of the encoder prompt."""
  160. encoder_prompt: NotRequired[Optional[str]]
  161. """
  162. The original encoder prompt text corresponding to the token IDs, if
  163. available.
  164. """
  165. multi_modal_data: NotRequired[Optional["MultiModalDataDict"]]
  166. """
  167. Optional multi-modal data to pass to the model,
  168. if the model supports it.
  169. """
  170. def is_valid_encoder_decoder_llm_inputs(inputs: LLMInputs) -> bool:
  171. """
  172. Return True if the LLMInputs instance has the correct configuration
  173. for encoder/decoder.
  174. """
  175. # True if encoder prompt token ids field exists &
  176. # is not None
  177. return ('encoder_prompt_token_ids' in inputs
  178. and inputs['encoder_prompt_token_ids'] is not None)