~/bend-docscommunity

main.bend checks

raw source on the hub · import bend-ml-llama@0.3.0.0/main.bend as Main

bend-ml-llama: Llama-architecture language models (SmolLM2 and the rest of the family) in Bend 2, with the structure in the types and laws checked by Bend's kernel.

The sizes of a model live in a config (Cfg), and every weight matrix has its shape in its type, computed from the config: a model of other sizes is the same code with another config. The numbers (F32) are tested against PyTorch / transformers by the scripts of the llama.bend repository; the structure is proved here:

- a head has 2·half numbers and is split into two halves of half numbers (RoPE rotates each number with the one half a head later), and splitting a head and joining the halves gives it back (head_split); - the query heads are cut into kv groups of g heads, and group j reads key/value head j: there are exactly kv groups (groups_len) and an empty cache holds one array of keys and one of values per key/value head (kv_heads, arrays_len), and query head j·g + i reads key/value head (j·g + i) / g = j, transformers' repeat_kv mapping (gqa_head); - the key/value cache at position t has the type KV<t>: t positions written in flat arrays, so attention at position t scores exactly the positions 0..t and nothing after (causal, read_len); - the shapes of the weights in the types add up to the number of values in the weight files (layout laws); - a token drawn with temperature, top-k and top-p is an id of the logits it was drawn from (sample_below).

The order of every F32 operation follows transformers' modeling_llama.py (5.19), so the results match it to a few ulps and the argmax exactly (llama.bend's NOTES.md).

2 imports
import Base
import bend-ml-tensor-array@0.1.20.0/main.bend as TA

Laws

law add_zero proved

Also proved in bend-mathlib as nat.add_zero: import bend-mathlib@0.8.2.0/nat.bend as MNat, then MNat.add_zero.

source · line 29 · raw

@a:Nat -> {Nat.add(a, 0n) == a : Nat}

law add_succ provedsource · line 41 · raw

@a:Nat -> @-b:Nat -> {1n+Nat.add(a, b) == Nat.add(a, 1n+b) : Nat}

law add_one provedsource · line 54 · raw

@a:Nat -> {Nat.add(a, 1n) == 1n+a : Nat}

law add_assoc proved

Also proved in bend-mathlib as nat.add_assoc: import bend-mathlib@0.8.2.0/nat.bend as MNat, then MNat.add_assoc.

source · line 66 · raw

@a:Nat -> @-b:Nat -> @-c:Nat -> {Nat.add(Nat.add(a, b), c) == Nat.add(a, Nat.add(b, c)) : Nat}

law smollm2_135m_sizes provedsource · line 165 · raw

Pair({Cfg.dim(smollm2_135m) == 576n : Nat}, Pair({Cfg.heads(smollm2_135m) == 9n : Nat}, Pair({Cfg.head(smollm2_135m) == 64n : Nat}, {Cfg.kvw(smollm2_135m) == 192n : Nat})))

LAW (the configs): SmolLM2-135M has 9 query heads of 64, a hidden size of 576 and keys of 192; SmolLM2-360M has 15 query heads of 64, a hidden size of 960 and keys of 320; SmolLM2-1.7B has 32 query heads of 64, a hidden size of 2048 and keys of 2048 (as many key/value heads as query heads), as their config.json files say.

law smollm2_360m_sizes provedsource · line 171 · raw

Pair({Cfg.dim(smollm2_360m) == 960n : Nat}, Pair({Cfg.heads(smollm2_360m) == 15n : Nat}, Pair({Cfg.head(smollm2_360m) == 64n : Nat}, {Cfg.kvw(smollm2_360m) == 320n : Nat})))

law smollm2_1_7b_sizes provedsource · line 177 · raw

Pair({Cfg.dim(smollm2_1_7b) == 2048n : Nat}, Pair({Cfg.heads(smollm2_1_7b) == 32n : Nat}, Pair({Cfg.head(smollm2_1_7b) == 64n : Nat}, {Cfg.kvw(smollm2_1_7b) == 2048n : Nat})))

law layout_135m provedsource · line 201 · raw

{Cfg.size(smollm2_135m) == 134515008 : U32}

LAW (layout): the shapes the types give to SmolLM2-135M's weights add up to 134,515,008 numbers, SmolLM2-360M's to 361,821,120 and SmolLM2-1.7B's to 1,711,376,384: the number of values in each model.safetensors (reference/prep.py counts them). The loader checks every file's size against the shape in its type, so together the files cover the checkpoint exactly. In U32, whose sums wrap at 2^32; every partial sum here is below it, so this is the exact count.

law layout_360m provedsource · line 207 · raw

{Cfg.size(smollm2_360m) == 361821120 : U32}

law layout_1_7b provedsource · line 213 · raw

{Cfg.size(smollm2_1_7b) == 1711376384 : U32}

law head_split provedsource · line 263 · raw

@+xs:List<&2, F32> -> @+half:Nat -> {join(split(half, xs)) == xs : List<&2, F32>}

LAW (head_split): splitting a list at half and joining the two parts gives the list back, so cutting a head into the halves that RoPE pairs up loses and reorders nothing.

law arrays_len provedsource · line 512 · raw

@+n:Nat -> @+d:Nat -> {List.length(&1, Array<F32>, arrays(n, d)) == n : Nat}

LAW (arrays_len): arrays(n, d) makes exactly n arrays.

law kv_heads provedsource · line 528 · raw

@+c:Cfg -> @+cap:Nat -> {kv_count(cap, 0n, KV.empty(c, cap)) == Cfg.kv(c) : Nat}

LAW (kv_heads): an empty cache holds one array of keys (and one of values) per key/value head, so key/value head j of the model reads column j and no other.

law kv_room provedsource · line 566 · raw

@+cap:Nat -> @+h:Nat -> @+t:Nat -> @e:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(Nat.mul(1n+t, h), 0x5c5507d77be847e1e454ec33ab54e087/main.pow2(0x5c5507d77be847e1e454ec33ab54e087/main.cap_depth(Nat.mul(cap, h)))) : Bool}

LAW (kv_room): KV.empty(c, cap) gives each key/value head arrays of 2^cap_depth(cap·head) slots, and KV.push at position t writes head numbers from t·head. For a position below the capacity (t < cap) they all fall inside the slots: (1 + t)·head <= 2^cap_depth(cap·head), so a push never wraps around onto position 0. (What stays trusted is tensor-array's: Array.new allocates 2^d slots.) KV.push takes a proof that t < cap, so a push at t >= cap does not compile (tests/bad_cap.bend).

law groups_len provedsource · line 602 · raw

@n:Nat -> @+g:Nat -> @+hs:List<&2, Head> -> {List.length(&2, List<&2, Head>, groups(n, g, hs)) == n : Nat}

LAW (groups_len): the query heads make exactly as many groups as there are key/value heads, so attention (which walks the groups and the columns together) pairs every group with a key/value head.

law div_block provedsource · line 617 · raw

@+m:Nat -> @-n:Nat -> @-d:Nat -> @-r:Nat -> {Nat.divmod.go(Nat.add(1n+m, n), m, d, r) == Nat.divmod.go(n, Nat.add(m, r), 1n+d, 0n) : Pair(Nat, Nat)}

one block of 1 + m numbers in Nat.divmod: the counter m runs down to 0, then the quotient d grows by one

law div_blocks provedsource · line 633 · raw

@+j:Nat -> @+s:Nat -> @+n:Nat -> @+d:Nat -> {Nat.divmod.go(Nat.add(Nat.mul(j, 1n+s), n), s, d, 0n) == Nat.divmod.go(n, s, Nat.add(j, d), 0n) : Pair(Nat, Nat)}

j whole blocks of 1 + s numbers add j to the quotient

law div_rest provedsource · line 652 · raw

@+i:Nat -> @-k:Nat -> @-d:Nat -> @-r:Nat -> {Pair.fst(Nat, Nat, Nat.divmod.go(i, Nat.add(i, k), d, r)) == d : Nat}

fewer than 1 + s numbers after the last whole block leave the quotient alone

law gqa_head provedsource · line 669 · raw

@+j:Nat -> @+i:Nat -> @+k:Nat -> {Nat.div(Nat.add(Nat.mul(j, 1n+Nat.add(i, k)), i), 1n+Nat.add(i, k)) == j : Nat}

LAW (gqa_head): with g = 1 + i + k query heads per key/value head (g > 0 and i < g), query head j·g + i, the head i of group j, reads key/value head (j·g + i) / g = j. That division is how transformers maps query heads to key/value heads (repeat_kv), so grouping the heads computes the same attention.

law read_len provedsource · line 704 · raw

@k:Array<F32> -> @+m:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> {sr_len(sr_of(k, 0x5c5507d77be847e1e454ec33ab54e087/main.read_l(m, idx, acc, rc))) == Nat.add(1n+m, List.length(&2, F32, acc)) : Nat}

LAW (read_len): reading m + 1 numbers of an array onto acc gives m + 1 more scores. PROOF: by induction on m. With 0 left it reads one number onto acc (1 + the length of acc, by the definitions); with 1 + p left it reads one and recurses one index down, and len_step moves the 1 out of the sum (tensor-array's leaf_len, for scores).

law causal provedsource · line 739 · raw

@+hd:Nat -> @+t:Nat -> @k:Array<F32> -> @+q:List<&2, F32> -> {sr_len(scores(hd, t, k, q)) == 1n+t : Nat}

LAW (causal): attention at position t scores exactly t + 1 keys, the positions 0..t: one score per position written, none for a position not yet computed. PROOF: the gemm's result has one constructor, so matching it opens it without computing the product (sr_len_g); read_len counts the 1 + t numbers read back.

law ins_below provedsource · line 1299 · raw

@+cs:List<&2, Cand> -> @+g:Bool -> @+v:F32 -> @+i:Nat -> @+n:Nat -> @+e:{True{} == below(cs, n) : Bool} -> @ei:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+i, n) : Bool} -> {True{} == below(ins(cs, g, v, i), n) : Bool}

PROOF: by induction on cs. Inserting puts the new candidate (id i < n) at the front, or keeps the head and inserts into the tail, whose ids are below n by the induction hypothesis.

law take_below provedsource · line 1322 · raw

@+cs:List<&2, Cand> -> @+k:Nat -> @+n:Nat -> @+e:{True{} == below(cs, n) : Bool} -> {True{} == below(List.take(&2, Cand, cs, k), n) : Bool}

PROOF: taking the first k candidates keeps a part of them.

law topk_below provedsource · line 1341 · raw

@+xs:List<&2, F32> -> @+g:Bool -> @+k:Nat -> @+i:Nat -> @+m:Nat -> @+lo:F32 -> @+cs:List<&2, Cand> -> @+n:Nat -> @+inv:{Nat.add(i, List.length(&2, F32, xs)) == n : Nat} -> @+e:{True{} == below(cs, n) : Bool} -> {True{} == below(topk.go(xs, g, k, i, m, lo, cs), n) : Bool}

PROOF: by induction on xs, with i + length(xs) = n: the head of xs has id i < n (step_room), and after it 1 + i + length(tail) = n still holds (step_inv); ins_below and take_below keep the candidates' ids below n.

law pick_below provedsource · line 1367 · raw

@+cs:List<&2, Cand> -> @+s:Bool -> @+q:Nat -> @+temp:F32 -> @+top:F32 -> @+target:F32 -> @+acc:F32 -> @+n:Nat -> @+e:{True{} == below(cs, n) : Bool} -> @pos:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n, n) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+Cand.i(pick.go(cs, s, q, temp, top, target, acc)), n) : Bool}

PROOF: by induction on cs: the draw returns a candidate of cs, whose id is below n, or id 0 < n when cs is empty.

law sample_below provedsource · line 1399 · raw

@+xs:List<&2, F32> -> @+temp:F32 -> @+k:Nat -> @+p:F32 -> @+u:F32 -> @pos:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n, List.length(&2, F32, xs)) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+Cand.i(sample(xs, temp, k, p, u)), List.length(&2, F32, xs)) : Bool}

LAW (sample_below): from logits xs with at least one value, the id sample draws is below the length of xs: an id of the vocabulary, whatever the temperature, k, p and draw. PROOF: the top-k pass only stores ids of xs, each below its length (topk_below, from i = 0 and no candidates), and the draw returns one of them (pick_below, through the top-p count, whatever it is).

Types

type Cfg source · line 87 · raw

Data

kv: key/value heads; g: query heads per key/value head; half: half a head (a head has 2·half numbers, so its size is even by construction); f: the MLP's inner width; layers; vocab: the vocabulary (the embedding has vocab rows, and the LM head is the embedding); eps: RMSNorm's epsilon; theta: RoPE's base.

type Rot source · line 423 · raw

Data

the cosines and sines of position t, one per frequency (shared by every head of every layer)

type KV source · line 476 · raw

@-cap:Nat -> @-t:Nat -> Type

One pair of arrays per key/value head: the keys (after RoPE) and the values of positions 0..t-1, position p at [p·head, p·head + head). The type indexes are the capacity cap (the positions the arrays have room for, set by KV.empty) and t, the number of positions written. KV.push asks for a proof that t < cap, so a cache is never written past its room (kv_room says why that is enough).

type SR source · line 687 · raw

Type

the scores of one query head against the keys of its key/value head, and the keys handed back

type WR source · line 750 · raw

Type

the weighted sum of the values of positions 0..t: w (t + 1 weights) times V (t + 1 rows of head numbers)

type HO source · line 767 · raw

Type

one query head: softmax(q·K · scale) · V, and the two arrays handed back

type AT source · line 799 · raw

Type

every group, in order: the outputs of query heads 0, 1, ..., and the caches handed back

type GW source · line 808 · raw

Type

one key/value head's work: its group of query heads, its keys and its values

type GS source · line 831 · raw

Type

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

type KA source · line 870 · raw

@-cap:Nat -> @-t:Nat -> Type

the attention output and the cache handed back

type LW source · line 899 · raw

@-c:Cfg -> Type

the weights of one layer, with their shapes from the config (row = output, as PyTorch stores them)

type Act source · line 904 · raw

Data

what a layer computes at one position, every intermediate: the tests compare them with transformers, and a backward pass would read them

type BR source · line 909 · raw

@-c:Cfg -> @-cap:Nat -> @-t:Nat -> Type

the layer's output at position t, its cache grown to t + 1 positions, its weights handed back, and the activations

type Model source · line 985 · raw

@-c:Cfg -> Type

emb: the embedding (vocab rows; also the LM head, tied); lnf: the final norm's gains

type Run source · line 989 · raw

@-c:Cfg -> @-cap:Nat -> @-t:Nat -> Type

the last layer's output, the layers and caches handed back, and every layer's activations

type Out source · line 1009 · raw

@-c:Cfg -> @-cap:Nat -> @-t:Nat -> Type

what one position produces: the logits, the final norm, every layer's activations, the caches grown to t + 1 positions and the model handed back

type AM source · line 1079 · raw

Data

the first index of the largest value (torch.argmax does the same), and the value

type Cand source · line 1138 · raw

Data

a candidate: a logit and its id

type Kept source · line 1215 · raw

Data

how many candidates top-p keeps (n) and their weight (z)

Definitions

def Cfg.half source · line 94 · raw

@+c:Cfg -> Nat

def Cfg.kv source · line 99 · raw

@+c:Cfg -> Nat

def Cfg.g source · line 104 · raw

@+c:Cfg -> Nat

def Cfg.ffn source · line 109 · raw

@+c:Cfg -> Nat

def Cfg.layers source · line 114 · raw

@+c:Cfg -> Nat

def Cfg.vocab source · line 119 · raw

@+c:Cfg -> Nat

def Cfg.eps source · line 124 · raw

@+c:Cfg -> F32

def Cfg.theta source · line 129 · raw

@+c:Cfg -> F32

def Cfg.head source · line 135 · raw

@+c:Cfg -> Nat

the size of a head

def Cfg.heads source · line 139 · raw

@+c:Cfg -> Nat

the query heads: kv groups of g

def Cfg.dim source · line 143 · raw

@+c:Cfg -> Nat

the hidden size: one head per query head

def Cfg.kvw source · line 147 · raw

@+c:Cfg -> Nat

the width of the keys (and of the values): one head per key/value head

def smollm2_135m source · line 151 · raw

Cfg

SmolLM2-135M (HuggingFaceTB/SmolLM2-135M): 3 key/value heads of 3 query heads each, heads of 64

def smollm2_360m source · line 155 · raw

Cfg

SmolLM2-360M: 5 key/value heads of 3 query heads each, heads of 64

def smollm2_1_7b source · line 159 · raw

Cfg

SmolLM2-1.7B: 32 key/value heads of one query head each (plain multi-head attention), heads of 64

def n32 source · line 185 · raw

@+n:Nat -> U32

def Cfg.layer_size source · line 190 · raw

@+c:Cfg -> U32

the numbers of one layer, from the shapes of LW<c>: two norms, q and o (dim × dim), k and v (kvw × dim), gate and up (ffn × dim) and down (dim × ffn)

def Cfg.size source · line 194 · raw

@+c:Cfg -> U32

the numbers of the model, from the shapes of Model<c>: the embedding (vocab × dim), the layers, the final norm

def split source · line 227 · raw

@+half:Nat -> @+xs:List<&2, F32> -> Head

def join source · line 230 · raw

@h:Head -> List<&2, F32>

def heads source · line 236 · raw

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

n heads of 2·half numbers from xs, in order

def chunks source · line 244 · raw

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

n plain heads (the values, which RoPE does not touch)

def joins source · line 251 · raw

@hs:List<&2, Head> -> List<&2, List<&2, F32>>

def flat source · line 258 · raw

@hs:List<&2, Head> -> List<&2, F32>

def list.add source · line 288 · raw

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

xs + ys, element by element (to the shorter length)

def list.mul source · line 296 · raw

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

xs * ys, element by element (to the shorter length)

def list.scale source · line 304 · raw

@+k:F32 -> @xs:List<&2, F32> -> List<&2, F32>

k * xs

def list.sum.go source · line 312 · raw

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

acc + xs, a running sum from the left

def list.max.go source · line 319 · raw

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

def list.max source · line 327 · raw

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

the maximum of xs (0 for an empty list)

def list.exp_shift source · line 335 · raw

@xs:List<&2, F32> -> @+m:F32 -> List<&2, F32>

exp(x - m) for each x

def list.div source · line 343 · raw

@xs:List<&2, F32> -> @+s:F32 -> List<&2, F32>

xs / s

def list.softmax.fin source · line 350 · raw

@+es:List<&2, F32> -> List<&2, F32>

def list.softmax source · line 354 · raw

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

stable softmax: subtracts the maximum before exponentiating

def squares source · line 358 · raw

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

the squares of xs

def pair_sums source · line 366 · raw

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

adjacent pairs added: [a, b, c, d, e] -> [a + b, c + d, e]

def tree_sum source · line 375 · raw

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

the sum as a balanced tree of additions (pairwise summation): its rounding error grows with log2(n), not n, as in PyTorch's cascade sum. A running sum loses the small squares next to a large one (NOTES, 2026-10-10).

def rmsnorm source · line 387 · raw

@+c:Cfg -> @+x:List<&2, F32> -> @+g:List<&2, F32> -> List<&2, F32>

RMSNorm: g * (x * (1 / sqrt(mean(x²) + eps))), in this order (LlamaRMSNorm.forward)

def inv_freq.go source · line 391 · raw

@n:Nat -> @+i:Nat -> @+theta:F32 -> @+hd:F32 -> List<&2, F32>

inv_freq[i] = 1 / (theta ^ (2i / head)), i = 0..half-1 (LlamaRotaryEmbedding, default rope)

def inv_freq source · line 398 · raw

@+c:Cfg -> List<&2, F32>

def angles source · line 401 · raw

@+t:F32 -> @fs:List<&2, F32> -> List<&2, F32>

def cos.l source · line 408 · raw

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

def sin.l source · line 415 · raw

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

def rot_at source · line 426 · raw

@+c:Cfg -> @+t:Nat -> Rot

def rot_lo source · line 430 · raw

@h1:List<&2, F32> -> @h2:List<&2, F32> -> @cs:List<&2, F32> -> @sn:List<&2, F32> -> List<&2, F32>

first half: lo*cos + (-hi)*sin; second half: hi*cos + lo*sin (q*cos + rotate_half(q)*sin)

def rot_hi source · line 437 · raw

@h1:List<&2, F32> -> @h2:List<&2, F32> -> @cs:List<&2, F32> -> @sn:List<&2, F32> -> List<&2, F32>

def rope source · line 444 · raw

@rt:Rot -> @h:Head -> Head

def ropes source · line 449 · raw

@+rt:Rot -> @hs:List<&2, Head> -> List<&2, Head>

def silu source · line 457 · raw

@+x:F32 -> F32

silu(x) = x / (1 + exp(-x)), the form ATen's CPU kernel uses

def swiglu source · line 461 · raw

@g:List<&2, F32> -> @u:List<&2, F32> -> List<&2, F32>

silu(gate) * up

def arrays source · line 480 · raw

@n:Nat -> @+d:Nat -> List<&1, Array<F32>>

n arrays of 2^d slots

def KV.empty source · line 488 · raw

@+c:Cfg -> @+cap:Nat -> KV<cap, 0n>

a cache with no position yet, with room for cap positions per key/value head

def write_rows source · line 492 · raw

@arrs:List<&1, Array<F32>> -> @rs:List<&2, List<&2, F32>> -> @+off:U32 -> List<&1, Array<F32>>

writes row j of rs into array j, at off

def KV.push source · line 500 · raw

@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+ks:List<&2, List<&2, F32>> -> @+vs:List<&2, List<&2, F32>> -> KV<cap, 1n+t>

position t: the keys and values of every key/value head written at t·head; room is the proof that t < cap

def kv_count source · line 506 · raw

@-cap:Nat -> @-t:Nat -> @kv:KV<cap, t> -> Nat

the number of key/value heads a cache holds

def leb_trans source · line 537 · raw

@a:Nat -> @b:Nat -> @c:Nat -> @e1:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(a, b) : Bool} -> @e2:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(b, c) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(a, c) : Bool}

a <= b and b <= c -> a <= c

def leb_mul source · line 551 · raw

@a:Nat -> @b:Nat -> @+h:Nat -> @e:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(a, b) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(Nat.mul(a, h), Nat.mul(b, h)) : Bool}

a <= b -> a·h <= b·h

def step_room source · line 575 · raw

@+t:Nat -> @+p:Nat -> @-cap:Nat -> @e:{Nat.add(t, 1n+p) == cap : Nat} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool}

For callers that walk the positions with a counter of steps left (fuel), keeping t + fuel = cap: with 1 + p steps left, position t is below the capacity (step_room), and after the step t + 1 + p = cap still holds (step_inv).

def step_inv source · line 580 · raw

@+t:Nat -> @-p:Nat -> @-cap:Nat -> @e:{Nat.add(t, 1n+p) == cap : Nat} -> {Nat.add(1n+t, p) == cap : Nat}

def groups source · line 593 · raw

@n:Nat -> @+g:Nat -> @+hs:List<&2, Head> -> List<&2, List<&2, Head>>

===================================================================================================== Grouped-query attention ===================================================================================================== the query heads in n groups of g, in order: group j holds query heads j·g .. j·g + g - 1

def sr_len source · line 690 · raw

@sr:SR -> Nat

def sr_of source · line 695 · raw

@k:Array<F32> -> @al:0x5c5507d77be847e1e454ec33ab54e087/main.AL -> SR

def sr_read source · line 720 · raw

@+t:Nat -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.G -> SR

read the 1 + t scores the gemm wrote (read_l reads from the last index down, so it starts at t)

def scores source · line 727 · raw

@+hd:Nat -> @+t:Nat -> @k:Array<F32> -> @+q:List<&2, F32> -> SR

q·k for the keys of positions 0..t, oldest first: K (t + 1 rows of head numbers) times q, the product Mat.matmul_nt computes for one row (bend-ml-tensor-array's gemm)

def sr_len_g source · line 730 · raw

@+t:Nat -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.G -> {sr_len(sr_read(t, g)) == 1n+t : Nat}

def wr_of source · line 753 · raw

@v:Array<F32> -> @al:0x5c5507d77be847e1e454ec33ab54e087/main.AL -> WR

def wr_read source · line 758 · raw

@+hd:Nat -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.G -> WR

def weighted source · line 763 · raw

@+hd:Nat -> @+t:Nat -> @v:Array<F32> -> @+w:List<&2, F32> -> WR

def HO.then source · line 770 · raw

@-R:Type -> @h:HO -> @then:(@_:Array<F32> -> @_:Array<F32> -> @_:List<&2, F32> -> R) -> R

def head_wr source · line 775 · raw

@k:Array<F32> -> @wr:WR -> HO

def head_sr source · line 780 · raw

@+hd:Nat -> @+t:Nat -> @+scale:F32 -> @v:Array<F32> -> @sr:SR -> HO

def head_att source · line 785 · raw

@+hd:Nat -> @+t:Nat -> @+scale:F32 -> @k:Array<F32> -> @v:Array<F32> -> @+q:List<&2, F32> -> HO

def group_att source · line 789 · raw

@hs:List<&2, Head> -> @+hd:Nat -> @+t:Nat -> @+scale:F32 -> @k:Array<F32> -> @v:Array<F32> -> @+acc:List<&2, F32> -> HO

the query heads of one group over its key/value head, in order; the arrays are threaded through

def at_join source · line 802 · raw

@p:AT -> @q:AT -> AT

def gws source · line 811 · raw

@gs:List<&2, List<&2, Head>> -> @ks:List<&1, Array<F32>> -> @vs:List<&1, Array<F32>> -> List<&1, GW>

def seq_att source · line 819 · raw

@ws:List<&1, GW> -> @+hd:Nat -> @+t:Nat -> @+scale:F32 -> @aks:List<&1, Array<F32>> -> @avs:List<&1, Array<F32>> -> @+acc:List<&2, F32> -> AT

some groups one after another, in order

def cut source · line 834 · raw

@n:Nat -> @xs:List<&1, GW> -> @acc:List<&1, GW> -> GS

def halvings source · line 842 · raw

@fuel:Nat -> @+n:Nat -> Nat

how many times n groups are halved (the larger half kept) until one or none is left

def par_att source · line 859 · raw

@d:Nat -> @gs:GS -> @+n:Nat -> @+hd:Nat -> @+t:Nat -> @+scale:F32 -> AT

the n groups of gs (cut in two halves) in a tree of parallel lets of depth d, each leaf a half of a node; the outputs and the caches come back in group order, so every head computes what it computed one after another

def ka_of source · line 873 · raw

@-cap:Nat -> @-t:Nat -> @at:AT -> KA<cap, t>

def KA.then source · line 878 · raw

@-R:Type -> @-cap:Nat -> @-t:Nat -> @ka:KA<cap, t> -> @then:(@_:KV<cap, t> -> @_:List<&2, F32> -> R) -> R

def attention source · line 887 · raw

@+c:Cfg -> @+d:Nat -> @-cap:Nat -> @+t:Nat -> @qs:List<&2, Head> -> @kv:KV<cap, 1n+t> -> KA<cap, 1n+t>

attention of the (rotated) query heads at position t over the cache of positions 0..t (the newest included). d is the model's parallel depth (depth_in): with d = 0 (one thread) the key/value heads run one after another, since a fork tree on one thread is only overhead (+5% on the 1.7B, NOTES 2026-10-12); otherwise they run in parallel, one group per leaf. Both orders compute every head the same way, so the bits are the same.

def BR.then source · line 914 · raw

@-R:Type -> @-c:Cfg -> @-cap:Nat -> @-t:Nat -> @b:BR<c, cap, t> -> @k:(@_:List<&2, F32> -> @_:KV<cap, 1n+t> -> @_:LW<c> -> @_:Act -> R) -> R

continues with the fields of a layer's result (a computed value cannot be matched; a loop over positions recurses inside k)

def mv.go source · line 919 · raw

@-B:Type -> @-r:Nat -> @-k:Nat -> @bl:0x5c5507d77be847e1e454ec33ab54e087/main.BL<r, k> -> @then:(@_:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, k> -> @_:List<&2, F32> -> B) -> B

def mv source · line 925 · raw

@-B:Type -> @+r:Nat -> @+k:Nat -> @w:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, k> -> @+x:List<&2, F32> -> @then:(@_:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, k> -> @_:List<&2, F32> -> B) -> B

y = W · x, then then with W handed back and y (an Array has one owner, so a product returns its matrix)

def block source · line 930 · raw

@+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+rt:Rot -> @+x:List<&2, F32> -> BR<c, cap, t>

the layer at position t (0-based) on input x, with the cache of positions 0..t-1 and the rotation of t; d is the parallel depth of attention (see attention)

def block_gen source · line 953 · raw

@+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+rt:Rot -> @+x:List<&2, F32> -> BR<c, cap, t>

block for generation: the same computation, with the activations left empty. Generation never reads them, and building them (the copies of the rotated heads among them) cost about a fifth of each token on 16 threads (found by Mathews Fernando's bend-lint, unused_result). It is a copy of block's body, not a flag in it: a flag that chooses after the computation keeps every intermediate alive to the end, and that measured slower than building them.

def block_k source · line 973 · raw

@keep:Bool -> @+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+rt:Rot -> @+x:List<&2, F32> -> BR<c, cap, t>

keep: block with the activations (the tests read them), or block_gen without them; chosen before anything is computed

def run source · line 993 · raw

@+keep:Bool -> @+c:Cfg -> @+d:Nat -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+rt:Rot -> @ls:List<&1, LW<c>> -> @cs:List<&1, KV<cap, t>> -> @al:List<&1, LW<c>> -> @ac:List<&1, KV<cap, 1n+t>> -> @aa:List<&2, Act> -> @pend:BR<c, cap, t> -> Run<c, cap, t>

pend is the previous layer's result: its output is the next layer's input

def layers source · line 1000 · raw

@+keep:Bool -> @+c:Cfg -> @+d:Nat -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+rt:Rot -> @ls:List<&1, LW<c>> -> @cs:List<&1, KV<cap, t>> -> @+x:List<&2, F32> -> Run<c, cap, t>

def Out.then source · line 1013 · raw

@-R:Type -> @-c:Cfg -> @-cap:Nat -> @-t:Nat -> @o:Out<c, cap, t> -> @k:(@_:List<&2, F32> -> @_:List<&2, F32> -> @_:List<&2, Act> -> @_:List<&1, KV<cap, 1n+t>> -> @_:Model<c> -> R) -> R

continues with the fields of a position's result: logits, final norm, activations, caches, model

def head_of source · line 1019 · raw

@+c:Cfg -> @-cap:Nat -> @-t:Nat -> @+d:Nat -> @emb:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<Cfg.vocab(c), Cfg.dim(c)> -> @+lnf:List<&2, F32> -> @r:Run<c, cap, t> -> Out<c, cap, t>

the final norm, then logits = emb · nf (the LM head is the embedding matrix)

def forward.go source · line 1025 · raw

@+keep:Bool -> @+c:Cfg -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+d:Nat -> @ls:List<&1, LW<c>> -> @+lnf:List<&2, F32> -> @cs:List<&1, KV<cap, t>> -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.BRow<Cfg.vocab(c), Cfg.dim(c)> -> Out<c, cap, t>

def forward_k source · line 1032 · raw

@+keep:Bool -> @+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>

token tok at position t (0-based), with the caches of positions 0..t-1 (one per layer) and room for cap positions; room is the proof that t < cap; keep: whether the result carries every layer's activations

def forward source · line 1038 · raw

@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>

a position with every layer's activations (the tests read them)

def forward_gen source · line 1046 · raw

@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>

a position for generation: the same logits, caches and model, with the activations left empty, since generation never reads them (about a fifth faster per token on 16 threads, llama.bend NOTES, 2026-10-16). One band (d = 0) kept them until 2026-11-12, since leaving them out had measured 4-11% slower on one thread with Bend 2.0.37. On 2.0.40 it measures from the same to 4% faster on an idle machine, and 10 to 16% faster while the desktop is redrawing (NOTES, 2026-11-12)

def pre_of source · line 1055 · raw

@-c:Cfg -> @-cap:Nat -> @-t:Nat -> @+d:Nat -> @emb:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<Cfg.vocab(c), Cfg.dim(c)> -> @+lnf:List<&2, F32> -> @r:Run<c, cap, t> -> Out<c, cap, t>

a prompt position, whose logits nobody reads: the layers and the caches as forward_gen gives them (the activations empty on one band too since 2026-11-12, as there: the same on an idle machine, 5 to 12% off the time to the first token while the desktop is redrawing), without the final norm and the product by the embedding (vocab x dim, the largest of a position), so lg and nf are empty

def forward_pre.go source · line 1060 · raw

@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+d:Nat -> @ls:List<&1, LW<c>> -> @+lnf:List<&2, F32> -> @cs:List<&1, KV<cap, t>> -> @r:0x5c5507d77be847e1e454ec33ab54e087/main.BRow<Cfg.vocab(c), Cfg.dim(c)> -> Out<c, cap, t>

def forward_pre source · line 1065 · raw

@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>

def caches source · line 1071 · raw

@+c:Cfg -> @n:Nat -> @+cap:Nat -> List<&1, KV<cap, 0n>>

n empty caches, one per layer, each with room for cap positions

def AM.i source · line 1082 · raw

@am:AM -> Nat

def AM.v source · line 1087 · raw

@am:AM -> F32

def gt_head source · line 1092 · raw

@xs:List<&2, F32> -> @+v:F32 -> Bool

def argmax.go source · line 1099 · raw

@xs:List<&2, F32> -> @g:Bool -> @+i:Nat -> @+bi:Nat -> @+bv:F32 -> AM

def argmax source · line 1108 · raw

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

def hash32 source · line 1125 · raw

@+x:U32 -> U32

Chris Wellons' lowbias32 hash (public domain): xor-shifts and two odd multipliers, a bijection on U32

def uniform source · line 1134 · raw

@+seed:U32 -> @+n:U32 -> F32

draw n of a seed, a uniform F32 in [0, 1): the top 24 bits of hash32(hash32(seed) + n), times 2^-24 (exact). Nothing is kept between draws, so a seed gives the same draws whatever ran before.

def Cand.i source · line 1141 · raw

@c:Cand -> Nat

def Cand.v source · line 1146 · raw

@c:Cand -> F32

def goes_before source · line 1152 · raw

@cs:List<&2, Cand> -> @+v:F32 -> Bool

whether a value v goes before the head of cs: the head is smaller (an equal one keeps its place, ahead of v)

def ins source · line 1160 · raw

@cs:List<&2, Cand> -> @g:Bool -> @+v:F32 -> @+i:Nat -> List<&2, Cand>

v (id i) into the candidates cs, largest first; g: whether it goes before the head

def last_v source · line 1170 · raw

@cs:List<&2, Cand> -> @+lo:F32 -> F32

the value of the last candidate (lo if there is none)

def enters source · line 1178 · raw

@xs:List<&2, F32> -> @room:Bool -> @+lo:F32 -> Bool

whether the head of xs enters the candidates: there is room, or it is above the smallest one (lo)

def topk.go source · line 1187 · raw

@xs:List<&2, F32> -> @g:Bool -> @+k:Nat -> @+i:Nat -> @+m:Nat -> @+lo:F32 -> @+cs:List<&2, Cand> -> List<&2, Cand>

the k largest of xs (the values from id i on) and of the candidates cs, largest first; cs holds m <= k of them, lo the smallest; g: whether the head of xs enters. One pass, and a value at or below lo costs one comparison.

def topk source · line 1199 · raw

@+k:Nat -> @+xs:List<&2, F32> -> List<&2, Cand>

def weight source · line 1203 · raw

@+v:F32 -> @+temp:F32 -> @+top:F32 -> F32

the weight of a logit v: exp(v/temp - top/temp), top the largest logit (its weight is 1)

def total source · line 1207 · raw

@cs:List<&2, Cand> -> @+temp:F32 -> @+top:F32 -> @+acc:F32 -> F32

acc plus the weights of cs, in order

def kept.go source · line 1219 · raw

@cs:List<&2, Cand> -> @g:Bool -> @+temp:F32 -> @+top:F32 -> @+lim:F32 -> @+n:Nat -> @+acc:F32 -> Kept

g: whether the head of cs is kept (the weight acc before it is below lim = p·Z; the first is always kept)

def stops source · line 1231 · raw

@cs:List<&2, Cand> -> @+q:Nat -> @+temp:F32 -> @+top:F32 -> @+target:F32 -> @+acc:F32 -> Bool

whether the draw stops at the head of cs: it is the last kept one (q = 0 kept after it), or the cumulative weight acc plus its own is above the target u·Zp

def pick.go source · line 1239 · raw

@cs:List<&2, Cand> -> @s:Bool -> @+q:Nat -> @+temp:F32 -> @+top:F32 -> @+target:F32 -> @+acc:F32 -> Cand

the candidate drawn; s: whether it is the head of cs

def sample.pick source · line 1249 · raw

@+cs:List<&2, Cand> -> @kp:Kept -> @+temp:F32 -> @+top:F32 -> @+u:F32 -> Cand

def sample.cs source · line 1254 · raw

@+cs:List<&2, Cand> -> @+temp:F32 -> @+top:F32 -> @+p:F32 -> @+u:F32 -> Cand

def top_of source · line 1257 · raw

@cs:List<&2, Cand> -> F32

def sample source · line 1266 · raw

@+xs:List<&2, F32> -> @+temp:F32 -> @+k:Nat -> @+p:F32 -> @+u:F32 -> Cand

a token drawn from the logits xs (its id and logit) with temperature temp > 0, the k > 0 largest logits and top-p p, by the uniform draw u

def below source · line 1273 · raw

@cs:List<&2, Cand> -> @+n:Nat -> Bool

every candidate's id is below n

def and_l source · line 1282 · raw

@+a:Bool -> @-b:Bool -> @e:{True{} == Bool.and(a, b) : Bool} -> {True{} == a : Bool}

the two halves of a true Bool.and

def and_r source · line 1289 · raw

@+a:Bool -> @+b:Bool -> @e:{True{} == Bool.and(a, b) : Bool} -> {True{} == b : Bool}

def sample.pick_below source · line 1390 · raw

@+cs:List<&2, Cand> -> @kp:Kept -> @+temp:F32 -> @+top:F32 -> @+u:F32 -> @+n:Nat -> @e:{True{} == below(cs, n) : Bool} -> @pos:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n, n) : Bool} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(1n+Cand.i(sample.pick(cs, kp, temp, top, u)), n) : Bool}

def bitcast source · line 1423 · raw

@u:U32 -> F32

4 little-endian bytes -> F32 (U32 and F32 hold the same Word(32n): it is a bitcast)

def f32_le source · line 1428 · raw

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

def f32s source · line 1431 · raw

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

def rf_loop source · line 1439 · raw

@k:Nat -> @+off:U32 -> @+blk:U32 -> @m:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> @acc:List<&2, List<&2, F32>> -> IO(List<&2, F32>)

reads the file in blocks of blk bytes; k blocks remaining; m is the block already read

def blocks_of source · line 1451 · raw

@n:Nat -> @+per:Nat -> Nat

def read_floats source · line 1455 · raw

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

n F32 numbers (little-endian) from a file (the tests' inputs; the weights are BF16, below)

def depth_sel source · line 1469 · raw

@le1:Bool -> @le2:Bool -> @wide:Bool -> @is4:Bool -> @le8:Bool -> Nat

the weight matrices are kept in 2^d row bands (TA.Bands); each matrix · vector product runs its bands in parallel without copying the weights (bend-ml NOTES exp. 11). One thread gets d = 0: a single band, which computes what the sequential Mat.matmul_nt computes (bend-ml exp. 14). Two threads get two bands. From three threads on, a model at least 1024 wide gets 16 bands; a narrower one, whose bands are small next to a fork, gets 4 bands on four threads, 8 on three and on five to eight, and 16 beyond. Until 2026-11-12 it was one band up to two threads and 16 above, so two threads ran on one core (NOTES, 2026-11-12). The bits are the same at every depth. depth_sel: whether the thread count is at most 1, at most 2, 4, and at most 8, and whether the model is wide

def depth_for source · line 1485 · raw

@+threads:U32 -> Nat

the depth for a model narrower than 1024

def depth_in source · line 1489 · raw

@+c:Cfg -> @+threads:U32 -> Nat

the depth for the model of c

def bf16_le source · line 1495 · raw

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

2 little-endian bytes of a BF16 number -> the F32 whose upper 16 bits they are (exact)

def bf16s source · line 1498 · raw

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

def fill_bf16 source · line 1507 · raw

@bs:List<&2, U32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>

writes the BF16 numbers held in bs (2 bytes each), widened to F32, at index i, without building a list of F32 first

def fill_bf16_mat source · line 1514 · raw

@-r:Nat -> @-c:Nat -> @+i:U32 -> @bs:List<&2, U32> -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> 0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>

def stream_exact source · line 1521 · raw

@k:Nat -> @-r:Nat -> @-c:Nat -> @+rem:Nat -> @+off:U32 -> @+idx:U32 -> @m:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> @acc:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> IO(0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>)

reads blocks of at most 1 MB (2^19 numbers) until rem bytes have been read, so a band never takes numbers that belong to the next one

def get_band source · line 1535 · raw

@path:String -> @-r:Nat -> @+rr:Nat -> @+c:Nat -> @+lo:U32 -> @+start:Nat -> IO(0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c>)

rr x c numbers from number start of the tensor at byte lo, into a band of r rows (rr == r, see load_tree; the Array is sized for rr * c numbers as Mat.zeros would size it)

def load_tree source · line 1545 · raw

@+c:Nat -> @-r:Nat -> @+d:Nat -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+path:String -> @+lo:U32 -> @+start:Nat -> IO(0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c>)

fills a tree of 2^d bands (TA.Bands.zeros's shape: each node halves its rows with TA.half) in row order; rr is r at run time. The tree is walked by its depth, not built first with TA.Bands.zeros and matched: that tree's zero-filled arrays were dropped unread (NOTES.md, 2026-11-01)

def span_cmp source · line 1559 · raw

@ok:Bool -> @+path:String -> @+t:Nat -> IO(Unit)

def span_ok source · line 1566 · raw

@m:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> @+hi:U32 -> @+lo:U32 -> @+end:U32 -> @+path:String -> @+t:Nat -> IO(Unit)

def check_span source · line 1575 · raw

@+path:String -> @+hi:U32 -> @+lo:U32 -> @+n:Nat -> @+t:Nat -> IO(Unit)

stops the program unless tensor t, n BF16 numbers from byte hi * 2^32 + lo, ends inside the file with hi = 0

def rb_done source · line 1583 · raw

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

def read_bytes source · line 1590 · raw

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

def digit_head source · line 1596 · raw

@xs:List<&2, U32> -> Bool

def u32s.go source · line 1604 · raw

@bs:List<&2, U32> -> @dg:Bool -> @+cur:U32 -> @have:Bool -> @acc:List<&2, U32> -> List<&2, U32>

the decimal numbers in bs, separated by anything that is not a digit; dg says whether bs starts with a digit

def u32s source · line 1617 · raw

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

def cfg_of source · line 1620 · raw

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

def read_cfg source · line 1628 · raw

@+dir:String -> IO(Cfg)

the config of the model in dir (written by reference/prep.py)

def st_path source · line 1635 · raw

@+dir:String -> String

def st_nth source · line 1638 · raw

@xs:List<&2, U32> -> @k:Nat -> U32

def t_layer source · line 1649 · raw

@+i:Nat -> @+j:Nat -> Nat

the tensors in w/st.txt's order (reference/prep.py's weights()): emb, then layer i's ln1 q k v o ln2 gate up down (j = 0..8), then lnf

def st_check source · line 1652 · raw

@ok:Bool -> @+dir:String -> @xs:List<&2, U32> -> IO(List<&2, U32>)

def read_st source · line 1660 · raw

@+dir:String -> @+layers:Nat -> IO(List<&2, U32>)

w/st.txt's numbers, two for each of the 2 + 9 * layers tensors

def st_hi source · line 1666 · raw

@+st:List<&2, U32> -> @+t:Nat -> U32

def st_lo source · line 1669 · raw

@+st:List<&2, U32> -> @+t:Nat -> U32

def get_bands source · line 1673 · raw

@+dir:String -> @+st:List<&2, U32> -> @+t:Nat -> @+d:Nat -> @+r:Nat -> @+c:Nat -> IO(0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c>)

tensor t (w/st.txt's order) as bands of depth d

def get_norm source · line 1679 · raw

@+dir:String -> @+st:List<&2, U32> -> @+t:Nat -> @+n:Nat -> IO(List<&2, F32>)

a norm's n weights as a list (n * 2 bytes, read at once)

def load_lw_st source · line 1688 · raw

@+c:Cfg -> @+dir:String -> @+st:List<&2, U32> -> @+i:Nat -> @+d:Nat -> IO(LW<c>)

one layer's weights (tensors t_layer(i, 0..8)) from w/st.txt's numbers st; d: the band depth (depth_in)

def load_lw source · line 1702 · raw

@+c:Cfg -> @+dir:String -> @+i:Nat -> @+d:Nat -> IO(LW<c>)

layer i's weights alone (layer_test); d: the band depth (depth_in)

def load_layers source · line 1707 · raw

@k:Nat -> @+c:Cfg -> @+dir:String -> @+st:List<&2, U32> -> @+d:Nat -> @+i:Nat -> @acc:List<&1, LW<c>> -> IO(List<&1, LW<c>>)

def load_model source · line 1717 · raw

@+c:Cfg -> @+dir:String -> IO(Model<c>)

the whole model; the embedding gets one more level of bands than the layers (2^5 with 16 threads)