Skip to content

fix: support FA4 backend for GLM4.7-flash - #33436

Merged
BBuf merged 5 commits into
sgl-project:mainfrom
zyzshishui:yuzhen/fix-blackwell-mla-pack-gqa
Aug 9, 2026
Merged

BBuf merged 5 commits into
sgl-project:mainfrom
zyzshishui:yuzhen/fix-blackwell-mla-pack-gqa

Conversation

@zyzshishui

@zyzshishui zyzshishui commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Enable GLM-4.7-Flash to serve correctly with --attention-backend fa4 on Blackwell using default non-deterministic decode and CUDA Graphs. SGLang's absorbed MLA presents 20 Q/QV heads over one latent KV head at TP1, but FA4's packed kernel requires the ratio to be compatible with its 128-row tile.

The current pack_gqa=False QV varlen decode path is incorrect for bs>1: queries after the first sequence can read another sequence's paged KV. Even if that upstream bug is fixed, unpacking would schedule Q heads separately instead of folding them into the M tile, repeating traversal of the shared latent KV and badly underutilizing 128-row decode tiles. Padding keeps the packed path; using one split also avoids unsupported automatic split-KV for QV MLA.

Modifications

  • Pad each Q/QV KV group to a tile-compatible power-of-two ratio (20 to 32 for GLM TP1), then crop output and LSE to the original heads.
  • Use one split when a QV call requests automatic split selection.
  • Materialize dense Q/K/V for the dedicated head-dimension-256 kernel.
  • Add QV coverage to test_flash_attn_varlen_output. All runtime changes stay in the SGLang FA4 wrapper.

Accuracy Tests

python -m sglang.test.few_shot_gsm8k \
  --num-questions 200 \
  --data-path gsm8k-test.jsonl \
  --port 30000
Backend Accuracy Correct Invalid Latency Output tok/s
FA4 0.830 166/200 0 11.215 s 1668.941
Triton 0.820 164/200 0 8.889 s 2075.046

Speed Tests and Profiling

on GB300

python -m sglang.launch_server \
  --model zai-org/GLM-4.7-Flash \
  --attention-backend 【fa4/triton】 \
  --tp 1
Workload Concurrency FA4 output tok/s Triton output tok/s Cookbook Triton output tok/s FA4 mean TPOT Triton mean TPOT Cookbook Triton mean TPOT
Chat 1K/1K 1 90.86 138.41 108.37 8.19 ms 6.96 ms 8.75 ms
Chat 1K/1K 16 862.27 922.22 773.83 15.80 ms 14.93 ms 18.03 ms
Chat 1K/1K 100 2612.03 2569.80 2761.82 32.05 ms 35.67 ms 32.26 ms
Reasoning 1K/8K 1 83.68 132.76 84.62 10.85 ms 7.36 ms 10.94 ms
Reasoning 1K/8K 16 676.44 866.63 671.65 19.52 ms 15.89 ms 20.36 ms
Reasoning 1K/8K 64 2140.98 2030.39 1820.33 25.25 ms 27.35 ms 30.51 ms
Summarization 8K/1K 1 71.96 120.21 72.42 12.97 ms 7.61 ms 12.63 ms
Summarization 8K/1K 16 508.69 628.28 465.10 24.78 ms 22.20 ms 27.87 ms
Summarization 8K/1K 64 1516.72 1057.45 1017.93 37.31 ms 56.20 ms 58.06 ms

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.

CI States

Latest PR Test (Base): ❌ Run #31140424842
Latest PR Test (Extra): ❌ Run #31140424713

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@zyzshishui
zyzshishui force-pushed the yuzhen/fix-blackwell-mla-pack-gqa branch from 1f5466a to ed2070c Compare August 4, 2026 22:57
@zyzshishui zyzshishui changed the title fix: disable unsupported pack-gqa for Blackwell MLA fix: support Blackwell MLA head ratios in FA4 Aug 4, 2026
Pad SGLang absorbed-MLA Q/QV heads per KV group to the smallest ratio compatible with the Blackwell 128-row cluster tile, then crop outputs back to the model head layout.

Materialize dense hd256 inputs and force QV auto split mode to one split so GLM-4.7-Flash serves correctly with default non-deterministic decode CUDA Graphs.
@zyzshishui
zyzshishui force-pushed the yuzhen/fix-blackwell-mla-pack-gqa branch from ed2070c to cf0b4f8 Compare August 4, 2026 23:17
@zyzshishui zyzshishui changed the title fix: support Blackwell MLA head ratios in FA4 fix: support FA4 backend for GLM4.7-flash Aug 5, 2026
@zyzshishui
zyzshishui marked this pull request as ready for review August 5, 2026 03:06

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cf0b4f88a4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@zyzshishui

Copy link
Copy Markdown
Contributor Author

/tag-run-ci-label

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Aug 5, 2026
@BBuf

BBuf commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Can you add some real datasets such as AIME or GMS8K's accuracy comparsion results?

return None
prefix = x.shape[:-2]
x = x.reshape(*prefix, num_kv_heads, qhead_per_kvhead, x.shape[-1])
x = F.pad(x, (0, 0, 0, qhead_per_kvhead_padded - qhead_per_kvhead))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add B200 performance numbers for this path? For GLM-4.7-Flash TP1, this pads Q/QV from 20 to 32 heads, so the MLA kernel processes 60% more query-head rows, in addition to the two padding operations on every layer. Since this is on the decode hot path, we should verify end-to-end decode latency and throughput with CUDA Graph enabled, ideally against the currently working attention backend.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have B200, ran on GB300 instead and result added above

The (20, 1, 0) case is the GLM-4.7-Flash TP1 shape: a 20:1 head ratio is
incompatible with the MLA kernel's 128-row cluster tile, so the SGLang
wrapper must pad each KV group to 32 q heads and crop the output, and must
coerce num_splits=0 (the production non-deterministic decode default) to 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you validate the exact configuration this PR claims to fix under CUDA Graph capture and replay? This test exercises the wrappers only in eager mode, while the reported failure occurs with non-deterministic decode and CUDA Graph enabled. At minimum, please provide a reproducible GLM-4.7-Flash launch command and accuracy/generation results after repeated graph replay; ideally, add a regression test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a regression test for GLM-4.7-Flash TP1 decode shape. It captures and replays flash_attn_with_kvcache with paged KV and num_splits=0, then compares output and LSE against eager execution bit-for-bit. Launch command and accuracy/generation results added in the pr desc.

[
(8, 1, 1), # DeepSeek-style MQA, tile-compatible head ratio
(8, 4, 1), # GQA
(20, 1, 0), # GLM-4.7-Flash TP1: padded head ratio, auto split selection

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also cover (10, 1, 0) and (5, 1, 0), corresponding to GLM-4.7-Flash with TP2 and TP4? The model partitions its 20 attention heads across attention TP ranks, and these cases exercise different padding sizes. It would also be useful to call the wrapper with return_softmax_lse=True and verify both output and LSE, since _unpad_mla_result has a separate LSE cropping path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added TP2 (10, 1, 0) and TP4 (5, 1, 0) coverage. The test now requests return_softmax_lse=True and validates both the cropped output and LSE.

# The vendored hd256 kernel assumes dense Q/K/V strides.
# TODO: Remove this workaround after the FA4 in current environment includes
# https://github.com/Dao-AILab/flash-attention/pull/2670 (flash-attn-4 >= 4.0.0b20).
q, k, v = [t.contiguous() for t in (q, k, v)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This workaround affects all non-QV FA4 hd256 callers and is independent of the GLM MLA path. Could you add a regression test with non-contiguous hd256 Q/K/V inputs? Otherwise, it may be cleaner to move this to a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test_flash_attn_hd256_noncontiguous_inputs

@zyzshishui

Copy link
Copy Markdown
Contributor Author

Can you add some real datasets such as AIME or GMS8K's accuracy comparsion results?

added

@zyzshishui

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

1 similar comment
@zyzshishui

Copy link
Copy Markdown
Contributor Author

/rerun-failed-ci

@BBuf
BBuf merged commit 7120f3e into sgl-project:main Aug 9, 2026
301 of 350 checks passed
@zyzshishui
zyzshishui deleted the yuzhen/fix-blackwell-mla-pack-gqa branch August 9, 2026 16:43
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Noridom1 added a commit to Noridom1/sglang that referenced this pull request Nov 21, 2026
…ided paged caches

flash_attn_varlen_func makes Q/K/V contiguous on every head-dim-256 call
(sgl-project#33436). The workaround exists for the dedicated hd256 kernel that
interface.py selects on other architectures (use_dedicated_hd256_kernel),
which builds its K/V layout from the shapes alone. On SM90 the hd256 path
is FlashAttentionForwardSm90, which reads the real strides.

For a dense paged cache .contiguous() is a no-op. With the unified-memory
pool a layer's cache is an as_strided view whose token stride is the whole
slot, so every hd256 call on SM90 copied the layer's entire paged K and V
cache and allocated two cache-sized buffers per call.

Keep the copy only where the dedicated kernel runs, and add an SM90 test
that a strided fp8 hd256 paged cache is read in place: the output equals
a dense copy bit for bit and the call allocates no copy of the cache.

Fixes sgl-project#52079

Signed-off-by: Noridom1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants