Skip to content

[AMD] [GLM-5.3-Flash Day 0] Handle the zero-width RoPE tail in the ROCm MHA k concat - #37530

Closed
Jacob0226 wants to merge 2 commits into
sgl-project:xinyuan/glm-5.3-flash-supportfrom
Jacob0226:jacob/glm53-rocm-mla
Closed

Jacob0226 wants to merge 2 commits into
sgl-project:xinyuan/glm-5.3-flash-supportfrom
Jacob0226:jacob/glm53-rocm-mla

Conversation

@Jacob0226

@Jacob0226 Jacob0226 commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

GLM-5.3-Flash's 11 full-attention layers are norope (qk_rope_head_dim == 0), so k_pe reaches _concat_and_cast_mha_k_rocm as None or zero-width while the helper still allocates a qk_head_dim-wide k and appends it. #31531 split this helper out of the shared CUDA path on 2026-08-08, when no norope model existed and neither side carried a guard; 0b9c384 on this branch then added one to the CUDA twin _concat_and_cast_mha_k only. This restores parity.

k_pe current_attention_backend today with this PR
None aiter concat_and_cast_mha_k_triton handed a None rope tensor early return
None other k[..., qk_nope_head_dim:] = k_pe raises early return
zero-width aiter Triton concat driven over a zero-width tail early return
zero-width other correct, but one new_empty + one k_nope-sized copy per call early return

Scope

forward_mha_rocm.py only. CUDA and every other backend are untouched. ROCm models with qk_rope_head_dim > 0 — DeepSeek V3/R1, the roped GLM-5.x MLA layers — never reach the guard and keep current behaviour. No new environment variable, no architecture gate, no dispatch change. k_pe widens to torch.Tensor | None to match the CUDA signature.

On MI355X this is the default short-prefill path, not an edge case:

Dispatch step Resolves to
handle_attention_dsa reads backend.use_mha
HybridLinearAttnBackend.use_mha property, forwards to the DSA backend — the hybrid wrapper does not block this
DeepseekSparseAttnBackend.set_dsa_prefill_impl True on _IS_GFX95, listed beside SM90/SM100, when max_kv_len <= SGLANG_DSA_PREFILL_DENSE_ATTN_KV_LEN_THRESHOLD (2048)
resolve_rocm_forward_method MHA_ONE_SHOT → MHA_ONE_SHOT_ROCM
forward_normal_one_shot_rocm_prepare _concat_and_cast_mha_k_rocm(k_nope, k_pe)
no dtype cast CUDA casts because its concat branch can allocate k in the KV-pool dtype for fa3 on an FP8 KV cache; the ROCm branch is k_nope.new_empty(*k_shape) with no override, so k_nope.contiguous() is dtype-identical to what it would have built

Test plan

rocm/sgl-dev:v0.5.18-rocm720-mi35x-20260901, the image family pr-test-amd-rocm720.yml resolves to, on xinyuan/glm-5.3-flash-support @ 515e865189 with no other PRs applied. Both builds run the same test file; the guard is the only variable. cd test && python3 registered/unit/models/test_nope_mha_k_cast.py.

Build Result
Baseline 3 failed, 4 passed — both None cases and zero-width on aiter fail
This PR 7 passed — 3 existing CUDA cases, 4 new ROCm

Zero-width on the non-aiter backend passes either way, per the Summary table's last row; it is kept so both backends are pinned to one behaviour rather than one of them resting on that accident.

Check Detail
Registration register_cpu_ci(suite="base-a-test-cpu"), unchanged — the four cases are mocked and GPU-free, and this change removes a kernel call rather than adding one, so there is no gfx950 work for an AMD-suite test to cover
pre-commit all hooks pass on both files, including validate registered test CI registries
Accuracy not measured — GLM-5.3-Flash cannot yet be served on ROCm from this branch; GSM8K lands with the DSA / k-pool enablement to follow

CI States

Latest PR Test (Base): ❌ Run #33619805848
Latest PR Test (Extra): ❌ Run #33627914921
Latest PR Test (AMD ROCm 7.2): ❌ Run #33619805838

GLM-5.3-Flash's norope MLA layers set qk_rope_head_dim=0 and pass no RoPE
tail, but _concat_and_cast_mha_k_rocm always allocates a qk_head_dim-wide
buffer and appends k_pe. On the aiter backend the Triton concat kernel is
handed a None/zero-width k_pe; on the fallback branch the slice assignment
is a no-op that still costs an allocation and a copy.

Mirror the CUDA path in forward_mha.py, which already returns k_nope
directly when qk_rope_head_dim == 0. No cast is needed on ROCm because
the concat branch keeps k_nope's dtype.

Co-authored-by: Cursor <[email protected]>
@github-actions github-actions Bot added the amd label Sep 2, 2026
@Jacob0226 Jacob0226 changed the title [AMD] Handle the zero-width RoPE tail in the ROCm MHA k concat [AMD] [GLM-5.3-Flash Day 0] Handle the zero-width RoPE tail in the ROCm MHA k concat Sep 2, 2026
@Jacob0226
Jacob0226 marked this pull request as ready for review September 2, 2026 10:29
@Jacob0226

Copy link
Copy Markdown
Contributor Author

Recovered as #39338, off main. This one cannot be reopened — its base branch xinyuan/glm-5.3-flash-support no longer exists. The replacement #38541 is closed; please continue review and CI on #39338.

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants