Skip to content

[Perf] [SM120] dsv41: Read decode and verify WO-A from an exact FP8 copy of the weight - #43008

Closed
ormandj wants to merge 2 commits into
sgl-project:mainfrom
ormandj:dsv41-wo-a-fp8-weight
Closed

ormandj wants to merge 2 commits into
sgl-project:mainfrom
ormandj:dsv41-wo-a-fp8-weight

Conversation

@ormandj

@ormandj ormandj commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

DeepSeek-V4.1 checkpoints store WO-A as E4M3 with power-of-two 32x32 block scales, and the loader dequantizes it to BF16. On SM120, where the fused SM100 WO-A path does not apply, decode and target verify read the 16 MiB BF16 weight (TP4 shape [2, 1024, 4096]) in every layer, and this projection is bound by weight bandwidth. The E4M3 values and their scales reproduce the BF16 weight exactly from 8 MiB.

Modifications

  • quantize_wo_a_fp8 builds the E4M3 copy and returns None unless it reproduces the BF16 weight bit for bit.
  • wo_a_fp8_small_batch / wo_a_fp8_small_batch_mxfp8 (1-64 rows) use the existing split-K kernel, which now builds the BF16 operand from the E4M3 copy in registers.
  • _apply_wo_a_bf16_matmul uses the copy for single-row decode and for target verify up to 64 rows. Only DeepSeek-V4.1 on SM120 (compute capability 12.0) uses it; other GPUs, other models, and larger batches keep their current paths. When wo_b runs FlashInfer MXFP8, target verify of 9-64 rows now returns the MXFP8-quantized activation from the kernel's fused epilogue instead of BF16 that wo_b then quantizes. The copy costs 8 MiB per layer per rank and is built after loading. SGLANG_DSV41_WO_A_FP8_COPY=0 disables it.
  • Online weight updates keep working. The kernel takes an int32 device flag; while the flag is 0, it computes from the BF16 weight, including inside captured CUDA graphs.
    • WeightUpdater calls a new optional module hook, invalidate_derived_weights(), before every online update writes. That covers disk, distributed, tensor (including load_format="direct") and IPC updates, and the start of an update session. WO-A uses the hook to clear its flag. Modules that define both hooks are collected once per model. WeightChecker leaves the derived wo_a_fp8_* buffers out of its reset, comparison and checksum.
    • After the update, refresh_derived_weights() rewrites the copy in place. It runs from post_load_weights. At the end of an update session it runs once more: through post_load_weights when the session loaded nothing, or called directly by WeightUpdater when the session recorded a successful load (whose own post_load_weights already ran, or was skipped for load_format="direct"), so writes after the last load (for example P2P) are picked up. The flag is set again only if the new weight has an exact copy; if the refresh runs out of memory, the layer stays on the BF16 weight.
    • Each refresh rebuilds and checks the copy for every local WO-A layer, which includes a device sync per layer. On SM120 with DeepSeek-V4.1 this adds that work to startup and to every post_load_weights call of an online update.
  • Fix: the fused MXFP8 epilogue (_wo_a_reduce_quant) wrote scales correctly only for rows 0-31 of FlashInfer's 128x4 layout. It now matches mxfp8_quantize up to 128 rows. Current callers use at most 8 rows; this PR routes up to 64.

This PR is independent of other open PRs and applies to current main on its own.

Accuracy Tests

GPU tests in test_dsv4_wo_a_fp8.py (new, 19 cases) and test_dsv4_wo_a.py (3 cases) pass on an RTX PRO 6000 Blackwell (SM120) on this revision. The CPU tests in test_derived_weight_cache.py pass. test_deepseek_v4_unified_fp8_q_pair.py (from main) gives its stub layer the new wo_a_fp8_weight attribute. The new CPU test test_deepseek_v4_wo_a_fp8_dispatch.py checks with stubbed kernels that decode and target verify up to 64 rows take the copy (fused MXFP8 when requested) and that the BF16 kernels serve calls without the copy or above 64 rows.

  • The copy round-trips exactly over all finite E4M3 values. A perturbed weight gets no copy.
  • At 2-8 rows, outputs are within one BF16 ulp of wo_a_bf16_small_batch; 0.02-0.04% of them differ.
  • At 1-64 rows, max and mean error against FP64 match torch.bmm on the BF16 weight (ratio 1.000).
  • Batch invariance holds for row prefixes and permutations, across the 16/32/64 row tiles.
  • _quantize_partial output is bitwise equal to mxfp8_quantize at 1-8, 31-33, 63-65, 127 and 128 rows, and under graph replay at 6 and 40 rows. Before the fix, scales mismatch above 32 rows. The MXFP8 output of the new kernel matches mxfp8_quantize under graph replay at 1, 8, 33 and 64 rows.
  • With the flag cleared under CUDA-graph replay after the BF16 weight changed, the output is bitwise equal to wo_a_bf16_small_batch on the new weight at 2 and 8 rows, and at 33 rows its error against FP64 stays within one BF16 ulp of the largest output.
  • Layer lifecycle on GPU: a direct update makes a captured graph read the BF16 weight, a refresh without an exact copy keeps the fallback, an exact refresh rewrites the same buffers and re-enables the copy, and a refresh that runs out of memory stays on the BF16 weight.
  • The updater calls invalidate_derived_weights() before a direct update writes, and the update succeeds. An update session calls the invalidate hook at its start. At its end, WeightUpdater calls the refresh hook itself only when the session recorded a successful load; otherwise it leaves the refresh to the post_load_weights it runs. These CPU tests check the updater's own hook calls. The refresh from DeepseekV4ForCausalLM.post_load_weights is exercised at startup in the serving runs below, where it builds the copy; no unit test runs an online update through the full model.

Serving output quality, same build with the copy on and off (setup below): both arms pass the same gates, which are teacher-forced log-probability agreement with a reference (worst per-prompt mean difference 0.075 nats with the copy on, 0.048 and 0.094 nats in the two runs with it off; limit 0.15) and no repetition loops in 32 streams at 4 and 16 concurrent requests (temperature 1).

Speed Tests and Profiling

Per call on one RTX PRO 6000 Blackwell (SM120), TP4 shape, CUDA graph over 16 distinct layers so the weights are not L2-resident (us):

rows torch.bmm (BF16) wo_a_bf16_small_batch E4M3 copy (this PR)
1 13.5 - 5.2
2-8 24.7 13.3 5.2-5.5
9-16 24.7-24.8 - 5.5-5.8
32 25.0 - 8.2
64 20.3 - 12.9

Single-row decode on main uses wo_a_bf16_gemv, which this table does not include; the serving A/B below at one request is the end-to-end comparison for that path. Break-even with torch.bmm is near 96 rows, so the copy is capped at 64. The table was measured before the update flag was added. In a micro-benchmark with an L2-resident weight (20 calls per graph), the flag check adds about 4% at 1-16 rows. With the flag cleared, the kernel runs 3-11% slower than wo_a_bf16_small_batch at 2-8 rows (6.1-6.3 vs 5.5-5.9 us); only SM120 servers whose updated weight has no exact copy, or whose refresh ran out of memory, run that path.

Serving A/B with this change, same image, copy on vs SGLANG_DSV41_WO_A_FP8_COPY=0: DeepSeek-V4.1-Flash on 4x RTX PRO 6000 Blackwell (SM120, PCIe only, 250 W power limit), TP4/EP2, DSpark speculative decoding, temperature 1, decode from an empty context, up to 16 running requests. The build also carries other pending changes, identical in both arms. Each arm ran 3 warmed 30 s decode cells per concurrency; the copy-on arm is one run, the copy-off arm is two runs (6 repetitions), with matched GPU temperatures and clocks. Steps/s comes from the server's decode log lines at each batch size: generation throughput divided by accepted tokens per step, which is request verify steps per second summed over the batch (the target forward-pass rate times the batch size). At a fixed batch size its relative change equals the change in forward-pass rate, and it does not depend on DSpark acceptance length. Output tok/s is measured by the client.

concurrent requests (batch size) 1 2 4 8 16
steps/s, on (median) 86.5 148.2 225.6 308.8 474.2
steps/s, off (median) 85.7 145.6 217.3 283.5 483.1
change +0.9% +1.8% +3.8% +8.9% -1.8%
ranges separated overlap separated separated overlap
output tok/s, change +3.1% +3.5% +5.4% +6.0% -2.0%
tok/s ranges overlap overlap separated separated overlap

"Separated" means every copy-on repetition is above every copy-off repetition. The 16-request differences are within run-to-run variation. Prefill throughput at 8k, 32k and 128k tokens is unchanged (-0.2% to 0.0%).

Limitations: the copy-on run came first and was not repeated after the copy-off runs, so slow drift between runs is not excluded. The copy uses memory: in this configuration the KV pool shrank from 2,284,032 to 2,078,208 tokens with the copy on.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

Developed with AI assistance.


CI States

Latest PR Test (Base): ❌ Run #40960255867
Latest PR Test (Extra): ❌ Run #40960255931
Latest PR Test (AMD ROCm 10): ❌ Run #40960255953

@ormandj ormandj mentioned this pull request Oct 9, 2026
17 of 56 tasks
@ormandj ormandj changed the title [DSv4.1][SM120] Speed up decode/verify WO-A by reading an exact FP8 copy of the weight [Perf] [SM120] dsv41: Read decode and verify WO-A from an exact FP8 copy of the weight Oct 19, 2026
@ormandj
ormandj force-pushed the dsv41-wo-a-fp8-weight branch from 58adb15 to 5c62f33 Compare October 19, 2026 17:33
detain added a commit to detain/sglang that referenced this pull request Oct 20, 2026
…indexer) + stack_want decisions

- stack.txt: entries 1-2 (local) + Group A boot-critical indexer chain sgl-project#42371/sgl-project#42861/sgl-project#45261/sgl-project#42294/sgl-project#43008 (fe7be85..0dbd8c5).
- stack_want.txt: consolidated decision set from 8 parallel evaluators + dsv41(422)/SM120(254) sweeps + sybil/qwen harvests.
  Records E verdict (merge only sgl-project#43376) and D verdict (merge only sgl-project#42368; sgl-project#33407/sgl-project#34717 dead on flashinfer 0.7.2).
…weight

DeepSeek-V4.1 stores WO-A as E4M3 with power-of-two 32x32 block scales and
dequantizes it to BF16 at load. On SM120, single-row decode and target verify
up to 64 rows now read an E4M3 copy built after loading, which reproduces the
BF16 operand exactly and halves the bytes this bandwidth-bound projection
streams. Weights without an exact copy keep the BF16 kernels.

Online weight updates keep working: WeightUpdater calls an optional
invalidate_derived_weights() module hook before writing, which switches the
kernel to the BF16 weight through a device flag (so captured CUDA graphs
follow), and refresh_derived_weights() after post_load_weights and at the end
of an update session rewrites the copy in place, re-enabling it only when the
new weight has an exact copy.

Also write the fused MXFP8 epilogue's scales in FlashInfer's 128x4 layout for
any row count up to 128; the previous offset was correct for rows 0-31 only.
@ormandj
ormandj force-pushed the dsv41-wo-a-fp8-weight branch from 5c62f33 to 465a8cc Compare October 29, 2026 21:44
@github-actions

Copy link
Copy Markdown
Contributor

Thanks @ormandj. Closing this because you have 25 PRs open and none updated in 7 days, over our soft cap of 5 for idle PRs.

Reopen the two or three you most want reviewed - a short active set moves faster than a long idle one.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant