main
py 101 lines 3.16 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 import pytest
4 import torch
5 import torch.nn as nn
6 from transformers import AutoConfig, AutoModel, CLIPImageProcessor, PreTrainedModel
7
8 from vllm.distributed import cleanup_dist_env_and_memory
9 from vllm.platforms import current_platform
10 from vllm.transformers_utils.repo_utils import hf_api
11 from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
12
13 from ....conftest import ImageTestAssets
14
15 # we use snapshot_download to prevent conflicts between
16 # dynamic_module and trust_remote_code for hf_runner
17 DOWNLOAD_PATTERN = ["*.json", "*.py", "*.safetensors", "*.txt", "*.model"]
18
19 DEVICE_TYPE = current_platform.device_type
20
21
22 @torch.inference_mode()
23 def run_intern_vit_test(
24 image_assets: ImageTestAssets,
25 model_id: str,
26 *,
27 dtype: str,
28 ):
29 model = hf_api().snapshot_download(model_id, allow_patterns=DOWNLOAD_PATTERN)
30 torch_dtype = STR_DTYPE_TO_TORCH_DTYPE[dtype]
31
32 img_processor = CLIPImageProcessor.from_pretrained(model)
33 images = [asset.pil_image for asset in image_assets]
34 pixel_values = [
35 img_processor(images, return_tensors="pt").pixel_values.to(torch_dtype)
36 for images in images
37 ]
38
39 config = AutoConfig.from_pretrained(model, trust_remote_code=True)
40 if not getattr(config, "norm_type", None):
41 config.norm_type = "rms_norm"
42
43 # Monkey-patch for transformers v5 compatibility: InternVisionModel's
44 # custom code doesn't call post_init(), so all_tied_weights_keys is
45 # never set. Remove once https://github.com/OpenGVLab/InternVL fixes it.
46 _orig_init = PreTrainedModel.__init__
47
48 def _patched_init(self, *args, **kwargs):
49 _orig_init(self, *args, **kwargs)
50 self.all_tied_weights_keys = {}
51
52 PreTrainedModel.__init__ = _patched_init
53
54 try:
55 hf_model = AutoModel.from_pretrained(
56 model, dtype=torch_dtype, trust_remote_code=True
57 ).to(DEVICE_TYPE)
58 finally:
59 PreTrainedModel.__init__ = _orig_init
60 hf_outputs_per_image = [
61 hf_model(pixel_value.to(DEVICE_TYPE)).last_hidden_state
62 for pixel_value in pixel_values
63 ]
64
65 from vllm.model_executor.models.intern_vit import InternVisionModel
66
67 vllm_model = InternVisionModel(config)
68 vllm_model.load_weights(hf_model.state_dict().items())
69
70 del hf_model
71 cleanup_dist_env_and_memory()
72
73 vllm_model = vllm_model.to(DEVICE_TYPE, torch_dtype)
74 vllm_outputs_per_image = [
75 vllm_model(pixel_values=pixel_value.to(DEVICE_TYPE))
76 for pixel_value in pixel_values
77 ]
78 del vllm_model
79 cleanup_dist_env_and_memory()
80
81 cos_similar = nn.CosineSimilarity(dim=-1)
82 for vllm_output, hf_output in zip(vllm_outputs_per_image, hf_outputs_per_image):
83 assert cos_similar(vllm_output, hf_output).mean() > 0.99
84
85
86 @pytest.mark.parametrize(
87 "model_id",
88 [
89 "OpenGVLab/InternViT-300M-448px",
90 "OpenGVLab/InternViT-6B-448px-V1-5",
91 ],
92 )
93 @pytest.mark.parametrize("dtype", ["half"])
94 def test_models(
95 default_vllm_config, dist_init, image_assets, model_id, dtype: str
96 ) -> None:
97 run_intern_vit_test(
98 image_assets,
99 model_id,
100 dtype=dtype,
101 )