test_qwen2_vl.py 2.26 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

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, "Qwen/Qwen2-VL-2B-Instruct")])
15
# yapf: disable
16
@pytest.mark.parametrize(
17
    ("mm_processor_kwargs", "expected_toks_per_img", "expected_pixels_shape"), [
18
        ({}, 1426, (5704, 1176)),
19
        ({"min_pixels": 64**2, "max_pixels": 512**2}, 330, (1320, 1176)),
20
    ])
21
# yapf: enable
22
@pytest.mark.parametrize("num_imgs", [1, 2])
23
@pytest.mark.parametrize("kwargs_on_init", [True, False])
24
def test_processor_override(
25
    image_assets: ImageTestAssets,
26
27
    model_id: str,
    mm_processor_kwargs: dict[str, object],
28
    expected_toks_per_img: int,
29
    expected_pixels_shape: tuple[int, int],
30
    num_imgs: int,
31
    kwargs_on_init: bool,
32
33
34
):
    """Ensure Qwen2VLMultiModalProcessor handles min/max pixels properly."""
    ctx = build_model_context(
35
        model_id,
36
        mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None,
37
        limit_mm_per_prompt={"image": num_imgs},
38
    )
39
40
    processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
    tokenizer = processor.info.get_tokenizer()
41
    hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
42

43
44
    # Build the image str / prompt based on the number of images we pass
    prompt = "<|vision_start|><|image_pad|><|vision_end|>" * num_imgs
45
    mm_data = {"image": [image_assets[0].pil_image] * num_imgs}
46

47
    processed_inputs = processor.apply(prompt, mm_data, hf_processor_mm_kwargs)
48
49

    # Ensure we have the right number of placeholders per num_crops size
50
    hf_processor = processor.info.get_hf_processor(**hf_processor_mm_kwargs)
51
52
53
54
55
56
    image_token_id = tokenizer.convert_tokens_to_ids(hf_processor.image_token)
    img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
    pixel_shape = processed_inputs["mm_kwargs"]["pixel_values"].shape

    assert img_tok_count == expected_toks_per_img * num_imgs
    assert pixel_shape[0] == expected_pixels_shape[0] * num_imgs
zhuwenwen's avatar
zhuwenwen committed
57
    assert pixel_shape[1] == expected_pixels_shape[1]