Docs / Proofs

Float bounds of reductions: any tree, butterflies of any width, block trees, mean, variance, max-subtracted exponential sums, the compiled exponential and log-softmax

Lean: MojoProof/Float/Tree.lean, MojoProof/Float/Halve.lean, MojoProof/Float/Stats.lean, MojoProof/Float/ExpSum.lean, MojoProof/Float/Ex2.lean and the additions to MojoProof/Sem/Bfly.lean and MojoProofTest/Bfly.lean. Instances: MojoProofTest/FloatTree.lean, MojoProofTest/FloatStats.lean and MojoProofTest/FloatEx2.lean. Welford's one-pass mean and variance are in docs/proofs/float-welford.md, layer norm's and RMS norm's outputs in docs/proofs/float-norm.md. Floats are FloatLib's Model fmt words; u = Model.unitRoundoffAt fmt is FloatLib's unit roundoff and η = Model.underflowErrorAt fmt its underflow allowance. MixedError x x₀ r a is FloatLib's |x - x₀| ≤ r · |x₀| + a with r, a ≥ 0 (FloatLib/Numerics/MixedError/Basic.lean).

Every statement here is about words and the trees of 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 (see "Not proved"). The statements hold in every IEEE format (fmt.isIEEE = true), for every length and every tree as stated; none of them is about one program.

The shape of every bound

Each headline result has the shape of a kernel theorem's float clause (matmul_error in MojoProofTest/NaiveKernel.lean): for every input meeting the premises, if the computed word is finite then its real value is within the stated bound of the exact real value, and that exact value is TorchLean's real specification where TorchLean has one. Nothing is claimed when the computed word is infinite or a NaN.

Statements

Any tree (MojoProof/Float/Tree.lean)

graft t f replaces each leaf j of a FloatLib ReductionTree by the tree f j: the tree of a reduction whose inputs are themselves reductions (a block reduction over the warps' butterflies). FloatLib has no such operation.

roundsTree r ps c and rounds combine r ps v c are rounds of a per-cell step over cells numbered by Nat: in round p, cell c's new value is the tree r p c of cell numbers evaluated at the values before the round. A block's shared-memory halving round is such a step. roundsTree r ps c is defined by recursion on the rounds: the leaf c with no rounds, and after p :: ps the tree after ps with each cell c' replaced by the first round's tree r p c'.

  1. eval_graft, leaves_graft, depth_graft: for every carrier, combining operation and pair t, f, evaluating graft t f is evaluating t at the values of the f j; its leaves are those of the f j, j along t's leaves; if every f j at a leaf of t has depth d, its depth is t.depth + d. eval_roundsTree: cell c's value after the rounds is the evaluation of roundsTree r ps c.
  2. leaves_graft_range: if t's leaves are a permutation of 0, …, W - 1 and the leaves of each f w, w < W, are a permutation of b·w, …, b·w + b - 1, the leaves of graft t f are a permutation of 0, …, W·b - 1.
  3. finiteEval_of_isFinite, isFinite_of_mem_leaves: in every IEEE format, a tree of Model.add whose root is finite is FloatLib's Model.ReductionTree.FiniteEval (every node's sum finite), and every leaf of such a tree is finite.
  4. eval_error: for every carrier, combining operation, values, map interpret to the reals, u ≥ 0, tree t over any index type and list S such that t's leaves are a permutation of S: if every combination at a node of t is within u · (|x| + |y|) of its operands' exact sum, the result is within ((1 + u) ^ t.depth - 1) · Σ_{j∈S} |v j| of Σ_{j∈S} v j. It is FloatLib's ReductionTree.abs_eval_sub_exact_le_depth with the sums taken over S.
  5. sum_error: in every IEEE format, for every tree t whose leaves are a permutation of S and values v: if t.eval Model.add v is finite, every v j, j ∈ S, is finite and the result is within ((1 + u) ^ t.depth - 1) · Σ_{j∈S} |v j| of Σ_{j∈S} v j. It rests on FloatLib's Model.ReductionTree.abs_toReal_eval_sub_sum_le_depth and statement 3.
  6. sum_mixedError: the same tree with inputs that approximate exact reals w j with mixed errors (r j, a j) when finite: the result has the absolute error ((1 + u) ^ t.depth - 1) · Σ_{j∈S} |w j| + (1 + u) ^ t.depth · Σ_{j∈S} (r j · |w j| + a j) against Σ_{j∈S} w j. This is the tree form of FloatLib's left-to-right Model.mixedError_toReal_foldl_add_absolute, with the depth in place of the number of additions.

Butterflies of any width and order (MojoProof/Sem/Bfly.lean)

  1. leaves_bfly: for every k, every list of offsets ms that is a permutation of 1, 2, 4, …, 2 ^ (k - 1) (any order of the rounds) and every lane l < 2 ^ k, the leaves of bflyTree ms l are a permutation of 0, …, 2 ^ k - 1. leaves_warp is its case k = 5 at warpOffsets.
  2. bfly_warp_error and bfly_add_error keep their statements (bfly.md, statements 5 and 6) and are statement 4 and statement 5 at t = bflyTree ms l, S = 0, …, w - 1. With statement 7, their width premise holds at every width 2 ^ k and every order of the offsets.

Block reductions (MojoProof/Float/Halve.lean)

halveStep s c is the tree of cell c's value after the shared-memory halving round with stride s: a cell c < s combines its value with cell c + s's, its own first; the other cells keep theirs. The strides 2 ^ (k - 1), …, 2, 1 are the butterfly's offsets bflyOffsets k (MojoProof/Sem/Bfly.lean), so the halving loop over 2 ^ k cells is rounds combine halveStep (bflyOffsets k) and cell 0's tree is roundsTree halveStep (bflyOffsets k) 0.

  1. leaves_halve, depth_halve: for every k, cell 0's leaves after the halving rounds over 2 ^ k cells are a permutation of 0, …, 2 ^ k - 1 and its depth is k.
  2. halve_error: in every IEEE format, for every k and values: if cell 0's value after the halving rounds of Model.add is finite, every input below 2 ^ k is finite and the value is within ((1 + u) ^ k - 1) · Σ_{j<2^k} |v j| of Σ_{j<2^k} v j (statement 5).
  3. block_error: in every IEEE format, for every tree T whose leaves are a permutation of the W warps, offsets ms that are a permutation of 1, …, 2 ^ (j - 1) and values of the threads (thread 2 ^ j · w + l is lane l of warp w): if the sum along T of each warp's lane 0 after its butterfly rounds is finite, every value below W · 2 ^ j is finite and the sum is within ((1 + u) ^ (T.depth + j) - 1) · Σ |v| of Σ v. The two stages are one tree, graft T (fun w => (bflyTree ms 0).map (2 ^ j · w + ·)), whose depth is the sum of the two stages' depths. With T cell 0's halving tree over 2 ^ k warps (statement 9) the depth is k + j.

Mean and variance, two passes (MojoProof/Float/Stats.lean)

mean t x N is the first pass: the n words x summed by Model.add along a tree t, divided by a word N that denotes n. variance t₁ t₂ x N is the second: each difference of an input with the mean word mean t₁ x N by Model.sub, squared by Model.mul, summed along a tree t₂ and divided by N. Each tree's leaves are a permutation of the n indices (List.finRange n); the two trees are independent and of any shape. meanErr, devErr and varErr are the closed forms of the absolute errors of the mean, of each difference and of the variance.

  1. mean_error: in every IEEE format, for every n, tree t, inputs x : Fin n → Model fmt and finite word N whose real value is n: if the mean word is finite, every input is finite and the mean is within MixedError … u (meanErr u η t.depth x) of the exact mean 𝔼 j, x j (Mathlib's Finset.expect), where `meanErr u η d x = (1 + u) · ((1 + u) ^ d - 1) · 𝔼 j, |x j|
    • η`.
  2. var_error: under the same premises on N, for every two trees t₁, t₂: if the variance word is finite, it is within MixedError … u (varErr u η t₁.depth t₂.depth x) of TorchLean's population variance TorchLean.var of the inputs' real values. 𝔼 and TorchLean.var are the two statistics of TorchLean's real layer norm TorchLean.TransformerBlock.layerNorm.

The same module has RMS norm's mean of squares msq t x N and its bound msq_error (docs/proofs/float-norm.md, statement 1).

Max-subtracted exponential sums and softmax (MojoProof/Float/ExpSum.lean)

The maximum is a tree of Model.maximumNumber (LLVM's maxnum) over the inputs. expTerm b x m j is FloatLib's correctly rounded Model.Transcendentals.CorrectlyRounded.exp, with the search budget b, of the rounded difference Model.sub (x j) m; the terms are summed along any tree whose leaves are a permutation of the n indices; a softmax entry is Model.div of term j by the sum. expSumErr u η d y is the closed form of the sum's absolute error, `((1 + u) ^ d - 1) · Σ_j exp (y j) + (1 + u) ^ d · Σ_j (((1 + u) · exp (u · |y j|) - 1) · exp (y j)

  1. max_exact: in every format, for every tree over any index type and inputs finite at its leaves, the tree's maximum is finite, its real value is one input's and it is at least every input's: the maximum is exact.
  2. expTerm_error: in every IEEE format, if the difference Model.sub (x j) m and the term are finite, the term is within MixedError … ((1 + u) · exp (u · |x j - m|) - 1) η of exp (x j - m).
  3. expSum_error: in every IEEE format, for every n, tree t, inputs and word m: if every difference Model.sub (x j) m is finite and the sum of the terms along t is finite, the sum is within MixedError … 0 (expSumErr u η t.depth (x - m)) of Σ_j exp (x j - m).
  4. softmax_error: in every IEEE format, for every n, tree t, inputs, word m whose real value is some input's (the maximum of statement 14, or any input) and index j, with A the absolute error of statement 16: if every difference is finite, A < 1, the sum is finite and the entry is finite, the entry is within MixedError … ((1 + u) · ((r + A) / (1 - A)) + u) ((1 + u) · η / (1 - A) + η) of TorchLean's real softmax TorchLean.TransformerBlock.softmax Finset.univ x j, with r = (1 + u) · exp (u · |x j - m|) - 1. The exponential of -∞ is +0, a finite word, so the differences' finiteness is a premise and not a consequence of the entry's.

The compiled exponential (MojoProof/Float/Ex2.lean)

Mojo's exp on float32 is not a correctly rounded exponential. Its NVPTX lowering of exp (x - m) is fsub contract, fmul contract by the word 0x3FB8AA3B of log₂ e (log2e) and the instruction ex2.approx.ftz.f32 as inline assembly (the standard library's math.exp). A subtraction followed by a multiplication cannot contract, so the instruction's operand is Model.mul (Model.sub x m) log2e. PTX ISA 9.5 §9.7.3.21 states its accuracy: at most 2 ulp from the correctly rounded result across the full range of inputs, with subnormal inputs and results flushed to zero of their sign. Its consequence at a finite operand a < 128 is the contract Ex2Close a w: the word w is finite and within ex2Err · 2 ^ a + ex2Tiny of 2 ^ a, with ex2Err = 3 · 2 ^ -23 (two ulp of a normal value c are at most 2 ^ -22 · c, and the correctly rounded c is within 2 ^ -24 · 2 ^ a of 2 ^ a) and ex2Tiny = 2 ^ -126 (a flushed result, a result near the subnormal range or the 1 of a flushed subnormal operand). The contract is an assumption on the hardware, as rsqrt.approx's is; the execution model stops at inline assembly. ex2Rel u η ρ y = (1 + ex2Err) · exp (((1 + u) · (u + ρ + u ρ) + u) · |y| + η · log 2) - 1 is one term's relative error.

  1. log2e_close: the word log2e (12102203 / 2 ^ 23) is within MixedError … 2 ^ -26 0 of 1 / log 2.
  2. ex2_error: for every binary32 multiplier word L within MixedError … ρ 0 of 1 / log 2 and words x, m, w: if the operand Model.mul (Model.sub x m) L is finite and w meets the contract there, w is within MixedError … (ex2Rel u η ρ (x - m)) ex2Tiny of exp (x - m).
  3. ex2Sum_error: for every n, tree t whose leaves are a permutation of the n indices, such L, words x, word m and term words e: if every operand is finite, every e j meets the contract at its operand and the sum of the e j along t is finite, the sum is within MixedError … 0 (((1 + u) ^ d - 1) · Σ_j exp (y j) + (1 + u) ^ d · Σ_j (ex2Rel u η ρ (y j) · exp (y j) + ex2Tiny)) of Σ_j exp (x j - m), with y j = x j - m and d = t.depth: statement 16's form with the compiled term's error.

Log-sum-exp and log-softmax (MojoProof/Float/ExpSum.lean)

lse t b c x m is Model.add m (log s) and logSoftmax t b c x m j is Model.sub (Model.sub (x j) m) (log s), with s the sum of statement 16 and log FloatLib's correctly rounded Model.Transcendentals.CorrectlyRounded.log with the search budget c. The entry is in the order of Mojo's three-pass CPU log-softmax (max/kernels/src/nn/softmax.mojo, _softmax_3_pass_base with identity, exp, log and sub): it stores x j - m, sums the exponentials and subtracts the logarithm of the sum. With S = Σ_j exp (x j - m) and A the absolute error of statement 16:

  1. lse_error: in every IEEE format, for every n, tree t, budgets, inputs and word m whose real value is some input's: if every difference is finite, A < 1, the sum is finite and lse is finite, it is within `MixedError … u ((1 + u) · (u · log S + (1 + u) · -log (1 - A)
    • η))oflog Σ_j exp (x j)`.
  2. logSoftmax_error: under the same premises with the entry j finite in place of lse, the entry is within MixedError … u ((1 + u) · (u · |x j - m| + (u · log S + (1 + u) · -log (1 - A) + η))) of x j - log Σ_i exp (x i). TorchLean has no log-softmax over the reals as a function of Fin n → ℝ in the modules built here; its TorchLean.Roundoff.CrossEntropy.log_probs_reference (TorchLean/Verify/RuntimeApprox/Roundoff/CrossEntropy.lean, not built here) states its reference in this real form, which is written out here rather than defined a second time.

The relative part u is of the result itself; the absolute part grows with log S ≤ log n and, through -log (1 - A), with the sum's error.

Masked inputs (MojoProof/Float/ExpSum.lean)

A masked input is -∞: less a finite m, its difference is -∞, whose correctly rounded exponential is +0, an exact zero of the sum. maskErr U u η d y is expSumErr's form with both sums over the kept indices U, and expSumErr is maskErr with every index kept.

  1. expTerm_negInf: in every format, if the difference Model.sub (x j) m is -∞ (infinite with its sign bit set), the term expTerm b x m j is the word +0.
  2. maskSum_error: in every IEEE format, for every n, tree t, inputs, word m and set U of kept indices: if the difference is finite at every j ∈ U and -∞ at every other index and the sum of the terms along t is finite, the sum is within MixedError … 0 (maskErr U u η t.depth (x - m)) of Σ_{j ∈ U} exp (x j - m).
  3. maskSoftmax_error: in every IEEE format, for every n, tree t, inputs, set U, word m whose real value is some kept input's and index j ∈ U, with A = maskErr U u η t.depth (x - m): if the differences are as in statement 24, A < 1, the sum is finite and the entry is finite, the entry is within statement 17's mixed error with this A of TorchLean's real softmax over the kept indices TorchLean.TransformerBlock.softmax U x j.

Statements 16 and 17 are statements 24 and 25 with every index kept, and are proved so.

The entry in the kernel's order (MojoProof/Float/ExpSum.lean)

Mojo's GPU softmax computes an entry as e_j * (1 / s): the reciprocal of the sum rounded, then the product, where statements 17 and 25 take one Model.div e_j s. With r = termErr u (x j - m) = (1 + u) · exp (u · |x j - m|) - 1, the term's relative error of statement 15, and q = recipErr u A = (1 + u) · (A / (1 - A)) + u, the reciprocal's:

  1. softmaxRecip_error: in every IEEE format, for every n, tree t, inputs, set U, word m whose real value is some kept input's and index j ∈ U, under statement 25's premises on the differences, A < 1 and the sum: if the entry Model.mul (expTerm b x m j) (Model.div 1 s) is finite, it is within MixedError … ((1 + u) · (r + q + r · q) + u) ((1 + u) · ((1 + q) · η + (1 + r) · η · exp (x j - m) + η²) + η) of TorchLean's real softmax over the kept indices TorchLean.TransformerBlock.softmax U x j; U = Finset.univ is the unmasked softmax of statement 17.

Instances (MojoProofTest/FloatTree.lean, MojoProofTest/FloatStats.lean, MojoProofTest/FloatEx2.lean)

Each headline statement has one binary32 instance whose premises are discharged by evaluation (decide +kernel): chain_error, chain_mixed (statements 5 and 6 at a loop), halve_ones (statement 10 over 8 cells), block_ones and block_ones8 (statement 11 with two warps and with four groups of 8 lanes), mean_xs and var_xs (statements 12 and 13 at the inputs 1, 2, 3, 4, the mean along a loop and the squares along a pairwise tree), max_xs, term_xs, expSum_xs (statements 14 to 16), softmax_ones (statement 17 at four inputs 1 shifted by their maximum, with A < 1 by norm_num on FloatLib's binary32 constants in errA_lt), lse_ones and logSoftmax_ones (statements 21 and 22 under softmax_ones's premises, with the correctly rounded logarithm of the sum 4), and term_masked, maskSum_masked and maskSoftmax_masked (statements 23 to 25 at the inputs 1, -∞, 1, -∞ less 1, kept at 0 and 2, with A < 1 in errM_lt). Statement 26 has two, softmaxRecip4 and softmaxRecip8, at four and eight inputs 1 less 1 along halveFin 2 and halveFin 3, every index kept, each with A < 1 closed by the tactic recip_num. ex2_ones and ex2Sum_ones are statements 19 and 20 at four inputs 1 less the word 1 with L = log2e: the operand is +0, at which the word 1 meets the contract (zero_close). Each is one example, not a claim about a program.

block_halve is statement 11 at the inputs 1 for every k halving rounds after j butterfly rounds, its premise the finiteness of the sum word; block_ones and block_ones8 are its cases at (1, 5) and (2, 3), declared by the command theorem name := e, a theorem without a type (MojoProofTest/FloatTree.lean), which states the type of the term e. The inputs xs are iota 4, the words of 1, …, n at n = 4, and pairs is halveFin 2, cell 0's halving tree over 2 ^ k cells with its leaves in Fin (2 ^ k) at k = 2; iota_toReal, iota_bound, var_iota and msq_iota give their real values, bound, variance (n ^ 2 - 1) / 12 and mean of squares (n + 1) (2 n + 1) / 6 at every n ≤ 2 ^ 24 (MojoProofTest/FloatStats.lean). four_toReal and eight_toReal are nat32_toReal, the real value of the word of a natural number m ≤ 2 ^ 24, at 4 and 8.

Proofs

  1. Induction on t. The depth uses that every f j has the same depth at t's leaves. eval_roundsTree: induction on the rounds, with eval_graft at the first one.
  2. Statement 1 rewrites the leaves to a flatMap; a permutation of t's leaves permutes the flatMap, and the flatMap over 0, …, W - 1 of the blocks b·w + · is 0, …, W·b - 1 (induction on W, List.range_add).
  3. A finite sum has finite operands (FloatLib's Model.isFinite_of_isFinite_add); induction.
  4. FloatLib's depth bound, eval_add_eq_sum and the permutation on both sums.
  5. Statement 3 on t.map v, then FloatLib's IEEE depth bound and the permutation.
  6. Statement 5, then the triangle inequality with each leaf's error, bounding Σ |v j| by Σ |w j| + Σ (r j · |w j| + a j).
  7. The leaves of bflyTree ms l are L ms XOR l, with L the leaves of lane 0. A round appends L XOR m to L, two rounds commute up to a permutation, so L depends only on the multiset of the offsets; at 1, 2, …, 2 ^ (k - 1) in order L is 0, …, 2 ^ k - 1 (List.range_add), and XOR with l < 2 ^ k permutes that list (it has no duplicates and stays below 2 ^ k).
  8. Statement 4 or 5 rewritten by eval_bflyTree and depth_bflyTree.
  9. Induction on k: by the definition of roundsTree, cell 0's tree over bflyOffsets (k + 1) is its tree over bflyOffsets k with each leaf c < 2 ^ k replaced by the pair c, c + 2 ^ k, so its leaves are those of 0, …, 2 ^ k - 1 followed by those shifted by 2 ^ k (List.flatMap_append_perm, List.range_add), and its depth grows by 1 (statement 1).
  10. Statement 5 at cell 0's tree, with eval_roundsTree and statement 9.
  11. Statement 1 (eval_graft, FloatLib's eval_map, eval_bflyTree) for the value, statement 2 with statement 7 at lane 0 for the leaves, depth_graft for the depth, then statement 5.
  12. Statement 5 as an absolute MixedError of the sum, then FloatLib's quotient rule Model.mixedError_toReal_div with the exact divisor N (relative error 0); (Σ_j x j) / n = 𝔼 j, x j.
  13. FloatLib's Model.mixedError_toReal_sub for the difference with the mean of statement 12 and Model.mixedError_toReal_mul for the square give each term's error against (x j - 𝔼 x) · (x j - 𝔼 x); statement 6 sums them along t₂; the quotient rule divides by N; TorchLean.var is the mean of those squares.
  14. Induction on the tree: at finite operands maximumNumber is maximum (FloatLib's maximumNumber_eq_maximum_of_not_isNaN), finite and the real max (FloatLib's toReal_maximum_eq_max_of_isFinite); max_choice picks the side.
  15. FloatLib's toReal_exp_of_isFinite (a finite correctly rounded exponential of a finite argument is the rounded real exponential), Model.mixedError_toReal_sub for the difference and MixedError.trans_exp.
  16. Statement 24 with every index kept.
  17. Statement 25 with every index kept.
  18. log2e's value by evaluation, then Mathlib's Real.log_two_gt_d9 and Real.log_two_lt_d9: |log2e · log 2 - 1| ≤ 2 ^ -26.
  19. Model.mixedError_toReal_sub for the difference, Model.mixedError_toReal_mul with L's error and FloatLib's MixedError.const_mul by log 2 put log 2 · p within ((1 + u) · (u + ρ + u ρ) + u, η · log 2) of x - m; then 2 ^ p = exp (log 2 · p) and FloatLib's MixedError.trans_exp with the contract.
  20. Statement 6 with statement 19 at every leaf.
  21. Statement 17's relative error A of the sum (shared by a private lemma), FloatLib's toReal_pos_of_isFinite_log and toReal_log_of_isFinite (a finite correctly rounded logarithm of a finite input is the rounded real logarithm of a positive one), then FloatLib's MixedError.trans_log, which needs A < 1, for log S, and Model.mixedError_toReal_add with the exact m; log S = log Σ_j exp (x j) - m (Real.exp_sub, Real.log_div) and log S ≥ 0.
  22. As statement 21, with Model.mixedError_toReal_sub for the difference x j - m and for the entry.
  23. FloatLib's correctly rounded exponential of an infinite word with its sign bit set is +0 by its definition (an infinite word is not a NaN, FloatLib's Model.isNaN_eq_false_of_isInfinite_eq_true).
  24. Statement 6 with the weights exp (x j - m) at the kept leaves (statement 15) and 0 at the masked ones, whose terms are +0 (statement 23), so of real value 0 with error (0, 0); a sum over every index of terms that are 0 outside U is the sum over U (Mathlib's Finset.sum_ite_mem).
  25. The term at the kept k is exp 0 = 1 and every term is positive, so the exact sum over U is at least 1 and FloatLib's MixedError.relative_of_le_abs makes the sum's absolute error A a relative one; then Model.mixedError_toReal_div, TorchLean's softmax_eq_exp_sub_div over U (the softmax is exp (x j - m) / Σ_{i ∈ U} exp (x i - m) for every m) and η / ((1 - A) · Σ) ≤ η / (1 - A).
  26. Statement 25's relative error A of the sum (the same private lemma); FloatLib's Model.mixedError_toReal_div with the exact numerator 1 (Model.toReal_posOne) gives the reciprocal's error (q, η), and Model.mixedError_toReal_mul with statement 15's term error the entry's (both factors are finite by Model.isFinite_of_isFinite_mul); then softmax_eq_exp_sub_div, a / Σ = a · (1 / Σ) and |1 / Σ| ≤ 1, as Σ ≥ 1.

Not proved

These are about other carriers or orders and are not reproved here: