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

DistributedSampler

Deterministic distributed sampler with skip-ahead support.

MixtureSampler

Weighted sampler over a MixtureDataset.

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 rank r reads positions r, 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.

__init__(dataset, num_replicas=None, rank=None, shuffle=True, seed=0, drop_last=True)[source]
Parameters:
Return type:

None

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 position of the epoch’s global order.

position counts the samples all ranks have read together, so it means the same at any num_replicas. Rank r starts at its first sample at or after position, and every rank yields samples_after(position) samples, so the ranks stay in step and none reads a sample before position again. 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.

Parameters:

position (int)

Return type:

int

state_dict()[source]

Return checkpoint state. Keys: epoch, seed, num_replicas, rank.

Return type:

dict

load_state_dict(state)[source]

Restore from checkpoint. Only epoch is restored; rank info is local.

Parameters:

state (dict)

Return type:

None

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). The weights control 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 same num_replicas.

Weights set by update_weights apply 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), ...] from MixtureDataset.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]
Parameters:
Return type:

None

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:

dict

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).

Parameters:
Return type:

None

load_state_dict(state)[source]

Restore from checkpoint: the epoch and the weights it recorded.

A record of epoch_weights for 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