[DeepSeek-V4] Fuse UE8M0 scale rounding into FP8 group quantization (#26766)

Co-authored-by: liqichao <liqichao@baidu.com>
Co-authored-by: yhyang201 <yhyang201@gmail.com>
This commit is contained in:
Qichao Li
2026-06-18 14:41:56 -07:00
committed by GitHub
co-authored by liqichao yhyang201
parent 27a374eaef
commit bea282cede
6 changed files with 70 additions and 23 deletions
@@ -10,6 +10,7 @@ from sglang.srt.layers.quantization.fp8_kernel import (
fp8_dtype,
fp8_max,
fp8_min,
sglang_per_token_group_quant_fp8,
)
from sglang.test.ci.ci_register import register_cuda_ci
@@ -82,6 +83,32 @@ def test_v2_jit_matches_aot(dtype, num_tokens, hidden, fuse_silu_and_mul, scale_
assert torch.equal(x_s, s_ref), "scales differ"
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
@pytest.mark.parametrize("num_tokens", [1, 33, 128])
@pytest.mark.parametrize("hidden", [128, 512, 4096, 7168])
def test_sglang_per_token_group_quant_fp8_row_major_ue8m0(dtype, num_tokens, hidden):
"""Row-major scale_ue8m0=True quantizes WITH the rounded (power-of-2) scale.
Verify: (1) scales are exact powers of 2, (2) dequant ≈ original within FP8 tolerance.
"""
torch.manual_seed(num_tokens * 1000 + hidden)
x = torch.randn(num_tokens, hidden, device="cuda", dtype=dtype)
x_q, x_s = sglang_per_token_group_quant_fp8(x, G, scale_ue8m0=True)
torch.cuda.synchronize()
# Scales must be exact powers of 2
log2_s = torch.log2(x_s.abs())
assert torch.equal(log2_s, log2_s.round()), "scales are not power-of-2"
# Dequant should approximate original within FP8 precision
x_deq = x_q.float().view(num_tokens, -1, G) * x_s.unsqueeze(-1)
x_deq = x_deq.view(num_tokens, hidden)
rel_err = (x.float() - x_deq).abs() / (x.float().abs() + 1e-6)
assert (
rel_err.mean() < 0.05
), f"mean relative dequant error too large: {rel_err.mean():.4f}"
# Masked (EP-MoE) path: the v2 op only has a masked scheduler for the
# column-major + ue8m0 + fused-silu+mul + masked combination. Input is 3D
# [num_experts, tokens_padded, hidden*2]; only tokens < masked_m[e] are processed
@@ -41,7 +41,7 @@ class TestDSV4FlashFP4B200(
"""LowLatency recipe: TP=4, FP4 (mxfp4), EAGLE spec decoding."""
gsm8k_accuracy_thres = 0.93
accept_length_thres = 2.6
accept_length_thres = 2.8
bs_1_speed_thres = 220
@classmethod
@@ -95,7 +95,7 @@ class TestDSV4FlashFP4B200W4A4MegaMoE(
"""Balanced recipe: TP=4, DP=4, MegaMoE."""
gsm8k_accuracy_thres = 0.93
accept_length_thres = 2.6
accept_length_thres = 2.8
bs_1_speed_thres = 100
@classmethod