@@ -742,24 +742,22 @@ def _apply_matches(
742742 mm_prompt_updates : "MultiModalPromptUpdates" ,
743743 tokenizer : AnyTokenizer ,
744744) -> tuple [list [_S ], "MultiModalPromptUpdatesApplyResult" ]:
745- prompt_len = len (prompt )
746745 mm_item_counts = {m : len (items ) for m , items in mm_prompt_updates .items ()}
747746
748747 out_seqs = list [str | list [int ]]()
749748 out_result : MultiModalPromptUpdatesApplyResult = {
750749 m : [None ] * len (items ) for m , items in mm_prompt_updates .items ()
751750 }
752751
752+ # Early exit if no items to find
753753 mm_found_counts = {
754754 m : sum (r is not None for r in res ) for m , res in out_result .items ()
755755 }
756756 if _all_items_found (mm_item_counts , mm_found_counts ):
757757 return [prompt ], out_result
758758
759- start_idx = prev_end_idx = 0
760- while start_idx < max (prompt_len , 1 ): # Allow inserts into empty prompt
761- found = False
762-
759+ prev_end_idx = 0
760+ while True :
763761 mode , matches_to_apply = _find_matches (
764762 prompt ,
765763 mm_prompt_updates ,
@@ -768,39 +766,37 @@ def _apply_matches(
768766 current_result = out_result ,
769767 )
770768
771- if mode is not None :
772- for (modality , item_idx ), (match , update_idx ) in matches_to_apply :
773- found = True
769+ if mode is None :
770+ break # No more matches to find
774771
775- matched_update = mm_prompt_updates [modality ][item_idx ][update_idx ]
776- matched_content = matched_update .content .full
772+ for (modality , item_idx ), (match , update_idx ) in matches_to_apply :
773+ matched_update = mm_prompt_updates [modality ][item_idx ][update_idx ]
774+ matched_content = matched_update .content .full
777775
778- if mode == UpdateMode .INSERT :
779- end_idx_to_insert = match .end_idx
780- elif mode == UpdateMode .REPLACE :
781- end_idx_to_insert = match .start_idx
782- else :
783- assert_never (mode )
784-
785- out_seqs .append (prompt [prev_end_idx :end_idx_to_insert ])
786- out_seqs .append (
787- _seq2text (tokenizer , matched_content )
788- if isinstance (prompt , str )
789- else _seq2tokens (tokenizer , matched_content )
790- )
791- out_result [modality ][item_idx ] = update_idx
776+ if mode == UpdateMode .INSERT :
777+ end_idx_to_insert = match .end_idx
778+ elif mode == UpdateMode .REPLACE :
779+ end_idx_to_insert = match .start_idx
780+ else :
781+ assert_never (mode )
792782
793- # Exclude overlapping matches
794- start_idx = prev_end_idx = match .end_idx
783+ out_seqs .append (prompt [prev_end_idx :end_idx_to_insert ])
784+ out_seqs .append (
785+ _seq2text (tokenizer , matched_content )
786+ if isinstance (prompt , str )
787+ else _seq2tokens (tokenizer , matched_content )
788+ )
789+ out_result [modality ][item_idx ] = update_idx
795790
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
791+ # Exclude overlapping matches
792+ prev_end_idx = match .end_idx
801793
802- if not found :
803- start_idx += 1
794+ # Early exit if all items found
795+ mm_found_counts = {
796+ m : sum (r is not None for r in res ) for m , res in out_result .items ()
797+ }
798+ if _all_items_found (mm_item_counts , mm_found_counts ):
799+ break
804800
805801 out_seqs .append (prompt [prev_end_idx :])
806802
0 commit comments