Build a Data Loader Pipeline That Prevents GPU Starvation
performance data-engineering distributed-systems
AI System Design Deep Dive
GPU-Starvation-Proof Data Loader
128 GPUs wait for the slowest loader, so the tail beats the mean.
Picture a 128-piece orchestra where the conductor refuses to start the next bar until every musician has turned the page. If each musician is late once in a thousand bars, the orchestra is late in 12 percent of bars. Synchronous data-parallel training works exactly this way. Every optimizer step ends in an all-reduce, and an all-reduce cannot finish until the slowest of the 128 ranks has a batch on the GPU. The data loader is the page turn, and at this scale the 99.9th percentile of one loader is the median of the cluster.
The workload is a vision-language model trained on 2.4 billion image-text pairs on 16 p5.48xlarge nodes, 128 H100 GPUs in total. A step takes 213 ms and each GPU consumes 64 samples per step, so the cluster must deliver 38,460 samples per second. At an average of 160 KB per JPEG that is 6.15 GB/s from object storage, 384 MB/s per node, and 36 CPU cores per node spent decoding at 15 ms per image. The cluster costs $880.64 per hour, so one idle second costs about 24 cents. A 21-day run is $443,843 of compute if the GPUs never wait.
The naive loader is a PyTorch DataLoader with num_workers=0 that reads one image at a time from S3. It decodes inline, so the step cannot start until 64 JPEGs have been fetched and decoded, roughly 0.96 s of CPU for a 0.213 s step. In our simulation that configuration keeps the GPUs busy 8.7 percent of the time. The fixes people reach for first, more workers and a bigger prefetch factor, get you to 62 percent and then stop helping, because the remaining loss is not throughput. It is the tail: an occasional shard fetch that takes 19 seconds instead of 1.6, on any one of 128 ranks, stalls everyone.
We need to solve for sustained throughput (bytes in, decoded tensors out, at 38,460 samples/s), tail latency (no single slow fetch may reach the GPU), and resumability (a restart after a node failure must not replay or skip data) simultaneously.
Requirements and Constraints
Functional requirements
- Stream 2.4 billion image-text pairs from object storage, shuffled, to 128 ranks with no sample seen twice per epoch.
- Decode, resize and crop each image to 448 x 448 and tokenize its caption.
- Deliver pinned, device-ready batches of 64 samples to each rank.
- Checkpoint loader position with every model checkpoint and resume without skipping data.
- Emit per-step data wait time so starvation is visible, not inferred.
Non-functional requirements
- Throughput: 38,460 samples/s cluster wide, 300 samples/s per GPU, 6.15 GB/s from S3.
- Latency: p99 data wait per step under 5 ms, and under 0.5 percent of steps with any wait above 50 ms.
- GPU utilization: 98 percent or better of the 213 ms step time, measured across the barrier.
- Quality: shuffle quality comparable to a global shuffle, measured as no loss curve divergence against the reference loader over 2,000 steps. Duplicate samples after a resume below 0.25 percent of one epoch.
- Cost: loader overhead under 1 percent of the run, S3 request cost under $15 per day.
- Capacity: pinned host memory under 12 GB per node, shuffle buffers under 48 GB per node, NVMe window under 10 GB per node.
Constraints
- Pre-sharded data: 160 MB tar shards of 1,000 samples each, 2.4 million shards in total, 384 TB. Re-sharding is a one-time batch job and out of scope.
p5.48xlargenodes: 8 H100 80 GB, 192 vCPUs, 2 TB RAM, 8 local NVMe drives, per-node S3 bandwidth well above 384 MB/s.- PyTorch 2.x
DataLoaderwith worker processes,torch.distributedwith NCCL. No custom CUDA kernels, no DALI in the base design (it appears as an option). - Out of scope: online dataset mixing, curriculum learning, and the tokenizer for multilingual captions.
High-Level Architecture
Think of a hospital pharmacy that fills prescriptions for 128 nurses who all start their rounds at the same instant. The pharmacy does not wait for a nurse to ask. It keeps a tray ready for each nurse, restocks from the warehouse in bulk, and calls a second courier if the first is late. The loader has the same five parts, spread across a control plane that is one manifest and a host plane that runs on every node.
A batch travels like this. The manifest assigns each rank a deterministic list of shards for the epoch. The shard fetcher pulls the next four shards of that list from S3 with 20 MB ranged GETs, fires a backup request when a part is slower than twice the rolling median, and lands the bytes on local NVMe. Eight decode worker processes per rank read the tar files from NVMe, push samples through a shuffle buffer, decode and crop the JPEGs, and write uint8 tensors into a bounded queue of pinned memory. A side CUDA stream copies the next batch to the GPU while the current step runs, and normalization happens on the GPU, so the PCIe link carries one byte per channel instead of four.
The monitor closes the loop. Each step records how long the trainer waited for a batch, the waits are reduced with MAX across ranks every 100 steps, and the slowest rank is named in the log. Without that, starvation shows up as a vague “MFU is 38 percent” ticket that nobody can attribute.
- Shard fetcher: keeps a window of shards on NVMe, hedges slow requests, verifies checksums.
- Shard sampler: gives each rank and worker a deterministic, resumable shard order.
- Decode workers: turn tar bytes into cropped
uint8tensors with a shuffle buffer in front. - Pinned queue: bounded buffer that applies backpressure when the GPU is the bottleneck.
- Device prefetcher: overlaps the host to device copy and GPU normalization with the current step.
- Stall monitor: measures wait time per step and finds the slowest rank across the cluster.
The loader is sized for the slowest of 128 ranks, not the average rank: the same pipeline that gives 99.5 percent utilization on one GPU gives 77.2 percent on 128.
The Shard Fetcher
The shard fetcher’s job is to have the next four shards of every rank on local NVMe before the decode workers ask for them, and to make sure one slow S3 request can never be the reason they are not there.
Most engineers assume the loader needs more bandwidth. It does not: 384 MB/s per node is under 2 percent of what a p5 node can pull from S3. What it needs is a bounded tail. A shard is 160 MB and a rank consumes one every 3.4 seconds (16 batches x 213 ms). If a GET that normally takes 1.6 s occasionally takes 19 s, a lead of one or two shards is not enough, and with 128 ranks there is almost always a rank in that situation.
Premium Content
Unlock the full article along with everything else in the archive — all in one place.