test_quant_model.py 4.9 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7
8
9
10
# Adapted from
# https://github.com/fmmoret/vllm/blob/fm-support-lora-on-quantized-models/tests/lora/test_llama.py
from dataclasses import dataclass

import pytest

import vllm
11
from vllm.distributed import cleanup_dist_env_and_memory
12
from vllm.lora.request import LoRARequest
13
from vllm.platforms import current_platform
14
15
16
17
18
19
20
21


@dataclass
class ModelWithQuantization:
    model_path: str
    quantization: str


22
MODELS: list[ModelWithQuantization]
23
# AWQ quantization is currently not supported in ROCm.
24
if current_platform.is_rocm():
25
26
    MODELS = [
        ModelWithQuantization(
27
28
            model_path="TheBloke/TinyLlama-1.1B-Chat-v0.3-GPTQ", quantization="gptq"
        ),
29
30
31
32
    ]
else:
    MODELS = [
        ModelWithQuantization(
33
34
            model_path="TheBloke/TinyLlama-1.1B-Chat-v0.3-AWQ", quantization="awq"
        ),
35
        ModelWithQuantization(
36
37
            model_path="TheBloke/TinyLlama-1.1B-Chat-v0.3-GPTQ", quantization="gptq"
        ),
38
    ]
39
40


41
42
43
def do_sample(
    llm: vllm.LLM, lora_path: str, lora_id: int, max_tokens: int = 256
) -> list[str]:
44
45
46
47
48
49
50
51
52
53
    raw_prompts = [
        "Give me an orange-ish brown color",
        "Give me a neon pink color",
    ]

    def format_prompt_tuples(prompt):
        return f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"

    prompts = [format_prompt_tuples(p) for p in raw_prompts]

54
55
56
    sampling_params = vllm.SamplingParams(
        temperature=0, max_tokens=max_tokens, stop=["<|im_end|>"]
    )
57
58
59
    outputs = llm.generate(
        prompts,
        sampling_params,
60
61
        lora_request=LoRARequest(str(lora_id), lora_id, lora_path) if lora_id else None,
    )
62
    # Print the outputs.
63
    generated_texts: list[str] = []
64
65
66
67
68
69
70
71
72
    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        generated_texts.append(generated_text)
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
    return generated_texts


@pytest.mark.parametrize("model", MODELS)
73
def test_quant_model_lora(tinyllama_lora_files, model):
74
75
76
77
78
79
    llm = vllm.LLM(
        model=model.model_path,
        enable_lora=True,
        max_num_seqs=16,
        max_loras=4,
        max_model_len=400,
80
        gpu_memory_utilization=0.2,  # avoid OOM
81
        quantization=model.quantization,
82
        trust_remote_code=True,
83
        enable_chunked_prefill=True,
84
85
        tokenizer=tinyllama_lora_files,
    )
86
87
88
89
90
91

    if model.quantization is None:
        expected_lora_output = [
            "#ff8050",
            "#ff8080",
        ]
92
    elif model.quantization == "awq":
93
94
95
96
        expected_lora_output = [
            "#f07700: A v",
            "#f00000: A v",
        ]
97
    elif model.quantization == "gptq":
98
99
100
101
102
103
104
105
        expected_lora_output = [
            "#f08800: This is",
            "#f07788 \n#",
        ]

    def expect_match(output, expected_output):
        # HACK: GPTQ lora outputs are just incredibly unstable.
        # Assert that the outputs changed.
106
        if model.quantization == "gptq" and expected_output is expected_lora_output:
107
            for i, o in enumerate(output):
108
109
110
                assert o.startswith("#"), (
                    f"Expected example {i} to start with # but got {o}"
                )
111
112
113
114
115
116
117
            return
        assert output == expected_output

    max_tokens = 10

    print("lora adapter created")
    print("lora 1")
118
    output = do_sample(llm, tinyllama_lora_files, lora_id=1, max_tokens=max_tokens)
119
120
121
    expect_match(output, expected_lora_output)

    print("lora 2")
122
    output = do_sample(llm, tinyllama_lora_files, lora_id=2, max_tokens=max_tokens)
123
124
125
126
127
    expect_match(output, expected_lora_output)

    print("removing lora")

    del llm
128
    cleanup_dist_env_and_memory()
129
130
131


@pytest.mark.parametrize("model", MODELS)
132
def test_quant_model_tp_equality(tinyllama_lora_files, num_gpus_available, model):
133
134
    if num_gpus_available < 2:
        pytest.skip(f"Not enough GPUs for tensor parallelism {2}")
135
    if model.quantization == "gptq":
136
        pytest.skip("GPTQ lora outputs are just incredibly unstable")
137
138
139
140
141
    llm_tp1 = vllm.LLM(
        model=model.model_path,
        enable_lora=True,
        max_num_seqs=16,
        max_loras=4,
142
        gpu_memory_utilization=0.2,  # avoid OOM
143
        quantization=model.quantization,
144
        trust_remote_code=True,
145
146
        enable_chunked_prefill=True,
    )
147
148
149
    output_tp1 = do_sample(llm_tp1, tinyllama_lora_files, lora_id=1)

    del llm_tp1
150
    cleanup_dist_env_and_memory()
151

152
153
154
155
156
157
    llm_tp2 = vllm.LLM(
        model=model.model_path,
        enable_lora=True,
        max_num_seqs=16,
        max_loras=4,
        tensor_parallel_size=2,
158
        gpu_memory_utilization=0.2,  # avoid OOM
159
        quantization=model.quantization,
160
161
        enable_chunked_prefill=True,
    )
162
163
164
    output_tp2 = do_sample(llm_tp2, tinyllama_lora_files, lora_id=1)

    del llm_tp2
165
    cleanup_dist_env_and_memory()
166
167

    assert output_tp1 == output_tp2