main.bend checks
raw source on the hub · import bend-ml-llama@0.2.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).
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.14.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.
@a:Nat -> {Nat.add(a, 0n) == a : Nat}
law add_succ provedsource · line 40 · raw
@a:Nat -> @-b:Nat -> {1n+Nat.add(a, b) == Nat.add(a, 1n+b) : Nat}
law add_one provedsource · line 53 · 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.
@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 164 · 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 170 · 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 176 · 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 200 · 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 206 · raw
{Cfg.size(smollm2_360m) == 361821120 : U32}
law layout_1_7b provedsource · line 212 · raw
{Cfg.size(smollm2_1_7b) == 1711376384 : U32}
law head_split provedsource · line 262 · 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 511 · 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 527 · 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 565 · raw
@+cap:Nat -> @+h:Nat -> @+t:Nat -> @e:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(1n+t, cap) : Bool} -> {True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(Nat.mul(1n+t, h), 0x0f6479f09ada7ae614e64e31e8f5a730/main.pow2(0x0f6479f09ada7ae614e64e31e8f5a730/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 601 · 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 616 · 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 632 · 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 651 · 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 668 · 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 702 · raw
@k:Array<F32> -> @+m:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> {sr_len(sr_of(k, 0x0f6479f09ada7ae614e64e31e8f5a730/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 and reverses the list (rev_len counts it); with 1 + p left it reads one and recurses, and len_step moves the 1 out of the sum (tensor-array's leaf_len, for scores).
law causal provedsource · line 737 · 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.
Types
type Cfg source · line 86 · 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.
Cfg@kv:Nat -> @g:Nat -> @half:Nat -> @f:Nat -> @layers:Nat -> @vocab:Nat -> @eps:F32 -> @theta:F32 -> Cfg
type Head source · line 223 · raw
Data
a head as its two halves: RoPE rotates number i of the first half with number i of the second
Head@lo:List<&2, F32> -> @hi:List<&2, F32> -> Head
type Rot source · line 422 · raw
Data
the cosines and sines of position t, one per frequency (shared by every head of every layer)
Rot@cs:List<&2, F32> -> @sn:List<&2, F32> -> Rot
type KV source · line 475 · 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).
KV@-cap:Nat -> @-t:Nat -> @ks:List<&1, Array<F32>> -> @vs:List<&1, Array<F32>> -> KV<cap, t>
type SR source · line 686 · raw
Type
the scores of one query head against the keys of its key/value head, and the keys handed back
SR@k:Array<F32> -> @s:List<&2, F32> -> SR
type WR source · line 748 · raw
Type
the weighted sum of the values of positions 0..t: w (t + 1 weights) times V (t + 1 rows of head numbers)
WR@v:Array<F32> -> @o:List<&2, F32> -> WR
type HO source · line 765 · raw
Type
one query head: softmax(q·K · scale) · V, and the two arrays handed back
HO@k:Array<F32> -> @v:Array<F32> -> @o:List<&2, F32> -> HO
type AT source · line 797 · raw
Type
every group, in order: the outputs of query heads 0, 1, ..., and the caches handed back
AT@ks:List<&1, Array<F32>> -> @vs:List<&1, Array<F32>> -> @o:List<&2, F32> -> AT
type GW source · line 806 · raw
Type
one key/value head's work: its group of query heads, its keys and its values
GW@g:List<&2, Head> -> @k:Array<F32> -> @v:Array<F32> -> GW
type GS source · line 829 · 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)
GS@l:List<&1, GW> -> @r:List<&1, GW> -> GS
type KA source · line 868 · raw
@-cap:Nat -> @-t:Nat -> Type
the attention output and the cache handed back
KA@-cap:Nat -> @-t:Nat -> @kv:KV<cap, t> -> @o:List<&2, F32> -> KA<cap, t>
type LW source · line 897 · raw
@-c:Cfg -> Type
the weights of one layer, with their shapes from the config (row = output, as PyTorch stores them)
LW@-c:Cfg -> @ln1:List<&2, F32> -> @q:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.dim(c), Cfg.dim(c)> -> @k:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.kvw(c), Cfg.dim(c)> -> @v:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.kvw(c), Cfg.dim(c)> -> @o:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.dim(c), Cfg.dim(c)> -> @ln2:List<&2, F32> -> @gate:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.ffn(c), Cfg.dim(c)> -> @up:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.ffn(c), Cfg.dim(c)> -> @down:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.dim(c), Cfg.ffn(c)> -> LW<c>
type Act source · line 902 · raw
Data
what a layer computes at one position, every intermediate: the tests compare them with transformers, and a backward pass would read them
Act@n1:List<&2, F32> -> @q:List<&2, F32> -> @k:List<&2, F32> -> @v:List<&2, F32> -> @qr:List<&2, F32> -> @kr:List<&2, F32> -> @attn:List<&2, F32> -> @a:List<&2, F32> -> @n2:List<&2, F32> -> @m:List<&2, F32> -> @y:List<&2, F32> -> Act
type BR source · line 907 · 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
BR@-c:Cfg -> @-cap:Nat -> @-t:Nat -> @y:List<&2, F32> -> @kv:KV<cap, 1n+t> -> @lw:LW<c> -> @act:Act -> BR<c, cap, t>
type Model source · line 983 · raw
@-c:Cfg -> Type
emb: the embedding (vocab rows; also the LM head, tied); lnf: the final norm's gains
Model@-c:Cfg -> @d:Nat -> @emb:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<Cfg.vocab(c), Cfg.dim(c)> -> @layers:List<&1, LW<c>> -> @lnf:List<&2, F32> -> Model<c>
type Run source · line 987 · raw
@-c:Cfg -> @-cap:Nat -> @-t:Nat -> Type
the last layer's output, the layers and caches handed back, and every layer's activations
Run@-c:Cfg -> @-cap:Nat -> @-t:Nat -> @x:List<&2, F32> -> @layers:List<&1, LW<c>> -> @cs:List<&1, KV<cap, 1n+t>> -> @acts:List<&2, Act> -> Run<c, cap, t>
type Out source · line 1007 · 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
Out@-c:Cfg -> @-cap:Nat -> @-t:Nat -> @lg:List<&2, F32> -> @nf:List<&2, F32> -> @acts:List<&2, Act> -> @cs:List<&1, KV<cap, 1n+t>> -> @m:Model<c> -> Out<c, cap, t>
type AM source · line 1073 · raw
Data
the first index of the largest value (torch.argmax does the same), and the value
AM@i:Nat -> @v:F32 -> AM
Definitions
def head source · line 90 · raw
@+half:Nat -> Nat
a head: 2·half numbers
def Cfg.half source · line 93 · raw
@+c:Cfg -> Nat
def Cfg.kv source · line 98 · raw
@+c:Cfg -> Nat
def Cfg.g source · line 103 · raw
@+c:Cfg -> Nat
def Cfg.ffn source · line 108 · raw
@+c:Cfg -> Nat
def Cfg.layers source · line 113 · raw
@+c:Cfg -> Nat
def Cfg.vocab source · line 118 · raw
@+c:Cfg -> Nat
def Cfg.eps source · line 123 · raw
@+c:Cfg -> F32
def Cfg.theta source · line 128 · raw
@+c:Cfg -> F32
def Cfg.head source · line 134 · raw
@+c:Cfg -> Nat
the size of a head
def Cfg.heads source · line 138 · raw
@+c:Cfg -> Nat
the query heads: kv groups of g
def Cfg.dim source · line 142 · raw
@+c:Cfg -> Nat
the hidden size: one head per query head
def Cfg.kvw source · line 146 · raw
@+c:Cfg -> Nat
the width of the keys (and of the values): one head per key/value head
def smollm2_135m source · line 150 · raw
Cfg
SmolLM2-135M (HuggingFaceTB/SmolLM2-135M): 3 key/value heads of 3 query heads each, heads of 64
def smollm2_360m source · line 154 · raw
Cfg
SmolLM2-360M: 5 key/value heads of 3 query heads each, heads of 64
def smollm2_1_7b source · line 158 · 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 184 · raw
@+n:Nat -> U32
def Cfg.layer_size source · line 189 · 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 193 · 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 226 · raw
@+half:Nat -> @+xs:List<&2, F32> -> Head
def join source · line 229 · raw
@h:Head -> List<&2, F32>
def heads source · line 235 · 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 243 · 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 250 · raw
@hs:List<&2, Head> -> List<&2, List<&2, F32>>
def flat source · line 257 · raw
@hs:List<&2, Head> -> List<&2, F32>
def list.add source · line 287 · 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 295 · 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 303 · raw
@+k:F32 -> @xs:List<&2, F32> -> List<&2, F32>
k * xs
def list.sum.go source · line 311 · raw
@xs:List<&2, F32> -> @acc:F32 -> F32
acc + xs, a running sum from the left
def list.max.go source · line 318 · raw
@xs:List<&2, F32> -> @cur:F32 -> F32
def list.max source · line 326 · raw
@xs:List<&2, F32> -> F32
the maximum of xs (0 for an empty list)
def list.exp_shift source · line 334 · raw
@xs:List<&2, F32> -> @+m:F32 -> List<&2, F32>
exp(x - m) for each x
def list.div source · line 342 · raw
@xs:List<&2, F32> -> @+s:F32 -> List<&2, F32>
xs / s
def list.softmax.fin source · line 349 · raw
@+es:List<&2, F32> -> List<&2, F32>
def list.softmax source · line 353 · raw
@+xs:List<&2, F32> -> List<&2, F32>
stable softmax: subtracts the maximum before exponentiating
def squares source · line 357 · raw
@xs:List<&2, F32> -> List<&2, F32>
the squares of xs
def pair_sums source · line 365 · 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 374 · 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 386 · 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 390 · 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 397 · raw
@+c:Cfg -> List<&2, F32>
def angles source · line 400 · raw
@+t:F32 -> @fs:List<&2, F32> -> List<&2, F32>
def cos.l source · line 407 · raw
@xs:List<&2, F32> -> List<&2, F32>
def sin.l source · line 414 · raw
@xs:List<&2, F32> -> List<&2, F32>
def rot_at source · line 425 · raw
@+c:Cfg -> @+t:Nat -> Rot
def rot_lo source · line 429 · 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 436 · raw
@h1:List<&2, F32> -> @h2:List<&2, F32> -> @cs:List<&2, F32> -> @sn:List<&2, F32> -> List<&2, F32>
def rope source · line 443 · raw
@rt:Rot -> @h:Head -> Head
def ropes source · line 448 · raw
@+rt:Rot -> @hs:List<&2, Head> -> List<&2, Head>
def silu source · line 456 · raw
@+x:F32 -> F32
silu(x) = x / (1 + exp(-x)), the form ATen's CPU kernel uses
def swiglu source · line 460 · raw
@g:List<&2, F32> -> @u:List<&2, F32> -> List<&2, F32>
silu(gate) * up
def arrays source · line 479 · raw
@n:Nat -> @+d:Nat -> List<&1, Array<F32>>
n arrays of 2^d slots
def KV.empty source · line 487 · 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 491 · 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 499 · raw
@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 505 · raw
@-cap:Nat -> @-t:Nat -> @kv:KV<cap, t> -> Nat
the number of key/value heads a cache holds
def leb_trans source · line 536 · raw
@a:Nat -> @b:Nat -> @c:Nat -> @e1:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(a, b) : Bool} -> @e2:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(b, c) : Bool} -> {True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(a, c) : Bool}a <= b and b <= c -> a <= c
def leb_mul source · line 550 · raw
@a:Nat -> @b:Nat -> @+h:Nat -> @e:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(a, b) : Bool} -> {True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(Nat.mul(a, h), Nat.mul(b, h)) : Bool}a <= b -> a·h <= b·h
def step_room source · line 574 · raw
@+t:Nat -> @+p:Nat -> @-cap:Nat -> @e:{Nat.add(t, 1n+p) == cap : Nat} -> {True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 579 · 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 592 · 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 689 · raw
@sr:SR -> Nat
def sr_of source · line 694 · raw
@k:Array<F32> -> @al:0x0f6479f09ada7ae614e64e31e8f5a730/main.AL -> SR
def sr_read source · line 718 · raw
@+t:Nat -> @g:0x0f6479f09ada7ae614e64e31e8f5a730/main.G -> SR
read the 1 + t scores the gemm wrote
def scores source · line 725 · 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 728 · raw
@+t:Nat -> @g:0x0f6479f09ada7ae614e64e31e8f5a730/main.G -> {sr_len(sr_read(t, g)) == 1n+t : Nat}
def wr_of source · line 751 · raw
@v:Array<F32> -> @al:0x0f6479f09ada7ae614e64e31e8f5a730/main.AL -> WR
def wr_read source · line 756 · raw
@+hd:Nat -> @g:0x0f6479f09ada7ae614e64e31e8f5a730/main.G -> WR
def weighted source · line 761 · raw
@+hd:Nat -> @+t:Nat -> @v:Array<F32> -> @+w:List<&2, F32> -> WR
def HO.then source · line 768 · raw
@-R:Type -> @h:HO -> @then:(@_:Array<F32> -> @_:Array<F32> -> @_:List<&2, F32> -> R) -> R
def head_wr source · line 773 · raw
@k:Array<F32> -> @wr:WR -> HO
def head_sr source · line 778 · raw
@+hd:Nat -> @+t:Nat -> @+scale:F32 -> @v:Array<F32> -> @sr:SR -> HO
def head_att source · line 783 · raw
@+hd:Nat -> @+t:Nat -> @+scale:F32 -> @k:Array<F32> -> @v:Array<F32> -> @+q:List<&2, F32> -> HO
def group_att source · line 787 · 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 800 · raw
@p:AT -> @q:AT -> AT
def gws source · line 809 · 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 817 · 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 832 · raw
@n:Nat -> @xs:List<&1, GW> -> @acc:List<&1, GW> -> GS
def halvings source · line 840 · 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 857 · 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 871 · raw
@-cap:Nat -> @-t:Nat -> @at:AT -> KA<cap, t>
def KA.then source · line 876 · raw
@-R:Type -> @-cap:Nat -> @-t:Nat -> @ka:KA<cap, t> -> @then:(@_:KV<cap, t> -> @_:List<&2, F32> -> R) -> R
def attention source · line 885 · 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_for): with d = 0 (two threads or fewer) 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 912 · 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 917 · raw
@-B:Type -> @-r:Nat -> @-k:Nat -> @bl:0x0f6479f09ada7ae614e64e31e8f5a730/main.BL<r, k> -> @then:(@_:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<r, k> -> @_:List<&2, F32> -> B) -> B
def mv source · line 923 · raw
@-B:Type -> @+r:Nat -> @+k:Nat -> @w:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<r, k> -> @+x:List<&2, F32> -> @then:(@_:0x0f6479f09ada7ae614e64e31e8f5a730/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 928 · raw
@+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 951 · raw
@+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 971 · raw
@keep:Bool -> @+c:Cfg -> @+d:Nat -> @lw:LW<c> -> @-cap:Nat -> @+t:Nat -> @kv:KV<cap, t> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 991 · raw
@+keep:Bool -> @+c:Cfg -> @+d:Nat -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 998 · raw
@+keep:Bool -> @+c:Cfg -> @+d:Nat -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 1011 · 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 1017 · raw
@+c:Cfg -> @-cap:Nat -> @-t:Nat -> @+d:Nat -> @emb:0x0f6479f09ada7ae614e64e31e8f5a730/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 1023 · raw
@+keep:Bool -> @+c:Cfg -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(1n+t, cap) : Bool} -> @+d:Nat -> @ls:List<&1, LW<c>> -> @+lnf:List<&2, F32> -> @cs:List<&1, KV<cap, t>> -> @r:0x0f6479f09ada7ae614e64e31e8f5a730/main.BRow<Cfg.vocab(c), Cfg.dim(c)> -> Out<c, cap, t>
def forward_k source · line 1030 · raw
@+keep:Bool -> @+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 1036 · raw
@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/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 1042 · raw
@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>a position for generation: the same logits, caches and model. With bands (d > 0, more than two threads) the activations are left empty, about a fifth faster per token on 16 threads; with one band (d = 0) they are kept, since leaving them out measured 4-11% slower per token on one thread (llama.bend NOTES, 2026-10-16), for no reason found yet
def pre_of source · line 1049 · raw
@-c:Cfg -> @-cap:Nat -> @-t:Nat -> @+d:Nat -> @emb:0x0f6479f09ada7ae614e64e31e8f5a730/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, 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 1054 · raw
@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(1n+t, cap) : Bool} -> @+d:Nat -> @ls:List<&1, LW<c>> -> @+lnf:List<&2, F32> -> @cs:List<&1, KV<cap, t>> -> @r:0x0f6479f09ada7ae614e64e31e8f5a730/main.BRow<Cfg.vocab(c), Cfg.dim(c)> -> Out<c, cap, t>
def forward_pre source · line 1059 · raw
@+c:Cfg -> @-cap:Nat -> @+t:Nat -> @m:Model<c> -> @cs:List<&1, KV<cap, t>> -> @-room:{True{} == 0x0f6479f09ada7ae614e64e31e8f5a730/main.leb(1n+t, cap) : Bool} -> @+tok:Nat -> Out<c, cap, t>
def caches source · line 1065 · 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 1076 · raw
@am:AM -> Nat
def AM.v source · line 1081 · raw
@am:AM -> F32
def gt_head source · line 1086 · raw
@xs:List<&2, F32> -> @+v:F32 -> Bool
def argmax.go source · line 1093 · raw
@xs:List<&2, F32> -> @g:Bool -> @+i:Nat -> @+bi:Nat -> @+bv:F32 -> AM
def argmax source · line 1102 · raw
@xs:List<&2, F32> -> AM
def bitcast source · line 1115 · 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 1120 · raw
@+b0:U32 -> @+b1:U32 -> @+b2:U32 -> @+b3:U32 -> F32
def f32s source · line 1123 · raw
@bs:List<&2, U32> -> List<&2, F32>
def rf_loop source · line 1131 · 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 1143 · raw
@n:Nat -> @+per:Nat -> Nat
def read_floats source · line 1147 · raw
@path:String -> @+n:Nat -> IO(List<&2, F32>)
n F32 numbers (little-endian) from a file
def depth_sel source · line 1157 · raw
@few: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). With 2 threads or fewer the bands cannot help, so d = 0: a single band, which computes what the sequential Mat.matmul_nt computes (bend-ml exp. 14).
def depth_for source · line 1164 · raw
@+threads:U32 -> Nat
def nm source · line 1167 · raw
@+i:Nat -> @f:String -> String
def size_cmp source · line 1172 · raw
@ok:Bool -> @+path:String -> @+got:U32 -> @+want:Nat -> IO(Unit)
def size_ok source · line 1179 · raw
@m:Pair(File, Result<&1, &1, Pair(U32, String), U32>) -> @+want:Nat -> @+path:String -> IO(Unit)
def check_size source · line 1188 · raw
@+path:String -> @+n:Nat -> IO(Unit)
stops the program unless the file holds exactly n F32 numbers (n * 4 bytes)
def wpath source · line 1194 · raw
@dir:String -> @name:String -> String
def get_list source · line 1197 · raw
@+dir:String -> @+name:String -> @+n:Nat -> IO(List<&2, F32>)
def fill_bytes source · line 1208 · raw
@bs:List<&2, U32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>
writes the little-endian F32 numbers held in bs (4 bytes each) at index i, without building a list of F32 first
def fill_bytes_mat source · line 1215 · raw
@-r:Nat -> @-c:Nat -> @+i:U32 -> @bs:List<&2, U32> -> @m:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> 0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>
def stream_exact source · line 1222 · 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:0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c> -> IO(0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>)
reads blocks of at most 1 MB until rem bytes have been read, so a band never takes numbers that belong to the next one
def get_band source · line 1236 · raw
@path:String -> @-r:Nat -> @+rr:Nat -> @+c:Nat -> @+start:Nat -> IO(0x0f6479f09ada7ae614e64e31e8f5a730/main.Mat<r, c>)
rr x c numbers starting at number start of the file, 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 1243 · raw
@+c:Nat -> @-r:Nat -> @b:0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+path:String -> @+start:Nat -> IO(0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<r, c>)fills the bands of b (whose shape comes from TA.Bands.zeros) in row order; rr is r at run time
def get_bands source · line 1255 · raw
@+dir:String -> @+name:String -> @+d:Nat -> @+r:Nat -> @+c:Nat -> IO(0x0f6479f09ada7ae614e64e31e8f5a730/main.Bands<r, c>)
def load_lw source · line 1261 · raw
@+c:Cfg -> @+dir:String -> @+i:Nat -> @+d:Nat -> IO(LW<c>)
one layer's weights; d: the band depth (depth_for)
def load_layers source · line 1274 · raw
@k:Nat -> @+c:Cfg -> @+dir:String -> @+d:Nat -> @+i:Nat -> @acc:List<&1, LW<c>> -> IO(List<&1, LW<c>>)
def load_model source · line 1284 · 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)
def rb_done source · line 1295 · raw
@m:Pair(File, Result<&1, &1, Pair(U32, String), List<&2, U32>>) -> IO(List<&2, U32>)
def read_bytes source · line 1302 · raw
@path:String -> IO(List<&2, U32>)
def digit_head source · line 1308 · raw
@xs:List<&2, U32> -> Bool
def u32s.go source · line 1316 · 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 1329 · raw
@+bs:List<&2, U32> -> List<&2, U32>
def cfg_of source · line 1332 · raw
@+path:String -> @xs:List<&2, U32> -> IO(Cfg)
def read_cfg source · line 1340 · raw
@+dir:String -> IO(Cfg)
the config of the model in dir (written by reference/prep.py)