Float bounds of layer norm's and RMS norm's outputs with rsqrt.approx
Lean: MojoProof/Float/Norm.lean, with msq and msq_error in MojoProof/Float/Stats.lean.
Instances: MojoProofTest/FloatNorm.lean. Floats are FloatLib's Model fmt words;
u = Model.unitRoundoffAt fmt is FloatLib's unit roundoff and η = Model.underflowErrorAt fmt
its underflow allowance (in binary32, u = 2 ^ -24 and η = 2 ^ -150). MixedError x x₀ r a is
FloatLib's |x - x₀| ≤ r · |x₀| + a with r, a ≥ 0.
The statements are about words and the operations a kernel performs on them, as functions of the inputs. That a grid run of a kernel performs those operations is not proved here. The shape is the one of docs/proofs/float-reductions.md: for every input meeting the premises, if the computed word is finite, it is within the stated bound of the exact real value, TorchLean's where TorchLean has one.
The outputs
The compiled layer norm kernels normalize each input x j with the Welford mean word μ and the
word r = rsqrt.approx (v + ε) of the variance word v (docs/proofs/float-welford.md) plus the
word ε, then scale and shift: ((x j - μ) · r) · γ j + β j
(tests/corpus/layernorm/ln_rowwise_warp_t1_r1_ax0_w1_bs128_1a9a768a.nvptx.ll, lines 430-432 for
v by fdiv, v + ε and llvm.nvvm.rsqrt.approx.ftz.f, lines 513-516 for the output, all four
operations contract). lnOut fused x μ r g b is that word, by Model.sub, Model.mul,
Model.mul and Model.add, or with the last multiplication and addition one Model.fma when
fused. lnorm t x g b ε k fused j is the binary32 word at j with the Welford words along the
tree t and Sem.rsqrt with oracle number k.
RMS norm computes the mean of squares msq t x N (each square by Model.mul, summed by
Model.add along t, divided by a word N denoting n), r = rsqrt.approx (msq t x N + ε) and
rmsOut (x j) r (g j) = (x j · r) · g j; rmsnorm t x g N ε k j is that binary32 word.
Mojo's RMS norm (max/kernels/src/nn/normalization.mojo, lines 339-345 at the pinned commit) adds
a weight offset o to the weight g of the dtype, in one of two orders: multiplied in the
accumulator before the cast into the dtype, cast ((x · r) · (cast g + o)), or cast first,
cast (x · r) · (g + cast o). rmsOff mbc x r o g is that word for an accumulator format and a
dtype, each cast Model.cast to nearest; rmsnormOff mbc t x g N ε o k j is the word at j
accumulated in binary32.
rsqrt.approx enters through its contract Sem.rsqrt_close (MojoProof/Sem/FloatRel.lean): for a
positive normal operand s, every word Sem.rsqrt s k is finite and within relative error
rsqrtErr = 1.0718 · 2 ^ -23 of 1 / √s.
Statements
msq_error: in every IEEE format, for everyn, tree over0, …, n - 1up to order, inputs and finite wordNof real valuen: if the mean of squares word is finite, it is withinMixedError … u (msqErr u η t.depth x)of𝔼 j, x j ^ 2, withmsqErr u η d x = (1 + u) · (((1 + u) ^ d - 1) · 𝔼 x² + (1 + u) ^ d · 𝔼 (u · x² + η)) + η.rsqrt_rel: for realss,t > 0withswithinMixedError … r aoft,r + a / t < 1,ρ ≥ 0andwwithin relative errorρof1 / √s:wis within relative errorrsqrtRel r ρ a t = (1 + ρ) · δ / (1 - δ) + ρ,δ = r + a / t, of1 / √t.lnOut_error: in every IEEE format, fused or not, for a mean word withineofμ₀and a reciprocal word within relative errorκofw: a finite output is withinMixedError … u (lnErr u η e κ w g (x - μ₀))of(x - μ₀) · w · g + b. The fused bound is below the unfused one, whichlnErrstates.lnorm_error: in binary32, for everyn ≤ 2 ^ 24, tree over0, …, n - 1up to order, inputs withinBin magnitude, weights, biases, wordε > 0, oracle numberk, fused or not, and indexj: if the Welford words and the variance word are finite, the operandv + εofrsqrt.approxis finite, positive and normal,u + lnDev u η B t.depth V / (V + ε) < 1(Vthe inputs' variance) and the output is finite, then the output is withinMixedError … u (lnErr u η e κ (1 / √(V + ε)) (g j) (x j - 𝔼 x))of TorchLean's real layer normTorchLean.TransformerBlock.layerNorm g b ε x j, withe = (welfErr u η B t.depth).1andκ = rsqrtRel u rsqrtErr (lnDev u η B t.depth V) (V + ε).rmsOut_error: in every IEEE format, for a reciprocal word within relative errorκofw: a finite(x · r) · gis withinMixedError … ((1 + u) · ((1 + u) · κ + u) + u) ((1 + u) · η · |g| + η)ofx · w · g.rmsnorm_error: in binary32, for everyn, tree over0, …, n - 1up to order, inputs, weights, finite wordNof real valuen, wordε > 0, oracle numberkand indexj: if the operandmsq t x N + εis finite, positive and normal,u + (1 + u) · (u · M + msqErr u η t.depth x) / (M + ε) < 1(M = 𝔼 x²) and the output is finite, the output is within statement 5's mixed error, withκ = rsqrtRel u rsqrtErr ((1 + u) · (u · M + msqErr …)) (M + ε), of the real RMS normx j / √(M + ε) · g j.rmsOff_error: for every IEEE accumulator format and IEEE dtype, ordermbc, and reciprocal word within relative errorκofw: a finitermsOff mbc x r o gis withinMixedError … (rmsOffErr mbc ua ηa ud ηd κ (x · w) g o)ofx · w · (g + o), with the unit roundoffs and underflow allowances of the two formats.rmsnormOff_error: accumulated in binary32, for every IEEE dtype, ordermbc,n, tree over0, …, n - 1up to order, binary32 inputs, weights of the dtype, finite wordNof real valuen, wordsε > 0ando, oracle numberkand indexj: under statement 6's premises on the operand (the one on its error written¬ 1 ≤ …), a finite output is within statement 7's mixed error, with statement 6'sκ, of the real RMS norm with the offsetx j / √(M + ε) · (g j + o).
TorchLean's TorchLean.Tensor.rmsNorm (TorchLean/Tensor/Layers/Normalization.lean) divides each
fiber by its largest magnitude before it takes the mean of the squares, and TorchLean proves no
closed form of it over the reals; its docstring states x / sqrt (mean x ^ 2 + epsilon) · gamma
for a positive scale. Statement 6 is against that real value, written out; no second RMS norm is
defined. TorchLean.TransformerBlock has no RMS norm.
Instances (MojoProofTest/FloatNorm.lean)
In binary32, with weights 1. lnorm_iota is statement 4 at the inputs 1, …, 2 ^ k along the
halving tree halveFin k, for every k ≤ 24, with the inputs' variance
((2 ^ k) ^ 2 - 1) / 12 in place of V; the tactic lndev_num closes its premise … < 1 at a
concrete k and ε by norm_num on FloatLib's binary32 constants. Its instances, fused or not,
with biases 0, are lnorm_xs at k = 2 (the inputs 1, 2, 3, 4 along (0 + 2) + (1 + 3))
with ε = 2.75, where the variance word is 1.25, the operand is 4 and, under the oracle number
of the word 0.5, the output at 0 is -0.75 (outs_xs), and lnorm8 at k = 3 with
ε = 10.75, where the real operand is 5.25 + 10.75 = 16, under the oracle number of 0.25.
rmsnorm_iota is statement 6 at 1, …, 2 ^ k along halveFin k with N = 2 ^ k, for every
k ≤ 24, with the inputs' mean of squares (2 ^ k + 1) (2 · 2 ^ k + 1) / 6 in place of M, and
lndev_num closes its premise … < 1 too. Its instances are rmsnorm_xs at k = 2 with
ε = 8.5, where the mean of squares word is 7.5, the operand 16 and, under the oracle number
of 0.25, the output at 0 is 0.25, and rmsnorm8 at k = 3 with ε = 38.5, where the real
operand is 25.5 + 38.5 = 64, under the oracle number of 0.125. The finiteness and normality
premises are discharged by evaluation (decide +kernel). var_xs_eq (V = 5 / 4) and
msq_xs_eq (M = 15 / 2) are the instances at 4 of
var_iota and msq_iota (the variance (n ^ 2 - 1) / 12 and the mean of squares
(n + 1) (2 n + 1) / 6 of 1, …, n, MojoProofTest/FloatStats.lean), whose instances at 8 are
var8 and msq8. Statement 8's instances, with the offset 1 and the tactic rmsoff_num for
its premise on the operand's error, are rmsnormOff_bf16 at k = 2 with ε = 8.5, bfloat16
weights 1 and the order multiplied before the cast, and rmsnormOff_f16 at k = 3 with
ε = 38.5, binary16 weights 1 and the cast first. Each instance is one example and
lnorm_iota and rmsnorm_iota cover only the inputs 1, …, 2 ^ k; none is a claim about a
program.
Proofs
- Each square is the rounding of
x j ^ 2(FloatLib'sModel.toReal_mul_eq_roundAt), so within(u, η)of it;sum_mixedErrorsums them along the tree; FloatLib'sModel.mixedError_toReal_divdivides by the exactN. - FloatLib's
MixedError.relative_of_le_absmakes the operand's error relative, thenMixedError.sqrt,MixedError.inv(which needsδ < 1) andMixedError.transwithw's relative error. - One FloatLib lemma per operation:
Model.mixedError_toReal_subwith the mean's absolute error,Model.mixedError_toReal_multwice andModel.mixedError_toReal_add, orModel.mixedError_toReal_fmafor the fused pair. wmean_errorfor the mean,wvar_errorandModel.mixedError_toReal_addfor the operand,Sem.rsqrt_closeand statement 2 for the reciprocal, statement 3, andlayerNorm γ β ε x j = (x j - 𝔼 x) · (1 / √(V + ε)) · γ j + β j.Model.mixedError_toReal_multwice.- Statement 1 and
Model.mixedError_toReal_addfor the operand,Sem.rsqrt_closeand statement 2, then statement 5. - In the kernel's order,
Model.mixedError_toReal_mul,Model.mixedError_toReal_addandModel.mixedError_toReal_castper operation, each cast bounded as a rounding. - As 6, then statement 7.
Not proved
- The Welford words' fused multiply-adds.
lnormtakes the words ofwelford, which rounds every product, while the kernel's merge has twofmul contractproducts feedingfadd contract(lines 263-264 and 266-268 of the file above) and so has its loop's update (lines 151-152 and 155-156), each pair free to fuse.fusedcovers only the output's last multiplication and addition (lines 515-516). The merge and the loop are bounded on their own for every choice of contraction at each node and each iteration (welfordC_errorandwloop_error, docs/proofs/float-welford.md), not composed intolnormhere. - RMS norm's squares fused into the additions that sum them.
- The cast of a narrower input dtype into binary32 before the squares: statement 8 takes the binary32 words the kernel computes with. The casts of the weight, the offset and the output are bounded as roundings, also where they are exact (a widening).
- That a grid run of a kernel computes these words.
- Any bound when the output is infinite or a NaN.