test_fp8.py 6.02 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
# flake8: noqa
"""Tests fp8 models against ground truth generation
Note: these tests will only pass on L4 GPU.
"""
import pytest

10
from tests.quantization.utils import is_quant_method_supported
11
from vllm.platforms import current_platform
12
from vllm.utils import STR_BACKEND_ENV_VAR
13

14
from ..utils import check_logprobs_close
zhuwenwen's avatar
zhuwenwen committed
15
from ...utils import models_path_prefix
16

17

18
@pytest.mark.skipif(not is_quant_method_supported("fp8"),
19
                    reason="fp8 is not supported on this GPU type.")
20
@pytest.mark.parametrize(
21
    "kv_cache_dtype,base_model,test_model",
22
23
    [
        # Test FP8 checkpoint w. fp8_e4m3 kv-cache scaling factors.
zhuwenwen's avatar
zhuwenwen committed
24
        ("fp8_e4m3", os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
zhuwenwen's avatar
zhuwenwen committed
25
         os.path.join(models_path_prefix, "nm-testing/Llama-3.2-1B-Instruct-FP8-KV")),
26
        # Test FP16 checkpoint w. fp8_e5m2 kv-cache.
zhuwenwen's avatar
zhuwenwen committed
27
        ("fp8_e5m2", os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct"),
zhuwenwen's avatar
zhuwenwen committed
28
         os.path.join(models_path_prefix, "meta-llama/Llama-3.2-1B-Instruct")),
29
        # Test BF16 checkpoint w. fp8_e4m3 kv-cache scaling factors in json.
zhuwenwen's avatar
zhuwenwen committed
30
31
        ("fp8_e4m3", 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"))
32
33
34
    ])
# Due to low-precision numerical divergence, we only test logprob of 4 tokens
@pytest.mark.parametrize("max_tokens", [4])
35
@pytest.mark.parametrize("enforce_eager", [True])
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@pytest.mark.parametrize("backend", ["FLASH_ATTN", "XFORMERS", "FLASHINFER"])
# 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])
# Due to low-precision numerical divergence, this test is too sensitive for
# the async postprocessor
@pytest.mark.parametrize("disable_async_output_proc", [True])
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,
    disable_async_output_proc: bool,
54
    monkeypatch: pytest.MonkeyPatch,
55
56
57
58
59
) -> None:
    """
    Only checks log probs match to cover the discrepancy in
    numerical sensitive kernels.
    """
60
61
62
63
64
65
66
67

    if backend == "FLASHINFER" and current_platform.is_rocm():
        pytest.skip("Flashinfer does not support ROCm/HIP.")

    if kv_cache_dtype == "fp8_e5m2" and current_platform.is_rocm():
        pytest.skip(
            f"{kv_cache_dtype} is currently not supported on ROCm/HIP.")

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    with monkeypatch.context() as m:
        m.setenv("TOKENIZERS_PARALLELISM", 'true')
        m.setenv(STR_BACKEND_ENV_VAR, backend)

        MAX_MODEL_LEN = 1024
        NUM_LOG_PROBS = 8

        with vllm_runner(
                base_model,
                max_model_len=MAX_MODEL_LEN,
                tensor_parallel_size=tensor_parallel_size,
                enforce_eager=enforce_eager,
                kv_cache_dtype="auto",
                disable_async_output_proc=disable_async_output_proc,
        ) as vllm_model:
            baseline_outputs = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, NUM_LOG_PROBS)

        with vllm_runner(
                test_model,
                max_model_len=MAX_MODEL_LEN,
                tensor_parallel_size=tensor_parallel_size,
                enforce_eager=enforce_eager,
                kv_cache_dtype=kv_cache_dtype,
                disable_async_output_proc=disable_async_output_proc,
        ) as vllm_model:
            test_outputs = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, NUM_LOG_PROBS)

        check_logprobs_close(
            outputs_0_lst=baseline_outputs,
            outputs_1_lst=test_outputs,
            name_0="fp16_kv_cache",
            name_1="fp8_kv_cache",
        )
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127


@pytest.mark.cpu_model
@pytest.mark.skipif(not current_platform.is_cpu(),
                    reason="test for the CPU backend.")
@pytest.mark.parametrize(
    "kv_cache_dtype,base_model,test_model",
    [
        # Test BF16 checkpoint w. fp8_e5m2 kv-cache.
        ("fp8_e5m2", "meta-llama/Llama-3.2-1B-Instruct",
         "meta-llama/Llama-3.2-1B-Instruct"),
    ])
# Due to low-precision numerical divergence, we only test logprob of 4 tokens
@pytest.mark.parametrize("max_tokens", [4])
# Due to low-precision numerical divergence, this test is too sensitive for
# the async postprocessor
@pytest.mark.parametrize("disable_async_output_proc", [True])
def test_cpu_models(
    vllm_runner,
    example_prompts,
    kv_cache_dtype: str,
    base_model: str,
    test_model: str,
    max_tokens: int,
    disable_async_output_proc: bool,
128
    monkeypatch: pytest.MonkeyPatch,
129
130
131
132
133
) -> None:
    """
    Only checks log probs match to cover the discrepancy in
    numerical sensitive kernels.
    """
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    with monkeypatch.context() as m:
        m.setenv("TOKENIZERS_PARALLELISM", 'true')

        MAX_MODEL_LEN = 1024
        NUM_LOG_PROBS = 8

        with vllm_runner(
                base_model,
                max_model_len=MAX_MODEL_LEN,
                dtype="bfloat16",
                kv_cache_dtype="auto",
                disable_async_output_proc=disable_async_output_proc,
        ) as vllm_model:
            baseline_outputs = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, NUM_LOG_PROBS)

        with vllm_runner(
                test_model,
                max_model_len=MAX_MODEL_LEN,
                dtype="bfloat16",
                kv_cache_dtype=kv_cache_dtype,
                disable_async_output_proc=disable_async_output_proc,
        ) as vllm_model:
            test_outputs = vllm_model.generate_greedy_logprobs(
                example_prompts, max_tokens, NUM_LOG_PROBS)

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