test_minicpmv_tp.py 4.01 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
from importlib.metadata import version

6
import pytest
7
from packaging.version import Version
8
9
10
11

import vllm
from vllm.assets.image import ImageAsset
from vllm.lora.request import LoRARequest
12
from vllm.platforms import current_platform
13

14
from ..utils import multi_gpu_test
15

16
17
18
19
20
21
22
23
pytestmark = pytest.mark.skipif(
    Version("5.0") <= Version(version("transformers")),
    reason=(
        "MiniCPMV custom processor uses tokenizer.im_start_id which is not "
        "available on TokenizersBackend in transformers v5.0+"
    ),
)

24
25
26
27
28
MODEL_PATH = "openbmb/MiniCPM-Llama3-V-2_5"

PROMPT_TEMPLATE = (
    "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n"
    "(<image>./</image>)\nWhat is in the image?<|eot_id|>"
29
30
    "<|start_header_id|>assistant<|end_header_id|>\n\n"
)
31
32
33
34
35
36
37
38
39
40
41

IMAGE_ASSETS = [
    ImageAsset("stop_sign"),
]

# After fine-tuning with LoRA, all generated content should start begin `A`.
EXPECTED_OUTPUT = [
    "A red and white stop sign with a Chinese archway in the background featuring red lanterns and gold accents.",  # noqa: E501
]


42
def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> list[str]:
43
44
45
46
47
48
    sampling_params = vllm.SamplingParams(
        temperature=0,
        max_tokens=5,
        stop_token_ids=[128001, 128009],  # eos_id, eot_id
    )

49
50
51
52
53
54
55
    inputs = [
        {
            "prompt": PROMPT_TEMPLATE,
            "multi_modal_data": {"image": asset.pil_image},
        }
        for asset in IMAGE_ASSETS
    ]
56
57
58
59

    outputs = llm.generate(
        inputs,
        sampling_params,
60
        lora_request=LoRARequest(str(lora_id), lora_id, lora_path) if lora_id else None,
61
62
    )
    # Print the outputs.
63
    generated_texts: list[str] = []
64
65
66
    for output in outputs:
        generated_text = output.outputs[0].text.strip()
        generated_texts.append(generated_text)
67
        print(f"Generated text: {generated_text!r}")
68
69
70
    return generated_texts


71
72
73
74
75
76
77
78
def test_minicpmv_lora(minicpmv_lora_files):
    llm = vllm.LLM(
        MODEL_PATH,
        max_num_seqs=2,
        enable_lora=True,
        max_loras=2,
        max_lora_rank=8,
        enforce_eager=True,
Jee Jee Li's avatar
Jee Jee Li committed
79
        max_model_len=2048,
80
        limit_mm_per_prompt={"image": 2, "video": 0},
81
82
83
84
85
86
87
88
89
90
        trust_remote_code=True,
    )
    output1 = do_sample(llm, minicpmv_lora_files, lora_id=1)
    for i in range(len(EXPECTED_OUTPUT)):
        assert EXPECTED_OUTPUT[i].startswith(output1[i])
    output2 = do_sample(llm, minicpmv_lora_files, lora_id=2)
    for i in range(len(EXPECTED_OUTPUT)):
        assert EXPECTED_OUTPUT[i].startswith(output2[i])


91
92
93
@pytest.mark.skipif(
    current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests"
)
94
@multi_gpu_test(num_gpus=4)
95
def test_minicpmv_tp4_wo_fully_sharded_loras(minicpmv_lora_files):
96
97
98
99
100
101
    llm = vllm.LLM(
        MODEL_PATH,
        enable_lora=True,
        max_num_seqs=2,
        max_loras=4,
        max_lora_rank=64,
102
        tensor_parallel_size=4,
103
        limit_mm_per_prompt={"image": 2, "video": 0},
104
105
106
107
108
109
110
        trust_remote_code=True,
    )
    output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1)
    for i in range(len(EXPECTED_OUTPUT)):
        assert EXPECTED_OUTPUT[i].startswith(output_tp[i])


111
112
113
@pytest.mark.skipif(
    current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests"
)
114
@multi_gpu_test(num_gpus=4)
115
def test_minicpmv_tp4_fully_sharded_loras(minicpmv_lora_files):
116
117
118
119
    llm = vllm.LLM(
        MODEL_PATH,
        enable_lora=True,
        max_num_seqs=2,
120
121
        max_loras=2,
        max_lora_rank=8,
122
123
        tensor_parallel_size=4,
        trust_remote_code=True,
124
        limit_mm_per_prompt={"image": 1, "video": 0},
125
        fully_sharded_loras=True,
126
127
128
129
    )
    output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1)
    for i in range(len(EXPECTED_OUTPUT)):
        assert EXPECTED_OUTPUT[i].startswith(output_tp[i])
130
131
132
    output_tp = do_sample(llm, minicpmv_lora_files, lora_id=2)
    for i in range(len(EXPECTED_OUTPUT)):
        assert EXPECTED_OUTPUT[i].startswith(output_tp[i])