Repository navigation
Token balance: UNSLOTH_TOKEN_BALANCE=labels evens out supervised tokens per optimizer step - #18768
Conversation
|
@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 Summary: ❌ FailedThis 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: da3222f7ba
ℹ️ 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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ee9aabb895
ℹ️ 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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dc7785bac2
ℹ️ 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. 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". |
…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]>
Add UNSLOTH_TOKEN_BALANCE=labels: even out supervised tokens per optimizer step
With
train_on_responses_only(or any prompt-masked dataset), the number of tokens that actually carry a loss swings a lot from one optimizer step to the next, since some rows are mostly prompt and others mostly answer. The loss is normalised by the step's ownnum_items_in_batch, so a step with few supervised tokens weighs each of them much more than a step with many.The existing
UNSLOTH_TOKEN_BALANCE=1only reorders rows inside one step window, so it never changes per-step totals. This adds an opt-in labels mode:UNSLOTH_TOKEN_BALANCE=labelsreads the per-row supervised count from the dataset as the loss sees it (shiftedlabels != -100, elsecompletion_mask/assistant_masks) and regroups rows acrossUNSLOTH_TOKEN_BALANCE_WINDOW(default 8) consecutive optimizer steps with the same Karmarkar-Karp partitioner, so every step carries a similar supervised count.This changes which rows share a step, so it changes the data order. That is why it is its own opt-in value;
UNSLOTH_TOKEN_BALANCE=1(exact) is unchanged and nothing changes by default.Results
Per-step supervised-token CV, measured on the real collator output (FineTome rows, Qwen3 chat template,
train_on_responses_only, batch 4 x accumulation 4):=1)Colab T4, Qwen3-0.6B LoRA r=16, fp16, 64 steps (1024 rows, a multiple of the window, so both arms train on exactly the same rows), held-out loss over 256 unseen FineTome rows with the same response-only masking:
The per-step supervised count spread drops about 3.3x. Held-out loss is not worse (mean -0.00016, inside the seed spread), so treat this as a stability knob, not a loss improvement.
Step time on T4 is unchanged: with the arms run in reversed order and an off/off repeat on one card, median step time was 2.257 s (labels) vs 2.256 s and 2.252 s (off, off). In the first runs, where labels always ran second, it showed +1 to +4%, which the reversed run traces to run order on the card, not to the change.
Tests
tests/test_token_budget_batches.py: the regroup keeps every row and its window, keeps order inside a step, lowers CV; the sampler handles partial tails and the kept first window; dataloader end to end with padded and padding-free batches; falls back with a note when there is no labels column; label counts use shifted labels and masks. Existing token balance and budget tests unchanged and passing.