Skip to content

[PyTorch] Restore FlashAttention 2 head dim support on sm103 - #3360

Open
kalectory wants to merge 1 commit into
NVIDIA:mainfrom
kalectory:fix-sm103-fa2-head-dim
Open

[PyTorch] Restore FlashAttention 2 head dim support on sm103#3360
kalectory wants to merge 1 commit into
NVIDIA:mainfrom
kalectory:fix-sm103-fa2-head-dim

Conversation

@kalectory

@kalectory kalectory commented Aug 12, 2026

Copy link
Copy Markdown

Description

Restore the FlashAttention 2 head-dimension behavior from #2836. #2629 preserved the new padded Q/V head-dimension handling but accidentally reintroduced the exact compute-capability allowlist that #2836 removed.

On B300 (sm103), that allowlist rejects a padded head dimension of 256 and falls back to quadratic unfused attention.

image

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

Testing

Status quo

  • Layout: one 8xB300 node with four B300s assigned to the Megatron trainer (TP=4) and four to rollout inference. Qwen3.6-27B used BF16 THD attention, head dim 256, Transformer Engine 2.11.0, and FlashAttention 2.8.3.
  • Our production workload requested FlashAttention, but TE's exact compute-capability allowlist omitted B300 (sm103) and silently selected UnfusedDotProductAttention instead.
  • XID 1043331 replayed an unsliced 65,332-token datum with a 65,536-token microbatch budget. The quadratic unfused softmax tried to allocate 202.75 GiB and raised torch.OutOfMemoryError during forward.

After the fix

  • Used the same image, package versions, TP4 trainer layout, model, BF16 THD configuration, head dim, 65,536-token budget, and exact 65,332-token datum. Only this selector gate was patched.
  • The full 64k training update completed without OOM: forward_backward took 413.2s, the Adam optimizer step took 9.6s, and grad norm was finite at 1776.48. This validates FlashAttention 2 and 64k context on the four-B300 trainer configuration.
  • Python compilation and diff checks pass.

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

Signed-off-by: Neil Kale <263453039+kalectory@users.noreply.github.com>
@kalectory
kalectory requested a review from cyanguwa as a code owner August 12, 2026 22:20
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Aug 12, 2026
@greptile-apps

greptile-apps Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR restores FlashAttention 2 eligibility for padded Q/V head dimensions up to 256 on sm103 by removing a stale architecture allowlist.

  • Retains validation that the padded head dimension is divisible by 8 and no greater than 256.
  • Updates the backend-selection diagnostic to reflect the revised eligibility condition.

Confidence Score: 5/5

The PR appears safe to merge with no concrete blocking or independently actionable non-blocking issues identified.

The change narrowly removes an obsolete architecture filter while retaining FlashAttention 2’s padded head-dimension bounds, and the reviewed selection and dispatch paths reveal no established regression.

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/dot_product_attention/utils.py Removes the stale architecture-specific FA2 head-dimension restriction while preserving the general padded-dimension limits and aligning the diagnostic message.

Reviews (1): Last reviewed commit: "Restore FlashAttention 2 head dim suppor..." | Re-trigger Greptile

@cyanguwa

Copy link
Copy Markdown
Collaborator

/te-ci L0

erictang000 added a commit to NovaSky-AI/SkyRL that referenced this pull request Aug 17, 2026
applying NVIDIA/TransformerEngine#3360 to TE
2.16

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes attention backend selection at Megatron init via `exec` on TE
internals; mitigated by SM guards, literal 2.16.0 source matching, and
idempotent no-ops on unaffected GPUs, but wrong TE versions or
arch-specific FA2 issues could still affect training memory or numerics.
> 
> **Overview**
> Backports **NVIDIA/TransformerEngine#3360** for the pinned
**transformer-engine 2.16.0** so Megatron training can select
**FlashAttention 2** for **head_dim 256** (e.g. Gemma 2/3) on GPUs
outside TE’s SM allowlist (notably **sm103** B300/GB300, also
sm86/sm89).
> 
> A runtime patch **recompiles and rebinds** TE’s
`get_attention_backend` to drop the erroneous `head_dim > 192` +
compute-capability gate, keeping only FA2’s real limits (`<= 256`, `% 8
== 0`). It **no-ops** on sm80/90/100/120, when TE isn’t importable, or
when TE source no longer matches 2.16.0, and clears TE’s memoized
`_attention_backends` after apply.
> 
> **Megatron only:** `patch_fa2_head_dim_allowlist()` runs in
`make_megatron_module()` before `provide_distributed_model()` (policy
and ref workers). A **verify script** and README document repro and
numerics vs unfused attention.
> 
> Remove this patch when the TE pin includes upstream #3360.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
1d9d8c9. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants