test_awq.py 3.94 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7

import pytest
import torch

8
from vllm.multimodal.image import rescale_image_size
9

10
from ...conftest import IMAGE_ASSETS, ImageTestAssets, VllmRunner
11
from ..utils import check_logprobs_close
12

13
14
15
16
17
18
HF_IMAGE_PROMPTS = IMAGE_ASSETS.prompts(
    {
        "stop_sign": "<|im_start|>User\n<image>\nWhat's the content in the center of the image?<|im_end|>\n<|im_start|>Assistant\n",  # noqa: E501
        "cherry_blossom": "<|im_start|>User\n<image>\nWhat is the season?<|im_end|>\n<|im_start|>Assistant\n",  # noqa: E501
    }
)
19
20


21
def run_awq_test(
22
    vllm_runner: type[VllmRunner],
23
    image_assets: ImageTestAssets,
24
25
    source_model: str,
    quant_model: str,
26
    *,
27
    size_factors: list[float],
28
29
30
31
    dtype: str,
    max_tokens: int,
    num_logprobs: int,
    tensor_parallel_size: int,
32
    distributed_executor_backend: str | None = None,
33
34
35
):
    images = [asset.pil_image for asset in image_assets]

36
37
38
39
40
41
42
    inputs_per_image = [
        (
            [prompt for _ in size_factors],
            [rescale_image_size(image, factor) for factor in size_factors],
        )
        for image, prompt in zip(images, HF_IMAGE_PROMPTS)
    ]
43
44
45
46
47
48
49

    # 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).

    # max_model_len should be greater than image_feature_size
50
    with vllm_runner(
51
52
53
54
55
56
57
        source_model,
        max_model_len=4096,
        dtype=dtype,
        tensor_parallel_size=tensor_parallel_size,
        distributed_executor_backend=distributed_executor_backend,
        enforce_eager=True,
        default_torch_num_threads=1,
58
    ) as vllm_model:
59
        source_outputs_per_image = [
60
61
62
            vllm_model.generate_greedy_logprobs(
                prompts, max_tokens, num_logprobs=num_logprobs, images=images
            )
63
64
65
            for prompts, images in inputs_per_image
        ]

66
    with vllm_runner(
67
68
69
70
71
72
73
74
        quant_model,
        quantization="awq",
        max_model_len=4096,
        dtype=dtype,
        tensor_parallel_size=tensor_parallel_size,
        distributed_executor_backend=distributed_executor_backend,
        enforce_eager=True,
        default_torch_num_threads=1,
75
    ) as vllm_model:
76
        quant_outputs_per_image = [
77
78
79
            vllm_model.generate_greedy_logprobs(
                prompts, max_tokens, num_logprobs=num_logprobs, images=images
            )
80
81
82
            for prompts, images in inputs_per_image
        ]

83
84
85
    for source_outputs, quant_outputs in zip(
        source_outputs_per_image, quant_outputs_per_image
    ):
86
87
88
89
90
91
92
93
94
95
96
        # TODO: Check whether using original CLIPVisionModel can improve
        # consistency against HF
        check_logprobs_close(
            outputs_0_lst=source_outputs,
            outputs_1_lst=quant_outputs,
            name_0="source",
            name_1="awq",
        )


@pytest.mark.parametrize(
97
98
99
    ("source_model", "quant_model"),
    [("OpenGVLab/InternVL2-2B", "OpenGVLab/InternVL2-2B-AWQ")],
)
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@pytest.mark.parametrize(
    "size_factors",
    [
        # No image
        [],
        # Single-scale
        [1.0],
        # Single-scale, batched
        [1.0, 1.0, 1.0],
        # Multi-scale
        [0.25, 0.5, 1.0],
    ],
)
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("max_tokens", [128])
@pytest.mark.parametrize("num_logprobs", [5])
@torch.inference_mode()
117
118
119
120
121
122
123
124
125
126
def test_awq_models(
    vllm_runner,
    image_assets,
    source_model,
    quant_model,
    size_factors,
    dtype,
    max_tokens,
    num_logprobs,
) -> None:
127
128
129
    run_awq_test(
        vllm_runner,
        image_assets,
130
131
        source_model,
        quant_model,
132
133
134
135
136
137
        size_factors=size_factors,
        dtype=dtype,
        max_tokens=max_tokens,
        num_logprobs=num_logprobs,
        tensor_parallel_size=1,
    )