|
2 | 2 | import mimetypes |
3 | 3 | import os |
4 | 4 | from tempfile import NamedTemporaryFile, TemporaryDirectory |
5 | | -from typing import Dict, Tuple |
| 5 | +from typing import TYPE_CHECKING, Dict, NamedTuple, Optional, Tuple |
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pytest |
9 | 9 | from PIL import Image, ImageChops |
10 | 10 | from transformers import AutoConfig, AutoTokenizer |
11 | 11 |
|
| 12 | +from vllm.multimodal.inputs import PlaceholderRange |
12 | 13 | from vllm.multimodal.utils import (MediaConnector, |
| 14 | + merge_and_sort_multimodal_metadata, |
13 | 15 | repeat_and_pad_placeholder_tokens) |
14 | 16 |
|
| 17 | +if TYPE_CHECKING: |
| 18 | + from vllm.multimodal.hasher import MultiModalHashDict |
| 19 | + from vllm.multimodal.inputs import MultiModalPlaceholderDict |
| 20 | + |
15 | 21 | # Test different image extensions (JPG/PNG) and formats (gray/RGB/RGBA) |
16 | 22 | TEST_IMAGE_URLS = [ |
17 | 23 | "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", |
@@ -191,3 +197,204 @@ def test_repeat_and_pad_placeholder_tokens(model): |
191 | 197 | assert new_prompt == expected_prompt |
192 | 198 | assert new_token_ids == expected_token_ids |
193 | 199 | assert ranges == expected_ranges |
| 200 | + |
| 201 | + |
| 202 | +# Used for the next two tests related to `merge_and_sort_multimodal_metadata`. |
| 203 | +class TestCase(NamedTuple): |
| 204 | + mm_positions: "MultiModalPlaceholderDict" |
| 205 | + mm_hashes: Optional["MultiModalHashDict"] |
| 206 | + expected_modalities: list[str] |
| 207 | + expected_ranges: list[PlaceholderRange] |
| 208 | + expected_hashes: Optional[list[str]] |
| 209 | + |
| 210 | + |
| 211 | +def test_merge_and_sort_multimodal_metadata(): |
| 212 | + |
| 213 | + test_cases = [ |
| 214 | + # Single modality should return result as is but flattened |
| 215 | + TestCase( |
| 216 | + mm_positions={ |
| 217 | + "image": [ |
| 218 | + PlaceholderRange(offset=0, length=2), |
| 219 | + PlaceholderRange(offset=3, length=2), |
| 220 | + ] |
| 221 | + }, |
| 222 | + mm_hashes={"image": ["hash1", "hash2"]}, |
| 223 | + expected_modalities=["image"], |
| 224 | + expected_ranges=[ |
| 225 | + PlaceholderRange(offset=0, length=2), |
| 226 | + PlaceholderRange(offset=3, length=2), |
| 227 | + ], |
| 228 | + expected_hashes=["hash1", "hash2"], |
| 229 | + ), |
| 230 | + |
| 231 | + # Single modality without hashes return None for mm hash. |
| 232 | + TestCase( |
| 233 | + mm_positions={ |
| 234 | + "image": [ |
| 235 | + PlaceholderRange(offset=0, length=2), |
| 236 | + PlaceholderRange(offset=2, length=2), |
| 237 | + ] |
| 238 | + }, |
| 239 | + mm_hashes=None, |
| 240 | + expected_modalities=["image"], |
| 241 | + expected_ranges=[ |
| 242 | + PlaceholderRange(offset=0, length=2), |
| 243 | + PlaceholderRange(offset=2, length=2), |
| 244 | + ], |
| 245 | + expected_hashes=None, |
| 246 | + ), |
| 247 | + |
| 248 | + # Multiple modalities with hashes should return sorted modalities |
| 249 | + # and flattened ranges and hashes. |
| 250 | + TestCase( |
| 251 | + mm_positions={ |
| 252 | + "image": [ |
| 253 | + PlaceholderRange(offset=7, length=4), |
| 254 | + PlaceholderRange(offset=11, length=5), |
| 255 | + ], |
| 256 | + "audio": [ |
| 257 | + PlaceholderRange(offset=0, length=2), |
| 258 | + PlaceholderRange(offset=2, length=3), |
| 259 | + ] |
| 260 | + }, |
| 261 | + mm_hashes={ |
| 262 | + "image": ["image_hash1", "image_hash2"], |
| 263 | + "audio": ["audio_hash1", "audio_hash2"], |
| 264 | + }, |
| 265 | + expected_modalities=["audio", "image"], |
| 266 | + expected_ranges=[ |
| 267 | + PlaceholderRange(offset=0, length=2), |
| 268 | + PlaceholderRange(offset=2, length=3), |
| 269 | + PlaceholderRange(offset=7, length=4), |
| 270 | + PlaceholderRange(offset=11, length=5), |
| 271 | + ], |
| 272 | + expected_hashes=[ |
| 273 | + "audio_hash1", "audio_hash2", "image_hash1", "image_hash2" |
| 274 | + ], |
| 275 | + ), |
| 276 | + |
| 277 | + # Multiple modalities without hashes should return sorted modalities |
| 278 | + # and flattened ranges and None. |
| 279 | + TestCase( |
| 280 | + mm_positions={ |
| 281 | + "image": [ |
| 282 | + PlaceholderRange(offset=7, length=4), |
| 283 | + PlaceholderRange(offset=11, length=5), |
| 284 | + ], |
| 285 | + "audio": [ |
| 286 | + PlaceholderRange(offset=0, length=2), |
| 287 | + PlaceholderRange(offset=2, length=3), |
| 288 | + ] |
| 289 | + }, |
| 290 | + mm_hashes=None, |
| 291 | + expected_modalities=["audio", "image"], |
| 292 | + expected_ranges=[ |
| 293 | + PlaceholderRange(offset=0, length=2), |
| 294 | + PlaceholderRange(offset=2, length=3), |
| 295 | + PlaceholderRange(offset=7, length=4), |
| 296 | + PlaceholderRange(offset=11, length=5), |
| 297 | + ], |
| 298 | + expected_hashes=None, |
| 299 | + ), |
| 300 | + |
| 301 | + # Three modalities |
| 302 | + TestCase( |
| 303 | + mm_positions={ |
| 304 | + "image": [ |
| 305 | + PlaceholderRange(offset=15, length=7), |
| 306 | + PlaceholderRange(offset=22, length=8), |
| 307 | + ], |
| 308 | + "audio": [ |
| 309 | + PlaceholderRange(offset=0, length=2), |
| 310 | + ], |
| 311 | + "video": [ |
| 312 | + PlaceholderRange(offset=3, length=4), |
| 313 | + PlaceholderRange(offset=7, length=5), |
| 314 | + PlaceholderRange(offset=12, length=6), |
| 315 | + ] |
| 316 | + }, |
| 317 | + mm_hashes={ |
| 318 | + "image": ["image_hash1", "image_hash2"], |
| 319 | + "audio": ["audio_hash1"], |
| 320 | + "video": ["video_hash1", "video_hash2", "video_hash3"] |
| 321 | + }, |
| 322 | + expected_modalities=["audio", "video", "image"], |
| 323 | + expected_ranges=[ |
| 324 | + PlaceholderRange(offset=0, length=2), |
| 325 | + PlaceholderRange(offset=3, length=4), |
| 326 | + PlaceholderRange(offset=7, length=5), |
| 327 | + PlaceholderRange(offset=12, length=6), |
| 328 | + PlaceholderRange(offset=15, length=7), |
| 329 | + PlaceholderRange(offset=22, length=8), |
| 330 | + ], |
| 331 | + expected_hashes=[ |
| 332 | + "audio_hash1", "video_hash1", "video_hash2", "video_hash3", |
| 333 | + "image_hash1", "image_hash2" |
| 334 | + ], |
| 335 | + ), |
| 336 | + ] |
| 337 | + |
| 338 | + for (mm_positions, mm_hashes, expected_modalities, expected_ranges, |
| 339 | + expected_hashes) in test_cases: |
| 340 | + modalities, ranges, hashes = merge_and_sort_multimodal_metadata( |
| 341 | + mm_positions, mm_hashes) |
| 342 | + |
| 343 | + assert modalities == expected_modalities |
| 344 | + assert ranges == expected_ranges |
| 345 | + assert hashes == expected_hashes |
| 346 | + |
| 347 | + |
| 348 | +def test_merge_and_sort_multimodal_metadata_with_interleaving(): |
| 349 | + |
| 350 | + test_cases = [ |
| 351 | + |
| 352 | + # <image> <audio> <image> <audio> |
| 353 | + TestCase( |
| 354 | + mm_positions={ |
| 355 | + "image": [ |
| 356 | + PlaceholderRange(offset=0, length=4), |
| 357 | + PlaceholderRange(offset=8, length=2), |
| 358 | + ], |
| 359 | + "audio": [ |
| 360 | + PlaceholderRange(offset=5, length=2), |
| 361 | + PlaceholderRange(offset=11, length=4), |
| 362 | + ] |
| 363 | + }, |
| 364 | + mm_hashes={ |
| 365 | + "image": ["image_hash1", "image_hash2"], |
| 366 | + "audio": ["audio_hash1", "audio_hash2"], |
| 367 | + }, |
| 368 | + expected_modalities=[], |
| 369 | + expected_ranges=[], |
| 370 | + expected_hashes=None, |
| 371 | + ), |
| 372 | + |
| 373 | + # <image> <image> <video> <audio> <image> |
| 374 | + TestCase( |
| 375 | + mm_positions={ |
| 376 | + "image": [ |
| 377 | + PlaceholderRange(offset=0, length=2), |
| 378 | + PlaceholderRange(offset=2, length=3), |
| 379 | + PlaceholderRange(offset=20, length=4), |
| 380 | + ], |
| 381 | + "audio": [ |
| 382 | + PlaceholderRange(offset=5, length=2), |
| 383 | + ], |
| 384 | + "video": [ |
| 385 | + PlaceholderRange(offset=8, length=5), |
| 386 | + ] |
| 387 | + }, |
| 388 | + mm_hashes=None, |
| 389 | + expected_modalities=[], |
| 390 | + expected_ranges=[], |
| 391 | + expected_hashes=None, |
| 392 | + ), |
| 393 | + ] |
| 394 | + |
| 395 | + for case in test_cases: |
| 396 | + with pytest.raises(ValueError) as ex_info: |
| 397 | + merge_and_sort_multimodal_metadata(case.mm_positions, |
| 398 | + case.mm_hashes) |
| 399 | + |
| 400 | + assert "Interleaved mixed-modality" in str(ex_info.value) |
0 commit comments