[Renderer] Move InputPreprocessor into Renderer (1.5/2) (#34598)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
+110
-3
@@ -3,7 +3,7 @@
|
||||
from typing import TYPE_CHECKING, Any, Literal, TypeAlias
|
||||
|
||||
import torch
|
||||
from typing_extensions import NotRequired, TypedDict
|
||||
from typing_extensions import NotRequired, TypedDict, assert_never
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.multimodal.inputs import (
|
||||
@@ -200,15 +200,22 @@ class TokenInputs(_InputOptions):
|
||||
prompt_token_ids: list[int]
|
||||
"""The token IDs of the prompt."""
|
||||
|
||||
prompt: NotRequired[str]
|
||||
"""The prompt text corresponding to the token IDs, if available."""
|
||||
|
||||
|
||||
def token_inputs(
|
||||
prompt_token_ids: list[int],
|
||||
*,
|
||||
prompt: str | None = None,
|
||||
cache_salt: str | None = None,
|
||||
) -> TokenInputs:
|
||||
"""Construct [`TokenInputs`][vllm.inputs.data.TokenInputs] from optional
|
||||
values."""
|
||||
inputs = TokenInputs(type="token", prompt_token_ids=prompt_token_ids)
|
||||
|
||||
if prompt is not None:
|
||||
inputs["prompt"] = prompt
|
||||
if cache_salt is not None:
|
||||
inputs["cache_salt"] = cache_salt
|
||||
|
||||
@@ -224,15 +231,22 @@ class EmbedsInputs(_InputOptions):
|
||||
prompt_embeds: torch.Tensor
|
||||
"""The embeddings of the prompt."""
|
||||
|
||||
prompt: NotRequired[str]
|
||||
"""The prompt text corresponding to the token IDs, if available."""
|
||||
|
||||
|
||||
def embeds_inputs(
|
||||
prompt_embeds: torch.Tensor,
|
||||
*,
|
||||
prompt: str | None = None,
|
||||
cache_salt: str | None = None,
|
||||
) -> EmbedsInputs:
|
||||
"""Construct [`EmbedsInputs`][vllm.inputs.data.EmbedsInputs] from optional
|
||||
values."""
|
||||
inputs = EmbedsInputs(type="embeds", prompt_embeds=prompt_embeds)
|
||||
|
||||
if prompt is not None:
|
||||
inputs["prompt"] = prompt
|
||||
if cache_salt is not None:
|
||||
inputs["cache_salt"] = cache_salt
|
||||
|
||||
@@ -278,10 +292,12 @@ class EncoderDecoderInputs(TypedDict):
|
||||
for encoder-decoder models.
|
||||
"""
|
||||
|
||||
encoder: EncoderInputs
|
||||
type: Literal["enc_dec"]
|
||||
|
||||
encoder_prompt: EncoderInputs
|
||||
"""The inputs for the encoder portion."""
|
||||
|
||||
decoder: DecoderInputs
|
||||
decoder_prompt: DecoderInputs
|
||||
"""The inputs for the decoder portion."""
|
||||
|
||||
|
||||
@@ -296,3 +312,94 @@ which can be passed to
|
||||
|
||||
SingletonInputs: TypeAlias = DecoderOnlyInputs | MultiModalEncDecInputs
|
||||
"""The inputs for a single encoder/decoder prompt."""
|
||||
|
||||
|
||||
def _validate_enc_inputs(inputs: SingletonInputs) -> EncoderInputs:
|
||||
if inputs["type"] == "embeds":
|
||||
raise ValueError(
|
||||
"Embedding inputs are not supported for encoder-decoder models"
|
||||
)
|
||||
|
||||
if inputs["type"] == "multimodal" and "encoder_prompt_token_ids" not in inputs:
|
||||
raise RuntimeError(
|
||||
"You should register an encoder-decoder multi-modal processor "
|
||||
"for encoder-decoder models."
|
||||
)
|
||||
|
||||
return inputs # type: ignore[return-value]
|
||||
|
||||
|
||||
def _validate_dec_inputs(inputs: SingletonInputs) -> DecoderInputs:
|
||||
if inputs["type"] == "embeds":
|
||||
raise ValueError(
|
||||
"Embedding inputs are not supported for encoder-decoder models"
|
||||
)
|
||||
|
||||
return inputs
|
||||
|
||||
|
||||
def _prepare_decoder_input_ids_for_generation(
|
||||
decoder_input_ids: list[int],
|
||||
decoder_start_token_id: int,
|
||||
) -> list[int]:
|
||||
"""
|
||||
Prepare `decoder_input_ids` for generation with encoder-decoder models,
|
||||
according to `GenerationMixin._prepare_decoder_input_ids_for_generation()`.
|
||||
|
||||
Source:
|
||||
https://github.com/huggingface/transformers/blob/v5.1.0/src/transformers/generation/utils.py
|
||||
"""
|
||||
if len(decoder_input_ids) == 0 or decoder_input_ids[0] != decoder_start_token_id:
|
||||
decoder_input_ids = [decoder_start_token_id] + decoder_input_ids
|
||||
|
||||
return decoder_input_ids
|
||||
|
||||
|
||||
def build_enc_dec_inputs(
|
||||
encoder_inputs: SingletonInputs,
|
||||
decoder_inputs: SingletonInputs | None,
|
||||
decoder_start_token_id: int,
|
||||
) -> EncoderDecoderInputs:
|
||||
enc_inputs = _validate_enc_inputs(encoder_inputs)
|
||||
|
||||
if decoder_inputs is None:
|
||||
dec_inputs: DecoderInputs = enc_inputs
|
||||
else:
|
||||
dec_inputs = _validate_dec_inputs(decoder_inputs)
|
||||
|
||||
enc_inputs_new: EncoderInputs
|
||||
dec_inputs_new: DecoderInputs
|
||||
|
||||
if enc_inputs["type"] == "multimodal":
|
||||
from vllm.multimodal.inputs import mm_inputs
|
||||
|
||||
enc_inputs_new = token_inputs(
|
||||
enc_inputs["encoder_prompt_token_ids"],
|
||||
prompt=enc_inputs.get("encoder_prompt"),
|
||||
)
|
||||
dec_inputs_new = mm_inputs(
|
||||
prompt_token_ids=dec_inputs["prompt_token_ids"],
|
||||
prompt=dec_inputs.get("prompt"),
|
||||
mm_kwargs=enc_inputs["mm_kwargs"],
|
||||
mm_hashes=enc_inputs["mm_hashes"],
|
||||
mm_placeholders=enc_inputs["mm_placeholders"],
|
||||
)
|
||||
elif enc_inputs["type"] == "token":
|
||||
enc_inputs_new = token_inputs(prompt_token_ids=[])
|
||||
dec_inputs_new = dec_inputs
|
||||
else:
|
||||
assert_never(enc_inputs)
|
||||
|
||||
dec_inputs_new["prompt_token_ids"] = _prepare_decoder_input_ids_for_generation(
|
||||
dec_inputs_new["prompt_token_ids"],
|
||||
decoder_start_token_id,
|
||||
)
|
||||
|
||||
if cache_salt := enc_inputs.get("cache_salt"):
|
||||
dec_inputs_new["cache_salt"] = cache_salt
|
||||
|
||||
return EncoderDecoderInputs(
|
||||
type="enc_dec",
|
||||
encoder_prompt=enc_inputs_new,
|
||||
decoder_prompt=dec_inputs_new,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user