composer.datasets.dataloader#

composer.datasets.dataloader

Functions

dataclass

Returns the same class as was passed in, with dunder methods added based on the fields defined in the class.

unwrap_data_loader

Recursively unwraps a dataloader if it is of type WrappedDataLoader.

Classes

DataLoader

Protocol for custom DataLoaders compatible with torch.utils.data.DataLoader.

WrappedDataLoader

composer.datasets.dataloader.WrappedDataLoader

Hparams

These classes are used with yahp for YAML-based configuration.

DataloaderHparams

Hyperparameters to initialize a Dataloader.

Attributes

  • Any

  • Batch

  • Callable

  • Dataset

  • Iterator

  • Optional

  • annotations

  • log

class composer.datasets.dataloader.DataloaderHparams(num_workers=8, prefetch_factor=2, persistent_workers=True, pin_memory=True, timeout=0)[source]#

Bases: yahp.hparams.Hparams

Hyperparameters to initialize a Dataloader.

Parameters
  • num_workers (int, optional) โ€“ Number of CPU workers to use per device to fetch data. Set to 0 to use the main training thread for dataloading. While zero workers can be useful for debugging, it should not be used for performance reasons. (default: 8)

  • prefetch_factor (int, optional) โ€“ Number of samples loaded in advance by each worker. For example, 2 means there will be a total of 2 * num_workers samples prefetched across all workers. If num_workers = 0, then the prefetch_factor must be left at the default value. (default: 2)

  • persistent_workers (bool) โ€“ Whether to reuse dataloader workers across epochs. If num_workers is 0, then this field must be False. (default: True)

  • pin_memory (bool, optional) โ€“ Whether or not to copy Tensors into CUDA pinned memory before returning them. If num_workers = 0, then the pin_memory must be False. (default: True)

  • timeout (float) โ€“ Timeout, in seconds, for collecting a batch from workers. Set to 0 for no timeout. (default: 0)

initialize_object(dataset, *, batch_size, sampler, drop_last, collate_fn=None, worker_init_fn=None)[source]#

Create a dataloader.

Parameters
  • dataset (Dataset) โ€“ The dataset.

  • batch_size (int) โ€“ The per-device batch size.

  • sampler (Sampler[int] or None) โ€“ The sampler to use for the dataloader.

  • drop_last (bool) โ€“ Whether to drop the last batch if the number of samples is not evenly divisible by the batch size.

  • collate_fn (callable, optional) โ€“ Custom collate function. Defaults to None.

  • worker_init_fn (callable, optional) โ€“ Custom worker init function. Defaults to None.

Returns

DataLoader โ€“ The dataloader.

composer.datasets.dataloader.unwrap_data_loader(dataloader)[source]#

Recursively unwraps a dataloader if it is of type WrappedDataLoader.

Parameters

dataloader (DataLoader) โ€“ The dataloader to unwrap

Returns

DataLoader โ€“ The underlying dataloader