[Bugfix][Frontend] Bound the prompt after multimodal expansion (#57076)
Signed-off-by: Dilber P Shakir <dilbersha@gmail.com>
This commit is contained in:
+94
-8
@@ -7,11 +7,13 @@ from abc import ABC, abstractmethod
|
||||
from collections.abc import Mapping, Sequence
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from contextlib import ExitStack
|
||||
from dataclasses import replace
|
||||
from functools import cached_property
|
||||
from typing import TYPE_CHECKING, Any, Generic, overload
|
||||
|
||||
from typing_extensions import TypeVar
|
||||
|
||||
from vllm.exceptions import VLLMValidationError
|
||||
from vllm.inputs import (
|
||||
EmbedsInput,
|
||||
EmbedsPrompt,
|
||||
@@ -962,27 +964,103 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
|
||||
return engine_input
|
||||
|
||||
def _truncate_expanded_prompt(
|
||||
self,
|
||||
engine_input: TokensInput | MultiModalInput,
|
||||
params: TokenizeParams | None,
|
||||
) -> None:
|
||||
"""Re-apply `truncate_prompt_tokens` once placeholders are expanded.
|
||||
|
||||
`TokenizeParams.apply_post_tokenization` truncates the prompt before
|
||||
multimodal expansion, so a single placeholder token can expand into
|
||||
many and push the result back over the requested bound.
|
||||
|
||||
Multimodal spans are never cut: a request whose spans would not all
|
||||
survive intact is rejected, matching the post-expansion rejection in
|
||||
`vllm.v1.engine.input_processor._validate_model_input`.
|
||||
|
||||
Only multimodal inputs can grow after `apply_post_tokenization`, so
|
||||
everything else returns immediately.
|
||||
"""
|
||||
if engine_input["type"] != "multimodal":
|
||||
return
|
||||
|
||||
if params is None or params.truncate_prompt_tokens is None:
|
||||
return
|
||||
|
||||
prompt_token_ids = engine_input.get("prompt_token_ids")
|
||||
if prompt_token_ids is None:
|
||||
return
|
||||
|
||||
num_tokens = len(prompt_token_ids)
|
||||
truncation = params._truncation_slice(self.tokenizer, num_tokens)
|
||||
if truncation is None:
|
||||
return
|
||||
|
||||
start, stop, _ = truncation.indices(num_tokens)
|
||||
|
||||
mm_placeholders = engine_input["mm_placeholders"]
|
||||
for modality, positions in mm_placeholders.items():
|
||||
for position in positions:
|
||||
end = position.offset + position.length
|
||||
if position.offset >= start and end <= stop:
|
||||
continue
|
||||
raise VLLMValidationError(
|
||||
f"truncate_prompt_tokens={params.truncate_prompt_tokens} "
|
||||
f"would split or drop a(n) {modality} item, which spans "
|
||||
f"tokens {position.offset} to {end} of the "
|
||||
f"{num_tokens}-token prompt. Multimodal placeholders "
|
||||
f"expand after truncation is applied, so the limit must "
|
||||
f"leave every multimodal item intact. Please raise "
|
||||
f"truncate_prompt_tokens or shorten the multimodal input.",
|
||||
parameter="truncate_prompt_tokens",
|
||||
value=params.truncate_prompt_tokens,
|
||||
)
|
||||
|
||||
if start:
|
||||
engine_input["mm_placeholders"] = {
|
||||
modality: [
|
||||
replace(position, offset=position.offset - start)
|
||||
for position in positions
|
||||
]
|
||||
for modality, positions in mm_placeholders.items()
|
||||
}
|
||||
|
||||
engine_input["prompt_token_ids"] = prompt_token_ids[truncation]
|
||||
|
||||
def _process_singleton(
|
||||
self,
|
||||
prompt: SingletonTokPrompt,
|
||||
*,
|
||||
skip_mm_cache: bool = False,
|
||||
tok_params: TokenizeParams | None = None,
|
||||
) -> SingletonInput:
|
||||
if "prompt_embeds" in prompt:
|
||||
return self._process_embeds(prompt) # type: ignore[arg-type]
|
||||
|
||||
return self._process_tokens(prompt, skip_mm_cache=skip_mm_cache) # type: ignore[arg-type]
|
||||
engine_input = self._process_tokens( # type: ignore[arg-type]
|
||||
prompt, # type: ignore[arg-type]
|
||||
skip_mm_cache=skip_mm_cache,
|
||||
)
|
||||
self._truncate_expanded_prompt(engine_input, tok_params)
|
||||
return engine_input
|
||||
|
||||
async def _process_singleton_async(
|
||||
self,
|
||||
prompt: SingletonTokPrompt,
|
||||
*,
|
||||
skip_mm_cache: bool = False,
|
||||
tok_params: TokenizeParams | None = None,
|
||||
) -> SingletonInput:
|
||||
if "prompt_embeds" in prompt:
|
||||
return self._process_embeds(prompt) # type: ignore[arg-type]
|
||||
|
||||
return await self._process_tokens_async(prompt, skip_mm_cache=skip_mm_cache) # type: ignore[arg-type]
|
||||
engine_input = await self._process_tokens_async( # type: ignore[arg-type]
|
||||
prompt, # type: ignore[arg-type]
|
||||
skip_mm_cache=skip_mm_cache,
|
||||
)
|
||||
self._truncate_expanded_prompt(engine_input, tok_params)
|
||||
return engine_input
|
||||
|
||||
def _get_skip_decoder_start_token(self) -> bool:
|
||||
"""Whether the multimodal processor supplies a complete decoder prefix."""
|
||||
@@ -1048,12 +1126,15 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
arrival_time: float,
|
||||
*,
|
||||
skip_mm_cache: bool = False,
|
||||
tok_params: TokenizeParams | None = None,
|
||||
) -> EngineInput:
|
||||
engine_input: EngineInput
|
||||
if "encoder_prompt" in prompt:
|
||||
engine_input = self._process_enc_dec(prompt, skip_mm_cache=skip_mm_cache) # type: ignore[arg-type]
|
||||
else:
|
||||
engine_input = self._process_singleton(prompt, skip_mm_cache=skip_mm_cache)
|
||||
engine_input = self._process_singleton(
|
||||
prompt, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
|
||||
engine_input["arrival_time"] = arrival_time
|
||||
|
||||
@@ -1065,6 +1146,7 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
arrival_time: float,
|
||||
*,
|
||||
skip_mm_cache: bool = False,
|
||||
tok_params: TokenizeParams | None = None,
|
||||
) -> EngineInput:
|
||||
engine_input: EngineInput
|
||||
if "encoder_prompt" in prompt:
|
||||
@@ -1074,7 +1156,7 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
)
|
||||
else:
|
||||
engine_input = await self._process_singleton_async(
|
||||
prompt, skip_mm_cache=skip_mm_cache
|
||||
prompt, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
|
||||
engine_input["arrival_time"] = arrival_time
|
||||
@@ -1101,7 +1183,9 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
self._apply_prompt_extras(tok_prompts, prompt_extras)
|
||||
|
||||
return [
|
||||
self.process_for_engine(prompt, arrival_time, skip_mm_cache=skip_mm_cache)
|
||||
self.process_for_engine(
|
||||
prompt, arrival_time, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
for prompt in tok_prompts
|
||||
]
|
||||
|
||||
@@ -1126,7 +1210,7 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
return await asyncio.gather(
|
||||
*(
|
||||
self.process_for_engine_async(
|
||||
p, arrival_time, skip_mm_cache=skip_mm_cache
|
||||
p, arrival_time, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
for p in tok_prompts
|
||||
)
|
||||
@@ -1164,7 +1248,9 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
self._apply_prompt_extras(tok_prompts, prompt_extras)
|
||||
|
||||
eng_prompts = [
|
||||
self.process_for_engine(prompt, arrival_time, skip_mm_cache=skip_mm_cache)
|
||||
self.process_for_engine(
|
||||
prompt, arrival_time, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
for prompt in tok_prompts
|
||||
]
|
||||
|
||||
@@ -1204,7 +1290,7 @@ class BaseRenderer(ABC, Generic[_T]):
|
||||
eng_prompts = await asyncio.gather(
|
||||
*(
|
||||
self.process_for_engine_async(
|
||||
p, arrival_time, skip_mm_cache=skip_mm_cache
|
||||
p, arrival_time, skip_mm_cache=skip_mm_cache, tok_params=tok_params
|
||||
)
|
||||
for p in tok_prompts
|
||||
)
|
||||
|
||||
+25
-27
@@ -1194,22 +1194,20 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
coordinate space, no offset shifting needed afterwards.
|
||||
"""
|
||||
prompt_embeds_info = cast(dict, prompt).pop("_prompt_embeds", None)
|
||||
if prompt_embeds_info is not None:
|
||||
tensors, placeholder_token_id = prompt_embeds_info
|
||||
mm_updates = _build_prompt_embeds_updates(tensors, placeholder_token_id)
|
||||
cast(dict, prompt)["prompt_token_ids"] = _expand_prompt_embeds_placeholders(
|
||||
list(prompt["prompt_token_ids"]), mm_updates
|
||||
)
|
||||
if prompt_embeds_info is None:
|
||||
return super()._process_tokens(prompt, skip_mm_cache=skip_mm_cache)
|
||||
|
||||
tensors, placeholder_token_id = prompt_embeds_info
|
||||
mm_updates = _build_prompt_embeds_updates(tensors, placeholder_token_id)
|
||||
cast(dict, prompt)["prompt_token_ids"] = _expand_prompt_embeds_placeholders(
|
||||
list(prompt["prompt_token_ids"]), mm_updates
|
||||
)
|
||||
engine_input = super()._process_tokens(prompt, skip_mm_cache=skip_mm_cache)
|
||||
|
||||
if prompt_embeds_info is not None:
|
||||
tensors, _ = prompt_embeds_info
|
||||
self._apply_prompt_embeds_to_engine_input(
|
||||
cast(MultiModalInput, engine_input),
|
||||
tensors,
|
||||
mm_updates,
|
||||
)
|
||||
self._apply_prompt_embeds_to_engine_input(
|
||||
cast(MultiModalInput, engine_input),
|
||||
tensors,
|
||||
mm_updates,
|
||||
)
|
||||
|
||||
return engine_input
|
||||
|
||||
@@ -1222,24 +1220,24 @@ class HfRenderer(BaseRenderer[HfTokenizer]):
|
||||
) -> TokensInput | MultiModalInput:
|
||||
"""Async equivalent of `_process_tokens`."""
|
||||
prompt_embeds_info = cast(dict, prompt).pop("_prompt_embeds", None)
|
||||
if prompt_embeds_info is not None:
|
||||
tensors, placeholder_token_id = prompt_embeds_info
|
||||
mm_updates = _build_prompt_embeds_updates(tensors, placeholder_token_id)
|
||||
cast(dict, prompt)["prompt_token_ids"] = _expand_prompt_embeds_placeholders(
|
||||
list(prompt["prompt_token_ids"]), mm_updates
|
||||
if prompt_embeds_info is None:
|
||||
return await super()._process_tokens_async(
|
||||
prompt, skip_mm_cache=skip_mm_cache
|
||||
)
|
||||
|
||||
tensors, placeholder_token_id = prompt_embeds_info
|
||||
mm_updates = _build_prompt_embeds_updates(tensors, placeholder_token_id)
|
||||
cast(dict, prompt)["prompt_token_ids"] = _expand_prompt_embeds_placeholders(
|
||||
list(prompt["prompt_token_ids"]), mm_updates
|
||||
)
|
||||
engine_input = await super()._process_tokens_async(
|
||||
prompt, skip_mm_cache=skip_mm_cache
|
||||
)
|
||||
|
||||
if prompt_embeds_info is not None:
|
||||
tensors, _ = prompt_embeds_info
|
||||
self._apply_prompt_embeds_to_engine_input(
|
||||
cast(MultiModalInput, engine_input),
|
||||
tensors,
|
||||
mm_updates,
|
||||
)
|
||||
self._apply_prompt_embeds_to_engine_input(
|
||||
cast(MultiModalInput, engine_input),
|
||||
tensors,
|
||||
mm_updates,
|
||||
)
|
||||
|
||||
return engine_input
|
||||
|
||||
|
||||
Reference in New Issue
Block a user