Skip to content

Skip FA4 for deterministic backward on SM12x - #3595

Merged
sudhakarsingh27 merged 3 commits into
NVIDIA:mainfrom
janbernloehr:jbernloehr/fix-fa4-sm120-deterministic-backward
Oct 6, 2026
Merged

sudhakarsingh27 merged 3 commits into
NVIDIA:mainfrom
janbernloehr:jbernloehr/fix-fa4-sm120-deterministic-backward

Conversation

@janbernloehr

@janbernloehr janbernloehr commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Deterministic backward on SM12x can select FlashAttention 4 and fail with AssertionError: deterministic backward not supported on SM 12.0. This also affects .eval() with autograd enabled, and a cached selection from another device can bypass the restriction. Exclude FA4 when the call may require deterministic backward, allowing the existing selector to choose an enabled compatible fallback or report no available backend.

FA4 remains eligible for forward-only calls and nondeterministic backward. Training mode and dropout semantics are unchanged.

Fixes #3594.

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

  • Derive backward eligibility from grad mode and Q/K/V gradient requirements, independently of .train() / .eval().
  • Include the input device and backward eligibility in AttentionParams, so both the scalar and mixed-THD backend caches distinguish these execution contexts. Query compute capability for the input device.
  • Scope the guard to compute-capability major version 12, including SM121. This matches FA4's SM12x backward dispatch and assertion.
  • Add 11 focused cases in a standalone test module and wire it into the PyTorch L0 launcher: seven selector cases, two mixed-THD cache cases, and two GPU regressions for eval/autograd transitions and stale device selections. Keep the restriction documented beside the selector guard.

Validation

Verified on 2026-09-30 on one RTX 5090, SM120, driver 615.36, Python 3.12.3, PyTorch 2.14.0a0, CUDA 13.4, cuDNN 9.26.0, FA4 4.0.0b24, and TE 2.21.0.dev0 at 699ed6ec9e9c600c3d0acd3a488c4ca6f339e5d9. The production diff at commit 4b0ad76b across all three Python modules was applied to the affected installation and restored afterward; original and patched file hashes were checked. No native rebuild was needed.

Follow-up commit 7bb7230e simplifies the capability lookup to pass attention_params.device directly; the helper already handles None. Pre-commit, changed-module pylint and the nine supplemental CPU selector/cache cases pass for this cleanup. GPU revalidation of this latest commit on Spark (SM121) is pending.

The verified reproducer in #3594 uses a single process with separate contiguous BF16 Q/K/V tensors shaped (2, 128, 8, 128), BSHD layout, causal masking, dropout 0, seed 0, and out.float().square().mean().backward(). Controls used NVTE_FLASH_ATTN=1, NVTE_FLASH_ATTN_V2=0, NVTE_FLASH_ATTN_V3=0, NVTE_FLASH_ATTN_V4=1, NVTE_FUSED_ATTN=0, NVTE_UNFUSED_ATTN=1, and NVTE_ALLOW_NONDETERMINISTIC_ALGO=0; backend selection was checked in debug logs. The eval-backward variant changes the module to .eval() while retaining autograd and backward.

Check Observed result
Original installation; deterministic training FA4 selected; exact backward assertion
Previous training-only guard; eval backward Same assertion
Previous guard; seeded other-device cache entry Regression test fails with the same assertion
Revised code; complete new test module 11 passed in 7.98 seconds
Revised code; training and eval backward Unfused selected; forward/backward and finite-value checks pass
Revised code; unfused fallback disabled NoBackend selected; expected TE error
Revised code; nondeterministic training and eval/no-grad FA4 selected; both controls pass
Reentrant checkpoint; torch.compile(backend="eager") Forward/backward smoke checks pass
Original installation restored; eval backward FA4 selected; exact assertion returns

Training and eval output/dQ/dK/dV passed comparison with an independent float64 causal-attention reference at atol=rtol=0.02. Maximum absolute errors were 0.01211518 / 0.01729168 / 0.01740309 / 0.02124534; repeated execution with identical inputs and upstream gradients was bitwise equal for all four tensors.

The complete test_attention_backend_selection.py module ran through pytest with -v --tb=short against real installed PyTorch/TE. The selector and mixed-THD tests use capability/selector mocks; the two integration tests execute on the real SM120 GPU. The device-cache regression seeds an SM100/other-device selection on that single GPU, so this is not physical mixed-architecture validation. SM121 has selector coverage only.

Pre-commit, license, Python 3.12 syntax, diff checks, and pylint on all three changed production modules passed. A supplemental CPU source harness passed all nine selector/cache cases. Full Python lint reports the existing E1120 at unchanged transformer_engine/common/__init__.py:295.

Coverage limits: the branch's newer base was not rebuilt as a complete GPU installation; the full L0 suites were not run. The pytest run reported 29 dependency deprecation/JIT warnings; the compile smoke check also reported Dynamo warnings from the existing NVTX helpers. Inductor and physical multi-GPU execution were not tested. A fallback can be slower than FA4.

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: jbernloehr <jbernloehr@nvidia.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Sep 30, 2026
@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Changes attention backend selection logic for deterministic backward.

The PR appears safe to merge; no new actionable issue or outstanding previous finding was identified.

Summary

The PR prevents FA4 selection for deterministic backward on SM12x while retaining FA4 for forward-only calls. It adds device and backward eligibility to attention backend cache inputs, plus selector and regression tests in PyTorch L0.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Attention call] --> B[Cache key: device and backward eligibility]
  B --> C[Backend selection]
  C --> D{SM12x deterministic backward?}
  D -- Yes --> E[Exclude FA4; select compatible fallback]
  D -- No --> F[FA4 remains eligible]
Loading

Reviews (3) · Last reviewed commit: "Simplify attention device capability loo..."

Comment thread docs/envvars.rst Resolved
Signed-off-by: jbernloehr <jbernloehr@nvidia.com>
@janbernloehr janbernloehr changed the title Skip FA4 for deterministic backward on SM120 Skip FA4 for deterministic backward on SM12x Sep 30, 2026
Signed-off-by: jbernloehr <jbernloehr@nvidia.com>
@sudhakarsingh27

Copy link
Copy Markdown
Member

/te-ci pytorch L0 L1 L3

@sudhakarsingh27 sudhakarsingh27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Relevant tests passed in CI

@sudhakarsingh27
sudhakarsingh27 merged commit 6d6a500 into NVIDIA:main Oct 6, 2026
26 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.21 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] Avoid selecting FA4 for deterministic training on SM120

3 participants