[MM] Simplify prompt updates: replace PromptSeq with list[int] (#53372)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Co-authored-by: Qwen Code <noreply@alibaba-inc.com>
This commit is contained in:
@@ -88,7 +88,7 @@ from vllm.multimodal.processing.processor import (
|
||||
PromptUpdate,
|
||||
PromptUpdateDetails,
|
||||
ResolvedPromptUpdate,
|
||||
_seq2text,
|
||||
cached_encode,
|
||||
)
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.sequence import IntermediateTensors
|
||||
@@ -938,30 +938,24 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
mm_prompt_updates: MultiModalPromptUpdates,
|
||||
) -> tuple[list[int], Mapping[str, list[PlaceholderFeaturesInfo]]]:
|
||||
"""Apply multi-modal prompt updates to token IDs."""
|
||||
tokenizer = self.info.get_tokenizer()
|
||||
|
||||
new_token_ids, match_result = self._apply_token_matches(
|
||||
token_ids,
|
||||
mm_prompt_updates,
|
||||
)
|
||||
|
||||
# If the search text does not represent a special token,
|
||||
# it may have different token IDs in the prompt, because
|
||||
# the tokens may go across the boundaries of the search text.
|
||||
# ----
|
||||
# e.g. when searching for "foo" in "food", if "food" itself makes
|
||||
# up a token, then the token ID of "foo" will not appear at all
|
||||
# ----
|
||||
# Since it is inefficient to search for all possible tokenizations
|
||||
# of the search text in the prompt, we instead perform string-based
|
||||
# updates on the decoded token IDs, then encode them back.
|
||||
# If the target does not consist of special tokens, it may be
|
||||
# tokenized differently inside the prompt, so fall back to
|
||||
# performing the updates on the decoded text, then encoding the
|
||||
# result back. The segments are encoded separately to avoid BPE
|
||||
# merges across segment boundaries.
|
||||
if not all(
|
||||
all(update_idx is not None for update_idx in update_idxs)
|
||||
for update_idxs in match_result.values()
|
||||
):
|
||||
new_token_ids, match_result = self._apply_text_matches_as_segmented_tokens(
|
||||
_seq2text(tokenizer, token_ids, use_cache=False),
|
||||
new_token_ids, match_result = self._apply_prompt_updates_via_text(
|
||||
token_ids,
|
||||
mm_prompt_updates,
|
||||
encode_segments_separately=True,
|
||||
)
|
||||
|
||||
matched_updates = defaultdict[str, list[Sequence[ResolvedPromptUpdate]]](list)
|
||||
@@ -1062,6 +1056,9 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
additional_placeholders.append((modality, sub_pattern))
|
||||
placeholders += additional_placeholders
|
||||
|
||||
vocab = tokenizer.get_vocab()
|
||||
unk_token_ids = [vocab["<unk>"]]
|
||||
|
||||
def get_image_replacement(item_idx: int):
|
||||
images = mm_items.get_items(
|
||||
"image", (MiniCPMVImageEmbeddingItems, ImageProcessorItems)
|
||||
@@ -1069,9 +1066,13 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
|
||||
image_size = images.get_image_size(item_idx)
|
||||
|
||||
return PromptUpdateDetails.select_text(
|
||||
self.get_image_prompt_texts(image_size, item_idx),
|
||||
"<unk>",
|
||||
return PromptUpdateDetails.select_token_ids(
|
||||
cached_encode(
|
||||
tokenizer,
|
||||
self.get_image_prompt_texts(image_size, item_idx),
|
||||
add_special_tokens=False,
|
||||
),
|
||||
unk_token_ids,
|
||||
)
|
||||
|
||||
def get_video_replacement(item_idx: int):
|
||||
@@ -1082,9 +1083,13 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
frame_size = videos.get_frame_size(item_idx)
|
||||
num_frames = videos.get_num_frames(item_idx)
|
||||
|
||||
return PromptUpdateDetails.select_text(
|
||||
self.get_video_prompt_texts(frame_size, num_frames),
|
||||
"<unk>",
|
||||
return PromptUpdateDetails.select_token_ids(
|
||||
cached_encode(
|
||||
tokenizer,
|
||||
self.get_video_prompt_texts(frame_size, num_frames),
|
||||
add_special_tokens=False,
|
||||
),
|
||||
unk_token_ids,
|
||||
)
|
||||
|
||||
get_replacement = {
|
||||
@@ -1094,7 +1099,9 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
|
||||
return [
|
||||
PromptReplacement(
|
||||
modality=modality, target=pattern, replacement=get_replacement[modality]
|
||||
modality=modality,
|
||||
target=cached_encode(tokenizer, pattern, add_special_tokens=False),
|
||||
replacement=get_replacement[modality],
|
||||
)
|
||||
for modality, pattern in placeholders
|
||||
]
|
||||
@@ -1114,7 +1121,7 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
image_processor = self.info.get_image_processor()
|
||||
version = self.info.get_model_version()
|
||||
|
||||
text = _seq2text(tokenizer, cached_update.content.full)
|
||||
text = tokenizer.decode(cached_update.content.full)
|
||||
prev_item_idx = cached_update.item_idx
|
||||
|
||||
if version == (2, 0) or version == (2, 5):
|
||||
@@ -1130,13 +1137,17 @@ class MiniCPMVMultiModalProcessor(BaseMultiModalProcessor[_I]):
|
||||
|
||||
embed_text = getattr(tokenizer, "image_token", "<unk>")
|
||||
new_update = new_update.with_content(
|
||||
PromptUpdateDetails.select_text(
|
||||
text.replace(
|
||||
f"{im_start}{prev_item_idx}{im_end}",
|
||||
f"{im_start}{new_item_idx}{im_end}",
|
||||
1,
|
||||
PromptUpdateDetails.select_token_ids(
|
||||
cached_encode(
|
||||
tokenizer,
|
||||
text.replace(
|
||||
f"{im_start}{prev_item_idx}{im_end}",
|
||||
f"{im_start}{new_item_idx}{im_end}",
|
||||
1,
|
||||
),
|
||||
add_special_tokens=False,
|
||||
),
|
||||
embed_text,
|
||||
cached_encode(tokenizer, embed_text, add_special_tokens=False),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user