~/bend-docscommunity

main.bend checks

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

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

import ../torch/main.bend as T

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

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

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

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

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

3 imports
import Base
import bend-ml-tensor-array@0.1.14.0/main.bend as TA
import bend-ml-nat-lemmas@0.1.1.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 OptState source · line 51 · raw

@-P:Type -> Type

Owned optimizer buffers: SGD has none, momentum one, Adam/AdamW two.

type Step source · line 78 · raw

@-P:Type -> Type

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

type Opt source · line 84 · 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 90 · raw

@-P:Type -> Type

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

type PA source · line 95 · 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 98 · raw

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

type Q3 source · line 194 · raw

Type

type Q4 source · line 197 · raw

Type

type MatStep source · line 200 · raw

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

type OptCfg source · line 290 · 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).

type Lin source · line 429 · raw

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

type LossOut source · line 714 · raw

@-Y:Type -> Type

type Train source · line 722 · raw

@-P:Type -> Type

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

type DropP source · line 1065 · raw

Data

type D2 source · line 1136 · raw

Type

type A2 source · line 1139 · raw

Type

type AS source · line 1155 · raw

Type

type PC source · line 1184 · raw

Type

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

type R3 source · line 1413 · raw

Type

type R5 source · line 1453 · 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 1456 · raw

Type

type LN source · line 1524 · raw

@-d:Nat -> Type

type ASM source · line 1535 · raw

Type

type R4 source · line 1552 · raw

Type

type R3SS source · line 1591 · 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 1607 · raw

Type

type Three source · line 1868 · raw

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

type CS source · line 1969 · raw

Type

type RS3 source · line 1986 · raw

Type

type AttnS source · line 2051 · raw

Type

type DR source · line 2067 · raw

Type

type QK source · line 2089 · raw

Type

type PW source · line 2092 · raw

Type

type PR source · line 2103 · raw

Type

type PO source · line 2146 · raw

Type

type FS source · line 2198 · raw

Type

type Dm source · line 2201 · raw

Data

type HC source · line 2206 · 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 2251 · raw

Type

type GW source · line 2267 · raw

Type

type GN source · line 2270 · raw

Type

type GR source · line 2281 · raw

Type

type BG source · line 2356 · raw

Type

type Halves source · line 2380 · raw

@-A:Type -> Type

type FL source · line 2397 · raw

Type

type BL source · line 2453 · raw

Type

type DB source · line 2488 · raw

Type

type D3 source · line 2512 · raw

Type

type AttnDims source · line 2524 · raw

Data

type CSM source · line 2655 · 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 2740 · raw

Type

type NS source · line 2763 · raw

Type

type TrainPL source · line 2813 · 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 57 · 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.

def OptState.split source · line 63 · raw

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

def OptState.join source · line 71 · 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 101 · 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 106 · 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 111 · 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 116 · raw

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

def Layer.dump source · line 122 · 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 128 · 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 133 · 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 137 · raw

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

def ew2_fin source · line 166 · raw

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

def f32_add source · line 177 · raw

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

def f32_mul source · line 180 · raw

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

def Mat.add source · line 184 · raw

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

(a + b, b)

def zero_of source · line 187 · raw

@+x:F32 -> F32

def sgd_l source · line 204 · raw

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

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

def mom_l source · line 212 · 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 224 · 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 231 · raw

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

def adam_m source · line 241 · 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 244 · raw

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

def adam_w source · line 247 · raw

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

def adam_l source · line 250 · 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 Opt.sgd_fin source · line 263 · raw

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

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

def Opt.mom_fin source · line 267 · raw

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

def Opt.fin source · line 271 · raw

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

def Opt.mat source · line 277 · raw

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

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

def Opt.at source · line 296 · raw

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

def OptCfg.b1 source · line 307 · raw

@+cfg:OptCfg -> F32

def OptCfg.b2 source · line 316 · raw

@+cfg:OptCfg -> F32

def OptCfg.c1 source · line 325 · raw

@+cfg:OptCfg -> F32

def OptCfg.c2 source · line 334 · raw

@+cfg:OptCfg -> F32

def beta_ok source · line 346 · 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 352 · raw

@+cfg:OptCfg -> Bool

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

def cfg_stop source · line 361 · raw

@ok:Bool -> IO(Unit)

def OptCfg.check source · line 368 · raw

@+cfg:OptCfg -> IO(Unit)

def mat_dump_fin source · line 372 · raw

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

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

def Mat.dump source · line 377 · raw

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

def load_cells source · line 380 · raw

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

def mat_restore_fin source · line 386 · raw

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

def Mat.restore source · line 390 · raw

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

def store_cells source · line 396 · raw

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

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

def mat_store_fin source · line 402 · raw

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

def Mat.store source · line 406 · raw

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

def pa_second source · line 411 · raw

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

def pl_pair source · line 415 · raw

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

def pl_second source · line 420 · raw

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

def lin_f3 source · line 432 · raw

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

def lin_f2 source · line 437 · raw

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

def lin_fwd source · line 442 · raw

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

def lin_b4 source · line 447 · raw

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

def lin_b3 source · line 452 · raw

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

def lin_b2 source · line 457 · raw

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

def lin_bwd source · line 464 · raw

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

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

def pair_s3 source · line 469 · raw

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

def lin_pair source · line 474 · raw

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

def lin_unpair source · line 478 · raw

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

def lin_s2 source · line 482 · raw

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

def lin_step2 source · line 487 · raw

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

def lin_step source · line 492 · 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 495 · raw

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

def lin_dump source · line 500 · raw

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

def lin_load3 source · line 505 · raw

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

def lin_load2 source · line 509 · raw

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

def lin_load source · line 514 · raw

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

def lin_store2 source · line 518 · raw

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

def lin_store source · line 523 · raw

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

def lin_zeros source · line 527 · raw

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

def Linear source · line 531 · raw

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

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

def linf_b3 source · line 537 · raw

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

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

def linf_bwd source · line 542 · raw

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

def LinearFirst source · line 547 · raw

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

def pick source · line 554 · raw

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

def no_step source · line 561 · raw

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

def act_f2 source · line 566 · raw

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

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

def act_b2 source · line 571 · raw

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

def relu_v source · line 581 · 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 584 · raw

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

def ReLU source · line 587 · raw

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

def seq_f3 source · line 594 · 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 609 · 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 624 · raw

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

def seq_at_f2 source · line 669 · 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 673 · 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 677 · 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 681 · 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 685 · 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 689 · 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 693 · 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 697 · 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 701 · 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 705 · raw

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

def q_start source · line 732 · raw

@ok:Bool -> F32

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

def ts5 source · line 743 · raw

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

def train_at_b source · line 772 · 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 779 · 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 783 · 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 787 · raw

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

def bitcast source · line 797 · raw

@u:U32 -> F32

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

def bits source · line 802 · raw

@x:F32 -> U32

def f32_le source · line 807 · raw

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

def f32s source · line 810 · raw

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

def rd_done source · line 817 · raw

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

def read_at source · line 826 · raw

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

n bytes from offset off

def mat_or_die source · line 832 · raw

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

def Mat.load source · line 840 · raw

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

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

def words source · line 845 · raw

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

def ml_words source · line 852 · raw

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

def Mat.print source · line 858 · raw

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

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

def F32.print source · line 861 · raw

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

def depth_go source · line 866 · 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 875 · raw

@+n:Nat -> Nat

def zeros_a source · line 879 · raw

@+n:Nat -> Array<F32>

n zeros, in an array with room for them

def sigmoid_v source · line 895 · raw

@+x:F32 -> F32

def sigmoid_d source · line 899 · raw

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

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

def Sigmoid source · line 902 · raw

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

def tanh_v source · line 905 · raw

@+x:F32 -> F32

def tanh_d source · line 908 · raw

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

def Tanh source · line 911 · raw

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

def silu_v source · line 916 · 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 919 · raw

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

def SiLU source · line 923 · raw

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

def gelu_inner source · line 928 · 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 931 · raw

@+x:F32 -> F32

def gelu_d source · line 934 · raw

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

def GELU source · line 941 · raw

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

def erf_hp source · line 963 · 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 966 · raw

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

def erf_small source · line 969 · raw

@+x:F32 -> F32

def erf_p0 source · line 974 · raw

@+t:F32 -> F32

[0.84375, 1.25): t = a - 1.0

def erf_p1 source · line 978 · raw

@+t:F32 -> F32

[1.25, 1.75): t = a - 1.5

def erf_p2 source · line 982 · raw

@+t:F32 -> F32

[1.75, 2.25): t = a - 2.0

def erf_p3 source · line 986 · raw

@+t:F32 -> F32

[2.25, 2.75): t = a - 2.5

def erf_p4 source · line 990 · raw

@+t:F32 -> F32

[2.75, 3.25): t = a - 3.0

def erf_p5 source · line 994 · raw

@+t:F32 -> F32

[3.25, 3.92): t = a - 3.5

def erf_sign source · line 997 · raw

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

def erf_i0 source · line 1002 · 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 1007 · raw

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

def erf_i2 source · line 1012 · raw

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

def erf_i3 source · line 1017 · raw

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

def erf_i4 source · line 1022 · raw

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

def erf_i5 source · line 1027 · raw

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

def erf_top source · line 1032 · raw

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

def erf source · line 1037 · raw

@+x:F32 -> F32

def gelu_erf_cdf source · line 1046 · 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 1049 · raw

@+x:F32 -> F32

def gelu_erf_d source · line 1052 · raw

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

def GELUErf source · line 1056 · raw

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

def mix32 source · line 1069 · raw

@+x:U32 -> U32

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

def drop_k source · line 1077 · 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 1080 · raw

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

def drop_mask source · line 1087 · raw

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

def drop_f2 source · line 1090 · raw

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

def drop_fwd source · line 1095 · raw

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

def drop_b2 source · line 1100 · raw

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

def drop_step source · line 1105 · raw

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

def drop_dump source · line 1110 · raw

@d:DropP -> PL<DropP>

def drop_load_fin source · line 1115 · raw

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

def drop_load source · line 1119 · raw

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

def drop_store source · line 1123 · raw

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

the counter, bitcast to one F32 as dump writes it

def Dropout source · line 1127 · raw

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

def sdot source · line 1144 · 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 1151 · raw

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

def pmax source · line 1159 · 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 1166 · raw

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

def pexp source · line 1174 · 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 1187 · raw

@r:PC -> Array<F32>

def pdiv source · line 1193 · 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 1200 · raw

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

def psm2 source · line 1205 · raw

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

def psmc source · line 1212 · 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 1215 · raw

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

def copy_n source · line 1219 · 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 1231 · 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 1239 · raw

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

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

def sm_fwd source · line 1244 · raw

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

def smb_fix source · line 1250 · 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 1257 · raw

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

def smb_rows source · line 1263 · 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 1270 · raw

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

def sm_bwd source · line 1275 · raw

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

def Softmax source · line 1281 · raw

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

softmax over each row; saves its output

def lsum source · line 1287 · 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 1295 · 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 1302 · raw

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

def lsm_r2 source · line 1307 · raw

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

def lsm_row source · line 1312 · raw

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

def lsmf_rows source · line 1315 · raw

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

def lsm_fwd source · line 1322 · raw

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

def asum source · line 1328 · 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 1336 · 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 1343 · raw

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

def lsmb_rows source · line 1349 · 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 1356 · raw

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

def LogSoftmax source · line 1361 · raw

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

def mat_step_fin source · line 1367 · raw

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

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

def mat_step source · line 1371 · raw

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

def g_add_fin source · line 1374 · raw

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

def Mat.sum2 source · line 1380 · raw

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

a + b, dropping b

def dup_fin source · line 1383 · raw

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

def Mat.dup source · line 1389 · raw

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

two copies of a matrix

def ml_list source · line 1392 · raw

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

def ssq source · line 1402 · 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 1410 · raw

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

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

def rms_w source · line 1417 · 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 1424 · raw

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

def rmsf_row source · line 1429 · raw

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

def rmsf_rows source · line 1435 · 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 1442 · raw

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

def rms_fwd source · line 1447 · raw

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

def rmsb_dot source · line 1460 · 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 1467 · raw

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

def rmsb_w source · line 1473 · 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 1482 · raw

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

def rmsb_row2 source · line 1487 · 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 1493 · raw

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

def rmsb_rows source · line 1498 · raw

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

def gain_bwd_fin source · line 1505 · raw

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

def rms_b2 source · line 1508 · raw

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

def rms_bwd source · line 1513 · raw

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

def RMSNorm source · line 1519 · raw

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

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

def csq source · line 1528 · 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 1538 · raw

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

def ln_mr1 source · line 1543 · raw

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

def ln_mr source · line 1549 · 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 1556 · 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 1563 · raw

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

def lnf_row source · line 1568 · raw

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

def lnf_rows source · line 1573 · raw

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

def ln_f2 source · line 1580 · raw

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

def ln_fwd source · line 1585 · raw

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

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

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

def lnb_w source · line 1611 · 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 1620 · raw

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

def lnb_row2 source · line 1625 · 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 1630 · raw

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

def lnb_rows source · line 1635 · raw

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

def ln_b2 source · line 1642 · raw

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

def ln_bwd source · line 1647 · raw

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

def ln_pair source · line 1652 · raw

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

def ln_unpair source · line 1656 · raw

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

def ln_s2 source · line 1660 · raw

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

def ln_step2 source · line 1665 · raw

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

def ln_step source · line 1670 · raw

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

def ln_dump2 source · line 1673 · raw

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

def ln_dump source · line 1678 · raw

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

def ln_load3 source · line 1683 · raw

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

def ln_load2 source · line 1687 · raw

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

def ln_load source · line 1692 · raw

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

def ln_store2 source · line 1696 · raw

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

def ln_store source · line 1701 · raw

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

def LayerNorm source · line 1705 · raw

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

def emb_cp source · line 1713 · 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 1720 · raw

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

def emb_f source · line 1725 · raw

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

def emb_add source · line 1731 · 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 1739 · 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 1746 · raw

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

def emb_bwd source · line 1751 · raw

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

def Embedding source · line 1757 · raw

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

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

def lnb_f2 source · line 1764 · raw

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

def lnb_b3 source · line 1769 · raw

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

def lnb_b2 source · line 1774 · raw

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

def LinearNB source · line 1779 · raw

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

def pair_f4 source · line 1798 · 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 1841 · 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 1871 · 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 1891 · 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 1918 · raw

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

def Add2 source · line 1923 · raw

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

def mul2_f3 source · line 1926 · raw

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

def mul2_f2 source · line 1931 · raw

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

def mul2_f source · line 1936 · raw

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

def mpair_a source · line 1941 · raw

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

def mul2_b2 source · line 1947 · raw

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

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

def Mul2 source · line 1952 · raw

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

def rope_ang source · line 1973 · 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 1977 · 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 1991 · 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 1998 · 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 2004 · 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 2013 · 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 2020 · raw

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

def rope_go2 source · line 2025 · raw

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

def rope_go source · line 2030 · raw

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

def RoPE source · line 2036 · raw

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

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

def kv_of source · line 2056 · 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 2060 · raw

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

xs reversed onto acc

def drow source · line 2072 · 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 2079 · raw

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

def qrow source · line 2086 · 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 2096 · 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 2106 · raw

@r:PR -> Nat

def pr_of source · line 2111 · raw

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

def prow3 source · line 2117 · 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 2122 · raw

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

def prow source · line 2130 · 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 2135 · 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 2143 · raw

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

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

def pmul source · line 2151 · 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 2158 · raw

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

def pacc source · line 2165 · 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 2173 · 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 2176 · raw

@r:A2 -> Array<F32>

def rows_in source · line 2182 · 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 2185 · 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 2193 · raw

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

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

def head_cols source · line 2209 · raw

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

def hc_kc source · line 2214 · raw

@+c:HC -> U32

def fs_fin source · line 2219 · raw

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

def head_f2 source · line 2224 · 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 2231 · 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 2236 · raw

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

def fs_o source · line 2243 · raw

@s:FS -> Array<F32>

def gfix source · line 2255 · 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 2262 · raw

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

def gw source · line 2274 · 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 2284 · raw

@r:GR -> Nat

def gr3 source · line 2290 · 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 2295 · raw

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

def grow source · line 2304 · 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 2308 · 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 2315 · raw

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

def hb4 source · line 2321 · 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 2327 · 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 2332 · 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 2338 · 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 2344 · 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 2349 · raw

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

def bg_fin source · line 2360 · 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 2365 · raw

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

def bgroups source · line 2370 · raw

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

def hcat source · line 2384 · 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 2390 · raw

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

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

def fl3 source · line 2400 · raw

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

def fl2 source · line 2405 · raw

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

def fl1 source · line 2410 · raw

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

def fslice source · line 2415 · raw

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

def fslices source · line 2421 · raw

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

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

def fleaf source · line 2428 · raw

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

def fpar source · line 2436 · raw

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

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

def blocks_in source · line 2446 · raw

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

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

def bl4 source · line 2456 · raw

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

def bl3 source · line 2461 · raw

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

def bl2 source · line 2466 · raw

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

def bl1 source · line 2471 · raw

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

def bslice source · line 2476 · raw

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

def bslices source · line 2481 · raw

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

def db_of source · line 2491 · raw

@s:BG -> DB

def bleaf source · line 2496 · raw

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

def bpar source · line 2503 · raw

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

def dblocks_in source · line 2515 · raw

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

def dm_of source · line 2527 · raw

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

def attn_tree source · line 2538 · raw

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

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

def fgo source · line 2541 · raw

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

def bgo source · line 2548 · raw

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

def af2 source · line 2555 · raw

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

def attn_fwd source · line 2560 · raw

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

def ab3 source · line 2565 · raw

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

def ab2 source · line 2570 · raw

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

def attn_bwd source · line 2575 · raw

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

def Attention source · line 2581 · raw

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

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

def hit source · line 2592 · raw

@b:Bool -> F32

def ts_add source · line 2602 · 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 2607 · raw

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

def ts_pairs source · line 2612 · raw

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

def ts_put source · line 2619 · raw

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

def ts_carry source · line 2624 · raw

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

def ts_pass source · line 2631 · raw

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

def ts_first source · line 2634 · raw

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

def ts_go source · line 2639 · raw

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

def tree_sum source · line 2648 · raw

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

def cesm4 source · line 2658 · raw

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

def cesm3 source · line 2661 · raw

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

def cesm_e source · line 2666 · raw

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

def cesm2 source · line 2671 · raw

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

def ce_sm source · line 2676 · raw

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

def ce_g source · line 2680 · 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 2689 · 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 2694 · raw

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

def ce_bad source · line 2699 · raw

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

def ce_pick source · line 2705 · 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 2712 · raw

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

def ce_row source · line 2717 · raw

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

def ce_rows source · line 2722 · raw

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

def ce_fin source · line 2729 · raw

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

def CrossEntropy source · line 2734 · raw

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

def mse_l source · line 2744 · 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 2751 · raw

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

def MSE source · line 2758 · raw

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

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

def nll_hit source · line 2766 · raw

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

def nll_one source · line 2772 · 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 2780 · 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 2787 · raw

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

def NLL source · line 2793 · raw

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

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

def byte_of source · line 2804 · raw

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

def read_all source · line 2807 · raw

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

def td0 source · line 2816 · raw

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

def td1 source · line 2820 · raw

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

def td3 source · line 2824 · raw

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

def f32_at source · line 2839 · raw

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

def tr0 source · line 2848 · raw

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

def tr_m2 source · line 2852 · raw

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

def tr_adam2 source · line 2860 · raw

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

def tr_adam source · line 2864 · raw

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

def tr3 source · line 2868 · raw

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

def WCHUNK source · line 2896 · raw

U32

def chunk_bytes source · line 2900 · 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 2907 · 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 2914 · 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 2922 · 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 2930 · raw

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

def write_f32 source · line 2938 · 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 2944 · raw

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

def tsa_fin source · line 2958 · raw

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

def tsa_m source · line 2965 · raw

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

def tsa_pw source · line 2969 · raw

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

Templates

template ew1_l source · line 146 · 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 154 · raw

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

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

template Ew.map source · line 161 · raw

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

template Ew.zip source · line 172 · raw

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

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

template ActOut source · line 576 · raw

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

template seq_f2 source · line 599 · 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 604 · 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 614 · 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 619 · 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 627 · 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 630 · 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 635 · raw

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

template pair_dump source · line 639 · raw

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

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

template pair_load2 source · line 644 · 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 649 · 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 654 · 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 659 · 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 664 · 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 725 · raw

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

template Train.new source · line 739 · raw

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

template ts4 source · line 748 · 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 755 · 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 760 · 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 766 · raw

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

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

template acti_f3 source · line 887 · raw

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

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

template ActIn source · line 892 · raw

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

template Id source · line 1786 · raw

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

template pair_step2 source · line 1790 · 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 1795 · 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 1804 · 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 1811 · 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 1816 · 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 1821 · 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 1826 · 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 1831 · 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 1836 · 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 1846 · 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 1851 · 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 1856 · 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 1861 · 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 1876 · 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 1881 · 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 1886 · 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 1896 · 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 1901 · 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 1906 · 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 1913 · raw

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

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

template Residual source · line 1957 · raw

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

template Train.dump source · line 2830 · 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

template tr_m1 source · line 2856 · raw

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

template tr2 source · line 2872 · raw

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

template tr1 source · line 2877 · raw

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

template Train.restore source · line 2883 · 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)

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

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

template tsa_mm source · line 2978 · raw

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

template tsa_mv source · line 2982 · raw

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

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