Data Loading
About
A DataProvider is the factory that you supply to the train or inference loop, like ModelProvider or OptimizerProvider. Given the run context, it builds a MicrobatchPackStream. This is a Stateful iterable that yields microbatch packs and reports its length through the total_steps property.
Concepts
- A pack holds the data of one step: a sequence of microbatches.
len(pack)is the number of microbatches in that step (the gradient accumulation factor). It can vary from step to step. The loop copies packs to the device ahead of their steps (see Data Prefetching). total_stepsis the number of steps the stream yields. It isNonewhen the length is not known ahead of time, e.g. for streaming or data-dependent batching.JobScheduletakes the job duration fromJobScheduleConfig.total_stepsif it is set. It must not exceed the stream length. Otherwise,JobScheduleuses thetotal_stepsof the stream. The loop runs exactly that many steps. It cuts a longer stream short and raises an error if the stream ends earlier.- The stream is the only checkpoint boundary for the data. It saves and restores its own position per data-parallel rank, so the job resumes exactly. Prefetching iterates the stream on a background thread and can call
state_dict()after every pack. So keepstate_dict()cheap, and do not return objects that later iteration changes.
You can use the shipped AutoDataProvider for the common case, or write your own provider for full control.
Using AutoDataProvider
AutoDataProvider builds the default data stack for you. You supply the two parts that cannot be serialized: a dataset_factory and a collator. An AutoDataConfig holds the serializable settings: global_batch_size, microbatch, shard_indexing_mode, drop_last and loader settings. The loader settings include shuffle, num_workers, pin_memory and prefetch_factor. A PinMemoryMicrobatchPackStream does the pinning. Unlike the DataLoader option, it also pins tensors nested in dataclasses.
AutoDataProvider shards the dataset across data-parallel ranks for you. It also builds the loader, derives the gradient accumulation factor and returns the stream. So your factory returns the unsharded dataset.
- The
dataset_factoryreceives theInitializeDataProviderContext. Itsdist_contextcan guard data preparation. For example,context.dist_context.main_process_first()lets rank 0 fill the cache before the other ranks read it. Itsseed_scopegives the seeds of a dataset that draws random numbers. - The
collatorcollates a list of samples into one microbatch.
microbatch selects how the loader forms a microbatch:
samples: A microbatch holdssizesamples.global_batch_sizecounts samples.concat: A microbatch is one row of consecutive samples, at mostmax_lengthlong. The provider groups consecutive samples into rows before it shards them, so all ranks build the same rows.global_batch_sizecounts rows. The dataset must implementDatasetImplementingItemLengthProtocol. The collator receives a list with oneSampleRowand concatenates its samples, e.g.concat_collate. See Sample Concatenation.
The loader takes its own generator, seeded from shard_seed_scope of the context. It shuffles with this generator if shuffle is set, and seeds its workers from it. So all ranks of one data-parallel shard read the same samples in the same order. See Random Numbers in the Loop.
The batch sizes are known, so the stream reports its total_steps. JobSchedule then derives the job duration without JobScheduleConfig.total_steps.
Writing a Custom DataProvider
A custom provider builds the same default stack by hand. It has two layers:
- A loader: any
DataLoaderProtocol(fromd9d.data). It is aStateful,Sizediterable of single collated microbatches. torchdata'sStatefulDataLoadersatisfies it directly. - A packer:
FixedCountMicrobatchPacker(microbatches_per_step=k)groupskmicrobatches into each pack, which gives gradient accumulation.drop_lastcontrols whether a short trailing pack is dropped. Training drops it, evaluation keeps it. The helpernum_microbatches_for_global_batchderivesk.
Wrap the packer in a PinMemoryMicrobatchPackStream to pin the packs, so the loop copies them to the device asynchronously. The packer and the pinning stream live in d9d.data.batch_iterator.
Unlike with AutoDataProvider, you own the data-parallel sharding. Shard the dataset yourself before you build the loader, e.g. with shard_dataset_data_parallel. See Data.
You also own the seeds. Take seeds for code before sharding from context.seed_scope, which is equal on all ranks. Give the loader a generator seeded from context.shard_seed_scope, which is equal on the ranks of one data-parallel shard. See Random Numbers in the Loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
API Reference
d9d.loop.control.data_provider
Interface for providing training and inference data.
DataProvider
Bases: Protocol
Protocol for defining how the data pipeline is built.
A DataProvider is the factory the user supplies to the train/eval loop, exactly like
ModelProvider or OptimizerProvider. Given the run context, it composes and returns a
MicrobatchPackStream.
The user is responsible for sharding the dataset across data-parallel ranks (e.g. with
d9d.data.shard_dataset_data_parallel) inside the provider.
The iteration over the data can run on a prefetch thread, so it must not draw from the global generators. It must take its seeds from the seed scopes of the context instead.
__call__(context)
Builds the microbatch pack stream for the job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
InitializeDataProviderContext
|
Context for this operation. |
required |
Returns:
| Type | Description |
|---|---|
MicrobatchPackStream
|
The microbatch pack stream the loop will drive. |
InitializeDataProviderContext
dataclass
Context data required to initialize a data provider.
Attributes:
| Name | Type | Description |
|---|---|---|
dist_context |
DistributedContext
|
The distributed context containing rank and world size information. |
seed_scope |
SeedScope
|
The seed scope for code before sharding. It is equal on all ranks. |
shard_seed_scope |
SeedScope
|
The seed scope for code after sharding, e.g. the generator of the data loader. It is equal on the ranks of one data-parallel shard. |
d9d.loop.auto.auto_data
Default data provider built from a configuration.
AutoMicrobatchConfig = Annotated[SampleMicrobatchConfig | ConcatMicrobatchConfig, Field(discriminator='name')]
module-attribute
Discriminated union of the supported ways to form a microbatch.
DatasetFactory = Callable[[InitializeDataProviderContext], Dataset]
module-attribute
A callable that builds the (unsharded) dataset, given the context of the data provider.
The context can guard data preparation (e.g. with context.dist_context.main_process_first()). A dataset that
draws random numbers takes its seeds from context.seed_scope.
AutoDataConfig
Bases: BaseModel
Configuration for the default data pipeline.
Attributes:
| Name | Type | Description |
|---|---|---|
global_batch_size |
int
|
The total effective batch size across all replicas and accumulation, in microbatch items: samples, or rows of concatenated samples. |
microbatch |
AutoMicrobatchConfig
|
How the loader forms a microbatch. |
shard_indexing_mode |
ShardIndexingMode
|
The dataset sharding strategy. |
shuffle |
bool
|
Whether to reshuffle the data every epoch. |
drop_last |
bool
|
Whether to drop the trailing incomplete microbatch and pack. Set it to |
num_workers |
int
|
The number of subprocesses to use for data loading. |
pin_memory |
bool
|
Whether to copy the packs into pinned host memory before returning them. |
persistent_workers |
bool
|
Whether to keep worker processes alive between epochs. |
prefetch_factor |
int | None
|
The number of batches each worker prefetches ahead. |
timeout |
float
|
The timeout in seconds for collecting a batch from workers. |
AutoDataProvider
Bases: DataProvider
Data provider for the default data stack, which shards the dataset, loads microbatches and packs them per step.
With ConcatMicrobatchConfig, the provider concatenates the samples into rows before it shards them.
The dataset is sharded across data-parallel ranks and loaded by a stateful loader at the microbatch size.
The loader shuffles with its own generator, seeded from shard_seed_scope of the context.
The number of microbatches per step follows from the global batch size. A FixedCountMicrobatchPacker
groups the microbatches, and a PinMemoryMicrobatchPackStream pins the packs if enabled.
For a different stack, write your own DataProvider.
__init__(dataset_factory, collator, config)
Constructs the AutoDataProvider object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_factory
|
DatasetFactory
|
Builds the unsharded dataset given the context of the data provider. |
required |
collator
|
CollateFn
|
Collates individual samples into a microbatch. |
required |
config
|
AutoDataConfig
|
The serializable settings for the default stack. |
required |
ConcatMicrobatchConfig
Bases: BaseModel
Configuration for microbatches that are one row of concatenated samples.
The dataset must implement DatasetImplementingItemLengthProtocol. The collator receives a list with one
SampleRow and concatenates its samples, e.g. concat_collate. See
SampleConcatDataset.from_item_lengths().
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Literal['concat']
|
Discriminator field. Always |
max_length |
int
|
The maximum length of a row. |
d9d.data.batch_iterator
Composable building blocks for the batch iterator stack.
DevicePackStream
Abstract base class for streams that hand microbatch packs to the loop on the device.
Its state is the data state stored in checkpoints.
__iter__()
abstractmethod
Yields the stream's packs on the device, ready for use on the current CUDA stream.
Closing the generator stops any work done ahead and releases the packs prepared for later steps.
DirectDevicePackStream
Bases: DevicePackStream
Device pack stream that copies each pack to the device on the current CUDA stream when it is handed out.
__init__(stream, device)
Constructs the DirectDevicePackStream object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
MicrobatchPackStream
|
The stream of host-side packs. |
required |
device
|
Device
|
The device to copy the packs to. |
required |
FixedCountMicrobatchPacker
Bases: MicrobatchPackStream
Microbatch pack stream that groups the microbatches of a loader into packs of microbatches_per_step.
It is the default MicrobatchPackStream. A short trailing pack (drop_last=False) is consistent across
ranks only when every rank sees the same number of microbatches, e.g. when the dataset was sharded with
pad_to_equal_size_across_shards.
Its total_steps is derived from the loader's length, and it delegates its state to the loader
(the checkpoint boundary).
total_steps
property
The number of packs (steps) this packer yields.
It includes a trailing short pack unless drop_last is set.
__init__(loader, microbatches_per_step, drop_last=True)
Constructs the FixedCountMicrobatchPacker object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
DataLoaderProtocol
|
The microbatch stream to group. |
required |
microbatches_per_step
|
int
|
The number of microbatches in each pack. |
required |
drop_last
|
bool
|
Whether to drop the trailing incomplete pack instead of yielding it short. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
__iter__()
Iterates the loader, grouping microbatches into packs.
Yields:
| Type | Description |
|---|---|
MicrobatchPack
|
A full pack, or a shorter trailing pack when |
PinMemoryMicrobatchPackStream
Bases: MicrobatchPackStream
Microbatch pack stream wrapper that copies every tensor of each pack into pinned host memory.
Pinned memory lets the loop copy packs to the device asynchronously. Unlike the pin_memory option of
torch.utils.data.DataLoader, the traversal uses d9d.pytree, so tensors nested in dataclasses are
pinned as well. Pinning runs in the iterating thread.
Its total_steps and state are those of the wrapped stream.
total_steps
property
The number of packs (steps) the wrapped stream yields, or None if it is unknown.
__init__(inner)
Constructs the PinMemoryMicrobatchPackStream object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
MicrobatchPackStream
|
The wrapped stream that owns the data and its position. |
required |
__iter__()
Iterates the wrapped stream, pinning each pack.
Yields:
| Type | Description |
|---|---|
MicrobatchPack
|
The next pack with all of its tensors in pinned host memory. |
PrefetchingDevicePackStream
Bases: DevicePackStream
Device pack stream that copies packs to the device on a side CUDA stream ahead of the steps that use them.
A background thread pulls the packs from the stream, so their loading, pinning and copy launches stay off the
loop's critical path. Prefetching runs the stream's state ahead of the job, so state_dict returns the stream
state as of the last handed out pack. This is the state of the last step, since checkpoints are only taken
between steps.
__init__(stream, device, prefetch_factor)
Constructs the PrefetchingDevicePackStream object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
MicrobatchPackStream
|
The stream of host-side packs. |
required |
device
|
Device
|
The device to copy the packs to. |
required |
prefetch_factor
|
int
|
The number of packs copied ahead. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
build_device_pack_stream(stream, device, prefetch_factor)
Builds the device pack stream for the given prefetch factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
MicrobatchPackStream
|
The stream of host-side packs. |
required |
device
|
Device
|
The device to copy the packs to. |
required |
prefetch_factor
|
int
|
The number of packs copied ahead; |
required |
Returns:
| Type | Description |
|---|---|
DevicePackStream
|
A prefetching stream for a positive |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
num_microbatches_for_global_batch(dist_context, global_batch_size, microbatch_size)
Computes the number of microbatches per step required to reach a target global batch size.
The global batch is spread across the data-parallel ranks, and each rank processes microbatch_size samples
per microbatch. So each rank processes global_batch_size / (dp_size * microbatch_size) microbatches per
optimizer step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_context
|
DistributedContext
|
The distributed context. |
required |
global_batch_size
|
int
|
The total effective batch size across all replicas and accumulation. |
required |
microbatch_size
|
int
|
The number of samples in a single microbatch on a single rank. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of microbatches per step (the gradient-accumulation factor). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |