Skip to content

[attention] Varlen attention defaults to SDPA's FlashAttention only - #6295

Draft
felipemello1 wants to merge 2 commits into
mainfrom
felipemello1/varlen-flash-first
Draft

felipemello1 wants to merge 2 commits into
mainfrom
felipemello1/varlen-flash-first

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

Needs retesting on Hopper: on H100, main ran cuDNN varlen attention for head dims up to 128 (cuDNN >= 9.18). This PR runs FA3 there, which VarlenInnerAttention already requires on SM90. Speed is not measured.

TLDR: VarlenInnerAttention now defaults to SDPA's FlashAttention backend only, instead of cuDNN first. The evidence is narrow. In a no-grad forward of one 78k-token Qwen3.8-27B sequence (GB300, cuDNN 9.27), cuDNN attention at head dim 256 gave one token a wrong logprob in two model implementations: -6.87 in the TorchTitan trainer model and -9.13 in transformers, against an fp32 reference of -0.016. FlashAttention (FA4) gives -0.0146. Training at head dim 256 already runs FA4 on main. So this PR fixes no-grad forwards there, and it also moves training at head dims up to 128, and at (192, 128) on SM100, off cuDNN, where no failure was seen. Open question for the author: which of three scopes to land (below). The code stays Flash-only until then.

On main, varlen attention runs cuDNN wherever torch allows it, so at head dims 256/256 no-grad forwards run cuDNN (one token scored -6.87, fp32 reference -0.016) while training runs FA4, and this PR runs FA4 in every head-dim and grad-mode cell.

Kernel that runs, VarlenInnerAttention, GB300, cuDNN 9.27, bf16 (torch profiler)
head dims (q/k, v)   main: no-grad forward   main: training fwd+bwd   this PR: both
256 / 256            cuDNN                   FA4                      FA4
128 / 128            cuDNN                   cuDNN                    FA4
192 / 128            cuDNN                   cuDNN                    FA4

Problem

  1. VarlenInnerAttention.Config.sdpa_backends sets the SDPA backends for torch's varlen_attn. pytorch/torchtitan#5152 added the field with cuDNN first, for speed (review: "I think we preferred cudnn because it's more performant").
  2. torch lets cuDNN train above head dim 128 only at (192, 128) on SM100. Without grad, it also takes three forward-only pairs from cuDNN 9.24: (192, 192), (256, 128) and (256, 256) (torch/nn/attention/varlen.py, _cudnn_supports_head_dims). Only (256, 256) was measured. Hence the table above.
  3. The failing case is that no-grad forward at head dim 256: one token of one 78k-token sequence scores 6.9 nats too low in the TorchTitan trainer model and 9.1 nats too low in transformers.
  4. No-grad forwards run in validation (Validator.validate) and in scoring scripts. The RL trainer's loss runs with grad, and at head dim 256 it already uses FA4, like the RL generator.

Solution

  1. The default becomes ["FLASH_ATTENTION"]. On Blackwell, VarlenInnerAttention routes Flash to FA4.
  2. Why not Flash first with cuDNN second: under torch.compile, varlen_attn ignores the listed order and runs cuDNN whenever it is listed and accepts the call. Dynamo's sdpa_kernel sets torch's priority order but not the flag that varlen's _get_sdp_priority_order reads. A TODO at the sdpa_kernel call says so.
  3. In eager, a cuDNN listed after Flash never runs either: torch picks the first eligible backend, and Flash has no eligibility check. So dropping cuDNN loses no fallback.
  4. A TODO says when to list cuDNN again: once its no-grad forward at head dim 256 stops giving that wrong logprob, and only where a timing shows cuDNN faster. No issue is filed yet.
  5. Unchanged: an explicit sdpa_backends still wins, and deterministic mode still drops cuDNN from it. The muse_spark recipe keeps its Flash pin (only its comment changes).

Open question: which scope to land

  • (a) Flash only for every model, as this PR does now. Before landing, it needs a training timing at head dim 128 on GB300 and H100, cuDNN vs Flash: those runs move off cuDNN with no failure seen there.
  • (b) Avoid cuDNN in no-grad forwards at the forward-only head dims on SM100: (192, 192), (256, 128) and (256, 256). Every no-grad forward then runs the kernel training runs at its head dims. This is the smallest change that removes the failing case. Only (256, 256) was measured; the other two pairs change unmeasured. Training keeps main's kernels.
  • (c) Keep main's default, and pin Flash in the RL recipes, as the muse_spark recipe already does. pytorch/torchtitan#5882 adds a second recipe-level Flash pin.
  • Recommendation: (b). It removes the failing case, makes validation run the kernels training runs, needs no speed number, and also covers validation outside RL. (a) is simpler code but changes training for every model with head dims up to 128. (c) leaves validation at head dim 256 on the failing kernel.

Results

Numerics: not bitwise vs main wherever cuDNN ran before (another kernel).

Qwen3.8-27B, one 78,147-token sequence (prompt + 1,024 sampled tokens), 1 GB300, cuDNN 9.27.0.42,
torch 2.16.0.dev20261107, no-grad forward. Logprob of the generators' token at completion position 562
(fp32 reference -0.016):
TorchTitan trainer model, fused GDN   cuDNN attention -6.87 (full sequence and prefix)   FlashAttention (FA4) -0.0146
transformers bf16, SDPA pinned        CUDNN -9.13    FLASH -0.0008    EFFICIENT -0.0185
fp32 reference: transformers, fp32 weights and activations, SDPA EFFICIENT, TF32 off

What does not reproduce it: varlen_attn alone, random bf16 q/k/v, 24 q / 4 kv heads, head dim 256.
max |out - fp32 reference| over the last 64 rows; causal = first T-1 rows alone vs all T
T = 77,685    cuDNN 8.97e-3   Flash 7.93e-3   both exactly causal
T = 131,072   cuDNN 9.64e-3   Flash 7.85e-3   both exactly causal
T = 262,144   cuDNN 1.10e-2   Flash 7.88e-3   both exactly causal

Kernel that runs by list, eager vs torch.compile (torch profiler, GB300, bf16, head dim 64):
sdpa_backends        eager          torch.compile
[FLASH, CUDNN]       FA4            cuDNN
[CUDNN, FLASH]       cuDNN          cuDNN
[FLASH] (this PR)    FA4            FA4

NOTE: The "fused GDN" row runs #6294 (GDN/KDA chunk kernels: always fused).

You may ask:

  • Why not only on Blackwell? The wrong result was measured on GB300 only; picking by GPU would guess where else it is wrong.
  • What does this cost? Not measured by this PR. On GB300 the muse_spark recipe measured cuDNN ~13% slower than FA4 on its 128k global layers (torchtitan_recipes/models/muse_spark.py). Hopper is unmeasured.
  • Do shapes that only cuDNN accepted now raise? At head dims 256/256, 128/128 and 192/128, the Flash-only default ran FA4 with and without grad on GB300. Other shapes are untested.

NOTE: Measured at one position of one sequence. The trigger is unknown. Shorter sequences and cuDNN 9.28 were not checked.
NOTE: Not run: the 2-GPU test_bitwise_parity.py (this box has 1 GPU).

Test plan

  • test_deterministic_mode_skips_cudnn now lists cuDNN explicitly (the default no longer has it): [CUDNN, FLASH], and [FLASH] in deterministic mode. pytest tests/unit_tests/cpu/models/common/test_varlen_attention.py: 31 passed.
  • pytest tests/unit_tests/cpu/models/common: 362 passed, 1 skipped, 1 failed. The failure, test_lora_forward, also fails on main on this box (flex_attention compile is not supported on its CPU).
  • pytest tests/unit_tests/gpu/models/common/test_varlen_attention.py tests/unit_tests/gpu/ops/test_varlen_attn.py on 1 GB300: 32 passed, 1 skipped. With [FLASH, CUDNN] as the default, test_execution_modes_match_eager[None-compile-*] failed 3 times (eager ran FA4, compile ran cuDNN); main passes it.
  • pytest tests/rl/unit_tests/cpu: 734 passed, 6 skipped, 2 errors, as on main on this box (the 2 errors are test_multiturn_images.py downloads from Hugging Face, which the proxy blocks).
  • pre-commit (ufmt, flake8, codespell, pydoclint) on the changed files: clean.

Felipe Mello and others added 2 commits November 20, 2026 17:15
VarlenInnerAttention.Config.sdpa_backends defaulted to [CUDNN_ATTENTION, FLASH_ATTENTION]. On one 78k-token
Qwen3.8-27B sequence (GB300, cuDNN 9.27, torch 2.16 nightly, no grad), cuDNN attention gave one token a wrong
logprob: -6.87 in the TorchTitan trainer and -9.13 in transformers bf16, against an fp32 reference of -0.016.
FlashAttention (FA4) gave -0.0146. Kernel-only checks on random inputs don't reproduce it.

The default is now [FLASH_ATTENTION]. Listing cuDNN second would not keep a fallback: in eager, torch's
varlen_attn takes Flash whenever it comes first (Flash has no eligibility check), and under torch.compile it
ignores the order and runs cuDNN whenever cuDNN is listed and accepts the call. Dynamo's sdpa_kernel sets the
priority but not the flag varlen's _get_sdp_priority_order reads; a TODO at the sdpa_kernel call says so. On GB300 with
[FLASH, CUDNN], eager ran FA4 and the compiled module ran cuDNN, which failed
test_execution_modes_match_eager[None-compile-*].

A TODO says when to list cuDNN again: once that no-grad forward is fixed, where a timing shows cuDNN faster. test_deterministic_mode_skips_cudnn lists cuDNN explicitly, since
the default no longer has it. The muse_spark recipe keeps its Flash pin; only its comment changes.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant