|
| 1 | +"""Tests for H2OVL's multimodal preprocessing kwargs.""" |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from vllm.multimodal import MULTIMODAL_REGISTRY |
| 7 | +from vllm.multimodal.image import rescale_image_size |
| 8 | +from vllm.multimodal.utils import cached_get_tokenizer |
| 9 | + |
| 10 | +from ....conftest import _ImageAssets |
| 11 | +from ...utils import build_model_context |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize("model_id", [ |
| 15 | + "h2oai/h2ovl-mississippi-800m", |
| 16 | + "h2oai/h2ovl-mississippi-2b", |
| 17 | +]) |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "size_factors", |
| 20 | + [ |
| 21 | + # Single-scale |
| 22 | + [1.0], |
| 23 | + # Single-scale, batched |
| 24 | + [1.0, 1.0, 1.0], |
| 25 | + # Multi-scale |
| 26 | + [0.25, 0.5, 1.0], |
| 27 | + ], |
| 28 | +) |
| 29 | +@pytest.mark.parametrize("max_dynamic_patch", [1, 2, 4, 8]) |
| 30 | +@pytest.mark.parametrize("dynamic_image_size", [True, False, None]) |
| 31 | +@pytest.mark.parametrize("num_imgs", [1, 2]) |
| 32 | +def test_processor_override( |
| 33 | + model_id: str, |
| 34 | + image_assets: _ImageAssets, |
| 35 | + size_factors: list[int], |
| 36 | + max_dynamic_patch: int, |
| 37 | + dynamic_image_size: Optional[bool], |
| 38 | + num_imgs: int, |
| 39 | +): |
| 40 | + from vllm.model_executor.models.h2ovl import (calculate_h2ovl_targets, |
| 41 | + get_h2ovl_target_ratios) |
| 42 | + |
| 43 | + ctx = build_model_context( |
| 44 | + model_name=model_id, |
| 45 | + tokenizer_name=model_id, |
| 46 | + trust_remote_code=True, |
| 47 | + mm_processor_kwargs=None, |
| 48 | + limit_mm_per_prompt={"image": num_imgs}, |
| 49 | + ) |
| 50 | + tokenizer = cached_get_tokenizer( |
| 51 | + ctx.model_config.tokenizer, |
| 52 | + trust_remote_code=ctx.model_config.trust_remote_code, |
| 53 | + ) |
| 54 | + processor = MULTIMODAL_REGISTRY.create_processor( |
| 55 | + ctx.model_config, |
| 56 | + tokenizer=tokenizer, |
| 57 | + ) |
| 58 | + |
| 59 | + config = processor.info.get_hf_config() |
| 60 | + use_msac = config.use_msac |
| 61 | + |
| 62 | + mm_processor_kwargs = { |
| 63 | + "max_dynamic_patch": max_dynamic_patch, |
| 64 | + } |
| 65 | + if dynamic_image_size is not None: |
| 66 | + mm_processor_kwargs["dynamic_image_size"] = dynamic_image_size |
| 67 | + |
| 68 | + # Build the image str / prompt based on the number of images we pass |
| 69 | + prompt = "<image>" * num_imgs |
| 70 | + |
| 71 | + for asset in image_assets: |
| 72 | + for factor in size_factors: |
| 73 | + image = rescale_image_size(asset.pil_image, factor) |
| 74 | + mm_data = {"image": [image] * num_imgs} |
| 75 | + |
| 76 | + width, height = image.size |
| 77 | + |
| 78 | + # Calculate the expected number of blocks |
| 79 | + if use_msac: |
| 80 | + # First pass |
| 81 | + blocks1, _, _, aspect_ratio = calculate_h2ovl_targets( |
| 82 | + orig_width=width, |
| 83 | + orig_height=height, |
| 84 | + target_ratios=get_h2ovl_target_ratios( |
| 85 | + config.min_dynamic_patch, |
| 86 | + max_dynamic_patch, |
| 87 | + prior_aspect_ratio=None, |
| 88 | + ), |
| 89 | + image_size=config.vision_config.image_size, |
| 90 | + use_thumbnail=False, # Thumbnail is handled separately |
| 91 | + ) |
| 92 | + |
| 93 | + # Second pass |
| 94 | + blocks2, _, _, _ = calculate_h2ovl_targets( |
| 95 | + orig_width=width, |
| 96 | + orig_height=height, |
| 97 | + target_ratios=get_h2ovl_target_ratios( |
| 98 | + config.min_dynamic_patch, |
| 99 | + max_dynamic_patch, |
| 100 | + prior_aspect_ratio=aspect_ratio, |
| 101 | + ), |
| 102 | + image_size=config.vision_config.image_size, |
| 103 | + use_thumbnail=False, |
| 104 | + ) |
| 105 | + |
| 106 | + # Add thumbnail if use_thumbnail is True and total_blocks > 1 |
| 107 | + if config.use_thumbnail: |
| 108 | + blocks1 += 1 if blocks1 > 1 else 0 |
| 109 | + blocks2 += 1 if blocks2 > 1 else 0 |
| 110 | + |
| 111 | + # Total blocks is the sum of blocks from both passes minus |
| 112 | + # overlapping |
| 113 | + total_blocks = blocks1 + blocks2 - 1 |
| 114 | + |
| 115 | + expected_num_patches = total_blocks |
| 116 | + else: |
| 117 | + blocks, _, _, _ = calculate_h2ovl_targets( |
| 118 | + orig_width=width, |
| 119 | + orig_height=height, |
| 120 | + target_ratios=get_h2ovl_target_ratios( |
| 121 | + config.min_dynamic_patch, |
| 122 | + max_dynamic_patch, |
| 123 | + prior_aspect_ratio=None, |
| 124 | + ), |
| 125 | + image_size=config.vision_config.image_size, |
| 126 | + use_thumbnail=False, |
| 127 | + ) |
| 128 | + expected_num_patches = blocks |
| 129 | + |
| 130 | + if config.use_thumbnail and expected_num_patches != 1: |
| 131 | + expected_num_patches += 1 |
| 132 | + |
| 133 | + processed_inputs = processor.apply(prompt, mm_data, |
| 134 | + mm_processor_kwargs) |
| 135 | + pixel_shape = ( |
| 136 | + processed_inputs["mm_kwargs"]["pixel_values_flat"].shape) |
| 137 | + |
| 138 | + assert pixel_shape[0] == expected_num_patches * num_imgs |
0 commit comments