main.bend relies on unsafe/foreign
raw source on the hub · import bendtorch-gpu@2.1.0.0/main.bend as Main
BendTorch's layers with their work on the GPU (bend-ml-tensor-gpu): the same layers, types and bits as torch/main.bend's and torch/fast.bend's.
import ../torch/main.bend as T import ../torch/gpu.bend as F F.LinearFirstF(100n, 784n, 128n, 4n) # this layer's products and optimizer step run as bangs
Every layer here runs its work as bangs (one ! call each), which run on the GPU in a binary built with CUDA and on
the CPU threads otherwise:
- LinearF, LinearFirstF and LinearNBF: their three products (x·Wᵀ, dY·W, dYᵀ·X added into dW) from tensor-gpu, and
their optimizer step.
- SiLU, GELUErf, Mul2, Add2, Residual, Mat.dup and Mat.sum2: their element-wise loops.
- RMSNorm, RoPE, RoPE2d and Attention (main.bend's names; fast.bend's are main.bend's, on the CPU).
- im2col, col2im and Conv2d: the gather and its scatter as cell bangs, then the linear layer's three products.
- Tile: its broadcast and the sum of its rows as cell bangs.
- MSE, CrossEntropy and Embedding (its rows, its gradient and its step), so that a training step's work, the loss
included, stays on the device; the host reads back a few numbers a row for the loss.
The names are fast.bend's, so a program switches by its import line alone (the Makefile builds the GPU demos so).
Each bang is one of tensor-gpu's run1..run4 (outputs in place) or run1_new..run4_new (outputs made on the device): a tree of leaves, every node sharing the arrays. The code of a leaf is below, in plain Bend, and computes each number with main.bend's own expression or row code (rmsf_row, ssq, rmsb_dot, rope_one, dot0, psmc, g_set2, adam_m..), so the bits are main.bend's, except where an exponential is taken on the device (SiLU, GELUErf's gradient, attention's softmax, cross-entropy): CUDA takes the float overload of exp there and its last bit can differ from the host's (the known limit of bendlang/bend#1305); 1 ulp is accepted there, my decision of 2026-10-18 (tests: demo/gpu_test.bend and demo/gpu_layers_test.bend). Sizes, depths and capacities are computed on the host and passed in. The arrays stay on the device from one bang to the next as long as the host only passes their handles; a bang that follows the host's parallel lets in one evaluation runs on the CPU threads. A sum whose order fixes the bits (a row's softmax, a row's squares) is a row bang: a leaf a row, a tail loop along it. Everything else is a cell bang: a leaf takes cells t, t + l, .., each one number (a dot product, or one expression of a row's sums), so a bang has as many independent pieces of work as the output has numbers.
Outside the proofs, on purpose, like fast.bend: tensor-gpu shares arrays between the leaves of a bang 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). This file writes no @unsafe of its own. 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-gpu@0.1.5.2/main.bend as TG import bendtorch@2.1.0.0/main.bend as T
Types
type EN source · line 66 · raw
Data
EN@n:U32 -> EN
type ESGD source · line 226 · raw
Data
ESGD@n:U32 -> @lr:F32 -> ESGD
type EMOM source · line 239 · raw
Data
EMOM@n:U32 -> @lr:F32 -> @mu:F32 -> EMOM
type EADAM source · line 254 · raw
Data
EADAM@n:U32 -> @lr:F32 -> @c1:F32 -> @b2:F32 -> @c2:F32 -> @eps:F32 -> @wd:F32 -> @dec:Bool -> @step:F32 -> @bc2:F32 -> EADAM
type ERF source · line 333 · raw
Data
ERF@n:U32 -> @d:Nat -> @d32:U32 -> @df:F32 -> @eps:F32 -> ERF
type EDG source · line 406 · raw
Data
EDG@d:U32 -> @n:Nat -> EDG
type EGN source · line 439 · raw
Data
EGN@nb:U32 -> @ng:U32 -> @hw:Nat -> @hwc:U32 -> @c:U32 -> @cg:Nat -> @cg32:U32 -> @nf:F32 -> @eps:F32 -> EGN
type GB source · line 471 · raw
Type
GB@x:Array<F32> -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> GB
type GS source · line 518 · raw
Type
GS@x:Array<F32> -> @dy:Array<F32> -> @mr:Array<F32> -> @sg:F32 -> @sb:F32 -> GS
type EGW source · line 552 · raw
Data
EGW@c:U32 -> @n:Nat -> @hw:Nat -> @hwc:U32 -> @ng:U32 -> @cg:U32 -> EGW
type ERO source · line 594 · raw
Data
ERO@n:U32 -> @fwd:Bool -> @hh:U32 -> @half:U32 -> @w:U32 -> @t:U32 -> ERO
type ER2 source · line 629 · raw
Data
ER2@n:U32 -> @ny:U32 -> @fwd:Bool -> @h:U32 -> @hy:U32 -> @hx:U32 -> @w:U32 -> @nt:U32 -> @tw:U32 -> ER2
type RS5 source · line 636 · raw
Type
the matrix rotated in place, and the y and x tables
RS5@x:Array<F32> -> @yc:Array<F32> -> @ys:Array<F32> -> @xc:Array<F32> -> @xs:Array<F32> -> RS5
type S4 source · line 708 · raw
Type
S4@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> @d:Array<F32> -> S4
type EAC source · line 728 · raw
Data
the sizes; n is how many cells (or rows) the bang has, nw and nk the cells of dQ and of dK, tr = t(t + 1)/2 the cells of a head's P
EAC@n:U32 -> @t:Nat -> @t32:U32 -> @h:U32 -> @g:U32 -> @gn:Nat -> @hd:Nat -> @hd32:U32 -> @w:U32 -> @kvw:U32 -> @sc:F32 -> @nw:U32 -> @nk:U32 -> @tr:U32 -> EAC
type S3 source · line 742 · raw
Type
S3@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> S3
type IJ source · line 851 · raw
Data
the score cells are P's lower triangle (j <= i), dealt in pairs of rows i and t - 1 - i (t + 1 cells a pair), so every lane gets real work: cell e is pair p of head bh, place c; c <= p is (p, c), the rest (t - 1 - p, c - p - 1); in the middle row of an odd t the second half repeats the first and is skipped (ok false)
IJ@ok:Bool -> @bh:U32 -> @i:U32 -> @j:U32 -> IJ
type S6 source · line 915 · raw
Type
backward 1: P's score cell as forward 1, and G's cell dP = dO's row · v's row, as drow's dot (S6{q, k, v, d, p, g})
S6@q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @d:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> S6
type S8 source · line 973 · raw
Type
backward 3: cells [0, nw) are dQ's, [nw, nw + nk) dK's, the next nk dV's (S8{q, k, d, g, p, dq, dk, v})
S8@q:Array<F32> -> @k:Array<F32> -> @d:Array<F32> -> @g:Array<F32> -> @p:Array<F32> -> @dq:Array<F32> -> @dk:Array<F32> -> @v:Array<F32> -> S8
type EMS source · line 1270 · raw
Data
EMS@nb:U32 -> @p:Nat -> @bs:U32 -> @kk:F32 -> EMS
type MS source · line 1273 · raw
Type
MS@y:Array<F32> -> @t:Array<F32> -> @s:F32 -> MS
type IA source · line 1338 · raw
Type
IA@a:Array<F32> -> @k:Nat -> IA
type EEM source · line 1354 · raw
Data
EEM@k:Nat -> @d:U32 -> EEM
type ES3 source · line 1357 · raw
Type
ES3@ids:Array<F32> -> @a:Array<F32> -> @o:Array<F32> -> ES3
type TS source · line 1463 · raw
Type
TS@a:Array<F32> -> @s:F32 -> TS
type LS source · line 1496 · raw
Type
the host's part, row by row in order: loss - ((x[y] - m) - log(s)), or main.bend's clamped term for a label past the last class
LS@a:Array<F32> -> @l:F32 -> LS
type EC1 source · line 1509 · raw
Data
1. a leaf a row: st[3t] = x[y] - m, st[3t + 1] = m (x[y] read only when y is a class)
EC1@k:U32 -> @c1:Nat -> @c32:U32 -> EC1
type EC2 source · line 1603 · raw
Data
a leaf takes the blocks b, b + l, .. of the nb = rows * per; block b is row b / per, cells (b % per) * 2^p on
EC2@nb:U32 -> @per:U32 -> @p:Nat -> @bsz:U32 -> @c32:U32 -> EC2
type C3S source · line 1606 · raw
Type
C3S@a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> C3S
type EC3 source · line 1636 · raw
Data
3. a leaf a row: st[3t + 2] = s, tree_sum's remaining passes over the row's per block sums
EC3@k:U32 -> @per:U32 -> @tl:Nat -> @th:U32 -> EC3
type EC4 source · line 1653 · raw
Data
4. element-wise over the first k rows: z[i] = ((e / s) - [j == y]) / nf, j = i % c, s and y of row i / c
EC4@n:U32 -> @c32:U32 -> @nf:F32 -> EC4
type EIC source · line 1734 · raw
Data
EIC@n:U32 -> @h:U32 -> @w:U32 -> @c:U32 -> @k:U32 -> @p:U32 -> @s:U32 -> @oh:U32 -> @ow:U32 -> @per:U32 -> @ckk:U32 -> EIC
type CIC source · line 1738 · raw
Type
one input array and one output array, threaded through a bang's cells (arrays are linear in Bend)
CIC@d:Array<F32> -> @o:Array<F32> -> CIC
type GIS source · line 1742 · raw
Type
the gathered matrix's cells, and a col2im cell's running sum
GIS@d:Array<F32> -> @o:Array<F32> -> @acc:F32 -> GIS
type ETI source · line 1935 · raw
Data
ETI@n:U32 -> @t:U32 -> @c:U32 -> ETI
Definitions
def lin_fwd source · line 44 · raw
@+n:Nat -> @+i:Nat -> @+o: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 49 · raw
@+n:Nat -> @+i:Nat -> @+o: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 54 · raw
@+n:Nat -> @+i:Nat -> @+o: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 nn source · line 69 · raw
@u:Unit -> Array<F32>
def f32_id source · line 102 · raw
@+x:F32 -> F32
def lf_copy source · line 105 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def lf_silu source · line 108 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def lf_silud source · line 112 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
o = dy, written in place, x1 = x: main.bend's silu_d(dy, x)
def lf_gelue source · line 116 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
GELUErf: main.bend's gelu_erf_v(x), and gelu_erf_d(dy, x) with o = dy written in place, x1 = x
def lf_gelued source · line 119 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def lf_add source · line 123 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
o = a, written in place, x1 = b: a + b
def lf_mul source · line 126 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def mulb_loop source · line 130 · raw
@k:Nat -> @+i:U32 -> @+l:U32 -> @rd:Pair(Array<F32>, F32) -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = dy; the saved a and b, written in place: a becomes db = dy * a and b becomes da = dy * b, in one bang
def lf_mulb source · line 137 · raw
@+t:U32 -> @+l:U32 -> @+e:EN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def en source · line 141 · raw
@+cnt:Nat -> EN
def r1_a source · line 144 · raw
@r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> Array<F32>
def dup_fin source · line 150 · raw
@-r:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>>
def Mat.dup source · line 155 · 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, the second made on the device
def Mat.sum2 source · line 160 · raw
@+r:Nat -> @+c:Nat -> @a:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>
a + b, as main.bend's Mat.sum2 adds each cell, written over a (both are consumed)
def silu_f2 source · line 164 · raw
@-n:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def silu_f source · line 168 · raw
@+n:Nat -> @+c:Nat -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def silu_b source · line 172 · raw
@+n:Nat -> @+c:Nat -> @s:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def SiLU source · line 177 · raw
@+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
SiLU: y = silu(x), the input saved; dx = silu'(x) * dy
def gelue_f source · line 180 · raw
@+n:Nat -> @+c:Nat -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def gelue_b source · line 184 · raw
@+n:Nat -> @+c:Nat -> @s:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def GELUErf source · line 190 · raw
@+n:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
GELUErf, the exact GELU: y = gelu(x) with main.bend's erf in plain F32 arithmetic, the input saved; dx = gelu'(x) * dy, whose pdf takes F32.exp (CUDA's float exp on the device, as SiLU)
def mul2_f2 source · line 193 · raw
@-n:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 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 197 · 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 201 · raw
@-n:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>>
def mul2_b source · line 205 · raw
@+n:Nat -> @+c:Nat -> @s:0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>> -> @dy: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 210 · 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>>>
Mul2: y = a * b, the inputs saved; da = dy * b, db = dy * a
def add2_f source · line 213 · 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 217 · 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 sgd_loop source · line 229 · raw
@k:Nat -> @+i:U32 -> @+l:U32 -> @+lr:F32 -> @rw:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_sgd source · line 235 · raw
@+t:U32 -> @+l:U32 -> @+e:ESGD -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def mom_loop source · line 242 · raw
@k:Nat -> @+i:U32 -> @+l:U32 -> @+lr:F32 -> @+mu:F32 -> @rw:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> @rm:Pair(Array<F32>, F32) -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3
def lf_mom source · line 250 · raw
@+t:U32 -> @+l:U32 -> @+e:EMOM -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3
def adam_loop source · line 257 · raw
@k:Nat -> @+i:U32 -> @+l:U32 -> @+lr:F32 -> @+c1:F32 -> @+b2:F32 -> @+c2:F32 -> @+eps:F32 -> @+wd:F32 -> @+dec:Bool -> @+step:F32 -> @+bc2:F32 -> @rw:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> @rm:Pair(Array<F32>, F32) -> @rv:Pair(Array<F32>, F32) -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4
def lf_adam source · line 268 · raw
@+t:U32 -> @+l:U32 -> @+e:EADAM -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4
def sgd_fin source · line 272 · raw
@-r:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>
def mom_fin source · line 276 · raw
@-r:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R3 -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>
def opt_fin source · line 280 · raw
@-r:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R4 -> 0x82db62d229f00333d556466609c8ab27/main.MatStep<r, c>
def opt_mat source · line 285 · 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>
one matrix's step, in place: SGD writes two arrays, momentum three, Adam four
def mat_step source · line 297 · 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 300 · 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 305 · 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 LinearF source · line 310 · 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 314 · raw
@+n:Nat -> @+i:Nat -> @+o: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>>
LinearFirst: no dX (the first layer's input needs no gradient)
def LinearFirstF source · line 319 · 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 323 · raw
@+n:Nat -> @+i:Nat -> @+o: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>>
LinearNB: y = x·Wᵀ, no bias (Llama's projections)
def LinearNBF source · line 328 · 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 r3_y source · line 336 · raw
@s:0x82db62d229f00333d556466609c8ab27/main.R3 -> Array<F32>
def rmsf_if source · line 340 · raw
@ok:Bool -> @+t:U32 -> @+d:Nat -> @+d32:U32 -> @+df:F32 -> @+eps:F32 -> @x:Array<F32> -> @g:Array<F32> -> @y:Array<F32> -> Array<F32>
def lf_rmsf source · line 346 · raw
@+t:U32 -> @+l:U32 -> @+e:ERF -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @y:Array<F32> -> Array<F32>
x1 = x, x2 = the gain g; y a row a leaf
def erf source · line 350 · raw
@+n:Nat -> @+d:Nat -> @+eps:F32 -> ERF
def rms_f2 source · line 353 · raw
@-n:Nat -> @-d:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def rms_f source · line 357 · raw
@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def gdx_w source · line 365 · raw
@k:Nat -> @+j:U32 -> @+off:U32 -> @+r:F32 -> @+m:F32 -> @dx:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> Array<F32>
def gdx_3 source · line 374 · raw
@+d:Nat -> @+off:U32 -> @+df:F32 -> @+r:F32 -> @+t:U32 -> @dx:Array<F32> -> @rr:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.R3S -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gdx_2 source · line 379 · raw
@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @+t:U32 -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @rr:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.AS -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gdx_if source · line 385 · raw
@ok:Bool -> @+t:U32 -> @+d:Nat -> @+d32:U32 -> @+df:F32 -> @+eps:F32 -> @x:Array<F32> -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @rr:Array<F32> -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_gdx source · line 391 · raw
@+t:U32 -> @+l:U32 -> @+e:ERF -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = x, x2 = dy, x3 = g; outputs dx and 1/rms of each row
def gdg_loop source · line 396 · raw
@k:Nat -> @+i:U32 -> @+d:U32 -> @+row:U32 -> @acc:F32 -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rr:Pair(Array<F32>, F32) -> F32
acc + dy[i]·(x[i]·r[row]) down a column, i from j by d
def gdg_put source · line 402 · raw
@+j:U32 -> @+s:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>
def gdg_if source · line 409 · raw
@ok:Bool -> @+j:U32 -> @+n:Nat -> @+d:U32 -> @x:Array<F32> -> @dy:Array<F32> -> @rr:Array<F32> -> @ga:Array<F32> -> Array<F32>
def lf_gdg source · line 415 · raw
@+t:U32 -> @+l:U32 -> @+e:EDG -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @ga:Array<F32> -> Array<F32>
x1 = x, x2 = dy, x3 = 1/rms; the gradient of the gain, in place: a leaf a column
def rms_b3 source · line 419 · raw
@-n:Nat -> @-d:Nat -> @g:Array<F32> -> @dx:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def rms_b2 source · line 423 · raw
@+n:Nat -> @+d:Nat -> @ga:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def rms_b source · line 427 · raw
@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d> -> @gacc:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def RMSNorm source · line 432 · 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>>
RMSNorm over the last dimension, its gain trained (main.bend's RMSNorm)
def egn source · line 442 · raw
@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> EGN
def r4_y source · line 445 · raw
@s:0x82db62d229f00333d556466609c8ab27/main.R4 -> Array<F32>
def gnf_if source · line 449 · raw
@ok:Bool -> @+t:U32 -> @+ng:U32 -> @+hw:Nat -> @+hwc:U32 -> @+c:U32 -> @+cg:Nat -> @+cg32:U32 -> @+nf:F32 -> @+eps:F32 -> @x:Array<F32> -> @g:Array<F32> -> @b:Array<F32> -> @y:Array<F32> -> Array<F32>
def lf_gnf source · line 455 · raw
@+t:U32 -> @+l:U32 -> @+e:EGN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @y:Array<F32> -> Array<F32>
x1 = x, x2 = the gain g, x3 = the bias b; y a block a leaf
def gn_f2 source · line 459 · raw
@-n:Nat -> @-d:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>>
def gn_f source · line 463 · 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 gb_dx source · line 474 · raw
@s:GB -> Array<F32>
def gnb_seg source · line 479 · raw
@k:Nat -> @+j:U32 -> @+off:U32 -> @+mu:F32 -> @+r:F32 -> @+ma:F32 -> @+mb:F32 -> @dx:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> GB
dx[off + j] = (((dy·g - ma) - xh·mb)·r) along one row's segment
def gnb_segs source · line 488 · raw
@left:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+mu:F32 -> @+r:F32 -> @+ma:F32 -> @+mb:F32 -> @s:GB -> GB
def gnb_blk3 source · line 495 · raw
@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+mu:F32 -> @+r:F32 -> @+t:U32 -> @dx:Array<F32> -> @mr:Array<F32> -> @q:0x82db62d229f00333d556466609c8ab27/main.R3SS -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gnb_blk2 source · line 500 · raw
@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+t:U32 -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @mr:Array<F32> -> @m:0x82db62d229f00333d556466609c8ab27/main.ASM -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gnb_blk source · line 505 · raw
@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @+t:U32 -> @x:Array<F32> -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @mr:Array<F32> -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gnb_if source · line 508 · raw
@ok:Bool -> @+t:U32 -> @+ng:U32 -> @+hw:Nat -> @+hwc:U32 -> @+c:U32 -> @+cg:Nat -> @+cg32:U32 -> @+nf:F32 -> @+eps:F32 -> @x:Array<F32> -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @mr:Array<F32> -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_gnb source · line 514 · raw
@+t:U32 -> @+l:U32 -> @+e:EGN -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = x, x2 = dy, x3 = g; outputs dx and each block's mean and 1/sqrt(var + eps)
def gdw_rows source · line 522 · raw
@k:Nat -> @+i:U32 -> @+c:U32 -> @+mu:F32 -> @+r:F32 -> @sg:F32 -> @sb:F32 -> @mr:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> GS
sg + dy·xh and sb + dy down one image's rows of a channel, i its cell, by c
def gdw_blk2 source · line 529 · raw
@+hw:Nat -> @+i:U32 -> @+c:U32 -> @+mu:F32 -> @sg:F32 -> @sb:F32 -> @x:Array<F32> -> @dy:Array<F32> -> @rr:Pair(Array<F32>, F32) -> GS
def gdw_blk1 source · line 533 · raw
@+hw:Nat -> @+i:U32 -> @+bl:U32 -> @+c:U32 -> @sg:F32 -> @sb:F32 -> @x:Array<F32> -> @dy:Array<F32> -> @rm:Pair(Array<F32>, F32) -> GS
def gdw_blk source · line 538 · raw
@+hw:Nat -> @+i:U32 -> @+bl:U32 -> @+c:U32 -> @s:GS -> GS
block bl's mean and 1/sqrt(var + eps), then its rows
def gdw_imgs source · line 543 · raw
@left:Nat -> @+i:U32 -> @+bl:U32 -> @+hw:Nat -> @+hwc:U32 -> @+c:U32 -> @+ng:U32 -> @s:GS -> GS
a channel's images in order: i steps by hw·c to the next image's first row, bl by ng to its block of the same group
def gdw_put source · line 548 · raw
@+j:U32 -> @ga:Array<F32> -> @gb:Array<F32> -> @s:GS -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def gdw_if source · line 555 · raw
@ok:Bool -> @+t:U32 -> @+n:Nat -> @+hw:Nat -> @+hwc:U32 -> @+c:U32 -> @+ng:U32 -> @+cg:U32 -> @x:Array<F32> -> @dy:Array<F32> -> @mr:Array<F32> -> @ga:Array<F32> -> @gb:Array<F32> -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_gdw source · line 562 · raw
@+t:U32 -> @+l:U32 -> @+e:EGW -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = x, x2 = dy, x3 = the blocks' means and 1/sqrt(var + eps); the gain's and the bias's gradients, in place: a leaf a channel
def gn_b3 source · line 566 · raw
@-m:Nat -> @-d:Nat -> @g:Array<F32> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, d> -> @dx:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<m, d>>
def gn_b2 source · line 570 · raw
@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, Nat.mul(ng, cg)> -> @ga:Array<F32> -> @gb:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.LN<Nat.mul(ng, cg)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>>
def gn_b source · line 574 · 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)> -> @gacc: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)>>
def ln_step2 source · line 579 · 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 584 · 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 588 · 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 rope_loop source · line 597 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+fwd:Bool -> @+hh:U32 -> @+half:U32 -> @+w:U32 -> @+t:U32 -> @st:0x82db62d229f00333d556466609c8ab27/main.RS3 -> 0x82db62d229f00333d556466609c8ab27/main.RS3
def rs3_x source · line 602 · raw
@st:0x82db62d229f00333d556466609c8ab27/main.RS3 -> Array<F32>
def lf_rope source · line 607 · raw
@+t:U32 -> @+l:U32 -> @+e:ERO -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @x:Array<F32> -> Array<F32>
x1 = cos, x2 = sin; x rotated in place
def rope_go2 source · line 611 · raw
@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+t:Nat -> @+h:Nat -> @+half:Nat -> @x:Array<F32> -> @tb:0x82db62d229f00333d556466609c8ab27/main.CS -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>
def rope_go source · line 615 · raw
@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+t:Nat -> @+h:Nat -> @+half:Nat -> @+base:F32 -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>
def RoPE source · line 620 · 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>RoPE on n = b·t rows of h heads of 2·half (main.bend's RoPE)
def er2 source · line 632 · raw
@+n:Nat -> @+fwd:Bool -> @+th:Nat -> @+tw:Nat -> @+h:Nat -> @+hy:Nat -> @+hx:Nat -> @+w:Nat -> ER2
def r2_by source · line 639 · raw
@r:0x82db62d229f00333d556466609c8ab27/main.RS3 -> @xc:Array<F32> -> @xs:Array<F32> -> RS5
def r2_bx source · line 643 · raw
@r:0x82db62d229f00333d556466609c8ab27/main.RS3 -> @yc:Array<F32> -> @ys:Array<F32> -> RS5
def r2_y source · line 649 · raw
@+e:U32 -> @+fwd:Bool -> @+h:U32 -> @+hy:U32 -> @+hx:U32 -> @+w:U32 -> @+nt:U32 -> @+tw:U32 -> @s:RS5 -> RS5
the y run's pair e: row e / (h·hy), head (e mod h·hy) / hy, j = e mod hy; the pair (base + j, base + j + hy), the table entry the row's y = (r mod nt) / tw times hy, plus j
def r2_x source · line 661 · raw
@+e:U32 -> @+fwd:Bool -> @+h:U32 -> @+hy:U32 -> @+hx:U32 -> @+w:U32 -> @+nt:U32 -> @+tw:U32 -> @s:RS5 -> RS5
the x run's pair e: row e / (h·hx), head (e mod h·hx) / hx, j = e mod hx; the head's last 2·hx numbers, so the pair is (base + 2·hy + j, base + 2·hy + j + hx), and the table entry the row's x = (r mod nt) mod tw times hx, plus j
def r2_pick source · line 671 · raw
@isy:Bool -> @+e:U32 -> @+ny:U32 -> @+fwd:Bool -> @+h:U32 -> @+hy:U32 -> @+hx:U32 -> @+w:U32 -> @+nt:U32 -> @+tw:U32 -> @s:RS5 -> RS5
def r2_loop source · line 676 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+g:ER2 -> @s:RS5 -> RS5
def rs5_x source · line 682 · raw
@s:RS5 -> Array<F32>
def er2_n source · line 686 · raw
@+g:ER2 -> U32
def lf_r2 source · line 691 · raw
@+t:U32 -> @+l:U32 -> @+g:ER2 -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @x:Array<F32> -> Array<F32>
x1, x2 = the y table's cos and sin, x3, x4 = the x table's; x rotated in place
def rope2_go2 source · line 694 · raw
@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+th:Nat -> @+tw:Nat -> @+h:Nat -> @+hy:Nat -> @+hx:Nat -> @x:Array<F32> -> @yt:0x82db62d229f00333d556466609c8ab27/main.CS -> @xt:0x82db62d229f00333d556466609c8ab27/main.CS -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>
def rope2_go source · line 699 · raw
@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+th:Nat -> @+tw:Nat -> @+h:Nat -> @+hy:Nat -> @+hx:Nat -> @+base:F32 -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>
def RoPE2d source · line 704 · raw
@+n:Nat -> @+w:Nat -> @+th:Nat -> @+tw:Nat -> @+h:Nat -> @+half_y:Nat -> @+half_x:Nat -> @+base:F32 -> @-e:{w == Nat.mul(h, Nat.add(Nat.mul(half_y, 2n), Nat.mul(half_x, 2n))) : Nat} -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, Unit>RoPE2d on n = b·th·tw rows of h heads of 2·half_y + 2·half_x (main.bend's RoPE2d)
def toff source · line 735 · raw
@+bh:U32 -> @+i:U32 -> @+tr:U32 -> U32
where row i of head bh starts in P and G, which keep only the lower triangle (j <= i), row after row: main.bend's P and G are t·t with zeros above the diagonal, and a product by one of those zeros adds +0.0 or -0.0 to a sum that started at +0.0, which leaves it as it is; so the sums below skip them and give the same bits (for finite numbers: an infinity or a NaN in a masked key's row would reach main.bend's sums as 0·inf = NaN, and does not reach these)
def eac_n source · line 738 · raw
@+c:EAC -> U32
def s3_c source · line 745 · raw
@s:S3 -> Array<F32>
def sc_fin source · line 749 · raw
@y:Array<F32> -> @s:S3 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def pv_val source · line 755 · raw
@+e:U32 -> @+c:EAC -> @a:Array<F32> -> @b:Array<F32> -> 0x82db62d229f00333d556466609c8ab27/main.D2
the cell e = row·w + col of o (a = P, b = V) or of dQ (a = G, b = K): row i of P (or G) for the query's head, its i + 1 positions, · V's (or K's) column of the head's key/value head
def tdot source · line 767 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+i:U32 -> @+sb:U32 -> @acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> 0x82db62d229f00333d556466609c8ab27/main.D2
acc + a[ia]·b[ib] + .. down column j of a head's packed triangle (row i + 1's cell is i + 1 after row i's) and b's rows (sb apart), from row i on: dot0's sum, but with the step in a growing by one a row
def tsum_h source · line 774 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+j:U32 -> @+tn:Nat -> @+w:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> 0x82db62d229f00333d556466609c8ab27/main.D2
acc + the group's heads' sums, in head order (main.bend's pacc): a's column j (tr apart a head) · b's column (hd apart a head), rows j..t-1
def kv_val source · line 781 · raw
@+e:U32 -> @+c:EAC -> @a:Array<F32> -> @b:Array<F32> -> 0x82db62d229f00333d556466609c8ab27/main.D2
the cell e = row·kvw + col of dK (a = G, b = Q) or of dV (a = P, b = dO): key j = row % t, over the group's g query heads: the head's column j of a (rows j..t-1) · the head's column of b
def tk_src source · line 796 · raw
@+e:U32 -> @+t32:U32 -> @+kv:U32 -> @+hd32:U32 -> @+kvw:U32 -> U32
K and V transposed for the score cells (kᵀ: b·kv·hd·t, by sequence, key/value head, column c, position j), so that neighbouring lanes, which take neighbouring keys j, read neighbouring cells. dot0 reads kᵀ with stride t: the same numbers in the same order as reading k's row, so the same bits.
def tk_put source · line 802 · raw
@+e:U32 -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def tk_loop source · line 806 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+t32:U32 -> @+kv:U32 -> @+hd32:U32 -> @+kvw:U32 -> @s:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def o2_b source · line 811 · raw
@s:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> Array<F32>
def lf_tk source · line 816 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
x1 = k; kᵀ written (made here)
def tkv_k source · line 821 · raw
@v:Array<F32> -> @vt:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> S4
both: cells [0, nk) are kᵀ's from k, the next nk vᵀ's from v (S4{k, v, kᵀ, vᵀ})
def tkv_v source · line 825 · raw
@k:Array<F32> -> @kt:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> S4
def tkv_sel source · line 829 · raw
@lo:Bool -> @+e:U32 -> @+nk:U32 -> @+t32:U32 -> @+kv:U32 -> @+hd32:U32 -> @+kvw:U32 -> @s:S4 -> S4
def tkv_loop source · line 834 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+nk:U32 -> @+t32:U32 -> @+kv:U32 -> @+hd32:U32 -> @+kvw:U32 -> @s:S4 -> S4
def tkv_o source · line 839 · raw
@dq:Array<F32> -> @s:S4 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3
def lf_tkv source · line 844 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O3
x1 = k, x2 = v; outputs kᵀ and vᵀ (written), dQ (made here, written by backward 3)
def ij_of source · line 854 · raw
@+e:U32 -> @+t32:U32 -> IJ
def sc_put source · line 863 · raw
@+e:U32 -> @+sc:F32 -> @p:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S3
forward 1: P's cell (i, j) = (q's row i · k's row j) * sc, as qrow's dot and pw's product (S3{q, kᵀ, p})
def sc_if source · line 867 · raw
@ok:Bool -> @+e:U32 -> @+qa:U32 -> @+ka:U32 -> @+hd:Nat -> @+t32:U32 -> @+sc:F32 -> @s:S3 -> S3
def sc_ij source · line 872 · raw
@+a:IJ -> @+t32:U32 -> @+h:U32 -> @+g:U32 -> @+hd:Nat -> @+hd32:U32 -> @+w:U32 -> @+sc:F32 -> @+tr:U32 -> @s:S3 -> S3
def sc_cell source · line 876 · raw
@+e:U32 -> @+c:EAC -> @s:S3 -> S3
def sc_loop source · line 880 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S3 -> S3
def lf_sc source · line 886 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = q, x2 = k; outputs P (written) and o (made here, written by forward 3)
def sm_loop source · line 891 · raw
@k:Nat -> @+r:U32 -> @+l:U32 -> @+t32:U32 -> @+tr:U32 -> @p:Array<F32> -> Array<F32>
forward 2: P's row r (query i = r % t) by main.bend's softmax over its i + 1 positions
def lf_sm source · line 896 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @p:Array<F32> -> Array<F32>
def put_c source · line 901 · raw
@+e:U32 -> @o:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S3
forward 3 (and backward 3's dQ): o[e] from pv_val (S3{a, b, o})
def pv_loop source · line 905 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S3 -> S3
def lf_pv source · line 911 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
x1 = P, x2 = v; o written in place
def b1_g source · line 918 · raw
@+e:U32 -> @q:Array<F32> -> @k:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S6
def b1_p source · line 922 · raw
@+e:U32 -> @+sc:F32 -> @+qa:U32 -> @+ka:U32 -> @+hd:Nat -> @+t32:U32 -> @v:Array<F32> -> @d:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S6
def b1_if source · line 926 · raw
@ok:Bool -> @+e:U32 -> @+qa:U32 -> @+ka:U32 -> @+hd:Nat -> @+t32:U32 -> @+sc:F32 -> @s:S6 -> S6
def b1_ij source · line 931 · raw
@+a:IJ -> @+t32:U32 -> @+h:U32 -> @+g:U32 -> @+hd:Nat -> @+hd32:U32 -> @+w:U32 -> @+sc:F32 -> @+tr:U32 -> @s:S6 -> S6
def b1_cell source · line 935 · raw
@+e:U32 -> @+c:EAC -> @s:S6 -> S6
def b1_loop source · line 939 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S6 -> S6
def b1_o source · line 944 · raw
@s:S6 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_b1 source · line 949 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = q, x2 = kᵀ, x3 = vᵀ, x4 = dO; outputs P and G (made here, written)
def gr_2 source · line 955 · raw
@+n:Nat -> @+off:U32 -> @+sc:F32 -> @g:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.PC -> 0x82db62d229f00333d556466609c8ab27/main.A2
backward 2: row r of P by the softmax, then G's row: s = dP·P over the i + 1 positions and G = (P * (dP - s)) * sc, main.bend's gr3 and g_set2
def gr_loop source · line 959 · raw
@k:Nat -> @+r:U32 -> @+l:U32 -> @+t32:U32 -> @+tr:U32 -> @+sc:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x82db62d229f00333d556466609c8ab27/main.A2
def a2_o2 source · line 964 · raw
@s:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_gr source · line 968 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def b3_q source · line 976 · raw
@+e:U32 -> @q:Array<F32> -> @d:Array<F32> -> @p:Array<F32> -> @dq:Array<F32> -> @dk:Array<F32> -> @v:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S8
def b3_k source · line 980 · raw
@+e:U32 -> @k:Array<F32> -> @d:Array<F32> -> @p:Array<F32> -> @dq:Array<F32> -> @dk:Array<F32> -> @v:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S8
def b3_v source · line 984 · raw
@+e:U32 -> @q:Array<F32> -> @k:Array<F32> -> @g:Array<F32> -> @dq:Array<F32> -> @dk:Array<F32> -> @v:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> S8
def b3_sel source · line 988 · raw
@a:Bool -> @b:Bool -> @+e:U32 -> @+nw:U32 -> @+nk:U32 -> @+c:EAC -> @s:S8 -> S8
def b3_cell source · line 994 · raw
@+e:U32 -> @+c:EAC -> @s:S8 -> S8
def b3_loop source · line 998 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S8 -> S8
def b3_o source · line 1003 · raw
@s:S8 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4
def lf_b3 source · line 1008 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4
x1 = q, x2 = k, x3 = dO, x4 = G; P read from the first output (unchanged), dQ, dK and dV (over V) written
def eac source · line 1013 · raw
@+cnt:Nat -> @+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> EAC
the sizes of a bang of cnt cells (or rows)
def pcells source · line 1017 · raw
@+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> Nat
P's cells (b·h·t(t + 1)/2), the score cells dealt in pairs of rows (b·h·ceil(t/2)·(t + 1)) and P's rows (b·h·t)
def tcells source · line 1020 · raw
@+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> Nat
def prows source · line 1023 · raw
@+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> Nat
def af3 source · line 1026 · raw
@-n:Nat -> @-w:Nat -> @-kvw:Nat -> @q:Array<F32> -> @k:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af2 source · line 1030 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @o:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af1 source · line 1034 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @k:Array<F32> -> @v:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af0 source · line 1038 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @v:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def attn_f source · line 1042 · 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>
def ab4 source · line 1046 · raw
@-n:Nat -> @-w:Nat -> @-kvw:Nat -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R4 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
def ab3 source · line 1051 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @d:Array<F32> -> @dq:Array<F32> -> @kt:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
backward 3 writes dK over kᵀ and dV over v (neither read any more)
def ab2 source · line 1055 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @k:Array<F32> -> @v:Array<F32> -> @dq:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
def ab1 source · line 1059 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @d:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R3 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
def attn_b source · line 1063 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.AttnS -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
def toff_f source · line 1071 · raw
@+bh:U32 -> @+i:U32 -> @+t32:U32 -> @+tr:U32 -> U32
def pcf source · line 1074 · raw
@+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> Nat
def eacf source · line 1077 · raw
@+cnt:Nat -> @+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> EAC
def ij_of_f source · line 1080 · raw
@+e:U32 -> @+t32:U32 -> IJ
def sc_ij_f source · line 1085 · raw
@+a:IJ -> @+t32:U32 -> @+h:U32 -> @+g:U32 -> @+hd:Nat -> @+hd32:U32 -> @+w:U32 -> @+sc:F32 -> @+tr:U32 -> @s:S3 -> S3
def sc_cell_f source · line 1089 · raw
@+e:U32 -> @+c:EAC -> @s:S3 -> S3
def sc_loop_f source · line 1093 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S3 -> S3
def lf_sc_f source · line 1098 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def sm_loop_f source · line 1103 · raw
@k:Nat -> @+r:U32 -> @+l:U32 -> @+t32:U32 -> @+tr:U32 -> @p:Array<F32> -> Array<F32>
softmax over all t of row i, at the dense row start
def lf_sm_f source · line 1108 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @p:Array<F32> -> Array<F32>
def pv_val_f source · line 1112 · raw
@+e:U32 -> @+c:EAC -> @a:Array<F32> -> @b:Array<F32> -> 0x82db62d229f00333d556466609c8ab27/main.D2
def pv_loop_f source · line 1122 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S3 -> S3
def lf_pv_f source · line 1127 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def af3_f source · line 1130 · raw
@-n:Nat -> @-w:Nat -> @-kvw:Nat -> @q:Array<F32> -> @k:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af2_f source · line 1134 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @o:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af1_f source · line 1138 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @k:Array<F32> -> @v:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def af0_f source · line 1142 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @v:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.AttnS>
def attn_f_f source · line 1146 · 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>
def b1_ij_f source · line 1155 · raw
@+a:IJ -> @+t32:U32 -> @+h:U32 -> @+g:U32 -> @+hd:Nat -> @+hd32:U32 -> @+w:U32 -> @+sc:F32 -> @+tr:U32 -> @s:S6 -> S6
backward 1, dense: the score cell as forward 1, and G's dP = dO's row · v's row, at the dense offsets
def b1_cell_f source · line 1159 · raw
@+e:U32 -> @+c:EAC -> @s:S6 -> S6
def b1_loop_f source · line 1163 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S6 -> S6
def lf_b1_f source · line 1169 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = q, x2 = kᵀ, x3 = vᵀ, x4 = dO; outputs P and G-dP (made here, written)
def gr_2_f source · line 1175 · raw
@+n:Nat -> @+off:U32 -> @+sc:F32 -> @g:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.PC -> 0x82db62d229f00333d556466609c8ab27/main.A2
backward 2, dense: row r's softmax over all t, then G's row: s = dP·P over all t and G = (P * (dP - s)) * sc. gr_2's own arithmetic: it takes the row's count and offset, so the dense lane only changes those two numbers.
def gr_loop_f source · line 1178 · raw
@k:Nat -> @+r:U32 -> @+l:U32 -> @+t32:U32 -> @+tr:U32 -> @+sc:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.A2 -> 0x82db62d229f00333d556466609c8ab27/main.A2
def lf_gr_f source · line 1183 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def ddot source · line 1188 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> 0x82db62d229f00333d556466609c8ab27/main.D2
acc + a's cells ia, ia + sa, .. times b's ib, ib + sb, .. (k of them): dot0's sum with the strides given, as tdot
def dsum_h source · line 1195 · raw
@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+tn:Nat -> @+t32:U32 -> @+w:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @r:0x82db62d229f00333d556466609c8ab27/main.D2 -> 0x82db62d229f00333d556466609c8ab27/main.D2
acc + the group's heads' sums, in head order (main.bend's pacc): a's column j from row 0 (rows t32 apart) · b's column (rows w apart), all tn rows; the dense form of tsum_h and tdot
def kv_val_f source · line 1202 · raw
@+e:U32 -> @+c:EAC -> @a:Array<F32> -> @b:Array<F32> -> 0x82db62d229f00333d556466609c8ab27/main.D2
the cell e = row·kvw + col of dK (a = G, b = Q) or of dV (a = P, b = dO), dense: key j = row % t, over the group's g query heads, each the head's column j of a from row 0 (t apart a row) · the head's column of b (w apart a row)
def b3_sel_f source · line 1215 · raw
@a:Bool -> @b:Bool -> @+e:U32 -> @+nw:U32 -> @+nk:U32 -> @+c:EAC -> @s:S8 -> S8
backward 3, dense: cells [0, nw) are dQ's, [nw, nw + nk) dK's, the next nk dV's (S8 as the causal lane's)
def b3_cell_f source · line 1221 · raw
@+e:U32 -> @+c:EAC -> @s:S8 -> S8
def b3_loop_f source · line 1225 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+c:EAC -> @s:S8 -> S8
def lf_b3_f source · line 1231 · raw
@+t:U32 -> @+l:U32 -> @+c:EAC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O4
x1 = q, x2 = k, x3 = dO, x4 = G; P read from the first output (unchanged), dQ, dK and dV (over V) written
def ab3_f source · line 1236 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @d:Array<F32> -> @dq:Array<F32> -> @kt:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
backward 3's bang: dense cells, dK over kᵀ and dV over V (neither read any more)
def ab2_f source · line 1241 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @k:Array<F32> -> @v:Array<F32> -> @dq:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
backward 2's bang: the softmax over all t, then G, in place
def ab1_f source · line 1246 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @q:Array<F32> -> @d:Array<F32> -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R3 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
backward 1's bang: dense P and G-dP (made here), kᵀ and vᵀ read
def attn_b_f source · line 1251 · raw
@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+t:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @+sc:F32 -> @s:0x82db62d229f00333d556466609c8ab27/main.AttnS -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<Unit, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x82db62d229f00333d556466609c8ab27/main.Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>>
x1 = q, x2 = k, x3 = v, x4 = dO; kᵀ and vᵀ made here, then dQ, dK and dV
def Attention source · line 1256 · 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>causal attention with grouped heads over b sequences of t positions (main.bend's Attention)
def FullAttention source · line 1261 · 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>the same layer with every key visible (main.bend's FullAttention on the device); Attention is untouched, so the causal lane's bits stay as they are
def ms_add source · line 1276 · raw
@+s:F32 -> @r:MS -> MS
def ms_then source · line 1280 · raw
@r:MS -> @k:(@+s:F32 -> @_:Array<F32> -> @_:Array<F32> -> MS) -> MS
def ms_cell source · line 1284 · raw
@+i:U32 -> @+kk:F32 -> @ry:Pair(Array<F32>, F32) -> @rt:Pair(Array<F32>, F32) -> MS
def ms_blk source · line 1289 · raw
@p:Nat -> @+i:U32 -> @+h:U32 -> @+kk:F32 -> @y:Array<F32> -> @t:Array<F32> -> MS
the squares of the block of 2^p cells from i summed as a balanced tree of pairs (h = 2^(p-1)), gradients written
def ms_put source · line 1294 · raw
@+t:U32 -> @ps:Array<F32> -> @r:MS -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def ms_if source · line 1298 · raw
@ok:Bool -> @+t:U32 -> @+p:Nat -> @+bs:U32 -> @+kk:F32 -> @tg:Array<F32> -> @y:Array<F32> -> @ps:Array<F32> -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_mse source · line 1304 · raw
@+t:U32 -> @+l:U32 -> @+e:EMS -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
x1 = the target; y written in place with the gradient, ps[t] = leaf t's block sum (one leaf a block)
def ms_p source · line 1309 · raw
@k:Nat -> @+m:U32 -> Nat
the largest p <= k with 2^p dividing m (m > 0)
def mse_fin source · line 1315 · raw
@-n:Nat -> @-c:Nat -> @+nb:U32 -> @+nc:F32 -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def mse_go source · line 1319 · raw
@+n:Nat -> @+c:Nat -> @+m:U32 -> @+p:Nat -> @y:Array<F32> -> @t:Array<F32> -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def MSE source · line 1323 · 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>>
mean squared error over every number (main.bend's MSE, the same bits)
def u2f source · line 1330 · raw
@x:U32 -> F32
def f2u source · line 1334 · raw
@x:F32 -> U32
def ia_go source · line 1341 · raw
@left:Nat -> @ids:List<&2, Nat> -> @+i:U32 -> @k:Nat -> @a:Array<F32> -> IA
def ids_arr source · line 1346 · raw
@+n:Nat -> @ids:List<&2, Nat> -> IA
def ef_3 source · line 1360 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @ids:Array<F32> -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> ES3
def ef_2 source · line 1364 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @e:Array<F32> -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> ES3
def ef_cell source · line 1369 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @st:ES3 -> ES3
o[i, j] = e[ids[i], j]
def ef_loop source · line 1373 · raw
@k:Nat -> @+i:U32 -> @+j:U32 -> @+d:U32 -> @st:ES3 -> ES3
def es3_o source · line 1378 · raw
@st:ES3 -> Array<F32>
def ef_if source · line 1382 · raw
@ok:Bool -> @+t:U32 -> @+k:Nat -> @+d:U32 -> @e:Array<F32> -> @ids:Array<F32> -> @o:Array<F32> -> Array<F32>
def lf_embf source · line 1388 · raw
@+t:U32 -> @+l:U32 -> @+e:EEM -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
x1 = the table, x2 = the ids; o (made on the device, zeros) gets the rows
def eb_4 source · line 1392 · raw
@+gi:U32 -> @ids:Array<F32> -> @g:Array<F32> -> @+a:F32 -> @r:Pair(Array<F32>, F32) -> ES3
def eb_3 source · line 1396 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @+gi:U32 -> @ids:Array<F32> -> @dy:Array<F32> -> @r:Pair(Array<F32>, F32) -> ES3
def eb_2 source · line 1400 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @dy:Array<F32> -> @g:Array<F32> -> @r:Pair(Array<F32>, F32) -> ES3
def eb_cell source · line 1405 · raw
@+i:U32 -> @+j:U32 -> @+d:U32 -> @st:ES3 -> ES3
g[ids[i], j] = g[ids[i], j] + dy[i, j]
def eb_loop source · line 1409 · raw
@k:Nat -> @+i:U32 -> @+j:U32 -> @+d:U32 -> @st:ES3 -> ES3
def eb_if source · line 1414 · raw
@ok:Bool -> @+t:U32 -> @+k:Nat -> @+d:U32 -> @dy:Array<F32> -> @ids:Array<F32> -> @g:Array<F32> -> Array<F32>
def lf_embb source · line 1420 · raw
@+t:U32 -> @+l:U32 -> @+e:EEM -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @g:Array<F32> -> Array<F32>
x1 = dy, x2 = the ids; the gradient g, in place
def emb_f3 source · line 1424 · raw
@-n:Nat -> @-v:Nat -> @-d:Nat -> @+ids:List<&2, Nat> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, List<&2, Nat>>
def emb_f2 source · line 1428 · raw
@+n:Nat -> @+v:Nat -> @+d:Nat -> @+ids:List<&2, Nat> -> @ea:Array<F32> -> @r:IA -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, List<&2, Nat>>
def emb_f source · line 1432 · raw
@+n:Nat -> @+v:Nat -> @+d:Nat -> @+ids:List<&2, Nat> -> @e:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d> -> 0x82db62d229f00333d556466609c8ab27/main.Fwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d>, List<&2, Nat>>
def emb_b3 source · line 1436 · raw
@-v:Nat -> @-d:Nat -> @e:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, List<&2, Nat>>
def emb_b2 source · line 1440 · raw
@+v:Nat -> @+d:Nat -> @e:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d> -> @da:Array<F32> -> @ga:Array<F32> -> @r:IA -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, List<&2, Nat>>
def emb_bwd source · line 1444 · raw
@+n:Nat -> @+v:Nat -> @+d:Nat -> @e:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d> -> @ids:List<&2, Nat> -> @dy:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, d> -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<v, d>, List<&2, Nat>>
def Embedding source · line 1449 · 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>>
a table of v rows of d numbers, looked up by n ids (main.bend's Embedding, the same bits); its step on the device
def ts_leaf source · line 1466 · raw
@r:Pair(Array<F32>, F32) -> TS
def ts_plus source · line 1470 · raw
@+s:F32 -> @r:TS -> TS
def ts_then source · line 1474 · raw
@r:TS -> @k:(@+s:F32 -> @_:Array<F32> -> TS) -> TS
def tnode source · line 1481 · raw
@l:Nat -> @big:Bool -> @+lo:U32 -> @+len:U32 -> @+h:U32 -> @a:Array<F32> -> TS
tree_sum of the len cells from lo, read in place: a node l passes above the cells sums its left child (the first h = 2^(l-1) cells) and its right child (the rest) when there is a rest (big = h < len), and is its left child otherwise (tree_sum's odd last cell carried up a pass)
def ts_lv source · line 1488 · raw
@f:Nat -> @+m:U32 -> @+p:U32 -> Nat
the passes tree_sum makes over m cells: the smallest l with 2^l >= m (f is fuel)
def ce_t2 source · line 1499 · raw
@ok:Bool -> @+loss:F32 -> @+xm:F32 -> @r:Pair(Array<F32>, F32) -> LS
def ce_g4 source · line 1504 · raw
@+n:Nat -> @-c:Nat -> @z:Array<F32> -> @r:LS -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def c1_3 source · line 1512 · raw
@+t:U32 -> @+m:F32 -> @st:Array<F32> -> @r:Pair(Array<F32>, F32) -> Array<F32>
def c1_2 source · line 1516 · raw
@ok:Bool -> @+t:U32 -> @+off:U32 -> @+y:U32 -> @st:Array<F32> -> @r:0x82db62d229f00333d556466609c8ab27/main.AS -> Array<F32>
def c1_1 source · line 1521 · raw
@+t:U32 -> @+off:U32 -> @+c1:Nat -> @+c32:U32 -> @z:Array<F32> -> @st:Array<F32> -> @r:Pair(Array<F32>, F32) -> Array<F32>
def c1_if source · line 1525 · raw
@ok:Bool -> @+t:U32 -> @+c1:Nat -> @+c32:U32 -> @lab:Array<F32> -> @z:Array<F32> -> @st:Array<F32> -> Array<F32>
def lf_c1 source · line 1530 · raw
@+t:U32 -> @+l:U32 -> @+e:EC1 -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @st:Array<F32> -> Array<F32>
def tsx_leaf source · line 1535 · raw
@+lo:U32 -> @+m:F32 -> @r:Pair(Array<F32>, F32) -> TS
2. the 2^l cells from lo: e = exp(x - m) written over each, and their sum as a balanced tree of pairs (h = 2^(l-1))
def tsx source · line 1541 · raw
@l:Nat -> @+lo:U32 -> @+h:U32 -> @+m:F32 -> @a:Array<F32> -> TS
def x8_7 source · line 1548 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @+e2:F32 -> @+e3:F32 -> @+e4:F32 -> @+e5:F32 -> @+e6:F32 -> @r:Pair(Array<F32>, F32) -> TS
the same over 8 cells in straight-line code (no closure a node): e0..e7 written, then ((e0 + e1) + (e2 + e3)) + ((e4 + e5) + (e6 + e7)), the balanced tree's three levels
def x8_6 source · line 1554 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @+e2:F32 -> @+e3:F32 -> @+e4:F32 -> @+e5:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_5 source · line 1560 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @+e2:F32 -> @+e3:F32 -> @+e4:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_4 source · line 1566 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @+e2:F32 -> @+e3:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_3 source · line 1572 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @+e2:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_2 source · line 1578 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @+e1:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_1 source · line 1584 · raw
@+lo:U32 -> @+m:F32 -> @+e0:F32 -> @r:Pair(Array<F32>, F32) -> TS
def x8_0 source · line 1590 · raw
@+lo:U32 -> @+m:F32 -> @r:Pair(Array<F32>, F32) -> TS
def tsx8 source · line 1597 · raw
@l:Nat -> @+lo:U32 -> @+h:U32 -> @+m:F32 -> @a:Array<F32> -> TS
tsx over 2^(l+3) cells, its last three levels in blocks of 8
def c2_put source · line 1609 · raw
@+b:U32 -> @st:Array<F32> -> @bs:Array<F32> -> @r:TS -> C3S
def c2_m source · line 1613 · raw
@b8:Bool -> @+b:U32 -> @+lo:U32 -> @+p:Nat -> @+h:U32 -> @z:Array<F32> -> @bs:Array<F32> -> @r:Pair(Array<F32>, F32) -> C3S
def c2_cell source · line 1618 · raw
@+b:U32 -> @+per:U32 -> @+p:Nat -> @+bsz:U32 -> @+c32:U32 -> @s:C3S -> C3S
def c2_loop source · line 1622 · raw
@k:Nat -> @+b:U32 -> @+l:U32 -> @+per:U32 -> @+p:Nat -> @+bsz:U32 -> @+c32:U32 -> @s:C3S -> C3S
def c2_o source · line 1627 · raw
@s:C3S -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def lf_c2 source · line 1631 · raw
@+t:U32 -> @+l:U32 -> @+e:EC2 -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2 -> 0x14e5af4fd3ad6b86b8b16959a9f5960b/main.O2
def c3_put source · line 1639 · raw
@+t:U32 -> @st:Array<F32> -> @r:TS -> Array<F32>
def c3_if source · line 1643 · raw
@ok:Bool -> @+t:U32 -> @+per:U32 -> @+tl:Nat -> @+th:U32 -> @bs:Array<F32> -> @st:Array<F32> -> Array<F32>
def lf_c3 source · line 1648 · raw
@+t:U32 -> @+l:U32 -> @+e:EC3 -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @st:Array<F32> -> Array<F32>
def c4_4 source · line 1656 · raw
@+i:U32 -> @+j:U32 -> @+y:U32 -> @+s:F32 -> @+nf:F32 -> @lab:Array<F32> -> @st:Array<F32> -> @r:Pair(Array<F32>, F32) -> C3S
def c4_3 source · line 1660 · raw
@+i:U32 -> @+j:U32 -> @+y:U32 -> @+nf:F32 -> @lab:Array<F32> -> @z:Array<F32> -> @r:Pair(Array<F32>, F32) -> C3S
def c4_2 source · line 1664 · raw
@+i:U32 -> @+row:U32 -> @+j:U32 -> @+nf:F32 -> @st:Array<F32> -> @z:Array<F32> -> @r:Pair(Array<F32>, F32) -> C3S
def c4_cell source · line 1668 · raw
@+i:U32 -> @+c32:U32 -> @+nf:F32 -> @s:C3S -> C3S
def c4_loop source · line 1672 · raw
@k:Nat -> @+i:U32 -> @+l:U32 -> @+c32:U32 -> @+nf:F32 -> @s:C3S -> C3S
def c3s_c source · line 1677 · raw
@s:C3S -> Array<F32>
def lf_c4 source · line 1681 · raw
@+t:U32 -> @+l:U32 -> @+e:EC4 -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @z:Array<F32> -> Array<F32>
def cx_t1 source · line 1686 · raw
@ok:Bool -> @+i:U32 -> @+loss:F32 -> @r:Pair(Array<F32>, F32) -> LS
the host's part over st, row by row
def cx_ls source · line 1690 · raw
@left:Nat -> @ls:List<&2, Nat> -> @+i:U32 -> @+c:Nat -> @r:LS -> LS
def cx_5 source · line 1695 · raw
@+n:Nat -> @+c:Nat -> @+labels:List<&2, Nat> -> @+k:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def cx_4 source · line 1699 · raw
@+n:Nat -> @+c:Nat -> @+labels:List<&2, Nat> -> @+k:Nat -> @la:Array<F32> -> @z:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def cx_3 source · line 1703 · raw
@+n:Nat -> @+c:Nat -> @+labels:List<&2, Nat> -> @+k:Nat -> @+per:U32 -> @la:Array<F32> -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R2 -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def cx_2 source · line 1709 · raw
@+n:Nat -> @+c:Nat -> @+labels:List<&2, Nat> -> @+k:Nat -> @+p:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def cx_1 source · line 1713 · raw
@+n:Nat -> @+c:Nat -> @+labels:List<&2, Nat> -> @z:Array<F32> -> @r:IA -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
def CrossEntropy source · line 1718 · raw
@+n:Nat -> @+c:Nat -> @z:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c> -> @+labels:List<&2, Nat> -> 0x82db62d229f00333d556466609c8ab27/main.LossOut<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>>
cross-entropy over n rows of c classes against the labels (main.bend's CrossEntropy; its bits but CUDA's exp)
def eic source · line 1745 · raw
@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> EIC
def eic_n source · line 1748 · raw
@+g:EIC -> U32
def i2c_set source · line 1753 · raw
@+e:U32 -> @r:Pair(Array<F32>, F32) -> @o:Array<F32> -> CIC
the cell a read hands back, written at e
def i2c_put source · line 1758 · raw
@+e:U32 -> @ok:Bool -> @+src:U32 -> @s:CIC -> CIC
cell e = the value at src when the window is inside, else 0.0 (as p_rc gives it)
def i2c_cell source · line 1765 · raw
@+e:U32 -> @+g:EIC -> @s:CIC -> CIC
def i2c_loop source · line 1788 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+g:EIC -> @s:CIC -> CIC
def cic_o source · line 1793 · raw
@s:CIC -> Array<F32>
def lf_i2c source · line 1797 · raw
@+t:U32 -> @+l:U32 -> @+g:EIC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def i2c_fin source · line 1800 · raw
@-n:Nat -> @-h:Nat -> @-w:Nat -> @-c:Nat -> @-k:Nat -> @-p:Nat -> @-s:Nat -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(0x82db62d229f00333d556466609c8ab27/main.ocount(h, p, k, s), 0x82db62d229f00333d556466609c8ab27/main.ocount(w, p, k, s))), Nat.mul(c, Nat.mul(k, k))>
def im2col source · line 1805 · raw
@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(h, w)), c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(0x82db62d229f00333d556466609c8ab27/main.ocount(h, p, k, s), 0x82db62d229f00333d556466609c8ab27/main.ocount(w, p, k, s))), Nat.mul(c, Nat.mul(k, k))>
the gathered matrix: n·oh·ow rows of c·k·k numbers (main.bend's im2col)
def gi_put source · line 1811 · raw
@+a:F32 -> @r:Pair(Array<F32>, F32) -> @o:Array<F32> -> GIS
dp[j] added into the running sum when the candidate is a patch that reads this cell
def gi_add source · line 1815 · raw
@ok:Bool -> @+j:U32 -> @s:GIS -> GIS
def gi_qx source · line 1823 · raw
@k:Nat -> @+yok:Bool -> @+qx:U32 -> @+ox0:U32 -> @+oy:U32 -> @+ky:U32 -> @+ix:U32 -> @+b:U32 -> @+ch:U32 -> @+g:EIC -> @r:GIS -> GIS
the candidates along x, in increasing ox: ky is the patch's row offset (from gi_qy)
def gi_qy source · line 1838 · raw
@k:Nat -> @+qy:U32 -> @+oy0:U32 -> @+ox0:U32 -> @+iy:U32 -> @+ix:U32 -> @+b:U32 -> @+ch:U32 -> @+g:EIC -> @r:GIS -> GIS
the candidates along y, in increasing oy, each running gi_qx in increasing ox
def gis_set source · line 1848 · raw
@+e:U32 -> @s:GIS -> CIC
def gi_cell source · line 1853 · raw
@+e:U32 -> @+g:EIC -> @s:CIC -> CIC
the number col2im writes at image cell e: the sum, in main.bend's order, of the gathered cells that read it
def gi_loop source · line 1868 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+g:EIC -> @s:CIC -> CIC
def gi_n source · line 1874 · raw
@+g:EIC -> U32
the image cells, n·h·w·c of them
def lf_gi source · line 1878 · raw
@+t:U32 -> @+l:U32 -> @+g:EIC -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def gi_fin source · line 1881 · raw
@-n:Nat -> @-h:Nat -> @-w:Nat -> @-c:Nat -> @-k:Nat -> @-p:Nat -> @-s:Nat -> @r:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(h, w)), c>
def col2im source · line 1886 · raw
@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @dp:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(0x82db62d229f00333d556466609c8ab27/main.ocount(h, p, k, s), 0x82db62d229f00333d556466609c8ab27/main.ocount(w, p, k, s))), Nat.mul(c, Nat.mul(k, k))> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(h, w)), c>
the gradient of the gather: n·h·w rows of c numbers (main.bend's col2im)
def conv_b2 source · line 1891 · raw
@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @b:0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.Lin<0x82db62d229f00333d556466609c8ab27/main.ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x82db62d229f00333d556466609c8ab27/main.Lin<0x82db62d229f00333d556466609c8ab27/main.ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.chw(k, hs, ws, p)), c>>
def Conv2d source · line 1900 · raw
@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x82db62d229f00333d556466609c8ab27/main.Lin<0x82db62d229f00333d556466609c8ab27/main.ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.chw(k, hs, ws, p)), c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>>
Conv2d: device im2col, then the device Linear's products; the backward is the device Linear's, then device col2im.
The parameters are main.bend's Lin<c·k·k, co>, so lin_step, lin_zeros, lin_dump, lin_load and lin_store serve it as
they serve Linear. The image's size is the kernel plus a surplus (hs/ws, main.bend's cimg), so a window bigger
than its image cannot be written here either.
def convnb_b2 source · line 1911 · raw
@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @b:0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<co, 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>> -> 0x82db62d229f00333d556466609c8ab27/main.Bwd<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<co, 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.chw(k, hs, ws, p)), c>>
def Conv2dNB source · line 1919 · raw
@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @+par:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<co, 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.chw(k, hs, ws, p)), c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, 0x82db62d229f00333d556466609c8ab27/main.cohw(k, hs, ws, p, s1)), 0x82db62d229f00333d556466609c8ab27/main.ckk(c, k)>>
Conv2dNB (main.bend's Conv2d without the bias): device im2col, then the device LinearNB's products; the backward is the device LinearNB's, then device col2im. The parameters are the co x c·k·k weight alone, so mat_step and main.bend's Mat dump, restore and store serve it as they serve LinearNB.
def eti source · line 1938 · raw
@+n:Nat -> @+t:Nat -> @+c:Nat -> ETI
def tif_cell source · line 1942 · raw
@+e:U32 -> @+t:U32 -> @+c:U32 -> @s:CIC -> CIC
output cell e of row r = e / c, column k = e mod c: the input cell (r / t)·c + k
def tif_loop source · line 1947 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+t:U32 -> @+c:U32 -> @s:CIC -> CIC
def lf_tif source · line 1952 · raw
@+t:U32 -> @+l:U32 -> @+g:ETI -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def tib_rows source · line 1957 · raw
@k:Nat -> @+j:U32 -> @+c:U32 -> @s:GIS -> GIS
the tile's rows of dy at one column, j stepping by c, added into the running sum in increasing row
def tib_cell source · line 1963 · raw
@+e:U32 -> @+t:U32 -> @+c:U32 -> @s:CIC -> CIC
input cell e of image i = e / c, column k = e mod c: the sum of dy's rows i·t .. i·t + t - 1 at column k
def tib_loop source · line 1968 · raw
@k:Nat -> @+e:U32 -> @+l:U32 -> @+t:U32 -> @+c:U32 -> @s:CIC -> CIC
def lf_tib source · line 1973 · raw
@+t:U32 -> @+l:U32 -> @+g:ETI -> @x1:Array<F32> -> @x2:Array<F32> -> @x3:Array<F32> -> @x4:Array<F32> -> @o:Array<F32> -> Array<F32>
def tile_fin source · line 1977 · raw
@-r:Nat -> @-c:Nat -> @q:0x14e5af4fd3ad6b86b8b16959a9f5960b/main.R1 -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>
def tile_fwd source · line 1981 · raw
@+b:Nat -> @+t:Nat -> @+c:Nat -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<b, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(b, t), c>
def tile_bwd source · line 1986 · raw
@+b:Nat -> @+t:Nat -> @+c:Nat -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(b, t), c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<b, c>
def Tile source · line 1992 · raw
@+b:Nat -> @+t:Nat -> @+c:Nat -> 0x82db62d229f00333d556466609c8ab27/main.Layer<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<b, c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(b, t), c>, Unit>
b images (rows) of c numbers, each row broadcast over t tokens: Mat<b, c> to Mat<b·t, c> (main.bend's Tile)
def Linear source · line 2000 · 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 2003 · 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 2006 · 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_loop source · line 73 · raw
@-f:(@+a:F32 -> F32) -> @k:Nat -> @+i:U32 -> @+l:U32 -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> Array<F32>
out[i] = f(x[i]) for the leaf's cells
template ew1 source · line 78 · raw
@-f:(@+a:F32 -> F32) -> @+t:U32 -> @+l:U32 -> @+e:EN -> @x:Array<F32> -> @o:Array<F32> -> Array<F32>
template ew2_loop source · line 83 · raw
@-f:(@+a:F32 -> @+b:F32 -> F32) -> @k:Nat -> @+i:U32 -> @+l:U32 -> @o:Array<F32> -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> Array<F32>
out[i] = f(x[i], y[i]) for the leaf's cells
template ew2 source · line 88 · raw
@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+t:U32 -> @+l:U32 -> @+e:EN -> @x:Array<F32> -> @y:Array<F32> -> @o:Array<F32> -> Array<F32>
template ewi_loop source · line 93 · raw
@-f:(@+a:F32 -> @+b:F32 -> F32) -> @k:Nat -> @+i:U32 -> @+l:U32 -> @ro:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> Array<F32>
o[i] = f(o[i], y[i]) for the leaf's cells, in place (o's old value is the first argument)
template ewi source · line 98 · raw
@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+t:U32 -> @+l:U32 -> @+e:EN -> @o:Array<F32> -> @y:Array<F32> -> Array<F32>
template Residual source · line 221 · 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