nvalchemi.data.datapipes.MultiDatasetBatchSampler#
- class nvalchemi.data.datapipes.MultiDatasetBatchSampler(dataset, *, batch_size, weights=None, samples_per_dataset=None, num_batches=None, epoch_policy='dataset_size', replacement=True, shuffle=True, generator=None, num_replicas=None, rank=None, distributed_manager=None, seed=0, drop_last=False)[source]#
Sample full global-index batches from a
MultiDataset.MultiDatasetBatchSampleryields whole batches – each alistof global indices (it is atorch.utils.data.Sampleroflist[int]) – and is passed to aDataLoaderasbatch_sampler=(mutually exclusive withsampler,shuffle, and the loader’sbatch_size). UnlikeMultiDatasetSampler, it fixes the composition of every batch: each batch holds a deterministic number of samples from each child dataset, set either bysamples_per_dataset(explicit integer counts, or floats read as relative rates) or byweights(rates that splitbatch_sizeacross children). Use this class for ratio- or curriculum-controlled multi-source training where every optimizer step must see a guaranteed mixture ratio.Epoch length (
num_batches) can be given directly or derived fromepoch_policy:"dataset_size"sizes the epoch by the combined length,"min_size"stops when the smallest contributing child is exhausted, and"max_size"runs until the largest is exhausted (oversampling smaller children whenreplacement=True). LikeMultiDatasetSampler, it shards acrossnum_replicasranks and honorsset_epoch(), mirroringtorch.utils.data.DistributedSampler.- Parameters:
dataset (MultiDataset) – Dataset wrapper that defines child dataset offsets.
batch_size (int) – Number of samples in each emitted batch.
weights (Sequence[float] | None, default=None) – Per-child rates used to allocate
batch_sizeslots.Noneuses child lengths, matching proportional sampling from the global index space.samples_per_dataset (Sequence[int | float] | None, default=None) – Per-child batch allocation. Integer entries are exact sample counts per batch. If any entry is a float, the full sequence is interpreted as relative per-dataset rates and allocated across
batch_size. Mutually exclusive withweights.num_batches (int | None, default=None) – Number of batches per epoch. For replacement sampling, the default is
ceil(len(dataset) / batch_size). Without replacement, the default is the number of complete batches supported by the smallest requested child allocation.epoch_policy ({"dataset_size", "min_size", "max_size"}, default="dataset_size") – Policy used to compute
num_batcheswhen it is not provided."dataset_size"simply returns the combined dataset length divided by the batch size whenreplacement=True, otherwisemin_size."min_size"stops when the smallest contributing dataset would be exhausted."max_size"runs until the largest contributing dataset would be exhausted, oversampling smaller datasets whenreplacement=True.replacement (bool, default=True) – Whether local samples may repeat within an epoch.
shuffle (bool, default=True) – Randomize local sample order and sample order within each batch.
generator (torch.Generator | None, default=None) – Optional random generator for reproducible sampling.
num_replicas (int | None, default=None) – Number of distributed ranks.
Noneuses initializeddistributed_manager.world_sizeor defaults to1.rank (int | None, default=None) – Rank for this sampler.
Noneuses initializeddistributed_manager.rankor defaults to0.distributed_manager (DistributedManager | None, default=None) – Optional distributed manager used to infer rank and world size.
seed (int, default=0) – Base seed used for deterministic shuffling across epochs when
generatorisNone.drop_last (bool, default=False) – Drop tail batches to make the epoch evenly divisible across ranks.
Examples
Guarantee three samples from the first child and one from the second in every batch of four:
>>> from nvalchemi.data.datapipes import DataLoader >>> from nvalchemi.data.datapipes.samplers import MultiDatasetBatchSampler >>> sampler = MultiDatasetBatchSampler( ... multi, batch_size=4, samples_per_dataset=(3, 1) ... ) >>> loader = DataLoader(multi, batch_sampler=sampler)
See also
MultiDatasetSamplerEmit single indices for stochastic per-sample mixing.
MultiDatasetThe concatenated dataset these global indices address.
- classmethod balanced(dataset, *, batch_size, num_batches=None, epoch_policy='dataset_size', replacement=True, shuffle=True, generator=None, num_replicas=None, rank=None, distributed_manager=None, seed=0, drop_last=False)[source]#
Create a batch sampler with equal dataset-level sampling rates.
- Parameters:
dataset (MultiDataset) – Dataset wrapper that defines child dataset offsets.
batch_size (int) – Number of samples in each emitted batch.
num_batches (int | None, default=None) – Number of batches per epoch.
epoch_policy ({"dataset_size", "min_size", "max_size"}, default="dataset_size") – Policy used to compute
num_batcheswhen it is not provided.replacement (bool, default=True) – Whether local samples may repeat within an epoch.
shuffle (bool, default=True) – Randomize local sample order and sample order within each batch.
generator (torch.Generator | None, default=None) – Optional random generator for reproducible sampling.
num_replicas (int | None, default=None) – Number of distributed ranks.
rank (int | None, default=None) – Rank for this sampler.
distributed_manager (DistributedManager | None, default=None) – Optional distributed manager used to infer rank and world size.
seed (int, default=0) – Base seed used for deterministic shuffling across epochs.
drop_last (bool, default=False) – Drop tail batches to make the epoch evenly divisible across ranks.
- Returns:
Batch sampler with one equal relative weight per child dataset.
- Return type:
Self