Forward pass: Process mini-batch Br through local model copy θr
Backward pass: Compute local gradients ∇θrL(Br)
AllReduce: Synchronize gradients across all GPUs:∇ˉθ=N1∑r=1N∇θrL(Br)
Update: Apply optimizer: θ←θ−η∇ˉθ
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.
What: Each GPU stores only N1 of the optimizer states.
How:
Partition parameters into N groups: θ=[θ1,θ2,…,θN]
GPU r stores optimizer states only for partition θr
After AllReduce of gradients, GPU r updates only θr using its local optimizer states
AllGather updated parameters so all GPUs have the full model
Memory savings:
MZeRO-1=2Φ+2Φ+N12Φ=4Φ+N12Φ
WHY does this work? Optimizer states (momentum, variance) for parameter θi only depend on gradient ∇θi, not on other parameters. So GPU r can independently update its assigned partition.
What: Additionally partition gradients—each GPU only stores N1 of gradients.
How:
During backward pass, each GPU computes full local gradients
Use ReduceScatter instead of AllReduce: GPU r receives only the averaged gradient for partition θr
GPU r updates θr, then AllGather
Memory savings:
MZeRO-2=2Φ+N2Φ+N12Φ=2Φ+N14Φ
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.
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).
Gradient Accumulation: Can combine with ZeRO to handle larger effective batch sizes
Activation Checkpointing: Reduces Mact 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Φ 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Φ bytes where Φ 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=N16Φ — 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=N⋅Tcompute18Φ(N−1). 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.
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.