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

3
4
import numpy as np
import pytest
5
import os
zhuwenwen's avatar
zhuwenwen committed
6

7
from transformers import LlavaNextImageProcessor
8

9
from vllm.config import ModelConfig
10
from vllm.multimodal import MultiModalRegistry
11
from vllm.multimodal.utils import rescale_image_size
12
from ..utils import models_path_prefix
13

14

15
16
17
18
19
@pytest.fixture
def mm_registry():
    return MultiModalRegistry()


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

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

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

41
    mm_registry.init_mm_limits_per_prompt(model_config)
42

43
    for asset in image_assets:
44
45
        image = rescale_image_size(asset.pil_image, size_factor)

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

        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}"
62
63
64
65
66
67
68
69


@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):
zhuwenwen's avatar
zhuwenwen committed
70
    MODEL_NAME = os.path.join(models_path_prefix, "llava-hf/llava-v1.6-mistral-7b-hf")
71
72
73

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

84
    mm_registry.init_mm_limits_per_prompt(model_config)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

    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):
zhuwenwen's avatar
zhuwenwen committed
101
    MODEL_NAME = os.path.join(models_path_prefix, "llava-hf/llava-v1.6-mistral-7b-hf")
102
103
104

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

115
    mm_registry.init_mm_limits_per_prompt(model_config)
116
117
118
119
120
121

    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