main
py 85 lines 3.23 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Tests for smolvlm's multimodal preprocessing kwargs."""
4
5 import pytest
6 from transformers import SmolVLMConfig
7
8 from vllm.multimodal import MULTIMODAL_REGISTRY
9
10 from ....conftest import ImageTestAssets
11 from ...utils import build_model_context
12
13
14 @pytest.mark.parametrize("model_id", ["HuggingFaceTB/SmolVLM2-2.2B-Instruct"])
15 @pytest.mark.parametrize(
16 ("mm_processor_kwargs", "expected_toks_per_img"),
17 [
18 ({"max_image_size": {"longest_edge": 384}}, 1377),
19 ({"max_image_size": {"longest_edge": 768}}, 405),
20 ({"do_image_splitting": False}, 81),
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, object],
29 expected_toks_per_img: int,
30 num_imgs: int,
31 kwargs_on_init: bool,
32 ):
33 """Ensure Idefics3MultiModalProcessor handles num_crops properly."""
34 # Same as the previous test - don't initialize mm_processor_kwargs
35 # in this test and assume that the kwargs will be correctly expanded by
36 # the partial when calling the custom input processor.
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 placeholders = (
47 "<image>"
48 if num_imgs == 1
49 else "\n".join(f"Image-{i}: <image>\n" for i in range(1, num_imgs + 1))
50 )
51 prompt = f"<|im_start|>User:{placeholders}\n<end_of_utterance>\nAssistant:" # noqa: E501
52
53 # Build mm_data
54 image_size = ctx.get_hf_config(SmolVLMConfig).vision_config.image_size
55 dummy_image_size = (image_size * 4, image_size * 4)
56 dummy_image = image_assets[0].pil_image.resize(dummy_image_size)
57 mm_data = {"image": [dummy_image] * num_imgs}
58
59 processed_inputs = processor(
60 prompt,
61 mm_items=processor.info.parse_mm_data(mm_data),
62 hf_processor_mm_kwargs=hf_processor_mm_kwargs,
63 )
64
65 # Ensure the placeholders format are correct
66 hf_processor = processor.info.get_hf_processor(**hf_processor_mm_kwargs)
67 num_patches = processor.info.get_num_patches(
68 image_width=dummy_image.width,
69 image_height=dummy_image.height,
70 processor=hf_processor,
71 mm_kwargs=hf_processor_mm_kwargs,
72 )
73 assert num_patches == expected_toks_per_img // hf_processor.image_seq_len
74
75 hf_processed_inputs = hf_processor(
76 text=prompt,
77 images=mm_data["image"],
78 **processor.info.ctx.get_merged_mm_kwargs(hf_processor_mm_kwargs),
79 )
80 assert processed_inputs["prompt_token_ids"] == hf_processed_inputs["input_ids"][0]
81
82 # Ensure we have the right number of placeholders per num_crops size
83 image_token_id = ctx.get_hf_config().image_token_id
84 img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
85 assert img_tok_count == expected_toks_per_img * num_imgs