~/bend-docscommunity

LAWS.bend open laws/TODOs

raw source on the hub · import bendtorch@1.0.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> -> {0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(g, 0x1ca1def0868294e0aed9920bf123e68a/spec.mv(w, v)) == 0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(0x1ca1def0868294e0aed9920bf123e68a/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> -> {0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(g, 0x1ca1def0868294e0aed9920bf123e68a/spec.mv(w, v)) == 0x1ca1def0868294e0aed9920bf123e68a/spec.frob(0x1ca1def0868294e0aed9920bf123e68a/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> -> {0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(g, 0x1ca1def0868294e0aed9920bf123e68a/spec.mv(w2, 0x1ca1def0868294e0aed9920bf123e68a/spec.mv(w1, v))) == 0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(0x1ca1def0868294e0aed9920bf123e68a/spec.mtv(w1, 0x1ca1def0868294e0aed9920bf123e68a/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>> -> {0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(dy, 0x1ca1def0868294e0aed9920bf123e68a/spec.fy(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.fwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.SeqL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, x))) == 0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(0x1ca1def0868294e0aed9920bf123e68a/spec.bdx(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.bwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.SeqL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, 0x1ca1def0868294e0aed9920bf123e68a/spec.fsv(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.fwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.SeqL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, x)), dy, 0x1ca1def0868294e0aed9920bf123e68a/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>> -> {0x1ca1def0868294e0aed9920bf123e68a/spec.pdot(0x1ca1def0868294e0aed9920bf123e68a/main.Two{dy1, dy2}, 0x1ca1def0868294e0aed9920bf123e68a/spec.fy(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.fwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.ForkL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, x))) == 0x1ca1def0868294e0aed9920bf123e68a/spec.dotn(0x1ca1def0868294e0aed9920bf123e68a/spec.bdx(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.bwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.ForkL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, 0x1ca1def0868294e0aed9920bf123e68a/spec.fsv(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Layer.fwd(0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x1ca1def0868294e0aed9920bf123e68a/spec.ForkL, 0x1ca1def0868294e0aed9920bf123e68a/main.Two{w1, w2}, x)), 0x1ca1def0868294e0aed9920bf123e68a/main.Two{dy1, dy2}, 0x1ca1def0868294e0aed9920bf123e68a/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 causal_keys provedin PROOF.bendsource · line 64 · raw

@+i:Nat -> @+hd:Nat -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> {List.length(&2, F32, 0x1ca1def0868294e0aed9920bf123e68a/main.dr_row(0x1ca1def0868294e0aed9920bf123e68a/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 80 · 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> -> {0x1ca1def0868294e0aed9920bf123e68a/main.pr_n(0x1ca1def0868294e0aed9920bf123e68a/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 99 · 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> -> {0x1ca1def0868294e0aed9920bf123e68a/main.gr_n(0x1ca1def0868294e0aed9920bf123e68a/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 118 · raw

@d:Nat -> @gs:0x1ca1def0868294e0aed9920bf123e68a/main.Halves<0x1ca1def0868294e0aed9920bf123e68a/main.FS> -> @+n:Nat -> @+h:Nat -> @+dm:0x1ca1def0868294e0aed9920bf123e68a/main.Dm -> {0x1ca1def0868294e0aed9920bf123e68a/main.fpar(d, gs, n, h, dm) == 0x1ca1def0868294e0aed9920bf123e68a/main.fleaf(0x1ca1def0868294e0aed9920bf123e68a/main.hcat(0x1ca1def0868294e0aed9920bf123e68a/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 128 · raw

@d:Nat -> @gs:0x1ca1def0868294e0aed9920bf123e68a/main.Halves<0x1ca1def0868294e0aed9920bf123e68a/main.BG> -> @+n:Nat -> @+kv:Nat -> @+dm:0x1ca1def0868294e0aed9920bf123e68a/main.Dm -> {0x1ca1def0868294e0aed9920bf123e68a/main.bpar(d, gs, n, kv, dm) == 0x1ca1def0868294e0aed9920bf123e68a/main.bleaf(0x1ca1def0868294e0aed9920bf123e68a/main.hcat(0x1ca1def0868294e0aed9920bf123e68a/main.BG, gs), kv, dm, []) : List<&1, 0x1ca1def0868294e0aed9920bf123e68a/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 138 · raw

@+j:Nat -> @+i:Nat -> @+k:Nat -> {0x1ca1def0868294e0aed9920bf123e68a/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 147 · 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 156 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+sc:F32 -> @+j:Nat -> @+i:Nat -> @+k:Nat -> {0x1ca1def0868294e0aed9920bf123e68a/main.hc_kc(0x1ca1def0868294e0aed9920bf123e68a/main.head_cols(0x1ca1def0868294e0aed9920bf123e68a/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 169 · raw

@+a:List<&2, Nat> -> @+b:List<&2, Nat> -> @+o:List<&2, Nat> -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.bc_ok(a, b, o) == 0x1ca1def0868294e0aed9920bf123e68a/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 177 · raw

@+s:List<&2, Nat> -> @+k:Nat -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.numel(s) == Nat.mul(0x1ca1def0868294e0aed9920bf123e68a/tensor.at1(s, k), 0x1ca1def0868294e0aed9920bf123e68a/tensor.numel(0x1ca1def0868294e0aed9920bf123e68a/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 184 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+l:Nat -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.remove(0x1ca1def0868294e0aed9920bf123e68a/tensor.setd(s, d, l), d) == 0x1ca1def0868294e0aed9920bf123e68a/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 191 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+l:Nat -> @e:{True{} == Nat.is_lt(d, 0x1ca1def0868294e0aed9920bf123e68a/tensor.rank(s)) : Bool} -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.at1(0x1ca1def0868294e0aed9920bf123e68a/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 200 · raw

@+s:List<&2, Nat> -> @+d:Nat -> @+x:Nat -> @+y:Nat -> @e:{True{} == Nat.is_lt(d, 0x1ca1def0868294e0aed9920bf123e68a/tensor.rank(s)) : Bool} -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.numel(0x1ca1def0868294e0aed9920bf123e68a/tensor.setd(s, d, Nat.add(x, y))) == Nat.add(0x1ca1def0868294e0aed9920bf123e68a/tensor.numel(0x1ca1def0868294e0aed9920bf123e68a/tensor.setd(s, d, x)), 0x1ca1def0868294e0aed9920bf123e68a/tensor.numel(0x1ca1def0868294e0aed9920bf123e68a/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 209 · raw

@+r:Nat -> @+c:Nat -> {0x1ca1def0868294e0aed9920bf123e68a/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 215 · raw

@+s:List<&2, Nat> -> @p:List<&2, Nat> -> {0x1ca1def0868294e0aed9920bf123e68a/tensor.rank(0x1ca1def0868294e0aed9920bf123e68a/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.