"vllm/vscode:/vscode.git/clone" did not exist on "1caca5a5899a7f55209ccb402ccf55a804a09675"
test_phi3v.py 4.22 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
4
import pytest
import torch.nn.functional as F
pansicheng's avatar
pansicheng committed
5
6
7
8
from PIL import Image

from vllm.assets.base import get_vllm_public_assets
from vllm.assets.image import VLM_IMAGES_DIR
9

Cyrus Leung's avatar
Cyrus Leung committed
10
11
from ....conftest import IMAGE_ASSETS, HfRunner, PromptImageInput, VllmRunner
from ....utils import large_gpu_test
12
from ...utils import check_embeddings_close
13

Cyrus Leung's avatar
Cyrus Leung committed
14
15
16
17
18
19
20
HF_TEXT_PROMPTS = [
    # T -> X
    "Find me an everyday image that matches the given caption: The label of the object is stop sign",  # noqa: E501
    # T -> X
    "Retrieve an image of this caption: cherry blossom",
]

21
HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts({
Cyrus Leung's avatar
Cyrus Leung committed
22
    # T + I -> X
23
24
    "stop_sign":
    "<|image_1|> Select the portion of the image that isolates the object of the given label: The label of the object is stop sign",  # noqa: E501
Cyrus Leung's avatar
Cyrus Leung committed
25
    # I -> X
26
    "cherry_blossom":
Cyrus Leung's avatar
Cyrus Leung committed
27
    "<|image_1|> Represent the given image for classification",  # noqa: E501
28
29
30
31
32
})

MODELS = ["TIGER-Lab/VLM2Vec-Full"]


Cyrus Leung's avatar
Cyrus Leung committed
33
def _run_test(
34
35
36
    hf_runner: type[HfRunner],
    vllm_runner: type[VllmRunner],
    input_texts: list[str],
Cyrus Leung's avatar
Cyrus Leung committed
37
    input_images: PromptImageInput,
38
    model: str,
Cyrus Leung's avatar
Cyrus Leung committed
39
    *,
40
41
42
43
44
45
    dtype: str,
) -> None:
    # NOTE: take care of the order. run vLLM first, and then run HF.
    # vLLM needs a fresh new process without cuda initialization.
    # if we run HF first, the cuda initialization will be done and it
    # will hurt multiprocessing backend with fork method (the default method).
46
    with vllm_runner(model, task="embed", dtype=dtype,
47
                     enforce_eager=True) as vllm_model:
Cyrus Leung's avatar
Cyrus Leung committed
48
        vllm_outputs = vllm_model.encode(input_texts, images=input_images)
49

Cyrus Leung's avatar
Cyrus Leung committed
50
51
52
53
54
    # use eager mode for hf runner, since phi3_v didn't work with flash_attn
    hf_model_kwargs = {"_attn_implementation": "eager"}
    with hf_runner(model, dtype=dtype,
                   model_kwargs=hf_model_kwargs) as hf_model:
        all_inputs = hf_model.get_inputs(input_texts, images=input_images)
55
56
57
58
59

        all_outputs = []
        for inputs in all_inputs:
            # Based on: https://github.com/TIGER-AI-Lab/VLM2Vec/blob/db3b951bccabba220c1f53ab46a734e50dd2fc08/src/model.py
            outputs = hf_model.model(
60
                **hf_model.wrap_device(inputs),
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                return_dict=True,
                output_hidden_states=True,
            )
            last_hidden_state = outputs.hidden_states[-1][0]
            reps = last_hidden_state[inputs.attention_mask[0].sum() - 1]
            pooled_output = F.normalize(reps, p=2, dim=-1)

            all_outputs.append(pooled_output.tolist())

        hf_outputs = all_outputs

    check_embeddings_close(
        embeddings_0_lst=hf_outputs,
        embeddings_1_lst=vllm_outputs,
        name_0="hf",
        name_1="vllm",
    )
Cyrus Leung's avatar
Cyrus Leung committed
78
79


80
@pytest.mark.core_model
Cyrus Leung's avatar
Cyrus Leung committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
@pytest.mark.parametrize("model", MODELS)
@pytest.mark.parametrize("dtype", ["half"])
def test_models_text(
    hf_runner,
    vllm_runner,
    image_assets,
    model: str,
    dtype: str,
) -> None:
    input_texts_images = [(text, None) for text in HF_TEXT_PROMPTS]
    input_texts = [text for text, _ in input_texts_images]
    input_images = [image for _, image in input_texts_images]

    _run_test(
        hf_runner,
        vllm_runner,
        input_texts,
        input_images,  # type: ignore
        model,
        dtype=dtype,
    )


@large_gpu_test(min_gb=48)
105
@pytest.mark.core_model
Cyrus Leung's avatar
Cyrus Leung committed
106
107
108
109
110
111
112
113
114
115
116
117
118
@pytest.mark.parametrize("model", MODELS)
@pytest.mark.parametrize("dtype", ["half"])
def test_models_image(
    hf_runner,
    vllm_runner,
    image_assets,
    model: str,
    dtype: str,
) -> None:
    input_texts_images = [
        (text, asset.pil_image)
        for text, asset in zip(HF_IMAGE_PROMPTS, image_assets)
    ]
pansicheng's avatar
pansicheng committed
119
120
121
122
123
124
125
126
127
    # add cases for special_tokens
    input_texts_images.append((
        "\n<s><|user|>\n <|image_1|>\n\t <s>"
        "Represent the given image for classification<|end|>"
        "\n<|assistant|>\n",
        Image.open(
            get_vllm_public_assets(filename="cherry_blossom.jpg",
                                   s3_prefix=VLM_IMAGES_DIR)),
    ))
Cyrus Leung's avatar
Cyrus Leung committed
128
129
130
131
132
133
134
135
136
137
138
    input_texts = [text for text, _ in input_texts_images]
    input_images = [image for _, image in input_texts_images]

    _run_test(
        hf_runner,
        vllm_runner,
        input_texts,
        input_images,
        model,
        dtype=dtype,
    )