Skip to content

Commit bfdf24d

Browse files
authored
[PyTorch] Add per-version FlashAttention env vars (NVTE_FLASH_ATTN_V2… (#3204)
[PyTorch] Add per-version FlashAttention env vars (NVTE_FLASH_ATTN_V2/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>
1 parent 70957ad commit bfdf24d

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

docs/envvars.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ backend-selection overview.
141141
:Default: ``1``
142142
:Description: Enable or disable FlashAttention backend for DotProductAttention. When set to ``0``, FlashAttention will not be used.
143143

144+
.. envvar:: NVTE_FLASH_ATTN_V2
145+
146+
:Type: ``int`` (0 or 1)
147+
:Default: ``1``
148+
:Description: Enable or disable FlashAttention 2 (the ``flash-attn`` package) for DotProductAttention, without affecting FlashAttention 3 or 4. When set to ``0``, FlashAttention 2 will not be used even if it is installed. Useful for pinning the FlashAttention version, e.g. so training-side attention runs the same kernel generation as an inference engine.
149+
150+
.. envvar:: NVTE_FLASH_ATTN_V3
151+
152+
:Type: ``int`` (0 or 1)
153+
:Default: ``1``
154+
:Description: Enable or disable FlashAttention 3 (the ``flash-attn-3`` package) for DotProductAttention, without affecting FlashAttention 2 or 4. When set to ``0``, FlashAttention 3 will not be used even if it is installed.
155+
156+
.. envvar:: NVTE_FLASH_ATTN_V4
157+
158+
:Type: ``int`` (0 or 1)
159+
:Default: ``1``
160+
:Description: Enable or disable FlashAttention 4 (the ``flash-attn-4`` package) for DotProductAttention, without affecting FlashAttention 2 or 3. When set to ``0``, FlashAttention 4 will not be used even if it is installed.
161+
144162
.. envvar:: NVTE_FUSED_ATTN
145163

146164
:Type: ``int`` (0 or 1)

transformer_engine/pytorch/attention/dot_product_attention/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,18 @@ def get_attention_backend(
455455

456456
# Filter: Environment variables
457457
use_flash_attention = int(os.getenv("NVTE_FLASH_ATTN", "1"))
458-
use_flash_attention_2 = use_flash_attention
459-
use_flash_attention_3 = use_flash_attention
460-
use_flash_attention_4 = use_flash_attention
458+
use_flash_attention_2 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V2", "1"))
459+
use_flash_attention_3 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V3", "1"))
460+
use_flash_attention_4 = use_flash_attention and int(os.getenv("NVTE_FLASH_ATTN_V4", "1"))
461461
flash_attention_backend = None
462462
use_fused_attention = int(os.getenv("NVTE_FUSED_ATTN", "1"))
463463
use_unfused_attention = int(os.getenv("NVTE_UNFUSED_ATTN", "1"))
464464
if not use_flash_attention_2 and FlashAttentionUtils.is_installed:
465-
logger.debug("Disabling FlashAttention 2 due to NVTE_FLASH_ATTN=0")
465+
logger.debug("Disabling FlashAttention 2 due to NVTE_FLASH_ATTN=0 or NVTE_FLASH_ATTN_V2=0")
466466
if not use_flash_attention_3 and FlashAttentionUtils.v3_is_installed:
467-
logger.debug("Disabling FlashAttention 3 due to NVTE_FLASH_ATTN=0")
467+
logger.debug("Disabling FlashAttention 3 due to NVTE_FLASH_ATTN=0 or NVTE_FLASH_ATTN_V3=0")
468468
if not use_flash_attention_4 and FlashAttentionUtils.v4_is_installed:
469-
logger.debug("Disabling FlashAttention 4 due to NVTE_FLASH_ATTN=0")
469+
logger.debug("Disabling FlashAttention 4 due to NVTE_FLASH_ATTN=0 or NVTE_FLASH_ATTN_V4=0")
470470
if not use_fused_attention:
471471
logger.debug("Disabling FusedAttention due to NVTE_FUSED_ATTN=0")
472472
if not use_unfused_attention:

0 commit comments

Comments
 (0)