Skip to content

[Perf] dsv41: Restore the wide-prefill mHC stats overlap with the TP all-reduce - #42985

Closed
ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:mhc-prefill-stats-overlap
Closed

ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:mhc-prefill-stats-overlap

Conversation

@ormandj

@ormandj ormandj commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

#39704 ran each DeepSeek-V4.1 hyper-connection's mix statistics (the mix GEMM and Sinkhorn that produce pre, post and comb) on the model-wide stats stream beside that sublayer's TP all-reduce in wide target prefill. The stream forked after the sublayer's compute, right before the collective, and the main stream joined it before the post.

#42245 unified mHC into one state machine and listed this as "Dropped for now: wide-prefill stats multistream overlap". Since then deepseek_v4_mhc.use_stats_stream allows the stream only for decode and target verify, so in prefill run_attn_post and run_moe_post compute the triplet on the main stream after the all-reduce returns. That puts the mix GEMM and Sinkhorn on the critical path twice per layer, for every row of every prefill chunk. At 8192 rows each call reads the full [8192, 4 x 5120] BF16 residual.

This PR restores the overlap inside the state-machine design, with the gating #39704 used.

Modifications

  • deepseek_v4_mhc.use_prefill_stats_overlap(cfg, forward_batch, x) is the per-forward gate. It is true only when all of these hold, which are the conditions of [DSV4.1] Reduce mHC, metadata and small-batch router overhead #39704's _get_hc_stats_stream prefill branch:

    • pre_from_prev (V4.1) without prefill CP
    • CUDA rows, with 4096 <= rows <= 65536
    • extend without speculation
    • Blackwell (SM100 and SM120 alike)
    • attention DP 1, no SP, and batch-invariant mode off
  • deepseek_v4_mhc.issue_prefill_stats(hc, residual, stats_stream) forks the stats stream from the main stream and issues the triplet on it. It records the three coefficients on the main stream. It does not record the residual on the stats stream. The read state keeps the residual alive until HcUpdate.post has joined the stats stream, and the boundary releases it only after the post. Until a freed block returns to the driver, which synchronizes the device, the caching allocator reuses it only for allocations on the stream that allocated it (the main stream), so every reuse is ordered after the mix GEMM. Recording the residual would make the caching allocator hold each freed residual until the GPU caught up with the CPU. In a prefill forward that is most of the layers: an 8192-row chunk would hold about twenty [8192, 4, 5120] BF16 residuals (320 MiB each) at once.

  • In deepseek_v4_boundary, AttentionTransport.prepare_output and MoeTransport.prepare_output call _issue_prefill_stats right before they hand back the output that still owes its TP sum. This is the point just before the all-reduce, which is where [DSV4.1] Reduce mHC, metadata and small-batch router overhead #39704 forked. The call is skipped when:

    • the attention did not hand back an unreduced AttnOutput, for example because it reduced its own output
    • the transport defers the sum to a fused post
    • decode attention TP is active
    • the MoE has no owed sum, or a reduce-scatter already sums it
    • the layer's static gate is off
    • the read state already carries stats

    _issue_prefill_stats stores the triplet and the stream on the ReadState (stats, new stats_join), and HcUpdate.post joins stats_join before running the unchanged post with the precomputed triplet.

  • The static half of the gate is two cached properties on DeepseekV4DecoderLayerBase, matching [DSV4.1] Reduce mHC, metadata and small-batch router overhead #39704's layer conditions:

    • _can_overlap_attn_stats: pre_from_prev, a stats stream, and tp_size == attn_tp_size == 4
    • _can_overlap_ffn_stats: pre_from_prev, a stats stream, MoE tp_size == 4, and no MoE A2A backend
  • Decode and target verify are unchanged: they still fork before each combine through use_stats_stream. Other prefill widths, batch-invariant mode and the excluded layouts compute the triplet in line, as on main.

  • The kernels, their inputs and the all-reduce are the same as on main. Only the stream and the issue order of the mix kernels change.

Accuracy Tests

New CPU test test/registered/unit/models/test_deepseek_v4_mhc_prefill_overlap.py. It drives the real transports' prepare_output, the owed sum's completion and HcUpdate.post with a mocked layer and recorded streams, and it checks:

  • In EXTEND, MIXED and SPLIT_PREFILL at 4096, 8192 and 65536 rows, for the attention, the MoE and merged MoE output:
    • the order is fork, triplet on the stats stream, all-reduce on the main stream, join, post
    • the post receives the triplet
    • the three coefficients are recorded on the main stream
    • the residual is not recorded on the stats stream
  • At 6, 4095 and 65537 prefill rows, in decode and target verify, in batch-invariant mode, on non-Blackwell, with attention DP 2, SP, prefill CP, or no stats stream: the all-reduce and the post run on the main stream with nothing issued on the stats stream.
  • Stats already precomputed on the read state are kept.
  • The static attention and MoE gates.

The kernels, their inputs and the collective are unchanged, so prefill outputs are expected to match main. This expectation is not a measured result.

On the hardware below, short-prompt greedy outputs were bitwise identical between builds with and without this change. Long (8K to 32K) prefills are not bitwise reproducible from run to run even within one build on this setup, so they were not compared exactly. Teacher-forced log-probability and loop checks passed with this change.

Speed Tests and Profiling

Setup: 4x RTX PRO 6000 Blackwell (SM120), TP4 over PCIe (PCIe-IPC all-reduce), 250 W power limit, chunked prefill of 8192 tokens.

Regression after #42245, from a TP0 profiler trace of two cold 32K-token prefills (eight 8192-token chunks), comparing builds from before and after #42245:

before #42245 after #42245
prefill mix kernels on the stats stream 336 calls, 302 ms none
of which overlapping the PCIe-IPC all-reduce wait 261.5 ms none
prefill mix kernels on the main stream none 1,280 calls, 305 ms
main-stream busy time 4425.8 ms 4637.2 ms (+211 ms, about 26 ms per chunk)

End-to-end cold prefill throughput of the same two builds changed by -3.1%, -4.0% and -4.0% at 8K, 32K and 128K tokens. These builds also differ by other upstream commits in that range; the profile attributes the per-chunk loss to the mix kernels moving onto the main stream.

With this PR:

A build with this change on top of the same base: a TP0 trace of the same two cold 32K prefills shows the prefill mix kernels back on the stats stream, overlapping the all-reduce wait, and cold prefill throughput recovers to the pre-#42245 level:

prompt tokens before #42245 after #42245 after #42245 with this PR
8K 10,052 9,736 10,138
32K 12,561 12,057 12,634
128K 12,875 12,354 12,947

(tok/s, median of 3, same thermal state.) Decode is unaffected: the change only applies to extend batches of 4096 to 65536 rows.

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 #41019187062
Latest PR Test (Extra): ❌ Run #41019186967
Latest PR Test (AMD ROCm 10): ❌ Run #41019186942

@github-actions

This comment has been minimized.

@ormandj ormandj changed the title [DeepSeek-V4.1] Restore the wide-prefill mHC stats overlap with the TP all-reduce [Perf] dsv41: Restore the wide-prefill mHC stats overlap with the TP all-reduce Oct 19, 2026
detain added a commit to detain/sglang that referenced this pull request Oct 20, 2026
…/deferred/avoid map

- 3A [24-35]: NVFP4 MoE boot sgl-project#39807/sgl-project#40173, block-FP8 TP refine sgl-project#42566, k-pool indexer split sgl-project#43619,
  SM120 skinny GEMM sgl-project#42865/sgl-project#42867, DeepGEMM warmup guards sgl-project#32686/sgl-project#43712, autotune sgl-project#43363/sgl-project#40420,
  mHC overlap sgl-project#42985, FP8-einsum precompile sgl-project#42821.
- 3B [36-43]: PCIe CustomAllreduce VMM sgl-project#44448, EP local expert ids sgl-project#44480, shm TP broadcast sgl-project#44202,
  PCIe-IPC all-gather sgl-project#42863 + multicast guard sgl-project#41888, TP-sampling consistency sgl-project#41916/sgl-project#38565,
  draft embed/lm_head VRAM sgl-project#37155.
- sgl-project#39807 toolchain commit verified safe against our CUDA_HOME=12.9 pin (cu13 fallback needs CUDA_HOME unset AND glibc>=2.41; we are 2.39).
- stack_want.txt: execution-status banner. Branch tip 5dd6ec7.
…P all-reduce

sgl-project#42245 dropped the wide-prefill multistream overlap that sgl-project#39704 added: in
target prefill of 4096 to 65536 rows each sublayer's mix stats ran on the
stats stream beside that sublayer's TP all-reduce. Since then only decode and
verify use the stream, so in prefill the mix GEMM and Sinkhorn run after the
all-reduce on the critical path, twice per layer.

Bring it back on the layer-boundary transports (sgl-project#43607). When a sublayer's
TP all-reduce is still owed and not deferred to a fused post, the attention
and MoE transports fork the stats stream in prepare_output, issue the
triplet on it, and record the stream on the residual state; HcUpdate.post
waits on it before reading the triplet. Gating follows sgl-project#39704: lagged V4.1
layers, Blackwell, extend without speculation, 4096 to 65536 rows,
attention DP 1, no SP, no prefill CP, not batch-invariant, TP4 and no MoE
A2A backend. Decode and verify keep forking before the combine.
@ormandj
ormandj force-pushed the mhc-prefill-stats-overlap branch from a6a64b6 to bf274a7 Compare October 29, 2026 22:19
The wide-prefill overlap recorded each residual on the stats stream, so the
caching allocator kept every freed residual until the GPU caught up with the
CPU. In a prefill forward that is most of the layers: an 8K chunk held about
twenty 320 MiB residuals at once, and the allocator hit its OOM-retry path on
every chunk. The post already joins the stats stream before it reads the
residual, and the residual is released only after the post, so main-stream
reuse is ordered after the mix GEMM without the record.
…record

The caller keeps the residual alive until the main stream joins the stats
stream, and the caching allocator reuses a freed block only on the stream that
allocated it until the block returns to the driver.
ormandj added a commit to ormandj/sglang-deepseek-v41-flash-sm120 that referenced this pull request Oct 30, 2026
…stats stream

Patch 13 (sgl-project/sglang#42985 at e60bfae9de) no longer records each
residual on the mHC stats stream. The record kept every freed residual until
the GPU caught up with the CPU: an 8K prefill chunk held about twenty 320 MiB
residuals at once, and every chunk and startup warmup went through the caching
allocator's OOM retry. Same stack key.
@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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant