~/bend-docscommunity

main.bend checks

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

@-Y:Type -> Type

type Train source · line 895 · 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 1225 · raw

Data

type D2 source · line 1296 · raw

Type

type A2 source · line 1299 · raw

Type

type AS source · line 1315 · raw

Type

type PC source · line 1344 · raw

Type

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

type R3 source · line 1573 · raw

Type

type R5 source · line 1613 · 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 1616 · raw

Type

type LN source · line 1684 · raw

@-d:Nat -> Type

type ASM source · line 1695 · raw

Type

type R4 source · line 1712 · raw

Type

type R3SS source · line 1751 · 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 1767 · raw

Type

type Three source · line 2028 · raw

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

type CS source · line 2129 · raw

Type

type RS3 source · line 2146 · raw

Type

type AttnS source · line 2211 · raw

Type

type DR source · line 2227 · raw

Type

type QK source · line 2249 · raw

Type

type PW source · line 2252 · raw

Type

type PR source · line 2263 · raw

Type

type PO source · line 2306 · raw

Type

type FS source · line 2358 · raw

Type

type Dm source · line 2361 · raw

Data

type HC source · line 2366 · 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 2411 · raw

Type

type GW source · line 2427 · raw

Type

type GN source · line 2430 · raw

Type

type GR source · line 2441 · raw

Type

type BG source · line 2516 · raw

Type

type Halves source · line 2540 · raw

@-A:Type -> Type

type FL source · line 2557 · raw

Type

type BL source · line 2613 · raw

Type

type DB source · line 2648 · raw

Type

type D3 source · line 2672 · raw

Type

type AttnDims source · line 2684 · raw

Data

type CSM source · line 2815 · 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 2900 · raw

Type

type NS source · line 2923 · raw

Type

type PTU source · line 2986 · raw

Type

type PTA source · line 3001 · raw

Type

type UA source · line 3004 · raw

Type

type PK source · line 3074 · raw

Data

what a tag says

type TrainPL source · line 3116 · 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:0x0f6479f09ada7ae614e64e31e8f5a730/main.P2 -> 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @b:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/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) -> 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @g:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @st:OptState<0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.ML<r, c> -> PL<0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> PL<0x0f6479f09ada7ae614e64e31e8f5a730/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> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.P2

def mat_restore_fin source · line 559 · raw

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

def Mat.restore source · line 563 · raw

@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @xs:Array<F32> -> @+off:U32 -> PA<0x0f6479f09ada7ae614e64e31e8f5a730/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> -> 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.P2 -> PA<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>>

def Mat.store source · line 579 · raw

@+r:Nat -> @+c:Nat -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @xs:Array<F32> -> @+off:U32 -> PA<0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i> -> @x:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i> -> @r:0x0f6479f09ada7ae614e64e31e8f5a730/main.MBias<n, o> -> Fwd<Lin<i, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i>>

def lin_f2 source · line 610 · raw

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

def lin_fwd source · line 615 · raw

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

def lin_b4 source · line 620 · raw

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

def lin_b3 source · line 625 · raw

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

def lin_b2 source · line 630 · raw

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

def lin_bwd source · line 637 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> @p:Lin<i, o> -> @x:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i> -> @dy:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, o> -> @g:Lin<i, o> -> Bwd<Lin<i, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/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<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o>>

def lin_unpair source · line 651 · raw

@-i:Nat -> @-o:Nat -> @p:Two<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/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<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i>>, OptState<0x0f6479f09ada7ae614e64e31e8f5a730/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<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/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<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o>>> -> PA<Lin<i, o>>

def lin_load2 source · line 682 · raw

@+i:Nat -> @+o:Nat -> @b:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o> -> @r:PA<0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o> -> @r:PA<0x0f6479f09ada7ae614e64e31e8f5a730/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>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<o, i> -> @b:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o> -> @gb:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<1n, o> -> @r:0x0f6479f09ada7ae614e64e31e8f5a730/main.MMulTN<o, n, i> -> Bwd<Lin<i, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i> -> @dy:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, o> -> @g:Lin<i, o> -> Bwd<Lin<i, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i>>

def LinearFirst source · line 720 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> Layer<Lin<i, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, i>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, o>, 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.MM2<n, c> -> Fwd<Unit, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.MPair<n, c> -> Bwd<Unit, 0x0f6479f09ada7ae614e64e31e8f5a730/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, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/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 842 · 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 846 · 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 850 · 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 854 · 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 858 · 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 862 · 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 866 · 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 870 · 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 874 · 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 878 · 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 906 · 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 917 · raw

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

def train_at_b source · line 946 · 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 953 · 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 957 · 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 961 · 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 971 · raw

@u:U32 -> F32

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

def bits source · line 976 · raw

@x:F32 -> U32

def f32_le source · line 981 · raw

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

def f32s source · line 984 · raw

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

def rd_done source · line 991 · raw

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

def read_at source · line 1000 · raw

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

n bytes from offset off

def mat_or_die source · line 1006 · raw

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

def Mat.load source · line 1014 · raw

@+r:Nat -> @+c:Nat -> @+path:String -> @+off:U32 -> IO(0x0f6479f09ada7ae614e64e31e8f5a730/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 1019 · raw

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

def ml_words source · line 1026 · raw

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

def Mat.print source · line 1032 · raw

@+r:Nat -> @+c:Nat -> @+name:String -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/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 1035 · raw

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

def zeros_a source · line 1039 · raw

@+n:Nat -> Array<F32>

n zeros, in an array with room for them

def sigmoid_v source · line 1055 · raw

@+x:F32 -> F32

def sigmoid_d source · line 1059 · raw

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

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

def Sigmoid source · line 1062 · raw

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

def tanh_v source · line 1065 · raw

@+x:F32 -> F32

def tanh_d source · line 1068 · raw

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

def Tanh source · line 1071 · raw

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

def silu_v source · line 1076 · 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 1079 · raw

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

def SiLU source · line 1083 · raw

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

def gelu_inner source · line 1088 · 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 1091 · raw

@+x:F32 -> F32

def gelu_d source · line 1094 · raw

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

def GELU source · line 1101 · raw

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

def erf_hp source · line 1123 · 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 1126 · raw

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

def erf_small source · line 1129 · raw

@+x:F32 -> F32

def erf_p0 source · line 1134 · raw

@+t:F32 -> F32

[0.84375, 1.25): t = a - 1.0

def erf_p1 source · line 1138 · raw

@+t:F32 -> F32

[1.25, 1.75): t = a - 1.5

def erf_p2 source · line 1142 · raw

@+t:F32 -> F32

[1.75, 2.25): t = a - 2.0

def erf_p3 source · line 1146 · raw

@+t:F32 -> F32

[2.25, 2.75): t = a - 2.5

def erf_p4 source · line 1150 · raw

@+t:F32 -> F32

[2.75, 3.25): t = a - 3.0

def erf_p5 source · line 1154 · raw

@+t:F32 -> F32

[3.25, 3.92): t = a - 3.5

def erf_sign source · line 1157 · raw

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

def erf_i0 source · line 1162 · 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 1167 · raw

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

def erf_i2 source · line 1172 · raw

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

def erf_i3 source · line 1177 · raw

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

def erf_i4 source · line 1182 · raw

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

def erf_i5 source · line 1187 · raw

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

def erf_top source · line 1192 · raw

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

def erf source · line 1197 · raw

@+x:F32 -> F32

def gelu_erf_cdf source · line 1206 · 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 1209 · raw

@+x:F32 -> F32

def gelu_erf_d source · line 1212 · raw

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

def GELUErf source · line 1216 · raw

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

def mix32 source · line 1229 · raw

@+x:U32 -> U32

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

def drop_k source · line 1237 · 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 1240 · raw

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

def drop_mask source · line 1247 · raw

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

def drop_f2 source · line 1250 · raw

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

def drop_fwd source · line 1255 · raw

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

def drop_b2 source · line 1260 · raw

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

def drop_step source · line 1265 · raw

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

def drop_dump source · line 1270 · raw

@d:DropP -> PL<DropP>

def drop_load_fin source · line 1275 · raw

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

def drop_load source · line 1279 · raw

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

def drop_store source · line 1283 · raw

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

the counter, bitcast to one F32 as dump writes it

def Dropout source · line 1287 · raw

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

def sdot source · line 1304 · 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 1311 · raw

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

def pmax source · line 1319 · 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 1326 · raw

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

def pexp source · line 1334 · 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 1347 · raw

@r:PC -> Array<F32>

def pdiv source · line 1353 · 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 1360 · raw

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

def psm2 source · line 1365 · raw

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

def psmc source · line 1372 · 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 1375 · raw

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

def copy_n source · line 1379 · 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 1391 · 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 1399 · raw

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

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

def sm_fwd source · line 1404 · raw

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

def smb_fix source · line 1410 · 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 1417 · raw

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

def smb_rows source · line 1423 · 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 1430 · raw

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

def sm_bwd source · line 1435 · raw

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

def Softmax source · line 1441 · raw

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

softmax over each row; saves its output

def lsum source · line 1447 · 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 1455 · 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 1462 · raw

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

def lsm_r2 source · line 1467 · raw

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

def lsm_row source · line 1472 · raw

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

def lsmf_rows source · line 1475 · raw

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

def lsm_fwd source · line 1482 · raw

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

def asum source · line 1488 · 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 1496 · 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 1503 · raw

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

def lsmb_rows source · line 1509 · 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 1516 · raw

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

def LogSoftmax source · line 1521 · raw

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

def mat_step_fin source · line 1527 · raw

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

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

def mat_step source · line 1531 · raw

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

def g_add_fin source · line 1534 · raw

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

def Mat.sum2 source · line 1540 · raw

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

a + b, dropping b

def dup_fin source · line 1543 · raw

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

def Mat.dup source · line 1549 · raw

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

two copies of a matrix

def ml_list source · line 1552 · raw

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

def ssq source · line 1562 · 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 1570 · raw

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

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

def rms_w source · line 1577 · 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 1584 · raw

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

def rmsf_row source · line 1589 · raw

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

def rmsf_rows source · line 1595 · 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 1602 · raw

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

def rms_fwd source · line 1607 · raw

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

def rmsb_dot source · line 1620 · 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 1627 · raw

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

def rmsb_w source · line 1633 · 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 1642 · raw

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

def rmsb_row2 source · line 1647 · 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 1653 · raw

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

def rmsb_rows source · line 1658 · raw

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

def gain_bwd_fin source · line 1665 · raw

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

def rms_b2 source · line 1668 · raw

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

def rms_bwd source · line 1673 · raw

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

def RMSNorm source · line 1679 · raw

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

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

def csq source · line 1688 · 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 1698 · raw

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

def ln_mr1 source · line 1703 · raw

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

def ln_mr source · line 1709 · 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 1716 · 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 1723 · raw

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

def lnf_row source · line 1728 · raw

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

def lnf_rows source · line 1733 · raw

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

def ln_f2 source · line 1740 · raw

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

def ln_fwd source · line 1745 · raw

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

def lnb_sums source · line 1755 · 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 1762 · raw

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

def lnb_w source · line 1771 · 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 1780 · raw

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

def lnb_row2 source · line 1785 · 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 1790 · raw

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

def lnb_rows source · line 1795 · raw

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

def ln_b2 source · line 1802 · raw

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

def ln_bwd source · line 1807 · raw

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

def ln_pair source · line 1812 · raw

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

def ln_unpair source · line 1816 · raw

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

def ln_s2 source · line 1820 · raw

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

def ln_step2 source · line 1825 · raw

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

def ln_step source · line 1830 · raw

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

def ln_dump2 source · line 1833 · raw

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

def ln_dump source · line 1838 · raw

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

def ln_load3 source · line 1843 · raw

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

def ln_load2 source · line 1847 · raw

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

def ln_load source · line 1852 · raw

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

def ln_store2 source · line 1856 · raw

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

def ln_store source · line 1861 · raw

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

def LayerNorm source · line 1865 · raw

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

def emb_cp source · line 1873 · 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 1880 · raw

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

def emb_f source · line 1885 · raw

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

def emb_add source · line 1891 · 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 1899 · 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 1906 · raw

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

def emb_bwd source · line 1911 · raw

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

def Embedding source · line 1917 · raw

@+n:Nat -> @+v:Nat -> @+d:Nat -> Layer<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<v, d>, List<&2, Nat>, 0x0f6479f09ada7ae614e64e31e8f5a730/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 1924 · raw

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

def lnb_b3 source · line 1929 · raw

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

def lnb_b2 source · line 1934 · raw

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

def LinearNB source · line 1939 · raw

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

def pair_f4 source · line 1958 · 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 2001 · 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 2031 · 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 2051 · 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 2078 · raw

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

def Add2 source · line 2083 · raw

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

def mul2_f3 source · line 2086 · raw

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

def mul2_f2 source · line 2091 · raw

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

def mul2_f source · line 2096 · raw

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

def mpair_a source · line 2101 · raw

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

def mul2_b2 source · line 2107 · raw

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

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

def Mul2 source · line 2112 · raw

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

def rope_ang source · line 2133 · 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 2137 · 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 2151 · 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 2158 · 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 2164 · 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 2173 · 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 2180 · raw

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

def rope_go2 source · line 2185 · raw

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

def rope_go source · line 2190 · raw

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

def RoPE source · line 2196 · 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, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, w>, 0x0f6479f09ada7ae614e64e31e8f5a730/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 2216 · 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 2220 · raw

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

xs reversed onto acc

def drow source · line 2232 · 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 2239 · raw

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

def qrow source · line 2246 · 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 2256 · 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 2266 · raw

@r:PR -> Nat

def pr_of source · line 2271 · raw

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

def prow3 source · line 2277 · 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 2282 · raw

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

def prow source · line 2290 · 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 2295 · 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 2303 · 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 pmul source · line 2311 · 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 2318 · raw

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

def pacc source · line 2325 · 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 2333 · 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 2336 · raw

@r:A2 -> Array<F32>

def rows_in source · line 2342 · 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 2345 · 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 2353 · 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 head_cols source · line 2369 · raw

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

def hc_kc source · line 2374 · raw

@+c:HC -> U32

def fs_fin source · line 2379 · raw

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

def head_f2 source · line 2384 · 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 2391 · 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 2396 · raw

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

def fs_o source · line 2403 · raw

@s:FS -> Array<F32>

def gfix source · line 2415 · 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 2422 · raw

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

def gw source · line 2434 · 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 2444 · raw

@r:GR -> Nat

def gr3 source · line 2450 · 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 2455 · raw

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

def grow source · line 2464 · 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 2468 · 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 2475 · raw

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

def hb4 source · line 2481 · 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 2487 · 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 2492 · 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 2498 · 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 2504 · 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 2509 · raw

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

def bg_fin source · line 2520 · 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 2525 · raw

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

def bgroups source · line 2530 · raw

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

def hcat source · line 2544 · 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 2550 · 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 fl3 source · line 2560 · raw

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

def fl2 source · line 2565 · raw

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

def fl1 source · line 2570 · raw

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

def fslice source · line 2575 · raw

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

def fslices source · line 2581 · raw

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

every sequence's rows of Q, K and V, newest first

def fleaf source · line 2588 · raw

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

def fpar source · line 2596 · 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 blocks_in source · line 2606 · raw

@xs:List<&1, Array<F32>> -> @+t:Nat -> @+c:Nat -> @+r0:Nat -> @o:Array<F32> -> Array<F32>

the blocks (t rows, c wide each) into o from row r0, in order

def bl4 source · line 2616 · raw

@+t:Nat -> @+w:Nat -> @+kvw: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

def bl3 source · line 2621 · raw

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

def bl2 source · line 2626 · raw

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

def bl1 source · line 2631 · raw

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

def bslice source · line 2636 · raw

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

def bslices source · line 2641 · raw

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

def db_of source · line 2651 · raw

@s:BG -> DB

def bleaf source · line 2656 · raw

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

def bpar source · line 2663 · raw

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

def dblocks_in source · line 2675 · raw

@xs:List<&1, DB> -> @+t:Nat -> @+w:Nat -> @+kvw:Nat -> @+r0:Nat -> @o:D3 -> D3

def dm_of source · line 2687 · raw

@+w:Nat -> @+kvw:Nat -> @+dm:AttnDims -> Dm

def attn_tree source · line 2698 · raw

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

whether the sequences run in the tree: from 8 sequences on, or from 2 when each sequence 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). 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 sequences in order, as one after another.

def fgo source · line 2701 · raw

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

def bgo source · line 2708 · raw

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

def af2 source · line 2715 · raw

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

def attn_fwd source · line 2720 · raw

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

def ab3 source · line 2725 · raw

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

def ab2 source · line 2730 · raw

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

def attn_bwd source · line 2735 · raw

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

def Attention source · line 2741 · 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<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, w>, Two<0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, kvw>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, kvw>>>, 0x0f6479f09ada7ae614e64e31e8f5a730/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 2752 · raw

@b:Bool -> F32

def ts_add source · line 2762 · 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 2767 · raw

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

def ts_pairs source · line 2772 · raw

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

def ts_put source · line 2779 · raw

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

def ts_carry source · line 2784 · raw

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

def ts_pass source · line 2791 · raw

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

def ts_first source · line 2794 · raw

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

def ts_go source · line 2799 · raw

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

def tree_sum source · line 2808 · raw

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

def cesm4 source · line 2818 · raw

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

def cesm3 source · line 2821 · raw

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

def cesm_e source · line 2826 · raw

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

def cesm2 source · line 2831 · raw

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

def ce_sm source · line 2836 · raw

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

def ce_g source · line 2840 · 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 2849 · 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 2854 · raw

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

def ce_bad source · line 2859 · raw

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

def ce_pick source · line 2865 · 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 2872 · raw

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

def ce_row source · line 2877 · raw

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

def ce_rows source · line 2882 · raw

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

def ce_fin source · line 2889 · raw

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

def CrossEntropy source · line 2894 · raw

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

def mse_l source · line 2904 · 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 2911 · raw

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

def MSE source · line 2918 · raw

@+n:Nat -> @+c:Nat -> @y:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c> -> @t:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c> -> LossOut<0x0f6479f09ada7ae614e64e31e8f5a730/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 2926 · raw

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

def nll_one source · line 2932 · 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 2940 · 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 2947 · raw

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

def NLL source · line 2953 · raw

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

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

def byte_of source · line 2964 · raw

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

def read_all source · line 2967 · raw

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

def ph_hw source · line 2978 · raw

@+n:U32 -> U32

def ph_bool source · line 2981 · raw

@b:Bool -> U32

def ptu_node source · line 2989 · raw

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

def pt_cells source · line 2994 · raw

@t:PTree -> PTU

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

def words_out source · line 3008 · 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 3014 · raw

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

def pta_then source · line 3020 · raw

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

def pta_node source · line 3024 · raw

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

def pts_v source · line 3028 · raw

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

def pts_m source · line 3032 · raw

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

def ptsv_v source · line 3036 · raw

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

def ptsv_m source · line 3040 · raw

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

def pt_store source · line 3045 · raw

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

the tree's cells into xs from off

def ptl_v source · line 3057 · 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 3061 · raw

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

def ptlv_v source · line 3065 · raw

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

def ptlv_m source · line 3069 · raw

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

def pt_kind2 source · line 3080 · raw

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

def pt_kind source · line 3087 · raw

@+tag:U32 -> PK

def pt_next source · line 3091 · 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 3097 · 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 3113 · raw

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

a tree from xs at off

def td0 source · line 3119 · raw

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

def td1 source · line 3123 · raw

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

def td3 source · line 3127 · 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 3133 · 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 3140 · 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 3145 · raw

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

def f32_at source · line 3163 · raw

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

def tr0 source · line 3172 · raw

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

def tr_m2 source · line 3176 · raw

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

def tr_adam2 source · line 3184 · 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 3188 · raw

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

def tr3 source · line 3192 · raw

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

def trp4 source · line 3206 · raw

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

def trp3 source · line 3210 · raw

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

def trp2 source · line 3214 · raw

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

def trp source · line 3218 · raw

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

def WCHUNK source · line 3239 · raw

U32

def chunk_bytes source · line 3243 · 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 3250 · 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 3257 · 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 3265 · 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 3273 · raw

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

def write_f32 source · line 3281 · 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 3287 · raw

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

def tsa_fin source · line 3301 · 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 3308 · 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 3312 · 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 3329 · raw

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

def tsp2 source · line 3334 · 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) -> 0x0f6479f09ada7ae614e64e31e8f5a730/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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>

template Ew.zip source · line 193 · raw

@-f:(@+a:F32 -> @+b:F32 -> F32) -> @+r:Nat -> @+c:Nat -> @a:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> @b:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/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, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<n, c>, 0x0f6479f09ada7ae614e64e31e8f5a730/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_dump source · line 812 · 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>>

dump and load for a pair of layers: the first layer's numbers, then the second's

template pair_load2 source · line 817 · 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 822 · 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 827 · 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 832 · 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 837 · 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 898 · raw

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

template Train.new source · line 913 · 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 922 · 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 929 · 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 934 · 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 940 · 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 1047 · raw

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

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

template ActIn source · line 1052 · raw

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

template Id source · line 1946 · raw

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

template pair_step2 source · line 1950 · 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 1955 · 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 1964 · 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 1971 · 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 1976 · 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 1981 · 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 1986 · 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 1991 · 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 1996 · 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 2006 · 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 2011 · 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 2016 · 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 2021 · 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 2036 · 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 2041 · 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 2046 · 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 2056 · 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 2061 · 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 2066 · 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 2073 · 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 Residual source · line 2117 · raw

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

template Train.dump source · line 3152 · 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 3180 · 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 3196 · 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 3201 · 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 3224 · 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 3295 · 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 3317 · 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 3321 · 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 3325 · 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 3338 · 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 3344 · 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)