Convert formatting to use ruff instead of yapf + isort (#26247)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -10,23 +10,37 @@ from vllm.config import ModelConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.multimodal import MULTIMODAL_REGISTRY, MultiModalRegistry
|
||||
from vllm.multimodal.cache import BaseMultiModalProcessorCache
|
||||
from vllm.multimodal.inputs import (MultiModalDataDict, MultiModalEncDecInputs,
|
||||
MultiModalInputs, MultiModalUUIDDict)
|
||||
from vllm.multimodal.inputs import (
|
||||
MultiModalDataDict,
|
||||
MultiModalEncDecInputs,
|
||||
MultiModalInputs,
|
||||
MultiModalUUIDDict,
|
||||
)
|
||||
from vllm.multimodal.processing import BaseMultiModalProcessor
|
||||
from vllm.transformers_utils.tokenizer import AnyTokenizer
|
||||
|
||||
from .data import (DecoderOnlyInputs, EmbedsInputs, EmbedsPrompt,
|
||||
EncoderDecoderInputs, ExplicitEncoderDecoderPrompt,
|
||||
ProcessorInputs, PromptType, SingletonInputs,
|
||||
SingletonPrompt, TextPrompt, TokenInputs, TokensPrompt,
|
||||
embeds_inputs, token_inputs)
|
||||
from .data import (
|
||||
DecoderOnlyInputs,
|
||||
EmbedsInputs,
|
||||
EmbedsPrompt,
|
||||
EncoderDecoderInputs,
|
||||
ExplicitEncoderDecoderPrompt,
|
||||
ProcessorInputs,
|
||||
PromptType,
|
||||
SingletonInputs,
|
||||
SingletonPrompt,
|
||||
TextPrompt,
|
||||
TokenInputs,
|
||||
TokensPrompt,
|
||||
embeds_inputs,
|
||||
token_inputs,
|
||||
)
|
||||
from .parse import is_explicit_encoder_decoder_prompt, parse_singleton_prompt
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class InputPreprocessor:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model_config: ModelConfig,
|
||||
@@ -43,23 +57,26 @@ class InputPreprocessor:
|
||||
|
||||
def get_tokenizer(self) -> AnyTokenizer:
|
||||
if self.tokenizer is None:
|
||||
raise ValueError("You cannot pass text prompts when "
|
||||
"`skip_tokenizer_init` is True")
|
||||
raise ValueError(
|
||||
"You cannot pass text prompts when `skip_tokenizer_init` is True"
|
||||
)
|
||||
|
||||
return self.tokenizer
|
||||
|
||||
def get_bos_token_id(self) -> Optional[int]:
|
||||
if self.tokenizer is None:
|
||||
logger.warning("Using None for BOS token id because tokenizer "
|
||||
"is not initialized")
|
||||
logger.warning(
|
||||
"Using None for BOS token id because tokenizer is not initialized"
|
||||
)
|
||||
return None
|
||||
|
||||
return self.tokenizer.bos_token_id
|
||||
|
||||
def get_eos_token_id(self) -> Optional[int]:
|
||||
if self.tokenizer is None:
|
||||
logger.warning("Using None for EOS token id because tokenizer "
|
||||
"is not initialized")
|
||||
logger.warning(
|
||||
"Using None for EOS token id because tokenizer is not initialized"
|
||||
)
|
||||
return None
|
||||
|
||||
return self.tokenizer.eos_token_id
|
||||
@@ -74,22 +91,26 @@ class InputPreprocessor:
|
||||
if not self.model_config.is_encoder_decoder:
|
||||
logger.warning_once(
|
||||
"Using None for decoder start token id because "
|
||||
"this is not an encoder/decoder model.")
|
||||
"this is not an encoder/decoder model."
|
||||
)
|
||||
return None
|
||||
|
||||
if self.model_config is None or self.model_config.hf_config is None:
|
||||
logger.warning_once(
|
||||
"Using None for decoder start token id because "
|
||||
"model config is not available.")
|
||||
"model config is not available."
|
||||
)
|
||||
return None
|
||||
|
||||
dec_start_token_id = getattr(self.model_config.hf_config,
|
||||
"decoder_start_token_id", None)
|
||||
dec_start_token_id = getattr(
|
||||
self.model_config.hf_config, "decoder_start_token_id", None
|
||||
)
|
||||
if dec_start_token_id is None:
|
||||
logger.warning_once(
|
||||
"Falling back on <BOS> for decoder start token "
|
||||
"id because decoder start token id is not "
|
||||
"available.")
|
||||
"available."
|
||||
)
|
||||
dec_start_token_id = self.get_bos_token_id()
|
||||
|
||||
return dec_start_token_id
|
||||
@@ -159,8 +180,10 @@ class InputPreprocessor:
|
||||
# use decoder_start_token_id as decoder_input_ids
|
||||
decoder_input_ids = self._get_default_enc_dec_decoder_prompt()
|
||||
|
||||
if (len(decoder_input_ids) == 0
|
||||
or decoder_input_ids[0] != decoder_start_token_id):
|
||||
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
|
||||
@@ -254,7 +277,8 @@ class InputPreprocessor:
|
||||
raise ValueError(
|
||||
f"mm_hashes must contain only strings, got: {mm_hashes}. "
|
||||
"This is likely due to an incorrect custom implementation of "
|
||||
"MultiModalProcessor.apply method.")
|
||||
"MultiModalProcessor.apply method."
|
||||
)
|
||||
|
||||
return mm_input
|
||||
|
||||
@@ -263,8 +287,9 @@ class InputPreprocessor:
|
||||
parsed_content: EmbedsPrompt,
|
||||
) -> EmbedsInputs:
|
||||
if not self.model_config.enable_prompt_embeds:
|
||||
raise ValueError("You must set `--enable-prompt-embeds` to input "
|
||||
"`prompt_embeds`.")
|
||||
raise ValueError(
|
||||
"You must set `--enable-prompt-embeds` to input `prompt_embeds`."
|
||||
)
|
||||
|
||||
prompt_embeds = parsed_content["prompt_embeds"]
|
||||
|
||||
@@ -276,24 +301,25 @@ class InputPreprocessor:
|
||||
prompt_embeds = prompt_embeds.squeeze(dim=0)
|
||||
|
||||
if prompt_embeds.ndim != 2:
|
||||
raise ValueError(
|
||||
"prompt_embeds must be of shape (seq_len, hidden_size).")
|
||||
raise ValueError("prompt_embeds must be of shape (seq_len, hidden_size).")
|
||||
|
||||
# Tensors must be on CPU for serialization between processes
|
||||
# in the MsgpackEncoder. Casting to CPU here ensures that there is no
|
||||
# hidden device transfer in the critical path of generation.
|
||||
prompt_embeds = prompt_embeds.cpu()
|
||||
|
||||
return embeds_inputs(prompt_embeds=prompt_embeds,
|
||||
cache_salt=parsed_content.get("cache_salt"))
|
||||
return embeds_inputs(
|
||||
prompt_embeds=prompt_embeds, cache_salt=parsed_content.get("cache_salt")
|
||||
)
|
||||
|
||||
def _truncate_inputs(
|
||||
self,
|
||||
inputs: list[int],
|
||||
tokenization_kwargs: Optional[dict[str, Any]] = None) -> list[int]:
|
||||
|
||||
if not tokenization_kwargs or "truncation" not in \
|
||||
tokenization_kwargs or self.tokenizer is None:
|
||||
self, inputs: list[int], tokenization_kwargs: Optional[dict[str, Any]] = None
|
||||
) -> list[int]:
|
||||
if (
|
||||
not tokenization_kwargs
|
||||
or "truncation" not in tokenization_kwargs
|
||||
or self.tokenizer is None
|
||||
):
|
||||
return inputs
|
||||
|
||||
max_length = tokenization_kwargs["max_length"]
|
||||
@@ -311,7 +337,8 @@ class InputPreprocessor:
|
||||
mm_uuids: Optional[MultiModalUUIDDict] = None,
|
||||
) -> Union[TokenInputs, MultiModalInputs]:
|
||||
prompt_token_ids = self._truncate_inputs(
|
||||
parsed_content["prompt_token_ids"], tokenization_kwargs)
|
||||
parsed_content["prompt_token_ids"], tokenization_kwargs
|
||||
)
|
||||
|
||||
inputs: Union[TokenInputs, MultiModalInputs]
|
||||
if self.model_config.is_multimodal_model:
|
||||
@@ -324,8 +351,7 @@ class InputPreprocessor:
|
||||
)
|
||||
else:
|
||||
if parsed_content.get("multi_modal_data"):
|
||||
raise ValueError(
|
||||
"This model does not support multimodal inputs")
|
||||
raise ValueError("This model does not support multimodal inputs")
|
||||
|
||||
inputs = token_inputs(prompt_token_ids)
|
||||
|
||||
@@ -354,8 +380,7 @@ class InputPreprocessor:
|
||||
)
|
||||
else:
|
||||
if parsed_content.get("multi_modal_data"):
|
||||
raise ValueError(
|
||||
"This model does not support multimodal inputs")
|
||||
raise ValueError("This model does not support multimodal inputs")
|
||||
|
||||
prompt_token_ids = self._tokenize_prompt(
|
||||
prompt_text,
|
||||
@@ -415,16 +440,20 @@ class InputPreprocessor:
|
||||
encoder_inputs: SingletonInputs,
|
||||
decoder_inputs: Optional[SingletonInputs],
|
||||
) -> EncoderDecoderInputs:
|
||||
if (encoder_inputs["type"] == "embeds"
|
||||
or decoder_inputs and decoder_inputs["type"] == "embeds"):
|
||||
raise ValueError("Embedding inputs are not supported for encoder-"
|
||||
"decoder models")
|
||||
if (
|
||||
encoder_inputs["type"] == "embeds"
|
||||
or decoder_inputs
|
||||
and decoder_inputs["type"] == "embeds"
|
||||
):
|
||||
raise ValueError(
|
||||
"Embedding inputs are not supported for encoder-decoder models"
|
||||
)
|
||||
|
||||
# Needed for mypy
|
||||
encoder_inputs = cast(Union[TokenInputs, MultiModalInputs],
|
||||
encoder_inputs)
|
||||
decoder_inputs = cast(Optional[Union[TokenInputs, MultiModalInputs]],
|
||||
decoder_inputs)
|
||||
encoder_inputs = cast(Union[TokenInputs, MultiModalInputs], encoder_inputs)
|
||||
decoder_inputs = cast(
|
||||
Optional[Union[TokenInputs, MultiModalInputs]], decoder_inputs
|
||||
)
|
||||
|
||||
if decoder_inputs is None:
|
||||
if self.model_config.hf_config.model_type == "whisper":
|
||||
@@ -434,16 +463,18 @@ class InputPreprocessor:
|
||||
# overridden by the audio features.
|
||||
dec_token_ids = encoder_inputs["prompt_token_ids"].copy()
|
||||
else:
|
||||
dec_token_ids = self._prepare_decoder_input_ids_for_generation(
|
||||
None)
|
||||
dec_token_ids = self._prepare_decoder_input_ids_for_generation(None)
|
||||
decoder_inputs = token_inputs(dec_token_ids)
|
||||
else:
|
||||
if "multi_modal_data" in decoder_inputs:
|
||||
raise ValueError("Multi-modal decoder inputs of encoder-"
|
||||
"decoder models are not supported yet")
|
||||
raise ValueError(
|
||||
"Multi-modal decoder inputs of encoder-"
|
||||
"decoder models are not supported yet"
|
||||
)
|
||||
|
||||
dec_token_ids = self._prepare_decoder_input_ids_for_generation(
|
||||
decoder_inputs["prompt_token_ids"])
|
||||
decoder_inputs["prompt_token_ids"]
|
||||
)
|
||||
decoder_inputs["prompt_token_ids"] = dec_token_ids
|
||||
|
||||
return EncoderDecoderInputs(
|
||||
@@ -460,10 +491,14 @@ class InputPreprocessor:
|
||||
For encoder/decoder models only:
|
||||
Separate Encoder/Decoder inputs from a MultiModalEncDecInputs
|
||||
"""
|
||||
if (inputs["type"] == "embeds" or decoder_inputs_to_override
|
||||
and decoder_inputs_to_override["type"] == "embeds"):
|
||||
raise ValueError("Embedding inputs are not supported for encoder-"
|
||||
"decoder models")
|
||||
if (
|
||||
inputs["type"] == "embeds"
|
||||
or decoder_inputs_to_override
|
||||
and decoder_inputs_to_override["type"] == "embeds"
|
||||
):
|
||||
raise ValueError(
|
||||
"Embedding inputs are not supported for encoder-decoder models"
|
||||
)
|
||||
|
||||
# Needed for mypy
|
||||
inputs = cast(
|
||||
@@ -480,9 +515,11 @@ class InputPreprocessor:
|
||||
|
||||
if inputs["type"] == "multimodal": # Multimodal data inputs
|
||||
if "encoder_prompt_token_ids" not in inputs:
|
||||
raise RuntimeError("You should register an encoder-decoder "
|
||||
"multi-modal processor for encoder-decoder "
|
||||
"models.")
|
||||
raise RuntimeError(
|
||||
"You should register an encoder-decoder "
|
||||
"multi-modal processor for encoder-decoder "
|
||||
"models."
|
||||
)
|
||||
inputs = cast(MultiModalEncDecInputs, inputs)
|
||||
|
||||
encoder_inputs = token_inputs(inputs["encoder_prompt_token_ids"])
|
||||
@@ -564,9 +601,9 @@ class InputPreprocessor:
|
||||
# For multimodal model, override decoder prompt from processor
|
||||
# with explicit decoder prompt.
|
||||
if self.model_config.is_multimodal_model:
|
||||
encoder_inputs, decoder_inputs = (
|
||||
self._split_enc_dec_mm_inputs(encoder_inputs,
|
||||
decoder_inputs))
|
||||
encoder_inputs, decoder_inputs = self._split_enc_dec_mm_inputs(
|
||||
encoder_inputs, decoder_inputs
|
||||
)
|
||||
else:
|
||||
# `cast` is needed for mypy, but not pyright
|
||||
inputs = self._prompt_to_llm_inputs(
|
||||
@@ -576,8 +613,7 @@ class InputPreprocessor:
|
||||
)
|
||||
if self.model_config.is_multimodal_model:
|
||||
# Encoder-Decoder Multimodal model
|
||||
encoder_inputs, decoder_inputs = (
|
||||
self._split_enc_dec_mm_inputs(inputs))
|
||||
encoder_inputs, decoder_inputs = self._split_enc_dec_mm_inputs(inputs)
|
||||
else:
|
||||
encoder_inputs = inputs
|
||||
decoder_inputs = None
|
||||
@@ -589,8 +625,9 @@ class InputPreprocessor:
|
||||
prompt_inputs: DecoderOnlyInputs,
|
||||
) -> DecoderOnlyInputs:
|
||||
if "prompt_token_ids" in prompt_inputs:
|
||||
prompt_inputs = cast(Union[TokenInputs, MultiModalInputs],
|
||||
prompt_inputs) # Needed for mypy
|
||||
prompt_inputs = cast(
|
||||
Union[TokenInputs, MultiModalInputs], prompt_inputs
|
||||
) # Needed for mypy
|
||||
|
||||
return prompt_inputs
|
||||
|
||||
@@ -641,8 +678,9 @@ class InputPreprocessor:
|
||||
)
|
||||
|
||||
if is_explicit_encoder_decoder_prompt(prompt):
|
||||
raise ValueError("Cannot pass encoder-decoder prompt "
|
||||
"to decoder-only models")
|
||||
raise ValueError(
|
||||
"Cannot pass encoder-decoder prompt to decoder-only models"
|
||||
)
|
||||
|
||||
# Decoder-only operation
|
||||
# `cast` is needed for mypy, but not pyright
|
||||
|
||||
Reference in New Issue
Block a user