test_phi3v.py 2.07 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
"""Tests for phi3v's multimodal preprocessing kwargs."""
zhuwenwen's avatar
zhuwenwen committed
4
import os
5
6
import pytest

7
from vllm.multimodal import MULTIMODAL_REGISTRY
8

9
from ....conftest import ImageTestAssets
10
from ...utils import build_model_context
zhuwenwen's avatar
zhuwenwen committed
11
from ....utils import models_path_prefix
12
13


zhuwenwen's avatar
zhuwenwen committed
14
@pytest.mark.parametrize("model_id", [os.path.join(models_path_prefix, "microsoft/Phi-3.5-vision-instruct")])
15
# yapf: disable
16
@pytest.mark.parametrize(
17
    ("mm_processor_kwargs", "expected_toks_per_img"),
18
    [
19
20
        ({"num_crops": 4}, 757),
        ({"num_crops": 16}, 1921),
21
        # the default num_crops of phi-3.5-vision is 4
22
        ({}, 757),
23
    ])
24
# yapf: enable
25
@pytest.mark.parametrize("num_imgs", [1, 2])
26
@pytest.mark.parametrize("kwargs_on_init", [True, False])
27
def test_processor_override(
28
    image_assets: ImageTestAssets,
29
30
31
32
    model_id: str,
    mm_processor_kwargs: dict[str, int],
    expected_toks_per_img: int,
    num_imgs: int,
33
    kwargs_on_init: bool,
34
):
35
    """Ensure Phi3VMultiModalProcessor handles num_crops properly."""
36
37
38
    # Avoid initializing CUDA early
    from vllm.model_executor.models.phi3v import _IMAGE_TOKEN_ID

39
    ctx = build_model_context(
40
        model_id,
41
        mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None,
42
        limit_mm_per_prompt={"image": num_imgs},
43
    )
44
    processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
45
    hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
46

47
48
49
    # Build the image str / prompt based on the number of images we pass
    img_str = "".join([f"<|image_{idx}|>\n" for idx in range(1, num_imgs + 1)])
    prompt = f"<|user|>\n{img_str}<|end|>\n<|assistant|>\n"
50
    mm_data = {"image": [image_assets[0].pil_image] * num_imgs}
51

52
    processed_inputs = processor.apply(prompt, mm_data, hf_processor_mm_kwargs)
53
54
55

    # Ensure we have the right number of placeholders per num_crops size
    img_tok_count = processed_inputs["prompt_token_ids"].count(_IMAGE_TOKEN_ID)
zhuwenwen's avatar
zhuwenwen committed
56
    assert img_tok_count == expected_toks_per_img * num_imgs