| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Tests for phi4mm's multimodal preprocessing kwargs.""" |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from vllm.multimodal import MULTIMODAL_REGISTRY |
| 8 | |
| 9 | from ....conftest import ImageTestAssets |
| 10 | from ...utils import build_model_context |
| 11 | |
| 12 | |
| 13 | @pytest.mark.parametrize("model_id", ["microsoft/Phi-4-multimodal-instruct"]) |
| 14 | @pytest.mark.parametrize( |
| 15 | ("mm_processor_kwargs", "expected_toks_per_img"), |
| 16 | [ |
| 17 | ({"dynamic_hd": 4}, 1329), |
| 18 | ({"dynamic_hd": 16}, 4433), |
| 19 | # the default num_crops of phi-4-multimodal is 36 |
| 20 | ({}, 9585), |
| 21 | ], |
| 22 | ) |
| 23 | @pytest.mark.parametrize("num_imgs", [1, 2]) |
| 24 | @pytest.mark.parametrize("kwargs_on_init", [True, False]) |
| 25 | def test_processor_override( |
| 26 | image_assets: ImageTestAssets, |
| 27 | model_id: str, |
| 28 | mm_processor_kwargs: dict[str, int], |
| 29 | expected_toks_per_img: int, |
| 30 | num_imgs: int, |
| 31 | kwargs_on_init: bool, |
| 32 | ): |
| 33 | """Ensure Phi4MMMultiModalProcessor handles dynamic_hd properly.""" |
| 34 | # Avoid initializing CUDA early |
| 35 | from vllm.model_executor.models.phi4mm import _IMAGE_PLACEHOLDER_TOKEN_ID |
| 36 | |
| 37 | ctx = build_model_context( |
| 38 | model_id, |
| 39 | mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None, |
| 40 | limit_mm_per_prompt={"image": num_imgs}, |
| 41 | ) |
| 42 | processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config) |
| 43 | hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs |
| 44 | |
| 45 | # Build the image str / prompt based on the number of images we pass |
| 46 | img_str = "".join([f"<|image_{idx}|>\n" for idx in range(1, num_imgs + 1)]) |
| 47 | prompt = f"<|user|>\n{img_str}<|end|>\n<|assistant|>\n" |
| 48 | |
| 49 | image_size = ctx.get_hf_config().embd_layer["image_embd_layer"]["crop_size"] |
| 50 | dummy_image_size = (image_size * 7, image_size * 7) |
| 51 | dummy_image = image_assets[0].pil_image.resize(dummy_image_size) |
| 52 | mm_data = {"image": [dummy_image] * num_imgs} |
| 53 | |
| 54 | processed_inputs = processor( |
| 55 | prompt, |
| 56 | mm_items=processor.info.parse_mm_data(mm_data), |
| 57 | hf_processor_mm_kwargs=hf_processor_mm_kwargs, |
| 58 | ) |
| 59 | |
| 60 | # Ensure we have the right number of placeholders per num_crops size |
| 61 | img_tok_count = processed_inputs["prompt_token_ids"].count( |
| 62 | _IMAGE_PLACEHOLDER_TOKEN_ID |
| 63 | ) |
| 64 | assert img_tok_count == expected_toks_per_img * num_imgs |