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

4
import tempfile
5
from pathlib import Path
6
from typing import Any
7

8
import pytest
9
import torch
10

11
12
from tests.quantization.utils import is_quant_method_supported
from vllm import LLM, SamplingParams
13
from vllm.config import CompilationConfig, CompilationMode, CUDAGraphMode, PassConfig
14
from vllm.platforms import current_platform
15
from vllm.utils.torch_utils import is_torch_equal_or_newer
16

17
from ..utils import create_new_process_for_each_test
18
19


20
def models_list(*, all: bool = True, keywords: list[str] | None = None):
21
22
    TEST_MODELS: list[tuple[str, dict[str, Any]]] = [
        ("facebook/opt-125m", {}),
23
24
        (
            "neuralmagic/Llama-3.2-1B-Instruct-FP8-dynamic",
25
            {"dtype": torch.float16},
26
        ),
27
28
29
        ("meta-llama/Llama-3.2-1B-Instruct", {}),
    ]

30
    if all:
31
32
33
34
35
36
37
38
39
40
        TEST_MODELS.extend(
            [
                ("neuralmagic/Llama-3.2-1B-Instruct-quantized.w8a8", {}),
                (
                    "nm-testing/tinyllama-oneshot-w8w8-test-static-shape-change",
                    {"dtype": torch.float16},
                ),
            ]
        )

41
42
        # TODO: figure out why this fails.
        if False and is_quant_method_supported("gguf"):  # noqa: SIM223
43
44
45
            TEST_MODELS.append(
                ("TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF", {"quantization": "gguf"})
            )
46
47

        if is_quant_method_supported("gptq"):
48
49
50
            TEST_MODELS.append(
                ("TheBloke/TinyLlama-1.1B-Chat-v0.3-GPTQ", {"quantization": "gptq"})
            )
51
52

        if is_quant_method_supported("gptq_marlin"):
53
54
55
56
57
58
            TEST_MODELS.append(
                (
                    "TheBloke/TinyLlama-1.1B-Chat-v1.0-GPTQ",
                    {"quantization": "gptq_marlin"},
                )
            )
59

60
        if is_quant_method_supported("gptq_marlin_24"):
61
62
63
64
65
66
            TEST_MODELS.append(
                (
                    "alexm-nm/tinyllama-24-marlin24-4bit-g128",
                    {"quantization": "gptq_marlin_24"},
                )
            )
67

68
        if not current_platform.is_rocm() and is_quant_method_supported("awq"):
69
70
71
            TEST_MODELS.append(
                ("TheBloke/TinyLlama-1.1B-Chat-v0.3-AWQ", {"quantization": "AWQ"})
            )
72
73
74
75
76
77
78

    if keywords is None:
        return TEST_MODELS

    # filter by keywords
    pred = lambda model: any(keyword in model[0] for keyword in keywords)
    return list(filter(pred, TEST_MODELS))
79
80


81
@pytest.mark.parametrize(
82
83
    "compilation_mode",
    [CompilationMode.DYNAMO_TRACE_ONCE, CompilationMode.VLLM_COMPILE],
84
)
85
@pytest.mark.parametrize("model, model_kwargs", models_list(all=True))
86
@create_new_process_for_each_test()
87
88
def test_full_graph(
    monkeypatch: pytest.MonkeyPatch,
89
90
    model: str,
    model_kwargs: dict[str, Any],
91
    compilation_mode: int,
92
):
93
94
95
96
97
98
99
    if (
        "w8a8" in model
        or "w8w8" in model
        and current_platform.has_device_capability((10, 0))
    ):
        # int8 removed on Blackwell:
        pytest.skip("int8 support removed on Blackwell")
100

101
    with monkeypatch.context():
102
103
        print(f"MODEL={model}")

104
        run_model(compilation_mode, model, **model_kwargs)
105
106
107
108


# TODO(luka) add other supported compilation config scenarios here
@pytest.mark.parametrize(
109
    "compilation_config, model, model_kwargs",
110
    [
111
        # additional compile sizes, only some of the models
112
        (
113
            CompilationConfig(mode=CompilationMode.VLLM_COMPILE, compile_sizes=[1, 2]),
114
            *model_info,
115
        )
116
        for model_info in models_list(all=False)
117
118
    ]
    + [
119
        # RMSNorm + quant fusion, only 8-bit quant models
120
121
        (
            CompilationConfig(
122
                mode=CompilationMode.VLLM_COMPILE,
123
124
125
                custom_ops=["+rms_norm"],
                pass_config=PassConfig(enable_fusion=True, enable_noop=True),
            ),
126
            *model_info,
127
        )
128
        for model_info in models_list(keywords=["FP8-dynamic", "quantized.w8a8"])
129
130
    ]
    + [
131
        # Test depyf integration works
132
133
        (
            CompilationConfig(
134
                mode=CompilationMode.VLLM_COMPILE,
135
                debug_dump_path=Path(tempfile.gettempdir()),
136
            ),
137
138
            "facebook/opt-125m",
            {},
139
140
141
        ),
    ]
    + [
142
143
144
        # graph inductor partition
        (
            CompilationConfig(
145
                mode=CompilationMode.VLLM_COMPILE,
146
147
148
149
                # inductor graph partition uses
                # torch._C.Tag.cudagraph_unsafe to specify splitting ops
                use_inductor_graph_partition=True,
                cudagraph_mode=CUDAGraphMode.PIECEWISE,
150
151
                compile_sizes=[1, 2],
            ),
152
            *model_info,
153
        )
154
        for model_info in models_list(all=False)
155
        if is_torch_equal_or_newer("2.9.0.dev")
156
157
    ],
)
158
159
160
161
# only test some of the models
@create_new_process_for_each_test()
def test_custom_compile_config(
    compilation_config: CompilationConfig,
162
163
    model: str,
    model_kwargs: dict[str, Any],
164
):
165
166
167
168
169
170
171
172
    if (
        "w8a8" in model
        or "w8w8" in model
        and current_platform.has_device_capability((10, 0))
    ):
        # int8 removed on Blackwell:
        pytest.skip("int8 support removed on Blackwell")

173
174
175
176
    if compilation_config.use_inductor_graph_partition and not is_torch_equal_or_newer(
        "2.9.0.dev"
    ):
        pytest.skip("inductor graph partition is only available in PyTorch 2.9+")
177

178
    print(f"MODEL={model}")
179
    run_model(compilation_config, model, **model_kwargs)
180
181


182
@pytest.mark.parametrize(
183
184
    "compilation_mode",
    [CompilationMode.NONE, CompilationMode.VLLM_COMPILE],
185
)
186
def test_fp8_kv_scale_compile(compilation_mode: int):
187
188
189
190
191
192
193
    model = "Qwen/Qwen2-0.5B"
    model_kwargs = {
        "quantization": "fp8",
        "kv_cache_dtype": "fp8_e4m3",
        "calculate_kv_scales": True,
        "max_model_len": 512,
    }
194
    run_model(compilation_mode, model, **model_kwargs)
195
196


197
198
199
200
def run_model(compile_config: int | CompilationConfig, model: str, **model_kwargs):
    compilation_config = (
        compile_config
        if isinstance(compile_config, CompilationConfig)
201
        else CompilationConfig(mode=compile_config)
202
203
    )

204
205
206
207
208
209
210
    prompts = [
        "Hello, my name is",
        "The president of the United States is",
        "The capital of France is",
        "The future of AI is",
    ]
    sampling_params = SamplingParams(temperature=0)
211
212
213
214
215
216
217
218
    # Allow override from model_kwargs
    model_kwargs = {"tensor_parallel_size": 1, **model_kwargs}
    model_kwargs = {"disable_custom_all_reduce": True, **model_kwargs}

    # No cudagraphs by default
    if compilation_config.cudagraph_mode is None:
        compilation_config.cudagraph_mode = CUDAGraphMode.NONE

219
220
    llm = LLM(
        model=model,
221
        compilation_config=compilation_config,
222
223
224
225
226
227
228
229
230
        **model_kwargs,
    )
    outputs = llm.generate(prompts, sampling_params)

    # Print the outputs.
    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")