~/bend-docscommunity

spec.bend checks

raw source on the hub · import bendtorch@2.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

Types

type Sum source · line 153 · raw

Data

Definitions

def dotn source · line 12 · raw

@xs:List<&2, Nat> -> @ys:List<&2, Nat> -> Nat

the dot product of the common prefix

def vadd source · line 20 · 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 29 · raw

@+k:Nat -> @xs:List<&2, Nat> -> List<&2, Nat>

def mv source · line 37 · 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 45 · 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 53 · 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 61 · 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 69 · 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 81 · 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 91 · raw

@+w:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Fwd<List<&2, List<&2, Nat>>, List<&2, Nat>, List<&2, Nat>>

y = W·x, saving x

def lin_bwd source · line 95 · raw

@+w:List<&2, List<&2, Nat>> -> @+x:List<&2, Nat> -> @+dy:List<&2, Nat> -> @g:List<&2, List<&2, Nat>> -> 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Bwd<List<&2, List<&2, Nat>>, List<&2, Nat>>

dx = Wᵀ·dy, and dW += dy ⊗ x

def LinL source · line 98 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<List<&2, List<&2, Nat>>, List<&2, Nat>, List<&2, Nat>, List<&2, Nat>>

def SeqL source · line 102 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, List<&2, Nat>, 0x9fcea48a66fe9c6efe7d054f27f7e989/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 105 · raw

@+x:List<&2, Nat> -> 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>

def ForkL source · line 108 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, List<&2, Nat>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>>

def BothL source · line 112 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, List<&2, Nat>>, List<&2, List<&2, Nat>>>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>>

two linear layers on the two vectors of a pair, through T.Both

def add_fwd source · line 116 · raw

@+p:Unit -> @x:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>> -> 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Fwd<Unit, List<&2, Nat>, Unit>

a + b for a pair of vectors; the backward pass hands dy to both (T.Add2 over vectors)

def AddL source · line 121 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<Unit, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>>, List<&2, Nat>, Unit>

def ResL source · line 125 · raw

0x9fcea48a66fe9c6efe7d054f27f7e989/main.Layer<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<Unit, List<&2, List<&2, Nat>>>, Unit>, List<&2, Nat>, List<&2, Nat>, 0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<Unit, List<&2, Nat>>, Unit>>

x + W·x through T.ResidualOf, the code of T.Residual: Seq(Fork(Id, linear), AddL)

def fy source · line 128 · raw

@-P:Type -> @-Y:Type -> @-S:Type -> @r:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Fwd<P, Y, S> -> Y

def fsv source · line 133 · raw

@-P:Type -> @-Y:Type -> @-S:Type -> @r:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Fwd<P, Y, S> -> S

def bdx source · line 138 · raw

@-P:Type -> @-X:Type -> @r:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Bwd<P, X> -> X

def pdot source · line 144 · raw

@a:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>> -> @b:0x9fcea48a66fe9c6efe7d054f27f7e989/main.Two<List<&2, Nat>, List<&2, Nat>> -> Nat

the dot product of two pairs of vectors

def pw source · line 158 · raw

@p:Nat -> Nat

2^p

def bal source · line 168 · 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 176 · 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 184 · 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 195 · 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