Repository navigation
Packed mRoPE flash attention: cover a padded tail, same decision when compiled - #16058
Merged
Merged
Conversation
… compiled Follow-up to #15771. A padding-free collator with pad_to_multiple_of pads the flattened row after the document lengths are taken, so the lengths stop short of the query. The wrapper then passed the row through, and flash attention ran causal over every document, the leak #15771 set out to stop. The tail now gets its own segment through cover_padded_cu_seqlens, as Unsloth's own attention dispatch already does; lengths running past the row are still passed through. Under torch.compile the coverage check was skipped, so a padded row got boundaries that left the tail unwritten and an overrunning row got boundaries past the query, and a warm metadata cache handed the compiled region its maximum document length as a guarded constant, compiling one graph per distinct length (8 frames for 8 lengths on CPU, eager backend, dynamic=True). The boundary computation now runs under torch.compiler.disable when compiling, so compiled calls take the eager decision and the maximum length arrives as a call result: 3 frames for the same 8 lengths, warm or cold.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
[skip ci]
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.
Follow-up to #15771.
Problem
A padding-free collator with
pad_to_multiple_ofpads the flattened row after the document lengths are taken, so the lengths stop short of the query. The wrapper then passed the row through and flash attention ran causal over every document, which is the leak #15771 set out to stop (lengths [3, 5] in a 16-token row).Under
torch.compilethe coverage check was skipped. A padded row got boundaries that left the tail unwritten, and an overrunning row got boundaries past the end of the query. A warm metadata cache also handed the compiled region its maximum document length as a guarded constant, so it compiled one graph per distinct length (8 frames for 8 lengths).Change
The padded tail gets its own segment through
cover_padded_cu_seqlens, as Unsloth's own attention dispatch already does (cu_seqlens [0, 3, 8, 16] for the example). Lengths running past the row are still passed through. When compiling, the boundary computation runs undertorch.compiler.disable, so compiled calls take the same decision as eager and the maximum length arrives as a call result: 3 frames for the same 8 lengths, warm or cold. Warm calls now have 2 graph breaks instead of 0, in exchange for no per-length recompiles.Tests
tests/test_flash_attention_mrope_packed.py, CPU, torch 2.13, transformers 5.17:test_padded_tail_gets_its_own_segment,test_compiled_calls_match_eager_boundaries[False],[True],test_compiled_calls_do_not_recompile_per_document_length[False],[True].On CUDA (B200, flash attention 2 from the
kernels-community/flash-attn2hub kernel), same test file:With real flash attention on a row ending in a 16 token padded tail, main lets the second document attend into the first (max abs error 3.98 against attending that document alone); this branch gives 0.0. Without a tail both give 0.0.
Not covered
The CUDA cases ran with the hub flash attention kernel, not the
flash-attnwheel. If the whole model is compiled, each packed attention call now has a graph break where main had none; Unsloth compiles with attention excluded by default, so the default flow should not see it. The cold.item()graph break remains, on par with the upstream packed varlen path.