Skip to content

[Fix] glm-5.2: Record side-stream work right before its join to keep CUDA-graph replay on one stream - #45349

Merged
nvpohanh merged 2 commits into
sgl-project:mainfrom
wenscarl:glm52-graph-stream-fork
Oct 21, 2026
Merged

nvpohanh merged 2 commits into
sgl-project:mainfrom
wenscarl:glm52-graph-stream-fork

Conversation

@wenscarl

@wenscarl wenscarl commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

On GLM-5.2 (GlmMoeDsaForCausalLM), a CUDA-graph decode replay spreads across many CUDA streams, so a profiler trace is
hard to read: each layer's indexer chain lands on a different stream row.

This is the same issue as DeepSeek-V4.1 in #39420. That PR found that at graph replay a join node continues on the stream
of its first-recorded parent. A side-stream block captured before the concurrent main-stream kernels therefore pulls the
main chain onto a side lane at the join. Its fix keeps the fork where it is and captures the side-stream work right before
the join.

Inspired by and following the approach of #39420 ("[DSV4.1] Record side-stream work right before its join").

Modifications

Two side-stream blocks on the GLM-5.2 decode path were captured before the main-stream kernels they run alongside. In
both, the fork (alt_stream.wait_stream(current_stream)) stays where it was, and only the capture order of the side block
changes.

  1. python/sglang/srt/layers/attention/dsa/dsa_indexer.py, _fused_q_prepare_and_store, stage 1: the wq_b GEMM on
    alt_stream is now captured after the main-stream wk_weights_proj, right before the join.
  2. python/sglang/srt/models/deepseek_common/attention_forward_methods/forward_mla.py (decode graphs, DSA path): the
    q_b_proj block on alt_stream is now captured after the indexer, right before
    current_stream.wait_stream(self.alt_stream). k_nope.unsqueeze(1), a view with no kernel, now runs on the main stream.

I checked the rest of the GLM-5.2 path and found no other active violation under TP4 / NVFP4 / flashmla_kv. In
particular, DeepseekV2MoE.forward_normal_dual_stream already issues the routed experts before the shared-expert side
block, and stage 2 of the fused indexer is already ordered correctly.

Test: test/registered/unit/layers/attention/test_dsa_indexer_stream_order.py pins the issue order of the stage-1 block
(fork, main GEMM, alt wq_b, join). It fails without the change and passes with it.

Results

GLM-5.2-NVFP4, aggregated serving, TP=4, CUDA graphs on, dummy weights, GB200 x4. One run per arm, rank TP1, same load for
both.

Distinct CUDA streams seen in the profiler trace:

baseline this PR
10-step decode window 123 81
30 ms slice of the window 45 3

In the baseline, 99 of the 123 streams are tiny, each carrying a few kernels of the indexer chain
(fused_q_indexer_rope_hadamard_quant, sm100_paged_mqa_logits, topk_small_batch_cluster_kernel and a TgvGemm kernel).
With this PR those streams disappear: two main lanes carry about 13.3k and 9.8k kernels. The kernel count is identical
(23,324 vs 23,319), so the same work runs.

Median TPOT (sglang.bench_serving, random 512 in / 256 out):

concurrency baseline this PR
1 10.51 ms 10.43 ms
8 11.88 ms 11.78 ms
16 13.33 ms 13.28 ms

No regression. The 0.4% to 0.8% gain is within single-run noise and is not claimed as a speedup.

Reproduce

Model files only, no weights (config and tokenizer from nvidia/GLM-5.2-NVFP4; weights are random via --load-format dummy):

mkdir -p /path/to/glm52_cfg && cd /path/to/glm52_cfg
for f in config.json generation_config.json hf_quant_config.json tokenizer.json tokenizer_config.json chat_template.jinja
model.safetensors.index.json; do
  curl -sfL -O https://huggingface.co/nvidia/GLM-5.2-NVFP4/resolve/main/$f
done

Server (run once on main and once on this branch):

python3 -m sglang.launch_server --trust-remote-code \
  --model-path /path/to/glm52_cfg --load-format dummy \
  --tensor-parallel-size 4 --quantization modelopt_fp4 --kv-cache-dtype fp8_e4m3 \
  --dsa-prefill-backend flashmla_sparse --dsa-decode-backend flashmla_kv \
  --mem-fraction-static 0.8 --max-running-requests 16 --cuda-graph-max-bs-decode 16 \
  --context-length 32768 --host 127.0.0.1 --port 30000

TPOT at concurrency 1, 8 and 16:

for C in 1 8 16; do
  python3 -m sglang.bench_serving --backend sglang --port 30000 --model /path/to/glm52_cfg \
    --dataset-name random --random-input-len 512 --random-output-len 256 --random-range-ratio 1.0 \
    --num-prompts $((C*4)) --max-concurrency $C --warmup-requests 2 --seed 1
done

Trace of a steady-state decode window (start profiling about 6 s into the run, while it is decoding):

python3 -m sglang.bench_serving --backend sglang --port 30000 --model /path/to/glm52_cfg \
  --dataset-name random --random-input-len 256 --random-output-len 1024 --random-range-ratio 1.0 \
  --num-prompts 16 --max-concurrency 16 --seed 2 &
sleep 6
curl -s -X POST localhost:30000/start_profile -H 'Content-Type: application/json' \
  -d '{"num_steps":10,"activities":["CPU","GPU"],"output_dir":"/path/to/trace","profile_prefix":"run"}'
wait

Count distinct streams per rank: read the trace JSON, take events with ph == "X" and cat == "kernel", and count the
distinct args.stream values. The full traces are 330 to 460 MB each. For Perfetto, cut a 30 ms window from one rank so
it loads.

Notes and limits

  • Dummy weights: this checks stream layout and latency only. It says nothing about accuracy. I compared TPOT, not
    generated outputs.
  • Single run per arm, one rank analyzed. The TPOT difference is within noise.
  • Stream assignment at replay is driver behavior. The result is from one driver and torch version here (torch
    2.14.1+cu130, driver 580.x).
  • The forward_mla.py edit moves q_b_proj after the indexer. A reviewer raised a theoretical allocator-reuse hazard:
    tensors the indexer allocates on alt_stream are freed at return, and the trailing q_b_proj could reuse a block while a
    main-stream kernel still reads it. I did not reproduce a failure, and the A/B shows no change in throughput. I have not
    proven it safe either, so please look at this one closely.
  • The patched trace still has 78 streams that each hold one kernel, a FlashInfer RMSNorm, one per layer. They come from a
    separate stream hop that this PR does not touch.
  • This covers the CUDA-graph decode path (is_decode_or_idle). The target-verify and draft-extend graphs do not take the
    forward_mla.py overlap branch.

Checklist

  • Unit test added
  • Accuracy test (not run; dummy weights)

The diff is still uncommitted on glm52-graph-stream-fork, and nothing is pushed. Two things to decide before you post it:

  • Allocator-race note. I disclosed the unresolved allocator-race concern about the forward_mla.py edit in "Notes and
    limits". I'd leave it in; the alternative is dropping that edit or adding a keep-alive fix.
  • Repeat runs. Repeating each arm a few times would let you replace "within noise" with a number.

Perfetto screenshots (30 ms slice, rank TP1):

before PR:
Screenshot 2026-10-20 at 12 25 35 AM
after PR:
Screenshot 2026-10-20 at 12 26 08 AM


CI States

Latest PR Test (Base): ❌ Run #39392800055
Latest PR Test (Extra): ❌ Run #39392800085
Latest PR Test (AMD ROCm 10): ❌ Run #39392800119

cc. @nvpohanh @BBuf

@wenscarl wenscarl changed the title [Fix] glm-5.2: Record side-stream work right before its join in decod… [GLM5.2] Record side-stream work right before its join to keep CUDA-graph replay on one stream Oct 20, 2026
@wenscarl
wenscarl marked this pull request as ready for review October 20, 2026 07:33
@github-actions

This comment has been minimized.

@wenscarl
wenscarl requested a review from nvpohanh October 20, 2026 07:39
…e graphs

At CUDA-graph replay a join node continues on the stream of its first-recorded
parent, so a side-stream block captured before the concurrent main-stream
kernels pulls the main chain onto a side lane. GLM-5.2 decode graphs replayed
across 123 distinct streams in a 10-step window (45 in a 30 ms slice), with each
layer's indexer chain on its own stream.

Keep both forks where they are and capture the side block right before its join:
- dsa_indexer: _fused_q_prepare_and_store stage 1, alt wq_b after main
  wk_weights_proj.
- forward_mla: decode-graph q_b_proj on alt_stream after the DSA indexer;
  k_nope.unsqueeze(1) (a view) runs on main.

With this change the same window shows 81 streams (3 in the 30 ms slice) and the
same kernel count. Median TPOT at concurrency 1/8/16 on dummy-weight GLM-5.2-NVFP4
TP4 moves from 10.51/11.88/13.33 ms to 10.43/11.78/13.28 ms (one run per arm,
within noise).

Same approach as sgl-project#39420 for DSV4.1.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@wenscarl
wenscarl force-pushed the glm52-graph-stream-fork branch from c5fd067 to b7bf407 Compare October 20, 2026 07:43
@github-actions

This comment has been minimized.

@nvpohanh nvpohanh changed the title [GLM5.2] Record side-stream work right before its join to keep CUDA-graph replay on one stream [Fix] glm-5.2: Record side-stream work right before its join to keep CUDA-graph replay on one stream Oct 20, 2026
@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/e2e/models/test_dsa_glm52_nvfp4_tp_mtp.py test/registered/e2e/models/test_dsa_glm52_nvfp4_dp_mtp.py test/registered/e2e/models_large/test_deepseek_v32_indexcache.py test/registered/rl/test_return_indexer_topk.py test/registered/e2e/models_large/test_glm52_fp8.py test/registered/e2e/models/test_dsa_glm52_tp_mtp.py test/registered/e2e/models/test_dsa_glm52_dp_mtp.py test/registered/gb300/test_glm52_nvfp4.py test/registered/dcp/test_glm53_dcp2.py test/registered/cp/test_dsa_prefill_cp.py test/registered/ep/test_deepep_large.py test/registered/e2e/models/test_dsa_glm52_hisparse.py test/registered/e2e/models/test_dsa_glm52_pd_mtp_cp_layersplit.py test/registered/radix_cache/unified_radix_tree/linker/test_unified_cache_linker_kl_glm52.py test/registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_glm52.py test/registered/cp/test_glm53_flash_cp.py test/registered/e2e/models/test_glm53_flash_h200_low_latency.py test/registered/e2e/models/test_glm53_flash_h200_high_throughput.py test/registered/e2e/models/test_glm53_flash_b200.py

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

🚀 4-gpu-b200 (5 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_dsa_glm52_nvfp4_tp_mtp.py
cd test/ && python3 registered/e2e/models/test_dsa_glm52_nvfp4_dp_mtp.py
cd test/ && python3 registered/dcp/test_glm53_dcp2.py
cd test/ && python3 registered/cp/test_glm53_flash_cp.py
cd test/ && python3 registered/e2e/models/test_glm53_flash_b200.py

🚀 8-gpu-h200 (11 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models_large/test_deepseek_v32_indexcache.py
cd test/ && python3 registered/rl/test_return_indexer_topk.py
cd test/ && python3 registered/e2e/models_large/test_glm52_fp8.py
cd test/ && python3 registered/e2e/models/test_dsa_glm52_tp_mtp.py
cd test/ && python3 registered/e2e/models/test_dsa_glm52_dp_mtp.py
cd test/ && python3 registered/cp/test_dsa_prefill_cp.py
cd test/ && python3 registered/ep/test_deepep_large.py
cd test/ && python3 registered/radix_cache/unified_radix_tree/linker/test_unified_cache_linker_kl_glm52.py
cd test/ && python3 registered/radix_cache/unified_radix_tree/test_unified_radix_cache_kl_glm52.py
cd test/ && python3 registered/e2e/models/test_glm53_flash_h200_low_latency.py
cd test/ && python3 registered/e2e/models/test_glm53_flash_h200_high_throughput.py

🚀 8-gpu-b200 (2 tests): ✅ View workflow run

cd test/ && python3 registered/e2e/models_large/test_glm52_fp8.py
cd test/ && python3 registered/e2e/models/test_dsa_glm52_hisparse.py

🚀 4-gpu-gb300 (1 test): ✅ View workflow run

cd test/ && python3 registered/gb300/test_glm52_nvfp4.py

🚀 8-gpu-b300 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_dsa_glm52_pd_mtp_cp_layersplit.py

@nvpohanh

Copy link
Copy Markdown
Collaborator

@YAMY1234 could you review this tiny change? Thanks!

@nvpohanh

Copy link
Copy Markdown
Collaborator

Merging this since this is a tiny change and glm tests have passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants