Repository navigation
Keep packed documents apart on Unsloth's own models (flash path, eval, cached eval) and match transformers' Mistral window - #13313
Conversation
…ransformers' sliding window
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. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6bd427a6c5
ℹ️ 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".
|
|
||
| use_varlen = seq_info is not None and past_key_value is None and window_size == (-1, -1) | ||
| # Packed rows always take varlen: dense flash would let a document attend into the one before it. | ||
| use_varlen = seq_info is not None and past_key_value is None |
There was a problem hiding this comment.
Keep packed eval on varlen when producing a cache
In eval mode, Mistral's default config.use_cache=True causes line 113 to replace the initially absent cache with (K, V) before this condition is evaluated. Consequently use_varlen is false, FlashAttention selects its dense path, and packed_seq_lengths is ignored, so documents in a packed row can still attend across boundaries. The newly added packed-eval test also omits use_cache=False and will fail on CUDA environments with real FlashAttention installed; determine varlen eligibility from whether an input cache was supplied rather than from the output cache being constructed.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed in 2e8de8f. The varlen decision now reads the cache passed in (had_cache, taken before past_key_value becomes the returned cache), so an eval forward with use_cache=True keeps a packed row on varlen. Every Unsloth model forward had the same pattern (llama, qwen3, cohere, granite, gemma2, falcon_h1, mistral), so all of them get the same change. The packed eval tests now run on the flash path with use_cache=True for Mistral (with and without a window), Llama and Qwen3, and fail on main. Inside the trainer, prediction_step already passes use_cache=False for packed batches, so this mainly covers direct eval forwards.
…cache keep documents apart on flash
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
|
The red here is not from this PR. This run merged onto main at
A rerun replays the same merge commit, so updating the branch onto current main should clear both. |
Unsloth's own Mistral attention sends packed (padding-free /
packing=True) rows to dense flash attention whenever the packed row is longer thansliding_window, becauseuse_varlenalso required no active window. Dense flash ignorespacked_seq_lengths, so with flash-attn installed every document in a packed row attends into the documents before it, up to the window. The xformers and SDPA paths were not affected: both already build a block-diagonal mask with the window.Separately, in eval mode without xformers,
CausalLM_fast_forwardbuilds a causal mask over the whole row and ignorespacked_seq_lengths, so SDPA attends across the documents of a packed row. Padding-free is auto-enabled for SFT, sotrainer.evaluate()eval batches are packed rows and hit this. Training mode was already correct (it builds no mask), as is Llama's eval path.The dense flash path also used
window_size = (sliding_window, sliding_window), one key wider than transformers (k > q - sliding_window, which is(sliding_window - 1, sliding_window - 1)in transformers' own flash path) and than Unsloth's SDPA and xformers paths.Change
use_varlenno longer requireswindow_size == (-1, -1): packed rows takeflash_attn_varlen_func, which gets the window throughflash_varlen_kwargs["window_size"](supported since flash-attn 2.3, the same versions the dense path already passes it to).The window is
(sliding_window - 1, sliding_window - 1)on both flash paths.A packed row builds no causal mask in eval either, the same as training, so attention keeps the documents apart from
packed_seq_lengths(SDPA varlen / per-segment path, with the window).Every Unsloth model forward decided varlen from
past_key_valueafter it had been replaced by the cache it returns, so an eval forward withuse_cache=True(the default) sent a packed row to dense flash, across documents.use_varlennow reads the cache passed in (had_cache) in llama, qwen3, cohere, granite, gemma2, falcon_h1 and mistral. Inside the trainerprediction_stepalready passesuse_cache=Falsefor packed batches, so this covers direct eval forwards.Rows that are not packed and rows no longer than the window are unchanged apart from the one-key window fix.
Tests
New
tests/utils/test_packed_flash_documents.pyloadshf-internal-testing/tiny-random-MistralForCausalLMwith a window of 4 and stands in for flash-attn with referenceflash_attn_func/flash_attn_varlen_functhat apply flash-attn's causal and window semantics exactly:use_cache=True, with and without a window, on the SDPA and the flash path, gives the same logits as each document alone (fails on main)With the existing packing, padding-free eval and attention test files (
test_packing.py,test_padding_free_eval.py,test_attention_masks.py,test_sdpa_packed_segments.py) everything passes excepttest_peak_memory_is_linear_in_tokens, which fails the same way on main on this B200 host.