The warp butterfly as a reduction tree (bflyRounds, bflyRoundsAt, bflyOffsets, bflyTree, eval_bflyTree, depth_bflyTree, leaves_bfly, leaves_offsets, leaves_warp, shflSource_group, shflSource_bfly, bfly_warp_error, bfly_add_error, bflyRounds_congr)
Lean: proved, for every carrier, combining operation, offsets, values and lane as stated below,
in MojoProof/Sem/Bfly.lean. Instances and twins: MojoProofTest/Bfly.lean. The tree type is
FloatLib's FloatLib.Numerics.ReductionTree (FloatLib/Numerics/Reduction/Tree.lean), and the
error bounds are FloatLib's ReductionTree.abs_eval_sub_exact_le_depth
(FloatLib/Numerics/Reduction/Error.lean) and, for IEEE additions,
Model.ReductionTree.abs_toReal_eval_sub_sum_le_depth
(FloatLib/Floats/Formats/BinaryInterchange/Reduction/Tree.lean), stated for every tree as
eval_error and sum_error in MojoProof/Float/Tree.lean (float-reductions.md, statements 4 and
5). The statements are about the rounds as functions of the lanes' values; that a grid run of a
kernel computes them is not proved (see "Not proved").
What the kernels compute
Both kernels of max/kernels/src/linalg/gemv.mojo in tests/corpus/gemv/ end their warp reduction
with five butterfly rounds. In gemv_kernel (gemv_f32_gemv_kernel_float32_float32_float32_False_b79a852b.nvptx.ll),
each warp computes one row: lane l (%40, laneid) adds the products of the columns 32 i + l
into %56 (blocks %54 to %77), and then block %78 runs
%79 = phi float [ %56, %59 ]
%80 = bitcast float %79 to i32
%81 = call i32 @llvm.nvvm.shfl.sync.bfly.i32(i32 -1, i32 %80, i32 16, i32 31)
%82 = bitcast i32 %81 to float
%83 = fadd contract float %79, %82
and the same three lines (bitcast, shfl.sync.bfly, bitcast, then fadd contract of the
lane's own value and the shuffled one) with the offsets 8, 4, 2 and 1, ending in %99, which lane
0 (%41 = icmp eq i64 %40, 0) stores to the row's output (%37). gevm_kernel
(gemv_f32_gevm_kernel_float32_float32_float32_1024_1308aaae.nvptx.ll) runs the same five rounds
on the value %67 it loads from shared memory, ending in %87.
In the semantics (MojoProof/Sem/Grid.lean): a shfl.sync with the full member mask (-1) and
clamp 31 completes when every lane of the warp waits at it, and lane l receives the operand of
lane shflSource .bfly l m 31, which is l ^^^ m for l, m < 32 (shflSource_bfly). A shuffle
within groups of 2 ^ k lanes has the clamp (32 - 2 ^ k) · 256 + 31 (the segment mask in bits
8-12), and its source is l ^^^ m for m < 2 ^ k (shflSource_group); the corpus's kernels use
clamp 31 only. A
bitcast from float to i32 and back gives the same bits. The fadd contract's operands are a
phi or an fadd result and a bitcast, never an fmul contract result, so contract
(MojoProof/Sem/Float.lean) is FloatLib's Model.add at every oracle number, through deliver,
which changes only a NaN result.
Statements
Write bflyRound combine m v for the values after one round with offset m: lane l gets
combine (v l) (v (l ^^^ m)), its own value first, as in fadd contract float %79, %82.
bflyRoundsAt F ms v is the rounds with offsets ms, first to last, in which lane l of round
r (counted from 0) combines by F r l; bflyRounds combine ms v is its case
F r l = combine. bflyOffsets k is 2 ^ (k - 1), ..., 2, 1, the k rounds of a group of
2 ^ k lanes, and warpOffsets is by definition bflyOffsets 5, the offsets 16, 8, 4, 2, 1.
bflyTree ms l is a ReductionTree Nat whose leaves are lane numbers: with
no rounds lane l's tree is the leaf l, and a round with offset m makes lane l's tree the
node of its own tree and lane l ^^^ m's.
eval_bflyTree: for every carrierβ, everycombine : β → β → β, offsetsms, valuesv : Nat → βand lanel, evaluating lanel's tree withcombineat the leaves' values is lanel's value after the rounds:(bflyTree ms l).eval combine v = bflyRounds combine ms v l.depth_bflyTree: for all offsets and every lane,(bflyTree ms l).depth = ms.length; at a warp, 5.leaves_bfly: for everyk, offsetsmsthat are a permutation of1, 2, 4, ..., 2 ^ (k - 1)(the rounds in any order) and lanel < 2 ^ k, the leaves ofbflyTree ms lare a permutation of0, ..., 2 ^ k - 1: each lane's value is a leaf exactly once.leaves_offsets: its case atbflyOffsets k, for everykand lanel < 2 ^ k;leaves_warpisleaves_offsetsatk = 5, for every lanel < 32.shflSource_group: for everyk ≤ 5, lanel < 32and offsetm < 2 ^ k,shflSource .bfly l m ((32 - 2 ^ k) · 256 + 31) = l ^^^ m: a butterfly shuffle within groups of2 ^ klanes reads lanel ^^^ m.shflSource_bflyis its casek = 5: for alll, m < 32,shflSource .bfly l m 31 = l ^^^ m.bfly_warp_error: for every carrier,combine, valuesv, mapinterpret : β → ℝ,u ≥ 0, offsetsms, laneland widthwsuch that the leaves ofbflyTree ms lare a permutation of0, ..., w - 1: if at every node of lanel's tree the combination of the operandsx,yit meets is withinu · (|x| + |y|)of their exact sum (ReductionTree.AllNodes), then|interpret (bflyRounds combine ms v l) - Σ_{j<w} interpret (v j)|is at most((1 + u) ^ ms.length - 1) · Σ_{j<w} |interpret (v j)|. AtwarpOffsets,l < 32andw = 32(statement 3) the bound is((1 + u) ^ 5 - 1) · Σ_{j<32} |interpret (v j)|; at2 ^ klanes and offsets as in statement 3,((1 + u) ^ k - 1) · Σ_{j<2^k} |interpret (v j)|.bfly_add_error: in every IEEE format, for everyv : Nat → Model fmt, offsetsms, laneland widthwsuch that the leaves ofbflyTree ms lare a permutation of0, ..., w - 1: if lanel's valuerafter the rounds ofModel.addis finite, then everyv jwithj < wis finite, and|r - Σ_{j<w} v j| ≤ ((1 + u) ^ ms.length - 1) · Σ_{j<w} |v j|over the real values, withu = Model.unitRoundoffAt fmt. Only the root's sum is assumed finite. At binary32,warpOffsetsandw = 32(statement 3) the bound is((1 + 2 ^ -24) ^ 5 - 1) · Σ_{j<32} |v j|, the binary32 case of statement 5 withcombine = Model.addandinterpret = Model.toReal, under a finite root only.bflyRounds_congr: for every carrier, combining operation, offsets and lane, lanel's value after the rounds depends only on the values of the leaves ofbflyTree ms l: two families that agree there give the same value.
The depth 5 of the bound is ⌈log₂ 32⌉, the least depth of any schedule over 32 inputs
(FloatLib's clog_le_depth); a loop over the 32 values (FloatLib's leftChain) has depth 31.
Proofs
- By induction on
ms, for every starting family of treesTand valueswwith(T l).eval combine v = w lat every lane: a round makes both sides the combination of lanel's and lanel ^^^ m's, which are equal by the hypothesis. Start withT = leaf,w = v. - The same induction: if every lane's tree has depth
d, after a round every lane's tree has depthmax d d + 1 = d + 1. - As multisets, lane
l's leaves are lane 0's leaves XORl(induction on the rounds from the last one: a round with offsetmmakes lanel's leaves its own and lanel ^^^ m's, and XOR is associative), and a round with offsetmadds lane 0's leaves XORmto them. Two rounds commute (List.Perm.foldl_eq'), so lane 0's leaves depend only on the multiset of the offsets; at1, 2, ..., 2 ^ (k - 1)in order they are0, ..., 2 ^ k - 1, by induction onkwith2 ^ k ^^^ j = 2 ^ k + jforj < 2 ^ k. XOR withl < 2 ^ kpermutes that list: it has no duplicates (XOR withlis injective) and stays below2 ^ k(Nat.xor_lt_two_pow).leaves_offsets:bflyOffsets kis the reverse of1, 2, ..., 2 ^ (k - 1), so a permutation of it (List.reverse_perm). - Unfold
shflSource: the clamp's low five bits are 31 and its segment mask is32 - 2 ^ k = 2 ^ k (2 ^ (5 - k) - 1), whose bits are those fromkto 4, andm % 32 = m. The largest lane(l &&& seg) ||| (31 &&& (31 ^^^ seg))has every bit belowkandl's bits fromkto 4;l ^^^ mhasl's bits fromkon (m < 2 ^ k) and no bit from 5 on (l < 32), so it is at most the largest lane, bit by bit (Nat.le_of_testBit), and in range. eval_error(FloatLib'sabs_eval_sub_exact_le_depthwith the sums over the permuted leaves) at lanel's tree, rewritten by statement 1 on the computed side and statement 2 for the depth.sum_errorat lanel's tree, rewritten the same way. In it, a finite IEEE sum has finite operands (FloatLib'sModel.isFinite_of_isFinite_add), so by induction on the tree a finite root gives FloatLib'sFiniteEvalof the tree mapped byv, the leaves of such a tree are finite, and FloatLib'sModel.ReductionTree.abs_toReal_eval_sub_sum_le_depthbounds it.- Statement 1 on both sides, then induction on the tree: a leaf's value is the family's at the leaf, and a node's leaves are its two subtrees'.
Instances and twins
lane0_leaves: lane 0's leaves are0, 16, 8, 24, 4, 20, 12, 28, 2, ..., the lanes in bit-reversed order, by evaluation;bfly_sum: withcombine = (· + ·)onNatandv j = j, every lane ends with0 + ... + 31 = 496, by evaluation (decide +kernel).bfly_twin: with offsets[16, 8, 4, 2, 2](the last round repeated instead of offset 1), lane 0's leaves are not a permutation of0, ..., 31: each even lane occurs twice and no odd lane occurs, so statement 3 needs offsets that are distinct powers of two.leaves_mixed: statement 3 atk = 3, the offsets1, 4, 2(not in descending order) and lane 5: the leaves are a permutation of0, ..., 7; with eight lanes holding1at binary32 (add_mixed), statement 6 bounds lane 5's sum by((1 + u) ^ 3 - 1) · 8.add_ones: statement 6 at binary32,warpOffsets, lane 0 and 32 lanes holding1: the root's sum32is finite by evaluation, so every lane is finite and the bound holds.offsets8,leaves8:bflyOffsets 3is4, 2, 1and lane 0's leaves are0, 4, 2, 6, 1, 5, 3, 7(bydecide); statement 3'sleaves_offsetsatk = 3.shfl8: statement 4 atk = 3, clamp24 · 256 + 31 = 6175: lane 13 with offset 5 reads lane 8.shfl8_twin: with the same clamp and offset 8, outside the group, lane 5 reads itself, not lane5 ^^^ 8 = 13(bydecide), so statement 4 needsm < 2 ^ k.shfl32: statement 4 atk = 5, the warp's clamp0 · 256 + 31 = 31: lane 13 with offset 21 reads lane 24.exact4: statement 5 away from a warp, at the offsets2, 1, lane 3 (leaves3, 1, 2, 0, by evaluation),w = 4and exact addition of reals (u = 0): for everyv, lane 3's value isΣ_{j<4} v j.
Not proved
- That a grid run of
gemv_kernelorgevm_kernelcomputesbflyRoundsof the lanes' values: that needs the run's shuffles to complete with every lane of the warp at the same round (Grid.fire), and both kernels are outside the block checker's fragment from their entry block, so they have no proof path on this branch. - The row's dot product, the butterfly over the 32 strided loops whose
fadd contractmay fuse with thefmul contract(T9), is warp-dot.md: within a closed-form bound of TorchLean's real entry when finite, and FloatLib's reduction along lane 0's tree, up to NaNs, with the contraction off. That a grid run computes it is not proved there either. - Any bound when the result is infinite or a NaN: statement 6 assumes the root's sum finite.