Exclude trailing padding token from the fine-tuning loss - #1697
Open
yentur wants to merge 1 commit into
Open
Conversation
default_loss masks targets with `steps <= lengths[:, 1]`, but `steps[i]` is the index of `targets[i]` in the un-shifted row, so the last real target sits at index `length - 1` and the `<=` bound reaches one index past the end, into the zeros iterate_batches padded the row with. Every sequence is therefore trained to predict token id 0 after its last token, and ntoks is one too high. With completion only training that is a large share of the loss: a six token prompt with a three token answer counts four targets per row instead of three. The dropped position is in range only when the row was padded, so no real target can be lost.
yentur
force-pushed
the
fix/loss-trailing-pad-token
branch
from
August 9, 2026 10:59
013560e to
7a02faa
Compare
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.
Problem
default_losscounts one padding position per sequence.iterate_batchesright padseach row with zeros and reports
lengths[:, 1] = L, the number of real tokens, but themask selects
L - offset + 1targets and the extra one is the pad value0.So every fine-tuned sequence is trained to predict token id 0 after its last token, and
ntoksis one too high. With--mask-promptthat padded position is a large share ofthe signal: a six token prompt with a three token answer counts four targets per row
instead of three.
Root cause
mlx_lm/tuner/trainer.py:92targets[k] == batch[k + 1], sosteps[k]is the index oftargets[k]in theun-shifted row. Real tokens occupy
0..L-1, so the last valid target index isL - 1and
steps <= Lreaches one past the end.Fix
steps <= lengths[:, 1:]becomessteps < lengths[:, 1:].step == Lis in range only whentargets.shape[1] >= L, which happens only if the rowwas padded, so this cannot drop a real target. Rows truncated to
max_seq_lengthhaveno padding column and come out unchanged. Reported train and validation loss will move
slightly, since
ntoksis the denominator.Verification
Four rows, six token prompt and three token answer, driven through the real
iterate_batchesanddefault_loss. Before:After:
The second pair is the invariant worth holding: changing only the bytes in the padding
region must not change the loss.
The two new tests fail on main,
and pass with the fix:
test_finetune.py,test_tuner_trainer.py,test_tuner_utils.pyandtest_losses.pygive 29 passed.
test_datsets.py::TestDatasets::test_hffails on an unmodified checkouttoo,
huggingface_hubrejects the barebillsumname.A 20 iteration LoRA run on
mlx-community/Qwen2.5-0.5B-Instruct-4bitwith--mask-prompttrains normally, val 2.249 to 0.585.