Repository navigation
[PyTorch] Backport NVIDIA/TransformerEngine#3540: pass FA4 unbounded window sides as None - #2
Draft
maocheng23 wants to merge 1 commit into
Draft
maocheng23 wants to merge 1 commit into
maocheng23 wants to merge 1 commit into
Conversation
TE spells an unbounded attention-window side as -1 and hands FA4 its causal window (-1, 0) together with causal=True. FA4's cute interface spells unbounded as None. Since flash-attn-4 4.0.0b30 (Dao-AILab/flash-attention#2490), FA4 clears a negative window only when both sides are negative, so it resolves (-1, 0) to a local window with left bound -1. That band holds no keys: the forward is wrong (NVIDIA#3528 measured an all-zero output), and the SM100 head_dim=256 backward rejects the local window. FA4 b30 through b36 and main all behave this way. Wrap the two FA4 entry points TE imports so that -1 sides reach FA4 as None. Other values, including genuine sliding windows, pass through unchanged. This is the window-sentinel part of NVIDIA#3540, which v2.17 predates. The rest of NVIDIA#3540 (FA4 D=256 THD seqused selection, the _flash_attn_fwd/_bwd entry points used under context parallelism) does not apply: this branch neither imports those entry points nor runs FA4 under context parallelism. Tests: the sentinel-normalization table from NVIDIA#3540, and a padding_causal thd check of FA4 forward and backward against a float64 reference on SM100/103 (head_dim 128 and 256). L3 CI installs flash-attn-4 4.0.0b11, which predates NVIDIA#2490, and the existing FA4 tests only compare backends with each other. radixark/miles#4465 applies the same translation as a runtime wrapper. Drop this commit once miles-main is rebased onto a release that contains NVIDIA#3540 (>= 2.21). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Blackwell with flash-attn-4 >= 4.0.0b30, TE's FlashAttention-4 path computes causal attention incorrectly.
TE spells an unbounded window side as
-1(check_set_window_size). It hands FA4 the causal window(-1, 0)together withcausal=True. FA4's cute interface spells unbounded asNone. Since Dao-AILab/flash-attention#2490 (in 4.0.0b30), FA4 clears a negative window only when both sides are negative. So_resolve_causal_local_window(True, -1, 0)returnslocal=True, left=-1, and that band holds no keys.--attention-backend flashshowed a max abs error vs SDPA of 2.86 at head_dim 128 and 3.48 at head_dim 256.Only Blackwell runs with CP 1 where TE selects FA4 are affected: TE does not run FA4 under context parallelism, and it prefers FA3 on SM90.
Fix
This backports the window-sentinel part of NVIDIA#3540 (merged upstream 2026-10-12, on
release_v2.21, not in v2.20.2). That PR is what closed NVIDIA#3528, the upstream report of this bug._fa4_with_none_window_sentinelwrapsflash_attn_func_v4andflash_attn_varlen_func_v4at import, so-1sides reach FA4 asNone. Non-negative sides, i.e. real sliding windows, pass through unchanged. FA4 itself is not patched, so NVIDIA#2490's intentionally empty offset windows still work for other FA4 callers.The code matches upstream so that the commit can be dropped cleanly on the next rebase. As in upstream, only
-1is translated, not every negative value. For TE this is the same thing:check_set_window_sizeallows only-1or>= 0per side. The rest of NVIDIA#3540 does not apply here: this branch does not import_flash_attn_fwd/_flash_attn_bwdfrom FA4 and does not run FA4 under CP. The rest of NVIDIA#3540 covers FA4 D=256 THDsequsedbackend selection and the CP entry points.Tests (
tests/pytorch/attention/test_attention.py)test_fa4_window_sentinel_normalization: the normalization table from [PyTorch][CP] Support D=256 THD metadata with FA4 b33 NVIDIA/TransformerEngine#3540, for both FA4 window argument forms.test_dpa_fa4_causal_thd_reference(SM100/103 + FA4 only, skipped elsewhere):DotProductAttentionwith thd input andpadding_causal, the flash backend forced, GQA 8/2, packed seqlens[1, 37, 128, 300], head_dim 128 and 256.Validation
pylint --rcfile=pylintrconbackends.pyscores 10.00/10, before and after.backends.py, in front of FA4 main's real_resolve_causal_local_window.(-1, 0)goes fromlocal, left=-1to plain causal.(-1, -1),(128, 0)and(64, 64)resolve the same with and without the wrapper.test_dpa_fa4_causal_thd_referencehas not run, and its tolerance has not been checked on hardware. To run it on a B200/B300 with flash-attn-4 >= 4.0.0b30:NVTE_TORCH_COMPILE=0 python -m pytest -v tests/pytorch/attention/test_attention.py -k "fa4_causal_thd_reference or fa4_window_sentinel"Related
miles/backends/megatron_utils/te_fa4_window.py. That wrapper can be dropped once a miles-wheels release ships with this TE change. Until then the two wrappers stack harmlessly: Miles maps-1toNonefirst, and this wrapper then passesNonethrough.🤖 Generated with Claude Code