test_radio.py 3.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
import torch
import torch.nn as nn
from huggingface_hub import snapshot_download
from transformers import AutoConfig, AutoModel, CLIPImageProcessor

from vllm.distributed import cleanup_dist_env_and_memory
from vllm.model_executor.models.radio import RadioModel
from vllm.transformers_utils.configs.radio import RadioConfig
12
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

from ....conftest import ImageTestAssets

# we use snapshot_download to prevent conflicts between
# dynamic_module and trust_remote_code for hf_runner
DOWNLOAD_PATTERN = ["*.json", "*.py", "*.safetensors", "*.txt", "*.model"]


@torch.inference_mode()
def run_radio_test(
    image_assets: ImageTestAssets,
    model_id: str,
    *,
    dtype: str,
):
    model = snapshot_download(model_id, allow_patterns=DOWNLOAD_PATTERN)
    torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype]

    img_processor = CLIPImageProcessor.from_pretrained(model)
    images = [asset.pil_image for asset in image_assets]
    # Input resolution must be a multiple of `self.min_resolution_step`.
    # Using `self.get_nearest_supported_resolution`, for assets 432x642 the
    # nearest supported resolution is 432x640.
    pixel_values = [
37
38
39
        img_processor(image, return_tensors="pt").pixel_values.to(torch_dtype)[
            :, :, :, :640
        ]
40
41
42
        for image in images
    ]

43
    hf_config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
44

45
46
    # RADIO model on HF does not properly handle torch_dtype argument
    # And relies on args["dtype"] which we have to patch manually:
47
    hf_config.args["dtype"] = torch_dtype
48

49
50
    hf_model = AutoModel.from_pretrained(
        model_id,
51
        config=hf_config,
52
        dtype=torch_dtype,
53
54
55
56
        trust_remote_code=True,
    ).to("cuda")
    hf_model.eval()

57
58
59
60
61
62
63
    # A HF model has image normalization as a part of model's forward
    # However in vLLM we don't make normalization a part of the model
    # forward step since mean/std stored as model's parameters and
    # subject to precision loss (when using fp16/bf16) which negatively
    # affects evaluation benchmarks.
    hf_model.make_preprocessor_external()

64
    hf_outputs_per_image = [
65
        hf_model(pixel_value.to("cuda")) for pixel_value in pixel_values
66
67
    ]

68
69
70
    vllm_config = RadioConfig(
        model_name=hf_config.args["model"],
        **hf_config.args,
71
    )
72
    vllm_model = RadioModel(vllm_config)
73
74
75
76
    vllm_model.load_weights(hf_model.state_dict())
    vllm_model = vllm_model.to("cuda", torch_dtype)

    vllm_outputs_per_image = [
77
        vllm_model(pixel_values=pixel_value.to("cuda")) for pixel_value in pixel_values
78
79
80
81
82
    ]
    del vllm_model, hf_model
    cleanup_dist_env_and_memory()

    cos_similar = nn.CosineSimilarity(dim=-1)
83
    for vllm_output, hf_output in zip(vllm_outputs_per_image, hf_outputs_per_image):
84
85
        assert cos_similar(vllm_output[0], hf_output[0]).mean() > 0.99
        assert cos_similar(vllm_output[1], hf_output[1]).mean() > 0.99
86
87


88
89
90
91
92
93
@pytest.mark.parametrize(
    "model_id",
    [
        "nvidia/C-RADIOv2-H",
    ],
)
94
@pytest.mark.parametrize("dtype", ["half", "bfloat16"])
95
96
97
def test_radio(
    default_vllm_config, dist_init, image_assets, model_id, dtype: str
) -> None:
98
99
100
101
102
    run_radio_test(
        image_assets,
        model_id,
        dtype=dtype,
    )