LAWS.bend open laws/TODOs
raw source on the hub · import bendtorch@1.2.0.0/LAWS.bend as LAWS
BendTorch's laws. Each is a claim about every input, not a test case; PROOF.bend proves them, and
bend PROOF.bend --verdict checks the proofs again with Bend's kernel proved in Lean. Plain-language explanations
are in torch/README.md. The laws about linear maps speak about the exact model in torch/spec.bend; the others speak
about the package's own functions.
4 imports
import Base import ./main.bend as T import ./spec.bend as Spec import ./tensor.bend as TT
Laws
law adjoint provedin PROOF.bendsource · line 13 · raw
@w:List<&2, List<&2, Nat>> -> @g:List<&2, Nat> -> @+v:List<&2, Nat> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(g, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.mv(w, v)) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(0xea477ac4399d6f3f46b9d25d99c1900c/spec.mtv(w, g), v) : Nat}LAW (adjoint): the backward pass of a linear layer, dx = Wᵀ·dy, is the adjoint of its forward pass y = W·x: for any W, dy and x, dy·(W·x) == (Wᵀ·dy)·x. This is what makes dx the gradient of the loss with respect to x.
law weight_adjoint provedin PROOF.bendsource · line 20 · raw
@w:List<&2, List<&2, Nat>> -> @g:List<&2, Nat> -> @+v:List<&2, Nat> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(g, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.mv(w, v)) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.frob(0xea477ac4399d6f3f46b9d25d99c1900c/spec.outer(g, v), w) : Nat}LAW (weight_adjoint): the weight gradient dW = dy ⊗ x is the adjoint in W: dy·(W·x) == <dy ⊗ x, W>.
law adjoint_seq provedin PROOF.bendsource · line 28 · raw
@+w1:List<&2, List<&2, Nat>> -> @+w2:List<&2, List<&2, Nat>> -> @+g:List<&2, Nat> -> @+v:List<&2, Nat> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(g, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.mv(w2, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.mv(w1, v))) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(0xea477ac4399d6f3f46b9d25d99c1900c/spec.mtv(w1, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.mtv(w2, g)), v) : Nat}LAW (adjoint_seq): Seq runs the second layer's backward pass, then the first's. For two linear layers that is W1ᵀ·(W2ᵀ·dy), and it is the adjoint of the composed forward pass W2·(W1·x): the chain rule, for linear maps.
law seq_chain provedin PROOF.bendsource · line 39 · raw
@+w1:List<&2, List<&2, Nat>> -> @+w2:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> @+dy:List<&2, Nat> -> @g1:List<&2, List<&2, Nat>> -> @g2:List<&2, List<&2, Nat>> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(dy, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fy(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.SeqL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, x))) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(0xea477ac4399d6f3f46b9d25d99c1900c/spec.bdx(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.bwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.SeqL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fsv(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.SeqL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, x)), dy, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{g1, g2})), x) : Nat}LAW (seq_chain): T.Seq's own code, running two linear layers of the exact model one after the other, gives a backward pass that is the adjoint of its forward pass: dy·y == dx·x, for the y the forward pass returns and the dx its backward pass returns from the state the forward pass saved. adjoint_seq says it of the formulas; this says it of the combinator that composes the layers: Seq feeds the second layer's input gradient to the first, the chain rule.
law fork_sum provedin PROOF.bendsource · line 51 · raw
@+w1:List<&2, List<&2, Nat>> -> @+w2:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> @+dy1:List<&2, Nat> -> @+dy2:List<&2, Nat> -> @g1:List<&2, List<&2, Nat>> -> @g2:List<&2, List<&2, Nat>> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.pdot(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{dy1, dy2}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fy(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ForkL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, x))) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(0xea477ac4399d6f3f46b9d25d99c1900c/spec.bdx(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.bwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ForkL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fsv(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ForkL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, x)), 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{dy1, dy2}, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{g1, g2})), x) : Nat}LAW (fork_sum): T.Fork's own code, giving one input to two linear layers, gives as input gradient the sum of the two layers' input gradients, and that sum is the adjoint of the pair of outputs: dy1·y1 + dy2·y2 == dx·x. A residual block (Fork of the identity and a layer) and attention's q, k, v projections rely on it.
law both_sum provedin PROOF.bendsource · line 65 · raw
@+w1:List<&2, List<&2, Nat>> -> @+w2:List<&2, List<&2, Nat>> -> @+x1:List<&2, Nat> -> @+x2:List<&2, Nat> -> @+dy1:List<&2, Nat> -> @+dy2:List<&2, Nat> -> @g1:List<&2, List<&2, Nat>> -> @g2:List<&2, List<&2, Nat>> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.pdot(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{dy1, dy2}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fy(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.BothL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{x1, x2}))) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.pdot(0xea477ac4399d6f3f46b9d25d99c1900c/spec.bdx(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.bwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.BothL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fsv(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.BothL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{w1, w2}, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{x1, x2})), 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{dy1, dy2}, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{g1, g2})), 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{x1, x2}) : Nat}LAW (both_sum): T.Both's own code, running two linear layers side by side on the two inputs of a pair, gives a backward pass that is the adjoint of its forward pass: dy1·y1 + dy2·y2 == dx1·x1 + dx2·x2. Each layer gets its own input's gradient and nothing crosses between the two sides. Attention applies RoPE to q and k this way (Both(RoPE, Both(RoPE, Id))).
law residual provedin PROOF.bendsource · line 81 · raw
@+w:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> @+dy:List<&2, Nat> -> @g:List<&2, List<&2, Nat>> -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(dy, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fy(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, Nat>>, Unit>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, Nat>>, Unit>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ResL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{Unit{}, w}, Unit{}}, x))) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.dotn(0xea477ac4399d6f3f46b9d25d99c1900c/spec.bdx(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.bwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, Nat>>, Unit>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ResL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{Unit{}, w}, Unit{}}, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.fsv(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, Nat>>, Unit>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Layer.fwd(0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, List<&2, Nat>, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<0xea477ac4399d6f3f46b9d25d99c1900c/main.Two<Unit, List<&2, Nat>>, Unit>, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.ResL, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{Unit{}, w}, Unit{}}, x)), dy, 0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{0xea477ac4399d6f3f46b9d25d99c1900c/main.Two{Unit{}, g}, Unit{}})), x) : Nat}LAW (residual): T.Residual's own code (T.ResidualOf: Seq(Fork(Id, f), Add2)), with a linear layer as f, gives a backward pass that is the adjoint of its forward pass: for y = x + W·x and its dx, dy·y == dx·x. The backward pass hands dy to both branches and adds what comes back, dx = dy + Wᵀ·dy: the gradient goes through the skip connection untouched and through the layer, the rule every block of the Llama models relies on twice. (Over matrices the join is Add2; here it is its twin over vectors, a + b forward and dy to both backward.)
law causal_keys provedin PROOF.bendsource · line 91 · raw
@+i:Nat -> @+hd:Nat -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> {List.length(&2, F32, 0xea477ac4399d6f3f46b9d25d99c1900c/main.dr_row(0xea477ac4399d6f3f46b9d25d99c1900c/main.qrow(i, hd, qs, qc, ks, kc, q, k))) == 1n+i : Nat}LAW (causal_keys): T.Attention reads keys only through T.qrow, and the scores it builds for the query at position i come from keys 0..i, one per key: exactly i + 1 of them. So no key after position i is read, and T.causal_row (law causal) gets the i + 1 scores its condition asks for: past them, the row of probabilities is zeros.
law causal provedin PROOF.bendsource · line 107 · raw
@+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> @p:Array<F32> -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.pr_n(0xea477ac4399d6f3f46b9d25d99c1900c/main.prow(i, t, hd, sc, qs, qc, ks, kc, q, k, p)) == 1n+i : Nat}LAW (causal): in T.Attention, row i of a head's probabilities is a softmax over exactly i + 1 positions. T.prow writes the query's scores from the start of its row and runs the softmax over as many positions as it wrote; the count is the one the softmax's own division loop reports (T.psmc), and this law says it is i + 1, the keys 0..i of causal_keys. P starts as zeros and nothing else writes the row, so its other t - (i + 1) positions stay 0: no probability lands on a later position.
law causal_grad provedin PROOF.bendsource · line 126 · raw
@+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+ds:U32 -> @+dc:U32 -> @+vs:U32 -> @+vc:U32 -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.gr_n(0xea477ac4399d6f3f46b9d25d99c1900c/main.grow(i, t, hd, sc, ds, dc, vs, vc, d, v, p, g)) == 1n+i : Nat}LAW (causal_grad): in T.Attention's backward pass, row i of a head's G (the gradient of its scores) is computed over exactly i + 1 positions. T.grow reads dP's row against values 0..i only (as qrow reads keys 0..i), writes it from the start of its row and runs the softmax's backward pass over as many positions as it wrote; this law says that count is i + 1. G starts as zeros and nothing else writes the row, so its other t - (i + 1) positions stay 0: no gradient reaches a key or a value after the query's position.
law attn_par_fwd provedin PROOF.bendsource · line 145 · raw
@d:Nat -> @gs:0xea477ac4399d6f3f46b9d25d99c1900c/main.Halves<0xea477ac4399d6f3f46b9d25d99c1900c/main.FS> -> @+n:Nat -> @+h:Nat -> @+dm:0xea477ac4399d6f3f46b9d25d99c1900c/main.Dm -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.fpar(d, gs, n, h, dm) == 0xea477ac4399d6f3f46b9d25d99c1900c/main.fleaf(0xea477ac4399d6f3f46b9d25d99c1900c/main.hcat(0xea477ac4399d6f3f46b9d25d99c1900c/main.FS, gs), h, dm, []) : List<&1, Array<F32>>}LAW (attn_par_fwd): T.Attention runs its sequences in a tree of parallel lets (T.fpar: the sequences cut in halves, each half cut again, d levels deep, the leaves' outputs appended). Whatever the depth and however the sequences are split between the two halves, the tree gives exactly what T.fleaf gives running them one after another, in order: the parallel forward pass is the sequential one, and its output does not depend on d or on the thread count.
law attn_par_bwd provedin PROOF.bendsource · line 155 · raw
@d:Nat -> @gs:0xea477ac4399d6f3f46b9d25d99c1900c/main.Halves<0xea477ac4399d6f3f46b9d25d99c1900c/main.BG> -> @+n:Nat -> @+kv:Nat -> @+dm:0xea477ac4399d6f3f46b9d25d99c1900c/main.Dm -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.bpar(d, gs, n, kv, dm) == 0xea477ac4399d6f3f46b9d25d99c1900c/main.bleaf(0xea477ac4399d6f3f46b9d25d99c1900c/main.hcat(0xea477ac4399d6f3f46b9d25d99c1900c/main.BG, gs), kv, dm, []) : List<&1, 0xea477ac4399d6f3f46b9d25d99c1900c/main.DB>}LAW (attn_par_bwd): the same for the backward pass: T.bpar's tree gives each sequence's dQ, dK and dV exactly as T.bleaf, one sequence after another.
law kv_of_group provedin PROOF.bendsource · line 165 · raw
@+j:Nat -> @+i:Nat -> @+k:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.kv_of(Nat.add(Nat.mul(j, 1n+Nat.add(i, k)), i), 1n+Nat.add(i, k)) == j : Nat}LAW (kv_of_group): T.kv_of, the function both passes of T.Attention use to pick the key/value head of query head hi, gives j for every head of group j: query heads j·g + 0 .. j·g + g - 1 (g = 1 + i + k, i < g) all read key/value head j.
law gqa_head provedin PROOF.bendsource · line 174 · raw
@+j:Nat -> @+i:Nat -> @+k:Nat -> {Nat.div(Nat.add(Nat.mul(j, 1n+Nat.add(i, k)), i), 1n+Nat.add(i, k)) == j : Nat}LAW (gqa_head): with g = 1 + i + k query heads per key/value head, query head j·g + i reads key/value head (j·g + i) / g = j, the head of its own group: T.Attention's forward pass picks head hi / g, and its backward pass sums the gradients of heads j·g + 0 .. j·g + g - 1 into key/value head j, so both see the same groups.
law kv_cols provedin PROOF.bendsource · line 183 · raw
@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+sc:F32 -> @+j:Nat -> @+i:Nat -> @+k:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/main.hc_kc(0xea477ac4399d6f3f46b9d25d99c1900c/main.head_cols(0xea477ac4399d6f3f46b9d25d99c1900c/main.Dm{t, 1n+Nat.add(i, k), hd, w, kvw, sc}, Nat.add(Nat.mul(j, 1n+Nat.add(i, k)), i))) == U32.from_nat(Nat.mul(j, hd)) : U32}LAW (kv_cols): the attention code takes the columns each query head reads from T.head_cols, in both passes (the head loops pass no head index further, so they cannot compute other columns). With g = 1 + i + k query heads per key/value head, query head j·g + i reads the key and value columns of head j: j·hd onwards.
law bc_sym provedin PROOF.bendsource · line 196 · raw
@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.bc_ok(a, b, o) == 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.bc_ok(b, a, o) : Bool}LAW (bc_sym): broadcasting does not depend on the order of the operands: a and b broadcast to o exactly when b and a do, so x + y and y + x (and x * y, y * x) accept the same shapes.
law sum_numel provedin PROOF.bendsource · line 204 · raw
@+s:List<&2, Nat> -> @+k:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel(s) == Nat.mul(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.at1(s, k), 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.remove(s, k))) : Nat}LAW (sum_numel): summing over axis k partitions the elements: a tensor of shape s has exactly size(k) times as many numbers as its sum over axis k, for every shape and every k (past the last axis the size counts as 1).
law narrow_rest provedin PROOF.bendsource · line 211 · raw
@+s:List<&2, Nat> -> @+d:Nat -> @+l:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.remove(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.setd(s, d, l), d) == 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.remove(s, d) : List<&2, Nat>}LAW (narrow_rest): narrow (and setting any axis' size) changes only its own axis: with axis d taken out, the shape is what it was.
law narrow_axis provedin PROOF.bendsource · line 218 · raw
@+s:List<&2, Nat> -> @+d:Nat -> @+l:Nat -> @e:{True{} == Nat.is_lt(d, 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.rank(s)) : Bool} -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.at1(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.setd(s, d, l), d) == l : Nat}LAW (narrow_axis): narrowing axis d of a tensor that has it gives that axis exactly the length asked for.
law cat_numel provedin PROOF.bendsource · line 227 · raw
@+s:List<&2, Nat> -> @+d:Nat -> @+x:Nat -> @+y:Nat -> @e:{True{} == Nat.is_lt(d, 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.rank(s)) : Bool} -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.setd(s, d, Nat.add(x, y))) == Nat.add(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.setd(s, d, x)), 0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.setd(s, d, y))) : Nat}LAW (cat_numel): concatenating along an axis the tensor has adds the counts: a tensor whose axis d is x + y long holds as many numbers as the two with x and y.
law mat_numel provedin PROOF.bendsource · line 236 · raw
@+r:Nat -> @+c:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.numel([r, c]) == Nat.mul(r, c) : Nat}LAW (mat_numel): a Tensor<[r, c]> holds the r·c numbers of a Mat<r, c>, so of_mat and to_mat reuse the array as it is.
law permute_rank provedin PROOF.bendsource · line 242 · raw
@+s:List<&2, Nat> -> @p:List<&2, Nat> -> {0xea477ac4399d6f3f46b9d25d99c1900c/tensor.rank(0xea477ac4399d6f3f46b9d25d99c1900c/tensor.permuted(s, p)) == List.length(&2, Nat, p) : Nat}LAW (permute_rank): permuting by p gives as many axes as p has, so a permutation of the axes keeps the rank.
law block_sum provedin PROOF.bendsource · line 253 · raw
@+p:Nat -> @+k:Nat -> @+r:Nat -> {0xea477ac4399d6f3f46b9d25d99c1900c/spec.passes(Nat.add(r, p), 0xea477ac4399d6f3f46b9d25d99c1900c/spec.blocks(0n, Nat.mul(0xea477ac4399d6f3f46b9d25d99c1900c/spec.pw(p), k), 0n)) == 0xea477ac4399d6f3f46b9d25d99c1900c/spec.passes(r, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.blocks(p, k, 0n)) : List<&2, 0xea477ac4399d6f3f46b9d25d99c1900c/spec.Sum>}LAW (block_sum): summing a row in blocks gives the row's own sum, bit for bit. tree_sum adds a row's cells in pairs, pass after pass. The GPU's loss kernels instead give each block of 2^p cells to one leaf, which sums it as a balanced tree of pairs, and then run tree_sum's remaining passes over the block sums. When 2^p divides the row (a row of 2^p·k cells), both build the same tree of additions (torch/spec.bend), for every p, k and number of passes r after the first p: the same pairs added in the same order, so the same bits in F32. Any block size that divides the row gives the row's sum, which is why the mutant that shrinks the cross-entropy's blocks to one cell survives.