Repository navigation
Mask packed document boundary labels for TRL older than 0.23.1 - #13324
Conversation
…or wrappers TRL >= 0.23.1 sets labels[position_ids == 0] = -100 in its padding-free collator; older TRL leaves the first token of every packed document as a target of the previous one. FastLanguageModel's forward masks those positions, but the transformers modeling path (FastModel) does not, while unsloth_zoo's num_items_in_batch already discounts them, so the loss and gradients were inflated. Mask them in enable_sample_packing and enable_padding_free_metadata, idempotently and out of place, only for a flattened single row.
Codex Review SummaryThis 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 SummaryThis 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. |
|
The red here is not from this PR. This run merged onto main at
A rerun replays the same merge commit, so updating the branch onto current main should clear both. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! 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". |
|
Verified the boundary-label fix independently. No additional correctness defect found; Base Real TRL collators, both wrappers, documents of lengths
TRL 0.22.2 base gradient error was Additional execution:
Environment: Linux x86_64, Python 3.11.15, PyTorch 2.8.0+cu128; execution in a credential-free bubblewrap sandbox with the real GPU. The model comparison uses real transformers forward/backward with controlled attention, not a FastModel training run. Harnesses, exact dependency pins, GPU results, benchmark, and negative-control logs. Reproduction entry points: |
|
@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". |
mahiatlinux
left a comment
There was a problem hiding this comment.
LGTM! Ready to merge, all manually checked.
With TRL older than 0.23.1, auto padding-free SFT on the transformers modeling path (
FastModel/FastBaseModel) trains the first token of every packed document as a target of the previous document. The reported loss and the gradients are both inflated.Cause
labels[position_ids == 0] = -100to its padding-free collator. TRL 0.18.2 to 0.22.2, all inside Unsloth's supported range (trl>=0.18.2), do not mask those positions.FastLanguageModel's own forward masks them anyway (mask_packed_boundary_labelsinllama.py/mistral.py), so it is not affected. The transformers modeling forward has no such guard.num_items_in_batchalready zeroes those slots out of the count, so the loss divides boundary-inclusive sums by a boundary-free count.Change
enable_sample_packingandenable_padding_free_metadatanow pass the collated batch throughmask_packed_boundary_labelswith thepacked_seq_lengthsthey already attach, so every TRL version produces the labels TRL 0.23.1+ produces.seq_lengthsindex it; unflattened rows are left alone.unsloth_zoo>=2026.10.3), so pre-masked labels are not double counted.Verification
Real TRL collator wrapped by Unsloth, three documents of lengths 3, 2, 3:
[10, 11, 12, 13, 14, 15, 16, 17][-100, 11, 12, -100, 14, -100, 16, 17][-100, 11, 12, -100, 14, -100, 16, 17]Qwen3-0.6B LoRA SFT, auto padding-free, gradient checkpointing on, 12 steps, main and this PR on one Colab A100. First three losses, with the padded (unpacked) run as the reference:
FastModelmainFastModelthis PRFastLanguageModelmain / this PRWith TRL 0.22.2 the fix brings
FastModelonto the padded loss; with TRL 0.25.1 the first-step loss is unchanged (the rest is bf16 run-to-run noise), as isFastLanguageModeleverywhere.Step time is unchanged (one
index_fillon the CPU label tensor per batch).Tests
tests/utils/test_packing.py: both wrappers mask the boundary targets of an unmasked (TRL < 0.23.1 style) collator and the zoo-style count still sees every real target once; already-masked labels come back unchanged; unflattened rows are untouched. The previous test pinned the unmasked labels against a count helper that subtracted a constant, which the zoo stopped doing; it now mirrors the zoo's zeroing. 104 passed, 2 skipped.