Clip the protected contribution before aggregating gradients
Track per-example norms, separable losses and normalization rather than clipping only the final batch gradient.
On this page
The question for today
What you’ll understand
- Compute Euclidean clipping of individual gradient vectors.
- Distinguish clipping each contribution from clipping a batch aggregate.
- Keep per-example loss and logical-batch normalization consistent with the mechanism.
A DP-SGD-style sensitivity argument needs a bound on each protected contribution before summation. Clipping the final batch gradient is a different operation and does not reproduce that argument. The implementation must also define what one example means and recover its gradient under the chosen loss reduction. Otherwise a correct-looking clipping constant can be applied to gradients whose scale or dependence differs from the accountant’s mechanism.
Bound each vector in the intended norm
For a nonzero per-example gradient g, multiply it by the smaller of one and C divided by its Euclidean norm. Leave a zero gradient unchanged. Small gradients are preserved; larger gradients retain direction while their norm becomes C. If parameters span several tensors, flat clipping uses the norm of their concatenated per-example vector. Clipping every parameter tensor independently at C instead permits a larger overall norm and therefore needs a different sensitivity or per-layer analysis.
The gradient must correspond to one protected unit’s contribution at the fixed current model state. A separable sum of per-example losses has this form. Training-time BatchNorm and other cross-example operations can make changing one record alter other records’ gradients, defeating the simple “only one bounded term changes” proof. Validate model and loss compatibility rather than assuming the presence of a per-sample tensor proves the privacy argument. Per-user clipping requires constructing the user’s full contribution, not merely renaming a row gradient.
Clipping and averaging do not commute
Averaging unclipped gradients first allows a large contribution to affect the aggregate before any record-specific bound is applied. Clipping that average yields a bounded batch vector, but its privacy unit and sensitivity are different from a sum of independently clipped contributions. Even ordinary cancellation can make the two numerical results diverge. A batch-clipped mechanism can be analyzed on its own terms; it simply cannot inherit the per-example DP-SGD proof without showing the required equivalence or deriving a new bound.
Loss reduction introduces another scale. Frameworks often report a mean loss, while the clipping rule is expressed for each unaveraged example loss. Libraries may compensate for that reduction when constructing per-sample gradients. Do not divide by the batch size again without checking the implementation. Opacus v1.5.4 distinguishes per-sample clipping, summation, noise addition and later expected-batch scaling. Microbatching for memory should preserve the declared logical mechanism, including which units are clipped and how many independently noised updates occur.
Verify clipping with small vectors and the real reduction path
Test zero, below-threshold, exactly-at-threshold and above-threshold gradients, including parameters split across tensors. Compare a trusted explicit per-example calculation with any efficient clipping implementation. Check that repeated backward passes and accumulated microbatches do not reuse stale gradients or silently add several copies. Keep model-state updates such as running statistics in scope. Clipping bounds influence but supplies no statistical privacy by itself; an uncloaked clipped vector can still reveal a participant’s data.
For the geometry, Reset the vectors lab and set both angle 0° and magnitude 3. Predict (3,0), then clip on paper at C=2 to obtain (2,0). Separately Reset and set both angle 90° and magnitude 3; predict (0,3), clipped on paper to (0,2). The instrument itself does not clip vectors or compute gradients. It illustrates that an Euclidean norm threshold limits length independently of direction and does not add noise or calculate a privacy budget.
Work it through
Compare per-example clipping with clipping the mean
Let two per-example gradients be g₁=(3,4) and g₂=(0,1), with flat Euclidean threshold C=2. Use public divisor b=2. Compute their clipped mean and compare it with clipping their raw mean at the same threshold.
The first gradient has norm five, so its clip factor is 2/5 and it becomes (1.2,1.6). The second has norm one and remains (0,1). Each resulting vector has norm at most two.
Sum the clipped vectors to obtain (1.2,2.6), then divide by two to obtain (0.6,1.3). This is the contribution-first clipped mean for the declared two examples.
The raw mean is (1.5,2.5), with norm √8.5≈2.9155. Clipping it to length two gives (3/√8.5,5/√8.5)≈(1.0290,1.7150), a different vector. The same threshold number did not make the two mechanisms equivalent.
The contribution-first result is (0.6,1.3). Bounding a batch aggregate afterward changes both its numerical behavior and the sensitivity argument; neither clipping operation alone establishes DP.
The living notebook
Explore the model
Interactive lab · vectors
Length clipping preserves direction
Reset → set angle 0° and magnitude 3 → predict (3,0), then clip at C=2 on paper. Separate Reset → set angle 90° and magnitude 3 → predict (0,3), clipped on paper to (0,2).
- x component
- 1.393
- y component
- 0.975
- Magnitude
- 1.7
Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. 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 forThe vector instrument does not implement clipping, gradient extraction, random noise or accounting. It supplies the geometry for the hand calculation.
Pause & reason
Check your understanding
Your turn
Try it yourself
Clip g₁=(6,8) and g₂=(-3,4) independently at C=5, then average with public divisor two. Which vector changes, and what is the mean?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Both norms are easy to calculate: the first is ten and the second five. A vector exactly on the threshold remains unchanged.
Compare with the solution
The first becomes (3,4), using factor 1/2. The second already has norm five and remains (-3,4). Their sum is (0,8) and mean (0,4). This calculation verifies clipping arithmetic only; a privacy claim still needs the sampling, noise, adjacency and accounting contract.
Keep the idea
Take it with you
- Clip the full contribution for the protected unit before summation.
- Cross-example dependence and loss reduction belong in the sensitivity proof.
- Efficient microbatching must preserve the same logical clipped-and-noised mechanism.
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.