| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | import vllm |
| 7 | from vllm.lora.request import LoRARequest |
| 8 | from vllm.platforms import current_platform |
| 9 | |
| 10 | from ..utils import create_new_process_for_each_test, multi_gpu_test |
| 11 | |
| 12 | MODEL_PATH = "hmellor/Ilama-3.2-1B" |
| 13 | |
| 14 | 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 |
| 15 | |
| 16 | EXPECTED_LORA_OUTPUT = [ |
| 17 | "SELECT count(*) FROM singer", |
| 18 | "SELECT avg(age) , min(age) , max(age) FROM singer WHERE country = 'France'", # noqa: E501 |
| 19 | "SELECT DISTINCT Country FROM singer WHERE Age > 20", |
| 20 | ] |
| 21 | |
| 22 | |
| 23 | def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> list[str]: |
| 24 | prompts = [ |
| 25 | PROMPT_TEMPLATE.format(query="How many singers do we have?"), |
| 26 | PROMPT_TEMPLATE.format( |
| 27 | query="What is the average, minimum, and maximum age of all singers from France?" # noqa: E501 |
| 28 | ), |
| 29 | PROMPT_TEMPLATE.format( |
| 30 | query="What are all distinct countries where singers above age 20 are from?" # noqa: E501 |
| 31 | ), |
| 32 | ] |
| 33 | sampling_params = vllm.SamplingParams(temperature=0, max_tokens=32) |
| 34 | outputs = llm.generate( |
| 35 | prompts, |
| 36 | sampling_params, |
| 37 | lora_request=LoRARequest(str(lora_id), lora_id, lora_path) if lora_id else None, |
| 38 | ) |
| 39 | # Print the outputs. |
| 40 | generated_texts: list[str] = [] |
| 41 | for output in outputs: |
| 42 | prompt = output.prompt |
| 43 | generated_text = output.outputs[0].text.strip() |
| 44 | generated_texts.append(generated_text) |
| 45 | print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") |
| 46 | return generated_texts |
| 47 | |
| 48 | |
| 49 | def test_ilama_lora(ilama_lora_files): |
| 50 | llm = vllm.LLM( |
| 51 | MODEL_PATH, |
| 52 | max_model_len=1024, |
| 53 | enable_lora=True, |
| 54 | max_loras=4, |
| 55 | max_lora_rank=16, |
| 56 | trust_remote_code=True, |
| 57 | enable_chunked_prefill=True, |
| 58 | ) |
| 59 | |
| 60 | output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 61 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 62 | assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 63 | output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 64 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 65 | assert output2[i] == EXPECTED_LORA_OUTPUT[i] |
| 66 | |
| 67 | |
| 68 | @pytest.mark.skipif( |
| 69 | current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests" |
| 70 | ) |
| 71 | @multi_gpu_test(num_gpus=4) |
| 72 | @create_new_process_for_each_test() |
| 73 | def test_ilama_lora_tp4(ilama_lora_files): |
| 74 | llm = vllm.LLM( |
| 75 | MODEL_PATH, |
| 76 | max_model_len=1024, |
| 77 | enable_lora=True, |
| 78 | max_loras=4, |
| 79 | max_lora_rank=16, |
| 80 | tensor_parallel_size=4, |
| 81 | trust_remote_code=True, |
| 82 | fully_sharded_loras=False, |
| 83 | enable_chunked_prefill=True, |
| 84 | ) |
| 85 | |
| 86 | output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 87 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 88 | assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 89 | output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 90 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 91 | assert output2[i] == EXPECTED_LORA_OUTPUT[i] |
| 92 | |
| 93 | |
| 94 | @pytest.mark.skipif( |
| 95 | current_platform.is_cuda_alike(), reason="Skipping to avoid redundant model tests" |
| 96 | ) |
| 97 | @multi_gpu_test(num_gpus=4) |
| 98 | @create_new_process_for_each_test() |
| 99 | def test_ilama_lora_tp4_fully_sharded_loras(ilama_lora_files): |
| 100 | llm = vllm.LLM( |
| 101 | MODEL_PATH, |
| 102 | max_model_len=1024, |
| 103 | enable_lora=True, |
| 104 | max_loras=4, |
| 105 | max_lora_rank=16, |
| 106 | tensor_parallel_size=4, |
| 107 | trust_remote_code=True, |
| 108 | fully_sharded_loras=True, |
| 109 | enable_chunked_prefill=True, |
| 110 | ) |
| 111 | output1 = do_sample(llm, ilama_lora_files, lora_id=1) |
| 112 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 113 | assert output1[i] == EXPECTED_LORA_OUTPUT[i] |
| 114 | output2 = do_sample(llm, ilama_lora_files, lora_id=2) |
| 115 | for i in range(len(EXPECTED_LORA_OUTPUT)): |
| 116 | assert output2[i] == EXPECTED_LORA_OUTPUT[i] |