Repository navigation
Conversation
FA4's flash_attn_varlen_func is a Python autograd.Function that calls torch._guards.active_fake_mode(), which Dynamo refuses to trace. Under activation checkpointing each block is one checkpointed region, so that graph break drops the whole block to eager for every FA4 model, dense or MoE. Wrap FA4's varlen forward/backward in prime_rl_attn::flash_attn_4_varlen (with fake impls and autograd) and add the namespace to the selective checkpointing save list so attention is not recomputed in backward. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Rework of the FA4 custom op from #3676: instead of a minimal op called from a version-branch in _compute_attention, the op now mirrors the FA2/FA3 varlen signature (cu_seqlens_q/k, max_seqlen_q/k, causal, window_size_left/right), a thin adapter exposes it with FA2/FA3's call convention, and _funcs[4] dispatches to it. _compute_attention collapses to a single version-free path. The raw flash_attn_4_varlen_func re-export stays: gpt_oss and afmoe import it from attn.py. Ring/Ulysses CP replace _compute_attention and are unaffected, as in #3676. Local validation used stubbed kernels (the cu13 FA4 wheel cannot load on this box's 12.9 driver): schema registration, fake impls, autograd plumbing (exact fwd, matching dq/dk/dv), sliding-window and non-causal cases, _funcs dispatch, and torch.compile(fullgraph=True) tracing, while a raw autograd.Function calling active_fake_mode() graph-breaks.
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.
Summary
Rework of the FA4 custom op proposed in #3676 (stacked on its head commit; not measured in isolation from it). Same goal: FA4's
flash_attn_varlen_funcis a Pythonautograd.Functionthat callstorch._guards.active_fake_mode(), which Dynamo refuses to trace, so with activation checkpointing on, the graph break drops the whole block to eager.What differs from #3676:
flash_attn_4_varlen(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, causal, window_size_left, window_size_right) -> (out, lse), and likewise for the backward op.causaland both window edges are schema args, not hardcoded.causal=/window_size=(-1, -1)kwargs,-1 -> None, returnsoutonly) and_funcs[4]dispatches to it._compute_attentioncollapses to a single version-free path; the FA4 branch (and theqv-misalignment comment) are gone.flash_attn_4_varlen_funcre-export stays because gpt_oss and afmoe import it fromattn.py.Only the non-CP path changes, as in #3676: ring and Ulysses CP replace
_compute_attentionand keep their own FA4 calls. The wide schema is the shape Ulysses' local FA would need if it ever routes through the op.prime_rl_attnis already on the selective AC save list via #3676.Validation
ruff checkandruff formatpass.out (t, h, d),lse (h, t)fp32), autograd plumbing with exact forward and matchingdq/dk/dv, sliding-window and non-causal cases,_funcsdispatch through_compute_attention, andtorch.compile(fullgraph=True)traces forward+backward while a rawautograd.Functioncallingactive_fake_mode()graph-breaks withUnsupported.out/dqexact,dk/dvwithin 1 bf16 ulp) and perf/VRAM numbers. No perf claims are made here.🤖 Generated with Claude Code