|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import vllm |
| 8 | +from tests.utils import fork_new_process_for_each_test |
| 9 | +from vllm.lora.request import LoRARequest |
| 10 | + |
| 11 | +from ..utils import multi_gpu_test |
| 12 | + |
| 13 | +MODEL_PATH = "ArthurZ/ilama-3.2-1B" |
| 14 | + |
| 15 | +PROMPT_TEMPLATE = """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.\n"\n##Instruction:\nconcert_singer contains tables such as stadium, singer, concert, singer_in_concert. Table stadium has columns such as Stadium_ID, Location, Name, Capacity, Highest, Lowest, Average. Stadium_ID is the primary key.\nTable singer has columns such as Singer_ID, Name, Country, Song_Name, Song_release_year, Age, Is_male. Singer_ID is the primary key.\nTable concert has columns such as concert_ID, concert_Name, Theme, Stadium_ID, Year. concert_ID is the primary key.\nTable singer_in_concert has columns such as concert_ID, Singer_ID. concert_ID is the primary key.\nThe Stadium_ID of concert is the foreign key of Stadium_ID of stadium.\nThe Singer_ID of singer_in_concert is the foreign key of Singer_ID of singer.\nThe concert_ID of singer_in_concert is the foreign key of concert_ID of concert.\n\n###Input:\n{query}\n\n###Response:""" # noqa: E501 |
| 16 | + |
| 17 | +EXPECTED_LORA_OUTPUT = [ |
| 18 | + "SELECT count(*) FROM singer", |
| 19 | + "SELECT avg(age) , min(age) , max(age) FROM singer WHERE country = 'France'", # noqa: E501 |
| 20 | + "SELECT DISTINCT Country FROM singer WHERE Age > 20", |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> List[str]: |
| 25 | + prompts = [ |
| 26 | + PROMPT_TEMPLATE.format(query="How many singers do we have?"), |
| 27 | + PROMPT_TEMPLATE.format( |
| 28 | + query= |
| 29 | + "What is the average, minimum, and maximum age of all singers from France?" # noqa: E501 |
| 30 | + ), |
| 31 | + PROMPT_TEMPLATE.format( |
| 32 | + query= |
| 33 | + "What are all distinct countries where singers above age 20 are from?" # noqa: E501 |
| 34 | + ), |
| 35 | + ] |
| 36 | + sampling_params = vllm.SamplingParams(temperature=0, max_tokens=32) |
| 37 | + outputs = llm.generate( |
| 38 | + prompts, |
| 39 | + sampling_params, |
| 40 | + lora_request=LoRARequest(str(lora_id), lora_id, lora_path) |
| 41 | + if lora_id else None) |
| 42 | + # Print the outputs. |
| 43 | + generated_texts: List[str] = [] |
| 44 | + for output in outputs: |
| 45 | + prompt = output.prompt |
| 46 | + generated_text = output.outputs[0].text.strip() |
| 47 | + generated_texts.append(generated_text) |
| 48 | + print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 49 | + return generated_texts |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(autouse=True) |
| 53 | +def v1(run_with_both_engines_lora): |
| 54 | + # Simple autouse wrapper to run both engines for each test |
| 55 | + # This can be promoted up to conftest.py to run for every |
| 56 | + # test in a package |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.skip_v1 |
| 61 | +@fork_new_process_for_each_test |
| 62 | +def test_ilama_lora(ilama_lora_files): |
| 63 | + llm = vllm.LLM(MODEL_PATH, |
| 64 | + max_model_len=1024, |
| 65 | + enable_lora=True, |
| 66 | + max_loras=4, |
| 67 | + max_lora_rank=16, |
| 68 | + tensor_parallel_size=1, |
| 69 | + trust_remote_code=True, |
| 70 | + enable_chunked_prefill=True) |
| 71 | + |
| 72 | + output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 73 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 74 | + assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 75 | + output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 76 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 77 | + assert output2[i] == EXPECTED_LORA_OUTPUT[i] |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skip_v1 |
| 81 | +@multi_gpu_test(num_gpus=4) |
| 82 | +@fork_new_process_for_each_test |
| 83 | +def test_ilama_lora_tp4(ilama_lora_files): |
| 84 | + llm = vllm.LLM(MODEL_PATH, |
| 85 | + max_model_len=1024, |
| 86 | + enable_lora=True, |
| 87 | + max_loras=4, |
| 88 | + max_lora_rank=16, |
| 89 | + tensor_parallel_size=4, |
| 90 | + trust_remote_code=True, |
| 91 | + fully_sharded_loras=False, |
| 92 | + enable_chunked_prefill=True) |
| 93 | + |
| 94 | + output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 95 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 96 | + assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 97 | + output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 98 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 99 | + assert output2[i] == EXPECTED_LORA_OUTPUT[i] |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.skip_v1 |
| 103 | +@multi_gpu_test(num_gpus=4) |
| 104 | +@fork_new_process_for_each_test |
| 105 | +def test_ilama_lora_tp4_fully_sharded_loras(ilama_lora_files): |
| 106 | + llm = vllm.LLM(MODEL_PATH, |
| 107 | + max_model_len=1024, |
| 108 | + enable_lora=True, |
| 109 | + max_loras=4, |
| 110 | + max_lora_rank=16, |
| 111 | + tensor_parallel_size=4, |
| 112 | + trust_remote_code=True, |
| 113 | + fully_sharded_loras=True, |
| 114 | + enable_chunked_prefill=True) |
| 115 | + output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 116 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 117 | + assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 118 | + output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 119 | + for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 120 | + assert output2[i] == EXPECTED_LORA_OUTPUT[i] |
0 commit comments