~/bend-docscommunity

main.bend checks

raw source on the hub · import bendtorch@1.3.0.0/main.bend as Main

BendTorch: layers with typed forward and backward passes, composed generically, trained in Bend 2.

import ../torch/main.bend as T

A layer is a record of four functions over its parameters P, its input X, its output Y and what its forward pass saves for the backward pass, S:

fwd : P -> X -> Fwd<P, Y, S> the output, the parameters handed back, the saved values bwd : P -> S -> Y -> P -> Bwd<P, X> from dY (typed Y) to dX (typed X), adding dP into a gradient of type P step : Opt -> P -> P -> OptState<P> -> Step<P> one optimizer step on the parameters; the gradient comes back zeroed zeros : Unit -> P a zero P (gradients and optimizer state start from it)

The gradient of the input has the input's type and the gradient of the parameters has the parameters' type, so a backward pass that returns a transposed or a wrongly sized gradient does not compile. Layers are passed as templates (~l), so Seq composes any two layers whose types meet, with no closure called twice and no runtime cost.

Matrices are bend-ml's Mat<r, c> (bend-ml-tensor-array 0.1.14.0): r*c F32 numbers in one flat Array, row by row, the dimensions erased type parameters. An Array has one owner, so every operation hands back what it reads.

What is proved and what is tested: the shapes are in the types (checked); the laws at the end of this file are checked by Bend's kernel; every F32 result is tested against PyTorch by the scripts in reference/, never proved.

3 imports
import Base
import bend-ml-tensor-array@0.1.18.0/main.bend as TA
import bend-ml-nat-lemmas@0.1.2.0/main.bend as NL

Types

type Fwd source · line 32 · raw

@-P:Type -> @-Y:Type -> @-S:Type -> Type

what a forward pass returns: the parameters, the output, what the backward pass will need

type Bwd source · line 36 · raw

@-P:Type -> @-X:Type -> Type

what a backward pass returns: the parameters, the gradient of the input, the accumulated gradient of the parameters

type Two source · line 40 · raw

@-A:Type -> @-B:Type -> Type

a pair of owned values (parameters of two layers or their saved values)

type PTree source · line 56 · raw

Type

AdamW's moments stored as packed BF16 (CAdamWBF16): a tree that follows the parameters' pairs as the step walks them, with the buffers of each matrix at its leaf. A packed buffer has half the words of its matrix (two BF16 values a U32, element 2k in the low half of word k, torch/dtype.bend's layout), so it cannot be a value of the parameters' type P: the tree holds the words itself. A leaf is allocated on the matrix's first step (PNew marks a part not yet stepped, and stays at the parameter-free layers), so the full-size buffers never exist. v16 picks v packed as well; otherwise v is F32 (PLeafV). A leaf keeps its matrix's count n, for checkpoints.

type OptState source · line 63 · raw

@-P:Type -> Type

Owned optimizer buffers: SGD has none, momentum one, Adam/AdamW two (Packed: two, in packed BF16, see PTree).

type Step source · line 99 · raw

@-P:Type -> Type

what an optimizer step returns: the new parameters, the zeroed gradient, the optimizer state

type Opt source · line 105 · raw

Data

The optimizer, with the scalars of the current step. Adam's step is lr / (1 - b1^t) and bc2 is sqrt(1 - b2^t), as PyTorch computes them; Opt.at fills them from an OptCfg and the step count. dec picks AdamW's decoupled weight decay (w *= 1 - lr*wd) over Adam's (g += wd*w).

type PL source · line 111 · raw

@-P:Type -> Type

Parameters and their flat dump in a fixed order. Loading has a separate array cursor (PA).

type PA source · line 116 · raw

@-P:Type -> Type

Loading cursor: the shared source array and next element offset, handed back without a list or a clone. The IO boundary checks the source length against Layer.count before calling the pure loaders.

type Layer source · line 119 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> Type

type Q3 source · line 229 · raw

Type

type Q4 source · line 232 · raw

Type

type MatStep source · line 235 · raw

@-r:Nat -> @-c:Nat -> Type

type PQ source · line 346 · raw

Type

type PQV source · line 349 · raw

Type

type OptCfg source · line 450 · raw

Data

What the user picks; Opt.at turns it into the step's Opt from q1 = 1 - b1^t and q2 = 1 - b2^t (t = 1 on the first step). Adam's c1 and c2 are 1 - b1 and 1 - b2, written as their own constants (adam_m above). CAdamWBF16 is AdamW with its first moment (and its second, when v16) stored as packed BF16 (PTree); weights and gradient stay F32.

type Lin source · line 602 · raw

@-i:Nat -> @-o:Nat -> Type

type LossOut source · line 894 · raw

@-Y:Type -> Type

type Train source · line 902 · raw

@-P:Type -> Type

Parameters, gradient (zero between steps), optimizer-specific buffers, Adam's 1 - b1^t and 1 - b2^t, last loss.

type DropP source · line 1232 · raw

Data

type D2 source · line 1303 · raw

Type

type A2 source · line 1306 · raw

Type

type AS source · line 1322 · raw

Type

type PC source · line 1351 · raw

Type

the array and how many numbers the last loop of a softmax divided

type R3 source · line 1580 · raw

Type

type R5 source · line 1620 · raw

Type

with r = 1/rms, xh = x*r, dxh = dy*g: dx = r * (dxh - xh * mean(dxh * xh)); dg += dy * xh, row after row

type R3S source · line 1623 · raw

Type

type LN source · line 1691 · raw

@-d:Nat -> Type

type ASM source · line 1702 · raw

Type

type R4 source · line 1719 · raw

Type

type R3SS source · line 1758 · raw

Type

with xh = (x - mu) * r and dxh = dy * g: dx = r * ((dxh - mean(dxh)) - xh * mean(dxh * xh)); dg += dy * xh; db += dy

type R4B source · line 1774 · raw

Type

type Three source · line 2161 · raw

@-A:Type -> @-B:Type -> @-C:Type -> Type

type CS source · line 2268 · raw

Type

type RS3 source · line 2285 · raw

Type

type AttnS source · line 2350 · raw

Type

type DR source · line 2366 · raw

Type

type QK source · line 2388 · raw

Type

type PW source · line 2391 · raw

Type

type PR source · line 2402 · raw

Type

type PO source · line 2472 · raw

Type

type FS source · line 2540 · raw

Type

type Dm source · line 2543 · raw

Data

type HC source · line 2548 · raw

Data

the columns query head hi reads: its own hd columns of q (hc) and those of its key/value head kv_of(hi, g) in k and v (kc); both passes take their columns from here (the law kv_cols)

type BS source · line 2593 · raw

Type

type GW source · line 2609 · raw

Type

type GN source · line 2612 · raw

Type

type GR source · line 2623 · raw

Type

type BG source · line 2698 · raw

Type

type Halves source · line 2722 · raw

@-A:Type -> Type

type FL source · line 2739 · raw

Type

type UO source · line 2797 · raw

Type

type BL source · line 2816 · raw

Type

type DB source · line 2860 · raw

Type

type D3 source · line 2884 · raw

Type

type UD source · line 2887 · raw

Type

type AttnDims source · line 2907 · raw

Data

type CSM source · line 3168 · raw

Type

Cross-entropy's softmax over a row of c classes: as psm, but the row's exponentials are summed as a balanced tree (tree_sum, on a copy), since a running F32 sum of SmolLM2's 49152 classes was 3.4 tolerances off PyTorch's loss (NOTES, M10). psm itself keeps its order: attention's rows are short and the law causal reads its count. a row's probabilities, its max m and log of its sum of exp(x - m) (log_softmax's pieces)

type D3S source · line 3253 · raw

Type

type NS source · line 3276 · raw

Type

type PTU source · line 3339 · raw

Type

type PTA source · line 3355 · raw

Type

type UA source · line 3358 · raw

Type

type PK source · line 3428 · raw

Data

what a tag says

type TrainPL source · line 3470 · raw

@-P:Type -> Type

Definitions

def Pair3 source · line 44 · raw

@A:Type -> @B:Type -> @C:Type -> Type

Type helpers for the left-associated pairs used by Seq/Fork and for Residual's two parameter-free layers.

def ResidualType source · line 47 · raw

@A:Type -> Type

def OptState.map source · line 71 · raw

@-P:Type -> @-Q:Type -> @st:OptState<P> -> @f:(@_:P -> Q) -> @h:(@_:P -> Q) -> OptState<Q>

Two affine maps are supplied separately: each is called at most once. A packed tree is the same under any relabelling of the parameters (Lin <-> Two), so it passes unchanged.

def OptState.split source · line 80 · raw

@-A:Type -> @-B:Type -> @st:OptState<Two<A, B>> -> Two<OptState<A>, OptState<B>>

A packed tree splits at its node; a part not yet stepped splits into two such parts. A leaf belongs to one matrix and is never split (it goes to the left, the right side starts empty).

def OptState.join source · line 91 · raw

@-A:Type -> @-B:Type -> @a:OptState<A> -> @b:OptState<B> -> OptState<Two<A, B>>

Pair the buffers present on both sides. Internal callers split one state, then step both children with the same optimizer, so the variants agree. No buffer is ever allocated to fill an absent side.

def Layer.fwd source · line 122 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @p:P -> @x:X -> Fwd<P, Y, S>

def Layer.bwd source · line 127 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @p:P -> @s:S -> @dy:Y -> @g:P -> Bwd<P, X>

def Layer.step source · line 132 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @+o:Opt -> @p:P -> @g:P -> @st:OptState<P> -> Step<P>

def Layer.zeros source · line 137 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> P

def Layer.dump source · line 143 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @p:P -> PL<P>

every number of the parameters, in the layer's fixed order (the parameters handed back)

def Layer.load source · line 149 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @p:P -> @xs:Array<F32> -> @off:U32 -> PA<P>

Parameters overwritten from xs at an element offset, in dump order; xs and the next offset come back.

def Layer.store source · line 154 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> @p:P -> @xs:Array<F32> -> @off:U32 -> PA<P>

The parameters copied into xs from an element offset, in dump order (load's inverse); xs and the next offset come back.

def Layer.count source · line 158 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @l:Layer<P, X, Y, S> -> U32

def ew2_fin source · line 187 · raw

@-r:Nat -> @-c:Nat -> @p:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<r, c>

def f32_add source · line 198 · raw

@+a:F32 -> @+b:F32 -> F32

def f32_mul source · line 201 · raw

@+a:F32 -> @+b:F32 -> F32

def Mat.add source · line 205 · raw

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

(a + b, b)

def zero_of source · line 208 · raw

@+x:F32 -> F32

def depth_go source · line 213 · raw

@f:Nat -> @ok:Bool -> @+n:U32 -> @+p:U32 -> @d:Nat -> Nat

the smallest d with 2^d >= n (TA.cap_depth's answer), by doubling a U32: log2(n) cheap steps, where cap_depth compares Nats one unit at a time at each doubling; f is fuel (32 doublings cover every U32)

def depth source · line 222 · raw

@+n:Nat -> Nat

def sgd_l source · line 239 · raw

@nleft:Nat -> @+t:U32 -> @+lr:F32 -> @rw:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2

w <- w + (-lr)*g, as PyTorch's p.add_(g, alpha=-lr)

def mom_l source · line 247 · raw

@nleft:Nat -> @+t:U32 -> @+lr:F32 -> @+mu:F32 -> @rw:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> @rm:Pair(Array<F32>, F32) -> Q3

buf <- buf*mu + g; w <- w + (-lr)*buf, as PyTorch's SGD with momentum (no dampening, not Nesterov)

def adam_grad source · line 259 · raw

@dec:Bool -> @+wd:F32 -> @+w:F32 -> @+g:F32 -> F32

the Adam family, in PyTorch's order of operations (torch.optim.adam._single_tensor_adam): g' = g + wd*w (Adam) or w' = w*(1 - lr*wd) (AdamW); m = m + (1-b1)*(g' - m) (lerp); v = v*b2 + (1-b2)*g'*g'; w = w' + (-step*m) / (sqrt(v)/bc2 + eps)

def adam_decay source · line 266 · raw

@dec:Bool -> @+lr:F32 -> @+wd:F32 -> @+w:F32 -> F32

def adam_m source · line 276 · raw

@+c1:F32 -> @+m:F32 -> @+g:F32 -> F32

c1 = 1 - b1 and c2 = 1 - b2 are given as their own constants (0.1, 0.001), as PyTorch computes 1 - beta in float64 before rounding: 1 - b2 taken in F32 from b2 = 0.999 (0.99900001287 in F32) is 1.3e-5 too small, and over 100 steps that alone put the tiny Llama 21x further from float64 than PyTorch's float32 (NOTES, 2026-10-18)

def adam_v source · line 279 · raw

@+b2:F32 -> @+c2:F32 -> @+v:F32 -> @+g:F32 -> F32

def adam_w source · line 282 · raw

@+step:F32 -> @+bc2:F32 -> @+eps:F32 -> @+w:F32 -> @+m:F32 -> @+v:F32 -> F32

def adam_l source · line 285 · raw

@nleft:Nat -> @+t: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) -> Q4

def ph_f2u source · line 303 · raw

@x:F32 -> U32

def ph_u2f source · line 308 · raw

@u:U32 -> F32

def ph_pick source · line 313 · raw

@b:Bool -> @+x:U32 -> @+y:U32 -> U32

def ph_of_bits source · line 321 · raw

@+u:U32 -> U32

F32 -> BF16 bits (dtype.bend's BF16.of_f32): add 0x7FFF plus the lowest kept bit, keep the top half; NaN -> 0xFFFF

def ph_of source · line 324 · raw

@x:F32 -> U32

def ph_to source · line 328 · raw

@+h:U32 -> F32

BF16 bits -> F32, exact

def ph_half source · line 332 · raw

@odd:Bool -> @+w:U32 -> U32

the half of word w that element t uses (odd t: the high half), and w with that half replaced by h

def ph_put source · line 339 · raw

@odd:Bool -> @+w:U32 -> @+h:U32 -> U32

def adamh_l source · line 353 · raw

@nleft:Nat -> @+t: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<U32>, U32) -> @rv:Pair(Array<U32>, U32) -> PQ

m and v packed. rm and rv hold the word of element t (index t >> 1), read after the previous element's write.

def adamhv_l source · line 369 · raw

@nleft:Nat -> @+t: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<U32>, U32) -> @rv:Pair(Array<F32>, F32) -> PQV

m packed, v in F32 (one cell an element, as adam_l's)

def ph_words source · line 383 · raw

@+n:Nat -> Array<U32>

the words of n packed values: ceil(n / 2), in a block of the next power of two

def ph_fin source · line 386 · raw

@-r:Nat -> @-c:Nat -> @+n:U32 -> @q:PQ -> MatStep<r, c>

def phv_fin source · line 390 · raw

@-r:Nat -> @-c:Nat -> @+n:U32 -> @q:PQV -> MatStep<r, c>

def adamh_leaf source · line 395 · raw

@+r:Nat -> @+c:Nat -> @+o:Opt -> @wa:Array<F32> -> @ga:Array<F32> -> @t:PTree -> MatStep<r, c>

one matrix's step on its leaf; anything else leaves the values untouched, as Opt.mat's fallback

def ph_new source · line 405 · raw

@+n:Nat -> @v16:Bool -> PTree

the leaf of a matrix on its first step: zeros (BF16 0x0000 is +0.0)

def adamh_mat source · line 412 · raw

@+r:Nat -> @+c:Nat -> @+o:Opt -> @wa:Array<F32> -> @ga:Array<F32> -> @t:PTree -> MatStep<r, c>

def Opt.sgd_fin source · line 420 · raw

@-r:Nat -> @-c:Nat -> @q:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> MatStep<r, c>

One matrix step, with only the buffers this optimizer reads.

def Opt.mom_fin source · line 424 · raw

@-r:Nat -> @-c:Nat -> @q:Q3 -> MatStep<r, c>

def Opt.fin source · line 428 · raw

@-r:Nat -> @-c:Nat -> @q:Q4 -> MatStep<r, c>

def Opt.mat source · line 434 · raw

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

A mismatched state is initialized only at the training boundary; Opt.mat requires the matching variant. The fallback leaves the values untouched, so it cannot fabricate a moment buffer.

def Opt.at source · line 457 · raw

@+cfg:OptCfg -> @+q1:F32 -> @+q2:F32 -> Opt

def OptCfg.b1 source · line 470 · raw

@+cfg:OptCfg -> F32

def OptCfg.b2 source · line 481 · raw

@+cfg:OptCfg -> F32

def OptCfg.c1 source · line 492 · raw

@+cfg:OptCfg -> F32

def OptCfg.c2 source · line 503 · raw

@+cfg:OptCfg -> F32

def beta_ok source · line 517 · raw

@+b:F32 -> @+c:F32 -> Bool

c = 1 - b up to F32's rounding, with 0 < c <= 1 (PyTorch's 0 <= beta < 1). For b in [0, 1), 1 - b taken in F32 is within half an ulp of the exact value and c's own rounding adds at most as much, so a c written as 1 - b is always under 2^-23 from it, while a typo (0.01 for 0.001, c1 and c2 swapped) is far above. A NaN fails every comparison.

def OptCfg.ok source · line 523 · raw

@+cfg:OptCfg -> Bool

True unless an Adam configuration's c1, c2 disagree with its b1, b2. Train.new poisons the state of a configuration that fails (every loss after the first is NaN); a program checks it first with OptCfg.check to stop with a message.

def cfg_stop source · line 534 · raw

@ok:Bool -> IO(Unit)

def OptCfg.check source · line 541 · raw

@+cfg:OptCfg -> IO(Unit)

def mat_dump_fin source · line 545 · raw

@-r:Nat -> @-c:Nat -> @ml:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.ML<r, c> -> PL<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

dump and load for a matrix: its r*c numbers, row by row

def Mat.dump source · line 550 · raw

@+r:Nat -> @+c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c> -> PL<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def load_cells source · line 553 · raw

@left:Nat -> @+i:U32 -> @+off:U32 -> @rx:Pair(Array<F32>, F32) -> @dst:Array<F32> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2

def mat_restore_fin source · line 559 · raw

@-r:Nat -> @-c:Nat -> @+off:U32 -> @q:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def Mat.restore source · line 563 · raw

@+r:Nat -> @+c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c> -> @xs:Array<F32> -> @+off:U32 -> PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def store_cells source · line 569 · raw

@left:Nat -> @+i:U32 -> @+off:U32 -> @rm:Pair(Array<F32>, F32) -> @xs:Array<F32> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2

load_cells' inverse: copies the matrix's cells i.. into xs from off; both arrays come back (the matrix's first)

def mat_store_fin source · line 575 · raw

@-r:Nat -> @-c:Nat -> @+off:U32 -> @q:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def Mat.store source · line 579 · raw

@+r:Nat -> @+c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c> -> @xs:Array<F32> -> @+off:U32 -> PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def pa_second source · line 584 · raw

@-A:Type -> @-B:Type -> @a:A -> @rb:PA<B> -> PA<Two<A, B>>

def pl_pair source · line 588 · raw

@-A:Type -> @-B:Type -> @ra:PL<A> -> @rb:PL<B> -> PL<Two<A, B>>

def pl_second source · line 593 · raw

@-A:Type -> @-B:Type -> @a:A -> @rb:PL<B> -> PL<Two<A, B>>

def lin_f3 source · line 605 · raw

@-n:Nat -> @-i:Nat -> @-o:Nat -> @w:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MBias<n, o> -> Fwd<Lin<i, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lin_f2 source · line 610 · raw

@+n:Nat -> @-i:Nat -> @+o:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MMulNT<n, i, o> -> Fwd<Lin<i, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lin_fwd source · line 615 · raw

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

def lin_b4 source · line 620 · raw

@-n:Nat -> @-i:Nat -> @-o:Nat -> @w:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @dx:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i> -> @gw:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MColSum<n, o> -> Bwd<Lin<i, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lin_b3 source · line 625 · raw

@+n:Nat -> @-i:Nat -> @+o:Nat -> @w:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @dx:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i> -> @gb:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MMulTN<o, n, i> -> Bwd<Lin<i, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lin_b2 source · line 630 · raw

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

def lin_bwd source · line 637 · raw

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

dX = dY·W, dW += dYᵀ·X, db += the column sums of dY; the two sums are added straight into the gradient (the accumulating products of bend-ml-tensor-array 0.1.10.0), with the bits of computing them apart and adding them

def pair_s3 source · line 642 · raw

@-P1:Type -> @-P2:Type -> @r1:Step<P1> -> @r2:Step<P2> -> Step<Two<P1, P2>>

def lin_pair source · line 647 · raw

@-i:Nat -> @-o:Nat -> @p:Lin<i, o> -> Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o>>

def lin_unpair source · line 651 · raw

@-i:Nat -> @-o:Nat -> @p:Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o>> -> Lin<i, o>

def lin_s2 source · line 655 · raw

@-i:Nat -> @-o:Nat -> @a:MatStep<o, i> -> @c:MatStep<1n, o> -> Step<Lin<i, o>>

def lin_step2 source · line 660 · raw

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

def lin_step source · line 665 · raw

@+i:Nat -> @+o:Nat -> @+op:Opt -> @p:Lin<i, o> -> @g:Lin<i, o> -> @st:OptState<Lin<i, o>> -> Step<Lin<i, o>>

def lin_dump2 source · line 668 · raw

@-i:Nat -> @-o:Nat -> @r:PL<Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o>>> -> PL<Lin<i, o>>

def lin_dump source · line 673 · raw

@+i:Nat -> @+o:Nat -> @p:Lin<i, o> -> PL<Lin<i, o>>

def lin_load3 source · line 678 · raw

@-i:Nat -> @-o:Nat -> @r:PA<Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o>>> -> PA<Lin<i, o>>

def lin_load2 source · line 682 · raw

@+i:Nat -> @+o:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @r:PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>> -> PA<Lin<i, o>>

def lin_load source · line 687 · raw

@+i:Nat -> @+o:Nat -> @p:Lin<i, o> -> @xs:Array<F32> -> @off:U32 -> PA<Lin<i, o>>

def lin_store2 source · line 691 · raw

@+i:Nat -> @+o:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @r:PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>> -> PA<Lin<i, o>>

def lin_store source · line 696 · raw

@+i:Nat -> @+o:Nat -> @p:Lin<i, o> -> @xs:Array<F32> -> @off:U32 -> PA<Lin<i, o>>

def lin_zeros source · line 700 · raw

@+i:Nat -> @+o:Nat -> Lin<i, o>

def Linear source · line 704 · raw

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

a linear layer from i to o features on batches of n rows; its products run in 2^par blocks of rows

def linf_b3 source · line 710 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @w:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @gb:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, o> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MMulTN<o, n, i> -> Bwd<Lin<i, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

the first layer of a model, whose input (the data) needs no gradient, as an input without requires_grad in PyTorch: the backward pass computes dW and db only, skipping dX = dY·W (a third of a linear layer's work), and returns zeros as dX. Use it only where nothing before the layer has parameters.

def linf_bwd source · line 715 · raw

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

def LinearFirst source · line 720 · raw

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

def pick source · line 727 · raw

@b:Bool -> @+x:F32 -> @+y:F32 -> F32

def no_step source · line 734 · raw

@-P:Type -> @+op:Opt -> @p:P -> @g:P -> @st:OptState<P> -> Step<P>

def act_f2 source · line 739 · raw

@-n:Nat -> @-c:Nat -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MM2<n, c> -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

an element-wise activation that saves a copy of its output (relu, sigmoid, tanh: their derivative is a function of the output). f is the activation, d(dy, y) the gradient of its input.

def act_b2 source · line 744 · raw

@-n:Nat -> @-c:Nat -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<n, c> -> Bwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def relu_v source · line 754 · raw

@+x:F32 -> F32

relu(x) = x unless x < 0 (so relu(NaN) = NaN and relu(-0) = -0); its gradient is +0 where the output is <= 0 and passes everywhere else, NaN included (PyTorch's threshold_backward)

def relu_d source · line 757 · raw

@+dy:F32 -> @+y:F32 -> F32

def ReLU source · line 760 · raw

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

def seq_f3 source · line 767 · raw

@-P1:Type -> @-P2:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @p1:P1 -> @s1:S1 -> @r:Fwd<P2, Z, S2> -> Fwd<Two<P1, P2>, Z, Two<S1, S2>>

def seq_b3 source · line 782 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @p2:P2 -> @g2:P2 -> @r:Bwd<P1, X> -> Bwd<Two<P1, P2>, X>

def seq_s3 source · line 797 · raw

@-P1:Type -> @-P2:Type -> @r1:Step<P1> -> @r2:Step<P2> -> Step<Two<P1, P2>>

def seq_at_f2 source · line 849 · raw

@-P1:Type -> @-P2:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @p2:P2 -> @fb:(@_:P2 -> @_:Y -> Fwd<P2, Z, S2>) -> @r:Fwd<P1, Y, S1> -> Fwd<Two<P1, P2>, Z, Two<S1, S2>>

Owned layer composition for dimensions read at run time. Each child record is opened once; each closure goes to exactly one corresponding closure of the composite layer. The template-based Seq above keeps its existing laws.

def seq_at_f source · line 853 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @fa:(@_:P1 -> @_:X -> Fwd<P1, Y, S1>) -> @fb:(@_:P2 -> @_:Y -> Fwd<P2, Z, S2>) -> @p:Two<P1, P2> -> @x:X -> Fwd<Two<P1, P2>, Z, Two<S1, S2>>

def seq_at_b2 source · line 857 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-S1:Type -> @ba:(@_:P1 -> @_:S1 -> @_:Y -> @_:P1 -> Bwd<P1, X>) -> @p1:P1 -> @s1:S1 -> @g1:P1 -> @r:Bwd<P2, Y> -> Bwd<Two<P1, P2>, X>

def seq_at_b source · line 861 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @ba:(@_:P1 -> @_:S1 -> @_:Y -> @_:P1 -> Bwd<P1, X>) -> @bb:(@_:P2 -> @_:S2 -> @_:Z -> @_:P2 -> Bwd<P2, Y>) -> @p:Two<P1, P2> -> @s:Two<S1, S2> -> @dz:Z -> @g:Two<P1, P2> -> Bwd<Two<P1, P2>, X>

def seq_at_step source · line 865 · raw

@-P1:Type -> @-P2:Type -> @sa:(@_:Opt -> @_:P1 -> @_:P1 -> @_:OptState<P1> -> Step<P1>) -> @sb:(@_:Opt -> @_:P2 -> @_:P2 -> @_:OptState<P2> -> Step<P2>) -> @+op:Opt -> @p:Two<P1, P2> -> @g:Two<P1, P2> -> @st:Two<OptState<P1>, OptState<P2>> -> Step<Two<P1, P2>>

def seq_at_dump source · line 869 · raw

@-P1:Type -> @-P2:Type -> @da:(@_:P1 -> PL<P1>) -> @db:(@_:P2 -> PL<P2>) -> @p:Two<P1, P2> -> PL<Two<P1, P2>>

def seq_at_load2 source · line 873 · raw

@-P1:Type -> @-P2:Type -> @lb:(@_:P2 -> @_:Array<F32> -> @_:U32 -> PA<P2>) -> @p2:P2 -> @r:PA<P1> -> PA<Two<P1, P2>>

def seq_at_load source · line 877 · raw

@-P1:Type -> @-P2:Type -> @la:(@_:P1 -> @_:Array<F32> -> @_:U32 -> PA<P1>) -> @lb:(@_:P2 -> @_:Array<F32> -> @_:U32 -> PA<P2>) -> @p:Two<P1, P2> -> @xs:Array<F32> -> @off:U32 -> PA<Two<P1, P2>>

def seq_at_store source · line 881 · raw

@-P1:Type -> @-P2:Type -> @la:(@_:P1 -> @_:Array<F32> -> @_:U32 -> PA<P1>) -> @lb:(@_:P2 -> @_:Array<F32> -> @_:U32 -> PA<P2>) -> @p:Two<P1, P2> -> @xs:Array<F32> -> @off:U32 -> PA<Two<P1, P2>>

def Seq.at source · line 885 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @a:Layer<P1, X, Y, S1> -> @b:Layer<P2, Y, Z, S2> -> Layer<Two<P1, P2>, X, Z, Two<S1, S2>>

def q_start source · line 913 · raw

@ok:Bool -> F32

1 - b^t starts at 0; a configuration OptCfg.ok rejects starts at NaN, so every loss after the first is NaN.

def ts5 source · line 924 · raw

@-P:Type -> @+l:F32 -> @+p1:F32 -> @+p2:F32 -> @r:Step<P> -> Train<P>

def train_at_b source · line 953 · raw

@-P:Type -> @-X:Type -> @sf:(@_:Opt -> @_:P -> @_:P -> @_:OptState<P> -> Step<P>) -> @+cfg:OptCfg -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:Bwd<P, X> -> Train<P>

One owned model, including dimensions captured at run time. Its fwd, bwd and step closures each run once.

def train_at_loss source · line 960 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @bf:(@_:P -> @_:S -> @_:Y -> @_:P -> Bwd<P, X>) -> @sf:(@_:Opt -> @_:P -> @_:P -> @_:OptState<P> -> Step<P>) -> @+cfg:OptCfg -> @p:P -> @s:S -> @g:P -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @r:LossOut<Y> -> Train<P>

def train_at_f source · line 964 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-Target:Type -> @bf:(@_:P -> @_:S -> @_:Y -> @_:P -> Bwd<P, X>) -> @sf:(@_:Opt -> @_:P -> @_:P -> @_:OptState<P> -> Step<P>) -> @loss:(@_:Y -> @_:Target -> LossOut<Y>) -> @+cfg:OptCfg -> @g:P -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @t:Target -> @r:Fwd<P, Y, S> -> Train<P>

def Train.step_at source · line 968 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-Target:Type -> @m:Layer<P, X, Y, S> -> @loss:(@_:Y -> @_:Target -> LossOut<Y>) -> @+cfg:OptCfg -> @tr:Train<P> -> @x:X -> @t:Target -> Train<P>

def bitcast source · line 978 · raw

@u:U32 -> F32

U32 and F32 hold the same Word(32n): these are bitcasts

def bits source · line 983 · raw

@x:F32 -> U32

def f32_le source · line 988 · raw

@+b0:U32 -> @+b1:U32 -> @+b2:U32 -> @+b3:U32 -> F32

def f32s source · line 991 · raw

@bs:List<&2, U32> -> List<&2, F32>

def rd_done source · line 998 · raw

@m:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(List<&2, U32>)

def read_at source · line 1007 · raw

@path:String -> @+off:U32 -> @+n:U32 -> IO(List<&2, U32>)

n bytes from offset off

def mat_or_die source · line 1013 · raw

@-r:Nat -> @-c:Nat -> @+path:String -> @m:Maybe<&1, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>> -> IO(0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>)

def Mat.load source · line 1021 · raw

@+r:Nat -> @+c:Nat -> @+path:String -> @+off:U32 -> IO(0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>)

an r x c matrix stored as r*c little-endian F32 from byte off of a file

def words source · line 1026 · raw

@xs:List<&2, F32> -> String

def ml_words source · line 1033 · raw

@-r:Nat -> @-c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.ML<r, c> -> String

def Mat.print source · line 1039 · raw

@+r:Nat -> @+c:Nat -> @+name:String -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c> -> IO(Unit)

one line: the name, then each number as the decimal of its 32 bits (exact, for the tests in reference/)

def F32.print source · line 1042 · raw

@+name:String -> @x:F32 -> IO(Unit)

def zeros_a source · line 1046 · raw

@+n:Nat -> Array<F32>

n zeros, in an array with room for them

def sigmoid_v source · line 1062 · raw

@+x:F32 -> F32

def sigmoid_d source · line 1066 · raw

@+dy:F32 -> @+y:F32 -> F32

PyTorch's sigmoid_backward: dy * (1 - y) * y

def Sigmoid source · line 1069 · raw

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

def tanh_v source · line 1072 · raw

@+x:F32 -> F32

def tanh_d source · line 1075 · raw

@+dy:F32 -> @+y:F32 -> F32

def Tanh source · line 1078 · raw

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

def silu_v source · line 1083 · raw

@+x:F32 -> F32

silu(x) = x * sigmoid(x), computed as PyTorch's CPU kernel: x / (1 + exp(-x)); its gradient dy * s * (1 + x * (1 - s)) with s = sigmoid(x)

def silu_d source · line 1086 · raw

@+dy:F32 -> @+x:F32 -> F32

def SiLU source · line 1090 · raw

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

def gelu_inner source · line 1095 · raw

@+x:F32 -> F32

GELU in its tanh form (PyTorch's approximate="tanh"): 0.5 x (1 + tanh(sqrt(2/pi) (x + 0.044715 x^3))). The exact form, with erf, is GELUErf below.

def gelu_v source · line 1098 · raw

@+x:F32 -> F32

def gelu_d source · line 1101 · raw

@+dy:F32 -> @+x:F32 -> F32

def GELU source · line 1108 · raw

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

def erf_hp source · line 1130 · raw

@+c:F32 -> @+t:F32 -> @+s:F32 -> F32

one Horner step, c + t s; erf_hm is the step for a negative coefficient -c: t s - c gives the bits of -c + t s

def erf_hm source · line 1133 · raw

@+c:F32 -> @+t:F32 -> @+s:F32 -> F32

def erf_small source · line 1136 · raw

@+x:F32 -> F32

def erf_p0 source · line 1141 · raw

@+t:F32 -> F32

[0.84375, 1.25): t = a - 1.0

def erf_p1 source · line 1145 · raw

@+t:F32 -> F32

[1.25, 1.75): t = a - 1.5

def erf_p2 source · line 1149 · raw

@+t:F32 -> F32

[1.75, 2.25): t = a - 2.0

def erf_p3 source · line 1153 · raw

@+t:F32 -> F32

[2.25, 2.75): t = a - 2.5

def erf_p4 source · line 1157 · raw

@+t:F32 -> F32

[2.75, 3.25): t = a - 3.0

def erf_p5 source · line 1161 · raw

@+t:F32 -> F32

[3.25, 3.92): t = a - 3.5

def erf_sign source · line 1164 · raw

@+x:F32 -> @+e:F32 -> F32

def erf_i0 source · line 1169 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

the pieces are chosen from the top down, so that only one polynomial is computed (each test is a match on a Bool parameter); b is "a is at least the piece's lower end"

def erf_i1 source · line 1174 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf_i2 source · line 1179 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf_i3 source · line 1184 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf_i4 source · line 1189 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf_i5 source · line 1194 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf_top source · line 1199 · raw

@b:Bool -> @+x:F32 -> @+a:F32 -> F32

def erf source · line 1204 · raw

@+x:F32 -> F32

def gelu_erf_cdf source · line 1213 · raw

@+x:F32 -> F32

GELU in its exact form, PyTorch's default (approximate="none"): gelu(x) = x/2 (1 + erf(x / sqrt 2)), in PyTorch's CPU order, (x * 0.5) * (1 + erf(x * 0.70710677)). Its gradient is cdf + x pdf, with cdf = 0.5 (1 + erf(x / sqrt 2)) and pdf = exp(-x^2 / 2) / sqrt(2 pi), computed as PyTorch does: 0.5 (1 + erf(x * 0.70710677)) and 0.3989423 * exp((x * x) * -0.5). The exponential is F32.exp, as in SiLU (so on a GPU the gradient would take CUDA's float exp).

def gelu_erf_v source · line 1216 · raw

@+x:F32 -> F32

def gelu_erf_d source · line 1219 · raw

@+dy:F32 -> @+x:F32 -> F32

def GELUErf source · line 1223 · raw

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

def mix32 source · line 1236 · raw

@+x:U32 -> U32

lowbias32 (Chris Wellons): an invertible mix of the 32 bits

def drop_k source · line 1244 · raw

@+key:U32 -> @+i:U32 -> @+p:F32 -> @+scale:F32 -> F32

the factor of number i: 1 / (1 - p) if its uniform u = (h >> 8) / 2^24 is >= p, else 0

def drop_l source · line 1247 · raw

@nleft:Nat -> @+i:U32 -> @+key:U32 -> @+p:F32 -> @+scale:F32 -> @a:Array<F32> -> Array<F32>

def drop_mask source · line 1254 · raw

@+n:Nat -> @+c:Nat -> @+seed:U32 -> @+t:U32 -> @+p:F32 -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>

def drop_f2 source · line 1257 · raw

@-n:Nat -> @-c:Nat -> @+t:U32 -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<n, c> -> Fwd<DropP, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def drop_fwd source · line 1262 · raw

@+n:Nat -> @+c:Nat -> @+seed:U32 -> @+p:F32 -> @d:DropP -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> Fwd<DropP, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def drop_b2 source · line 1267 · raw

@-n:Nat -> @-c:Nat -> @d:DropP -> @g:DropP -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<n, c> -> Bwd<DropP, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def drop_step source · line 1272 · raw

@+op:Opt -> @d:DropP -> @g:DropP -> @st:OptState<DropP> -> Step<DropP>

def drop_dump source · line 1277 · raw

@d:DropP -> PL<DropP>

def drop_load_fin source · line 1282 · raw

@+off:U32 -> @r:Pair(Array<F32>, F32) -> PA<DropP>

def drop_load source · line 1286 · raw

@d:DropP -> @xs:Array<F32> -> @+off:U32 -> PA<DropP>

def drop_store source · line 1290 · raw

@d:DropP -> @xs:Array<F32> -> @+off:U32 -> PA<DropP>

the counter, bitcast to one F32 as dump writes it

def Dropout source · line 1294 · raw

@+n:Nat -> @+c:Nat -> @+p:F32 -> @+seed:U32 -> Layer<DropP, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def sdot source · line 1311 · raw

@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> D2

a[ia]·b[ib] + a[ia + sa]·b[ib + sb] + ... (k terms) added to acc left to right; ra and rb hold the first pair, already read

def dot0 source · line 1318 · raw

@+k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @a:Array<F32> -> @b:Array<F32> -> D2

def pmax source · line 1326 · raw

@k:Nat -> @+idx:U32 -> @cur:F32 -> @r:Pair(Array<F32>, F32) -> AS

the largest of a[idx] (held in r) and the k numbers after it: max(max(a0, a1), a2) ..., from the first

def pmax0 source · line 1333 · raw

@k:Nat -> @+idx:U32 -> @r:Pair(Array<F32>, F32) -> AS

def pexp source · line 1341 · raw

@k:Nat -> @+idx:U32 -> @+m:F32 -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> AS

a[j] = exp(a[j] - m) for j = idx..idx + k, their sum added to acc in order

def pc_a source · line 1354 · raw

@r:PC -> Array<F32>

def pdiv source · line 1360 · raw

@k:Nat -> @+idx:U32 -> @+s:F32 -> @c:Nat -> @r:Pair(Array<F32>, F32) -> PC

a[j] = a[j] / s for j = idx..idx + k, counted onto c

def psm3 source · line 1367 · raw

@k:Nat -> @+off:U32 -> @r:AS -> PC

def psm2 source · line 1372 · raw

@+k:Nat -> @+off:U32 -> @r:AS -> PC

def psmc source · line 1379 · raw

@+n:Nat -> @+off:U32 -> @p:Array<F32> -> PC

softmax in place over the n numbers of p from off (n >= 1), as PyTorch: exp(x - max) / sum; with the count of the numbers it divided (the law causal reads it)

def psm source · line 1382 · raw

@+n:Nat -> @+off:U32 -> @p:Array<F32> -> Array<F32>

def copy_n source · line 1386 · raw

@cnt:Nat -> @+si:U32 -> @+di:U32 -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> A2

dst[di..] = src[si..], cnt + 1 numbers; r holds src[si], already read

def smf_rows source · line 1398 · raw

@left:Nat -> @+i:U32 -> @+c:Nat -> @x:Array<F32> -> Array<F32>

rows i.. of x (c wide, left more after i), each softmaxed in place (psm)

def out_saved source · line 1406 · raw

@-n:Nat -> @-c:Nat -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MM2<n, c> -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

the output, and a copy of it saved for the backward pass

def sm_fwd source · line 1411 · raw

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

def smb_fix source · line 1417 · raw

@k:Nat -> @+idx:U32 -> @+s:F32 -> @rd:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> A2

dy[j] = y[j] * (dy[j] - s), j = idx..idx + k; rd and ry hold dy[idx] and y[idx]

def smb_row source · line 1424 · raw

@+c:Nat -> @+off:U32 -> @r:D2 -> A2

def smb_rows source · line 1430 · raw

@left:Nat -> @+i:U32 -> @+c:Nat -> @st:A2 -> A2

softmax backward, as PyTorch's: dx = y * (dy - sum(dy * y)), row after row, in place over dy

def dx_fin source · line 1437 · raw

@-n:Nat -> @-c:Nat -> @r:A2 -> Bwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def sm_bwd source · line 1442 · raw

@+n:Nat -> @+c:Nat -> @s:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> Bwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def Softmax source · line 1448 · raw

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

softmax over each row; saves its output

def lsum source · line 1454 · raw

@k:Nat -> @+idx:U32 -> @+m:F32 -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> AS

acc + exp(a[j] - m) for j = idx..idx + k, in order

def lw source · line 1462 · raw

@k:Nat -> @+idx:U32 -> @+m:F32 -> @+l:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

a[j] = (a[j] - m) - l for j = idx..idx + k

def lsm_r3 source · line 1469 · raw

@+c:Nat -> @+off:U32 -> @+m:F32 -> @r:AS -> Array<F32>

def lsm_r2 source · line 1474 · raw

@+c:Nat -> @+off:U32 -> @r:AS -> Array<F32>

def lsm_row source · line 1479 · raw

@+c:Nat -> @+off:U32 -> @x:Array<F32> -> Array<F32>

def lsmf_rows source · line 1482 · raw

@left:Nat -> @+i:U32 -> @+c:Nat -> @x:Array<F32> -> Array<F32>

def lsm_fwd source · line 1489 · raw

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

def asum source · line 1495 · raw

@k:Nat -> @+idx:U32 -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> AS

acc + a[j] for j = idx..idx + k, in order

def lsmb_fix source · line 1503 · raw

@k:Nat -> @+idx:U32 -> @+s:F32 -> @rd:Pair(Array<F32>, F32) -> @ry:Pair(Array<F32>, F32) -> A2

dy[j] = dy[j] - exp(y[j]) * s, j = idx..idx + k

def lsmb_row source · line 1510 · raw

@+c:Nat -> @+off:U32 -> @y:Array<F32> -> @r:AS -> A2

def lsmb_rows source · line 1516 · raw

@left:Nat -> @+i:U32 -> @+c:Nat -> @st:A2 -> A2

dx = dy - exp(y) * sum(dy), row after row, in place over dy

def lsm_bwd source · line 1523 · raw

@+n:Nat -> @+c:Nat -> @s:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> Bwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def LogSoftmax source · line 1528 · raw

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

def mat_step_fin source · line 1534 · raw

@-r:Nat -> @-c:Nat -> @q:MatStep<r, c> -> Step<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

one optimizer step on a layer whose parameter is a single matrix

def mat_step source · line 1538 · raw

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

def g_add_fin source · line 1541 · raw

@-r:Nat -> @-c:Nat -> @r2:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<r, c> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>

def Mat.sum2 source · line 1547 · raw

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

a + b, dropping b

def dup_fin source · line 1550 · raw

@-r:Nat -> @-c:Nat -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MM2<r, c> -> Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<r, c>>

def Mat.dup source · line 1556 · raw

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

two copies of a matrix

def ml_list source · line 1559 · raw

@-r:Nat -> @-c:Nat -> @ml:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.ML<r, c> -> List<&2, F32>

def ssq source · line 1569 · raw

@k:Nat -> @+i:U32 -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> AS

acc + a[i]·a[i] + ... over k + 1 numbers from i, in order; r holds a[i], already read

def rms_inv source · line 1577 · raw

@+s:F32 -> @+df:F32 -> @+eps:F32 -> F32

1 / rms of a row whose sum of squares is s

def rms_w source · line 1584 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+r:F32 -> @y:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> R3

y[off + j] = g[j] * (x[off + j] * r), j = jj..jj + k; rx and rg hold x[off + jj] and g[jj]

def rmsf_row2 source · line 1591 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @g:Array<F32> -> @y:Array<F32> -> @r:AS -> R3

def rmsf_row source · line 1596 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @s:R3 -> R3

def rmsf_rows source · line 1602 · raw

@left:Nat -> @+i:U32 -> @+d:Nat -> @+df:F32 -> @+eps:F32 -> @s:R3 -> R3

rows i.. (left + 1 of them, d wide)

def rms_f2 source · line 1609 · raw

@-n:Nat -> @-d:Nat -> @s:R3 -> Fwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def rms_fwd source · line 1614 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> Fwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def rmsb_dot source · line 1627 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+r:F32 -> @acc:F32 -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> R3S

acc + dxh[j]·xh[j] + ..., in order; rx, rd, rg hold x[off + jj], dy[off + jj], g[jj]

def dg_add source · line 1634 · raw

@+j:U32 -> @+v:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

def rmsb_w source · line 1640 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+r:F32 -> @+m:F32 -> @dx:Array<F32> -> @dg:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> R5

dx[off + j] = ((dy[j]·g[j]) - (x[j]·r)·m)·r and dg[j] += dy[j]·(x[j]·r), j = jj..jj + k

def rmsb_row3 source · line 1649 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+r:F32 -> @dx:Array<F32> -> @dg:Array<F32> -> @q:R3S -> R5

def rmsb_row2 source · line 1654 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @dg:Array<F32> -> @q:AS -> R5

def rmsb_row source · line 1660 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @s:R5 -> R5

def rmsb_rows source · line 1665 · raw

@left:Nat -> @+i:U32 -> @+d:Nat -> @+df:F32 -> @+eps:F32 -> @s:R5 -> R5

def gain_bwd_fin source · line 1672 · raw

@-n:Nat -> @-d:Nat -> @-P:Type -> @p:P -> @dx:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @g:P -> Bwd<P, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def rms_b2 source · line 1675 · raw

@+d:Nat -> @-n:Nat -> @gacc:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @s:R5 -> Bwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def rms_bwd source · line 1680 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @gacc:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> Bwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def RMSNorm source · line 1686 · raw

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

RMSNorm over rows of d numbers, with a learned gain (Llama's norm); saves its input

def csq source · line 1695 · raw

@k:Nat -> @+idx:U32 -> @+mu:F32 -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> AS

acc + (a[j] - mu)·(a[j] - mu) for j = idx..idx + k, in order

def ln_mr2 source · line 1705 · raw

@+df:F32 -> @+eps:F32 -> @+mu:F32 -> @q:AS -> ASM

def ln_mr1 source · line 1710 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @q:AS -> ASM

def ln_mr source · line 1716 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @x:Array<F32> -> ASM

a row's mean and 1 / sqrt(var + eps), as ln_xh and ln_r computed them

def lnf_w source · line 1723 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+mu:F32 -> @+r:F32 -> @y:Array<F32> -> @rx:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> R4

y[off + j] = ((x - mu) * r) * g[j] + b[j], j = jj..jj + k

def lnf_row2 source · line 1730 · raw

@+d:Nat -> @+off:U32 -> @g:Array<F32> -> @b:Array<F32> -> @y:Array<F32> -> @m:ASM -> R4

def lnf_row source · line 1735 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @s:R4 -> R4

def lnf_rows source · line 1740 · raw

@left:Nat -> @+i:U32 -> @+d:Nat -> @+df:F32 -> @+eps:F32 -> @s:R4 -> R4

def ln_f2 source · line 1747 · raw

@-n:Nat -> @-d:Nat -> @s:R4 -> Fwd<LN<d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def ln_fwd source · line 1752 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:LN<d> -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> Fwd<LN<d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def lnb_sums source · line 1762 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+mu:F32 -> @+r:F32 -> @sa:F32 -> @sb:F32 -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> R3SS

sa + dxh[j] and sb + dxh[j]·xh[j], j = jj..jj + k, each in order

def ln_dw source · line 1769 · raw

@+j:U32 -> @+v:F32 -> @+w:F32 -> @st:A2 -> A2

def lnb_w source · line 1778 · raw

@k:Nat -> @+j:U32 -> @+off:U32 -> @+mu:F32 -> @+r:F32 -> @+ma:F32 -> @+mb:F32 -> @dx:Array<F32> -> @w:A2 -> @rx:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> @rg:Pair(Array<F32>, F32) -> R4B

dx[off + j] = (((dxh - ma) - xh * mb) * r), dg[j] += dy * xh, db[j] += dy, j = jj..jj + k (ma, mb the two means)

def lnb_row3 source · line 1787 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+mu:F32 -> @+r:F32 -> @dx:Array<F32> -> @w:A2 -> @q:R3SS -> R4B

def lnb_row2 source · line 1792 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @w:A2 -> @m:ASM -> R4B

def lnb_row source · line 1797 · raw

@+d:Nat -> @+off:U32 -> @+df:F32 -> @+eps:F32 -> @s:R4B -> R4B

def lnb_rows source · line 1802 · raw

@left:Nat -> @+i:U32 -> @+d:Nat -> @+df:F32 -> @+eps:F32 -> @s:R4B -> R4B

def ln_b2 source · line 1809 · raw

@+d:Nat -> @-n:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @gg:LN<d> -> @s:R4B -> Bwd<LN<d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def ln_bwd source · line 1814 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> @p:LN<d> -> @x:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @gg:LN<d> -> Bwd<LN<d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def ln_pair source · line 1819 · raw

@-d:Nat -> @p:LN<d> -> Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>>

def ln_unpair source · line 1823 · raw

@-d:Nat -> @p:Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>> -> LN<d>

def ln_s2 source · line 1827 · raw

@-d:Nat -> @a:MatStep<1n, d> -> @c:MatStep<1n, d> -> Step<LN<d>>

def ln_step2 source · line 1832 · raw

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

def ln_step source · line 1837 · raw

@+d:Nat -> @+op:Opt -> @p:LN<d> -> @g:LN<d> -> @st:OptState<LN<d>> -> Step<LN<d>>

def ln_dump2 source · line 1840 · raw

@-d:Nat -> @r:PL<Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>>> -> PL<LN<d>>

def ln_dump source · line 1845 · raw

@+d:Nat -> @p:LN<d> -> PL<LN<d>>

def ln_load3 source · line 1850 · raw

@-d:Nat -> @r:PA<Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>>> -> PA<LN<d>>

def ln_load2 source · line 1854 · raw

@+d:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @r:PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>> -> PA<LN<d>>

def ln_load source · line 1859 · raw

@+d:Nat -> @p:LN<d> -> @xs:Array<F32> -> @off:U32 -> PA<LN<d>>

def ln_store2 source · line 1863 · raw

@+d:Nat -> @b:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d> -> @r:PA<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<1n, d>> -> PA<LN<d>>

def ln_store source · line 1868 · raw

@+d:Nat -> @p:LN<d> -> @xs:Array<F32> -> @off:U32 -> PA<LN<d>>

def LayerNorm source · line 1872 · raw

@+n:Nat -> @+d:Nat -> @+eps:F32 -> Layer<LN<d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>>

def gn_sum source · line 1883 · raw

@left:Nat -> @+off:U32 -> @+c:U32 -> @+cg:Nat -> @q:AS -> AS

acc + the segments of the block's remaining left rows, each one row (c cells) below the last, in order

def gn_csq source · line 1891 · raw

@left:Nat -> @+off:U32 -> @+c:U32 -> @+cg:Nat -> @+mu:F32 -> @q:AS -> AS

acc + (x - mu)·(x - mu) over the same segments

def gn_mr1 source · line 1898 · raw

@+hw:Nat -> @+off:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @q:AS -> ASM

def gn_mr source · line 1904 · raw

@+hw:Nat -> @+off:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @x:Array<F32> -> ASM

a block's mean and 1 / sqrt(var + eps); off is its first cell, nf = hw·cg

def gnf_rows source · line 1908 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+mu:F32 -> @+r:F32 -> @s:R4 -> R4

y over the block's rows, one segment each (LayerNorm's lnf_w with the group's gains)

def gnf_grp2 source · line 1915 · raw

@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @g:Array<F32> -> @b:Array<F32> -> @y:Array<F32> -> @m:ASM -> R4

def gnf_grp source · line 1921 · raw

@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4 -> R4

one (image, group) block: off is the image's first row's first cell, j the group's first channel

def gnf_groups source · line 1926 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4 -> R4

def gnf_images source · line 1933 · raw

@left:Nat -> @+off:U32 -> @+ng:Nat -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4 -> R4

def gn_fwd source · line 1940 · raw

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

def gnb_sums source · line 1947 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+mu:F32 -> @+r:F32 -> @q:R3SS -> R3SS

the backward pass is LayerNorm's with the block in place of the row: the two means (of dxh and of dxh·xh) are taken over the whole block, then each cell's dx, dg[ch] += dy·xh and db[ch] += dy, in order

def gnb_rows source · line 1954 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+mu:F32 -> @+r:F32 -> @+ma:F32 -> @+mb:F32 -> @s:R4B -> R4B

def gnb_grp3 source · line 1961 · raw

@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+mu:F32 -> @+r:F32 -> @dx:Array<F32> -> @w:A2 -> @q:R3SS -> R4B

def gnb_grp2 source · line 1966 · raw

@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @dy:Array<F32> -> @g:Array<F32> -> @dx:Array<F32> -> @w:A2 -> @m:ASM -> R4B

def gnb_grp source · line 1971 · raw

@+hw:Nat -> @+off:U32 -> @+j:U32 -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4B -> R4B

def gnb_groups source · line 1976 · raw

@left:Nat -> @+off:U32 -> @+j:U32 -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4B -> R4B

def gnb_images source · line 1983 · raw

@left:Nat -> @+off:U32 -> @+ng:Nat -> @+hw:Nat -> @+c:U32 -> @+cg:Nat -> @+nf:F32 -> @+eps:F32 -> @s:R4B -> R4B

def gn_bwd source · line 1990 · raw

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

def GroupNorm source · line 1996 · raw

@+n:Nat -> @+hw:Nat -> @+ng:Nat -> @+cg:Nat -> @+eps:F32 -> Layer<LN<Nat.mul(ng, cg)>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<Nat.mul(n, hw), Nat.mul(ng, cg)>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/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; its parameters are LayerNorm's

def emb_cp source · line 2004 · raw

@left:Nat -> @ids:List<&2, Nat> -> @+i:U32 -> @+d:Nat -> @st:A2 -> A2

row i of the output (d numbers) copied from row ids[i] of the table, for the first n ids

def emb_f2 source · line 2011 · raw

@-n:Nat -> @-v:Nat -> @-d:Nat -> @+ids:List<&2, Nat> -> @r:A2 -> Fwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, List<&2, Nat>>

def emb_f source · line 2016 · raw

@+n:Nat -> @+v:Nat -> @+d:Nat -> @+ids:List<&2, Nat> -> @e:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d> -> Fwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d>, List<&2, Nat>>

def emb_add source · line 2022 · raw

@k:Nat -> @+gi:U32 -> @+di:U32 -> @rg:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> A2

g[gi + j] = g[gi + j] + dy[di + j], j = 0..k

def emb_sc source · line 2030 · raw

@left:Nat -> @ids:List<&2, Nat> -> @+i:U32 -> @+d:Nat -> @st:A2 -> A2

each dY row added into the gradient's row ids[i], in order, by index

def emb_b2 source · line 2037 · raw

@-v:Nat -> @-d:Nat -> @e:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d> -> @r:A2 -> Bwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d>, List<&2, Nat>>

def emb_bwd source · line 2042 · raw

@+n:Nat -> @+v:Nat -> @+d:Nat -> @e:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d> -> @ids:List<&2, Nat> -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, d> -> @g:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d> -> Bwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<v, d>, List<&2, Nat>>

def Embedding source · line 2048 · raw

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

a table of v rows of d numbers, looked up by n ids; an id has no gradient, so dX is the empty list

def lnb_f2 source · line 2055 · raw

@-n:Nat -> @-i:Nat -> @-o:Nat -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MMulNT<n, i, o> -> Fwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, o>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lnb_b3 source · line 2060 · raw

@-n:Nat -> @-i:Nat -> @-o:Nat -> @w:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i> -> @dx:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MMulTN<o, n, i> -> Bwd<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<o, i>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, i>>

def lnb_b2 source · line 2065 · raw

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

def LinearNB source · line 2070 · raw

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

def pair_f4 source · line 2089 · raw

@-P1:Type -> @-P2:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @p1:P1 -> @y1:Y1 -> @s1:S1 -> @r:Fwd<P2, Y2, S2> -> Fwd<Two<P1, P2>, Two<Y1, Y2>, Two<S1, S2>>

def both_b3 source · line 2133 · raw

@-P1:Type -> @-P2:Type -> @-X1:Type -> @-X2:Type -> @p2:P2 -> @g2:P2 -> @dx2:X2 -> @r:Bwd<P1, X1> -> Bwd<Two<P1, P2>, Two<X1, X2>>

def tie_f4 source · line 2164 · raw

@-P:Type -> @-Q:Type -> @-W:Type -> @-S1:Type -> @-S2:Type -> @-S3:Type -> @q:Q -> @s1:S1 -> @s2:S2 -> @r:Fwd<P, W, S3> -> Fwd<Two<P, Q>, W, Three<S1, S2, S3>>

def tie_b4 source · line 2184 · raw

@-P:Type -> @-Q:Type -> @-X:Type -> @q:Q -> @gq:Q -> @r:Bwd<P, X> -> Bwd<Two<P, Q>, X>

def add2_f source · line 2212 · raw

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

def Add2 source · line 2217 · raw

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

def mul2_f3 source · line 2220 · raw

@-n:Nat -> @-c:Nat -> @a2:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> @b2:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c> -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<n, c> -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>>

def mul2_f2 source · line 2225 · raw

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

def mul2_f source · line 2230 · raw

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

def mpair_a source · line 2235 · raw

@-n:Nat -> @-c:Nat -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.MPair<n, c> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>

def mul2_b2 source · line 2241 · raw

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

d(a*b) = (dy*b, dy*a)

def Mul2 source · line 2246 · raw

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

def rope_ang source · line 2272 · raw

@+e:U32 -> @+half:U32 -> @+base:F32 -> F32

the angle of table entry e: inv[c] * position, position e / half, c = e mod half, inv[c] = 1 / base^(2c / (2*half))

def rope_tab source · line 2276 · raw

@left:Nat -> @+e:U32 -> @+half:U32 -> @+base:F32 -> @tb:CS -> CS

entries e.. of the tables, left more after e

def rope_p3 source · line 2290 · raw

@fwd:Bool -> @+i1:U32 -> @+i2:U32 -> @+a:F32 -> @r2:Pair(Array<F32>, F32) -> @rc:Pair(Array<F32>, F32) -> @rs:Pair(Array<F32>, F32) -> RS3

one pair (a = x[i1], b = x[i2]) rotated in place; fwd picks the direction: (a cos - b sin, b cos + a sin) forward, (a cos + b sin, b cos - a sin) backward, in PyTorch's order of operations for each

def rope_p2 source · line 2297 · raw

@+fwd:Bool -> @+i1:U32 -> @+i2:U32 -> @+k:U32 -> @cs:Array<F32> -> @sn:Array<F32> -> @r1:Pair(Array<F32>, F32) -> RS3

def rope_one source · line 2303 · raw

@+e:U32 -> @+fwd:Bool -> @+hh:U32 -> @+half:U32 -> @+w:U32 -> @+t:U32 -> @st:RS3 -> RS3

pair e of the n x w matrix: row e / hh (hh = h * half pairs a row), head (e mod hh) / half, column c of it

def rope_e source · line 2312 · raw

@left:Nat -> @+e:U32 -> @+fwd:Bool -> @+hh:U32 -> @+half:U32 -> @+w:U32 -> @+t:U32 -> @st:RS3 -> RS3

pairs e.., left more after e

def rope_fin source · line 2319 · raw

@-n:Nat -> @-w:Nat -> @st:RS3 -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>

def rope_go2 source · line 2324 · raw

@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+t:Nat -> @+h:Nat -> @+half:Nat -> @x:Array<F32> -> @tb:CS -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>

def rope_go source · line 2329 · raw

@+n:Nat -> @+w:Nat -> @+fwd:Bool -> @+t:Nat -> @+h:Nat -> @+half:Nat -> @+base:F32 -> @m:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w> -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>

def RoPE source · line 2335 · raw

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

n rows (b sequences of t positions), w = h heads of 2*half numbers each

def kv_of source · line 2355 · raw

@+hi:Nat -> @+g:Nat -> Nat

the key/value head that query head hi reads, with g query heads per key/value head; the forward and the backward pass both pick it here (the law kv_of_group says which heads share one)

def rcat source · line 2359 · raw

@xs:List<&2, F32> -> @acc:List<&2, F32> -> List<&2, F32>

xs reversed onto acc

def drow source · line 2371 · raw

@m:Nat -> @+hd:Nat -> @+ia:U32 -> @+ib:U32 -> @+bs:U32 -> @acc:List<&2, F32> -> @r:D2 -> DR

the dots of one row of a (hd numbers from ia) with consecutive rows of b (hd numbers from ib, each bs further): the current one r and m more, in order

def dr_row source · line 2378 · raw

@r:DR -> List<&2, F32>

def qrow source · line 2385 · raw

@+i:Nat -> @+hd:Nat -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> DR

the scores of the query at position i (row i of q, qs apart, the head's hd columns from qc) against keys 0..i (rows of k, ks apart, from kc): the one place attention reads keys, i + 1 of them (the law causal_keys)

def pw source · line 2395 · raw

@xs:List<&2, F32> -> @+idx:U32 -> @+sc:F32 -> @n:Nat -> @p:Array<F32> -> PW

xs scaled by sc, written into p from idx on; n counts the numbers written (from the n given)

def pr_n source · line 2405 · raw

@r:PR -> Nat

def pr_of source · line 2410 · raw

@q:Array<F32> -> @k:Array<F32> -> @r:PC -> PR

def prow3 source · line 2416 · raw

@+off:U32 -> @q:Array<F32> -> @k:Array<F32> -> @w:PW -> PR

the softmax over the n numbers pw wrote; the row's count is the one the softmax reports

def prow2 source · line 2421 · raw

@+sc:F32 -> @+off:U32 -> @p:Array<F32> -> @r:DR -> PR

def prow source · line 2429 · raw

@+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> @p:Array<F32> -> PR

row i of a head's P (t x t, row-major, created zeros): the scaled scores of query i against keys 0..i (qrow) written from the row's start, then the softmax over as many positions as were written: i + 1 (the law causal). The positions after i are never written and keep their zeros.

def prows source · line 2434 · raw

@left:Nat -> @+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @r:PR -> QK

rows i.. of a head's P: r holds rows up to i; left rows more after it. Queries are rows of q (qs apart, the head's columns from qc), keys rows of k (ks apart, from kc).

def probs source · line 2442 · raw

@+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> QK

P of one head: queries in q (row stride qs, columns from qc), keys in k (row stride ks, columns from kc)

def frow source · line 2451 · raw

@+i:Nat -> @+m:Nat -> @+hd:Nat -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> DR

---- the same P with every key visible: full attention. qrow stops at the query's own position (i + 1 keys, the causal law); frow reads all t keys and fprow softmaxes over all of them, so the row's zeros after position i do not exist here. Everything else (the scaling, the write order, the softmax) is the causal path's own functions, so the two agree bit for bit on the triangle the causal one covers.

def fprow2 source · line 2454 · raw

@+sc:F32 -> @+off:U32 -> @p:Array<F32> -> @r:DR -> PR

def fprow source · line 2459 · raw

@+i:Nat -> @+m:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> @p:Array<F32> -> PR

row i of a head's P with every key: the scaled scores of query i against keys 0..t-1 (frow), then the softmax over all t of them (the law full)

def fprows source · line 2462 · raw

@left:Nat -> @+i:Nat -> @+m:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @r:PR -> QK

def fprobs source · line 2469 · raw

@+m:Nat -> @+hd:Nat -> @+sc:F32 -> @+qs:U32 -> @+qc:U32 -> @+ks:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> QK

def pmul source · line 2477 · raw

@left:Nat -> @+idx:U32 -> @+c:U32 -> @+t:Nat -> @+bs:U32 -> @+bc:U32 -> @+ow:U32 -> @+oc:U32 -> @o:Array<F32> -> @r:D2 -> PO

o[i·ow + oc + d] = a[i·t + 0]·b[0·bs + bc + d] + ... + a[i·t + t-1]·b[(t-1)·bs + bc + d], for idx = i·c + d from idx on, left more after r (P·V, and dQ = G·K): each sum left to right from +0.0

def acc_set source · line 2484 · raw

@+idx:U32 -> @+s:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

def pacc source · line 2491 · raw

@left:Nat -> @+idx:U32 -> @+c:U32 -> @+t:Nat -> @+bs:U32 -> @+bc:U32 -> @o:Array<F32> -> @r:D2 -> PO

o[j·c + d] += a[0·t + j]·b[0·bs + bc + d] + ... + a[(t-1)·t + j]·b[(t-1)·bs + bc + d], for idx = j·c + d from idx on (dK += Gᵀ·Q and dV += Pᵀ·dO): each sum left to right from +0.0, then added into the group's running sum

def rows_out source · line 2499 · raw

@+t:Nat -> @+c:Nat -> @+r0:Nat -> @src:Array<F32> -> A2

rows r0.. (t of them, c wide) of src into a new array, src handed back

def a2_b source · line 2502 · raw

@r:A2 -> Array<F32>

def rows_in source · line 2508 · raw

@+t:Nat -> @+c:Nat -> @+r0:Nat -> @blk:Array<F32> -> @dst:Array<F32> -> Array<F32>

a block of t rows, c wide, written into dst from row r0; the block is dropped

def pmul_copy source · line 2511 · raw

@left:Nat -> @+idx:U32 -> @+c:U32 -> @+ow:U32 -> @+oc:U32 -> @o:Array<F32> -> @r:Pair(Array<F32>, F32) -> A2

def cols_in source · line 2519 · raw

@+t:Nat -> @+c:Nat -> @+ow:U32 -> @+oc:U32 -> @src:Array<F32> -> @dst:Array<F32> -> Array<F32>

dst's columns oc.. (c of them, ow wide) of rows 0..t-1 from src (t x c): src dropped

def pick_copy source · line 2523 · raw

@left:Nat -> @+idx:U32 -> @+c:U32 -> @+ow:U32 -> @+oc:U32 -> @d:Array<F32> -> @r:Pair(Array<F32>, F32) -> A2

t x c numbers of src read from rows r0.. and columns c0.. (src ow wide), written in order into d; idx counts d's cells

def block_out source · line 2531 · raw

@+t:Nat -> @+c:Nat -> @+ow:Nat -> @+r0:Nat -> @+c0:Nat -> @src:Array<F32> -> A2

the block of rows r0.. (t of them) and columns c0.. (c of them) of src (ow wide) into a new t x c array, src handed back

def block_in source · line 2535 · raw

@+t:Nat -> @+c:Nat -> @+ow:Nat -> @+r0:Nat -> @+c0:Nat -> @blk:Array<F32> -> @dst:Array<F32> -> Array<F32>

a t x c block into dst's rows r0.. and columns c0.. (dst ow wide); the block is dropped

def head_cols source · line 2551 · raw

@+dm:Dm -> @+hi:Nat -> HC

def hc_kc source · line 2556 · raw

@+c:HC -> U32

def fs_fin source · line 2561 · raw

@q:Array<F32> -> @k:Array<F32> -> @r:PO -> FS

def head_f2 source · line 2566 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+hc:U32 -> @+kc:U32 -> @v:Array<F32> -> @o:Array<F32> -> @r:QK -> FS

def head_fwd source · line 2573 · raw

@+dm:Dm -> @+cols:HC -> @s:FS -> FS

one head's forward pass at the columns hc (its queries) and kc (its key/value head) that head_cols gives; it has no head index, so it reads no other columns

def fheads source · line 2578 · raw

@left:Nat -> @+hi:Nat -> @+dm:Dm -> @s:FS -> FS

def fs_o source · line 2585 · raw

@s:FS -> Array<F32>

def gfix source · line 2597 · raw

@k:Nat -> @+idx:U32 -> @+s:F32 -> @+sc:F32 -> @rg:Pair(Array<F32>, F32) -> @rp:Pair(Array<F32>, F32) -> A2

g[j] = (p[j] * (g[j] - s)) * sc for j = idx..idx + k; rg and rp hold g[idx] and p[idx]

def g_set2 source · line 2604 · raw

@+t:Nat -> @+sc:F32 -> @+off:U32 -> @r:D2 -> A2

def gw source · line 2616 · raw

@xs:List<&2, F32> -> @+idx:U32 -> @n:Nat -> @g:Array<F32> -> GN

xs written into g from idx, counted onto n

def gr_n source · line 2626 · raw

@r:GR -> Nat

def gr3 source · line 2632 · raw

@+sc:F32 -> @+off:U32 -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @w:GN -> GR

the n positions of the row written: s = sum(dP * P) over them (from +0.0, in order), then G = (P * (dP - s)) * scale

def gr2 source · line 2637 · raw

@+sc:F32 -> @+off:U32 -> @p:Array<F32> -> @g:Array<F32> -> @r:DR -> GR

def grow source · line 2646 · raw

@+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+ds:U32 -> @+dc:U32 -> @+vs:U32 -> @+vc:U32 -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> GR

row i of G (from i·t, t wide; softmax's backward pass, then the scale): dP's row against values 0..i only (drow, as qrow reads keys 0..i), written from the row's start, and s and G over as many positions as were written: i + 1 (the law causal_grad). G starts as zeros and nothing else writes the row, so its positions after i stay 0: no gradient reaches a key or value after the query's position. (P is 0 there too, so the full row gave the same G.)

def grows source · line 2650 · raw

@left:Nat -> @+i:Nat -> @+t:Nat -> @+hd:Nat -> @+sc:F32 -> @+ds:U32 -> @+dc:U32 -> @+vs:U32 -> @+vc:U32 -> @r:GR -> GW

rows i.. of G; r is row i

def hb5 source · line 2657 · raw

@q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @dq:Array<F32> -> @dka:Array<F32> -> @r:PO -> BS

def hb4 source · line 2663 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+hc:U32 -> @k:Array<F32> -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @dq:Array<F32> -> @dva:Array<F32> -> @r:PO -> BS

dK += Gᵀ·Q, then dV += Pᵀ·dO

def hb3 source · line 2669 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+hc:U32 -> @q:Array<F32> -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @dka:Array<F32> -> @dva:Array<F32> -> @r:PO -> BS

dQ = G·K into the head's columns, then dK

def hb2 source · line 2674 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+hc:U32 -> @+kc:U32 -> @q:Array<F32> -> @k:Array<F32> -> @dq:Array<F32> -> @dka:Array<F32> -> @dva:Array<F32> -> @r:GW -> BS

def hb1 source · line 2680 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+sc:F32 -> @+hc:U32 -> @+kc:U32 -> @v:Array<F32> -> @d:Array<F32> -> @dq:Array<F32> -> @dka:Array<F32> -> @dva:Array<F32> -> @r:QK -> BS

P again, then G

def head_bwd source · line 2686 · raw

@+dm:Dm -> @+cols:HC -> @s:BS -> BS

one head's backward pass at head_cols' columns (no head index, as head_fwd)

def bheads source · line 2691 · raw

@left:Nat -> @+hi:Nat -> @+dm:Dm -> @s:BS -> BS

def bg_fin source · line 2702 · raw

@+t:Nat -> @+hd:Nat -> @+kvw:Nat -> @+j:Nat -> @dk:Array<F32> -> @dv:Array<F32> -> @s:BS -> BG

the group's summed dK and dV into key/value head j's columns of dk and dv

def bgroup source · line 2707 · raw

@+dm:Dm -> @+j:Nat -> @s:BG -> BG

def bgroups source · line 2712 · raw

@left:Nat -> @+j:Nat -> @+dm:Dm -> @s:BG -> BG

def hcat source · line 2726 · raw

@-A:Type -> @h:Halves<A> -> List<&1, A>

the two halves back as one list, in order (what the laws attn_par_fwd and attn_par_bwd compare the trees with)

def cut source · line 2732 · raw

@-A:Type -> @n:Nat -> @xs:List<&1, A> -> @acc:List<&1, A> -> Halves<A>

xs cut after its first n (a tail loop: a helper in the fork tree must not hold the stack)

def fu3 source · line 2745 · raw

@+t:Nat -> @+gw:Nat -> @qq:Array<F32> -> @kk:Array<F32> -> @xs:List<&1, FS> -> @q:Array<F32> -> @k:Array<F32> -> @r:A2 -> FL

a unit of the parallel work is one sequence's key/value group j: its g query heads' columns of Q (t x g·hd), head j's columns of K and V (t x hd), and an output of its own (t x g·hd). Inside a unit the heads keep their order and their arithmetic (head_cols with j = 0: the law kv_cols), so the units give the bits of the whole sequence computed at once.

def fu2 source · line 2750 · raw

@+t:Nat -> @+hd:Nat -> @+kvw:Nat -> @+r0:Nat -> @+kc:Nat -> @+gw:Nat -> @qq:Array<F32> -> @vv:Array<F32> -> @xs:List<&1, FS> -> @q:Array<F32> -> @r:A2 -> FL

def fu1 source · line 2755 · raw

@+t:Nat -> @+hd:Nat -> @+kvw:Nat -> @+r0:Nat -> @+kc:Nat -> @+gw:Nat -> @kk:Array<F32> -> @vv:Array<F32> -> @xs:List<&1, FS> -> @r:A2 -> FL

def funit source · line 2760 · raw

@+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+g:Nat -> @+hd:Nat -> @+bi:Nat -> @+j:Nat -> @s:FL -> FL

def fgroups source · line 2765 · raw

@left:Nat -> @+j:Nat -> @+bi:Nat -> @+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+g:Nat -> @+hd:Nat -> @s:FL -> FL

def fslices source · line 2773 · raw

@left:Nat -> @+bi:Nat -> @+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @s:FL -> FL

every sequence's units, sequence by sequence and group by group, newest first

def fleaf source · line 2780 · raw

@xs:List<&1, FS> -> @+h:Nat -> @+dm:Dm -> @acc:List<&1, Array<F32>> -> List<&1, Array<F32>>

def fpar source · line 2788 · raw

@d:Nat -> @gs:Halves<FS> -> @+n:Nat -> @+h:Nat -> @+dm:Dm -> List<&1, Array<F32>>

the n sequences of gs (cut in two halves) in a tree of parallel lets of depth d; the outputs come back in order

def ub_row source · line 2801 · raw

@left:Nat -> @+j:Nat -> @+t:Nat -> @+gw:Nat -> @+w:Nat -> @+r0:Nat -> @r:UO -> UO

one sequence's units (t x gw each) into o's rows r0.. at columns 0, gw, 2·gw, ...

def blocks_in source · line 2809 · raw

@left:Nat -> @+bi:Nat -> @+t:Nat -> @+gw:Nat -> @+w:Nat -> @+kv:Nat -> @r:UO -> Array<F32>

the units' outputs into o (n x w), in the order fslices made them

def bu4 source · line 2821 · raw

@+t:Nat -> @+gw:Nat -> @+hd:Nat -> @qq:Array<F32> -> @kk:Array<F32> -> @vv:Array<F32> -> @xs:List<&1, BG> -> @q:Array<F32> -> @k:Array<F32> -> @v:Array<F32> -> @r:A2 -> BL

the backward pass's unit: one sequence's group j, as fslices' (Q, K, V, and dO's columns of the g heads), with dQ, dK and dV of its own

def bu3 source · line 2826 · raw

@+t:Nat -> @+w:Nat -> @+hd:Nat -> @+r0:Nat -> @+qc:Nat -> @+gw:Nat -> @qq:Array<F32> -> @kk:Array<F32> -> @dd:Array<F32> -> @xs:List<&1, BG> -> @q:Array<F32> -> @k:Array<F32> -> @r:A2 -> BL

def bu2 source · line 2831 · raw

@+t:Nat -> @+w:Nat -> @+hd:Nat -> @+kvw:Nat -> @+r0:Nat -> @+qc:Nat -> @+kc:Nat -> @+gw:Nat -> @qq:Array<F32> -> @vv:Array<F32> -> @dd:Array<F32> -> @xs:List<&1, BG> -> @q:Array<F32> -> @r:A2 -> BL

def bu1 source · line 2836 · raw

@+t:Nat -> @+w:Nat -> @+hd:Nat -> @+kvw:Nat -> @+r0:Nat -> @+qc:Nat -> @+kc:Nat -> @+gw:Nat -> @kk:Array<F32> -> @vv:Array<F32> -> @dd:Array<F32> -> @xs:List<&1, BG> -> @r:A2 -> BL

def bunit source · line 2841 · raw

@+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+g:Nat -> @+hd:Nat -> @+bi:Nat -> @+j:Nat -> @s:BL -> BL

def bgroups_u source · line 2846 · raw

@left:Nat -> @+j:Nat -> @+bi:Nat -> @+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+g:Nat -> @+hd:Nat -> @s:BL -> BL

def bslices source · line 2853 · raw

@left:Nat -> @+bi:Nat -> @+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+kv:Nat -> @+g:Nat -> @+hd:Nat -> @s:BL -> BL

def db_of source · line 2863 · raw

@s:BG -> DB

def bleaf source · line 2868 · raw

@xs:List<&1, BG> -> @+kv:Nat -> @+dm:Dm -> @acc:List<&1, DB> -> List<&1, DB>

def bpar source · line 2875 · raw

@d:Nat -> @gs:Halves<BG> -> @+n:Nat -> @+kv:Nat -> @+dm:Dm -> List<&1, DB>

def ud_row source · line 2891 · raw

@left:Nat -> @+j:Nat -> @+t:Nat -> @+gw:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+r0:Nat -> @r:UD -> UD

one sequence's units into rows r0..: dQ at columns j·gw (w wide), dK and dV at j·hd (kvw wide)

def dblocks_in source · line 2898 · raw

@left:Nat -> @+bi:Nat -> @+t:Nat -> @+gw:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+kv:Nat -> @r:UD -> D3

def dm_of source · line 2911 · raw

@+dm:AttnDims -> Dm

a unit's dimensions: g query heads (g·hd columns) over one key/value head (hd columns)

def attn_tree source · line 2923 · raw

@+b:Nat -> @+t:Nat -> @+w:Nat -> Bool

whether the units (a sequence's key/value group each, fslices) run in the tree: from 8 units on, or from 2 when each is big. With the tiny Llama's sequences (t = 16, w = 32) the tree cost more than it gave at 2 sequences (0.48 against 0.37 ms on 16 threads) and gained from 8 on (1.32 against 1.52 ms; 1.5x at 32, 1.7x at 128); with SmolLM2's (t = 128, w = 576, about half a second of a step each) the two sequences one after another kept a single thread busy for a third of the step (NOTES, 2026-10-21), and its 2 sequences x 3 groups now give 6 units. t w counts in U32 (a Nat comparison walks its numbers one unit at a time). The answer never changes the result: the laws attn_par_fwd and attn_par_bwd show the tree gives the units in order, as one after another.

def fgo source · line 2926 · raw

@big:Bool -> @+b:Nat -> @+h:Nat -> @+dm:Dm -> @xs:List<&1, FS> -> List<&1, Array<F32>>

def bgo source · line 2933 · raw

@big:Bool -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @xs:List<&1, BG> -> List<&1, DB>

def af2 source · line 2940 · raw

@-n:Nat -> @+w:Nat -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @s:FL -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

def attn_fwd source · line 2945 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+dm:AttnDims -> @x:Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>> -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

def head_full source · line 2954 · raw

@+dm:Dm -> @+cols:HC -> @s:FS -> FS

---- the same forward with every key visible. The leaf is the only new arithmetic: head_full builds P with fprobs instead of probs; the tree, the slices and the P·V are the causal path's own functions. Kept beside the causal ones rather than behind a flag so that no call into Attention changes and the laws attn_par_fwd / attn_par_bwd still compare the functions they name.

def fheads_full source · line 2959 · raw

@left:Nat -> @+hi:Nat -> @+dm:Dm -> @s:FS -> FS

def fleaf_full source · line 2966 · raw

@xs:List<&1, FS> -> @+h:Nat -> @+dm:Dm -> @acc:List<&1, Array<F32>> -> List<&1, Array<F32>>

def fpar_full source · line 2973 · raw

@d:Nat -> @gs:Halves<FS> -> @+n:Nat -> @+h:Nat -> @+dm:Dm -> List<&1, Array<F32>>

def fgo_full source · line 2982 · raw

@big:Bool -> @+b:Nat -> @+h:Nat -> @+dm:Dm -> @xs:List<&1, FS> -> List<&1, Array<F32>>

def af2_full source · line 2989 · raw

@-n:Nat -> @+w:Nat -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @s:FL -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

def attn_fwd_full source · line 2994 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+dm:AttnDims -> @x:Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>> -> Fwd<Unit, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

def ab3 source · line 2999 · raw

@-n:Nat -> @-w:Nat -> @-kvw:Nat -> @o:D3 -> Bwd<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>>

def ab2 source · line 3004 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @s:BL -> Bwd<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>>

def attn_bwd source · line 3009 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+dm:AttnDims -> @s:AttnS -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w> -> Bwd<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>>

def fgrow source · line 3016 · raw

@+i:Nat -> @+m:Nat -> @+hd:Nat -> @+sc:F32 -> @+ds:U32 -> @+dc:U32 -> @+vs:U32 -> @+vc:U32 -> @d:Array<F32> -> @v:Array<F32> -> @p:Array<F32> -> @g:Array<F32> -> GR

---- the backward of the full-visible forward. G's row i also needs every key, so fgrow takes t - 1 dots where grow takes i; the rest (gfix, pmul, pacc, the group sum, the tree) is the causal path's own code.

def fgrows source · line 3019 · raw

@left:Nat -> @+i:Nat -> @+m:Nat -> @+hd:Nat -> @+sc:F32 -> @+ds:U32 -> @+dc:U32 -> @+vs:U32 -> @+vc:U32 -> @r:GR -> GW

def fhb1 source · line 3026 · raw

@+t:Nat -> @+hd:Nat -> @+w:Nat -> @+kvw:Nat -> @+sc:F32 -> @+hc:U32 -> @+kc:U32 -> @v:Array<F32> -> @d:Array<F32> -> @dq:Array<F32> -> @dka:Array<F32> -> @dva:Array<F32> -> @r:QK -> BS

def head_bwd_full source · line 3031 · raw

@+dm:Dm -> @+cols:HC -> @s:BS -> BS

def bheads_full source · line 3036 · raw

@left:Nat -> @+hi:Nat -> @+dm:Dm -> @s:BS -> BS

def bgroup_full source · line 3043 · raw

@+dm:Dm -> @+j:Nat -> @s:BG -> BG

def bgroups_full source · line 3048 · raw

@left:Nat -> @+j:Nat -> @+dm:Dm -> @s:BG -> BG

def bleaf_full source · line 3055 · raw

@xs:List<&1, BG> -> @+kv:Nat -> @+dm:Dm -> @acc:List<&1, DB> -> List<&1, DB>

def bpar_full source · line 3062 · raw

@d:Nat -> @gs:Halves<BG> -> @+n:Nat -> @+kv:Nat -> @+dm:Dm -> List<&1, DB>

def bgo_full source · line 3071 · raw

@big:Bool -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @xs:List<&1, BG> -> List<&1, DB>

def ab2_full source · line 3078 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+b:Nat -> @+kv:Nat -> @+dm:Dm -> @s:BL -> Bwd<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>>

def attn_bwd_full source · line 3083 · raw

@+n:Nat -> @+w:Nat -> @+kvw:Nat -> @+dm:AttnDims -> @s:AttnS -> @dy:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w> -> Bwd<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>>

def FullAttention source · line 3090 · 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} -> Layer<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

the same layer as Attention with every key visible; Attention itself is untouched, so the causal laws and the bits of every existing model stay as they are

def Attention source · line 3094 · 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} -> Layer<Unit, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, Two<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, kvw>>>, 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, w>, AttnS>

the core of attention, after the projections and RoPE; the erased arguments are the shape facts it relies on

def hit source · line 3105 · raw

@b:Bool -> F32

def ts_add source · line 3115 · raw

@+i:U32 -> @+x:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

the sum of a[0] .. a[m-1] as a balanced tree of additions (pairwise summation), in place: each pass adds neighbours (a[i] = a[2i] + a[2i+1], an odd last one carried), until one number is left. Its rounding error grows with log2(m), not m, as in PyTorch's cascade sum: a running sum of 295k squares was 1e-4 off (NOTES, 2026-10-17)

def ts_pair source · line 3120 · raw

@+i:U32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

def ts_pairs source · line 3125 · raw

@k:Nat -> @+i:U32 -> @a:Array<F32> -> Array<F32>

def ts_put source · line 3132 · raw

@+i:U32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

def ts_carry source · line 3137 · raw

@odd:Bool -> @+m:U32 -> @a:Array<F32> -> Array<F32>

def ts_pass source · line 3144 · raw

@+m:U32 -> @a:Array<F32> -> Array<F32>

def ts_first source · line 3147 · raw

@r:Pair(Array<F32>, F32) -> F32

def ts_go source · line 3152 · raw

@f:Nat -> @done:Bool -> @+m:U32 -> @a:Array<F32> -> F32

def tree_sum source · line 3161 · raw

@+m:U32 -> @a:Array<F32> -> F32

def cesm4 source · line 3171 · raw

@+k:Nat -> @+off:U32 -> @+m:F32 -> @+s:F32 -> @a:Array<F32> -> CSM

def cesm3 source · line 3174 · raw

@+k:Nat -> @+off:U32 -> @+m:F32 -> @r:A2 -> CSM

def cesm_e source · line 3179 · raw

@+k:Nat -> @+off:U32 -> @+m:F32 -> @r:AS -> CSM

def cesm2 source · line 3184 · raw

@+k:Nat -> @+off:U32 -> @r:AS -> CSM

def ce_sm source · line 3189 · raw

@+c:Nat -> @+off:U32 -> @z:Array<F32> -> CSM

def ce_g source · line 3193 · raw

@k:Nat -> @+idx:U32 -> @+j:U32 -> @+y:U32 -> @+nf:F32 -> @r:Pair(Array<F32>, F32) -> Array<F32>

z[idx] = (p - [j == y]) / nf for j = jj..jj + k, p the row's probabilities

def ce_pick2 source · line 3202 · raw

@+xy:F32 -> @loss:F32 -> @r:CSM -> AS

loss - log_softmax(x)[y], log_softmax as PyTorch computes it: (x[y] - m) - log(sum exp(x - m)) (since 2026-10-18; it was loss - log(max(p[y], 1e-12)), which capped a row's loss at 27.63 where PyTorch's grows, NOTES)

def ce_pick3 source · line 3207 · raw

@+c:Nat -> @+off:U32 -> @loss:F32 -> @r:Pair(Array<F32>, F32) -> AS

def ce_bad source · line 3212 · raw

@loss:F32 -> @r:CSM -> AS

def ce_pick source · line 3218 · raw

@ok:Bool -> @+c:Nat -> @+off:U32 -> @+idx:U32 -> @loss:F32 -> @z:Array<F32> -> AS

a label past the last class picks probability 0.0, clamped at 1e-12, as tensor-array's pick

def ce_row2 source · line 3225 · raw

@+c:Nat -> @+off:U32 -> @+y:Nat -> @+nf:F32 -> @r:AS -> AS

def ce_row source · line 3230 · raw

@+c:Nat -> @+off:U32 -> @+y:Nat -> @+nf:F32 -> @s:AS -> AS

def ce_rows source · line 3235 · raw

@labels:List<&2, Nat> -> @+c:Nat -> @+i:U32 -> @+nf:F32 -> @s:AS -> AS

def ce_fin source · line 3242 · raw

@-n:Nat -> @-c:Nat -> @+nf:F32 -> @r:AS -> LossOut<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def CrossEntropy source · line 3247 · raw

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

def mse_l source · line 3257 · raw

@k:Nat -> @+idx:U32 -> @+kk:F32 -> @q:Array<F32> -> @ry:Pair(Array<F32>, F32) -> @rt:Pair(Array<F32>, F32) -> D3S

over every number from idx on (k more): d = y - t, q[idx] = d·d, y[idx] = d * kk

def mse_fin source · line 3264 · raw

@-n:Nat -> @-c:Nat -> @+m:U32 -> @+nc:F32 -> @r:D3S -> LossOut<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def MSE source · line 3271 · raw

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

mean squared error over every number (PyTorch's F.mse_loss); its gradient is 2 (y - t) / (n*c); the squares summed as a tree

def nll_hit source · line 3279 · raw

@+idx:U32 -> @+v:F32 -> @g:Array<F32> -> @acc:F32 -> @r:Pair(Array<F32>, F32) -> NS

def nll_one source · line 3285 · raw

@ok:Bool -> @+idx:U32 -> @+v:F32 -> @acc:F32 -> @lp:Array<F32> -> @g:Array<F32> -> NS

a label past the last class picks 0.0 and leaves its gradient row zero, as l_at and onehot do

def nll_rows source · line 3293 · raw

@left:Nat -> @labels:List<&2, Nat> -> @+i:U32 -> @+c:Nat -> @+v:F32 -> @st:NS -> NS

the sum of the picked log-probabilities (in row order, from +0.0), and the gradient (-1/n at each label)

def nll_fin source · line 3300 · raw

@-c:Nat -> @+n:Nat -> @r:NS -> LossOut<0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.Mat<n, c>>

def NLL source · line 3306 · raw

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

negative log-likelihood of log-probabilities (PyTorch's F.nll_loss, mean over the rows)

def byte_of source · line 3317 · raw

@+u:U32 -> @+k:Nat -> U32

def read_all source · line 3320 · raw

@+path:String -> IO(List<&2, U32>)

def ph_hw source · line 3331 · raw

@+n:U32 -> U32

def ph_bool source · line 3334 · raw

@b:Bool -> U32

def ptu_node source · line 3342 · raw

@ra:PTU -> @rb:PTU -> PTU

def pt_cells source · line 3347 · raw

@t:PTree -> PTU

the number of cells a tree takes (the tree comes back)

def words_out source · line 3362 · raw

@left:Nat -> @+i:U32 -> @+off:U32 -> @ra:Pair(Array<U32>, U32) -> @xs:Array<F32> -> UA

words i.. of a packed buffer into xs from off, as F32 cells with their bits (both arrays come back)

def words_in source · line 3368 · raw

@left:Nat -> @+i:U32 -> @+off:U32 -> @rx:Pair(Array<F32>, F32) -> @a:Array<U32> -> UA

def pta_then source · line 3374 · raw

@r:PTA -> @k:(@_:PTree -> @_:Array<F32> -> @_:U32 -> PTA) -> PTA

def pta_node source · line 3378 · raw

@a:PTree -> @r:PTA -> PTA

def pts_v source · line 3382 · raw

@+n:U32 -> @m:Array<U32> -> @+off:U32 -> @r:UA -> PTA

def pts_m source · line 3386 · raw

@+n:U32 -> @v:Array<U32> -> @+off:U32 -> @r:UA -> PTA

def ptsv_v source · line 3390 · raw

@+n:U32 -> @m:Array<U32> -> @+off:U32 -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> PTA

def ptsv_m source · line 3394 · raw

@+n:U32 -> @v:Array<F32> -> @+off:U32 -> @r:UA -> PTA

def pt_store source · line 3399 · raw

@t:PTree -> @xs:Array<F32> -> @+off:U32 -> PTA

the tree's cells into xs from off

def ptl_v source · line 3411 · raw

@+n:U32 -> @m:Array<U32> -> @+off:U32 -> @r:UA -> PTA

reading a leaf back: fresh buffers filled from xs

def ptl_m source · line 3415 · raw

@+n:U32 -> @+off:U32 -> @r:UA -> PTA

def ptlv_v source · line 3419 · raw

@+n:U32 -> @m:Array<U32> -> @+off:U32 -> @r:0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2 -> PTA

def ptlv_m source · line 3423 · raw

@+n:U32 -> @+off:U32 -> @r:UA -> PTA

def pt_kind2 source · line 3434 · raw

@z:Bool -> @o:Bool -> @l:Bool -> PK

def pt_kind source · line 3441 · raw

@+tag:U32 -> PK

def pt_next source · line 3445 · raw

@+off:U32 -> @r:Pair(Array<F32>, F32) -> @k:(@_:PK -> @_:Pair(Array<F32>, F32) -> PTA) -> PTA

the tag at off and the cell after it, to a continuation

def pt_read source · line 3451 · raw

@fuel:Nat -> @k:PK -> @+off:U32 -> @rn:Pair(Array<F32>, F32) -> PTA

the part at off, of kind k, with its second cell rn already read; fuel bounds the depth (a model's pairs nest far less than 1000 deep)

def pt_load source · line 3467 · raw

@xs:Array<F32> -> @+off:U32 -> PTA

a tree from xs at off

def td0 source · line 3473 · raw

@-P:Type -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @rp:PL<P> -> TrainPL<P>

def td1 source · line 3477 · raw

@-P:Type -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @rp:PL<P> -> @rm:PL<P> -> TrainPL<P>

def td3 source · line 3481 · raw

@-P:Type -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @rp:PL<P> -> @rm:PL<P> -> @rv:PL<P> -> TrainPL<P>

def cells_l source · line 3487 · raw

@k:Nat -> @+i:U32 -> @acc:List<&2, F32> -> @ra:Pair(Array<F32>, F32) -> List<&2, F32>

the first k cells of an array, in order

def tdp2 source · line 3494 · raw

@-P:Type -> @p:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @xp:List<&2, F32> -> @r:PTA -> TrainPL<P>

def tdp source · line 3499 · raw

@-P:Type -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @rp:PL<P> -> @rt:PTU -> TrainPL<P>

def f32_at source · line 3517 · raw

@xs:List<&2, F32> -> @n:Nat -> F32

def tr0 source · line 3526 · raw

@-P:Type -> @g:P -> @r:PA<P> -> Train<P>

def tr_m2 source · line 3530 · raw

@-P:Type -> @p:P -> @g:P -> @r:PA<P> -> Train<P>

def tr_adam2 source · line 3538 · raw

@-P:Type -> @p:P -> @mm:P -> @vv:P -> @g:P -> @p1:F32 -> @r:Pair(Array<F32>, F32) -> Train<P>

def tr_adam source · line 3542 · raw

@-P:Type -> @p:P -> @mm:P -> @vv:P -> @g:P -> @r:Pair(Array<F32>, F32) -> @+off:U32 -> Train<P>

def tr3 source · line 3546 · raw

@-P:Type -> @p:P -> @mm:P -> @g:P -> @r:PA<P> -> Train<P>

def trp4 source · line 3560 · raw

@-P:Type -> @p:P -> @g:P -> @t:PTree -> @+p1:F32 -> @r:Pair(Array<F32>, F32) -> Train<P>

def trp3 source · line 3564 · raw

@-P:Type -> @p:P -> @g:P -> @t:PTree -> @r:Pair(Array<F32>, F32) -> @+off:U32 -> Train<P>

def trp2 source · line 3568 · raw

@-P:Type -> @p:P -> @g:P -> @r:PTA -> Train<P>

def trp source · line 3572 · raw

@-P:Type -> @g:P -> @r:PA<P> -> Train<P>

def WCHUNK source · line 3593 · raw

U32

def chunk_bytes source · line 3597 · raw

@k:Nat -> @+i:U32 -> @rx:Pair(Array<F32>, F32) -> @acc:List<&2, U32> -> Pair(Array<F32>, List<&2, U32>)

the bytes of xs[i - k + 1 .. i] onto acc, built from the last number back (no reverse)

def wc_ok source · line 3604 · raw

@-R:Type -> @rr:Pair(File, Result<&1, &1, Pair(U32, String), Unit>) -> @a:Array<F32> -> @k:(@_:File -> @_:Array<F32> -> IO(R)) -> IO(R)

def wc_bytes source · line 3611 · raw

@-R:Type -> @f:File -> @r:Pair(Array<F32>, List<&2, U32>) -> @k:(@_:File -> @_:Array<F32> -> IO(R)) -> IO(R)

def write_chunks source · line 3619 · raw

@k:Nat -> @f:File -> @a:Array<F32> -> @+lo:U32 -> @+n:U32 -> IO(Pair(File, Array<F32>))

k chunks of a's numbers lo .. n - 1

def wf_close source · line 3627 · raw

@r:Pair(File, Array<F32>) -> IO(Array<F32>)

def write_f32 source · line 3635 · raw

@+path:String -> @a:Array<F32> -> @+n:U32 -> IO(Array<F32>)

cells 0 .. n - 1 of a, as little-endian F32, into a new file at path; a comes back

def ps_fin source · line 3641 · raw

@-P:Type -> @+path:String -> @+n:U32 -> @r:PA<P> -> IO(P)

def tsa_fin source · line 3655 · raw

@-P:Type -> @+path:String -> @g:P -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

def tsa_m source · line 3662 · raw

@-P:Type -> @+path:String -> @p:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

def tsa_pw source · line 3666 · raw

@-P:Type -> @+path:String -> @p:P -> @mm:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

def tsp3 source · line 3683 · raw

@-P:Type -> @+path:String -> @p:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PTA -> IO(Train<P>)

def tsp2 source · line 3688 · raw

@-P:Type -> @+path:String -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @t:PTree -> @r:PA<P> -> IO(Train<P>)

Templates

template ew1_l source · line 167 · raw

@-f:(@+a:F32 -> F32) -> @nleft:Nat -> @+t:U32 -> @ra:Pair(Array<F32>, F32) -> Array<F32>

a[t] = f(a[t]) for the n numbers from t; ra holds the array and a[t], already read

template ew2_l source · line 175 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @nleft:Nat -> @+t:U32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> 0xf4dfe0b415f12cd7b6b3e20aa7cc5aa0/main.P2

a[t] = f(a[t], b[t]): the result replaces a; b comes back unchanged

template Ew.map source · line 182 · raw

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

template Ew.zip source · line 193 · raw

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

(f(a, b), b), element by element

template ActOut source · line 749 · raw

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

template seq_f2 source · line 772 · raw

@-P2:Type -> @-Y:Type -> @-Z:Type -> @-S2:Type -> @-lb:Layer<P2, Y, Z, S2> -> @-P1:Type -> @-S1:Type -> @p2:P2 -> @r:Fwd<P1, Y, S1> -> Fwd<Two<P1, P2>, Z, Two<S1, S2>>

template seq_fwd source · line 777 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y, S1> -> @-lb:Layer<P2, Y, Z, S2> -> @p:Two<P1, P2> -> @x:X -> Fwd<Two<P1, P2>, Z, Two<S1, S2>>

template seq_b2 source · line 787 · raw

@-P1:Type -> @-X:Type -> @-Y:Type -> @-S1:Type -> @-la:Layer<P1, X, Y, S1> -> @-P2:Type -> @p1:P1 -> @s1:S1 -> @g1:P1 -> @r:Bwd<P2, Y> -> Bwd<Two<P1, P2>, X>

template seq_bwd source · line 792 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y, S1> -> @-lb:Layer<P2, Y, Z, S2> -> @p:Two<P1, P2> -> @s:Two<S1, S2> -> @dz:Z -> @g:Two<P1, P2> -> Bwd<Two<P1, P2>, X>

template seq_s2 source · line 800 · raw

@-P2:Type -> @-Y:Type -> @-Z:Type -> @-S2:Type -> @-lb:Layer<P2, Y, Z, S2> -> @-P1:Type -> @+op:Opt -> @p2:P2 -> @g2:P2 -> @s2:OptState<P2> -> @r1:Step<P1> -> Step<Two<P1, P2>>

template seq_step2 source · line 803 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y, S1> -> @-lb:Layer<P2, Y, Z, S2> -> @+op:Opt -> @p:Two<P1, P2> -> @g:Two<P1, P2> -> @st:Two<OptState<P1>, OptState<P2>> -> Step<Two<P1, P2>>

template seq_step source · line 808 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y, S1> -> @-lb:Layer<P2, Y, Z, S2> -> @+op:Opt -> @p:Two<P1, P2> -> @g:Two<P1, P2> -> @st:OptState<Two<P1, P2>> -> Step<Two<P1, P2>>

template pair_zeros source · line 814 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> Two<P1, P2>

dump and load for a pair of layers: the first layer's numbers, then the second's a pair's zeros, the two halves in a parallel let: a model's gradient and optimizer state are many arrays, and making them one after another left the training state's zeroing (page faults, mostly) on one thread

template pair_dump source · line 818 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @p:Two<P1, P2> -> PL<Two<P1, P2>>

template pair_load2 source · line 823 · raw

@-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-lb:Layer<P2, X2, Y2, S2> -> @-P1:Type -> @p2:P2 -> @r:PA<P1> -> PA<Two<P1, P2>>

template pair_load source · line 828 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @p:Two<P1, P2> -> @xs:Array<F32> -> @off:U32 -> PA<Two<P1, P2>>

template pair_store2 source · line 833 · raw

@-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-lb:Layer<P2, X2, Y2, S2> -> @-P1:Type -> @p2:P2 -> @r:PA<P1> -> PA<Two<P1, P2>>

template pair_store source · line 838 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @p:Two<P1, P2> -> @xs:Array<F32> -> @off:U32 -> PA<Two<P1, P2>>

template Seq source · line 843 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y, S1> -> @-lb:Layer<P2, Y, Z, S2> -> Layer<Two<P1, P2>, X, Z, Two<S1, S2>>

template OptState.new source · line 905 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @cfg:OptCfg -> OptState<P>

template Train.new source · line 920 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+cfg:OptCfg -> @p:P -> Train<P>

template ts4 source · line 929 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+cfg:OptCfg -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:Bwd<P, X> -> Train<P>

template ts3 source · line 936 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+cfg:OptCfg -> @p:P -> @s:S -> @g:P -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @r:LossOut<Y> -> Train<P>

template ts2 source · line 941 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-T:Type -> @-m:Layer<P, X, Y, S> -> @-loss:(@_:Y -> @_:T -> LossOut<Y>) -> @+cfg:OptCfg -> @g:P -> @st:OptState<P> -> @+p1:F32 -> @+p2:F32 -> @t:T -> @r:Fwd<P, Y, S> -> Train<P>

template Train.step source · line 947 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-T:Type -> @-m:Layer<P, X, Y, S> -> @-loss:(@_:Y -> @_:T -> LossOut<Y>) -> @+cfg:OptCfg -> @tr:Train<P> -> @x:X -> @t:T -> Train<P>

forward, loss, backward and one optimizer step: the same code for every model

template acti_f3 source · line 1054 · raw

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

an activation whose derivative needs its input (silu, gelu): saves a copy of the input

template ActIn source · line 1059 · raw

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

template Id source · line 2077 · raw

@-X:Type -> Layer<Unit, X, X, Unit>

template pair_step2 source · line 2081 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @+op:Opt -> @p:Two<P1, P2> -> @g:Two<P1, P2> -> @st:Two<OptState<P1>, OptState<P2>> -> Step<Two<P1, P2>>

one optimizer step on two layers' parameters, of any types

template pair_step source · line 2086 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @+op:Opt -> @p:Two<P1, P2> -> @g:Two<P1, P2> -> @st:OptState<Two<P1, P2>> -> Step<Two<P1, P2>>

template pair_f3 source · line 2095 · raw

@-P2:Type -> @-X2:Type -> @-Y2:Type -> @-S2:Type -> @-lb:Layer<P2, X2, Y2, S2> -> @-P1:Type -> @-Y1:Type -> @-S1:Type -> @p2:P2 -> @x2:X2 -> @r:Fwd<P1, Y1, S1> -> Fwd<Two<P1, P2>, Two<Y1, Y2>, Two<S1, S2>>

runs lb on x2 once la's forward pass is done

template fork_f2 source · line 2102 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X, Y1, S1> -> @-lb:Layer<P2, X, Y2, S2> -> @p1:P1 -> @p2:P2 -> @d:Two<X, X> -> Fwd<Two<P1, P2>, Two<Y1, Y2>, Two<S1, S2>>

template fork_b3 source · line 2107 · raw

@-X:Type -> @-add:(@_:X -> @_:X -> X) -> @-P1:Type -> @-P2:Type -> @p2:P2 -> @g2:P2 -> @dx2:X -> @r:Bwd<P1, X> -> Bwd<Two<P1, P2>, X>

template fork_b2 source · line 2112 · raw

@-P1:Type -> @-X:Type -> @-Y1:Type -> @-S1:Type -> @-add:(@_:X -> @_:X -> X) -> @-la:Layer<P1, X, Y1, S1> -> @-P2:Type -> @p1:P1 -> @s1:S1 -> @d1:Y1 -> @g1:P1 -> @r:Bwd<P2, X> -> Bwd<Two<P1, P2>, X>

template fork_bwd source · line 2117 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-add:(@_:X -> @_:X -> X) -> @-la:Layer<P1, X, Y1, S1> -> @-lb:Layer<P2, X, Y2, S2> -> @p:Two<P1, P2> -> @s:Two<S1, S2> -> @dy:Two<Y1, Y2> -> @g:Two<P1, P2> -> Bwd<Two<P1, P2>, X>

template fork_fwd source · line 2122 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-dup:(@_:X -> Two<X, X>) -> @-la:Layer<P1, X, Y1, S1> -> @-lb:Layer<P2, X, Y2, S2> -> @p:Two<P1, P2> -> @x:X -> Fwd<Two<P1, P2>, Two<Y1, Y2>, Two<S1, S2>>

template Fork source · line 2127 · raw

@-P1:Type -> @-P2:Type -> @-X:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-dup:(@_:X -> Two<X, X>) -> @-add:(@_:X -> @_:X -> X) -> @-la:Layer<P1, X, Y1, S1> -> @-lb:Layer<P2, X, Y2, S2> -> Layer<Two<P1, P2>, X, Two<Y1, Y2>, Two<S1, S2>>

template both_b2 source · line 2138 · raw

@-P1:Type -> @-X1:Type -> @-Y1:Type -> @-S1:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-P2:Type -> @-X2:Type -> @p1:P1 -> @s1:S1 -> @d1:Y1 -> @g1:P1 -> @r:Bwd<P2, X2> -> Bwd<Two<P1, P2>, Two<X1, X2>>

template both_bwd source · line 2143 · raw

@-P1:Type -> @-P2:Type -> @-X1:Type -> @-X2:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @p:Two<P1, P2> -> @s:Two<S1, S2> -> @dy:Two<Y1, Y2> -> @g:Two<P1, P2> -> Bwd<Two<P1, P2>, Two<X1, X2>>

template both_fwd source · line 2148 · raw

@-P1:Type -> @-P2:Type -> @-X1:Type -> @-X2:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> @p:Two<P1, P2> -> @x:Two<X1, X2> -> Fwd<Two<P1, P2>, Two<Y1, Y2>, Two<S1, S2>>

template Both source · line 2153 · raw

@-P1:Type -> @-P2:Type -> @-X1:Type -> @-X2:Type -> @-Y1:Type -> @-Y2:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P1, X1, Y1, S1> -> @-lb:Layer<P2, X2, Y2, S2> -> Layer<Two<P1, P2>, Two<X1, X2>, Two<Y1, Y2>, Two<S1, S2>>

template tie_f3 source · line 2169 · raw

@-P:Type -> @-Z:Type -> @-W:Type -> @-S3:Type -> @-lc:Layer<P, Z, W, S3> -> @-Q:Type -> @-S1:Type -> @-S2:Type -> @p:P -> @s1:S1 -> @r:Fwd<Q, Z, S2> -> Fwd<Two<P, Q>, W, Three<S1, S2, S3>>

template tie_f2 source · line 2174 · raw

@-P:Type -> @-Q:Type -> @-Y:Type -> @-Z:Type -> @-W:Type -> @-S2:Type -> @-S3:Type -> @-lb:Layer<Q, Y, Z, S2> -> @-lc:Layer<P, Z, W, S3> -> @-S1:Type -> @q:Q -> @r:Fwd<P, Y, S1> -> Fwd<Two<P, Q>, W, Three<S1, S2, S3>>

template tie_fwd source · line 2179 · raw

@-P:Type -> @-Q:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-W:Type -> @-S1:Type -> @-S2:Type -> @-S3:Type -> @-la:Layer<P, X, Y, S1> -> @-lb:Layer<Q, Y, Z, S2> -> @-lc:Layer<P, Z, W, S3> -> @pq:Two<P, Q> -> @x:X -> Fwd<Two<P, Q>, W, Three<S1, S2, S3>>

template tie_b3 source · line 2189 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S1:Type -> @-la:Layer<P, X, Y, S1> -> @-Q:Type -> @p:P -> @s1:S1 -> @gp:P -> @r:Bwd<Q, Y> -> Bwd<Two<P, Q>, X>

template tie_b2 source · line 2194 · raw

@-P:Type -> @-Q:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-S1:Type -> @-S2:Type -> @-la:Layer<P, X, Y, S1> -> @-lb:Layer<Q, Y, Z, S2> -> @q:Q -> @s1:S1 -> @s2:S2 -> @gq:Q -> @r:Bwd<P, Z> -> Bwd<Two<P, Q>, X>

template tie_bwd source · line 2199 · raw

@-P:Type -> @-Q:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-W:Type -> @-S1:Type -> @-S2:Type -> @-S3:Type -> @-la:Layer<P, X, Y, S1> -> @-lb:Layer<Q, Y, Z, S2> -> @-lc:Layer<P, Z, W, S3> -> @pq:Two<P, Q> -> @s:Three<S1, S2, S3> -> @dw:W -> @g:Two<P, Q> -> Bwd<Two<P, Q>, X>

template Tie source · line 2206 · raw

@-P:Type -> @-Q:Type -> @-X:Type -> @-Y:Type -> @-Z:Type -> @-W:Type -> @-S1:Type -> @-S2:Type -> @-S3:Type -> @-la:Layer<P, X, Y, S1> -> @-lb:Layer<Q, Y, Z, S2> -> @-lc:Layer<P, Z, W, S3> -> Layer<Two<P, Q>, X, W, Three<S1, S2, S3>>

The shared parameter steps once (through first's step; last's must be the same kind of layer for P, e.g. Embedding and LinearNB over the same v x d matrix), the middle with its own.

template ResidualOf source · line 2253 · raw

@-X:Type -> @-P:Type -> @-S:Type -> @-dup:(@_:X -> Two<X, X>) -> @-add:(@_:X -> @_:X -> X) -> @-join:Layer<Unit, Two<X, X>, X, Unit> -> @-f:Layer<P, X, X, S> -> Layer<Two<Two<Unit, P>, Unit>, X, X, Two<Two<Unit, S>, Unit>>

over any X with a copy (dup), an addition (add) and a layer that adds a pair (join): Seq(Fork(Id, f), join). Residual is this over matrices; the law residual runs it over the exact model of torch/spec.bend

template Residual source · line 2256 · raw

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

template Train.dump source · line 3506 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @tr:Train<P> -> TrainPL<P>

every number of a training state: the parameters, then the first moments, the second moments, b1^t and b2^t (packed moments: their tree's cells, pt_store's layout, in place of the two moments)

template tr_m1 source · line 3534 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @mm:P -> @g:P -> @r:PA<P> -> Train<P>

template tr2 source · line 3550 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @p:P -> @vv:P -> @g:P -> @r:PA<P> -> Train<P>

template tr1 source · line 3555 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @mm:P -> @vv:P -> @g:P -> @r:PA<P> -> Train<P>

template Train.restore source · line 3578 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @tr:Train<P> -> @xs:Array<F32> -> Train<P>

a training state filled from the numbers Train.dump wrote (its buffers are overwritten in place; packed moments are built again from the tree's cells)

template Params.save source · line 3649 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @p:P -> IO(P)

every parameter of p, in dump order, into a file at path (Layer.count numbers), through one array

template tsa_v source · line 3671 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @p:P -> @vv:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

template tsa_mm source · line 3675 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @mm:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

template tsa_mv source · line 3679 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @mm:P -> @vv:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @r:PA<P> -> IO(Train<P>)

template tsp source · line 3692 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @p:P -> @g:P -> @+p1:F32 -> @+p2:F32 -> @+l:F32 -> @rt:PTU -> IO(Train<P>)

template Train.save source · line 3698 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+path:String -> @tr:Train<P> -> IO(Train<P>)

the layout Train.dump gives, written from one array instead of a list (the same bytes)