Skip to content

[Perf] dsv41: Speed up decode-size mHC mixing with a vectorized split-K reduce and 160 K slices on SM120 - #42864

Closed
ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:mhc-mix-vec-reduce
Closed

ormandj wants to merge 3 commits into
sgl-project:mainfrom
ormandj:mhc-mix-vec-reduce

Conversation

@ormandj

@ormandj ormandj commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

hc_mix_stats_sinkhorn is the CUDA Triton path for DeepSeek-V4.x mHC mixing statistics and Sinkhorn, run twice per layer. It is used at every row count on SM120, on SM100 when the DeepGEMM prenorm GEMM is not used (including below 128 rows), and on SM90 at one row. It has two launches:

  1. _hc_mix_stats_partial_kernel: one CTA per (row tile, K slice). K=20480 is split into 80 slices.
  2. _hc_mix_reduce_sinkhorn_kernel: one single-warp CTA per row, which adds the 80 slice partials in a serial static_range loop before the Sinkhorn.

At decode sizes the serial loop takes about as long as the partial kernel. With one row tile, 80 CTAs also occupy fewer than half of the 188 SMs on an RTX PRO 6000.

Modifications

  • _hc_mix_reduce_sinkhorn_vec_kernel: loads the partials as one [slices, cols] tile and reduces them with tl.sum, then runs the unchanged Sinkhorn. There is still one CTA per row, and the reduction tree depends only on the slice count, so results are deterministic and batch-invariant. The summation order differs from the serial loop. hc_mix_stats_sinkhorn uses it on SM120 (compute capability 12.0), where it was measured; other GPUs keep the serial reducer, so their results are unchanged. The bf16x3 and DeepGEMM paths keep the serial reducer with 16 slices; DeepGEMM also keeps its residual input.
  • hc_mix_stats_sinkhorn(..., decode=False): with decode=True on SM120 (compute capability 12.0 only), it splits K into 160 slices when the number of 64-wide K blocks is a multiple of 160. DeepseekV4DecoderLayer.forward_hc_pre_from_prev sets decode for decode/idle and target-verify forward passes and passes it through mhc.run_attn_post / run_moe_post, _compute_triplet, mix_stats and _mix_stats_impl to hc_mix_stats_sinkhorn. Each new decode parameter defaults to False. Prefill/extend, other callers (including the DSpark draft layer, which calls mix_stats directly), other GPUs, and the unfused hc_mix_stats keep the existing choice (80 for K=20480). The slice count depends on K, the device, and the forward mode, never on M. A row therefore gets the same bits at any batch size within decode/verify on this backend, and likewise within prefill. A decode row can also run beside prefill rows in a MIXED batch, which takes the prefill split, so with batch-invariant mode enabled (--enable-deterministic-inference) _mix_stats_impl passes decode=False in every mode, as it already disables the compensated projections in that mode.

Accuracy Tests

New test_hc_mix_stats_sinkhorn.py (GPU) and test_deepseek_v4_mhc_decode_split.py (CPU). With this branch head's mhc.py and tests, all 23 tests in test_hc_mix_stats_sinkhorn.py passed on an RTX PRO 6000 Blackwell (SM120), and the 4 tests in test_deepseek_v4_mhc_decode_split.py passed on CPU. I have not run the ported model code end to end on a GPU (deepseek_v4.py, deepseek_v4_mhc.py on current main). The tests cover:

  • Slice selection: 160 for decode and 80 for prefill on SM120; 80 for both on SM100 and SM121; 80 for a K whose 64-wide block count is not a multiple of 160.
  • Accuracy: K=20480 in decode mode at M = 1, 6, 48 and 300 (160 slices) and in prefill mode at M = 6 and 300 (80 slices), plus K = 5120, 4096 and 192 (80, 64 and 1 slices). The fused op matches the unfused hc_mix_stats (serial sum, 80 slices at K=20480) followed by the Sinkhorn within rtol 1e-5 / atol 1e-6. It also matches FP64 mixing statistics followed by the FP32 Sinkhorn within rtol 1e-4 / atol 1e-5.
  • Determinism and batch invariance in each mode, with the device forced to SM120 (vectorized reduce) and to SM100 (serial reduce) on any host GPU, as in the accuracy cases. The test checks the selected reducer and slice count, that repeated calls are bitwise equal, and that rows computed alone, as the first 8, 9, 2048 and 2049, and as every 7th of a 2056-row batch are bitwise equal to the same rows in that batch. These subsets use all three row tiles (8, 16 and 32 rows; 32 starts at 2049 rows).
  • Model wiring, on CPU with a mocked layer and no weights: the real forward_hc_pre_from_prev, posts and mix_stats pass decode=True to hc_mix_stats_sinkhorn for both hyper-connections in decode, idle and target-verify forward passes, and decode=False in extend, mixed, draft-extend and split-prefill passes. With batch-invariant mode enabled, every forward mode passes decode=False, so decode and MIXED batches select the same slice count (80 on SM120). A direct mix_stats call defaults to decode=False.

On 2026-10-29 this branch was rebased onto main 974831643f, where the boundary read (HcReadCall.read) records the decode flag and HcUpdate.post passes it to the posts; on the new head the 4 CPU tests in test_deepseek_v4_mhc_decode_split.py (now driving HcReadCall.read and HcUpdate.post) pass, and the SM120 GPU results in this description are from the previous head and will be re-run.

Speed Tests and Profiling

Per call (partial + reduce/Sinkhorn, us) on one RTX PRO 6000 Blackwell Max-Q Workstation Edition (SM120, 188 SMs, 250 W power limit, used only by the benchmark), torch 2.14.1+cu130, Triton 3.8.0. Inputs are BF16 [M, 20480]. Each CUDA graph covers 24 distinct [24, 20480] FP32 weights at M <= 64 and 4 weights above; the time is the best of 20 graph replays divided by the number of calls. Columns:

  • main: hc_mix_stats_sinkhorn from the base commit's mhc.py (serial reduce, 80 slices);
  • decode=False: this PR's mhc.py (vectorized reduce, 80 slices);
  • decode=True: this PR's mhc.py (vectorized reduce, 160 slices, the SM120 decode/verify setting);
  • max abs diff: the largest absolute difference over the pre, post and comb outputs between decode=True and main, for the first weight.
M main decode=False decode=True max abs diff
1 8.45 7.59 6.23 2.38e-07
2 9.97 7.74 6.52 2.38e-07
4 10.08 7.77 6.57 3.58e-07
8 10.18 7.84 6.66 2.38e-07
16 11.02 8.56 7.21 2.38e-07
32 11.20 8.78 8.22 3.58e-07
48 12.54 10.17 10.60 3.58e-07
64 12.76 10.42 10.97 3.58e-07
96 15.53 13.28 13.85 3.58e-07
128 19.46 17.21 16.35 3.58e-07
256 27.24 25.13 26.02 5.36e-07

With 80 slices, the vectorized reduce lowers latency by 8-23% versus main at every M measured. With 160 slices, latency is a further 15-18% lower up to 16 rows (26-35% lower than main) and 6% lower at 32 rows. At 48-256 rows, 160 slices changes latency by -5% to +5% relative to 80 slices (these sizes occur in large verify batches) and is 4-16% lower than main.

I measured the following with the same kernel change on my branch before the port onto #42245, and have not repeated them on this port.

DeepSeek-V4.1-Flash on 4x RTX PRO 6000 Blackwell (PCIe), TP4/EP2, DSpark speculative decoding:

  • Vectorized reduce: the reduce took 6.0 -> 3.5 us per call at 6 rows. End-to-end decode throughput (median of 3 runs, temperature 1) on two consecutive builds, the second adding this reduce, changed by -0.5 / -1.6 / +3.5 / +0.1% at 1 / 2 / 4 / 8 concurrent requests.
  • 160 slices: in a decode profile, the mix partial kernel took 9.0 -> 6.5 us per call.

160 slices is limited to SM120 decode/verify, the only architecture and mode where I measured it; I have no same-build prefill A/B, so prefill keeps 80 slices.

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 #40964957784
Latest PR Test (Extra): ❌ Run #40964957689
Latest PR Test (AMD ROCm 10): ❌ Run #40964957549

@ormandj
ormandj force-pushed the mhc-mix-vec-reduce branch from 6dc0988 to d57e9f7 Compare October 7, 2026 05:42
@ormandj ormandj mentioned this pull request Oct 7, 2026
17 of 56 tasks
@ormandj ormandj changed the title [DSv4.1] Speed up decode-size mHC mixing with a vectorized split-K reduce and 160 K slices on SM120 [Perf] dsv41: Speed up decode-size mHC mixing with a vectorized split-K reduce and 160 K slices on SM120 Oct 19, 2026
…, 160 K slices for SM120 decode

On SM120, hc_mix_stats_sinkhorn reduces its K-slice partials with one tl.sum
over a [slices, cols] tile instead of a serial per-slice loop in a single-warp
CTA per row; the tree depends only on the slice count, so results stay
deterministic and batch-invariant. Other GPUs keep the serial reduce.

On SM120 (188-SM RTX PRO 6000), decode and target-verify forward passes split
K=20480 into 160 slices instead of 80: one row tile otherwise leaves most SMs
idle at decode sizes. Prefill, which does not benefit, keeps 80. The slice
count depends on K, the device and the forward mode, never on M.
…g from the layer

Run the batch-invariance test as SM100 and SM120 on any host GPU, checking the
reducer and slice count, with subsets that cross the 8/16/32 row tiles. Add a CPU
test that decode, idle and target-verify forwards pass decode=True to
hc_mix_stats_sinkhorn for both hyper-connections and prefill modes do not. Say
that 160 slices are limited to the measured decode/verify mode.
A decode row can run in a DECODE batch (160 slices on SM120) or beside
prefill rows in a MIXED batch (80), so with batch-invariant mode enabled
hc_mix_stats_sinkhorn always gets decode=False, as the compensated
projections already honor that mode. Test that every forward mode then
selects the same slice count.
@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