BugZoo: LayerNorm on a one-feature axis #
LayerNorm has a sharp degenerate case: if the normalized axis has length one, then the mean is the single input value and the variance is zero. The normalized value is therefore exactly zero, so the affine output is the bias and the forward result is independent of both the input and the scale.
PyTorch analogy:
torch.nn.functional.layer_norm(x, normalized_shape=(1,), weight=w, bias=b)
For every finite scalar x, the mathematical contract is:
$$ \begin{aligned} \operatorname{mean}([x]) &= x,\\ \operatorname{var}([x]) &= 0,\\ \left(\frac{x-\operatorname{mean}}{\sqrt{\operatorname{var}+\varepsilon}}\right) \operatorname{weight}+\operatorname{bias} &= \operatorname{bias}. \end{aligned} $$
So reverse mode must report zero gradient for weight and zero input gradient. This file keeps the
contract small: the real-valued theorems record the algebra, and the concrete definitions below are
the public TorchLean spec terms used by the Python reproducer notes.
Instances For
Instances For
Instances For
Instances For
The scalar algebra behind one-feature LayerNorm: normalization contributes zero, so the affine result is the bias.
The scale/weight gradient is zero because the normalized one-feature value is zero.
TorchLean spec value for the public PyTorch repro: forward output.
Instances For
TorchLean spec value for the public PyTorch repro: gradient with respect to weight.
Instances For
TorchLean spec value for the public PyTorch repro: gradient with respect to input.