Foundations to Frontier
Tests
C. Programming, Tools and HPC › C6 Distributed Training

Large-Scale Data Loading

At scale, the GPU is often waiting for data: datasets of terabytes live on parallel file systems or object storage, samples must be decoded and augmented on CPUs, and every rank and worker must read a different, well-shuffled subset each epoch. This topic covers the input pipeline (storage, reading, decoding, augmentation, batching, transfer), DataLoader workers, pinned memory and prefetching, sharded formats for streaming (WebDataset tar shards, TFRecord, Mosaic StreamingDataset, Parquet, HDF5/Zarr chunks), splitting shards across ranks and workers, approximate shuffling with shuffle buffers, deterministic and resumable iteration, and sizing the pipeline so the GPU never starves. In the exercises you build a sharded, shuffled, resumable loader and check that every sample is read exactly once per epoch; then you show how class-sorted shards and too-small shuffle buffers hurt one-pass training, and how buffer size and shard interleaving fix it.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Feeding a very hungry machine

A big GPU cluster eats data the way a factory line eats parts: if the parts arrive late, the whole line stands still. Data for training often sits in thousands of big files far away, has to be unpacked and prepared, and must be dealt out so every machine gets a different, well-mixed handful. Good data loading keeps the parts arriving on time and in a random-enough order.

Shuffling a sharded stream
consecutive samples with the same class: 68% (perfect shuffle: 25%) · buffer = 0.3 shards' worth
Samples are read left to right, row by row; colours are the four classes, each shard (30 samples) holding one class. A buffer smaller than a shard only blurs the edges of runs; mixing needs a buffer spanning several shards, or reading several shards at once. Writing shards with already-mixed data avoids the problem altogether.
Why engineers care

Earth-observation archives, telemetry from fleets of spacecraft and robots, and simulation outputs are petabyte-scale and stored in large files written by source, orbit or site. Training on them is limited by I/O and by how well the stream is mixed: data written per site and read in order teaches a model about the last site it saw.

📝 Quick check (Level 0)
0/3 answered
  1. 1. If data loading is slower than training…
  2. 2. Why mix the order of training samples?
  3. 3. In the demo, what helps more when shards hold one class each?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The input pipeline

storage → read (sequential, large requests) → decode (JPEG, compressed arrays) → augment → collate into batches → pin and copy to the GPU, overlapped with computation:

loader = DataLoader(dataset, batch_size=256, num_workers=16,    # CPU processes decoding in parallel
                    pin_memory=True,                             # page-locked memory for fast async copies
                    prefetch_factor=4, persistent_workers=True,  # keep batches ready; don't respawn workers
                    sampler=DistributedSampler(dataset))         # disjoint data per rank (C6.1)
for x, y in loader:
    x, y = x.cuda(non_blocking=True), y.cuda(non_blocking=True)

Rule of thumb: workers × (samples decoded per second per worker) must exceed the samples per second the GPUs consume, with margin. Profile (C5.4): a timeline with gaps where the main thread waits in DataLoader.__next__ means the pipeline, not the GPU, is the bottleneck.

📝 Quick check (Level 1)
0/3 answered
  1. 1. num_workers in DataLoader sets…
  2. 2. pin_memory=True helps by…
  3. 3. Decoding takes 5 ms per sample; the GPU consumes 2560 samples/s. Minimum workers?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Streaming from shards

Random access to millions of small files is slow on parallel and object storage (C4.1). Datasets are therefore packed into shards of 100 MB–1 GB (WebDataset tar files, TFRecord, Mosaic MDS, Parquet row groups, HDF5/Zarr chunks, C7) and read sequentially. Consequences:

  • Assignment: shards are split across ranks, then across each rank’s workers (rank::world, then worker::num_workers), so no sample is read twice; there must be at least ranks × workers shards (better, many more).
  • Shuffling: shuffle the shard order every epoch (seeded by epoch), then pass samples through a shuffle buffer: an approximate shuffle whose quality depends on the buffer size relative to the shard size. Reading several shards at once (interleaving) adds mixing.
  • Equal steps: ranks may get different sample counts; truncate or pad so all ranks run the same number of steps (or use join in DDP).
  • Resumption: the stream must be reproducible from (seed, epoch, rank, worker) and skippable to the position saved in a checkpoint (C6.6).
🐍 Exercise 1: a sharded, shuffled, resumable loader
Assign shards to ranks and then to workers; build each worker's epoch stream (seeded shard shuffle, sequential reads, shuffle buffer); interleave workers into a rank's stream; check that every sample appears exactly once per epoch, that epochs differ and runs are deterministic, resume from (epoch, samples consumed), and equalize ranks with different numbers of shards.
📝 Quick check (Level 2)
0/3 answered
  1. 1. 16 shards, 2 ranks × 4 workers: shards per worker?
  2. 2. With 4 shards and 2 ranks × 4 workers…
  3. 3. To resume mid-epoch, the loader needs…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Shuffle quality matters

SGD assumes each batch is a roughly representative sample. If shards were written per class, sensor, site or time period and read in order, consecutive batches come from one source and the model drifts towards whatever it saw last; a shuffle buffer much smaller than a shard only blurs the boundaries. Remedies, cheapest first: write shards from already-shuffled data; shuffle shard order per epoch; read many shards concurrently; use a buffer spanning several shards (memory permitting); for multi-epoch training, a global index with random access (indexed formats such as Mosaic MDS or Parquet with row-group indexes).

Throughput engineering

Decode in workers (or on the GPU: NVIDIA DALI, torchvision.io GPU JPEG decoding), cache decoded data on node-local SSD for multi-epoch runs, use compressed formats that decode fast, overlap host-to-device copies, and monitor per-rank data-wait time.

📝 Quick check (Level 3)
0/3 answered
  1. 1. A shuffle buffer of 100 with 200-sample single-class shards…
  2. 2. The cheapest fix for class-sorted shards is…
  3. 3. Caching decoded data on local SSD helps when…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Data loading, measured

Exercise 1. With 16 shards of 50 samples over 2 ranks × 4 workers, each worker reads 2 shards; every sample appears exactly once per epoch and never on both ranks; epoch 1 has a different order from epoch 0; regenerating epoch 0 is identical, and resuming after 123 samples continues exactly. With 4 shards some workers would get none. Ranks with 3 and 2 shards are equalized to 100 samples each.

Exercise 2: sorted shards and one-pass training. 50 shards of 200 samples, each shard holding a single class of a 5-class problem; softmax regression trained in one pass (learning rate 0.5, batch 16); mean over 5 shard orders:

Stream consecutive same-class rate test accuracy
no shuffling (shard order shuffled only) 1.00 0.705 ± 0.07
buffer 100 0.65 0.708
buffer 1000 0.27 0.838
buffer 5000 (25 shards) 0.20 0.865
interleave 8 shards + buffer 100 0.29 0.748
interleave 8 shards + buffer 1000 0.24 0.847
full shuffle 0.20 0.865 ± 0.002

A buffer smaller than a shard does nothing for accuracy; one spanning about 25 shards matches a full shuffle. Interleaving 8 shards gives most of the label mixing with a 100-sample buffer but not all of the accuracy: within a group of 8 shards the class mix is still lopsided. The poorly mixed runs also vary a lot from one shard order to another (± 0.07): the result depends on which classes happen to come last.

🐍 Exercise 2 (research): shuffle buffers and sorted shards
Create single-class shards, stream them with shuffled shard order, optional interleaving of several shards and a shuffle buffer; measure how often consecutive samples share a class and the test accuracy of one-pass softmax regression, averaged over several shard orders, for buffer sizes from 1 to a full shuffle.

Data loading in AI, robotics and aerospace

  • Earth observation: tiles from many scenes packed into shards with mixed locations and dates; cloud-optimized formats for streaming (C7).
  • Robot learning: trajectories from many robots and tasks, sharded and interleaved so each batch mixes embodiments.
  • Telemetry and logs: time-ordered data deliberately shuffled across missions and periods for training, but split by time for validation (C3.6).
  • Simulation datasets: outputs written per run; shuffle across runs when sharding.

References

  1. PyTorch documentation (pytorch.org/docs): torch.utils.data (DataLoader, IterableDataset, worker info).
  2. T. Breuel et al. WebDataset (github.com/webdataset/webdataset) and A. Aizman, G. Maltby, T. Breuel (2019). “High performance I/O for large scale deep learning.” IEEE Big Data.
  3. D. G. Murray et al. (2021). “tf.data: a machine learning data processing framework.” Proc. VLDB Endowment, 14(12).
  4. J. Mohan et al. (2021). “Analyzing and mitigating data stalls in DNN training.” Proc. VLDB Endowment, 14(5).
  5. MosaicML. StreamingDataset documentation (docs.mosaicml.com/projects/streaming).