main
py 135 lines 3.9 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Tests for InternVL's multimodal preprocessing kwargs."""
4
5 from collections.abc import Mapping
6
7 import pytest
8 from PIL import Image
9 from transformers import PreTrainedConfig
10
11 from vllm.multimodal import MULTIMODAL_REGISTRY
12 from vllm.multimodal.image import rescale_image_size
13 from vllm.multimodal.processing import BaseMultiModalProcessor
14
15 from ....conftest import ImageTestAssets
16 from ...utils import build_model_context
17
18
19 def _get_expected_num_patches(
20 config: PreTrainedConfig,
21 image: Image.Image,
22 num_imgs: int,
23 min_num: int,
24 max_num: int,
25 ):
26 from vllm.transformers_utils.processors.internvl import (
27 calculate_internvl_targets,
28 get_internvl_target_ratios,
29 )
30
31 width, height = image.size
32
33 blocks, _, _ = calculate_internvl_targets(
34 orig_width=width,
35 orig_height=height,
36 target_ratios=get_internvl_target_ratios(
37 min_num,
38 max_num,
39 ),
40 image_size=config.vision_config.image_size,
41 use_thumbnail=False,
42 )
43 expected_num_patches = blocks
44
45 if config.use_thumbnail and expected_num_patches > 1:
46 expected_num_patches += 1
47
48 return expected_num_patches
49
50
51 def _run_check(
52 processor: BaseMultiModalProcessor,
53 images: list[Image.Image],
54 min_num: int,
55 max_num: int,
56 mm_processor_kwargs: Mapping[str, object],
57 ):
58 tokenizer = processor.info.get_tokenizer()
59 config = processor.info.get_hf_config()
60
61 prompt = "<image>" * len(images)
62 mm_data = {"image": images}
63
64 total_expected_num_patches = sum(
65 _get_expected_num_patches(config, image, len(images), min_num, max_num)
66 for image in images
67 )
68
69 processed_inputs = processor(
70 prompt,
71 mm_items=processor.info.parse_mm_data(mm_data),
72 hf_processor_mm_kwargs=mm_processor_kwargs,
73 )
74
75 # Ensure we have the right number of placeholders per num_crops size
76 image_token_id = tokenizer.convert_tokens_to_ids("<IMG_CONTEXT>")
77 img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
78 pixel_shape = processed_inputs["mm_kwargs"].get_data()["pixel_values_flat"].shape
79
80 assert img_tok_count == 256 * total_expected_num_patches
81 assert pixel_shape[0] == total_expected_num_patches
82
83
84 @pytest.mark.parametrize("model_id", ["OpenGVLab/InternVL2-2B"])
85 @pytest.mark.parametrize(
86 "size_factors",
87 [
88 # Single-scale
89 [1.0],
90 # Single-scale, batched
91 [1.0, 1.0, 1.0],
92 # Multi-scale
93 [0.25, 0.5, 1.0],
94 [4.0, 2.0, 1.0],
95 ],
96 )
97 @pytest.mark.parametrize(
98 ("min_dynamic_patch", "max_dynamic_patch"),
99 [(1, 1), (1, 2), (1, 4), (1, 8), (2, 4), (4, 8)],
100 )
101 @pytest.mark.parametrize("dynamic_image_size", [True, False])
102 @pytest.mark.parametrize("kwargs_on_init", [True, False])
103 def test_processor_override(
104 model_id: str,
105 image_assets: ImageTestAssets,
106 size_factors: list[int],
107 min_dynamic_patch: int,
108 max_dynamic_patch: int,
109 dynamic_image_size: bool | None,
110 kwargs_on_init: bool,
111 ):
112 mm_processor_kwargs = {
113 "min_dynamic_patch": min_dynamic_patch,
114 "max_dynamic_patch": max_dynamic_patch,
115 "dynamic_image_size": dynamic_image_size,
116 }
117
118 ctx = build_model_context(
119 model_id,
120 mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None,
121 limit_mm_per_prompt={"image": len(size_factors)},
122 )
123 processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
124 hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
125
126 min_num = min_dynamic_patch if dynamic_image_size else 1
127 max_num = max_dynamic_patch if dynamic_image_size else 1
128
129 _run_check(
130 processor,
131 [rescale_image_size(image_assets[0].pil_image, f) for f in size_factors],
132 min_num,
133 max_num,
134 hf_processor_mm_kwargs,
135 )