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

9
from vllm.distributed import cleanup_dist_env_and_memory
10
from vllm.platforms import current_platform
11
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
12

13
from ....conftest import ImageTestAssets
14

15
16
17
18
19
pytestmark = pytest.mark.skip(
    reason="InternVisionModel's custom code is incompatible with "
    "transformers v5 (missing all_tied_weights_keys)"
)

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

24
25
DEVICE_TYPE = current_platform.device_type

26

27
@torch.inference_mode()
28
def run_intern_vit_test(
29
    image_assets: ImageTestAssets,
30
    model_id: str,
31
32
33
    *,
    dtype: str,
):
34
    model = snapshot_download(model_id, allow_patterns=DOWNLOAD_PATTERN)
35
    torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype]
36

37
38
39
    img_processor = CLIPImageProcessor.from_pretrained(model)
    images = [asset.pil_image for asset in image_assets]
    pixel_values = [
40
        img_processor(images, return_tensors="pt").pixel_values.to(torch_dtype)
41
42
43
44
45
46
47
        for images in images
    ]

    config = AutoConfig.from_pretrained(model, trust_remote_code=True)
    if not getattr(config, "norm_type", None):
        config.norm_type = "rms_norm"

48
    hf_model = AutoModel.from_pretrained(
49
        model, dtype=torch_dtype, trust_remote_code=True
50
    ).to(DEVICE_TYPE)
51
    hf_outputs_per_image = [
52
        hf_model(pixel_value.to(DEVICE_TYPE)).last_hidden_state
53
54
55
        for pixel_value in pixel_values
    ]

56
    from vllm.model_executor.models.intern_vit import InternVisionModel
57

58
59
60
61
    vllm_model = InternVisionModel(config)
    vllm_model.load_weights(hf_model.state_dict().items())

    del hf_model
62
    cleanup_dist_env_and_memory()
63

64
    vllm_model = vllm_model.to(DEVICE_TYPE, torch_dtype)
65
    vllm_outputs_per_image = [
66
67
        vllm_model(pixel_values=pixel_value.to(DEVICE_TYPE))
        for pixel_value in pixel_values
68
69
    ]
    del vllm_model
70
    cleanup_dist_env_and_memory()
71
72

    cos_similar = nn.CosineSimilarity(dim=-1)
73
    for vllm_output, hf_output in zip(vllm_outputs_per_image, hf_outputs_per_image):
74
75
76
        assert cos_similar(vllm_output, hf_output).mean() > 0.99


77
78
79
80
81
82
83
@pytest.mark.parametrize(
    "model_id",
    [
        "OpenGVLab/InternViT-300M-448px",
        "OpenGVLab/InternViT-6B-448px-V1-5",
    ],
)
84
@pytest.mark.parametrize("dtype", ["half"])
85
86
87
def test_models(
    default_vllm_config, dist_init, image_assets, model_id, dtype: str
) -> None:
88
89
    run_intern_vit_test(
        image_assets,
90
        model_id,
91
92
        dtype=dtype,
    )