test_idefics3.py 3.15 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
"""Tests for Idefics3's multimodal preprocessing kwargs."""
4

5
import pytest
6
from packaging.version import Version
7
from transformers import Idefics3Config
8
from transformers import __version__ as TRANSFORMERS_VERSION
9

10
from vllm.multimodal import MULTIMODAL_REGISTRY
11

12
from ....conftest import ImageTestAssets
13
from ...utils import build_model_context
14
15


16
17
18
19
@pytest.mark.skipif(
    Version(TRANSFORMERS_VERSION) < Version("5.2.0"),
    reason="See https://github.com/huggingface/transformers/pull/43948",
)
20
@pytest.mark.parametrize("model_id", ["HuggingFaceM4/Idefics3-8B-Llama3"])
21
22
23
24
25
@pytest.mark.parametrize(
    ("mm_processor_kwargs", "expected_toks_per_img"),
    [
        ({"size": {"longest_edge": 364}}, 169),
        ({"size": {"longest_edge": 728}}, 169 * (2**2 + 1)),
26
27
    ],
)
28
@pytest.mark.parametrize("num_imgs", [1, 2])
29
30
@pytest.mark.parametrize("kwargs_on_init", [True, False])
def test_processor_override(
31
    image_assets: ImageTestAssets,
32
    model_id: str,
33
34
35
36
37
    mm_processor_kwargs: dict[str, object],
    expected_toks_per_img: int,
    num_imgs: int,
    kwargs_on_init: bool,
):
38
    """Ensure Idefics3MultiModalProcessor handles num_crops properly."""
39
40
41
42
    # Same as the previous test - don't initialize mm_processor_kwargs
    # in this test and assume that the kwargs will be correctly expanded by
    # the partial when calling the custom input processor.
    ctx = build_model_context(
43
        model_id,
44
        mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None,
45
        limit_mm_per_prompt={"image": num_imgs},
46
    )
47
    processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
48
    hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
49
50

    # Build the image str / prompt based on the number of images we pass
51
52
53
54
55
    placeholders = (
        "<image>"
        if num_imgs == 1
        else "\n".join(f"Image-{i}: <image>\n" for i in range(1, num_imgs + 1))
    )
56
57
    prompt = f"<|begin_of_text|>User:{placeholders}\n<end_of_utterance>\nAssistant:"  # noqa: E501

58
59
60
61
62
63
    # Build mm_data
    image_size = ctx.get_hf_config(Idefics3Config).vision_config.image_size
    dummy_image_size = (image_size * 4, image_size * 4)
    dummy_image = image_assets[0].pil_image.resize(dummy_image_size)
    mm_data = {"image": [dummy_image] * num_imgs}

64
    processed_inputs = processor(
65
66
67
68
        prompt,
        mm_items=processor.info.parse_mm_data(mm_data),
        hf_processor_mm_kwargs=hf_processor_mm_kwargs,
    )
69

70
    # Ensure the placeholders format are correct
71
    hf_processor = processor.info.get_hf_processor(**hf_processor_mm_kwargs)
72
73
74
75
76
    hf_processed_inputs = hf_processor(
        text=prompt,
        images=mm_data["image"],
        **processor.info.ctx.get_merged_mm_kwargs(hf_processor_mm_kwargs),
    )
77
    assert processed_inputs["prompt_token_ids"] == hf_processed_inputs["input_ids"][0]
78
79
80
81
82

    # Ensure we have the right number of placeholders per num_crops size
    image_token_id = ctx.get_hf_config().image_token_id
    img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
    assert img_tok_count == expected_toks_per_img * num_imgs