nvalchemi.data.datapipes.MultiDatasetSampler#
- class nvalchemi.data.datapipes.MultiDatasetSampler(dataset, *, weights=None, num_samples=None, replacement=True, shuffle=True, generator=None, num_replicas=None, rank=None, distributed_manager=None, seed=0, drop_last=False)[source]#
Sample global indices from a
MultiDatasetat dataset-level rates.MultiDatasetSampleryields individual global sample indices (it is atorch.utils.data.Samplerofint), choosing which child dataset each sample is drawn from according to per-datasetweights– defaulting to the child lengths, which reproduces proportional sampling from the concatenated index space. Pass it to aDataLoaderassampler=; the loader then groups the emitted indices into batches ofbatch_size, so batch composition is stochastic. UseMultiDatasetBatchSamplerinstead when each batch must contain a guaranteed number of samples from each child.The sampler is distributed-aware: it shards the epoch across
num_replicasranks (inferred from an initializeddistributed_managerwhen one is supplied), andset_epoch()reseeds shuffling per epoch for correct cross-rank ordering – the same contract astorch.utils.data.DistributedSampler.replacementcontrols whether a child’s samples may repeat within an epoch; withreplacement=Falsethe requested per-child counts may not exceed the child sizes.- Parameters:
dataset (MultiDataset) – Dataset wrapper that defines child dataset offsets.
weights (Sequence[float] | None, default=None) – Per-child dataset sampling rates.
Noneuses child lengths, matching proportional sampling from the concatenated global index space.num_samples (int | None, default=None) – Number of global indices emitted per epoch.
Noneemitslen(dataset)samples.replacement (bool, default=True) – Whether local samples may repeat within an epoch.
shuffle (bool, default=True) – Randomize dataset choices and local sample order.
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 samples to make the epoch evenly divisible across ranks.
Examples
Oversample a small child dataset by weighting it above its natural share:
>>> from nvalchemi.data.datapipes import DataLoader >>> from nvalchemi.data.datapipes.samplers import MultiDatasetSampler >>> sampler = MultiDatasetSampler(multi, weights=(1.0, 3.0)) >>> loader = DataLoader(multi, batch_size=8, sampler=sampler)
See also
MultiDatasetBatchSamplerFix the per-child composition of every batch.
MultiDatasetThe concatenated dataset these global indices address.