Skip to content

[PyTorch] Backport NVIDIA/TransformerEngine#3540: pass FA4 unbounded window sides as None - #2

Draft
maocheng23 wants to merge 1 commit into
radixark:miles-mainfrom
maocheng23:fix/fa4-unbounded-window-sentinel
Draft

maocheng23 wants to merge 1 commit into
radixark:miles-mainfrom
maocheng23:fix/fa4-unbounded-window-sentinel

Conversation

@maocheng23

Copy link
Copy Markdown

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 with causal=True. FA4's cute interface spells unbounded as None. 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) returns local=True, left=-1, and that band holds no keys.

  • On radixark/miles:latest (TE 2.17.0+miles, FA4 4.0.0b35), Megatron training with --attention-backend flash showed a max abs error vs SDPA of 2.86 at head_dim 128 and 3.48 at head_dim 256.
  • The SM100 head_dim 256 backward rejects the local window outright.
  • FA4 b30 through b36 and FA4 main share the same resolver, so no FA4 version fixes this.

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_sentinel wraps flash_attn_func_v4 and flash_attn_varlen_func_v4 at import, so -1 sides reach FA4 as None. 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 -1 is translated, not every negative value. For TE this is the same thing: check_set_window_size allows only -1 or >= 0 per side. The rest of NVIDIA#3540 does not apply here: this branch does not import _flash_attn_fwd/_flash_attn_bwd from FA4 and does not run FA4 under CP. The rest of NVIDIA#3540 covers FA4 D=256 THD seqused backend 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): DotProductAttention with thd input and padding_causal, the flash backend forced, GQA 8/2, packed seqlens [1, 37, 128, 300], head_dim 128 and 256.
    • It checks that FA4 was the kernel that ran, then compares the output and dq/dk/dv against a float64 reference at the file's bf16 tolerance (1.5e-2).
    • The existing FA4 tests did not catch this for two reasons: L3 CI installs flash-attn-4 4.0.0b11, which predates [PyTorch] Add triton requirement NVIDIA/TransformerEngine#2490, and those tests only compare backends with each other.

Validation

  • Pre-commit passes on the changed files (black, vermin, whitespace). pylint --rcfile=pylintrc on backends.py scores 10.00/10, before and after.
  • CPU check: I ran the wrapper, extracted from this backends.py, in front of FA4 main's real _resolve_causal_local_window.
    • Causal (-1, 0) goes from local, left=-1 to plain causal.
    • (-1, -1), (128, 0) and (64, 64) resolve the same with and without the wrapper.
  • GPU tests not run. No SM100/SM103 machine was available, so test_dpa_fa4_causal_thd_reference has 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

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FA4 returns an all-zero output for causal attention on SM100 after flash-attention #2490

1 participant