Skip to content

[PyTorch] Add per-version FlashAttention env vars (NVTE_FLASH_ATTN_V2…#3204

Open
wdykas wants to merge 1 commit into
NVIDIA:mainfrom
wdykas:flash-attention-version-env-vars
Open

[PyTorch] Add per-version FlashAttention env vars (NVTE_FLASH_ATTN_V2…#3204
wdykas wants to merge 1 commit into
NVIDIA:mainfrom
wdykas:flash-attention-version-env-vars

Conversation

@wdykas

@wdykas wdykas commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

set fa version

Description

Please include a brief summary of the changes, relevant motivation and context.

Fixes # (issue)

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

Please list the changes introduced in this PR:

  • Change A
  • Change B

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

…/V3/V4)

NVTE_FLASH_ATTN enables or disables the whole FlashAttention family, but
the choice between FlashAttention 2, 3, and 4 is automatic (package
presence and compute capability) with no user override. Some workloads
need to pin the FlashAttention generation, e.g. RL training that must
produce bitwise-identical logprobs to an inference engine running a
specific FlashAttention version: different generations use different tile
sizes and online-softmax accumulation orders, so mixed versions between
training and inference break batch-invariant / train-inference parity
guarantees.

Add NVTE_FLASH_ATTN_V2, NVTE_FLASH_ATTN_V3, and NVTE_FLASH_ATTN_V4
(default 1) that disable a specific FlashAttention version even when it
is installed, following the existing NVTE_FLASH_ATTN filter pattern.
Behavior is unchanged when the variables are unset.

Signed-off-by: wdykas <wdykas@nvidia.com>
@wdykas wdykas requested a review from cyanguwa as a code owner July 11, 2026 00:58
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Jul 11, 2026
@greptile-apps

greptile-apps Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds per-version FlashAttention controls for PyTorch attention backend selection. The main changes are:

  • New NVTE_FLASH_ATTN_V2, NVTE_FLASH_ATTN_V3, and NVTE_FLASH_ATTN_V4 env vars.
  • Backend gating that combines each version flag with the global NVTE_FLASH_ATTN flag.
  • Documentation for the new environment variables.

Confidence Score: 5/5

This looks safe to merge after a small hardening cleanup.

  • The new version gates preserve the existing global FlashAttention disable behavior.
  • Backend fallback still re-checks install state before selecting a FlashAttention backend.
  • Malformed new env var values can stop backend selection instead of falling back.

transformer_engine/pytorch/attention/dot_product_attention/utils.py

Important Files Changed

Filename Overview
transformer_engine/pytorch/attention/dot_product_attention/utils.py Adds per-version FlashAttention environment checks to backend selection.
docs/envvars.rst Documents the new per-version FlashAttention environment variables.

Reviews (1): Last reviewed commit: "[PyTorch] Add per-version FlashAttention..." | Re-trigger Greptile

Comment on lines +458 to +460
use_flash_attention_2 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V2", "1"))
use_flash_attention_3 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V3", "1"))
use_flash_attention_4 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V4", "1"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Invalid Env Value Crashes Selection

When any new per-version variable is set to a non-integer value such as true, yes, or an empty string, int(os.getenv(...)) raises before backend fallback runs. A single malformed NVTE_FLASH_ATTN_V2, NVTE_FLASH_ATTN_V3, or NVTE_FLASH_ATTN_V4 setting can make get_attention_backend fail instead of selecting another available attention backend.

@cyanguwa

Copy link
Copy Markdown
Collaborator

/te-ci pytorch L1

@cyanguwa cyanguwa added the 2.18 label Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.18 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