[Perf] Avoid repeated multimodal prompt update scans (#51774)

Signed-off-by: Tianyu Guo <guoty@inferact.ai>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Tianyu Guo
2026-08-11 09:23:24 +00:00
committed by GitHub
co-authored by OpenAI Codex
parent 52c70b210c
commit 3fb7bb46d6
2 changed files with 233 additions and 128 deletions
+33
View File
@@ -1099,6 +1099,39 @@ def test_apply_matches_no_match_exits_quickly():
assert "".join(result) == long_prompt
def test_apply_matches_many_shared_targets_scales_linearly():
"""Shared replacement targets must not trigger per-item rescanning."""
replacement = [1] * 50
update = PromptReplacement("image", [0], replacement)
def measure(item_count: int) -> float:
mm_prompt_updates = {
"image": [[update.resolve(item_idx)] for item_idx in range(item_count)]
}
prompt = [0] * item_count
start = time.perf_counter()
result, match_result = apply_token_matches(
prompt,
mm_prompt_updates,
tokenizer=None,
)
elapsed = time.perf_counter() - start
assert len(result) == item_count * len(replacement)
assert all(token_id == 1 for token_id in result)
assert match_result == {"image": [0] * item_count}
return elapsed
measure(100)
small_time = measure(1_000)
large_time = measure(4_000)
time_ratio = large_time / small_time
assert time_ratio < 8, f"Expected linear scaling, got {time_ratio:.1f}x"
def test_iter_token_matches_rejects_negative_start_idx():
with pytest.raises(ValueError, match="non-negative"):
list(iter_token_matches([1, 2, 3], [2], start_idx=-1))