|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +Tests for applying default registered multimodal loras. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from huggingface_hub import snapshot_download |
| 10 | + |
| 11 | +from vllm.lora.request import LoRARequest |
| 12 | + |
| 13 | +from ..conftest import AudioTestAssets, VllmRunner |
| 14 | + |
| 15 | +MODEL_PATH = snapshot_download("microsoft/Phi-4-multimodal-instruct") |
| 16 | +AUDIO_LORA_PATH = os.path.join(MODEL_PATH, "speech-lora") |
| 17 | +IMAGE_LORA_PATH = os.path.join(MODEL_PATH, "vision-lora") |
| 18 | + |
| 19 | +AUDIO_PROMPT = "<|user|><|audio_1|>Can you transcribe this audio?<|end|><|assistant|>" # noqa: E501 |
| 20 | + |
| 21 | +# Responses are greedy decoded; we just check the end of |
| 22 | +# the generated text. If the lora is inactive, this model |
| 23 | +# generates commentary on the transcription. |
| 24 | +RESPONSE_SUFFIX_WITH_LORA = "Spoken text: The first words I spoke in the original chronograph, a little piece of practical poetry. Mary had a little lamb, it slept with quite a snow, and everywhere that Mary went, the lamb was sure to go." # noqa: E501 |
| 25 | +RESPONSE_SUFFIX_WITHOUT_LORA = "Certainly! Here is the transcription of the audio you provided:\n\nThe first words I spoke in the original phonograph record: A little piece of practical poetry. Mary had a little lamb; its fleece was white as snow, and everywhere that Mary went, the lamb was sure to go." # noqa: E501 |
| 26 | + |
| 27 | +VLLM_RUNNER_BASE_KWARGS = { |
| 28 | + "model_name": MODEL_PATH, |
| 29 | + "dtype": "half", |
| 30 | + "enable_lora": "True", |
| 31 | + "max_num_seqs": 2, |
| 32 | + "max_lora_rank": 320, |
| 33 | + "max_model_len": 12800, |
| 34 | + "gpu_memory_utilization": 0.8, |
| 35 | + "limit_mm_per_prompt": { |
| 36 | + "audio": 1 |
| 37 | + }, |
| 38 | + "enforce_eager": True, |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +def run_test(vllm_runner, audio_assets, lora_request, expected_suffix, |
| 43 | + **kwargs): |
| 44 | + inputs = [([AUDIO_PROMPT], [audio_assets[0].audio_and_sample_rate[0]])] |
| 45 | + |
| 46 | + # Apply any additional kwargs as overrides to the base kwargs |
| 47 | + vllm_runner_kwargs = {**VLLM_RUNNER_BASE_KWARGS, **kwargs} |
| 48 | + |
| 49 | + with vllm_runner(**vllm_runner_kwargs) as vllm_model: |
| 50 | + vllm_outputs_with_default_lora = [ |
| 51 | + vllm_model.generate_greedy( |
| 52 | + prompts, |
| 53 | + max_tokens=128, |
| 54 | + audios=audios, |
| 55 | + lora_request=lora_request, |
| 56 | + ) for prompts, audios in inputs |
| 57 | + ] |
| 58 | + |
| 59 | + assert vllm_outputs_with_default_lora[-1][-1][-1].endswith( |
| 60 | + expected_suffix) |
| 61 | + |
| 62 | + |
| 63 | +def test_active_default_mm_lora( |
| 64 | + vllm_runner: type[VllmRunner], |
| 65 | + audio_assets: AudioTestAssets, |
| 66 | +): |
| 67 | + """Ensure that we can use the default audio lora.""" |
| 68 | + run_test( |
| 69 | + vllm_runner, |
| 70 | + audio_assets, |
| 71 | + lora_request=None, |
| 72 | + default_mm_loras={"audio": AUDIO_LORA_PATH}, |
| 73 | + expected_suffix=RESPONSE_SUFFIX_WITH_LORA, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def test_inactive_default_mm_lora( |
| 78 | + vllm_runner: type[VllmRunner], |
| 79 | + audio_assets: AudioTestAssets, |
| 80 | +): |
| 81 | + """Ensure that modalities are filtered properly.""" |
| 82 | + # Default image lora won't be active since we only pass audio |
| 83 | + run_test( |
| 84 | + vllm_runner, |
| 85 | + audio_assets, |
| 86 | + lora_request=None, |
| 87 | + default_mm_loras={"image": IMAGE_LORA_PATH}, |
| 88 | + expected_suffix=RESPONSE_SUFFIX_WITHOUT_LORA, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def test_default_mm_lora_succeeds_with_redundant_lora_request( |
| 93 | + vllm_runner: type[VllmRunner], |
| 94 | + audio_assets: AudioTestAssets, |
| 95 | +): |
| 96 | + """Ensure that redundantly providing the lora works.""" |
| 97 | + run_test( |
| 98 | + vllm_runner, |
| 99 | + audio_assets, |
| 100 | + lora_request=LoRARequest("audio", 1, AUDIO_LORA_PATH), |
| 101 | + default_mm_loras={"audio": AUDIO_LORA_PATH}, |
| 102 | + expected_suffix=RESPONSE_SUFFIX_WITH_LORA, |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def test_default_mm_lora_fails_with_overridden_lora_request( |
| 107 | + vllm_runner: type[VllmRunner], |
| 108 | + audio_assets: AudioTestAssets, |
| 109 | +): |
| 110 | + """Ensure that if the lora_request conflicts with default_mm_loras, |
| 111 | + we use the lora_request.""" |
| 112 | + run_test( |
| 113 | + vllm_runner, |
| 114 | + audio_assets, |
| 115 | + lora_request=LoRARequest("speech", 2, AUDIO_LORA_PATH), |
| 116 | + default_mm_loras={"audio": IMAGE_LORA_PATH}, |
| 117 | + expected_suffix=RESPONSE_SUFFIX_WITH_LORA, |
| 118 | + ) |
0 commit comments