spec.bend checks
raw source on the hub · import bendtorch@1.0.0.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
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> -> 0x1ca1def0868294e0aed9920bf123e68a/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>> -> 0x1ca1def0868294e0aed9920bf123e68a/main.Bwd<List<&2, List<&2, Nat>>, List<&2, Nat>>
dx = Wᵀ·dy, and dW += dy ⊗ x
def LinL source · line 96 · raw
0x1ca1def0868294e0aed9920bf123e68a/main.Layer<List<&2, List<&2, Nat>>, List<&2, Nat>, List<&2, Nat>, List<&2, Nat>>
def SeqL source · line 100 · raw
0x1ca1def0868294e0aed9920bf123e68a/main.Layer<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>>>
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> -> 0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>>
def ForkL source · line 106 · raw
0x1ca1def0868294e0aed9920bf123e68a/main.Layer<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>>>
def fy source · line 109 · raw
@-P:Type -> @-Y:Type -> @-S:Type -> @r:0x1ca1def0868294e0aed9920bf123e68a/main.Fwd<P, Y, S> -> Y
def fsv source · line 114 · raw
@-P:Type -> @-Y:Type -> @-S:Type -> @r:0x1ca1def0868294e0aed9920bf123e68a/main.Fwd<P, Y, S> -> S
def bdx source · line 119 · raw
@-P:Type -> @-X:Type -> @r:0x1ca1def0868294e0aed9920bf123e68a/main.Bwd<P, X> -> X
def pdot source · line 125 · raw
@a:0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>> -> @b:0x1ca1def0868294e0aed9920bf123e68a/main.Two<List<&2, Nat>, List<&2, Nat>> -> Nat
the dot product of two pairs of vectors