6.1.8Scaling & Efficient Architectures

Data parallelism and ZeRO optimization

3,107 words14 min readdifficulty · medium1 backlinks

What is Data Parallelism?

How Standard Data Parallelism Works

The training loop on each GPU (rank rr):

  1. Forward pass: Process mini-batch Br\mathcal{B}_r through local model copy θr\theta_r
  2. Backward pass: Compute local gradients θrL(Br)\nabla_{\theta_r} \mathcal{L}(\mathcal{B}_r)
  3. AllReduce: Synchronize gradients across all GPUs:ˉθ=1Nr=1NθrL(Br)\bar{\nabla}_\theta = \frac{1}{N} \sum_{r=1}^{N} \nabla_{\theta_r} \mathcal{L}(\mathcal{B}_r)
  4. Update: Apply optimizer: θθηˉθ\theta \leftarrow \theta - \eta \bar{\nabla}_\theta
  5. Broadcast: All GPUs now have identical updated parameters

WHY does this work? Because the gradient of the full dataset is the average of gradients from each subset (linearity of expectation). Each GPU computes a noisy estimate of the true gradient, and averaging reduces variance.

Memory Consumption Analysis

The ZeRO Optimization

ZeRO Stages: Progressive Partitioning

ZeRO-1: Optimizer State Partitioning

What: Each GPU stores only 1N\frac{1}{N} of the optimizer states.

How:

  1. Partition parameters into NN groups: θ=[θ1,θ2,,θN]\theta =[\theta_1, \theta_2, \ldots, \theta_N]
  2. GPU rr stores optimizer states only for partition θr\theta_r
  3. After AllReduce of gradients, GPU rr updates only θr\theta_r using its local optimizer states
  4. AllGather updated parameters so all GPUs have the full model

Memory savings: MZeRO-1=2Φ+2Φ+12ΦN=4Φ+12ΦNM_{\text{ZeRO-1}} = 2\Phi + 2\Phi + \frac{12\Phi}{N} = 4\Phi + \frac{12\Phi}{N}

WHY does this work? Optimizer states (momentum, variance) for parameter θi\theta_i only depend on gradient θi\nabla \theta_i, not on other parameters. So GPU rr can independently update its assigned partition.

ZeRO-2: Gradient Partitioning

What: Additionally partition gradients—each GPU only stores 1N\frac{1}{N} of gradients.

How:

  1. During backward pass, each GPU computes full local gradients
  2. Use ReduceScatter instead of AllReduce: GPU rr receives only the averaged gradient for partition θr\theta_r
  3. GPU rr updates θr\theta_r, then AllGather

Memory savings: MZeRO-2=2Φ+2ΦN+12ΦN=2Φ+14ΦNM_{\text{ZeRO-2}} = 2\Phi + \frac{2\Phi}{N} + \frac{12\Phi}{N} = 2\Phi + \frac{14\Phi}{N}

WHY ReduceScatter? It's AllReduce + partition in one operation. Instead of every GPU getting the full averaged gradient (AllReduce), each GPU gets only its assigned slice of the averaged gradient.

ZeRO-3: Parameter Partitioning

What: The ultimate step—partition even the model parameters. Each GPU only stores 1N\frac{1}{N} of parameters.

How:

  1. Forward pass: GPU rr needs layer ll's parameters
    • If θl\theta_l is on GPU rr: use directly
    • Otherwise: AllGather θl\theta_l from owner GPU, compute, then discard
  2. Backward pass: AllGather parameters again when needed for gradient computation
  3. Update: Same as ZeRO-2 (ReduceScatter gradients, local update, AllGather updated params)

Memory savings: MZeRO-3=2ΦN+2ΦN+12ΦN=16ΦNM_{\text{ZeRO-3}} = \frac{2\Phi}{N} + \frac{2\Phi}{N} + \frac{12\Phi}{N} = \frac{16\Phi}{N}

This is the N×N\times reduction! All model state components are partitioned.

Advanced: ZeRO-Infinity and ZeRO-Offload

Memory hierarchy: GPU HBM5 TB/sAllGatherGPU HBMorCPU RAM30 GB/sPCIeGPUorNVMe7 GB/sPCIeCPU\text{GPU HBM} \xrightarrow[\text{5 TB/s}]{\text{AllGather}} \text{GPU HBM} \quad \text{or} \quad \text{CPU RAM} \xrightarrow[\text{30 GB/s}]{\text{PCIe}} \text{GPU} \quad \text{or} \quad \text{NVMe} \xrightarrow[\text{7 GB/s}]{\text{PCIe}} \text{CPU}

Strategy: Keep only the currently-computed layer's parameters on GPU. Prefetch next layer's parameters from CPU while computing current layer.

Recall Explain to a 12-year-old

Imagine you and7 friends are building identical LEGO castles (that's your neural network). You each need the instruction manual (model parameters), and you're building different rooms (processing different data batches).

Standard way: Everyone has their own full copy of the 1000-page manual. When you each finish a room, you compare notes: "I think tower should be 10 blocks high," "I think 11," etc. You average the suggestions and everyone updates their manual to say "10.5 blocks." This works, but you've printed 8,000 pages total!

ZeRO way:

  • ZeRO-1: Each friend only keeps1/8 of the manual (125 pages) for making changes, but everyone still has a full read-only copy.
  • ZeRO-2: When sharing suggestions, each friend only writes down the suggestions for their 125-page section.
  • ZeRO-3: Each friend only has 125 pages, period. When you need to build a tower, you yell "Who has the tower pages?" and that friend reads them out loud to everyone. You build, then forget those instructions.

ZeRO-3 means you only printed 1,000 pages total (8× savings!), but now you spend time asking friends to read pages out loud. That's the trade-off: less paper (memory), more talking (communication).

Practical Considerations

When to Use Each ZeRO Stage

Communication Efficiency Requirements

The efficiency of ZeRO-3 depends on: Efficiency=TcomputeTcompute+Tcomm\text{Efficiency} = \frac{T_{\text{compute}}}{T_{\text{compute}} + T_{\text{comm}}}

where Tcomm=CZeRO-3BandwidthT_{\text{comm}} = \frac{C_{\text{ZeRO-3}}}{\text{Bandwidth}}

For 75% efficiency (reasonable target): TcomputeTcompute+Tcomm0.75    TcommTcompute3\frac{T_{\text{compute}}}{T_{\text{compute}} + T_{\text{comm}}} \geq 0.75 \implies T_{\text{comm}} \leq \frac{T_{\text{compute}}}{3}

Connections

  • Model Parallelism: Complements ZeRO by splitting model layers across GPUs (vs. replicating)
  • Pipeline Parallelism: Another form of model parallelism; ZeRO can be combined with pipelines
  • Mixed Precision Training: ZeRO inherently uses mixed precision (fp16 compute, fp32 optimizer)
  • Gradient Accumulation: Can combine with ZeRO to handle larger effective batch sizes
  • Activation Checkpointing: Reduces MactM_{\text{act}} term, making ZeRO-2 feasible instead of ZeRO-3
  • AllReduce and Collective Communication: Core primitives; ZeRO replaces AllReduce with ReduceScatter + AllGather
  • Large Language Models Training: ZeRO is essential for training GPT-3, LaMA, PaLM scale models
  • Memory-Efficient Optimizers: Adafactor, 8-bit Adam reduce the 12Φ12\Phi optimizer term

#flashcards/ai-ml

What is data parallelism in distributed training? :: A strategy where each GPU holds a complete copy of the model, processes different data batches in parallel, and synchronizes gradients using AllReduce to update all replicas identically.

What is the memory consumption formula for standard data parallelism with mixed precision and Adam optimizer?
Mgpu=16ΦM_{\text{gpu}} = 16\Phi bytes where Φ\Phi is the number of parameters. Breakdown: 2Φ params (fp16) + 2Φ gradients + 12Φ optimizer states (fp32 momentum, variance, master weights).
What does ZeRO stand for and what does it optimize?
Zero Redundancy Optimizer. It eliminates memory redundancy in data parallelism by partitioning model states (optimizer states, gradients, parameters) across GPUs instead of replicating them.
What are the three ZeRO stages and what does each partition?
ZeRO-1: partitions optimizer states only. ZeRO-2: partitions optimizer states + gradients. ZeRO-3: partitions optimizer states + gradients + parameters.
What is the memory per GPU formula for ZeRO-3 with N GPUs?
MZeRO-3=16ΦNM_{\text{ZeRO-3}} = \frac{16\Phi}{N} — all model state components (params, gradients, optimizer states) are partitioned N ways, achieving N× memory reduction.
What communication primitive does ZeRO-2 use instead of AllReduce and why?
ReduceScatter. It combines AllReduce with partitioning—each GPU receives only the averaged gradient for its assigned parameter partition, saving gradient memory while maintaining correctness.

What is the main trade-off when using ZeRO-3 compared to standard data parallelism? :: Memory vs. communication. ZeRO-3 achieves N× memory savings but requires 3× more communication volume (AllGather parameters twice per step + ReduceScatter gradients once, vs. one AllReduce in standard DP).

What is ZeRO-Infinity and when is it used?
An extension of ZeRO-3 that offloads partitioned model states to CPU RAM and NVMe SSD, using bandwidth-aware prefetching. Used for training trillion-parameter models that don't fit even with ZeRO-3 across available GPUs.
Why does ZeRO-3 require high-bandwidth interconnects to be efficient?
Because it must AllGather parameters during forward and backward passes. For 75% compute efficiency, network bandwidth must satisfy BWmin=18Φ(N1)NTcompute\text{BW}_{\text{min}} = \frac{18\Phi(N-1)}{N \cdot T_{\text{compute}}}. Low bandwidth causes communication overhead to dominate.
When should you NOT use ZeRO-3 despite having it available?
When your model fits in GPU memory with standard DP or ZeRO-2. ZeRO-3's 3× communication overhead makes training slower if memory isn't the bottleneck. Use the minimum ZeRO stage that fits your model.

Concept Map

each GPU holds

processes

forward-backward

produces

drives

causes

solved by

works by

eliminates

enables

Data Parallelism

Full model copy per GPU

Different data batches

AllReduce gradient sync

Averaged gradient

Optimizer update

Redundant memory 16Phi per GPU

ZeRO Zero Redundancy Optimizer

Shard states across GPUs

Train 100B models

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo, ek simple picture se samajhte hain. Jab hum ek bada neural network train karte hain, toh data parallelism ka matlab hai ki har GPU ke paas poore model ki ek complete copy hoti hai, aur har GPU alag-alag data batches process karti hai. Phir sab GPUs apne gradients ko aapas mein average kar leti hain (AllReduce), taaki sab ki copy ek jaisi rahe. Yeh kaam isliye karta hai kyunki poore dataset ka gradient basically har chhote subset ke gradients ka average hi hota hai — toh multiple GPUs milke faster training kar leti hain, aur averaging se noise bhi kam ho jaata hai.

Ab problem yeh hai ki har GPU par model ki complete copy rakhne se bahut zyada memory waste hoti hai. Jaise formula batata hai, ek model ke liye har GPU ko lagbhag 16Φ bytes chahiye — parameters, gradients, aur optimizer states (Adam ke momentum, variance, aur fp32 master weights) sab milaake. GPT-3 jaise 175 billion parameter model mein yeh 2800 GB per GPU ban jaata hai, jabki ek A100 GPU mein sirf 80 GB memory hoti hai! Aur agar 8 GPUs hain, toh hum 8 baar wahi same cheez store kar rahe hain — yani 8x redundancy, matlab bilkul zaroorat se zyada memory barbaad.

Yahin par ZeRO (Zero Redundancy Optimizer) kaam aata hai. Iska core idea simple hai: har GPU par saari cheezein duplicate karne ke bajaye, model state (optimizer states, gradients, parameters) ko sabhi GPUs mein baant do. Matlab har GPU sirf apna assigned portion rakhti hai, aur zaroorat padne par smart communication se ek doosre ki madad karti hai. Isse memory redundancy khatam ho jaati hai, aur yehi reason hai ki ZeRO ke saath 100-billion parameter jaise gigantic models ko limited hardware par bhi train kar paana possible ho jaata hai. Yeh matter isliye karta hai kyunki bina ZeRO ke aise bade models train karna practically impossible hota, aur aaj ke sabse powerful AI models isi tarah ki efficient scaling techniques par tike hain.

Go deeper — visual, from zero

Test yourself — Scaling & Efficient Architectures

Connections