research27 min studyLesson 922 in Engineering

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

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.

g‾i=gimin⁡ ⁣(1,C∥gi∥2)(gi≠0),g‾i=0(gi=0),C>0\overline g_i=g_i\min\!\left(1,\frac{C}{\|g_i\|_2}\right)\quad(g_i\ne0),\qquad\overline g_i=0\quad(g_i=0),\quad C>0
g_i is the gradient of one declared unit’s loss at the current model state. C has gradient units and clips the entire vector in this flat-clipping contract; the explicit zero case avoids division by zero.↔ Scroll wide equations horizontally.

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.

∥g‾i∥2≤C,clip⁡C ⁣(1b∑igi)≠1b∑iclip⁡C(gi)in general\|\overline g_i\|_2\le C,\qquad \operatorname{clip}_C\!\left(\frac1b\sum_i g_i\right)\ne\frac1b\sum_i\operatorname{clip}_C(g_i)\quad\text{in general}
b is a positive public batch divisor for this arithmetic comparison. The inequality is a contribution bound; the displayed nonidentity states that the two clipping orders are generally different, not that every input makes them unequal.↔ Scroll wide equations horizontally.

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.

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.

  1. 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.

  2. 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.

  3. 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 result

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.

Explore the model

Interactive lab

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).

Length clipping preserves directionPlayback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Current readings: x component: 1.393; y component: 0.975; Magnitude: 1.7. All parameters are adjustable with the labeled controls below.−7.46−3.7303.737.46−3.5−1.7501.753.5xyvvₓ = 1.39
Vectorx / y projectionsTrace marker
x component
1.393
y component
0.975
Magnitude
1.7
t = 0 s

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.

Check your understanding

Why can training-time cross-example operations invalidate the simplest per-record clipped-sum sensitivity proof?

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.

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.