2025-02-02 14:58:18 -05:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2025-02-02 14:58:18 -05:00
|
|
|
|
2024-10-23 08:05:18 -06:00
|
|
|
import pytest
|
|
|
|
|
|
2025-01-08 18:59:58 +08:00
|
|
|
from vllm.multimodal import MULTIMODAL_REGISTRY
|
2024-10-23 08:05:18 -06:00
|
|
|
|
2025-05-02 18:29:25 +08:00
|
|
|
from ....conftest import ImageTestAssets
|
2025-01-11 13:50:05 +08:00
|
|
|
from ...utils import build_model_context
|
2024-10-23 08:05:18 -06:00
|
|
|
|
|
|
|
|
|
2025-01-04 19:40:53 +08:00
|
|
|
@pytest.mark.parametrize("model_id", ["Qwen/Qwen2-VL-2B-Instruct"])
|
|
|
|
|
# yapf: disable
|
2024-12-20 00:28:00 +08:00
|
|
|
@pytest.mark.parametrize(
|
2025-01-04 19:40:53 +08:00
|
|
|
("mm_processor_kwargs", "expected_toks_per_img", "expected_pixels_shape"), [
|
2024-12-20 00:28:00 +08:00
|
|
|
({}, 1426, (5704, 1176)),
|
2025-01-04 19:40:53 +08:00
|
|
|
({"min_pixels": 64**2, "max_pixels": 512**2}, 330, (1320, 1176)),
|
2024-12-20 00:28:00 +08:00
|
|
|
])
|
2025-01-04 19:40:53 +08:00
|
|
|
# yapf: enable
|
2024-12-20 00:28:00 +08:00
|
|
|
@pytest.mark.parametrize("num_imgs", [1, 2])
|
2025-02-19 21:13:50 +08:00
|
|
|
@pytest.mark.parametrize("kwargs_on_init", [True, False])
|
2024-12-20 00:28:00 +08:00
|
|
|
def test_processor_override(
|
2025-05-02 18:29:25 +08:00
|
|
|
image_assets: ImageTestAssets,
|
2025-01-04 19:40:53 +08:00
|
|
|
model_id: str,
|
|
|
|
|
mm_processor_kwargs: dict[str, object],
|
2024-12-20 00:28:00 +08:00
|
|
|
expected_toks_per_img: int,
|
2025-01-04 19:40:53 +08:00
|
|
|
expected_pixels_shape: tuple[int, int],
|
2024-12-20 00:28:00 +08:00
|
|
|
num_imgs: int,
|
2025-02-19 21:13:50 +08:00
|
|
|
kwargs_on_init: bool,
|
2024-12-20 00:28:00 +08:00
|
|
|
):
|
|
|
|
|
"""Ensure Qwen2VLMultiModalProcessor handles min/max pixels properly."""
|
|
|
|
|
ctx = build_model_context(
|
2025-03-14 02:25:37 +08:00
|
|
|
model_id,
|
2025-02-19 21:13:50 +08:00
|
|
|
mm_processor_kwargs=mm_processor_kwargs if kwargs_on_init else None,
|
2025-01-01 23:44:42 +08:00
|
|
|
limit_mm_per_prompt={"image": num_imgs},
|
2024-10-23 08:05:18 -06:00
|
|
|
)
|
2025-03-28 14:34:34 +08:00
|
|
|
processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
|
|
|
|
|
tokenizer = processor.info.get_tokenizer()
|
2025-02-19 21:13:50 +08:00
|
|
|
hf_processor_mm_kwargs = {} if kwargs_on_init else mm_processor_kwargs
|
2025-01-04 19:40:53 +08:00
|
|
|
|
2024-12-20 00:28:00 +08:00
|
|
|
# Build the image str / prompt based on the number of images we pass
|
|
|
|
|
prompt = "<|vision_start|><|image_pad|><|vision_end|>" * num_imgs
|
2025-01-04 19:40:53 +08:00
|
|
|
mm_data = {"image": [image_assets[0].pil_image] * num_imgs}
|
2024-12-20 00:28:00 +08:00
|
|
|
|
2025-02-19 21:13:50 +08:00
|
|
|
processed_inputs = processor.apply(prompt, mm_data, hf_processor_mm_kwargs)
|
2024-12-20 00:28:00 +08:00
|
|
|
|
|
|
|
|
# Ensure we have the right number of placeholders per num_crops size
|
2025-02-19 21:13:50 +08:00
|
|
|
hf_processor = processor.info.get_hf_processor(**hf_processor_mm_kwargs)
|
2024-12-20 00:28:00 +08:00
|
|
|
image_token_id = tokenizer.convert_tokens_to_ids(hf_processor.image_token)
|
|
|
|
|
img_tok_count = processed_inputs["prompt_token_ids"].count(image_token_id)
|
|
|
|
|
pixel_shape = processed_inputs["mm_kwargs"]["pixel_values"].shape
|
|
|
|
|
|
|
|
|
|
assert img_tok_count == expected_toks_per_img * num_imgs
|
|
|
|
|
assert pixel_shape[0] == expected_pixels_shape[0] * num_imgs
|
|
|
|
|
assert pixel_shape[1] == expected_pixels_shape[1]
|