Repository navigation
Token balance labels: keep the sampler's order when the regroup is less even - #18963
Merged
danielhanchen merged 2 commits intoNov 24, 2026
Merged
Conversation
…ss even Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01Pa8JdyYMHkqNpdksTfdmRG
…nd slot split too balance_step_indices runs the same Karmarkar-Karp heuristic for the cross-rank split and the grad-accum slots, and it can also land on a less even split than the sampler gave (about 1 in 12 random windows at batch_size=4, grad_accum=2). Share one helper with balance_label_steps and keep the given rank or slot assignment when the regroup is worse. Accepted regroups are unchanged. [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. 🎉 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
…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]>
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.
UNSLOTH_TOKEN_BALANCE=labels(#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:balance_label_stepsnow 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 #18720; both orders merge to the same tree.