Repository navigation
Packed attention lengths: keep them across BatchEncoding.to(), dense fallback for rejected varlen layers - #18635
Conversation
…mpiled block When ROCm varlen cannot run in the graph, _block_varlen_prepare sent every fp16 / bf16 packed layer to the eager wrapper (_LEAVE_BLOCK). Layers that the wrapper's _static_window rejects anyway (attention sinks such as gpt-oss, logit softcapping, head_dim not a multiple of 8, non-causal, attention dropout in training) then ran the same dense SDPA eagerly after a graph break. Those layers now keep the dense path inside the block, as before #18435; eligible layers still leave for eager varlen.
… batches keep them BatchEncoding.to() moves its tensors in place and returns self, so the send_to_device carry read the already-moved tensor and attached nothing; every packed micro-batch went back to a blocking .tolist(). Read the CPU lengths before calling send_to_device.
Codex Review Summary: ✅ CompletedThis 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: e87e530b57
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
…hs (unslothai#18720) unslothai#18494 and unslothai#18635 carry the packed document lengths to the device as a host tuple, so a packed micro-batch no longer reads them back. `UNSLOTH_TOKEN_BUDGET` undoes that for the batches it splits: the split calls `.tolist()` on the device tensor, and each chunk gets a new device tensor with no tuple, so every chunk's forward reads its lengths from the device again. The split now uses the attached tuple when it matches the tensor and attaches each chunk's own lengths. Values are unchanged. With labels in the batch the split still makes its one read of the labels at the cut points. `tests/test_token_budget_host_lengths.py` splits a packed batch on the `meta` device, where any device read raises, and passes the chunks to `get_packed_info_from_kwargs`: 3 failed on main fe5340f, 3 passed here. The token budget and host-length suites give 93 passed, 1 skipped with and without the change. Step time was not measured. ## Takeover update Checked the chunk lengths against the packing host-lengths contract (a tuple with the matching count, as attached by _carry_host_lengths). On an L4 (function-level probe), the per-chunk forward length reads that synced went from 5 to 0 for a 5-chunk split, and the split itself drops one lengths read (6 -> 5 syncs without labels, 8 -> 7 with labels; the rest is the labels boundary check, which exactness needs). The second test now imports unsloth.utils.packing directly instead of skipping on ImportError. Applies cleanly on current main and is independent of unslothai#18963 (198 token budget + packing tests pass on the combined tree). --------- Co-authored-by: danielhanchen <[email protected]>
Follow-up fixes for #18435, #18494, found while re-checking the PRs merged on 2026-11-19. One commit per fix; every fix has a test that fails on main and passes here.
Tests (this branch vs main
54af93780b, CPU)prepush_gate.pypasses.ROCm packed attention: rows eager varlen rejects stay dense in the block (follows up #18435)
Summary
Follow-up to #18435. On ROCm builds where torch's flash varlen cannot run inside a compiled graph, packed layers that the eager varlen wrapper rejects anyway now stay inside the compiled block on the dense path. Before this change they left the block, which costs a graph break per layer, and then ran the same dense attention eagerly.
What broke
#18435 made
_block_varlen_preparereturn_LEAVE_BLOCKfor every fp16 / bf16 packed layer with head dim up to 256 whenever the in-graph varlen probe refuses. The eager wrapper then checks the call with_static_window, which rejects:_softcapped);For those layers the eager wrapper calls the original dense SDPA. So the row left the compiled block only to run the attention it would have run inside the graph before #18435.
Repro (CPU, ROCm faked through
torch.version.hip, in-graph probe refusing)is_causal=False, dropout 0.1 in training_LEAVE_BLOCK(graph break, then dense)None(dense inside the block)_LEAVE_BLOCK(eager varlen)_LEAVE_BLOCK(eager varlen)Fix
_eager_varlen_rejects(attention, kwargs, head_dim)mirrors the layer-level conditions of_static_window, which need no tensors. When it is true, the ROCm branch returnsNoneand the layer keeps the existing dense path. It does not count ascompiled. Eligible layers, CUDA builds and ROCm builds whose varlen traces are unchanged.Tests
New
tests/test_hf_packed_attention_rocm_static_reject.py(CPU). Command (per worktree):python -m pytest -o addopts= -m "not server and not native_mxc" -p no:cacheprovider -q tests/test_hf_packed_attention_rocm_static_reject.py tests/test_hf_packed_attention_rocm_leave_block.py tests/test_hf_packed_attention.py. Each arm imports its own tree (the test asserts it).Not proven here
Packed SFT: host lengths survive an in-place BatchEncoding.to() (follows up #18494)
What broke
#18494 wraps accelerate's
send_to_deviceso that the CPUpacked_seq_lengthsfrom the packing collators travels as a host tuple on the moved tensor. That avoids a blocking.tolist()per packed micro-batch. The carry read the CPU tensor aftersend_to_devicereturned. ABatchEncoding, which transformers'DataCollatorForLanguageModelingreturns and which Unsloth installs in place ofDataCollatorForSeq2Seq(unsloth/models/rl.py:3312), moves in place:BatchEncoding.to()replaces its tensors and returnsself. By the time the carry ran, the "before" batch already held the device tensor, so it attached nothing. Every packed micro-batch then took the device read again.Repro (CPU,
metadevice, real acceleratesend_to_device)moved is batch, and_unsloth_lengths_tupleisNone_unsloth_lengths_tuple == (3, 2, 4)Fix
The wrapper reads the host tuple from the CPU tensor before calling the original
send_to_device, then attaches it to the moved tensor when that tensor is off the CPU and has the same length. Plain dict batches behave as before.Flip test
tests/test_packed_host_lengths_batch_encoding.py:tests/test_packed_host_lengths_and_eval_flush.pypasses on both; it is updated for the helper's new signature.Not proven here
ROCm hardware for the packed attention change; step time of packed SFT.