LayerNorm derivatives #
LayerNorm subtracts each row's mean, divides by sqrt (variance + ε), and applies the
per-column scale and bias. The input, scale, and bias all vary in these theorems; epsilon is a
fixed positive real number. In particular, neither the scale nor its perturbation is replaced
by an all-ones vector.
The graph calculus already proves that Spec.layerNorm is differentiable. Here we identify its
derivative with the actual Spec.layerNormJvp formula. We differentiate a normalized row entry,
apply the product rule for the scale, and add the bias perturbation. The existing JVP/VJP pairing
then identifies Spec.layerNormBackward with the adjoint derivative, including both parameter
cotangents. This also identifies the three tensors returned by the primitive with the backward
result of the detailed proof graph.
Positive epsilon keeps every row's standard deviation nonzero, including constant rows and rows with a single feature. All statements here concern exact real arithmetic.
The tensor and vector descriptions use the same row mean.
The tensor and vector descriptions use the same population variance.
One output entry is the normalized input times its own scale, plus its own bias.
Entrywise differentiation includes the input tangent and both affine parameter tangents.
The normalized row differential subtracts the tangent's row mean and its component along the
normalized input. The product rule supplies the additional xhat * dgamma term.
The primitive JVP has exactly the row differential obtained by differentiating the forward formula, with the same epsilon, scale, and bias perturbations.
The Fréchet derivative of the actual LayerNorm specification is its supplied JVP.
Both arguments use the graph's [input, scale, bias] packing, so the identity can be used
directly in a graph proof without changing the parameter layout.
Tensor form of the derivative bridge, with arbitrary input, scale, and bias tangents.
Every vector in the canonical LayerNorm context packs one input matrix and two parameter vectors. This also applies to arbitrary test directions in the adjoint proof.
The packed inner product is the sum of the input, scale, and bias tensor dot products.
The primitive backward rule is the adjoint of the actual LayerNorm derivative.
The three output tensors occupy the original input, scale, and bias slots. In particular, the scale and bias cotangents sum over rows because those parameters are shared by every row.
Backpropagating through the detailed LayerNorm graph returns exactly the same three cotangents as the primitive backward rule.