~/bend-docscommunity

main.bend relies on unsafe/foreign

raw source on the hub · import bendtorch-fast@2.1.0.0/main.bend as Main

BendTorch's layers over bend-ml-tensor-fork: the same layers, types and bits as torch/main.bend's, faster where the work splits into parallel blocks.

import ../torch/main.bend as T import ../torch/fast.bend as F F.LinearFirstF(100n, 784n, 128n, 3n) # instead of T.LinearFirst(100n, 784n, 128n, 3n)

LinearF, LinearFirstF and LinearNBF are main.bend's Linear, LinearFirst and LinearNB with their three products (x·Wᵀ, dY·W, dYᵀ·X added into dW) taken from bend-ml-tensor-fork instead of bend-ml-tensor-array, and their optimizer step in blocks. SiLU, ActIn, Mul2, Add2, Residual, Mat.dup and Mat.sum2 are main.bend's with their element-wise loops in blocks (tensor-fork's par1..par4), the optimizer step (mat_step, lin_step) too. GroupNorm is main.bend's block code in blocks: the forward pass a range of (image, group) blocks a block, the backward pass a range of groups. Attention's forward pass is main.bend's units (one sequence's key/value group) in blocks, reading Q, K and V and writing O in place; its backward pass is main.bend's. Every number is computed by main.bend's own loop or tensor-array's own cells, so the bits are main.bend's at every depth and thread count (tests: demo/fast_test.bend). Small matrices run in one block (ew_depth), as in main.bend.

Outside the proofs, on purpose: tensor-fork shares arrays through Base's @unsafe Array.fork, so bend names the defs below that reach it, and no file with laws imports this one (make laws checks both). All of it is Bend.

4 imports
import Base
import bend-ml-tensor-array@0.1.20.0/main.bend as TA
import bend-ml-tensor-fork@0.1.7.1/main.bend as TF
import bendtorch@2.1.0.0/main.bend as T

Types

type PE source · line 95 · raw

Data

type GNE source · line 339 · raw

Data

type AE source · line 467 · raw

Data

Definitions

def ew_d2 source · line 34 · raw

@small:Bool -> @big:Bool -> @+cnt:Nat -> Nat

log2 of the blocks for cnt numbers: one block below 131072 (MNIST's and the tiny Llama's matrices; at 100352 the blocks cost MNIST more than they gave on 16 threads), else one per 16384, at most 64

def ew_depth source · line 43 · raw

@+cnt:Nat -> Nat

def p2_b2 source · line 48 · raw

@q:0x5c5507d77be847e1e454ec33ab54e087/main.P2 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32>

def sgd_leaf source · line 53 · raw

@+lr:F32 -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32>

def mom_b source · line 58 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.Q3 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B3<F32, F32, F32>

def mom_leaf source · line 62 · raw

@+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B3<F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B3<F32, F32, F32>

def q4_b source · line 68 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.Q4 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def opt_leaf source · line 72 · raw

@+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def sgd_fin source · line 78 · raw

@-r:Nat -> @-c:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def mom_fin source · line 82 · raw

@-r:Nat -> @-c:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B3<F32, F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def opt_fin source · line 86 · raw

@-r:Nat -> @-c:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def ph_cnt source · line 99 · raw

@+n:U32 -> @+e0:U32 -> @+cnt:Nat -> Nat

the elements of a block of cnt words from element e0, the last block stopping at n

def pq_b source · line 103 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.PQ -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32>

def pqv_b source · line 107 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.PQV -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32>

def ph_go source · line 111 · raw

@+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @+n:U32 -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32>

def ph_leaf source · line 118 · raw

@+e:PE -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32>

def phv_go source · line 122 · raw

@+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @+n:U32 -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32>

def phv_leaf source · line 129 · raw

@+e:PE -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32>

def ph_fin source · line 133 · raw

@-r:Nat -> @-c:Nat -> @+n:U32 -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, U32> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def phv_fin source · line 137 · raw

@-r:Nat -> @-c:Nat -> @+n:U32 -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, U32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def ph_nw source · line 142 · raw

@+r:Nat -> @+c:Nat -> Nat

the words of r·c elements, ceil(r·c / 2)

def ph_leafs source · line 145 · raw

@+r:Nat -> @+c:Nat -> @+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @wa:Array<F32> -> @ga:Array<F32> -> @t:0x82db62d229f00333d556466609c8ab27/main.PTree -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def ph_mat source · line 154 · raw

@+r:Nat -> @+c:Nat -> @+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @wa:Array<F32> -> @ga:Array<F32> -> @t:0x82db62d229f00333d556466609c8ab27/main.PTree -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

def opt_mat source · line 162 · raw

@+r:Nat -> @+c:Nat -> @+o:0x82db62d229f00333d556466609c8ab27/main.Opt -> @w:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @st:0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>> -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>

SGD splits two arrays, momentum three, Adam four. No unused parameter-sized buffers.

def mat_step source · line 175 · raw

@+r:Nat -> @+c:Nat -> @+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @p:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @st:0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Step<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>>

def lin_step2 source · line 178 · raw

@+i:Nat -> @+o:Nat -> @+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @p:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @g:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @st:0x82db62d229f00333d556466609c8ab27/main.Two<0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i>>, 0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, o>>> -> 0x82db62d229f00333d556466609c8ab27/main.Step<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>>

def lin_step source · line 183 · raw

@+i:Nat -> @+o:Nat -> @+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @p:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @g:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @st:0x82db62d229f00333d556466609c8ab27/main.OptState<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>> -> 0x82db62d229f00333d556466609c8ab27/main.Step<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>>

def zip_fin source · line 202 · raw

@-r:Nat -> @-c:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0x5c5507d77be847e1e454ec33ab54e087/main.MPair<r, c>

def a2_b2 source · line 213 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32>

def copy_leaf source · line 219 · raw

@+e:Unit -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32>

numbers i0 .. i0+cnt-1 of a into b

def dup_fin source · line 226 · raw

@-r:Nat -> @-c:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>>

def Mat.dup source · line 232 · raw

@+r:Nat -> @+c:Nat -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>>

two copies of a matrix (T.Mat.dup)

def mpair_a source · line 237 · raw

@-r:Nat -> @-c:Nat -> @q:0x5c5507d77be847e1e454ec33ab54e087/main.MPair<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>

def Mat.sum2 source · line 243 · raw

@+r:Nat -> @+c:Nat -> @a:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>

a + b, dropping b (T.Mat.sum2)

def lin_fwd source · line 248 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @par:Nat -> @p:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def lin_b2 source · line 253 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @par:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, o> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i> -> @g:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.MMul<n, o, i> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def lin_bwd source · line 258 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> @p:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o> -> @g:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def LinearF source · line 263 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def linf_bwd source · line 268 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> @p:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o> -> @g:0x82db62d229f00333d556466609c8ab27/main.Lin<i, o> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def LinearFirstF source · line 273 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def lnb_b2 source · line 278 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i> -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i> -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.MMul<n, o, i> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def LinearNBF source · line 283 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def SiLU source · line 297 · raw

@+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>

def add2_f source · line 300 · raw

@+n:Nat -> @+c:Nat -> @x:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, Unit>

def Add2 source · line 305 · raw

@+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, Unit>

def mul2_f2 source · line 308 · raw

@+n:Nat -> @+c:Nat -> @da:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> @db:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>>

def mul2_f source · line 313 · raw

@+n:Nat -> @+c:Nat -> @x:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>>

def mul2_b2 source · line 318 · raw

@+n:Nat -> @+c:Nat -> @s:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> @d:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>>

def Mul2 source · line 323 · raw

@+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>>

def gn_e source · line 342 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> GNE

def gn_d2 source · line 345 · raw

@lt:Bool -> @+a:Nat -> @+b:Nat -> Nat

def gn_depth source · line 351 · raw

@+cnt:Nat -> @+k:Nat -> Nat

ew_depth's blocks for the cells, at most as many as the ranges to split

def gnf_blks source · line 355 · raw

@k:Nat -> @+t:U32 -> @+ng:U32 -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.R4 -> 0x82db62d229f00333d556466609c8ab27/main.R4

blocks t .. t + k - 1 of the forward pass

def r4_b4 source · line 360 · raw

@s:0x82db62d229f00333d556466609c8ab27/main.R4 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def gnf_leaf source · line 364 · raw

@+e:GNE -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def gn_f2 source · line 369 · raw

@-m:Nat -> @-d:Nat -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>>

def gn_fwd source · line 373 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> @p:0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>>

def gnb_imgs source · line 379 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+hwc:U32 -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.R4B -> 0x82db62d229f00333d556466609c8ab27/main.R4B

group j's blocks in the remaining left images, from the image whose first cell is off

def gnb_grps source · line 385 · raw

@k:Nat -> @+j:U32 -> @+n:Nat -> @+hwc:U32 -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.R4B -> 0x82db62d229f00333d556466609c8ab27/main.R4B

k groups from the one whose first channel is j, each over the n images

def a2_b source · line 390 · raw

@q:0x82db62d229f00333d556466609c8ab27/main.A2 -> Array<F32>

def gnb_put2 source · line 394 · raw

@+len1:Nat -> @+j0:U32 -> @+c:U32 -> @db:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.A2 -> Array<F32>

def gnb_put source · line 399 · raw

@+len1:Nat -> @+j0:U32 -> @+c:U32 -> @w:Array<F32> -> @dg:Array<F32> -> @db:Array<F32> -> Array<F32>

the range's channels j0 .. j0 + len1 of its dg and db into w, at c + j and 2c + j

def gnb_fin source · line 402 · raw

@+len1:Nat -> @+j0:U32 -> @+c:U32 -> @w:Array<F32> -> @s:0x82db62d229f00333d556466609c8ab27/main.R4B -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def gnb_go2 source · line 406 · raw

@+k:Nat -> @+j0:U32 -> @+e:GNE -> @x:Array<F32> -> @dy:Array<F32> -> @dx:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def gnb_go source · line 412 · raw

@+k:Nat -> @+i0:U32 -> @+e:GNE -> @x:Array<F32> -> @dy:Array<F32> -> @dx:Array<F32> -> @w:Array<F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

groups i0 .. i0 + k - 1 (k >= 1): g copied out of w, then the groups' blocks

def gnb_leaf source · line 417 · raw

@+e:GNE -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def gn_b5 source · line 422 · raw

@+d:Nat -> @-m:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @gg:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @g:Array<F32> -> @x:Array<F32> -> @dy:Array<F32> -> @dx:Array<F32> -> @dg:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>>

def gn_b4 source · line 426 · raw

@+d:Nat -> @-m:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @gg:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @g:Array<F32> -> @x:Array<F32> -> @dy:Array<F32> -> @dx:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>>

def gn_b3 source · line 431 · raw

@+d:Nat -> @-m:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @gg:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @g:Array<F32> -> @q:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>>

dg and db out of w, then main.bend's ln_b2 adds them into the gradients it is given

def gn_b2 source · line 435 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, Nat.mul(ng, cg)> -> @gg:0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)> -> @xa:Array<F32> -> @da:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>>

def gn_bwd source · line 441 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> @p:0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)> -> @gg:0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>>

w = g followed by 2c zeros

def ln_step2 source · line 447 · raw

@+d:Nat -> @+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @p:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @g:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @st:0x82db62d229f00333d556466609c8ab27/main.Two<0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>>, 0x82db62d229f00333d556466609c8ab27/main.OptState<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>>> -> 0x82db62d229f00333d556466609c8ab27/main.Step<0x82db62d229f00333d556466609c8ab27/main.LN<d>>

the gain and the bias take this file's optimizer step, as Linear's two matrices do

def ln_step source · line 452 · raw

@+d:Nat -> @+op:0x82db62d229f00333d556466609c8ab27/main.Opt -> @p:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @g:0x82db62d229f00333d556466609c8ab27/main.LN<d> -> @st:0x82db62d229f00333d556466609c8ab27/main.OptState<0x82db62d229f00333d556466609c8ab27/main.LN<d>> -> 0x82db62d229f00333d556466609c8ab27/main.Step<0x82db62d229f00333d556466609c8ab27/main.LN<d>>

def GroupNorm source · line 456 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>>

GroupNorm over n images of hw pixels and ng groups of cg channels, channels last (main.bend's GroupNorm)

def atf_fin source · line 470 · raw

@q:Array<F32> -> @k:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.PO -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def atf_head2 source · line 475 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+kc:U32 -> @+oc:U32 -> @v:Array<F32> -> @o:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.QK -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def atf_head source · line 482 · raw

@+e:AE -> @+qc:U32 -> @+kc:U32 -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

one query head: its columns of Q and of O from qc (its sequence's first row included), its key/value head's columns of K and V from kc; main.bend's head_f2c with the offsets of the shared arrays

def atf_heads source · line 488 · raw

@left:Nat -> @+e:AE -> @+qc:U32 -> @+kc:U32 -> @+hdu:U32 -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

a unit's query heads in order, hd columns apart, over one key/value head

def atf_unit source · line 496 · raw

@+e:AE -> @+u:U32 -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

unit u: sequence u / kv, key/value head j = u % kv and its query heads j·g .. j·g + g - 1 (main.bend's kv_of)

def atf_units source · line 503 · raw

@left:Nat -> @+e:AE -> @+u:U32 -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

def atf_leaf source · line 511 · raw

@+e:AE -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32>

a block: units i0 .. i0 + cnt - 1

def atf_out source · line 514 · raw

@-n:Nat -> @-w:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B4<F32, F32, F32, F32> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>

def attn_fwd source · line 520 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @x:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>

the b·kv units in 2^d blocks, d enough for one unit a block

def RMSNorm source · line 528 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>

main.bend's RMSNorm, RoPE, MSE and CrossEntropy under their own names, and Attention with the forward pass above (Embedding below, with its step in blocks), so that a program that takes its layers from here can take device versions of the same names from another file by its import line alone (they run as in main.bend)

def RoPE source · line 531 · raw

@+n:Nat -> @+w:Nat -> @+t:Nat -> @+h:Nat -> @+half:Nat -> @+base:F32 -> @-e:{w == Nat.mul(h, Nat.add(half, half)) : Nat} -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, Unit>

def Attention source · line 534 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @-e1:{n == Nat.mul(b, t) : Nat} -> @-e2:{w == Nat.mul(Nat.mul(kv, g), hd) : Nat} -> @-e3:{kvw == Nat.mul(kv, hd) : Nat} -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>

def MSE source · line 537 · raw

@+n:Nat -> @+c:Nat -> @y:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> @t:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>

def Embedding source · line 542 · raw

@+n:Nat -> @+v:Nat -> @+d:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, List<&2, Nat>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, List<&2, Nat>>

main.bend's Embedding with its optimizer step in blocks (mat_step): the table is SmolLM2's largest matrix (49152 x 576, tied to the output head), and main.bend's step updates it on one thread

def CrossEntropy source · line 545 · raw

@+n:Nat -> @+c:Nat -> @z:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> @+labels:List<&2, Nat> -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>

def Linear source · line 553 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def LinearFirst source · line 556 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

def LinearNB source · line 559 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>>

Templates

template ew1_leaf source · line 188 · raw

@-f:(@+a:F32 -> F32) -> @+e:Unit -> @+i0:U32 -> @+cnt:Nat -> @a:Array<F32> -> Array<F32>

template map source · line 191 · raw

@-f:(@+a:F32 -> F32) -> @+r:Nat -> @+c:Nat -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>

template ew2_leaf source · line 197 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+e:Unit -> @+i0:U32 -> @+cnt:Nat -> @r:0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32> -> 0xe36d1d7a7dd49aafdadea9face2bf48c/main.B2<F32, F32>

template zip source · line 208 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+r:Nat -> @+c:Nat -> @a:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.MPair<r, c>

a[i] = f(a[i], b[i]); b comes back unchanged

template acti_f3 source · line 288 · raw

@-f:(@+a:F32 -> F32) -> @+n:Nat -> @+c:Nat -> @m:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>

template ActIn source · line 294 · raw

@-f:(@+a:F32 -> F32) -> @-d:(@+a:F32 -> @+b:F32 -> F32) -> @+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>

T.ActIn: f on a copy, the input saved

template Residual source · line 327 · raw

@-n:Nat -> @-c:Nat -> @-P:Type -> @-S:Type -> @-f:0x82db62d229f00333d556466609c8ab27/main.Layer<P, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, S> -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Two<0x82db62d229f00333d556466609c8ab27/main.Two<Unit, P>, Unit>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x82db62d229f00333d556466609c8ab27/main.Two<Unit, S>, Unit>>

T.Residual with these copies and sums (T.ResidualOf, the composition the law residual is about)