Sampler¶
Two sampler classes in
kempnerforge/data/sampler.py:
DistributedSampler— rank-partitioned, deterministic shuffle, skip-ahead for resume. The default.MixtureSampler— weighted sampling over aMixtureDataset, partitioned per rank.
Both expose set_epoch(epoch), set_skip(samples), and state_dict()
/ load_state_dict() — the contract that StatefulDataLoader
assumes.
DistributedSampler also has set_position(position), which resumes at
a position in the epoch’s global order, the same on any number of ranks
(see Resume position).
DistributedSampler¶
Indices are partitioned across DP ranks with a stride pattern, not a
contiguous block. Rank r of N ranks gets indices
perm[r], perm[r+N], perm[r+2N], ...:
# kempnerforge/data/sampler.py — DistributedSampler.__iter__
if self.shuffle:
g = torch.Generator()
g.manual_seed(self.seed + self._epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = list(range(len(self.dataset)))
if self.drop_last:
indices = indices[: self.total_size] # total = num_samples * num_replicas
else:
padding = self.total_size - len(indices)
indices += indices[:padding]
# Stride partition
indices = indices[self.rank : self.total_size : self.num_replicas]
Why stride, not contiguous? With stride, changing num_replicas
between runs does change which rank sees which samples — but every
sample is still seen exactly once per epoch and the seed-derived
shuffle is identical. Contiguous block partitions are more sensitive
to dataset-size / rank-count interactions.
Shuffle seeding¶
The shuffle is keyed on seed + epoch, where seed is
TrainConfig.seed (default 42) and epoch is set via
set_epoch(self._epoch) at the top of every StatefulDataLoader.__iter__.
Two runs with the same seed and the same number of ranks produce
identical shuffle orders every epoch.
Drop-last vs pad-wrap¶
if drop_last:
self.num_samples = total // num_replicas
self.total_size = self.num_samples * num_replicas # shorter than total
else:
self.num_samples = math.ceil(total / num_replicas)
self.total_size = self.num_samples * num_replicas # padded by wrap-around
KempnerForge defaults to drop_last=True in every call site. Losing
a handful of samples per epoch at world sizes of 16-64 is a
rounding-error effect at pretraining scale; the upside is every rank
does exactly the same amount of work, every step.
Skip-ahead¶
# At the end of __iter__
if self._skip > 0:
indices = indices[self._skip :]
self._skip = 0 # one-shot — resets after being applied
set_skip(k) tells the sampler “drop the first k samples from
this epoch’s index list”. On resume StatefulDataLoader passes a
MixtureSampler this rank’s consumed sample count this way; with a
DistributedSampler it calls set_position instead (see
Resume position). The reset after application is
important: on the next epoch, no skip is applied, and iteration
starts from index 0.
Resume position¶
set_skip(k) counts this rank’s samples, so it is right only on the
data-parallel size that saved it. set_position(G) counts the samples of
the epoch’s global order that all ranks consumed together, which means
the same on any num_replicas. With G = q * num_replicas + m, rank r
skips its q + (1 if r < m else 0) samples that come before position G,
and every rank yields samples_after(G) = num_samples - q - (1 if m else 0)
samples, so no rank runs out of batches early. Like the skip, the position
applies to the next pass only. StatefulDataLoader uses it for every
resume with a DistributedSampler; see
Stateful dataloader § Resuming on another data-parallel size.
MixtureSampler¶
Used when data.datasets contains multiple sources. The pipeline
splits between construction and per-epoch iteration.
In __init__ (and re-run by update_weights):
Probabilities —
weightsnormalized, with optional temperature scaling — see Mixing and annealing.Per-rank budget — sum of
size // num_replicasacross all sub-datasets (orceilifdrop_last=False).Target counts —
round(p * total_per_rank)per dataset, with a rounding-fix pass that adds or subtracts 1 from datasets in probability order until the total matches exactly.
Each __iter__() then does:
Shuffled indexes — per sub-dataset,
torch.randperm(size)with the sameseed + epochtrick, then the rank’s stride slice.Oversample if needed — if the target exceeds per-rank available indices, the index list is repeated until it’s long enough.
Global offset — each sub-dataset’s local index is added to its offset so the final list indexes into the parent
MixtureDataset.Final shuffle — the assembled index list is shuffled once more so samples from different sources are interleaved, not grouped.
Weights changed inside the epoch: when the weights changed after the epoch began and the pass starts at a skip inside the epoch, the list keeps its first
skipentries and draws the rest under the new weights, from the samples not drawn in the epoch first. A second generator, seeded from the seed and the epoch alone, splits the datasets the epoch’s start did not draw from across ranks and shuffles the rest, so every rank splits them the same way. Every earlier change of the epoch is replayed the same way, in order (see Mixing and annealing § Phase changes inside an epoch).
update_weights(weights, temperature)¶
Called by the training loop at phase boundaries. Recomputes
probabilities and _target_counts; takes effect on the next
__iter__() call, which draws a new epoch with them, or the rest of
the current one when it starts inside it (step 8). The sampler’s
internal seed + epoch behavior is
unchanged — weight changes don’t re-seed.
Skip-ahead behavior¶
Same as DistributedSampler: set_skip(k) drops the first k
entries of the assembled (final, shuffled) index list. The state dict
records the weights the epoch drew with from its start and from each
position where they changed, so a resume rebuilds the same list, phase
changes before the saved position included, and weights that differ at
the resume apply from the saved position as a change of their own. Each
rank’s list depends on num_replicas, so there is no global position to
carry over: StatefulDataLoader refuses to resume a mixture position on
another data-parallel size.
What’s in state_dict¶
Both samplers return the same keys:
{
"epoch": self._epoch,
"seed": self.seed,
"num_replicas": self.num_replicas,
"rank": self.rank,
}
MixtureSampler adds epoch_weights from its epoch’s first pass on:
the sampling probabilities the epoch drew with, as
[position, probabilities] pairs from position 0, such as
[[0, [0.5, 0.5]]] for an epoch without a change and
[[0, [0.5, 0.5]], [1000, [0.6, 0.4]]] after one. Its load_state_dict
restores that record with epoch, and drops it with a warning when it
covers another number of datasets. A state without it draws the epoch
with the current weights from its start.
Apart from that record, load_state_dict restores only epoch.
num_replicas and rank are local to this process and are re-derived
from the current world size, not transplanted from the save;
StatefulDataLoader compares the saved num_replicas with the current
one to decide whether a per-rank position still applies. seed comes
from TrainConfig;
StatefulDataLoader logs a warning when the saved seed or the dataset
length differs from the run’s, since its position counts samples of the
saved order.
Eval sampler¶
Evaluation uses DistributedSampler(eval_dataset, shuffle=False):
# training/data_pipeline.py::build_eval_dataloader
eval_sampler = DistributedSampler(
eval_dataset, num_replicas=dp_size, rank=dp_rank,
shuffle=False, seed=tc.seed,
)
shuffle=False makes the order deterministic (no permutation),
which is what you want for reproducible eval loss numbers.
See also¶
Stateful dataloader — the caller that drives
set_epoch,set_skip, and the state dict.Mixing and annealing —
update_weightsand how phase transitions interact with the sampler.Checkpointing § Train state — the sampler state is captured inside the dataloader state dict.