• fix(sft): FakeDataset variable-length random samples have mismatched lengths (#4048)

    carsten released this 2026-10-13 20:01:10 +00:00

    With length="variable" and input_ids="random", FakeDataset drew a
    per-sample seq_len but always generated self.seq_len + 1 random ids.
    Samples ended up with input_ids/target_ids of length self.seq_len,
    while position_ids, loss_mask and seq_lens used the drawn length.
    After packing with CatDataset + cat_collate this gave e.g.
    input_ids of shape (1, 128) next to position_ids/loss_mask of
    shape (1, 101).

    • _draw_sample now generates seq_len + 1 random 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 counts seq_len (the emitted input_ids
      length), matching SFTDataset's post-shift count, instead of seq_len + 1 (or self.seq_len + 1 in the random case).
    • Added test_fake_dataset_sample_lengths, parametrized over length ×
      input_ids. It asserts that input_ids, target_ids, position_ids
      and loss_mask all have the same length and that sum(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 on main for random-variable)
    • Packed 20 variable+random samples with CatDataset(..., 128) +
      cat_collate: input_ids, target_ids, position_ids and loss_mask
      shapes match

    🤖 Generated with Claude Code

    Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

    Downloads