ZeRO-2 Sharded Adam and State Memory
ZeRO · SC 2020 · Medium · 40 minutes
Average and scatter gradients to owners, then update local Adam states and gather parameters.
Single-process ZeRO-2 semantics with equal-sized shards and a mixed-precision Adam-state byte model; activations and communication buffers are excluded.
Functions to implement
zero2_adam_step(params: Tensor, gradients: Tensor, first: Tensor, second: Tensor, step: int, lr: float, beta1: float = 0.9, beta2: float = 0.999, eps: float = 1e-8) -> tuple[Tensor, Tensor, Tensor]state_bytes(parameters: int, world_size: int, stage: int) -> int
ZeRO-2 Sharded Adam and State Memory
1. Problem
Average and scatter gradients to owners, then update local Adam states and gather parameters.
Single-process ZeRO-2 semantics with equal-sized shards and a mixed-precision Adam-state byte model; activations and communication buffers are excluded.
Source: ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Sections 5.1–5.3: optimizer, gradient and parameter partitioning.
2. Core equations
$$ g_s=\frac1R\sum_r g^{(r)}_{sS:(s+1)S},\quad m_s=\beta_1m_s+(1-\beta_1)g_s,\quad v_s=\beta_2v_s+(1-\beta_2)g_s^2,\quad p_s\leftarrow p_s-\eta\frac{m_s/(1-\beta_1^t)}{\sqrt{v_s/(1-\beta_2^t)}+\epsilon}. $$
3. From equations to code
zero2_adam_step(params: Tensor, gradients: Tensor, first: Tensor, second: Tensor, step: int, lr: float, beta1: float = 0.9, beta2: float = 0.999, eps: float = 1e-8) -> tuple[Tensor, Tensor, Tensor]— New sharded parameters and Adam moments, without input mutation.state_bytes(parameters: int, world_size: int, stage: int) -> int— Per-rank model-state bytes for ZeRO stage 0, 1, 2 or 3.
Reduce over gradient replicas, not parameter owners. Reshape the averaged gradient to (R,S) to simulate reduce-scatter. Owners update independently and flattening simulates all-gather. Stages 1, 2 and 3 successively shard the 12-, 2- and 2-byte categories.
4. Conventions
Parameters and moments have shape (R,S); replicated gradients have shape (R,R*S). Return updated parameter shards and both moments. Average across data-parallel ranks; step starts at one and weight decay is absent. Byte accounting uses two-byte weights, two-byte gradients, and twelve bytes for master weights plus two moments. Parameter count is divisible by R.
5. Experiment view
Two ranks process deterministic regression examples for 45 Adam steps at learning rate 0.04. Compare with an independent unsharded Adam update and report losses and model-state bytes.
The chart is generated by the actual submitted kernel during T3 and by the reference during export. Values are synthetic teaching measurements, not GPU throughput or full-model quality claims.
6. Run your implementation
Complete the functions in the Python editor on the right, then click Run tests. Inspect each failed check, update your implementation, and run again. Passing every required check marks the problem as solved and unlocks the notes.
7. Further study
Read the cited section and explain which dimensions or scheduling constraints the kernel preserves. Then extend the experiment beyond the stated fixture while retaining the existing contract. Larger models, real devices, and communication costs require separate measurement.