test_internvl.py 2.21 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
"""Tests for InternVL's multimodal preprocessing kwargs."""
3
from typing import Optional
4
5
6

import pytest

7
8
from vllm.multimodal import MULTIMODAL_REGISTRY
from vllm.multimodal.utils import cached_get_tokenizer
9

10
11
from ....conftest import _ImageAssets
from ...utils import build_model_context
12
13


14
@pytest.mark.parametrize("model_id", ["OpenGVLab/InternVL2-2B"])
15
16
@pytest.mark.parametrize("max_dynamic_patch", [1, 4])
@pytest.mark.parametrize("dynamic_image_size", [True, False, None])
17
18
19
@pytest.mark.parametrize("num_imgs", [1, 2])
def test_processor_override(
    model_id: str,
20
21
22
23
24
25
    image_assets: _ImageAssets,
    max_dynamic_patch: int,
    dynamic_image_size: Optional[bool],
    num_imgs: int,
):
    ctx = build_model_context(
26
27
        model_name=model_id,
        tokenizer_name=model_id,
28
29
        trust_remote_code=True,
        mm_processor_kwargs=None,
30
        limit_mm_per_prompt={"image": num_imgs},
31
    )
32
33
34
35
36
37
38
    tokenizer = cached_get_tokenizer(
        ctx.model_config.tokenizer,
        trust_remote_code=ctx.model_config.trust_remote_code,
    )
    processor = MULTIMODAL_REGISTRY.create_processor(
        ctx.model_config,
        tokenizer=tokenizer,
39
40
    )

41
42
43
44
45
    mm_processor_kwargs = {
        "max_dynamic_patch": max_dynamic_patch,
    }
    if dynamic_image_size is not None:
        mm_processor_kwargs["dynamic_image_size"] = dynamic_image_size
46

47
48
49
50
    # Build the image str / prompt based on the number of images we pass
    prompt = "<image>" * num_imgs
    image = image_assets[0].pil_image.resize((448 * 2, 448 * 2))
    mm_data = {"image": [image] * num_imgs}
51
52
53
54
55

    expected_num_patches = max_dynamic_patch + 1 if max_dynamic_patch > 1 else 1
    if dynamic_image_size is False:
        expected_num_patches = 1

56
    processed_inputs = processor.apply(prompt, mm_data, mm_processor_kwargs)
57
58

    # Ensure we have the right number of placeholders per num_crops size
59
    image_token_id = tokenizer.convert_tokens_to_ids("<IMG_CONTEXT>")
60
    img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
61
62
63
64
    pixel_shape = processed_inputs["mm_kwargs"]["pixel_values_flat"].shape

    assert img_tok_count == 256 * expected_num_patches * num_imgs
    assert pixel_shape[0] == expected_num_patches * num_imgs