[PyTorch] Support padded THD sequences with FlashAttention 4#3205
Draft
xiaoyao0115 wants to merge 1 commit into
Draft
[PyTorch] Support padded THD sequences with FlashAttention 4#3205xiaoyao0115 wants to merge 1 commit into
xiaoyao0115 wants to merge 1 commit into
Conversation
Signed-off-by: xiaoyao0115 <1804647152@qq.com>
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.
Description
Add support for
pad_between_seqs=Truewith the FlashAttention 4 backend for physically padded THD tensors.The FlashAttention varlen kernels use padded cumulative sequence boundaries to describe the physical tensor layout and
seqused_q/seqused_kto describe the valid token counts. FlashAttention 4 was previously disabled for this mode, and FlashAttention 3/4 may leave unused padding rows unwritten. This change enables the FA4 path and guarantees zero values at inter-sequence padding positions in both the attention output and Q/K/V gradients.FA2 remains disabled for
pad_between_seqs, and FA4 context parallel support is unchanged (not enabled by this PR).Type of change
Changes
cu_seqlens_q/k_paddedand validseqused_q/kto the FA4 varlen API.Validation
4.0.0b4) kernel parity test on GB200 against an equivalent compact varlen reference, including output and Q/K/V gradients: passed.python -m py_compileon all modified Python files: passed.git diff --check: passed.The available container's preinstalled Transformer Engine C++ extension does not match the current
mainPython API, so the full source-tree pytest suite could not be run in that environment. A separate FA3 runtime was not available; the shared masking path was exercised with FA4, while the existing FA3pad_between_seqspath remains unchanged.Checklist