test_mapper.py 3.48 KB
Newer Older
1
2
from contextlib import nullcontext

3
4
import numpy as np
import pytest
5
from transformers import LlavaNextImageProcessor
6

7
from vllm.config import ModelConfig
8
from vllm.multimodal import MultiModalRegistry
9
from vllm.multimodal.utils import rescale_image_size
10

11

12
13
14
15
16
@pytest.fixture
def mm_registry():
    return MultiModalRegistry()


17
@pytest.mark.parametrize("dtype", ["half", "float"])
18
@pytest.mark.parametrize("size_factor", [0.25, 0.5, 1.0])
19
20
def test_llava_next_image_processor(image_assets, mm_registry, dtype,
                                    size_factor):
21
    MODEL_NAME = "llava-hf/llava-v1.6-vicuna-7b-hf"
22
23
24
25
26
27

    hf_processor = LlavaNextImageProcessor.from_pretrained(MODEL_NAME)
    assert isinstance(hf_processor, LlavaNextImageProcessor)

    model_config = ModelConfig(
        model=MODEL_NAME,
28
        task="auto",
29
30
31
32
33
34
        tokenizer=MODEL_NAME,
        tokenizer_mode="auto",
        trust_remote_code=False,
        seed=0,
        dtype=dtype,
        revision=None,
35
        limit_mm_per_prompt={"image": 1},
36
    )
37

38
    mm_registry.init_mm_limits_per_prompt(model_config)
39

40
    for asset in image_assets:
41
42
        image = rescale_image_size(asset.pil_image, size_factor)

43
        hf_result = hf_processor.preprocess(
44
            image,
45
            return_tensors="pt",
46
        )
47
        vllm_result = mm_registry.map_input(
48
            model_config,
49
            {"image": image},
50
51
52
53
54
55
56
57
58
        )

        assert hf_result.keys() == vllm_result.keys()
        for key, hf_tensor in hf_result.items():
            hf_arr: np.ndarray = hf_tensor.numpy()
            vllm_arr: np.ndarray = vllm_result[key].numpy()

            assert hf_arr.shape == vllm_arr.shape, f"Failed for key={key}"
            assert np.allclose(hf_arr, vllm_arr), f"Failed for key={key}"
59
60
61
62
63
64
65
66


@pytest.mark.parametrize(
    ("num_images", "limit", "is_valid"),
    [(0, 0, True), (0, 1, True), (1, 0, False), (1, 1, True), (1, 2, True),
     (2, 1, False), (2, 2, True)],
)
def test_mm_limits(image_assets, mm_registry, num_images, limit, is_valid):
67
    MODEL_NAME = "llava-hf/llava-v1.6-mistral-7b-hf"
68
69
70

    model_config = ModelConfig(
        model=MODEL_NAME,
71
        task="auto",
72
73
74
75
76
77
        tokenizer=MODEL_NAME,
        tokenizer_mode="auto",
        trust_remote_code=False,
        seed=0,
        dtype="half",
        revision=None,
78
        limit_mm_per_prompt={"image": limit},
79
80
    )

81
    mm_registry.init_mm_limits_per_prompt(model_config)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    image = image_assets[0].pil_image
    if num_images == 0:
        mm_inputs = {}
    elif num_images == 1:
        mm_inputs = {"image": image}
    else:
        mm_inputs = {"image": [image] * num_images}

    with nullcontext() if is_valid else pytest.raises(ValueError):
        mm_registry.map_input(model_config, mm_inputs)


# NOTE: We don't test zero images since the HF processor doesn't support it
@pytest.mark.parametrize("num_images", [1, 2])
def test_image_mapper_multi(image_assets, mm_registry, num_images):
98
    MODEL_NAME = "llava-hf/llava-v1.6-mistral-7b-hf"
99
100
101

    model_config = ModelConfig(
        model=MODEL_NAME,
102
        task="auto",
103
104
105
106
107
108
        tokenizer=MODEL_NAME,
        tokenizer_mode="auto",
        trust_remote_code=False,
        seed=0,
        dtype="half",
        revision=None,
109
        limit_mm_per_prompt={"image": num_images},
110
111
    )

112
    mm_registry.init_mm_limits_per_prompt(model_config)
113
114
115
116
117
118

    image = image_assets[0].pil_image
    mm_inputs = {"image": [image] * num_images}

    mapped_inputs = mm_registry.map_input(model_config, mm_inputs)
    assert len(mapped_inputs["pixel_values"]) == num_images