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

zhuwenwen's avatar
zhuwenwen committed
5
import os
6
7
import pytest

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

11
12
from ....conftest import _ImageAssets
from ...utils import build_model_context
zhuwenwen's avatar
zhuwenwen committed
13
from ....utils import models_path_prefix
14
15


zhuwenwen's avatar
zhuwenwen committed
16
@pytest.mark.parametrize("model_id", [os.path.join(models_path_prefix, "OpenGVLab/InternVL2-2B")])
17
18
@pytest.mark.parametrize("max_dynamic_patch", [1, 4])
@pytest.mark.parametrize("dynamic_image_size", [True, False, None])
19
20
21
@pytest.mark.parametrize("num_imgs", [1, 2])
def test_processor_override(
    model_id: str,
22
23
24
25
26
27
    image_assets: _ImageAssets,
    max_dynamic_patch: int,
    dynamic_image_size: Optional[bool],
    num_imgs: int,
):
    ctx = build_model_context(
28
29
        model_name=model_id,
        tokenizer_name=model_id,
30
31
        trust_remote_code=True,
        mm_processor_kwargs=None,
32
        limit_mm_per_prompt={"image": num_imgs},
33
    )
34
35
36
37
38
39
40
    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,
41
42
    )

43
44
45
46
47
    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
48

49
50
51
52
    # 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}
53
54
55
56
57

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

58
    processed_inputs = processor.apply(prompt, mm_data, mm_processor_kwargs)
59
60

    # Ensure we have the right number of placeholders per num_crops size
61
    image_token_id = tokenizer.convert_tokens_to_ids("<IMG_CONTEXT>")
62
    img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
63
64
65
66
    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