-
fix(sft): FakeDataset variable-length random samples have mismatched lengths (#4048)
released this
2026-10-13 20:01:10 +00:00 With
length="variable"andinput_ids="random",FakeDatasetdrew a
per-sampleseq_lenbut always generatedself.seq_len + 1random ids.
Samples ended up withinput_ids/target_idsof lengthself.seq_len,
whileposition_ids,loss_maskandseq_lensused the drawn length.
After packing withCatDataset+cat_collatethis gave e.g.
input_idsof shape (1, 128) next toposition_ids/loss_maskof
shape (1, 101)._draw_samplenow generatesseq_len + 1random ids. Normal
iteration and fast-forward both go through_draw_sample, so resume
still replays the same generator stream. The stream itself changes for
variable+random, which is fine for fake data.num_tokens["fake"]now countsseq_len(the emittedinput_ids
length), matchingSFTDataset's post-shift count, instead ofseq_len + 1(orself.seq_len + 1in the random case).- Added
test_fake_dataset_sample_lengths, parametrized overlength×
input_ids. It asserts thatinput_ids,target_ids,position_ids
andloss_maskall have the same length and thatsum(seq_lens)equals
it. On the old code it fails only for[random-variable].
Test plan
pytest tests/unit/train/sft/test_fake_dataset.py: 12 passed (the new
test fails onmainforrandom-variable)- Packed 20 variable+random samples with
CatDataset(..., 128)+
cat_collate:input_ids,target_ids,position_idsandloss_mask
shapes match
🤖 Generated with Claude Code
Co-authored-by: Claude Opus 5.5 noreply@anthropic.com
Downloads