|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""Tests for mllama's multimodal preprocessing and profiling.""" |
| 3 | +import pytest |
| 4 | +from transformers import MllamaConfig |
| 5 | + |
| 6 | +from vllm.multimodal import MULTIMODAL_REGISTRY |
| 7 | +from vllm.multimodal.profiling import MultiModalProfiler |
| 8 | + |
| 9 | +from ...utils import build_model_context |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("model_id", |
| 13 | + ["meta-llama/Llama-3.2-11B-Vision-Instruct"]) |
| 14 | +@pytest.mark.parametrize("max_model_len", [4096, 8192, 25600, 131072]) |
| 15 | +@pytest.mark.parametrize("max_num_seqs", [1, 2, 8]) |
| 16 | +def test_profiling( |
| 17 | + model_id: str, |
| 18 | + max_model_len: int, |
| 19 | + max_num_seqs: int, |
| 20 | +): |
| 21 | + # regression test for https://github.com/vllm-project/vllm/issues/13929 |
| 22 | + from vllm.model_executor.models.mllama import calc_token_per_chunk |
| 23 | + |
| 24 | + model_config_kwargs = { |
| 25 | + "max_model_len": max_model_len, |
| 26 | + } |
| 27 | + ctx = build_model_context( |
| 28 | + model_id, |
| 29 | + model_config_kwargs=model_config_kwargs, |
| 30 | + limit_mm_per_prompt={"image": 1}, |
| 31 | + ) |
| 32 | + |
| 33 | + mm_config = ctx.get_mm_config() |
| 34 | + processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config) |
| 35 | + profiler = MultiModalProfiler(processor) |
| 36 | + |
| 37 | + dummy_encoder_data = profiler.get_encoder_dummy_data( |
| 38 | + max_model_len, |
| 39 | + mm_counts=mm_config.limit_per_prompt, |
| 40 | + ) |
| 41 | + dummy_mm_data = processor.dummy_inputs.get_dummy_processor_inputs( |
| 42 | + max_model_len, |
| 43 | + mm_counts=mm_config.limit_per_prompt, |
| 44 | + ) |
| 45 | + |
| 46 | + hf_config = ctx.get_hf_config(MllamaConfig) |
| 47 | + image_size = hf_config.vision_config.image_size |
| 48 | + encoder_seq_lens = [len(dummy_encoder_data.prompt_token_ids) |
| 49 | + ] * max_num_seqs |
| 50 | + |
| 51 | + mm_kwargs = processor.apply( |
| 52 | + prompt=dummy_mm_data.prompt_text, |
| 53 | + mm_data=dummy_mm_data.mm_data, |
| 54 | + hf_processor_mm_kwargs=dict(), |
| 55 | + )["mm_kwargs"] |
| 56 | + |
| 57 | + # Get the actual number of encoder tokens for each sample. |
| 58 | + # Because attn_metadata.encoder_seq_lens only counts the last |
| 59 | + # group of images for each sample, which is used to cheat the |
| 60 | + # block manager to allocate blocks for those images only. |
| 61 | + # See MllamaMultiModalProcessor for more details. |
| 62 | + num_tiles = [[t] for t in mm_kwargs.pop("num_tiles")] |
| 63 | + num_tokens_per_tile = calc_token_per_chunk(image_size) |
| 64 | + actual_encoder_seq_lens = [ |
| 65 | + sum(num_tile) * num_tokens_per_tile for num_tile in num_tiles |
| 66 | + ] |
| 67 | + |
| 68 | + # simulate mllama image-present prefill. |
| 69 | + for actual_len, last_group_len in zip(actual_encoder_seq_lens, |
| 70 | + encoder_seq_lens): |
| 71 | + assert actual_len >= last_group_len |
0 commit comments