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

zhuwenwen's avatar
zhuwenwen committed
4
import os
5
6
7
8
9
import pytest

import vllm
from vllm.assets.image import ImageAsset
from vllm.lora.request import LoRARequest
10
from vllm.platforms import current_platform
zhuwenwen's avatar
zhuwenwen committed
11
from ..utils import models_path_prefix
12

13
from ..utils import multi_gpu_test
14

zhuwenwen's avatar
zhuwenwen committed
15
MODEL_PATH = os.path.join(models_path_prefix, "openbmb/MiniCPM-Llama3-V-2_5")
16
17
18
19

PROMPT_TEMPLATE = (
    "<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n"
    "(<image>./</image>)\nWhat is in the image?<|eot_id|>"
20
21
    "<|start_header_id|>assistant<|end_header_id|>\n\n"
)
22
23
24
25
26
27
28
29
30
31
32

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
]


33
def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> list[str]:
34
35
36
37
38
39
    sampling_params = vllm.SamplingParams(
        temperature=0,
        max_tokens=5,
        stop_token_ids=[128001, 128009],  # eos_id, eot_id
    )

40
41
42
43
44
45
46
    inputs = [
        {
            "prompt": PROMPT_TEMPLATE,
            "multi_modal_data": {"image": asset.pil_image},
        }
        for asset in IMAGE_ASSETS
    ]
47
48
49
50

    outputs = llm.generate(
        inputs,
        sampling_params,
51
        lora_request=LoRARequest(str(lora_id), lora_id, lora_path) if lora_id else None,
52
53
    )
    # Print the outputs.
54
    generated_texts: list[str] = []
55
56
57
    for output in outputs:
        generated_text = output.outputs[0].text.strip()
        generated_texts.append(generated_text)
58
        print(f"Generated text: {generated_text!r}")
59
60
61
    return generated_texts


62
63
64
65
66
67
68
69
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
70
        max_model_len=2048,
71
        limit_mm_per_prompt={"image": 2, "video": 0},
72
73
74
75
76
77
78
79
80
81
        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])


82
83
84
@pytest.mark.skipif(
    current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests"
)
85
@multi_gpu_test(num_gpus=4)
86
def test_minicpmv_tp4_wo_fully_sharded_loras(minicpmv_lora_files):
87
88
89
90
91
92
    llm = vllm.LLM(
        MODEL_PATH,
        enable_lora=True,
        max_num_seqs=2,
        max_loras=4,
        max_lora_rank=64,
93
        tensor_parallel_size=4,
94
        limit_mm_per_prompt={"image": 2, "video": 0},
95
96
97
98
99
100
101
        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])


102
103
104
@pytest.mark.skipif(
    current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests"
)
105
@multi_gpu_test(num_gpus=4)
106
def test_minicpmv_tp4_fully_sharded_loras(minicpmv_lora_files):
107
108
109
110
    llm = vllm.LLM(
        MODEL_PATH,
        enable_lora=True,
        max_num_seqs=2,
111
112
        max_loras=2,
        max_lora_rank=8,
113
114
        tensor_parallel_size=4,
        trust_remote_code=True,
115
        limit_mm_per_prompt={"image": 1, "video": 0},
116
        fully_sharded_loras=True,
117
118
119
120
    )
    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])
121
122
    output_tp = do_sample(llm, minicpmv_lora_files, lora_id=2)
    for i in range(len(EXPECTED_OUTPUT)):
zhuwenwen's avatar
zhuwenwen committed
123
        assert EXPECTED_OUTPUT[i].startswith(output_tp[i])