test_llama4.py 3.21 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
7
8
9
"""Tests for Llama4's multimodal preprocessing kwargs."""

import pytest

from vllm.multimodal import MULTIMODAL_REGISTRY
from vllm.transformers_utils.tokenizer import encode_tokens

10
from ....conftest import ImageTestAssets
11
12
13
from ...utils import build_model_context


14
@pytest.mark.parametrize("model_id", ["meta-llama/Llama-4-Scout-17B-16E-Instruct"])
15
16
@pytest.mark.parametrize("mm_processor_kwargs", [{}])
@pytest.mark.parametrize("num_imgs", [1, 5])
17
@pytest.mark.parametrize("mm_processor_cache_gb", [0, 4])
18
19
@pytest.mark.parametrize("tokenized_prompt", [True, False])
def test_processor_override(
20
    image_assets: ImageTestAssets,
21
22
23
    model_id: str,
    mm_processor_kwargs: dict,
    num_imgs: int,
24
    mm_processor_cache_gb: int,
25
26
27
28
29
30
31
    tokenized_prompt: bool,
):
    """Ensure llama4 processor works properly."""
    ctx = build_model_context(
        model_id,
        mm_processor_kwargs=mm_processor_kwargs,
        limit_mm_per_prompt={"image": num_imgs},
32
        mm_processor_cache_gb=mm_processor_cache_gb,
33
34
35
36
37
38
39
    )
    processor = MULTIMODAL_REGISTRY.create_processor(ctx.model_config)
    config = processor.info.get_hf_config()
    tokenizer = processor.info.get_tokenizer()
    hf_processor = processor.info.get_hf_processor()
    vocab = tokenizer.get_vocab()

40
41
42
    prompt = (
        "<|begin_of_text|><|header_start|>user<|header_end|>"
        + "<|image|>" * num_imgs
43
        + "<|eot|><|header_start|>assistant<|header_end|>"
44
    )
45
46
    mm_data = {
        "image": [
47
            image_assets[(i % len(image_assets))].pil_image for i in range(num_imgs)
48
49
50
51
52
53
        ]
    }
    if tokenized_prompt:
        prompt = encode_tokens(tokenizer, prompt)

    processed_inputs = processor.apply(prompt, mm_data, mm_processor_kwargs)
54
    mm_data = processed_inputs["mm_kwargs"].get_data()
55
56
57
58
59
60

    # place holder replacements
    prompt_token_ids = processed_inputs["prompt_token_ids"]
    assert prompt_token_ids.count(config.boi_token_index) == num_imgs
    assert prompt_token_ids.count(config.eoi_token_index) == num_imgs
    assert prompt_token_ids.count(vocab[hf_processor.image_token]) == num_imgs
61
    aspect_ratios = mm_data["aspect_ratios"]
62
63
64
65
66
    num_x_separators = num_y_separators = 0
    for tiles_y, tiles_x in aspect_ratios:
        if tiles_x * tiles_y > 1:
            num_x_separators += (tiles_x - 1) * tiles_y
            num_y_separators += tiles_y
67
68
69
70
71
    assert prompt_token_ids.count(vocab[hf_processor.tile_token]) == num_x_separators
    assert (
        prompt_token_ids.count(vocab[hf_processor.tile_global_token])
        == num_y_separators
    )
72
73
74
75

    # image token offsets
    img_locs = processed_inputs["mm_placeholders"].get("image", [])
    assert len(img_locs) == num_imgs
76
77
78
    assert [img_loc.offset for img_loc in img_locs] == [
        i for i, v in enumerate(prompt_token_ids) if v == config.boi_token_index
    ]
79
80

    # patch sizes and masks
81
82
83
    num_patches_per_chunk = processor.info.get_patch_per_chunk(config.vision_config)
    assert (
        prompt_token_ids.count(config.image_token_index)
84
        == sum(mm_data["patches_per_image"]) * num_patches_per_chunk
85
86
    )
    assert len(mm_data["pixel_values"]) == sum(mm_data["patches_per_image"])