test_phi3v.py 2.09 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
3
4
"""Tests for phi3v's multimodal preprocessing kwargs."""
import pytest

5
from vllm.multimodal import MULTIMODAL_REGISTRY
6
from vllm.transformers_utils.tokenizer import cached_tokenizer_from_config
7

8
9
from ....conftest import _ImageAssets
from ...utils import build_model_context
10
11


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

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
    tokenizer = cached_tokenizer_from_config(ctx.model_config)
43
44
45
46
    processor = MULTIMODAL_REGISTRY.create_processor(
        ctx.model_config,
        tokenizer=tokenizer,
    )
47
    hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
48

49
50
51
    # 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"
52
    mm_data = {"image": [image_assets[0].pil_image] * num_imgs}
53

54
    processed_inputs = processor.apply(prompt, mm_data, hf_processor_mm_kwargs)
55
56
57
58

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