Skip to content

[PyTorch] Backport #3540: translate TE's -1 window sentinel for FA4 - #3

Merged
yueming-yuan merged 1 commit into
miles-mainfrom
yueming/fa4-window-sentinel
Oct 31, 2026
Merged

yueming-yuan merged 1 commit into
miles-mainfrom
yueming/fa4-window-sentinel

Conversation

@yueming-yuan

Copy link
Copy Markdown
Collaborator

Backport of the FA4 window-sentinel adapter from NVIDIA#3540 (backends.py only).

Problem

check_set_window_size turns a causal mask into window_size=(-1, 0), and TE passes it to FA4 unchanged. FA4's _resolve_causal_local_window treats -1 as a literal offset unless both sides are negative, so causal (-1, 0) becomes local=True with left offset -1:

  • wrong causal attention at every head dim, with no error
  • AssertionError: SM100 backward with head_dim=256 does not support local attention in the SM100 hd256 backward, which blocks every Qwen3.5 (head_dim 256) training run on B200/GB300 with --attention-backend flash

Fix

Wrap the two FA4 entry points that 2.17 uses (flash_attn_func, flash_attn_varlen_func) so that -1 maps to None. Other negative offsets are preserved. The adapter is copied unchanged from NVIDIA#3540; its other two wrapped entry points (_flash_attn_fwd/_bwd) are only used by the CP path, which doesn't use FA4 in 2.17.

Not backported from NVIDIA#3540:

  • the deterministic SM100 D=256 guard, which is wrong for FA4 b35 (its hd256 backward is deterministic)
  • the b31/b33 seqused gate, which is a no-op for b35

Validation (B200, flash-attn-4 4.0.0b35, patched TE overlaid on the miles image)

TE DotProductAttention, thd padding_causal, NVTE_FLASH_ATTN=1, max abs error against an fp32 reference:

case before after
D=128 causal fwd 3.67, dq/dk/dv 2.4 / 3.0 / 4.7 fwd 7.7e-3, grads ~1.2e-2
D=256 causal fwd 3.78, bwd assert fwd 8.8e-3, grads ~1.2e-2
D=128 SWA (64, 0) 9.5e-3 9.5e-3 (unchanged)

Full-param Qwen3.5-4B GRPO (miles, TP2, 4×B200): before, it crashed at the first backward; after, step 0 completes with loss -2.55e-5, grad_norm 0.583, train_rollout_logprob_abs_diff 0.0066.

… FA4

FA4 treats -1 as a literal window offset, so TE's causal (-1, 0) became a
local window: wrong attention at every head dim and an assert in the SM100
head_dim=256 backward.
@yueming-yuan
yueming-yuan merged commit 6d8b505 into miles-main Oct 31, 2026
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.

1 participant