| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | |
| 5 | # NOTE To avoid overloading the CI pipeline, this test script will not |
| 6 | # be triggered on CI and is primarily intended for local testing and verification. |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | import vllm |
| 11 | from vllm.lora.request import LoRARequest |
| 12 | |
| 13 | from ..utils import multi_gpu_test |
| 14 | |
| 15 | MODEL_PATH = "Qwen/Qwen3-30B-A3B" |
| 16 | |
| 17 | PROMPT_TEMPLATE = """<|im_start|>user |
| 18 | I want you to act as a SQL terminal in front of an example database, you need only to return the sql command to me.Below is an instruction that describes a task, Write a response that appropriately completes the request. |
| 19 | " |
| 20 | ##Instruction: |
| 21 | candidate_poll contains tables such as candidate, people. Table candidate has columns such as Candidate_ID, People_ID, Poll_Source, Date, Support_rate, Consider_rate, Oppose_rate, Unsure_rate. Candidate_ID is the primary key. |
| 22 | Table people has columns such as People_ID, Sex, Name, Date_of_Birth, Height, Weight. People_ID is the primary key. |
| 23 | The People_ID of candidate is the foreign key of People_ID of people. |
| 24 | |
| 25 | |
| 26 | ###Input: |
| 27 | {context} |
| 28 | |
| 29 | ###Response:<|im_end|> |
| 30 | <|im_start|>assistant""" # noqa: E501 |
| 31 | |
| 32 | EXPECTED_LORA_OUTPUT = [ |
| 33 | "<think>\n\n</think>\n\nSELECT count(*) FROM candidate", |
| 34 | "<think>\n\n</think>\n\nSELECT count(*) FROM candidate", |
| 35 | "<think>\n\n</think>\n\nSELECT poll_source FROM candidate GROUP BY poll_source ORDER BY count(*) DESC LIMIT 1", # noqa: E501 |
| 36 | "<think>\n\n</think>\n\nSELECT poll_source FROM candidate GROUP BY poll_source ORDER BY count(*) DESC LIMIT 1", # noqa: E501 |
| 37 | ] |
| 38 | |
| 39 | |
| 40 | def generate_and_test(llm: vllm.LLM, lora_path: str, lora_id: int) -> None: |
| 41 | prompts = [ |
| 42 | PROMPT_TEMPLATE.format(context="How many candidates are there?"), |
| 43 | PROMPT_TEMPLATE.format(context="Count the number of candidates."), |
| 44 | PROMPT_TEMPLATE.format( |
| 45 | context="Which poll resource provided the most number of candidate information?" # noqa: E501 |
| 46 | ), |
| 47 | PROMPT_TEMPLATE.format( |
| 48 | context="Return the poll resource associated with the most candidates." |
| 49 | ), |
| 50 | ] |
| 51 | sampling_params = vllm.SamplingParams(temperature=0, max_tokens=64) |
| 52 | outputs = llm.generate( |
| 53 | prompts, |
| 54 | sampling_params, |
| 55 | lora_request=LoRARequest(str(lora_id), lora_id, lora_path) if lora_id else None, |
| 56 | ) |
| 57 | # Print the outputs. |
| 58 | generated_texts: list[str] = [] |
| 59 | for output in outputs: |
| 60 | prompt = output.prompt |
| 61 | generated_text = output.outputs[0].text.strip() |
| 62 | generated_texts.append(generated_text) |
| 63 | print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 64 | |
| 65 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 66 | assert generated_texts[i].startswith(EXPECTED_LORA_OUTPUT[i]) |
| 67 | |
| 68 | |
| 69 | def test_qwen3moe_lora(qwen3moe_lora_files): |
| 70 | # We enable enforce_eager=True here to reduce VRAM usage for lora-test CI, |
| 71 | # Otherwise, the lora-test will fail due to CUDA OOM. |
| 72 | llm = vllm.LLM( |
| 73 | MODEL_PATH, |
| 74 | max_model_len=1024, |
| 75 | enable_lora=True, |
| 76 | max_loras=4, |
| 77 | enforce_eager=True, |
| 78 | trust_remote_code=True, |
| 79 | enable_chunked_prefill=True, |
| 80 | enable_mixed_moe_lora_format=True, |
| 81 | ) |
| 82 | |
| 83 | generate_and_test(llm, qwen3moe_lora_files, lora_id=1) |
| 84 | generate_and_test(llm, qwen3moe_lora_files, lora_id=2) |
| 85 | |
| 86 | |
| 87 | @multi_gpu_test(num_gpus=2) |
| 88 | @pytest.mark.parametrize("ep", [False, True]) |
| 89 | @pytest.mark.parametrize("enable_mixed_moe_lora_format", [False, True]) |
| 90 | def test_qwen3moe_lora_tp2(ep, qwen3moe_lora_files, enable_mixed_moe_lora_format): |
| 91 | llm = vllm.LLM( |
| 92 | MODEL_PATH, |
| 93 | max_model_len=1024, |
| 94 | enable_lora=True, |
| 95 | max_loras=4, |
| 96 | trust_remote_code=True, |
| 97 | enable_expert_parallel=ep, |
| 98 | tensor_parallel_size=2, |
| 99 | enable_mixed_moe_lora_format=enable_mixed_moe_lora_format, |
| 100 | ) |
| 101 | |
| 102 | generate_and_test(llm, qwen3moe_lora_files, lora_id=1) |
| 103 | generate_and_test(llm, qwen3moe_lora_files, lora_id=2) |
| 104 | |
| 105 | |
| 106 | @multi_gpu_test(num_gpus=4) |
| 107 | @pytest.mark.parametrize("ep", [False, True]) |
| 108 | @pytest.mark.parametrize("enable_mixed_moe_lora_format", [False, True]) |
| 109 | def test_qwen3moe_lora_tp4(ep, qwen3moe_lora_files, enable_mixed_moe_lora_format): |
| 110 | llm = vllm.LLM( |
| 111 | MODEL_PATH, |
| 112 | max_model_len=1024, |
| 113 | enable_lora=True, |
| 114 | max_loras=4, |
| 115 | trust_remote_code=True, |
| 116 | enable_expert_parallel=ep, |
| 117 | tensor_parallel_size=4, |
| 118 | enable_mixed_moe_lora_format=enable_mixed_moe_lora_format, |
| 119 | ) |
| 120 | |
| 121 | generate_and_test(llm, qwen3moe_lora_files, lora_id=1) |
| 122 | generate_and_test(llm, qwen3moe_lora_files, lora_id=2) |