main
py 79 lines 2.59 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 """E2E tests for online FP8 per-channel quantization.
5
6 Loads a BF16 model with ``--quantization fp8_per_channel`` (online
7 quantization) and compares log-probabilities against the same model served in
8 BF16 without quantization. This exercises the full pipeline: config parsing,
9 ``Fp8PtpcOnlineLinearMethod``, ``Fp8PtpcOnlineMoEMethod``, weight
10 loading, online quantization / shuffling, and inference.
11
12 ``example_prompts`` is a pytest fixture (from conftest.py) that loads 8
13 diverse prompts from ``tests/prompts/example.txt``.
14 """
15
16 import pytest
17
18 from tests.quantization.utils import is_quant_method_supported
19
20 from ..utils import check_logprobs_close
21
22 # Small MoE model that fits on a single GPU and exercises both linear + MoE.
23 MOE_MODEL = "allenai/OLMoE-1B-7B-0125-Instruct"
24 # Small dense model (no MoE) to validate the linear-only path.
25 DENSE_MODEL = "Qwen/Qwen3-0.6B"
26
27 MAX_MODEL_LEN = 1024
28 MAX_TOKENS = 4
29 NUM_LOG_PROBS = 8
30
31
32 @pytest.mark.skipif(
33 not is_quant_method_supported("fp8"),
34 reason="fp8 is not supported on this GPU type.",
35 )
36 @pytest.mark.quant_model
37 @pytest.mark.parametrize("model", [DENSE_MODEL, MOE_MODEL], ids=["dense", "moe"])
38 def test_fp8_per_channel_logprobs(
39 vllm_runner,
40 quant_baseline_logprobs,
41 example_prompts,
42 model: str,
43 monkeypatch: pytest.MonkeyPatch,
44 ) -> None:
45 """Compare BF16 baseline logprobs against online per-channel-quantized
46 model.
47
48 Runs the same model twice -- once in BF16 (baseline) and once with online
49 FP8 per-channel quantization -- then checks that the top log-probabilities
50 are close. Only 4 tokens are generated to keep the test fast while still
51 catching numerical divergence beyond expected per-channel error.
52 """
53 with monkeypatch.context() as m:
54 m.setenv("TOKENIZERS_PARALLELISM", "true")
55
56 baseline_outputs = quant_baseline_logprobs(
57 model,
58 example_prompts,
59 max_model_len=MAX_MODEL_LEN,
60 max_tokens=MAX_TOKENS,
61 num_logprobs=NUM_LOG_PROBS,
62 )
63
64 with vllm_runner(
65 model,
66 max_model_len=MAX_MODEL_LEN,
67 enforce_eager=True,
68 quantization="fp8_per_channel",
69 ) as vllm_model:
70 test_outputs = vllm_model.generate_greedy_logprobs(
71 example_prompts, MAX_TOKENS, NUM_LOG_PROBS
72 )
73
74 check_logprobs_close(
75 outputs_0_lst=baseline_outputs,
76 outputs_1_lst=test_outputs,
77 name_0="bf16",
78 name_1="fp8_per_channel",
79 )