Skip to content

Commit be6c928

Browse files
committed
Add unit test
1 parent a9e724c commit be6c928

File tree

2 files changed

+99
-24
lines changed

2 files changed

+99
-24
lines changed

tests/lora/test_minicpmv.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
from typing import List
22

3-
import pytest
4-
53
import vllm
64
from vllm.assets.image import ImageAsset
75
from vllm.lora.request import LoRARequest
86

9-
from ..utils import multi_gpu_test
10-
117
MODEL_PATH = "openbmb/MiniCPM-Llama3-V-2_5"
128

139
PROMPT_TEMPLATE = (
@@ -73,23 +69,3 @@ def test_minicpmv_lora(minicpmv_lora_files):
7369
output2 = do_sample(llm, minicpmv_lora_files, lora_id=2)
7470
for i in range(len(EXPECTED_OUTPUT)):
7571
assert output2[i] == EXPECTED_OUTPUT[i]
76-
77-
78-
@multi_gpu_test(num_gpus=4)
79-
@pytest.mark.parametrize("fully_sharded", [True, False])
80-
@pytest.mark.parametrize("tp", [2, 4])
81-
def test_minicpmv_tensor_parallel(minicpmv_lora_files, fully_sharded, tp):
82-
llm = vllm.LLM(
83-
MODEL_PATH,
84-
enable_lora=True,
85-
max_num_seqs=16,
86-
max_loras=4,
87-
max_lora_rank=64,
88-
tensor_parallel_size=tp,
89-
trust_remote_code=True,
90-
fully_sharded_loras=fully_sharded,
91-
)
92-
output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1)
93-
94-
for i in range(len(EXPECTED_OUTPUT)):
95-
assert output_tp[i] == EXPECTED_OUTPUT[i]

tests/lora/test_minicpmv_tp.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
import vllm
6+
from vllm.assets.image import ImageAsset
7+
from vllm.lora.request import LoRARequest
8+
9+
from ..utils import multi_gpu_test
10+
11+
MODEL_PATH = "openbmb/MiniCPM-Llama3-V-2_5"
12+
13+
PROMPT_TEMPLATE = (
14+
"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n"
15+
"(<image>./</image>)\nWhat is in the image?<|eot_id|>"
16+
"<|start_header_id|>assistant<|end_header_id|>\n\n"
17+
)
18+
19+
IMAGE_ASSETS = [
20+
ImageAsset("stop_sign"),
21+
ImageAsset("cherry_blossom"),
22+
]
23+
24+
# After fine-tuning with LoRA, all generated content should start begin `A`.
25+
EXPECTED_OUTPUT = [
26+
"A red and white stop sign with a Chinese archway in the background featuring red lanterns and gold accents.", # noqa: E501
27+
"A pink cherry blossom tree with a blue sky in the background.",
28+
]
29+
30+
31+
def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> List[str]:
32+
sampling_params = vllm.SamplingParams(
33+
temperature=0,
34+
max_tokens=256,
35+
stop_token_ids=[128001, 128009], # eos_id, eot_id
36+
)
37+
38+
inputs = [
39+
{
40+
"prompt": PROMPT_TEMPLATE,
41+
"multi_modal_data": {"image": asset.pil_image},
42+
}
43+
for asset in IMAGE_ASSETS
44+
]
45+
46+
outputs = llm.generate(
47+
inputs,
48+
sampling_params,
49+
lora_request=LoRARequest(str(lora_id), lora_id, lora_path)
50+
if lora_id
51+
else None,
52+
)
53+
# Print the outputs.
54+
generated_texts: List[str] = []
55+
for output in outputs:
56+
prompt = output.prompt
57+
generated_text = output.outputs[0].text.strip()
58+
generated_texts.append(generated_text)
59+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
60+
return generated_texts
61+
62+
@multi_gpu_test(num_gpus=2)
63+
@pytest.mark.parametrize("fully_sharded", [True, False])
64+
def test_minicpmv_tp2(minicpmv_lora_files, fully_sharded):
65+
llm = vllm.LLM(
66+
MODEL_PATH,
67+
enable_lora=True,
68+
max_num_seqs=16,
69+
max_loras=4,
70+
max_lora_rank=64,
71+
tensor_parallel_size=2,
72+
trust_remote_code=True,
73+
fully_sharded_loras=fully_sharded,
74+
)
75+
76+
output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1)
77+
78+
for i in range(len(EXPECTED_OUTPUT)):
79+
assert output_tp[i] == EXPECTED_OUTPUT[i]
80+
81+
82+
@multi_gpu_test(num_gpus=4)
83+
@pytest.mark.parametrize("fully_sharded", [True, False])
84+
def test_minicpmv_tp4(minicpmv_lora_files, fully_sharded):
85+
llm = vllm.LLM(
86+
MODEL_PATH,
87+
enable_lora=True,
88+
max_num_seqs=16,
89+
max_loras=4,
90+
max_lora_rank=64,
91+
tensor_parallel_size=4,
92+
trust_remote_code=True,
93+
fully_sharded_loras=fully_sharded,
94+
)
95+
96+
output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1)
97+
98+
for i in range(len(EXPECTED_OUTPUT)):
99+
assert output_tp[i] == EXPECTED_OUTPUT[i]

0 commit comments

Comments
 (0)