A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 12, 09:45 UTC
[Optimization] Early return for `_apply_matches` and `_iter_placeholders` (#29668)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
Cyrus Leung committed
Nov 28, 2025 at 21:26 UTC
1168768a2d17f50dfbdd03b5f4c6e6809fe7aaa3
1 file changed
+31
-2
vllm/multimodal/processing.py
+31
-2
@@ -727,18 +727,35 @@ def _find_matches(
727
return mode, matches_to_apply
728
729
730
+def _all_items_found(
731
+ mm_item_counts: dict[str, int],
732
+ mm_found_counts: dict[str, int],
733
+) -> bool:
734
+ return all(
735
+ item_idx >= mm_item_counts[modality]
736
+ for modality, item_idx in mm_found_counts.items()
737
+ )
738
+
739
+
740
def _apply_matches(
741
prompt: _S,
742
mm_prompt_updates: "MultiModalPromptUpdates",
743
tokenizer: AnyTokenizer,
744
) -> tuple[list[_S], "MultiModalPromptUpdatesApplyResult"]:
745
prompt_len = len(prompt)
746
+ mm_item_counts = {m: len(items) for m, items in mm_prompt_updates.items()}
747
748
out_seqs = list[str | list[int]]()
749
out_result: MultiModalPromptUpdatesApplyResult = {
750
m: [None] * len(items) for m, items in mm_prompt_updates.items()
751
}
752
753
+ mm_found_counts = {
754
+ m: sum(r is not None for r in res) for m, res in out_result.items()
755
+ }
756
+ if _all_items_found(mm_item_counts, mm_found_counts):
757
+ return [prompt], out_result
758
+
759
start_idx = prev_end_idx = 0
760
while start_idx < max(prompt_len, 1): # Allow inserts into empty prompt
761
found = False
@@ -776,6 +793,12 @@ def _apply_matches(
793
# Exclude overlapping matches
794
start_idx = prev_end_idx = match.end_idx
795
796
+ mm_found_counts = {
797
+ m: sum(r is not None for r in res) for m, res in out_result.items()
798
+ }
799
+ if _all_items_found(mm_item_counts, mm_found_counts):
800
+ break
801
+
802
if not found:
803
start_idx += 1
804
@@ -832,12 +855,15 @@ def _iter_placeholders(
855
856
Note that empty matches are ignored.
857
"""
835
- prompt_len = len(prompt)
858
mm_item_counts = {m: len(items) for m, items in mm_prompt_updates.items()}
859
+ item_idx_by_modality = {modality: 0 for modality in mm_prompt_updates}
860
838
- item_idx_by_modality = defaultdict[str, int](lambda: 0)
861
+ if _all_items_found(mm_item_counts, item_idx_by_modality):
862
+ return
863
864
+ prompt_len = len(prompt)
865
start_idx = 0
866
+
867
while start_idx < prompt_len:
868
found = False
869
@@ -875,6 +901,9 @@ def _iter_placeholders(
901
break
902
903
if found:
904
+ if _all_items_found(mm_item_counts, item_idx_by_modality):
905
+ return
906
+
907
break # Go back to the outer while loop
908
909
if not found: