Repository navigation
fix: support FA4 backend for GLM4.7-flash - #33436
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
1f5466a to
ed2070c
Compare
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.
ed2070c to
cf0b4f8
Compare
There was a problem hiding this comment.
💡 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".
|
/tag-run-ci-label |
|
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
added test_flash_attn_hd256_noncontiguous_inputs
added |
|
/rerun-failed-ci |
1 similar comment
|
/rerun-failed-ci |
…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]>
Motivation
Enable GLM-4.7-Flash to serve correctly with
--attention-backend fa4on 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=FalseQV 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
test_flash_attn_varlen_output. All runtime changes stay in the SGLang FA4 wrapper.Accuracy Tests
Speed Tests and Profiling
on GB300
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ❌ Run #31140424842
Latest PR Test (Extra): ❌ Run #31140424713