Calculus of row normalization #
LayerNorm normalizes every row of a [seqLen, embedDim] matrix; the flattened form of BatchNorm
normalizes every row of a [channels, positions] matrix. Both share the map
x ↦ (x - mean(x)) / sqrt (var(x) + ε)
applied to each row, where mean and var are the population statistics of that row. This file
proves that map is differentiable on flattened matrices (for 0 < ε) and identifies its
derivative with the closed form
dx ↦ invStd * (dx - mean(dx) - xhat * mean(dx * xhat))
used by the normalization JVPs.
Mean of row i of a flattened m × n matrix.
Instances For
Centered entry (i, j).
Instances For
Population variance of row i.
Instances For
Inverse standard deviation 1 / sqrt (var + ε) of row i.
Instances For
Normalized entry (i, j).
Instances For
Closed-form differential of the normalized entry (i, j) in the direction dX.
Instances For
The row variance is a mean of squares.
The centered entries of a row sum to zero.
The centered row is orthogonal to constants, so ∑ c_j (d_j - a) = ∑ c_j d_j.
Derivative building blocks #
Coordinate projection as a continuous linear functional on flattened matrices.
Instances For
The projection functional reads coordinate k.
Derivative of the row mean.
Instances For
The row-mean derivative is the row mean of the perturbation, since the mean is already linear.
The centering derivative subtracts the perturbation's own row mean.
Centering is linear too, so its derivative is itself; the nonlinearity in row normalization enters only through the variance and the square root below.
Derivative of the row variance at X.
Instances For
The variance differential only sees the centered direction.
Derivative of sqrt (var + ε) at X.
Instances For
Derivative of the inverse standard deviation at X.
Instances For
Derivative of the normalized entry (i, j) at X.
Instances For
Coordinates of a flattened matrix are differentiable.
Derivative of the row mean.
Derivative of a centered entry.
Derivative of the row variance.
Derivative of the inverse standard deviation, for positive ε.
The normalized entry is differentiable for positive ε.
The derivative of the normalized entry is the closed-form JVP.
Whole-matrix normalization #
Row index of a flattened matrix coordinate.
Instances For
Column index of a flattened matrix coordinate.
Instances For
Every flattened coordinate is idxMN of its row and column.
Row of the flattened coordinate idxMN i j.
Column of the flattened coordinate idxMN i j.
The tape row-mean map computes rowMean.
The tape row-broadcast map at idxMN i j reads entry i.
The tape column-broadcast map at idxMN i j reads entry j.
Normalize every row of a flattened matrix.
Instances For
Coordinate ip of the normalized matrix is the normalized entry at its row and column.
Closed-form JVP of nrmVec at X, packaged as a continuous linear map.
Instances For
The bundled JVP agrees with the closed-form nrmJvp coordinatewise.
Writing the derivative down in closed form and then proving HasFDerivAt against it, rather than
deriving it compositionally, is what keeps the ε guard visible: the formula is only the derivative
because ε > 0 keeps the denominator away from zero.
Row normalization of a flattened matrix is differentiable for positive ε, with the
closed-form JVP as derivative.