untest_fp8.py 5.56 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
zhuwenwen's avatar
zhuwenwen committed
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
5
6
7
# flake8: noqa
"""Tests fp8 models against ground truth generation
Note: these tests will only pass on L4 GPU.
"""
8
import os
9
10
import pytest

11
from tests.quantization.utils import is_quant_method_supported
12
from vllm.v1.attention.backends.fa_utils import flash_attn_supports_fp8
13
from vllm.platforms import current_platform
zhuwenwen's avatar
zhuwenwen committed
14
15
from ..utils import check_logprobs_close
from ...utils import models_path_prefix
16

17

18
19
20
21
@pytest.mark.skipif(
    not is_quant_method_supported("fp8"),
    reason="fp8 is not supported on this GPU type.",
)
22
@pytest.mark.parametrize(
23
    "kv_cache_dtype,base_model,test_model",
24
25
    [
        # Test FP8 checkpoint w. fp8_e4m3 kv-cache scaling factors.
26
27
        (
            "fp8_e4m3",
28
29
            os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
            os.path.join(models_path_prefix, "nm-testing/Llama-3.2-1B-Instruct-FP8-KV"),
30
        ),
31
        # Test BF16 checkpoint w. fp8_e5m2 kv-cache.
32
33
        (
            "fp8_e5m2",
34
35
            os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
            os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
36
        ),
37
        # Test BF16 checkpoint w. fp8_e4m3 kv-cache scaling factors in json.
38
39
        (
            "fp8_e4m3",
40
41
            os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
            os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
42
43
44
        ),
    ],
)
45
46
# Due to low-precision numerical divergence, we only test logprob of 4 tokens
@pytest.mark.parametrize("max_tokens", [4])
47
@pytest.mark.parametrize("enforce_eager", [True])
48
@pytest.mark.parametrize("backend", ["FLASH_ATTN"])
49
50
51
52
53
54
55
56
57
58
59
60
61
# NOTE: Increasing this in this suite will fail CI because we currently cannot
# reset distributed env properly. Use a value > 1 just when you test.
@pytest.mark.parametrize("tensor_parallel_size", [1])
def test_models(
    vllm_runner,
    example_prompts,
    kv_cache_dtype: str,
    base_model: str,
    test_model: str,
    max_tokens: int,
    enforce_eager: bool,
    backend: str,
    tensor_parallel_size: int,
62
    monkeypatch: pytest.MonkeyPatch,
63
64
65
66
67
) -> None:
    """
    Only checks log probs match to cover the discrepancy in
    numerical sensitive kernels.
    """
zhuwenwen's avatar
zhuwenwen committed
68
69

    if kv_cache_dtype == "fp8_e5m2" and current_platform.is_rocm():
70
        pytest.skip(f"{kv_cache_dtype} is currently not supported on ROCm/HIP.")
zhuwenwen's avatar
zhuwenwen committed
71

72
73
74
75
    if not flash_attn_supports_fp8():
        pytest.skip(
            f"{kv_cache_dtype} is not supported on this GPU type with {backend} attention."
        )
76

77
    with monkeypatch.context() as m:
78
        m.setenv("TOKENIZERS_PARALLELISM", "true")
79
80
81
82
83

        MAX_MODEL_LEN = 1024
        NUM_LOG_PROBS = 8

        with vllm_runner(
84
85
86
87
88
            base_model,
            max_model_len=MAX_MODEL_LEN,
            tensor_parallel_size=tensor_parallel_size,
            enforce_eager=enforce_eager,
            kv_cache_dtype="auto",
89
            attention_config={"backend": backend},
90
91
        ) as vllm_model:
            baseline_outputs = vllm_model.generate_greedy_logprobs(
92
93
                example_prompts, max_tokens, NUM_LOG_PROBS
            )
94
95

        with vllm_runner(
96
97
98
99
100
            test_model,
            max_model_len=MAX_MODEL_LEN,
            tensor_parallel_size=tensor_parallel_size,
            enforce_eager=enforce_eager,
            kv_cache_dtype=kv_cache_dtype,
101
            attention_config={"backend": backend},
102
103
        ) as vllm_model:
            test_outputs = vllm_model.generate_greedy_logprobs(
104
105
                example_prompts, max_tokens, NUM_LOG_PROBS
            )
106
107
108
109
110
111
112

        check_logprobs_close(
            outputs_0_lst=baseline_outputs,
            outputs_1_lst=test_outputs,
            name_0="fp16_kv_cache",
            name_1="fp8_kv_cache",
        )
113
114
115


@pytest.mark.cpu_model
116
@pytest.mark.skipif(not current_platform.is_cpu(), reason="test for the CPU backend.")
117
118
119
120
@pytest.mark.parametrize(
    "kv_cache_dtype,base_model,test_model",
    [
        # Test BF16 checkpoint w. fp8_e5m2 kv-cache.
121
122
123
124
125
126
127
        (
            "fp8_e5m2",
            "meta-llama/Llama-3.2-1B-Instruct",
            "meta-llama/Llama-3.2-1B-Instruct",
        ),
    ],
)
128
129
130
131
132
133
134
135
136
# Due to low-precision numerical divergence, we only test logprob of 4 tokens
@pytest.mark.parametrize("max_tokens", [4])
def test_cpu_models(
    vllm_runner,
    example_prompts,
    kv_cache_dtype: str,
    base_model: str,
    test_model: str,
    max_tokens: int,
137
    monkeypatch: pytest.MonkeyPatch,
138
139
140
141
142
) -> None:
    """
    Only checks log probs match to cover the discrepancy in
    numerical sensitive kernels.
    """
143
    with monkeypatch.context() as m:
144
        m.setenv("TOKENIZERS_PARALLELISM", "true")
145
146
147
148
149

        MAX_MODEL_LEN = 1024
        NUM_LOG_PROBS = 8

        with vllm_runner(
150
151
152
153
            base_model,
            max_model_len=MAX_MODEL_LEN,
            dtype="bfloat16",
            kv_cache_dtype="auto",
154
155
        ) as vllm_model:
            baseline_outputs = vllm_model.generate_greedy_logprobs(
156
157
                example_prompts, max_tokens, NUM_LOG_PROBS
            )
158
159

        with vllm_runner(
160
161
162
163
            test_model,
            max_model_len=MAX_MODEL_LEN,
            dtype="bfloat16",
            kv_cache_dtype=kv_cache_dtype,
164
165
        ) as vllm_model:
            test_outputs = vllm_model.generate_greedy_logprobs(
166
167
                example_prompts, max_tokens, NUM_LOG_PROBS
            )
168
169
170
171
172
173
174

        check_logprobs_close(
            outputs_0_lst=baseline_outputs,
            outputs_1_lst=test_outputs,
            name_0="bf16_kv_cache",
            name_1="fp8_kv_cache",
        )