Gradient sharding: reduce globally and retain the owner slice
Derive reduce-scatter normalization, gradient ownership, and global clipping without confusing a local shard norm with a whole-model norm.
On this page
The question for today
What you’ll understand
- Trace sum reduce-scatter into averaged owner gradients.
- Calculate a full-gradient norm from disjoint reduced shards.
- Separate retained gradient savings from transient communication storage.
A replicated gradient contains information that only one optimizer owner may need for its update. Gradient sharding combines contributions across workers and retains each reduced slice only where it belongs. Reduce-scatter provides this structural operation, but its reduction operator and normalization still need an explicit contract. We distinguish a manual SUM reduce-scatter derivation from PyTorch 2.9 FSDP and DeepSpeed v0.18.2, whose wrappers coordinate their own reduction and scaling behavior. Sharding location alone does not define the intended loss objective.
Reduction and ownership are separate parts of the operation
Start with one logical gradient vector per worker evaluated at compatible parameters. Split corresponding vectors into compatible slices and assign each output slice to an owner. A SUM reduce-scatter adds corresponding input coordinates and returns only the assigned reduced slice at each rank. If the intended aggregate is a worker average, a separate division by the participating group size is needed somewhere in the controlled protocol. A framework may implement that division differently for numerical or scheduling reasons; never add a manual division without checking what the wrapper already does.
ZeRO stage two partitions reduced gradient storage along with optimizer-owned state while the simple stage-two compute-parameter view remains replicated. A full local backward gradient or communication bucket can still exist transiently before reduction and release. Therefore the persistent gradient-shard size does not prove that every instant uses only that much memory. Track when a slice becomes ready, when communication completes, when its owner updates state, and when replicated parameter views become consistent. Ownership and buffer lifetime are correctness properties as well as memory-budget inputs.
Global transforms require the logical full gradient
Clipping each shard independently generally changes the update because a shard norm is not the whole-model norm. For disjoint owner slices of one already reduced gradient, sum their squared norms across owners and take one square root. Compute a common clipping coefficient and apply it consistently. Do not first average those squared norms merely because the operation uses an all-reduce: the mathematical norm requires a sum. Replicated or tied coordinates also need careful counting so that the logical vector is not included twice.
The same distinction appears in logging and optimizer diagnostics. A small local norm can coexist with a large global norm, and averaging local norm values is not equivalent to taking the square root of summed squares. Mixed precision adds another ordering constraint: evaluate the intended unscaled gradient, account for overflow, and avoid applying an owner update before the shared clipping decision is available. State whether the comparison uses exact arithmetic or a tolerance for the reduction order and precision actually chosen.
Validate the owner boundary and temporary state
Build a small fixture with distinct coordinate values and uneven parameter shapes. Reconstruct the logical reduced gradient from owner slices and compare it with an unsharded reference before testing the optimizer. Include clipping, zero gradients, tied parameters if supported, and a partially filled final bucket. A successful reduce-scatter on the wrong slice layout can produce plausible numbers in the wrong parameter locations. Preserve a canonical coordinate map and check both values and identities rather than compare only one aggregate norm.
The function lab isolates the squared-contribution arithmetic. Reset, explicitly set coefficient one and position two, predict y, then Play or Step. For an independent run Reset and explicitly set coefficient one and position one before predicting again. The results four and one are the squared contributions of a two-coordinate example; their sum is five. The instrument computes one parabola at a time. It does not contain shards, reduce-scatter, clipping, distributed norm reduction, or optimizer ownership, so those protocols must be checked with the actual fixture.
Work it through
Reduce two vectors, then clip one global gradient
Two workers contribute gradients [2,0,6,0] and [4,0,2,0]. Owner A retains the first two coordinates and owner B the last two. Use a SUM reduce-scatter, then worker averaging, then global norm clipping at threshold 2.5.
The summed logical vector is [6,0,8,0]. SUM reduce-scatter gives A [6,0] and B [8,0]. Divide each by the two workers exactly once to obtain owner slices [3,0] and [4,0].
Their squared norm contributions are nine and sixteen. Sum to 25 and take the square root to get the global norm five. Averaging local norms or clipping each slice with its own coefficient would implement another transformation.
The common coefficient is 2.5/5=0.5. Apply it to both slices, yielding [1.5,0] and [2,0], whose reconstructed logical norm is 2.5. Owners can then apply the intended compatible optimizer update to their assigned coordinates.
Correct normalization gives one logical gradient [3,0,4,0], and correct global clipping scales every shard by the same one-half coefficient.
The living notebook
Explore the model
Interactive lab · function
Sum squared contributions outside the one-coordinate model
For separate fresh runs use Reset → explicitly set BOTH a and x → predict → Play/Step with (1,2) and (1,1). Read y=4 and y=1, then manually sum to five for a toy squared norm. This parabola does not implement gradient sharding or a distributed norm.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA global norm needs the complete sum of disjoint squared contributions, not an average of local norms.
Pause & reason
Check your understanding
Your turn
Try it yourself
Two disjoint owner slices of the intended reduced gradient are [6,0] and [8,0]. Clip the logical gradient at threshold five. What common coefficient and resulting slices follow, and why should the squared-norm reduction use SUM?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compute the norm of the reconstructed logical vector without materializing it, then apply one coefficient.
Compare with the solution
The global squared norm is 36+64=100 and its norm is ten. The common coefficient is 0.5, producing [3,0] and [4,0]. SUM is required because every disjoint squared coordinate contributes once to the same logical norm; averaging would incorrectly introduce the owner count into the clipping threshold. This calculation assumes the slices are already correctly normalized reduced gradients, complete and non-overlapping. Do not divide them again simply because an additional norm collective is performed.
Keep the idea
Take it with you
- Specify the reduce-scatter operator and apply the intended divisor exactly once.
- Compute global transforms over disjoint logical coordinates with correct counting.
- Persistent gradient sharding does not eliminate transient buffers or ownership checks.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.