|
| 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 | +IMAGE_ASSETS = [ |
| 19 | + ImageAsset("stop_sign"), |
| 20 | + ImageAsset("cherry_blossom"), |
| 21 | +] |
| 22 | + |
| 23 | +# After fine-tuning with LoRA, all generated content should start begin `A`. |
| 24 | +EXPECTED_OUTPUT = [ |
| 25 | + "A red and white stop sign with a Chinese archway in the background featuring red lanterns and gold accents.", # noqa: E501 |
| 26 | + "A pink cherry blossom tree with a blue sky in the background.", |
| 27 | +] |
| 28 | + |
| 29 | + |
| 30 | +def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> List[str]: |
| 31 | + sampling_params = vllm.SamplingParams( |
| 32 | + temperature=0, |
| 33 | + max_tokens=5, |
| 34 | + stop_token_ids=[128001, 128009], # eos_id, eot_id |
| 35 | + ) |
| 36 | + |
| 37 | + inputs = [{ |
| 38 | + "prompt": PROMPT_TEMPLATE, |
| 39 | + "multi_modal_data": { |
| 40 | + "image": asset.pil_image |
| 41 | + }, |
| 42 | + } for asset in IMAGE_ASSETS] |
| 43 | + |
| 44 | + outputs = llm.generate( |
| 45 | + inputs, |
| 46 | + sampling_params, |
| 47 | + lora_request=LoRARequest(str(lora_id), lora_id, lora_path) |
| 48 | + if lora_id else None, |
| 49 | + ) |
| 50 | + # Print the outputs. |
| 51 | + generated_texts: List[str] = [] |
| 52 | + for output in outputs: |
| 53 | + prompt = output.prompt |
| 54 | + generated_text = output.outputs[0].text.strip() |
| 55 | + generated_texts.append(generated_text) |
| 56 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 57 | + return generated_texts |
| 58 | + |
| 59 | + |
| 60 | +@multi_gpu_test(num_gpus=2) |
| 61 | +@pytest.mark.parametrize("fully_sharded", [True, False]) |
| 62 | +def test_minicpmv_tp2(minicpmv_lora_files, fully_sharded): |
| 63 | + llm = vllm.LLM( |
| 64 | + MODEL_PATH, |
| 65 | + enable_lora=True, |
| 66 | + max_num_seqs=2, |
| 67 | + max_loras=4, |
| 68 | + max_lora_rank=64, |
| 69 | + tensor_parallel_size=2, |
| 70 | + trust_remote_code=True, |
| 71 | + fully_sharded_loras=fully_sharded, |
| 72 | + ) |
| 73 | + |
| 74 | + output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1) |
| 75 | + |
| 76 | + for i in range(len(EXPECTED_OUTPUT)): |
| 77 | + assert EXPECTED_OUTPUT[i].startswith(output_tp[i]) |
| 78 | + |
| 79 | + |
| 80 | +@multi_gpu_test(num_gpus=4) |
| 81 | +@pytest.mark.parametrize("fully_sharded", [True, False]) |
| 82 | +def test_minicpmv_tp4(minicpmv_lora_files, fully_sharded): |
| 83 | + llm = vllm.LLM( |
| 84 | + MODEL_PATH, |
| 85 | + enable_lora=True, |
| 86 | + max_num_seqs=2, |
| 87 | + max_loras=4, |
| 88 | + max_lora_rank=64, |
| 89 | + tensor_parallel_size=4, |
| 90 | + trust_remote_code=True, |
| 91 | + fully_sharded_loras=fully_sharded, |
| 92 | + ) |
| 93 | + output_tp = do_sample(llm, minicpmv_lora_files, lora_id=1) |
| 94 | + for i in range(len(EXPECTED_OUTPUT)): |
| 95 | + assert EXPECTED_OUTPUT[i].startswith(output_tp[i]) |
0 commit comments