kempnerforge.data.sampler¶
Distributed sampler with deterministic shuffling and skip-ahead.
- Correctly partitions data across data-parallel ranks with:
Epoch-based re-shuffling with deterministic seeds
Skip-ahead for exact resumption after checkpoint load
Handling of uneven dataset sizes (drop last partial batch)
Classes
Deterministic distributed sampler with skip-ahead support. |
|
Weighted sampler over a |
- class kempnerforge.data.sampler.DistributedSampler[source]¶
Bases:
~torch.utils.data.Sampler.Deterministic distributed sampler with skip-ahead support.
Partitions dataset indices across data-parallel ranks. Each rank sees a unique, non-overlapping subset of the data. Every epoch has one global order, the same at any
num_replicas, and rankrreads positionsr,r + num_replicas, … of it, so a resume position counted in that order (set_position) carries over to another number of ranks.- Parameters:
dataset – Dataset to sample from.
num_replicas – Number of data-parallel ranks (default: world_size).
rank – Current rank (default: from dist).
shuffle – Whether to shuffle indices.
seed – Base random seed for deterministic shuffling.
drop_last – Drop samples that don’t divide evenly across ranks.
- set_epoch(epoch)[source]¶
Set the epoch for deterministic re-shuffling.
- Parameters:
epoch (int)
- Return type:
None
- set_skip(skip)[source]¶
Set number of samples to skip (for resumption after checkpoint).
- Parameters:
skip (int)
- Return type:
None
- set_position(position)[source]¶
Start the next pass at sample
positionof the epoch’s global order.positioncounts the samples all ranks have read together, so it means the same at anynum_replicas. Rankrstarts at its first sample at or afterposition, and every rank yieldssamples_after(position)samples, so the ranks stay in step and none reads a sample beforepositionagain. Applies to the next pass only.- Parameters:
position (int)
- Return type:
None
- samples_after(position)[source]¶
How many samples each rank yields in a pass that starts at
position.
- class kempnerforge.data.sampler.MixtureSampler[source]¶
Bases:
~torch.utils.data.Sampler.Weighted sampler over a
MixtureDataset.Each sub-dataset’s indices are partitioned across ranks (like
DistributedSampler). Theweightscontrol what fraction of the epoch is drawn from each dataset — datasets with higher weight are oversampled. Each rank draws and shuffles its own list, so a resume position is a count of this rank’s samples and carries over only to the samenum_replicas.Weights set by
update_weightsapply from the next pass. A pass that starts inside the epoch, at a skip set after the change, draws the rest of the epoch with the new weights, from the samples this rank has not drawn in the epoch first, so a dataset that no weighting of the epoch oversamples draws no sample twice in the epoch. The state dict records the weights the epoch drew with from its start and from each such position, so a resume draws what the uninterrupted run draws, and weights that differ at a resume apply from the saved position.- Parameters:
cumulative_sizes –
[0, len(ds0), len(ds0)+len(ds1), ...]fromMixtureDataset.cumulative_sizes.weights – Per-dataset sampling weights (normalized internally).
num_replicas – Number of data-parallel ranks.
rank – Current rank.
shuffle – Whether to shuffle indices.
seed – Base random seed.
drop_last – Drop samples that don’t divide evenly across ranks.
temperature – Weight temperature (1.0 = as-is, >1 → more uniform).
- __init__(cumulative_sizes, weights, num_replicas=None, rank=None, shuffle=True, seed=0, drop_last=True, temperature=1.0)[source]¶
- set_epoch(epoch)[source]¶
Set epoch for deterministic re-shuffling.
- Parameters:
epoch (int)
- Return type:
None
- set_skip(skip)[source]¶
Set number of samples to skip (for resumption after checkpoint).
- Parameters:
skip (int)
- Return type:
None
- state_dict()[source]¶
Return checkpoint state.
Keys:
epoch,seed,num_replicas,rank, and, from the epoch’s first pass or a loaded record of it on,epoch_weights: the sampling probabilities the epoch drew with, as[position, probabilities]pairs from position 0.- Return type:
- update_weights(weights, temperature=1.0)[source]¶
Update sampling weights for phase transitions.
Recomputes internal probabilities and per-dataset target counts. Takes effect on the next
__iter__()call: a pass at the start of an epoch draws the epoch with the new weights, and a pass that starts inside the epoch draws the rest of it with them (see the class).
- load_state_dict(state)[source]¶
Restore from checkpoint: the epoch and the weights it recorded.
A record of
epoch_weightsfor another number of datasets is dropped with a warning; the epoch then continues as one drawn with the current weights from its start, as a state without the record does.- Parameters:
state (dict)
- Return type:
None