[Fix] deepseek-v4: FP8 quantize-dequantize the attention output before wo_a for V4.1-Flash-1017 - #45185
Open
05yuki wants to merge 1 commit into
Open
[Fix] deepseek-v4: FP8 quantize-dequantize the attention output before wo_a for V4.1-Flash-1017#4518505yuki wants to merge 1 commit into
05yuki wants to merge 1 commit into
Conversation
…o_a (V4.1-Flash-1017) DeepSeek-V4.1-Flash-1017's reference code rounds the attention output o to FP8 (blocks of 32 along each group's row, power-of-two scales) and back before the wo_a einsum, in the attention and in the DSpark draft's attention; its weights expect it. SGLANG_DSV4_O_FP8_QDQ_BLOCK=<n> turns the step on for the bf16 wo_a path (0, the default: as before). The kernel is an in-place Triton quantize-dequantize, bit-exact against the checkpoint's tilelang act_quant; GPUs older than sm89 get an fp32 emulation of the e4m3 rounding, also bit-exact (SGLANG_DSV4_O_FP8_QDQ_EMULATE=1 forces it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C2UBZdnrDZSBh8yQz8rc9g
05yuki
requested review from
Fridge003,
ch-wan,
fzyzcjy and
ispobock
as code owners
October 19, 2026 13:45
detain
added a commit
to detain/sglang
that referenced
this pull request
Oct 20, 2026
… correctness) - core batch [10-15]: sgl-project#43376 long-ctx indexer OOM bound, sgl-project#42368 FlashInfer-0.7 decode dispatch, sgl-project#39914 extra-KV 64-token page split + candidate fallback, sgl-project#42714 graph-safe page-split buffers, sgl-project#32464 SM120 sparse-prefill test gate, sgl-project#34027 int32->int64 page-split overflow guard. - Group C [16-23]: sgl-project#42717 TP-rank verify-budget, sgl-project#43225/sgl-project#43374 compact-verify seq_lens + padding renorm, sgl-project#42718 packed-verify token->request map, sgl-project#42713 forced-uniform capture metadata, sgl-project#45301 padded-query graph lifetime, sgl-project#44141 bounded-replay guard, sgl-project#45185 V4.1-Flash-1017 FP8 QDQ (env-gated, default OFF). sgl-project#42710 skipped (superseded upstream: step_logits already fp32).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
DeepSeek-V4.1-Flash-1017's reference code (
inference/model.pyin the checkpoint) rounds the attention outputoto FP8 and back before thewo_aeinsum. The rounding usesact_quant(o, 32, "ue8m0", ..., inplace=True): blocks of 32 values along each group's row, with power-of-two scales. The reference does this in the attention and in the DSpark draft's attention:The checkpoint's weights expect the rounded
o. SGLang's bf16wo_apath skips the step. That is the path 1017 takes: itsweight_block_sizeis[32, 32], sowo_a_fp8_gemm_enabledis false andwo_ais dequantized at load. On 1017 the step changesoby 1.8 to 2.6% (relative).Modifications
python/sglang/srt/models/deepseek_common/o_fp8_qdq.py(new): an in-place Triton FP8 (e4m3) quantize-dequantize.2^ceil(log2(amax / 448)), clamp to ±448, round to e4m3, multiply back.deepseek_v4.py(MQALayer) anddeepseek_v4_dspark.py(DSparkAttention): apply the step on the bf16wo_apath whenSGLANG_DSV4_O_FP8_QDQ_BLOCKis set.environ.py: two switches.SGLANG_DSV4_O_FP8_QDQ_BLOCK(default 0, off).SGLANG_DSV4_O_FP8_QDQ_EMULATEforces the emulated path, for checking it against the hardware conversion.The step is opt-in, so other DeepSeek-V4 checkpoints are untouched. The earlier V4.1 reference does not have it. The checkpoint's config carries no flag for it; 1017's
[32, 32]block size could switch it on automatically if you prefer that. I left it explicit.Accuracy Tests
The kernel was compared with the checkpoint's own tilelang
act_quanton bf16 tensors of the attention output's shape at T = 1, 3, 37 and 4096 tokens, including tiny and huge blocks and an all-zero block. 0 bf16 values differ, on the hardware conversion and on the emulation. 1017 runs with the step on in our setup: 2x RTX 5070 Ti, KTransformers CPU experts, TP2.Speed Tests and Profiling
One small elementwise kernel per attention layer on the bf16
wo_apath, only when enabled.Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_01C2UBZdnrDZSBh8yQz8rc9g
CI States
Latest PR Test (Base): ❌ Run #39262925866
Latest PR Test (Extra): ❌ Run #39262925552
Latest PR Test (AMD ROCm 10): ❌ Run #39262925672