spec.bend checks
raw source on the hub · import bendtorch@1.1.1.0/spec.bend as Spec
The mathematical model the laws speak about: vectors and matrices as lists of Nat, a matrix as its list of rows. Nothing here runs in a model; it is the specification that LAWS.bend states facts about. Nat makes the algebra exact (no rounding), so a law proved here says the formulas are right; whether the F32 code computes them to F32's precision is tested against PyTorch, never proved (docs/design.md, "Proved, tested, trusted").
2 imports
import Base import ./main.bend as T
Types
type Sum source · line 134 · raw
Data
SCell@i:Nat -> Sum
SAdd@a:Sum -> @b:Sum -> Sum
Definitions
def dotn source · line 10 · raw
@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> Nat
the dot product of the common prefix
def vadd source · line 18 · raw
@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> List<&2, Nat>
vector sum; the shorter vector counts as padded with zeros
def vscale source · line 27 · raw
@+k:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>
def mv source · line 35 · raw
@rows:List<&2, List<&2, Nat>> -> @+v:List<&2, Nat> -> List<&2, Nat>
M·v: one dot product per row (a linear layer's forward pass, y = W·x)
def mtv source · line 43 · raw
@rows:List<&2, List<&2, Nat>> -> @g:List<&2, Nat> -> List<&2, Nat>
Mᵀ·g: the rows weighted by g and summed (a linear layer's backward pass to its input, dx = Wᵀ·dy)
def outer source · line 51 · raw
@g:List<&2, Nat> -> @+v:List<&2, Nat> -> List<&2, List<&2, Nat>>
g ⊗ v: row i is g[i]·v (a linear layer's weight gradient, dW = dy ⊗ x)
def frob source · line 59 · raw
@a:List<&2, List<&2, Nat>> -> @b:List<&2, List<&2, Nat>> -> Nat
the sum of the products of matching entries of two matrices (Frobenius inner product)
def le source · line 67 · raw
@a:Nat -> @b:Nat -> Bool
a <= b, by recursion (simpler to reason about than Nat.is_le, which goes through Nat.cmp)
def madd source · line 79 · raw
@a:List<&2, List<&2, Nat>> -> @b:List<&2, List<&2, Nat>> -> List<&2, List<&2, Nat>>
row-wise sum of two matrices (the shorter counts as padded)
def lin_fwd source · line 89 · raw
@+w:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> 0xe1214263ebd97956a02f4f3ea7544c84/main.Fwd<List<&2, List<&2, Nat>>, List<&2, Nat>, List<&2, Nat>>
y = W·x, saving x
def lin_bwd source · line 93 · raw
@+w:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> @+dy:List<&2, Nat> -> @g:List<&2, List<&2, Nat>> -> 0xe1214263ebd97956a02f4f3ea7544c84/main.Bwd<List<&2, List<&2, Nat>>, List<&2, Nat>>
dx = Wᵀ·dy, and dW += dy ⊗ x
def LinL source · line 96 · raw
0xe1214263ebd97956a02f4f3ea7544c84/main.Layer<List<&2, List<&2, Nat>>, List<&2, Nat>, List<&2, Nat>, List<&2, Nat>>
def SeqL source · line 100 · raw
0xe1214263ebd97956a02f4f3ea7544c84/main.Layer<0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>>>
two linear layers in sequence, and side by side on one input, through BendTorch's combinators
def dupv source · line 103 · raw
@+x:List<&2, Nat> -> 0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>>
def ForkL source · line 106 · raw
0xe1214263ebd97956a02f4f3ea7544c84/main.Layer<0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>>, 0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>>>
def fy source · line 109 · raw
@-P:Type -> @-Y:Type -> @-S:Type -> @r:0xe1214263ebd97956a02f4f3ea7544c84/main.Fwd<P, Y, S> -> Y
def fsv source · line 114 · raw
@-P:Type -> @-Y:Type -> @-S:Type -> @r:0xe1214263ebd97956a02f4f3ea7544c84/main.Fwd<P, Y, S> -> S
def bdx source · line 119 · raw
@-P:Type -> @-X:Type -> @r:0xe1214263ebd97956a02f4f3ea7544c84/main.Bwd<P, X> -> X
def pdot source · line 125 · raw
@a:0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>> -> @b:0xe1214263ebd97956a02f4f3ea7544c84/main.Two<List<&2, Nat>, List<&2, Nat>> -> Nat
the dot product of two pairs of vectors
def pw source · line 139 · raw
@p:Nat -> Nat
2^p
def bal source · line 149 · raw
@+p:Nat -> @+i:Nat -> Sum
the 2^p cells from cell i summed as a balanced tree of pairs: how a GPU leaf sums its block (the device's MSE and cross-entropy block sums)
def blocks source · line 157 · raw
@+p:Nat -> @k:Nat -> @+lo:Nat -> List<&2, Sum>
k blocks of 2^p cells, the first from cell lo, each summed by bal; with p = 0, a row of k cells
def spass source · line 165 · raw
@xs:List<&2, Sum> -> List<&2, Sum>
one pass of main.bend's tree_sum (ts_pass): neighbours added in pairs, an odd last cell carried up as it is
def passes source · line 176 · raw
@r:Nat -> @xs:List<&2, Sum> -> List<&2, Sum>
r passes of tree_sum; tree_sum (ts_go) stops once one cell is left, and a pass leaves one cell as it is, so the sum of m cells is passes(r, row) for any r at least the number of passes it makes