builders.py 11.2 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
"""Helpers for building inputs that can be leveraged for different test types."""

5
from collections.abc import Iterable
6
from pathlib import PosixPath
7
from typing import Callable, Optional, Union
8
9
10

import torch

11
from vllm.multimodal.audio import AudioResampler
12
from vllm.multimodal.image import rescale_image_size
13
14
15
16
17
from vllm.multimodal.video import (
    rescale_video_size,
    resize_video,
    sample_frames_from_video,
)
18

19
from .....conftest import AudioTestAssets, ImageTestAssets, VideoTestAssets
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from .types import (
    SINGLE_AUDIO_BASE_PROMPT,
    SINGLE_IMAGE_BASE_PROMPTS,
    TEST_AUDIO_PLACEHOLDER,
    TEST_IMG_PLACEHOLDER,
    TEST_VIDEO_PLACEHOLDER,
    VIDEO_BASE_PROMPT,
    ImageSizeWrapper,
    PromptWithMultiModalInput,
    SizeType,
    VLMTestInfo,
)


def replace_test_placeholder(
    prompt: str, mm_idx_to_prompt: Callable[[int], str], test_placeholder: str
) -> str:
37
38
39
40
41
42
    """Given a prompt, replaces each test placeholder with the
    model-specific tag.
    """
    prompt_segments = prompt.split(test_placeholder)
    img_prompt = prompt_segments[0]
    for placeholder_idx, next_seg in enumerate(prompt_segments[1:], start=1):
43
        img_prompt += mm_idx_to_prompt(placeholder_idx)
44
45
46
47
        img_prompt += next_seg
    return img_prompt


48
49
50
51
52
53
54
def get_model_prompts(
    base_prompts: Iterable[str],
    img_idx_to_prompt: Optional[Callable[[int], str]],
    video_idx_to_prompt: Optional[Callable[[int], str]],
    audio_idx_to_prompt: Optional[Callable[[int], str]],
    prompt_formatter: Callable[[str], str],
) -> list[str]:
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    """Given a model-agnostic base prompt and test configuration for a model(s)
    to be tested, update the media placeholders and apply the prompt formatting
    to get the test prompt string for this model.

    Example for phi3v, given the base_prompt: "<image>What is the season?"
        1. Replace img placeholder(s)
          -> "<|image_1|>\nWhat is the season?"
        2. Apply prompt formatter:
          -> <|user|>\n<|image_1|>\nWhat is the season?<|end|>\n<|assistant|>\n
    """
    assert isinstance(base_prompts, (list, tuple))
    model_prompts = []
    for base_prompt in base_prompts:
        # Replace the multimodal placeholders in the base prompt with
        # the correct ones for the model that we are testing
        if img_idx_to_prompt:
71
72
73
            base_prompt = replace_test_placeholder(
                base_prompt, img_idx_to_prompt, TEST_IMG_PLACEHOLDER
            )
74
75

        if video_idx_to_prompt:
76
77
78
            base_prompt = replace_test_placeholder(
                base_prompt, video_idx_to_prompt, TEST_VIDEO_PLACEHOLDER
            )
79

80
        if audio_idx_to_prompt:
81
82
83
            base_prompt = replace_test_placeholder(
                base_prompt, audio_idx_to_prompt, TEST_AUDIO_PLACEHOLDER
            )
84

85
86
87
88
89
90
91
92
        # Apply the prompt formatter to wrap the base prompt with
        # the correct media placeholders to get the model test prompt
        model_prompt = prompt_formatter(base_prompt)
        model_prompts.append(model_prompt)
    return model_prompts


def build_single_image_inputs_from_test_info(
93
94
95
96
97
    test_info: VLMTestInfo,
    image_assets: ImageTestAssets,
    size_wrapper: ImageSizeWrapper,
    tmp_path: Optional[PosixPath] = None,
) -> list[PromptWithMultiModalInput]:
98
    if test_info.prompt_formatter is None:
99
        raise ValueError("Prompt formatter must be set to build single image inputs")
100

101
102
103
104
105
106
107
    model_prompts = get_model_prompts(
        test_info.single_image_prompts,
        test_info.img_idx_to_prompt,
        test_info.video_idx_to_prompt,
        test_info.audio_idx_to_prompt,
        test_info.prompt_formatter,
    )
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

    # For models that require a local path / URL encoded in the image; export
    # assets and encode into tmp_path for this test. This should be avoided
    # where possible (currently needed for Qwen-VL).
    if test_info.prompt_path_encoder is not None:
        if tmp_path is None:
            raise ValueError("Prompt path encoder requires setting local path")
        model_prompts = [
            test_info.prompt_path_encoder(tmp_path, prompt, [asset])
            for prompt, asset in zip(model_prompts, image_assets)
        ]

    images = [asset.pil_image for asset in image_assets]
    assert len(images) == len(model_prompts)
    return build_single_image_inputs(images, model_prompts, size_wrapper)


125
def build_single_image_inputs(
126
127
    images, model_prompts, size_wrapper: ImageSizeWrapper
) -> list[PromptWithMultiModalInput]:
128
129
130
131
132
133
    # For every image / prompt pair, get a pair containing two lists of
    # length size_factors, where the first contains duplicates of the model
    # prompt [str], and the second contains copies of the image after being
    # scaled by one of the size factors.
    #
    # NOTE: rescaling preserves the image aspect ratio.
134
135
136
137
138
139
140
    return [
        PromptWithMultiModalInput(
            prompts=[prompt for _ in size_wrapper.data],
            image_data=[
                apply_image_size_scaling(image, size, size_wrapper.type)
                for size in size_wrapper.data
            ],
141
142
        )
        for image, prompt in zip(images, model_prompts)
143
    ]
144
145
146


def build_multi_image_inputs_from_test_info(
147
148
149
150
151
    test_info: VLMTestInfo,
    image_assets: ImageTestAssets,
    size_wrapper: ImageSizeWrapper,
    tmp_path: Optional[PosixPath] = None,
) -> list[PromptWithMultiModalInput]:
152
    if test_info.prompt_formatter is None:
153
        raise ValueError("Prompt formatter must be set to build multi image inputs")
154

155
156
157
158
159
160
161
    model_prompts = get_model_prompts(
        [test_info.multi_image_prompt],
        test_info.img_idx_to_prompt,
        test_info.video_idx_to_prompt,
        test_info.audio_idx_to_prompt,
        test_info.prompt_formatter,
    )
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

    if test_info.prompt_path_encoder is not None:
        if tmp_path is None:
            raise ValueError("Prompt path encoder requires setting local path")
        model_prompts = [
            test_info.prompt_path_encoder(tmp_path, model_prompt, image_assets)
            for model_prompt in model_prompts
        ]

    images = [asset.pil_image for asset in image_assets]

    # Currently, we only have one multi-image list & one multi-image prompt
    return build_multi_image_inputs(
        image_lists=[images],
        model_prompts=model_prompts,
        size_wrapper=size_wrapper,
    )


181
def build_multi_image_inputs(
182
183
    image_lists, model_prompts, size_wrapper: ImageSizeWrapper
) -> list[PromptWithMultiModalInput]:
184
185
186
    return [
        PromptWithMultiModalInput(
            prompts=[prompt for _ in size_wrapper.data],
187
188
189
190
191
192
193
194
195
            image_data=[
                [
                    apply_image_size_scaling(image, size, size_wrapper.type)
                    for image in images
                ]
                for size in size_wrapper.data
            ],
        )
        for images, prompt in zip(image_lists, model_prompts)
196
    ]
197
198
199
200


def build_embedding_inputs_from_test_info(
    test_info: VLMTestInfo,
201
    image_assets: ImageTestAssets,
202
203
204
205
206
    size_wrapper: ImageSizeWrapper,
):
    # These conditions will always be true if invoked through filtering,
    # but we still check them in case this is ever called directly
    if test_info.prompt_formatter is None:
207
208
209
210
        raise ValueError("Prompt formatter must be set to build image embedding inputs")
    if size_wrapper.type != SizeType.SIZE_FACTOR or not all(
        factor == 1.0 for factor in size_wrapper.data
    ):
211
212
213
214
215
216
217
218
        raise ValueError("Embedding tests require constant (1.0) size factors")
    if test_info.convert_assets_to_embeddings is None:
        raise ValueError("No conversion func for getting embeddings found")

    model_prompts = get_model_prompts(
        SINGLE_IMAGE_BASE_PROMPTS,
        test_info.img_idx_to_prompt,
        test_info.video_idx_to_prompt,
219
        test_info.audio_idx_to_prompt,
220
221
222
223
224
        test_info.prompt_formatter,
    )

    images = [asset.pil_image for asset in image_assets]
    embeds = test_info.convert_assets_to_embeddings(image_assets)
225
226
227
    if test_info.dtype != "auto":
        dtype = getattr(torch, test_info.dtype)  # type: ignore
        embeds = [e.to(dtype=dtype) for e in embeds]
228
229
230
    assert len(images) == len(model_prompts)

    inputs = build_single_image_inputs(images, model_prompts, size_wrapper)
231
    vllm_embeddings = build_single_image_inputs(embeds, model_prompts, size_wrapper)
232
233
234
235
236
    return inputs, vllm_embeddings


def build_video_inputs_from_test_info(
    test_info: VLMTestInfo,
237
    video_assets: VideoTestAssets,
238
239
    size_wrapper: ImageSizeWrapper,
    num_frames: int,
240
) -> list[PromptWithMultiModalInput]:
241
242
243
244
245
246
    if test_info.prompt_formatter is None:
        raise ValueError("Prompt formatter must be set to build video inputs")
    model_prompts = get_model_prompts(
        [VIDEO_BASE_PROMPT],
        test_info.img_idx_to_prompt,
        test_info.video_idx_to_prompt,
247
        test_info.audio_idx_to_prompt,
248
249
250
251
252
253
254
255
        test_info.prompt_formatter,
    )

    sampled_vids = [
        sample_frames_from_video(asset.np_ndarrays, num_frames)
        for asset in video_assets
    ]

256
257
258
    video_scaler = (
        resize_video if size_wrapper.type == SizeType.FIXED_SIZE else rescale_video_size
    )
259

260
261
262
    return [
        PromptWithMultiModalInput(
            prompts=[prompt for _ in size_wrapper.data],
263
264
265
            video_data=[video_scaler(video, size) for size in size_wrapper.data],
        )
        for video, prompt in zip(sampled_vids, model_prompts)
266
    ]
267
268


269
270
271
def apply_image_size_scaling(
    image, size: Union[float, tuple[int, int]], size_type: SizeType
):
272
    """Applies a size scaler to one image; this can be an image size factor,
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    which scales the image while maintaining the aspect ratio"""
    # Special case for embeddings; if it's a tensor, it's only valid if we
    # are considering size factors at constant scale, i.e., we just clone
    # the tensor
    if isinstance(image, torch.Tensor):
        assert size_type == SizeType.SIZE_FACTOR and size == 1
        return image
    if size_type == SizeType.SIZE_FACTOR:
        # We have a list of image size factors
        return rescale_image_size(image, size)
    elif size_type == SizeType.FIXED_SIZE:
        # We have a list of fixed sizes
        return image.resize(size)
    raise ValueError("ImageSizeWrapper type must be FIXED_SIZE or SIZE_FACTOR")
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306


def build_audio_inputs_from_test_info(
    test_info: VLMTestInfo,
    audio_assets: AudioTestAssets,
) -> list[PromptWithMultiModalInput]:
    if test_info.prompt_formatter is None:
        raise ValueError("Prompt formatter must be set to build audio inputs")
    model_prompts = get_model_prompts(
        SINGLE_AUDIO_BASE_PROMPT,
        test_info.img_idx_to_prompt,
        test_info.video_idx_to_prompt,
        test_info.audio_idx_to_prompt,
        test_info.prompt_formatter,
    )
    resampler = AudioResampler(
        target_sr=16000,
        method="librosa",
    )
    audios = [asset.audio_and_sample_rate for asset in audio_assets]
307
308
309
310
311
312
313
314
315
316
    resampled_audios = [
        (
            resampler.resample(
                audio,
                orig_sr=sr,
            ),
            int(resampler.target_sr),
        )
        for audio, sr in audios
    ]
317
318
319
320
321
322
323

    return [
        PromptWithMultiModalInput(
            prompts=model_prompts,
            audio_data=resampled_audios,
        )
    ]