test_gemma.py 2.13 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
4
from typing import List

5
6
import pytest

7
8
import vllm
from vllm.lora.request import LoRARequest
9
from vllm.platforms import current_platform
10
11
12
13

MODEL_PATH = "google/gemma-7b"


14
def do_sample(llm: vllm.LLM, lora_path: str, lora_id: int) -> List[str]:
15
16
17
    prompts = [
        "Quote: Imagination is",
        "Quote: Be yourself;",
18
        "Quote: Painting is poetry that is seen rather than felt,",
19
20
21
22
23
24
25
26
    ]
    sampling_params = vllm.SamplingParams(temperature=0, max_tokens=32)
    outputs = llm.generate(
        prompts,
        sampling_params,
        lora_request=LoRARequest(str(lora_id), lora_id, lora_path)
        if lora_id else None)
    # Print the outputs.
27
    generated_texts: List[str] = []
28
29
30
31
32
33
34
35
    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text.strip()
        generated_texts.append(generated_text)
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
    return generated_texts


36
37
38
39
40
41
42
43
@pytest.fixture(autouse=True)
def v1(run_with_both_engines_lora):
    # Simple autouse wrapper to run both engines for each test
    # This can be promoted up to conftest.py to run for every
    # test in a package
    pass


44
45
@pytest.mark.xfail(current_platform.is_rocm(),
                   reason="There can be output mismatch on ROCm")
46
47
48
49
def test_gemma_lora(gemma_lora_files):
    llm = vllm.LLM(MODEL_PATH,
                   max_model_len=1024,
                   enable_lora=True,
50
51
                   max_loras=4,
                   enable_chunked_prefill=True)
52
53
54
55

    expected_lora_output = [
        "more important than knowledge.\nAuthor: Albert Einstein\n",
        "everyone else is already taken.\nAuthor: Oscar Wilde\n",
56
57
        "and poetry is painting that is felt rather than seen.\n"
        "Author: Leonardo da Vinci\n",
58
59
60
61
62
63
64
65
    ]

    output1 = do_sample(llm, gemma_lora_files, lora_id=1)
    for i in range(len(expected_lora_output)):
        assert output1[i].startswith(expected_lora_output[i])
    output2 = do_sample(llm, gemma_lora_files, lora_id=2)
    for i in range(len(expected_lora_output)):
        assert output2[i].startswith(expected_lora_output[i])