Repository navigation
[attention] Varlen attention defaults to SDPA's FlashAttention only - #6295
Draft
felipemello1 wants to merge 2 commits into
Draft
felipemello1 wants to merge 2 commits into
felipemello1 wants to merge 2 commits into
Conversation
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.
This branch has not been deployed
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 (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
VarlenInnerAttentionalready requires on SM90. Speed is not measured.TLDR:
VarlenInnerAttentionnow 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.Problem
VarlenInnerAttention.Config.sdpa_backendssets the SDPA backends for torch'svarlen_attn.pytorch/torchtitan#5152added the field with cuDNN first, for speed (review: "I think we preferred cudnn because it's more performant").torch/nn/attention/varlen.py,_cudnn_supports_head_dims). Only (256, 256) was measured. Hence the table above.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
["FLASH_ATTENTION"]. On Blackwell,VarlenInnerAttentionroutes Flash to FA4.torch.compile,varlen_attnignores the listed order and runs cuDNN whenever it is listed and accepts the call. Dynamo'ssdpa_kernelsets torch's priority order but not the flag that varlen's_get_sdp_priority_orderreads. A TODO at thesdpa_kernelcall says so.sdpa_backendsstill 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
pytorch/torchtitan#5882adds a second recipe-level Flash pin.Results
NOTE: The "fused GDN" row runs #6294 (GDN/KDA chunk kernels: always fused).
You may ask:
torchtitan_recipes/models/muse_spark.py). Hopper is unmeasured.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_cudnnnow 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.pyon 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 aretest_multiturn_images.pydownloads from Hugging Face, which the proxy blocks).