Repository navigation
Token budget: split packed chunks read and keep the host packed lengths - #18720
Merged
danielhanchen merged 2 commits intoNov 24, 2026
Merged
danielhanchen merged 2 commits into
danielhanchen merged 2 commits into
Conversation
Splitting a packed batch read packed_seq_lengths back from the device and built each chunk's lengths as a fresh device tensor, so every chunk's forward read them from the device again. The split now uses the host tuple the packing collators attach (unslothai#18494, unslothai#18635) and attaches each chunk's own.
…ipping on ImportError tests/utils/test_packing.py already imports unsloth.utils.packing at module level, so a broken import should fail here too rather than be reported as a skip. [skip ci]
Member
|
@codex review |
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: Didn't find any major issues. Another round soon, please! 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". |
FiditeNemini
pushed a commit
to FiditeNemini/unsloth
that referenced
this pull request
Nov 24, 2026
…ss even (unslothai#18963) `UNSLOTH_TOKEN_BALANCE=labels` (unslothai#18768) regroups a window of optimizer steps with the equal-size Karmarkar-Karp pass and uses the result unconditionally. The pass is a heuristic, so now and then the regrouped steps are less even than the order the sampler gave: ``` step_size = 3, label_counts = [2, 3, 3, 9, 0, 1] order given per-step labels [8, 10] max - min 2 balance_label_steps per-step labels [11, 7] max - min 4 ``` `balance_label_steps` now compares the max - min of per-step label sums before and after, and returns the order it was given when the regroup is strictly worse. Otherwise the output is unchanged. It is rare and small: 19 of 20000 random windows at the default shape (16 rows x 8 steps, lognormal counts), worst seen `[428, 433]` becoming `[342, 519]`. The cost is one extra pass over the window's counts. Test (fails on main): `tests/test_token_budget_batches.py::test_label_steps_are_never_less_even_than_the_order_given`, the case above plus 2000 random windows. ## Takeover update Karmarkar-Karp is a heuristic, and the per-step split used by UNSLOTH_TOKEN_BALANCE=1 (cross-rank and grad-accum slots) could also come out less even than the sampler's order (about 1 in 12 random windows at batch_size=4, grad_accum=2; e.g. slot sums [1085, 998] vs the sampler's [1029, 1054]). The check is now one shared helper (_more_even) applied at the label, rank and slot levels; wherever the regroup is kept the output is unchanged (39,076 of 40,000 random cases identical, the rest at least as even). With both opt-ins unset nothing is installed, so default training is identical. Added test_step_balance_is_never_less_even_than_the_order_given (fails before, passes after); token budget tests 85 passed. Independent of unslothai#18720; both orders merge to the same tree. --------- Co-authored-by: Claude Fable 5.2 <[email protected]> Co-authored-by: danielhanchen <[email protected]>
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.
#18494 and #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_BUDGETundoes 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.pysplits a packed batch on themetadevice, where any device read raises, and passes the chunks toget_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 #18963 (198 token budget + packing tests pass on the combined tree).