Skip to content

Token balance labels: keep the sampler's order when the regroup is less even - #18963

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
lonexreb:fix/token-balance-labels-never-worse
Nov 24, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
lonexreb:fix/token-balance-labels-never-worse

Conversation

@lonexreb

@lonexreb lonexreb commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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:

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 #18720; both orders merge to the same tree.

…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]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T08:58:53.549082Z b92f7c9 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: b92f7c9209

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 9730645 into unslothai:main Nov 24, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants