Repository navigation
[Triton/Gluon] Fix NaN dq in Triton MHA backward when every logit of a row is very n… - #6428
Conversation
…egative In the backward pass, the dq loop recomputes p = exp(qk * scale - lse). In the unmasked part of the loop, keys past end_n are loaded as 0 but p was not masked for them, so p = exp(-lse) there. When every logit of a query row is very negative (lse < -88), exp(-lse) overflows to inf and inf * k(=0) writes NaN into dq. This happens on real activations (Qwen2.5-1.5B, layer 15, head 7) and turns every gradient below that layer into NaN. Mask p with the key-range mask in the unmasked branch, in the one-kernel backward and in both dq loops of the dao_ai backward. Add a regression test with logits around -100 and sequence lengths that are not block multiples.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The modified split-mode backward branch lacks regression coverage.
1 open finding
What changed in this PR
Fixes NaN dq gradients in Triton MHA backward for ragged sequences with extremely negative logits.
Changes:
- Masks out-of-range key probabilities in three backward loops.
- Adds regression coverage for default and DAO AI implementations.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/attention/mha_onekernel_bwd.py |
Masks invalid keys in one-kernel backward. |
aiter/ops/triton/_triton_kernels/flash_attn_triton_amd/bwd.py |
Masks invalid keys in fused and split backward paths. |
op_tests/triton_tests/attention/test_mha.py |
Adds a ragged, negative-logit regression test. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The dao_ai varlen backward defaults to the fused mode, so the dq loop of the split mode (_bwd_dq_inner_split) was not exercised. Add a case that runs it with the same input.
|
@aiter-bot review |
|
aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time. |
ROCm/aiter PR #6428 — [Triton/Gluon] Fix NaN dq in Triton MHA backward when every logit of a row is very negativeMasks out-of-range keys in the recomputed attention probabilities of the three non-causal dq loops of the Triton MHA backward, so that exp(-m) overflowing to inf on zero-loaded padding keys can no longer put inf*0 = NaN into dq when a row's logsumexp drops below ~-88. Review (advisory): 📝 [verified] op_tests/triton_tests/attention/test_mha.py ships no benchmark entry point, so the deterministic perf stage can never time MHA backward and this PR's no-regression claim rests solely on the author's hand run. Reviewer should ask for a @benchmark/run_perftest entry (the harness test_mla_v4_triton.py:164 already uses) so future MHA backward changes get base-vs-head timing in CI. |
|
Where that review came from — I ran the bot on your PR to show what it does. It is not an automatic hook; nothing reviews your PR unless somebody asks. Any reviewer on the aiter allowlist can ask for one, on any PR including their own. The trigger is a comment containing the words below — shown quoted here, because quoted lines deliberately do not trigger it:
Feedback on the review itself is welcome; it is how the rules get better. |

Fix NaNs in Triton MHA backward
The Triton MHA backward can produce NaNs in
dqwhen attention logits are extremely negative (logsumexp < -88). Reproduced on Qwen2.5-1.5B (layer 15, head 7, logits around -100) while running TRL's Async GRPO on MI300X. These NaNs propagate through subsequent gradients and break training.Root cause: The
dqloop recomputesp = exp(qk * scale - lse)without masking out-of-range keys in the unmasked branch. Since these keys are loaded as zero,exp(-lse)can overflow toinf, andinf * 0produces NaNs. This occurs in the last row block when the sequence length is not block-aligned.Fix: Apply the key-range mask to
pinmha_onekernel_bwd.pyand bothdqloops inflash_attn_triton_amd/bwd.py.Tests (MI300X):
test_mha_backward_varlen_very_negative_logitsfor both implementations. Fails on main, passes with the fix.test_mha.py -k backward: 238 passed, no new failures.test_mha_dao_ai.py: 37 passed, unchanged from main.dqgoes from 384 NaNs to 0 and matches an FP32 reference.Performance: No measurable regression. Forward + backward on 16 sequences of ~2k tokens (BF16, causal, 32 Q / 8 KV heads, head dim 128):
The same fix is proposed for the vendored
aiter-flash-attncopy inhuggingface/kernels-community.