Skip to content

Packed mRoPE flash attention: cover a padded tail, same decision when compiled - #16058

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s9-packed-mrope-pad-tail
Oct 30, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s9-packed-mrope-pad-tail

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #15771.

Problem

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, which is the leak #15771 set out to stop (lengths [3, 5] in a 16-token row).

Under torch.compile the 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 under torch.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:

  • main: 5 failed, 8 passed, 2 skipped. Failing: 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].
  • this branch: 13 passed, 2 skipped.

On CUDA (B200, flash attention 2 from the kernels-community/flash-attn2 hub kernel), same test file:

  • main: 5 failed, 10 passed.
  • this branch: 15 passed, including both flash attention cases.

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-attn wheel. 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.

… 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.
@danielhanchen danielhanchen self-assigned this Oct 30, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T03:40:33.293822Z 3316443 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 3316443013

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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]
@danielhanchen
danielhanchen merged commit 85176e5 into main Oct 30, 2026
@danielhanchen
danielhanchen deleted the dh/s9-packed-mrope-pad-tail branch October 30, 2026 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant