cyclic dataloader without data sharding repeats and skips samples when resumed at a different data-parallel size (dataset size not divisible by micro_batch_size x DP)
Describe the bug
With --dataloader-type cyclic and --no-data-sharding, a run resumed at a different data-parallel size repeats some samples and skips others whenever the dataset size is not a multiple of micro_batch_size × data_parallel_size. There is no error or warning.
MegatronPretrainingRandomSampler.__iter__ rebuilds its in-epoch position from consumed_samples:
active_total_samples = self.total_samples - self.last_batch_size # last_batch_size = total % (mbs * dp)
self.epoch = self.consumed_samples // active_total_samples
current_epoch_samples = self.consumed_samples % active_total_samplesThe shuffled permutation (full_bucket_size, seeded by the epoch) does not depend on the DP size, but the epoch length does. After a DP change, the resumed ranks put the epoch boundary somewhere else. They skip or replay the tail of the current epoch, and every later epoch starts at the wrong offset.
This is separate from #7299 and from the sharded-path issue reported in #7645. The reproduction below gives the same counts with #7299's diff applied.
Steps/Code to reproduce bug
This runs on CPU and needs only the sampler class (checked against main @ ec806b2).
from collections import Counter
import torch
from megatron.training.datasets.data_samplers import MegatronPretrainingRandomSampler
N, MBS, GBS, STEPS = 250, 2, 16, 32 # N is not a multiple of MBS * DP for DP = 8
def draws(dp_before, dp_after, resume_step):
"""Every ID drawn over STEPS global steps, resuming at `resume_step` with another DP size.
Samplers are rebuilt from consumed_samples (build_pretraining_data_loader) and re-iterated
at the end of an epoch (dataloader_type='cyclic')."""
drawn = []
for dp, lo, hi in ((dp_before, 0, resume_step), (dp_after, resume_step, STEPS)):
ga = GBS // (MBS * dp)
samplers = [MegatronPretrainingRandomSampler(
torch.arange(N), total_samples=N, consumed_samples=lo * GBS, micro_batch_size=MBS,
data_parallel_rank=r, data_parallel_size=dp, data_sharding=False) for r in range(dp)]
its = [iter(s) for s in samplers]
for _ in range(lo, hi):
for r in range(dp):
for _ in range(ga):
try:
drawn += next(its[r])
except StopIteration:
its[r] = iter(samplers[r])
drawn += next(its[r])
return Counter(drawn)
for before, after, step in ((1, 1, 15), (1, 8, 15), (8, 1, 15), (1, 2, 7)):
ref, got = draws(before, before, STEPS), draws(before, after, step)
print(f"DP {before} -> {after} at step {step}: {sum((got - ref).values())} duplicated, "
f"{sum((ref - got).values())} dropped vs. the uninterrupted run")Output:
DP 1 -> 1 at step 15: 0 duplicated, 0 dropped vs. the uninterrupted run
DP 1 -> 8 at step 15: 19 duplicated, 19 dropped vs. the uninterrupted run
DP 8 -> 1 at step 15: 19 duplicated, 19 dropped vs. the uninterrupted run
DP 1 -> 2 at step 7: 3 duplicated, 3 dropped vs. the uninterrupted runExpected behavior
A run resumed at a different DP size consumes the same samples as the uninterrupted run. Alternatively, the resume should fail loudly if it cannot.
Additional context
- Changes between DP sizes with the same
total % (mbs × dp)are unaffected. With N=250 that is DP 2 ↔ 4. --dataloader-type singleis unaffected.- Possible directions, for the maintainers to choose from:
- Store the epoch index and the in-epoch offset in the checkpoint, instead of re-deriving them from
consumed_sampleswith a DP-dependent epoch length. - Use an epoch length that does not depend on the DP size, together with a matching rule for the dropped tail.
- Detect a resume where
total % (mbs × dp)changes and warn or refuse.
- Store the epoch index and the in-epoch offset in the checkpoint, instead of re-deriving them from
Source: NVIDIA/Megatron-LM