[megatron] Fix FA4 backward recompilation: pass max_seqlen as int in PackedSeqParams - #9798
Merged
tastelikefeet merged 1 commit intoJul 30, 2026
Conversation
…PackedSeqParams get_packed_seq_params() passed max_seqlen_q/kv as 0-dim CUDA tensors (from cu_seqlens.diff().max()). flash-attn 4 (CuTe DSL) embeds booleans derived from max_seqlen into its backward-kernel compile-cache key, and a tensor there hashes by object identity, so the cache never hits and every micro-batch backward triggers a full JIT recompilation. Measured on 8xB200 (Qwen3.5-35B-A3B SFT, packing + padding_free, EP=8): - before: 482-535 s/it (GPUs ~0% util, all ranks stall on MoE all-to-all while one rank compiles) - after: 13-14 s/it steady state Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
tastelikefeet
approved these changes
Jul 30, 2026
tastelikefeet
approved these changes
Jul 30, 2026
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.
Fixes #9797
PR type
PR information
get_packed_seq_params()passedmax_seqlen_q/kvas 0-dim CUDA tensors (fromcu_seqlens.diff().max(), no.item()).flash-attn 4 (CuTe DSL) embeds booleans derived from
max_seqleninto its backward-kernel compile-cache key. A 0-dim tensor there hashes by object identity, so the cache never hits and every micro-batch backward triggers a full JIT recompilation (tens of seconds each). With expert parallelism the other ranks stall on MoE all-to-all while any rank compiles, so the whole step is serialized on compilation. The forward key does not contain these fields, so only backward is affected.This PR converts
max_seqlen_q/kvto Python int before constructingPackedSeqParams(one microsecond-level device sync per micro-batch, negligible). This also matches the flash-attn varlen API contract (max_seqlenis documented/typed as int;transformers/modeling_flash_attention_utils.pyfollows the same convention).Root-cause evidence (from hooking
flash_attn.cute.interface._flash_attn_bwd.compile_cacheduring real training): 16 compilations of the same key within the first minute, with the key ending inExperiment results
8×B200, Qwen3.5-35B-A3B full-parameter SFT (megatron sft, packing + padding_free, EP=8, max_length=8192,
--attention_backend auto):Loss curve identical before/after (first-step loss 1.3514 in both).
🤖 Generated with Claude Code