Skip to content

[PyTorch] Keep FlashAttention 2 off for head_dim > 192 dropout backward on sm12x - #3715

Open
0z5a wants to merge 1 commit into
NVIDIA:mainfrom
0z5a:fa2-sm12x-dropout-hdim256
Open

0z5a wants to merge 1 commit into
NVIDIA:mainfrom
0z5a:fa2-sm12x-dropout-hdim256

Conversation

@0z5a

@0z5a 0z5a commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #3711.

flash-attn's head_dim 256 backward (run_mha_bwd_hdim256) picks its kernel by opt-in shared memory per block. Below 144 KB it launches no kernel at all when dropout is enabled, and returns without an error, so dq/dk/dv are never written. sm86, sm89, sm120 and sm121 all have 99 KB. Padded head dims 224 and 256 both use this kernel. The flash-attn side is Dao-AILab/flash-attention#3053; the check that used to reject this case was removed in Dao-AILab/flash-attention@bbb21d6.

TE's FA2 head_dim > 192 allowlist includes (12, 0). Training with dropout at a padded head dim in (192, 256] on sm120 therefore selects FA2 and gets unwritten gradients. With flash-attn 2.8.3, a backward with dq/dk/dv pre-filled with NaN leaves them 100% NaN:

  • on an RTX 5090 (sm_120, 101376 B opt-in shared memory per block), for head_dim 224 and 256 with dropout_p=0.1;
  • not without dropout, and not at head_dim ≤ 192;
  • not on B200 (232448 B), where every case computes.

This PR disables FA2 in exactly that case:

  • padded head dim > 192;
  • attention_dropout > 0;
  • training mode;
  • backward required;
  • one of the 99 KB GPUs, (8, 6), (8, 9), (12, 0), (12, 1).

Inference, dropout-free training and forward-only calls keep FA2. sm86/sm89/sm121 are already excluded by the allowlist for head_dim > 192. Listing them keeps the gate correct if that list is relaxed later.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • get_attention_backend: a separate check right after the existing FA2 head-dim check. It turns FA2 off for the case above and logs the reason at debug level.
  • test_fa2_large_head_dim_dropout_backend_selection in tests/pytorch/attention/test_attention_backend_selection.py, which mocks the compute capability:
    • sm120 with dropout at padded head dim 256, 224, and MLA 192/256: FA2 is not selected;
    • controls where FA2 stays selected: sm120 without dropout, in eval, forward only, and at head dim 192; sm100 with dropout.

Testing

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

🤖 Generated with Claude Code

…rd on sm12x

flash-attn's head_dim 256 backward picks its kernel by opt-in shared
memory per block. Below 144 KB (sm86, sm89, sm120 and sm121 have 99 KB)
it launches no kernel when dropout is enabled and returns without an
error, so dq/dk/dv are never written. The check that used to reject this
in flash-attn was removed in Dao-AILab/flash-attention@bbb21d6.

TE's FA2 head_dim > 192 allowlist includes (12, 0), so training with
dropout at a padded head dim in (192, 256] on sm120 selects FA2 and gets
unwritten gradients. Disable FA2 in exactly that case: padded head dim
> 192, attention dropout > 0, training, backward required, on a 99 KB
GPU. Inference and dropout-free training keep FA2.

Fixes NVIDIA#3711.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Oct 20, 2026
@0z5a
0z5a marked this pull request as ready for review October 21, 2026 13:42
@0z5a
0z5a requested a review from cyanguwa as a code owner October 21, 2026 13:42
@greptile-apps

greptile-apps Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

This PR prevents FA2 from running an unsafe dropout backward on sm120 when the padded head dimension exceeds 192.

  • Large-dimension dropout backward calls skip FlashAttention 2 on affected GPUs.

Reviews (1) · Last reviewed commit: "[PyTorch] Keep FlashAttention 2 off for ..." · Reviewed by Greptile

This branch has not been deployed

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

Labels

attention community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PyTorch] FA2 is selected for head_dim > 192 with dropout on sm120, where flash-attn's backward launches no kernel

2 participants