~/bend-docscommunity

main.bend checks

raw source on the hub · import bendtorch@2.0.2.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.20.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 Guarded source · line 549 · raw

@-P:Type -> Type

The step's Opt and the state it runs on. A step runs only when its configuration is the state's kind (SGD on NoMom, momentum on OneMom, Adam or AdamW on TwoMom, AdamW with BF16 moments on Packed) and passes OptCfg.ok; any other step runs with NaN for every number of the state's own kind, so its parameters and every loss after it are NaN, as Train.new does with a configuration OptCfg.ok rejects. Without it a step of another kind fell to Opt.mat's last case and left the parameters unchanged, with no error (BendTorch #24). The laws guard_nomom .. guard_packed state it.

type Lin source · line 651 · raw

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

type LossOut source · line 943 · raw

@-Y:Type -> Type

type Train source · line 952 · raw

@-P:Type -> Type

Parameters, gradient (zero between steps), optimizer-specific buffers, the configuration the steps use (Train.new's, or Train.with_cfg's), Adam's 1 - b1^t and 1 - b2^t, last loss.

type DropP source · line 1290 · raw

Data

type D2 source · line 1361 · raw

Type

type A2 source · line 1364 · raw

Type

type AS source · line 1380 · raw

Type

type PC source · line 1409 · raw

Type

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

type R3 source · line 1638 · raw

Type

type R5 source · line 1678 · 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 1681 · raw

Type

type LN source · line 1749 · raw

@-d:Nat -> Type

type ASM source · line 1760 · raw

Type

type R4 source · line 1777 · raw

Type

type R3SS source · line 1816 · 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 1832 · raw

Type

type Three source · line 2219 · raw

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

type CS source · line 2326 · raw

Type

type RS3 source · line 2343 · raw

Type

type AttnS source · line 2408 · raw

Type

type DR source · line 2424 · raw

Type

type QK source · line 2446 · raw

Type

type PW source · line 2449 · raw

Type

type PR source · line 2460 · raw

Type

type PO source · line 2530 · raw

Type

type FS source · line 2598 · raw

Type

type Dm source · line 2601 · raw

Data

type HC source · line 2606 · 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 2651 · raw

Type

type GW source · line 2667 · raw

Type

type GN source · line 2670 · raw

Type

type GR source · line 2681 · raw

Type

type BG source · line 2756 · raw

Type

type Halves source · line 2780 · raw

@-A:Type -> Type

type FL source · line 2797 · raw

Type

type UO source · line 2855 · raw

Type

type BL source · line 2874 · raw

Type

type DB source · line 2918 · raw

Type

type D3 source · line 2942 · raw

Type

type UD source · line 2945 · raw

Type

type AttnDims source · line 2965 · raw

Data

type GL source · line 3196 · raw

Type

type CSM source · line 3391 · 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 3476 · raw

Type

type NS source · line 3499 · raw

Type

type PTU source · line 3562 · raw

Type

type PTA source · line 3578 · raw

Type

type UA source · line 3581 · raw

Type

type PK source · line 3651 · raw

Data

what a tag says

type TrainPL source · line 3693 · 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:0x5c5507d77be847e1e454ec33ab54e087/main.P2 -> 0x5c5507d77be847e1e454ec33ab54e087/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:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/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) -> 0x5c5507d77be847e1e454ec33ab54e087/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:0x5c5507d77be847e1e454ec33ab54e087/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:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> @st:OptState<0x5c5507d77be847e1e454ec33ab54e087/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. Every step refuses a configuration that fails (Opt.guard: 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 Guarded.opt source · line 552 · raw

@-P:Type -> @r:Guarded<P> -> Opt

def OptCfg.kind source · line 558 · raw

@+cfg:OptCfg -> U32

the kind of state a configuration's optimizer keeps: 0 none (SGD), 1 one buffer (momentum), 2 two (Adam, AdamW), 3 two packed in BF16 (AdamW with BF16 moments)

def nan source · line 566 · raw

F32

def nan_mom source · line 569 · raw

Opt

def nan_adam source · line 572 · raw

Opt

def Opt.when source · line 576 · raw

@ok:Bool -> @+cfg:OptCfg -> @+q1:F32 -> @+q2:F32 -> @+no:Opt -> Opt

the configuration's Opt when ok, the refused one when not

def Opt.guard source · line 581 · raw

@-P:Type -> @+cfg:OptCfg -> @+q1:F32 -> @+q2:F32 -> @st:OptState<P> -> Guarded<P>

def mat_dump_fin source · line 594 · raw

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

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

def Mat.dump source · line 599 · raw

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

def load_cells source · line 602 · raw

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

def mat_restore_fin source · line 608 · raw

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

def Mat.restore source · line 612 · raw

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

def store_cells source · line 618 · raw

@left:Nat -> @+i:U32 -> @+off:U32 -> @rm:Pair(Array<F32>, F32) -> @xs:Array<F32> -> 0x5c5507d77be847e1e454ec33ab54e087/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 624 · raw

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

def Mat.store source · line 628 · raw

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

def pa_second source · line 633 · raw

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

def pl_pair source · line 637 · raw

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

def pl_second source · line 642 · raw

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

def lin_f3 source · line 654 · raw

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

def lin_f2 source · line 659 · raw

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

def lin_fwd source · line 664 · raw

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

def lin_b4 source · line 669 · raw

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

def lin_b3 source · line 674 · raw

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

def lin_b2 source · line 679 · raw

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

def lin_bwd source · line 686 · raw

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

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

def lin_pair source · line 696 · raw

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

def lin_unpair source · line 700 · raw

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

def lin_s2 source · line 704 · raw

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

def lin_step2 source · line 709 · raw

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

def lin_step source · line 714 · 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 717 · raw

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

def lin_dump source · line 722 · raw

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

def lin_load3 source · line 727 · raw

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

def lin_load2 source · line 731 · raw

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

def lin_load source · line 736 · raw

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

def lin_store2 source · line 740 · raw

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

def lin_store source · line 745 · raw

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

def lin_zeros source · line 749 · raw

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

def Linear source · line 753 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @+par:Nat -> Layer<Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, i>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, o>, 0x5c5507d77be847e1e454ec33ab54e087/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 759 · raw

@+n:Nat -> @+i:Nat -> @+o:Nat -> @w:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<o, i> -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, o> -> @gb:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<1n, o> -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.MMulTN<o, n, i> -> Bwd<Lin<i, o>, 0x5c5507d77be847e1e454ec33ab54e087/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 764 · raw

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

def LinearFirst source · line 769 · raw

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

def pick source · line 776 · raw

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

def no_step source · line 783 · raw

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

def act_f2 source · line 788 · raw

@-n:Nat -> @-c:Nat -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.MM2<n, c> -> Fwd<Unit, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, c>, 0x5c5507d77be847e1e454ec33ab54e087/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 793 · raw

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

def relu_v source · line 803 · 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 806 · raw

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

def ReLU source · line 809 · raw

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

def seq_f3 source · line 816 · 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 831 · 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 846 · raw

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

def seq_at_f2 source · line 898 · 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 902 · 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 906 · 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 910 · 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 914 · 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 918 · 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 922 · 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 926 · 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 930 · 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 934 · 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 ts5 source · line 967 · raw

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

def train_at_g source · line 999 · raw

@-P:Type -> @sf:(@_:Opt -> @_:P -> @_:P -> @_:OptState<P> -> Step<P>) -> @+cfg:OptCfg -> @+l:F32 -> @+q1:F32 -> @+q2:F32 -> @p:P -> @g:P -> @r:Guarded<P> -> Train<P>

def train_at_b source · line 1004 · 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 1011 · 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 1015 · 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 1019 · raw

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

def Train.with_cfg source · line 1027 · raw

@-P:Type -> @+cfg:OptCfg -> @tr:Train<P> -> Train<P>

The same training state under another configuration from the next step on, for a schedule or a change mid-run. It must be of the state's kind and pass OptCfg.ok: otherwise the next step is refused (NaN, see Opt.guard), since the moments of another optimizer cannot be made up. 1 - b1^t and 1 - b2^t go on from where they are.

def bitcast source · line 1036 · raw

@u:U32 -> F32

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

def bits source · line 1041 · raw

@x:F32 -> U32

def f32_le source · line 1046 · raw

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

def f32s source · line 1049 · raw

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

def rd_done source · line 1056 · raw

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

def read_at source · line 1065 · raw

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

n bytes from offset off

def mat_or_die source · line 1071 · raw

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

def Mat.load source · line 1079 · raw

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

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

def ml_words source · line 1091 · raw

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

def Mat.print source · line 1097 · raw

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

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

def zeros_a source · line 1104 · raw

@+n:Nat -> Array<F32>

n zeros, in an array with room for them

def sigmoid_v source · line 1120 · raw

@+x:F32 -> F32

def sigmoid_d source · line 1124 · raw

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

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

def Sigmoid source · line 1127 · raw

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

def tanh_v source · line 1130 · raw

@+x:F32 -> F32

def tanh_d source · line 1133 · raw

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

def Tanh source · line 1136 · raw

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

def silu_v source · line 1141 · 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 1144 · raw

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

def SiLU source · line 1148 · raw

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

def gelu_inner source · line 1153 · 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 1156 · raw

@+x:F32 -> F32

def gelu_d source · line 1159 · raw

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

def GELU source · line 1166 · raw

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

def erf_hp source · line 1188 · 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 1191 · raw

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

def erf_small source · line 1194 · raw

@+x:F32 -> F32

def erf_p0 source · line 1199 · raw

@+t:F32 -> F32

[0.84375, 1.25): t = a - 1.0

def erf_p1 source · line 1203 · raw

@+t:F32 -> F32

[1.25, 1.75): t = a - 1.5

def erf_p2 source · line 1207 · raw

@+t:F32 -> F32

[1.75, 2.25): t = a - 2.0

def erf_p3 source · line 1211 · raw

@+t:F32 -> F32

[2.25, 2.75): t = a - 2.5

def erf_p4 source · line 1215 · raw

@+t:F32 -> F32

[2.75, 3.25): t = a - 3.0

def erf_p5 source · line 1219 · raw

@+t:F32 -> F32

[3.25, 3.92): t = a - 3.5

def erf_sign source · line 1222 · raw

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

def erf_i0 source · line 1227 · 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 1232 · raw

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

def erf_i2 source · line 1237 · raw

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

def erf_i3 source · line 1242 · raw

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

def erf_i4 source · line 1247 · raw

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

def erf_i5 source · line 1252 · raw

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

def erf_top source · line 1257 · raw

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

def erf source · line 1262 · raw

@+x:F32 -> F32

def gelu_erf_cdf source · line 1271 · 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 1274 · raw

@+x:F32 -> F32

def gelu_erf_d source · line 1277 · raw

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

def GELUErf source · line 1281 · raw

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

def mix32 source · line 1294 · raw

@+x:U32 -> U32

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

def drop_k source · line 1302 · 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 1305 · raw

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

def drop_mask source · line 1312 · raw

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

def drop_f2 source · line 1315 · raw

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

def drop_fwd source · line 1320 · raw

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

def drop_b2 source · line 1325 · raw

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

def drop_step source · line 1330 · raw

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

def drop_dump source · line 1335 · raw

@d:DropP -> PL<DropP>

def drop_load_fin source · line 1340 · raw

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

def drop_load source · line 1344 · raw

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

def drop_store source · line 1348 · raw

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

the counter, bitcast to one F32 as dump writes it

def Dropout source · line 1352 · raw

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

def sdot source · line 1369 · 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 1376 · raw

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

def pmax source · line 1384 · 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 1391 · raw

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

def pexp source · line 1399 · 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 1412 · raw

@r:PC -> Array<F32>

def pdiv source · line 1418 · 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 1425 · raw

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

def psm2 source · line 1430 · raw

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

def psmc source · line 1437 · 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 1440 · raw

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

def copy_n source · line 1444 · 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 1456 · 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 1464 · raw

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

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

def sm_fwd source · line 1469 · raw

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

def smb_fix source · line 1475 · 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 1482 · raw

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

def smb_rows source · line 1488 · 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 1495 · raw

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

def sm_bwd source · line 1500 · raw

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

def Softmax source · line 1506 · raw

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

softmax over each row; saves its output

def lsum source · line 1512 · 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 1520 · 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 1527 · raw

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

def lsm_r2 source · line 1532 · raw

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

def lsm_row source · line 1537 · raw

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

def lsmf_rows source · line 1540 · raw

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

def lsm_fwd source · line 1547 · raw

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

def asum source · line 1553 · 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 1561 · 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 1568 · raw

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

def lsmb_rows source · line 1574 · 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 1581 · raw

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

def LogSoftmax source · line 1586 · raw

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

def mat_step_fin source · line 1592 · raw

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

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

def mat_step source · line 1596 · raw

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

def g_add_fin source · line 1599 · raw

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

def Mat.sum2 source · line 1605 · raw

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

a + b, dropping b

def dup_fin source · line 1608 · raw

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

def Mat.dup source · line 1614 · raw

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

two copies of a matrix

def ml_list source · line 1617 · raw

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

def ssq source · line 1627 · 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 1635 · raw

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

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

def rms_w source · line 1642 · 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 1649 · raw

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

def rmsf_row source · line 1654 · raw

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

def rmsf_rows source · line 1660 · 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 1667 · raw

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

def rms_fwd source · line 1672 · raw

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

def rmsb_dot source · line 1685 · 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 1692 · raw

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

def rmsb_w source · line 1698 · 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 1707 · raw

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

def rmsb_row2 source · line 1712 · 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 1718 · raw

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

def rmsb_rows source · line 1723 · raw

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

def gain_bwd_fin source · line 1730 · raw

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

def rms_b2 source · line 1733 · raw

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

def rms_bwd source · line 1738 · raw

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

def RMSNorm source · line 1744 · raw

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

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

def csq source · line 1753 · 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 1763 · raw

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

def ln_mr1 source · line 1768 · raw

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

def ln_mr source · line 1774 · 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 1781 · 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 1788 · raw

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

def lnf_row source · line 1793 · raw

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

def lnf_rows source · line 1798 · raw

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

def ln_f2 source · line 1805 · raw

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

def ln_fwd source · line 1810 · raw

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

def lnb_sums source · line 1820 · 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 1827 · raw

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

def lnb_w source · line 1836 · 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 1845 · raw

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

def lnb_row2 source · line 1850 · 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 1855 · raw

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

def lnb_rows source · line 1860 · raw

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

def ln_b2 source · line 1867 · raw

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

def ln_bwd source · line 1872 · raw

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

def ln_pair source · line 1877 · raw

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

def ln_unpair source · line 1881 · raw

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

def ln_s2 source · line 1885 · raw

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

def ln_step2 source · line 1890 · raw

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

def ln_step source · line 1895 · raw

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

def ln_dump2 source · line 1898 · raw

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

def ln_dump source · line 1903 · raw

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

def ln_load3 source · line 1908 · raw

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

def ln_load2 source · line 1912 · raw

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

def ln_load source · line 1917 · raw

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

def ln_store2 source · line 1921 · raw

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

def ln_store source · line 1926 · raw

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

def LayerNorm source · line 1930 · raw

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

def gn_sum source · line 1941 · raw

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

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

def gn_csq source · line 1949 · raw

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

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

def gn_mr1 source · line 1956 · raw

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

def gn_mr source · line 1962 · raw

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

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

def gnf_rows source · line 1966 · raw

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

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

def gnf_grp2 source · line 1973 · raw

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

def gnf_grp source · line 1979 · raw

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

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

def gnf_groups source · line 1984 · raw

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

def gnf_images source · line 1991 · raw

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

def gn_fwd source · line 1998 · raw

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

def gnb_sums source · line 2005 · raw

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

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

def gnb_rows source · line 2012 · raw

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

def gnb_grp3 source · line 2019 · raw

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

def gnb_grp2 source · line 2024 · raw

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

def gnb_grp source · line 2029 · raw

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

def gnb_groups source · line 2034 · raw

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

def gnb_images source · line 2041 · raw

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

def gn_bwd source · line 2048 · raw

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

def GroupNorm source · line 2054 · raw

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

GroupNorm over n images of hw pixels and ng groups of cg channels, channels last; its parameters are LayerNorm's

def emb_cp source · line 2062 · 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 2069 · raw

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

def emb_f source · line 2074 · raw

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

def emb_add source · line 2080 · 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 2088 · 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 2095 · raw

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

def emb_bwd source · line 2100 · raw

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

def Embedding source · line 2106 · raw

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

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

def lnb_f2 source · line 2113 · raw

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

def lnb_b3 source · line 2118 · raw

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

def lnb_b2 source · line 2123 · raw

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

def LinearNB source · line 2128 · raw

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

def pair_f4 source · line 2147 · 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 2191 · 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 2222 · 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 2242 · 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 2270 · raw

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

def Add2 source · line 2275 · raw

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

def mul2_f3 source · line 2278 · raw

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

def mul2_f2 source · line 2283 · raw

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

def mul2_f source · line 2288 · raw

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

def mpair_a source · line 2293 · raw

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

def mul2_b2 source · line 2299 · raw

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

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

def Mul2 source · line 2304 · raw

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

def rope_ang source · line 2330 · 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 2334 · 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 2348 · 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 2355 · 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 2361 · 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 2370 · 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 2377 · raw

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

def rope_go2 source · line 2382 · raw

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

def rope_go source · line 2387 · raw

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

def RoPE source · line 2393 · 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, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, 0x5c5507d77be847e1e454ec33ab54e087/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 2413 · 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 2417 · raw

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

xs reversed onto acc

def drow source · line 2429 · 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 2436 · raw

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

def qrow source · line 2443 · 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 2453 · 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 2463 · raw

@r:PR -> Nat

def pr_of source · line 2468 · raw

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

def prow3 source · line 2474 · 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 2479 · raw

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

def prow source · line 2487 · 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 2492 · 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 2500 · raw

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

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

def frow source · line 2509 · raw

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

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

def fprow2 source · line 2512 · raw

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

def fprow source · line 2517 · raw

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

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

def fprows source · line 2520 · raw

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

def fprobs source · line 2527 · raw

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

def pmul source · line 2535 · 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 2542 · raw

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

def pacc source · line 2549 · 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 2557 · 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 2560 · raw

@r:A2 -> Array<F32>

def rows_in source · line 2566 · 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 2569 · 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 2577 · raw

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

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

def pick_copy source · line 2581 · raw

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

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

def block_out source · line 2589 · raw

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

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

def block_in source · line 2593 · raw

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

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

def head_cols source · line 2609 · raw

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

def hc_kc source · line 2614 · raw

@+c:HC -> U32

def fs_fin source · line 2619 · raw

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

def head_f2 source · line 2624 · 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 2631 · 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 2636 · raw

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

def fs_o source · line 2643 · raw

@s:FS -> Array<F32>

def gfix source · line 2655 · 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 2662 · raw

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

def gw source · line 2674 · 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 2684 · raw

@r:GR -> Nat

def gr3 source · line 2690 · 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 2695 · raw

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

def grow source · line 2704 · 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 2708 · 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 2715 · raw

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

def hb4 source · line 2721 · 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 2727 · 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 2732 · 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 2738 · 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 2744 · 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 2749 · raw

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

def bg_fin source · line 2760 · 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 2765 · raw

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

def bgroups source · line 2770 · raw

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

def hcat source · line 2784 · 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 2790 · raw

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

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

def fu3 source · line 2803 · raw

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

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

def fu2 source · line 2808 · raw

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

def fu1 source · line 2813 · raw

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

def funit source · line 2818 · raw

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

def fgroups source · line 2823 · raw

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

def fslices source · line 2831 · raw

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

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

def fleaf source · line 2838 · raw

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

def fpar source · line 2846 · raw

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

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

def ub_row source · line 2859 · raw

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

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

def blocks_in source · line 2867 · raw

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

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

def bu4 source · line 2879 · raw

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

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

def bu3 source · line 2884 · raw

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

def bu2 source · line 2889 · raw

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

def bu1 source · line 2894 · raw

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

def bunit source · line 2899 · raw

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

def bgroups_u source · line 2904 · raw

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

def bslices source · line 2911 · raw

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

def db_of source · line 2921 · raw

@s:BG -> DB

def bleaf source · line 2926 · raw

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

def bpar source · line 2933 · raw

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

def ud_row source · line 2949 · raw

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

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

def dblocks_in source · line 2956 · raw

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

def dm_of source · line 2969 · raw

@+dm:AttnDims -> Dm

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

def attn_tree source · line 2981 · raw

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

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

def fgo source · line 2984 · raw

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

def bgo source · line 2991 · raw

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

def af2 source · line 2998 · raw

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

def attn_fwd source · line 3003 · raw

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

def head_full source · line 3012 · raw

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

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

def fheads_full source · line 3017 · raw

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

def fleaf_full source · line 3024 · raw

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

def fpar_full source · line 3031 · raw

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

def fgo_full source · line 3040 · raw

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

def af2_full source · line 3047 · raw

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

def attn_fwd_full source · line 3052 · raw

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

def ab3 source · line 3057 · raw

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

def ab2 source · line 3062 · raw

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

def attn_bwd source · line 3067 · raw

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

def fgrow source · line 3074 · raw

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

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

def fgrows source · line 3077 · raw

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

def fhb1 source · line 3084 · raw

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

def head_bwd_full source · line 3089 · raw

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

def bheads_full source · line 3094 · raw

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

def bgroup_full source · line 3101 · raw

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

def bgroups_full source · line 3106 · raw

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

def bleaf_full source · line 3113 · raw

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

def bpar_full source · line 3120 · raw

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

def bgo_full source · line 3129 · raw

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

def ab2_full source · line 3136 · raw

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

def attn_bwd_full source · line 3141 · raw

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

def FullAttention source · line 3148 · 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<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, AttnS>

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

def Attention source · line 3152 · 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<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, w>, Two<0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<n, kvw>>>, 0x5c5507d77be847e1e454ec33ab54e087/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 ocount source · line 3160 · raw

@+h:Nat -> @+p:Nat -> @+k:Nat -> @+s:Nat -> Nat

the output size: floor division, no divisibility condition (the law out_count says so)

def p_ch source · line 3165 · raw

@+i:Nat -> @+k:Nat -> Nat

the gathered matrix is one flat list of n·oh·ow·c·k·k numbers in raster order; cell i of position q decomposes as (channel, ky, kx) and the position as (batch, oy, ox), channel-major inside the patch as PyTorch's unfold

def p_ky source · line 3168 · raw

@+i:Nat -> @+k:Nat -> Nat

def p_kx source · line 3171 · raw

@+i:Nat -> @+k:Nat -> Nat

def p_iy source · line 3174 · raw

@+i:Nat -> @+oy:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> Nat

def p_ix source · line 3177 · raw

@+i:Nat -> @+ox:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> Nat

def p_ok source · line 3180 · raw

@+i:Nat -> @+oy:Nat -> @+ox:Nat -> @+h:Nat -> @+w:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> Bool

def p_src source · line 3185 · raw

@+i:Nat -> @+b:Nat -> @+oy:Nat -> @+ox:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> U32

def p_rc source · line 3189 · raw

@ok:Bool -> @d:Array<F32> -> @+src:U32 -> Pair(Array<F32>, F32)

the array and the cell's value: read when the window is inside, 0 when it is not, so no out-of-bounds cell is touched

def gather source · line 3200 · raw

@+left:Nat -> @+j:Nat -> @+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @+oh:Nat -> @+ow:Nat -> @+ckk:Nat -> @rc:Pair(Array<F32>, F32) -> @+acc:List<&2, F32> -> GL

one flat loop (read_l's shape): each step conses the current number and fetches the next, the array handed forward

def gl_out source · line 3219 · raw

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

def im2col_n source · line 3224 · raw

@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @d:Array<F32> -> List<&2, F32>

def add_pair source · line 3232 · raw

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

add v into acc[src], reading and writing the cell the same way (Array.get hands the array back)

def add_at source · line 3237 · raw

@+src:U32 -> @+v:F32 -> @acc:Array<F32> -> Array<F32>

def add_if source · line 3240 · raw

@ok:Bool -> @+src:U32 -> @+v:F32 -> @acc:Array<F32> -> Array<F32>

def c2_go source · line 3249 · raw

@+left:Nat -> @+j:Nat -> @+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @+oh:Nat -> @+ow:Nat -> @+ckk:Nat -> @+per:Nat -> @+total:Nat -> @dp:Pair(Array<F32>, F32) -> @acc:Array<F32> -> Array<F32>

col2im: the gradient of the gather. Every patch cell that read an image cell adds into it, in raster order of the patches and then of the cell inside the patch, which is the order a per-pixel sum on the device reproduces.

def col2im source · line 3266 · raw

@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @dp:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(ocount(h, p, k, s), ocount(w, p, k, s))), Nat.mul(c, Nat.mul(k, k))> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(h, w)), c>

def im2col source · line 3279 · raw

@+n:Nat -> @+h:Nat -> @+w:Nat -> @+c:Nat -> @+k:Nat -> @+p:Nat -> @+s:Nat -> @x:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(h, w)), c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, Nat.mul(ocount(h, p, k, s), ocount(w, p, k, s))), Nat.mul(c, Nat.mul(k, k))>

def cimg source · line 3297 · raw

@+k:Nat -> @+hs:Nat -> @+p:Nat -> Nat

the image's shape from the surplus: h = k + hs - 2p, and the output count, (h + 2p - k)/s + 1, which is hs/s + 1 once the surplus is the padded image's

def ckk source · line 3300 · raw

@+c:Nat -> @+k:Nat -> Nat

def chw source · line 3303 · raw

@+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> Nat

def coh source · line 3306 · raw

@+k:Nat -> @+hs:Nat -> @+p:Nat -> @+s1:Nat -> Nat

def cohw source · line 3309 · raw

@+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> Nat

def conv_b2 source · line 3312 · raw

@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @b:Bwd<Lin<ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, cohw(k, hs, ws, p, s1)), ckk(c, k)>> -> Bwd<Lin<ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, chw(k, hs, ws, p)), c>>

def Conv2d source · line 3317 · raw

@+n:Nat -> @+c:Nat -> @+co:Nat -> @+k:Nat -> @+hs:Nat -> @+ws:Nat -> @+p:Nat -> @+s1:Nat -> @+par:Nat -> Layer<Lin<ckk(c, k), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, chw(k, hs, ws, p)), c>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, cohw(k, hs, ws, p, s1)), co>, 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<Nat.mul(n, cohw(k, hs, ws, p, s1)), ckk(c, k)>>

def hit source · line 3328 · raw

@b:Bool -> F32

def ts_add source · line 3338 · 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 3343 · raw

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

def ts_pairs source · line 3348 · raw

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

def ts_put source · line 3355 · raw

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

def ts_carry source · line 3360 · raw

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

def ts_pass source · line 3367 · raw

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

def ts_first source · line 3370 · raw

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

def ts_go source · line 3375 · raw

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

def tree_sum source · line 3384 · raw

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

def cesm4 source · line 3394 · raw

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

def cesm3 source · line 3397 · raw

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

def cesm_e source · line 3402 · raw

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

def cesm2 source · line 3407 · raw

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

def ce_sm source · line 3412 · raw

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

def ce_g source · line 3416 · 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 3425 · 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 3430 · raw

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

def ce_bad source · line 3435 · raw

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

def ce_pick source · line 3441 · 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 3448 · raw

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

def ce_row source · line 3453 · raw

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

def ce_rows source · line 3458 · raw

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

def ce_fin source · line 3465 · raw

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

def CrossEntropy source · line 3470 · raw

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

def mse_l source · line 3480 · 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 3487 · raw

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

def MSE source · line 3494 · raw

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

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

def nll_one source · line 3508 · 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 3516 · 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 3523 · raw

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

def NLL source · line 3529 · raw

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

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

def byte_of source · line 3540 · raw

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

def read_all source · line 3543 · raw

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

def ph_hw source · line 3554 · raw

@+n:U32 -> U32

def ph_bool source · line 3557 · raw

@b:Bool -> U32

def ptu_node source · line 3565 · raw

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

def pt_cells source · line 3570 · raw

@t:PTree -> PTU

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

def words_out source · line 3585 · 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 3591 · raw

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

def pta_then source · line 3597 · raw

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

def pta_node source · line 3601 · raw

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

def pts_v source · line 3605 · raw

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

def pts_m source · line 3609 · raw

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

def ptsv_v source · line 3613 · raw

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

def ptsv_m source · line 3617 · raw

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

def pt_store source · line 3622 · raw

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

the tree's cells into xs from off

def ptl_v source · line 3634 · 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 3638 · raw

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

def ptlv_v source · line 3642 · raw

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

def ptlv_m source · line 3646 · raw

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

def pt_kind2 source · line 3657 · raw

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

def pt_kind source · line 3664 · raw

@+tag:U32 -> PK

def pt_next source · line 3668 · 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 3674 · 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 3690 · raw

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

a tree from xs at off

def td0 source · line 3696 · raw

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

def td1 source · line 3700 · raw

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

def td3 source · line 3704 · raw

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

def cells_l source · line 3710 · 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 3717 · raw

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

def tdp source · line 3722 · raw

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

def f32_at source · line 3741 · raw

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

def tr0 source · line 3750 · raw

@-P:Type -> @+cfg:OptCfg -> @g:P -> @r:PA<P> -> Train<P>

def tr_m2 source · line 3754 · raw

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

def tr_adam2 source · line 3762 · raw

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

def tr_adam source · line 3766 · raw

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

def tr3 source · line 3770 · raw

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

def trp4 source · line 3784 · raw

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

def trp3 source · line 3788 · raw

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

def trp2 source · line 3792 · raw

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

def trp source · line 3796 · raw

@-P:Type -> @+cfg:OptCfg -> @g:P -> @r:PA<P> -> Train<P>

def WCHUNK source · line 3817 · raw

U32

def chunk_bytes source · line 3821 · 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 3828 · 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 3835 · 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 3843 · 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 3851 · raw

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

def write_f32 source · line 3859 · 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 3865 · raw

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

def tsa_fin source · line 3879 · raw

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

def tsa_m source · line 3886 · raw

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

def tsa_pw source · line 3890 · raw

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

def tsp3 source · line 3907 · raw

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

def tsp2 source · line 3912 · raw

@-P:Type -> @+path:String -> @g:P -> @+cfg:OptCfg -> @+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) -> 0x5c5507d77be847e1e454ec33ab54e087/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:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>

template Ew.zip source · line 193 · raw

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

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

template ActOut source · line 798 · raw

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

template seq_f2 source · line 821 · 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 826 · 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 836 · 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 841 · 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 849 · 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 852 · 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 857 · raw

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

template pair_zeros source · line 863 · raw

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

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

template pair_dump source · line 867 · raw

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

template pair_load2 source · line 872 · 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 877 · 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 882 · 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 887 · 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 892 · 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 955 · raw

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

template Train.new source · line 964 · raw

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

1 - b^t starts at 0. A configuration OptCfg.ok rejects is kept as given: every step refuses it (Opt.guard), so every loss after the first is NaN, as with one given later through Train.with_cfg.

template ts4g source · line 972 · raw

@-P:Type -> @-X:Type -> @-Y:Type -> @-S:Type -> @-m:Layer<P, X, Y, S> -> @+cfg:OptCfg -> @+l:F32 -> @+q1:F32 -> @+q2:F32 -> @p:P -> @g:P -> @r:Guarded<P> -> Train<P>

template ts4 source · line 976 · 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 983 · 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 988 · 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 994 · raw

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

forward, loss, backward and one optimizer step under the record's configuration: the same code for every model

template acti_f3 source · line 1112 · raw

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

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

template ActIn source · line 1117 · raw

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

template Id source · line 2135 · raw

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

template pair_step2 source · line 2139 · 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 2144 · 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 2153 · 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 2160 · 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 2165 · 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 2170 · 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 2175 · 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 2180 · 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 2185 · 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 2196 · 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 2201 · 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 2206 · 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 2211 · 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 2227 · 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 2232 · 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 2237 · 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 2247 · 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 2252 · 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 2257 · 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 2264 · raw

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

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

template ResidualOf source · line 2311 · raw

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

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

template Residual source · line 2314 · raw

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

template Train.dump source · line 3730 · 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). Not its configuration: a checkpoint resumes under the configuration of the Train it is read into.

template tr_m1 source · line 3758 · raw

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

template tr2 source · line 3774 · raw

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

template tr1 source · line 3779 · raw

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

template Train.restore source · line 3802 · 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); its configuration stays the one tr was made with

template Params.save source · line 3873 · 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 3895 · raw

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

template tsa_mm source · line 3899 · raw

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

template tsa_mv source · line 3903 · raw

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

template tsp source · line 3916 · raw

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

template Train.save source · line 3922 · 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)