Streaming training silently replays already-consumed samples when resuming from a checkpoint
Checklist / 检查清单
- [x] I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。
Bug Description / Bug 描述
Summary
With --streaming true and the Megatron backend, resuming from a checkpoint restarts the training stream at position 0 instead of fast-forwarding past the samples already consumed. Every sample the run had seen since the last checkpoint is silently re-trained — no error, no warning, and a streaming dataset cannot be seeked back through after the fact, so the only symptom is an unexplained loss regression right after resume.
Root cause
_prepare_dataloader (swift/megatron/trainers/base.py) threads the consumed count into the batch sampler for the non-streaming path, but the streaming branch returns early without any resume information:
if args.streaming:
train_dataloader = build_streaming_dataloader(args, train_dataset, self.data_collator)
if val_dataset is not None:
val_dataloader = build_streaming_dataloader(args, val_dataset, self.data_collator)
return train_dataloader, val_dataloader # <- no resume info
consumed_samples = self.state.consumed_train_samples # <- non-streaming p
...
The skip infrastructure already exists and just is never wired up for streaming:
DataLoaderDispatcher.__init__(..., skip_batches=0)and_skip_batches(her.py) already fast-forward rank 0's iterator byworld_sizebasebatches per skipped logical step;build_streaming_dataloader(swift/megatron/trainers/utils.py) constructsMegatronDataLoaderDispatcher(base_dataloader)without askip_batchesargument, so the mechanism is unreachable for streaming runs.
Resume semantics therefore exist on the non-streaming path and are absent on the streaming path of the same function.
Impact
We hit this in production: a mid-run crash forced a resume from the last checkpoint, and with a 1200-step checkpoint interval at ~3.7 s/step the replay silently re-trained ~74 minutes of data per rank. This is the default resume path for our streaminration — every interrupted streaming job pays it.
### How to Reproduce / 如何复现
- ms-swift: current `main` (9853e46)
- Megatron backend (`--tuner megatron`), multi-GPU, streaming via `--streaming true` with any `IterableDataset` (we run a webdataset-style `IterableDataset` over tar shards)
Steps:
1. Train with `--streaming true` using any `IterableDataset` whose samples carry a monotonically increasing id, for a few steps; save a checkpoint.
2. Resume from that checkpoint.
3. The first batches after resume contain sample ids starting from 0 again — the same samples as the very first run — instead of continuing after the checkpoint.
### Additional Information / 补充信息
Related: the same replay class was fixed for the non-streaming sampler path in #10052 (#10050); the streaming branch is the remaining gap.
## Suggested fix
Thread the consumed count through, exactly like the non-streaming branch:
```python
skip_batches = state.consumed_train_samples // (args.micro_batch_size * dp_world_size)
build_streaming_dataloader(..., skip_batches=skip_batches)
The divisor is exact by construction: one MegatronDataLoaderDispatcher logical step has rank 0 draw one base batch (of micro_batch_size) per DP rank and scatter one to each, so a logical step consumes exactly micro_batch_size * dp_world_size global samples — the same quantity consumed_train_samples counts. A non-divisible remainder should floor and warn (replaying a bounded suffix is safe; skipping past unseen data is not). The validation dataloader is intentionally not skipped, matching the non-streaming path's consumed_samples=0 for validation.
We run this fix in our production streaming pipeline; happy to open a PR with it plus unit tests for the skip computation.