Skip to content

[trtllm_mha] perf: Stop allocating per-layer scratch inside the decode CUDA graph - #33063

Merged
Fridge003 merged 40 commits into
sgl-project:mainfrom
mattteochen:qwen35/mha-prologue-fill-removal
Aug 5, 2026
Merged

Fridge003 merged 40 commits into
sgl-project:mainfrom
mattteochen:qwen35/mha-prologue-fill-removal

Conversation

@mattteochen

@mattteochen mattteochen commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Two allocations in the TRTLLM-MHA decode path emit a fill kernel per attention
layer per forward, and CUDA-graph capture bakes them into every replay:

  1. FlashInfer allocates and zeroes a fresh multi-CTA KV counter buffer on every
    trtllm_batch_decode_with_kv_cache call when the caller supplies none — even
    though its own docstring says the kernel self-resets the counters at the end of
    each launch and that callers should own the buffer.
  2. fused_fp8_qkv_kv_cache builds float32 [1] scale tensors with torch.ones()
    when layer.k_scale / v_scale are None, which is every layer of any
    checkpoint without KV scales.

Three launches per full-attention layer, moving 4, 4 and ~600 bytes. They also sit
between two PDL-linked kernels, so _fused_qk_rmsnorm_rope_gate's
gdc_launch_dependents() was signalling a FillFunctor instead of
fused_fp8_qkv_kv_cache's PDLWaitPrimary.

Modifications

trtllm_mha_backend.py only:

  • Lease one counter buffer per backend instance via the existing
    make_persistent_multi_ctas_kv_counter_buffer (already used by
    trtllm_mla_backend and dsa_backend), sized for the widest batch the backend
    can see, and pass it to the decode call and all three target-verify calls.
    FlashInfer rejects an undersized buffer, never an oversized one.
  • Hold one float32 [1] ones tensor on the backend for layers with no checkpoint
    KV scale.

Both are allocated at backend construction, outside graph capture.

Correctness

Semantically an identity: the same constant 1.0 scale reaches the kernel, and the
counter buffer contract is FlashInfer's documented one.

GSM8K 5-shot, full 1319 questions, sglang.test.few_shot_gsm8k --parallel 128,
temperature 0, --max-new-tokens 4096:

arm GSM8K
main 0.822
this PR 0.813

Run-to-run spread on this harness is ~2pp, larger than the gap between arms.

Performance

Qwen3.5-397B-A17B-FP8, 4xB200, TP4, trtllm_mha + flashinfer_trtllm MoE, fp8
weights + fp8 KV cache, bs=1, input-len 8192.

-90us per decode step, 1.64% of a 5.490ms step. 45 launches removed (3 per
full-attention layer); the full-attention layer span drops 6.02us, x15 such layers.

main this PR
kernels per decode step 1656 1611
full-attn layer span 91.10us 85.09us
GDN layer span 77.85us 77.63us

main:
image

PR:
Screenshot 2026-07-31 at 15 28 10

The prologue in execution order, medians over the 15 full-attention layers:

main                                    this PR
_fused_qk_rmsnorm_rope_gate  2.82us     _fused_qk_rmsnorm_rope_gate  2.66us
    gap-after                0.35us         gap-after               -0.03us  <- PDL restored
FillFunctor<float>           1.12us     (removed)
FillFunctor<float>           1.05us     (removed)
fused_fp8_qkv_kv_cache       3.78us     fused_fp8_qkv_kv_cache       3.62us
    gap-after                0.32us         gap-after               -0.70us  <- PDL restored
FillFunctor<unsigned char>   1.18us     (removed)
fmhaSm100fKernel            10.98us     fmhaSm100fKernel            10.98us
-------------------------------------   -------------------------------------
window                      22.24us     window                      16.48us

CI States

Latest PR Test (Base): ❌ Run #30963620445
Latest PR Test (Extra): ❌ Run #30963620279

github-actions Bot and others added 30 commits May 1, 2026 13:24
@b8zhong b8zhong self-assigned this Aug 3, 2026
@b8zhong

b8zhong commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_qwen35_fp4_flashinfer.py test/registered/models_e2e/test_qwen35_fp4_mtp.py test/registered/lora/test_lora_qwen3_5_35b_a3b_logprob_diff.py test/registered/moe/test_flashinfer_a2a_cutedsl_v2.py

@b8zhong

b8zhong commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/8-gpu-models/test_qwen35.py

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

⚠️ Rebase Required Before Re-run

A major update has landed on main. Your PR is diverged relative to required base commit cdff33d.

Re-run was not dispatched. What to do:

  • Rebase your branch onto the latest main and push again
  • Follow issue #21065 for context
  • CI-fix PRs may request the bypass-maintenance label to skip this check

1 similar comment
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

⚠️ Rebase Required Before Re-run

A major update has landed on main. Your PR is diverged relative to required base commit cdff33d.

Re-run was not dispatched. What to do:

  • Rebase your branch onto the latest main and push again
  • Follow issue #21065 for context
  • CI-fix PRs may request the bypass-maintenance label to skip this check

@b8zhong

b8zhong commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/models_e2e/test_qwen35_fp4_flashinfer.py test/registered/models_e2e/test_qwen35_fp4_mtp.py

@b8zhong

b8zhong commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/lora/test_lora_qwen3_5_35b_a3b_logprob_diff.py test/registered/moe/test_flashinfer_a2a_cutedsl_v2.py

@b8zhong

b8zhong commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/8-gpu-models/test_qwen35.py

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_qwen35_fp4_flashinfer.py test/registered/models_e2e/test_qwen35_fp4_mtp.py:

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

cd test/ && python3 registered/models_e2e/test_qwen35_fp4_flashinfer.py
cd test/ && python3 registered/models_e2e/test_qwen35_fp4_mtp.py

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/8-gpu-models/test_qwen35.py:

🚀 8-gpu-h200 (1 test): ❌ View workflow run

cd test/ && python3 registered/8-gpu-models/test_qwen35.py

🚀 8-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/8-gpu-models/test_qwen35.py

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/lora/test_lora_qwen3_5_35b_a3b_logprob_diff.py test/registered/moe/test_flashinfer_a2a_cutedsl_v2.py:

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

cd test/ && python3 registered/lora/test_lora_qwen3_5_35b_a3b_logprob_diff.py
cd test/ && python3 registered/moe/test_flashinfer_a2a_cutedsl_v2.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py:

🚀 1-gpu-5090 (1 test): ❌ View workflow run

cd test/ && python3 registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py

@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py:

🚀 1-gpu-5090 (1 test): ❌ View workflow run

cd test/ && python3 registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py

🚀 4-gpu-b200 (1 test): ❌ View workflow run

cd test/ && python3 registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py test/registered/models_e2e/test_gpt_oss_4gpu_mxfp4.py

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py test/registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py test/registered/models_e2e/test_gpt_oss_4gpu_mxfp4.py:

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

cd test/ && python3 registered/quant/test_llama8b_nvfp4_kv_cache_sm120.py

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

cd test/ && python3 registered/cp/test_gpt_oss_4gpu_mxfp4_cp.py
cd test/ && python3 registered/models_e2e/test_gpt_oss_4gpu_mxfp4.py

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

cd test/ && python3 registered/models_e2e/test_gpt_oss_4gpu_mxfp4.py

@Fridge003
Fridge003 merged commit 6c05aaa into sgl-project:main Aug 5, 2026
102 of 118 checks passed
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Jianhong-Zhang pushed a commit to Jianhong-Zhang/sglang that referenced this pull request Aug 18, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants