~/bend-docscommunity

main.bend checks

raw source on the hub · import bend-ml-tensor-array@0.1.15.0/main.bend as Main

bend-ml-tensor-array: tensors over a flat Array<F32>, with the shape in the type.

import bend-ml-tensor-array@0.1.15.0/main.bend as TA

Mat<r, c> stores r*c numbers in an Array<F32>, row by row (index i*c + j). The dimensions are erased type parameters: the same guarantee as bend-ml-tensor (a product with wrong dimensions does not compile), but ~50x faster than lists: Array.get/set by index, and products in parallel blocks of rows.

An Array is affine (a single owner): every operation that reads an operand returns it together with the result (as Array.get does), in records MM, AR, ... Use Mat.clone when you need two copies.

What is NOT proved: the invariant "the Array has capacity >= r*c" holds because the constructors (Mat.zeros, Mat.of_list) allocate the right size; and F32 is validated by tests against PyTorch (reference/test_tensor_array.py), not by proof.

1 import
import Base

Laws

law cap_ok provedsource · line 627 · raw

@+n:Nat -> {True{} == leb(n, pow2(cap_depth(n))) : Bool}

LAW (cap_ok): an Array with 2^cap_depth(n) slots always has room for n elements.

law half_cover provedsource · line 1513 · raw

@+n:Nat -> {n == Nat.add(half(n), Nat.sub(n, half(n))) : Nat}

LAW (half_cover): the two bands of a node, half(r) rows and r - half(r) rows, add up to exactly r rows: no row is lost and none is counted twice.

law leaf_len provedsource · line 1767 · raw

@-r:Nat -> @-c:Nat -> @w:Array<F32> -> @+mleft:Nat -> @+idx:U32 -> @+acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> {bl_len(r, c, bv_g2(r, c, w, read_l(mleft, idx, acc, rc))) == Nat.add(1n+mleft, List.length(&2, F32, acc)) : Nat}

a leaf's list: read_l reads mleft + 1 numbers onto acc and reverses them. By induction on mleft: with 0 left it reads one number (rev_len counts the reversed list); with 1 + p left it reads one and recurses, and len_step moves the 1 out of the sum.

law band_len provedsource · line 1793 · raw

@-r:Nat -> @+c:Nat -> @+rows:Nat -> @x:Array<F32> -> @w:Array<F32> -> {bl_len(r, c, bv_gemm(r, c, rows, rows, x, w)) == rows : Nat}

LAW (band_len): the product of one band with rows rows gives exactly rows numbers. 0 rows give the empty list; 1 + p rows go through the gemm, and g_len counts what is read back.

Types

type R source · line 24 · raw

Type

type G source · line 27 · raw

Type

type S3 source · line 90 · raw

Type

type P2 source · line 205 · raw

Type

type AL source · line 208 · raw

Type

type L2 source · line 347 · raw

Type

type Mat source · line 360 · raw

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

type CR source · line 372 · raw

Type

type ML source · line 685 · raw

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

type MM2 source · line 700 · raw

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

type MMul source · line 717 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> Type

type MMulNT source · line 722 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> Type

type MMulTN source · line 727 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> Type

type T8 source · line 741 · raw

Data

type PK source · line 754 · raw

Type

type TD source · line 826 · raw

Type

type TR source · line 849 · raw

Type

type B2 source · line 862 · raw

Type

two cells of C at once: row i against columns j and j + 1 (cells ib and ic of b), each A cell read once for both; each dot keeps t8_dot's order, so the two cells are t8_cell's bits

type TD2 source · line 878 · raw

Type

type TR2 source · line 888 · raw

Type

type TG source · line 899 · raw

Type

type TL source · line 930 · raw

Type

rows i0 .. i0+cnt-1 of C as a list, in order (the parallel version's leaves)

type PKK source · line 965 · raw

Type

type PT source · line 986 · raw

Type

type PKT source · line 990 · raw

Type

type CB source · line 1027 · raw

Data

a block of C's columns j0 .. j0+w-1 for every row, row by row

type RW source · line 1040 · raw

Type

type SL source · line 1211 · raw

Type

type MBias source · line 1263 · raw

@-n:Nat -> @-m:Nat -> Type

type MPair source · line 1278 · raw

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

type MColSum source · line 1317 · raw

@-n:Nat -> @-m:Nat -> Type

type MCE source · line 1341 · raw

@-n:Nat -> @-c:Nat -> Type

type MHits source · line 1356 · raw

@-n:Nat -> @-c:Nat -> Type

type MRow source · line 1371 · raw

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

type Bands source · line 1520 · raw

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

type BV source · line 1592 · raw

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

B (intact) and y = B · x, typed like Mat.matmul_nt: x is 1 x c, y is 1 x r

type BL source · line 1596 · raw

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

the bands and y as a list in row order (the result of Bands.matvec_l)

type BRow source · line 1661 · raw

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

Definitions

def dotr source · line 35 · raw

@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @a0:F32 -> @a1:F32 -> @a2:F32 -> @a3:F32 -> @a4:F32 -> @a5:F32 -> @a6:F32 -> @a7:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> R

A dot product with eight partial sums (0.1.15.0; four before). Each step adds its product to a0 and rotates the sums (a1, ..., a7, a0), so each sum gets every eighth product and the processor can work on the next additions before the last one has finished; at the end the sums are added as a balanced tree of pairs. The order of the additions is fixed, so the result is the same on every run and every thread count, but it is not the order of a single running sum (NOTES.md, exp. 17 and 2026-10-25). The cells of 8 below keep exactly this order: sum q is lane q of every cell.

def dotg source · line 42 · raw

@k:Nat -> @+ia:U32 -> @+ib:U32 -> @+sa:U32 -> @+sb:U32 -> @acc:F32 -> @ra:Pair(Array<F32>, F32) -> @rb:Pair(Array<F32>, F32) -> R

def cwr_add source · line 48 · raw

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

def cwr source · line 53 · raw

@ac:Bool -> @+idx:U32 -> @+x:F32 -> @c:Array<F32> -> Array<F32>

def gcols source · line 62 · raw

@+ac:Bool -> @mleft:Nat -> @+kn:Nat -> @+ia0:U32 -> @+ib0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @+cidx:U32 -> @c:Array<F32> -> @r:R -> G

def grows source · line 69 · raw

@+ac:Bool -> @nleft:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @+mu:U32 -> @g:G -> G

def gemm_ac source · line 78 · raw

@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def gemm source · line 81 · raw

@+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def lcols source · line 93 · raw

@mleft:Nat -> @+kn:Nat -> @+ia0:U32 -> @+ib0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @out:List<&2, F32> -> @r:R -> S3

def lrows source · line 100 · raw

@nleft:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @st:S3 -> S3

def lblock_fin source · line 107 · raw

@st:S3 -> List<&2, F32>

def lblock source · line 112 · raw

@cnt:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>

def lpar source · line 115 · raw

@d:Nat -> @+cnt:Nat -> @+mm1:Nat -> @+kn:Nat -> @+i:U32 -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @ra:Pair(Array<F32>, Array<F32>) -> @rb:Pair(Array<F32>, Array<F32>) -> List<&2, F32>

def write_l source · line 123 · raw

@xs:List<&2, F32> -> @+idx:U32 -> @c:Array<F32> -> Array<F32>

def write_la source · line 132 · raw

@+ac:Bool -> @xs:List<&2, F32> -> @+idx:U32 -> @c:Array<F32> -> Array<F32>

def gp3 source · line 139 · raw

@+ac:Bool -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> @xs:List<&2, F32> -> G

def gp2 source · line 142 · raw

@d:Nat -> @+n:Nat -> @+mm1:Nat -> @+kn:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @c:Array<F32> -> @pa:Pair(Array<F32>, Array<F32>) -> @pb:Pair(Array<F32>, Array<F32>) -> G

def gemm_par source · line 149 · raw

@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def cblock_fin source · line 158 · raw

@st:S3 -> List<&2, F32>

def cblock_go source · line 165 · raw

@cnt1:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>

def cblock source · line 168 · raw

@cnt:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> List<&2, F32>

def cpar source · line 175 · raw

@d:Nat -> @+cnt:Nat -> @+kn:Nat -> @+ia0:U32 -> @+j0:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @ra:Pair(Array<F32>, Array<F32>) -> @rb:Pair(Array<F32>, Array<F32>) -> List<&2, F32>

def cp2 source · line 183 · raw

@+ac:Bool -> @d:Nat -> @+m:Nat -> @+kn:Nat -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @c:Array<F32> -> @pa:Pair(Array<F32>, Array<F32>) -> @pb:Pair(Array<F32>, Array<F32>) -> G

def gemm_par_cols source · line 191 · raw

@+ac:Bool -> @d:Nat -> @+m:Nat -> @+k:Nat -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def read_l source · line 213 · raw

@mleft:Nat -> @+idx:U32 -> @acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> AL

def addl source · line 222 · raw

@xs:List<&2, F32> -> @+idx:U32 -> @rc:Pair(Array<F32>, F32) -> Array<F32>

def bias_rows source · line 231 · raw

@nleft:Nat -> @+i:U32 -> @+mu:U32 -> @+bl:List<&2, F32> -> @c:Array<F32> -> Array<F32>

def relu_l source · line 240 · raw

@nleft:Nat -> @+t:U32 -> @rc:Pair(Array<F32>, F32) -> Array<F32>

def pos2 source · line 247 · raw

@b:Bool -> F32

def pos source · line 254 · raw

@+y:F32 -> F32

def mask_l source · line 259 · raw

@nleft:Nat -> @+t:U32 -> @rd:Pair(Array<F32>, F32) -> @rh:Pair(Array<F32>, F32) -> P2

def maxl2 source · line 268 · raw

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

def maxl source · line 275 · raw

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

def expl source · line 282 · raw

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

def suml source · line 289 · raw

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

def divl source · line 296 · raw

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

def softmax2 source · line 303 · raw

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

def softmax source · line 306 · raw

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

def sgd_l source · line 311 · raw

@nleft:Nat -> @+t:U32 -> @+lr:F32 -> @rw:Pair(Array<F32>, F32) -> @rd:Pair(Array<F32>, F32) -> P2

def pick source · line 322 · raw

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

def hit source · line 333 · raw

@b:Bool -> F32

def grad_l source · line 340 · raw

@ps:List<&2, F32> -> @+label:Nat -> @+j:Nat -> @+nf:F32 -> List<&2, F32>

def ce_row2 source · line 350 · raw

@+ps:List<&2, F32> -> @+label:Nat -> @+nf:F32 -> @+idx:U32 -> @loss:F32 -> @c:Array<F32> -> L2

def ce_row source · line 353 · raw

@al:AL -> @+label:Nat -> @+nf:F32 -> @+idx:U32 -> @loss:F32 -> L2

def ce_rows source · line 363 · raw

@labels:List<&2, Nat> -> @+mm1:Nat -> @+mu:U32 -> @+nf:F32 -> @+i:U32 -> @r:L2 -> L2

def gt_head source · line 375 · raw

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

def argmax2 source · line 382 · raw

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

def argmax source · line 391 · raw

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

def one source · line 398 · raw

@b:Bool -> Nat

def count_rows3 source · line 405 · raw

@+y:Nat -> @n:Nat -> @al:AL -> CR

def count_rows source · line 410 · raw

@labels:List<&2, Nat> -> @+mm1:Nat -> @+mu:U32 -> @+i:U32 -> @r:CR -> CR

def BD source · line 431 · raw

@b:Bool -> @t:Type -> @f:Type -> Type

a type chosen by a Bool: rewriting through it refutes True == False

def leb source · line 439 · raw

@a:Nat -> @b:Nat -> Bool

a <= b, by direct recursion

def pow2 source · line 448 · raw

@d:Nat -> Nat

def cap_pick source · line 456 · raw

@ok:Bool -> @d:Nat -> @+p:Nat -> Pair(Nat, Nat)

smallest power of two >= n, by structural recursion on n: returns (depth, 2^depth)

def cap_step source · line 463 · raw

@r:Pair(Nat, Nat) -> @+n:Nat -> Pair(Nat, Nat)

def cap_pair source · line 468 · raw

@n:Nat -> Pair(Nat, Nat)

def fst_nat source · line 475 · raw

@r:Pair(Nat, Nat) -> Nat

def snd_nat source · line 480 · raw

@r:Pair(Nat, Nat) -> Nat

def le_leb source · line 487 · raw

@a:Nat -> @b:Nat -> {Nat.is_le(a, b) == leb(a, b) : Bool}

leb is Base's Nat.is_le, case for case. leb walks its arguments one step at a time in the compiled program; Base's comparison is one native comparison there (0.1.13.0).

def leb_of source · line 498 · raw

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

def cap_dbl source · line 507 · raw

@f:Nat -> @ok:Bool -> @+n:Nat -> @+d:Nat -> @+p:Nat -> Pair(Nat, Nat)

the same pair by doubling (0.1.10.0): p doubles until n <= p, log2(n) steps in a tail loop, where cap_pair takes n steps of a recursion that is not a tail call, each with a comparison of up to n steps. Its fuel is n, which always suffices (2^n >= n); if it ran out, the answer would be cap_pair's, so the law below needs no arithmetic about how many doublings there are. ok is Nat.is_le(n, p), computed by the caller (match only inspects a parameter); with leb it was n steps in all, 152 ms for n = 786,432 on one GPU lane (0.1.13.0).

def cap_depth source · line 517 · raw

@+n:Nat -> Nat

the depth d such that an Array with 2^d slots holds n elements (the smallest such d)

def leb_succ source · line 523 · raw

@a:Nat -> @b:Nat -> @e:{True{} == leb(a, b) : Bool} -> {True{} == leb(a, 1n+b) : Bool}

a <= b -> a <= 1 + b

def leb_up_l source · line 534 · raw

@+a:Nat -> @+b:Nat -> @c:Nat -> @+e:{True{} == leb(a, b) : Bool} -> {True{} == leb(a, Nat.add(c, b)) : Bool}

a <= b -> a <= c + b

def leb_up source · line 542 · raw

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

a <= b -> a <= b + c

def leb_add source · line 553 · raw

@a:Nat -> @b:Nat -> @c:Nat -> @d:Nat -> @e1:{True{} == leb(a, b) : Bool} -> @e2:{True{} == leb(c, d) : Bool} -> {True{} == leb(Nat.add(a, c), Nat.add(b, d)) : Bool}

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

def pow2_pos source · line 564 · raw

@d:Nat -> {True{} == leb(1n, pow2(d)) : Bool}

1 <= 2^d

def pos_p source · line 572 · raw

@d:Nat -> @p:Nat -> @i1:{p == pow2(d) : Nat} -> {True{} == leb(1n, p) : Bool}

1 <= p when p = 2^d

def step_eq source · line 577 · raw

@d:Nat -> @p:Nat -> @i1:{p == pow2(d) : Nat} -> {Nat.add(p, p) == pow2(1n+d) : Nat}

p + p == 2^(1+d) when p == 2^d

def step_ok source · line 582 · raw

@ok:Bool -> @+d:Nat -> @+p:Nat -> @+m:Nat -> @+e:{ok == leb(1n+m, p) : Bool} -> @+i1:{p == pow2(d) : Nat} -> @+i2:{True{} == leb(m, p) : Bool} -> Pair({snd_nat(cap_pick(ok, d, p)) == pow2(fst_nat(cap_pick(ok, d, p))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_pick(ok, d, p))) : Bool})

one step: given the invariant for m, the invariant for 1+m holds whichever way the comparison goes

def step_pair_ok source · line 591 · raw

@+m:Nat -> @r:Pair(Nat, Nat) -> @i1:{snd_nat(r) == pow2(fst_nat(r)) : Nat} -> @i2:{True{} == leb(m, snd_nat(r)) : Bool} -> Pair({snd_nat(cap_step(r, 1n+m)) == pow2(fst_nat(cap_step(r, 1n+m))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_step(r, 1n+m))) : Bool})

the invariant for cap_step on a pair, opened

def cap_succ source · line 596 · raw

@+m:Nat -> @ih:Pair({snd_nat(cap_pair(m)) == pow2(fst_nat(cap_pair(m))) : Nat}, {True{} == leb(m, snd_nat(cap_pair(m))) : Bool}) -> Pair({snd_nat(cap_pair(1n+m)) == pow2(fst_nat(cap_pair(1n+m))) : Nat}, {True{} == leb(1n+m, snd_nat(cap_pair(1n+m))) : Bool})

def cap_inv source · line 602 · raw

@n:Nat -> Pair({snd_nat(cap_pair(n)) == pow2(fst_nat(cap_pair(n))) : Nat}, {True{} == leb(n, snd_nat(cap_pair(n))) : Bool})

the invariant of cap_pair: the second component is 2^(first) and is >= n

def dbl_inv source · line 611 · raw

@f:Nat -> @ok:Bool -> @+n:Nat -> @+d:Nat -> @+p:Nat -> @e:{ok == Nat.is_le(n, p) : Bool} -> @i1:{p == pow2(d) : Nat} -> Pair({snd_nat(cap_dbl(f, ok, n, d, p)) == pow2(fst_nat(cap_dbl(f, ok, n, d, p))) : Nat}, {True{} == leb(n, snd_nat(cap_dbl(f, ok, n, d, p))) : Bool})

the invariant of cap_dbl: whichever way it ends (n <= p found, or the fuel out and cap_pair's answer), the second component is 2^(first) and is >= n

def dbl_final source · line 620 · raw

@n:Nat -> @pr:Pair({snd_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n)) == pow2(fst_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n))) : Nat}, {True{} == leb(n, snd_nat(cap_dbl(n, Nat.is_le(n, 1n), n, 0n, 1n))) : Bool}) -> {True{} == leb(n, pow2(cap_depth(n))) : Bool}

def Mat.zeros source · line 636 · raw

@+r:Nat -> @+c:Nat -> Mat<r, c>

def Mat.fill source · line 639 · raw

@+r:Nat -> @+c:Nat -> @+v:F32 -> Mat<r, c>

def fill_list source · line 642 · raw

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

def len_is source · line 649 · raw

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

def Mat.of_list2 source · line 658 · raw

@-r:Nat -> @-c:Nat -> @+rr:Nat -> @+cc:Nat -> @ok:Bool -> @xs:List<&2, F32> -> Maybe<&1, Mat<r, c>>

def Mat.of_list source · line 667 · raw

@+r:Nat -> @+c:Nat -> @+xs:List<&2, F32> -> Maybe<&1, Mat<r, c>>

def Mat.from_list source · line 675 · raw

@+r:Nat -> @+c:Nat -> @xs:List<&2, F32> -> Mat<r, c>

def Mat.fill_at source · line 680 · raw

@-r:Nat -> @-c:Nat -> @+i:U32 -> @xs:List<&2, F32> -> @m:Mat<r, c> -> Mat<r, c>

writes the numbers of xs into m starting at flat index i (row-major); numbers past the capacity are dropped. For loading a large matrix block by block without building one big list.

def Mat.to_list2 source · line 688 · raw

@-r:Nat -> @-c:Nat -> @al:AL -> ML<r, c>

def Mat.to_list source · line 695 · raw

@+r:Nat -> @+c:Nat -> @m:Mat<r, c> -> ML<r, c>

def Mat.clone2 source · line 703 · raw

@-r:Nat -> @-c:Nat -> @p:Pair(Array<F32>, Array<F32>) -> MM2<r, c>

def Mat.clone source · line 712 · raw

@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> MM2<r, c>

def t8_zero source · line 744 · raw

T8

def pick0 source · line 747 · raw

@b:Bool -> @+v:F32 -> F32

def pk_g7 source · line 758 · raw

@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+v5:F32 -> @+v6:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

one cell: the 8 numbers of a at idx, idx + st, ..., those at positions q0 + q >= lim (past the end of the line) as 0

def pk_g6 source · line 763 · raw

@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+v5:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g5 source · line 768 · raw

@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+v4:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g4 source · line 773 · raw

@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+v3:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g3 source · line 778 · raw

@+v0:F32 -> @+v1:F32 -> @+v2:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g2 source · line 783 · raw

@+v0:F32 -> @+v1:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g1 source · line 788 · raw

@+v0:F32 -> @+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_g0 source · line 793 · raw

@+q0:U32 -> @+lim:U32 -> @+idx:U32 -> @+st:U32 -> @+o:U32 -> @t:Array<T8> -> @r:Pair(Array<F32>, F32) -> PK

def pk_line source · line 799 · raw

@tl:Nat -> @+q0:U32 -> @+lim:U32 -> @+base:U32 -> @+st:U32 -> @+o:U32 -> @r:PK -> PK

the cells of one line, cell o.. of t; q0 counts the line's numbers before the cell

def pk_lines source · line 807 · raw

@cnt:Nat -> @+tk:Nat -> @+tku:U32 -> @+ku:U32 -> @+i:U32 -> @+o:U32 -> @+lstep:U32 -> @+st:U32 -> @r:PK -> PK

lines count lines; source line i starts at i * lstep (in a), and goes to cell row o (cells o * tku .. in t)

def pk_pack source · line 814 · raw

@+cnt:Nat -> @+tk:Nat -> @+k:Nat -> @+lstep:U32 -> @+st:U32 -> @a:Array<F32> -> PK

def t8_dot source · line 819 · raw

@tk:Nat -> @+ia:U32 -> @+ib:U32 -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> @s4:F32 -> @s5:F32 -> @s6:F32 -> @s7:F32 -> @ra:Pair(Array<T8>, T8) -> @rb:Pair(Array<T8>, T8) -> TD

the dot of two lines of cells: tk cells from ia (in a) and ib (in b); ra, rb hold the arrays and the cells at ia, ib, already read. Product q of a cell goes to sum q, as dotr adds them.

def t8_comb source · line 830 · raw

@+rot:Nat -> @+s0:F32 -> @+s1:F32 -> @+s2:F32 -> @+s3:F32 -> @+s4:F32 -> @+s5:F32 -> @+s6:F32 -> @+s7:F32 -> F32

dotr's final sum after k products: the sums sit rotated by k mod 8 when its loop ends

def t8_fin source · line 852 · raw

@+rot:Nat -> @d:TD -> TR

def t8_cell source · line 857 · raw

@+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @a:Array<T8> -> @b:Array<T8> -> TR

def b2_mk source · line 865 · raw

@y:T8 -> @r:Pair(Array<T8>, T8) -> B2

def b2_get source · line 870 · raw

@+i2:U32 -> @r:Pair(Array<T8>, T8) -> B2

def t8_get2 source · line 875 · raw

@b:Array<T8> -> @+i1:U32 -> @+i2:U32 -> B2

def t8_dot2 source · line 881 · raw

@tk:Nat -> @+ia:U32 -> @+ib:U32 -> @+ic:U32 -> @s0:F32 -> @s1:F32 -> @s2:F32 -> @s3:F32 -> @s4:F32 -> @s5:F32 -> @s6:F32 -> @s7:F32 -> @u0:F32 -> @u1:F32 -> @u2:F32 -> @u3:F32 -> @u4:F32 -> @u5:F32 -> @u6:F32 -> @u7:F32 -> @ra:Pair(Array<T8>, T8) -> @rb:B2 -> TD2

def t8_fin2 source · line 891 · raw

@+rot:Nat -> @d:TD2 -> TR2

def t8_cell2 source · line 896 · raw

@+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @a:Array<T8> -> @b:Array<T8> -> TR2

def t8_set source · line 902 · raw

@+ac:Bool -> @+idx:U32 -> @c:Array<F32> -> @r:TR -> TG

def t8_set2 source · line 907 · raw

@+ac:Bool -> @+idx:U32 -> @c:Array<F32> -> @r:TR2 -> TG

def t8_cols source · line 913 · raw

@+ac:Bool -> @mleft:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @+cidx:U32 -> @g:TG -> TG

cells j.. of row i, two at a time, the last one alone when mleft is odd

def t8_rows source · line 922 · raw

@+ac:Bool -> @nleft:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+mu:U32 -> @g:TG -> TG

def tl_one source · line 933 · raw

@out:List<&2, F32> -> @r:TR -> TL

def tl_two source · line 938 · raw

@out:List<&2, F32> -> @r:TR2 -> TL

def t8_lcols source · line 944 · raw

@mleft:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j:U32 -> @l:TL -> TL

cells j.. of row i, mleft of them, onto out (newest first), two at a time as t8_cols

def t8_lrows source · line 953 · raw

@nleft:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @l:TL -> TL

def t8_lfin source · line 960 · raw

@l:TL -> List<&2, F32>

def t8_pk2 source · line 968 · raw

@pa:PK -> @pb:PK -> PKK

def t8_g source · line 973 · raw

@a:Array<F32> -> @b:Array<F32> -> @g:TG -> G

def t8_out_seq source · line 978 · raw

@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @p:PKK -> G

def pkt_leaf source · line 993 · raw

@r:PK -> PKT

def pkt_then source · line 998 · raw

@r:PKT -> @k:(@_:Array<F32> -> @_:PT -> PKT) -> PKT

def pk_tree source · line 1004 · raw

@+d:Nat -> @+cnt:Nat -> @+i:U32 -> @+tk:Nat -> @+tku:U32 -> @+ku:U32 -> @+lstep:U32 -> @+st:U32 -> @a:Array<F32> -> PKT

lines i .. i+cnt-1 of a, packed into 2^d pieces (halves of the line count, as the fork tree splits them)

def t8_with2 source · line 1011 · raw

@-R:Type -> @p:Pair(Array<T8>, Array<T8>) -> @k:(@_:Array<T8> -> @_:Array<T8> -> R) -> R

def t8_rpar source · line 1017 · raw

@t:PT -> @+cnt:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @b:Array<T8> -> List<&2, F32>

split by rows of C: the pieces are A's rows, B is shared; the result is C's rows in order

def t8_cpar source · line 1031 · raw

@t:PT -> @+cnt:Nat -> @+j0:U32 -> @+n:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @a:Array<T8> -> List<&2, CB>

split by columns of C: the pieces are B's columns, A is shared

def cb_row source · line 1044 · raw

@+ac:Bool -> @w:Nat -> @+idx:U32 -> @c:Array<F32> -> @xs:List<&2, F32> -> RW

w numbers of xs into c from idx; what is left of xs comes back

def cb_rows source · line 1051 · raw

@+ac:Bool -> @n:Nat -> @+i:U32 -> @+j0:U32 -> @+w:Nat -> @+mu:U32 -> @r:RW -> Array<F32>

def cb_write source · line 1058 · raw

@+ac:Bool -> @bs:List<&2, CB> -> @+n:Nat -> @+mu:U32 -> @c:Array<F32> -> Array<F32>

def t8_pr2 source · line 1065 · raw

@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @ra:PKT -> @rb:PK -> G

def t8_pc2 source · line 1070 · raw

@+ac:Bool -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @ra:PK -> @rb:PKT -> G

def t8_par2 source · line 1076 · raw

@+ac:Bool -> @byrows:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

2^d blocks along the larger of n (rows of C, pieces of A) and m (columns of C, pieces of B)

def gemm_t8b source · line 1083 · raw

@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def gemm_t8_ac source · line 1092 · raw

@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

C (n x m) = A · B (or C + A · B when ac) in cells of 8, sequential (d = 0) or with 2^d blocks of rows in parallel; A and B come back intact

def gemm_t8 source · line 1095 · raw

@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def gemm_any3 source · line 1103 · raw

@+ac:Bool -> @one:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

one row (matrix · vector): the scalar loop at every depth; two or more rows: cells of 8. Up to 0.1.11.0 one row in parallel ran gemm_par_cols, which clones both operands at every node of the fork tree: a 50,257 x 768 weight matrix at depth 4 ran out of the default heap (5.1 GB) and 8192 x 2048 took 124 ms against 10 ms sequentially. Packing the matrix into cells of 8 instead costs more than the product, which reads each weight once (64 ms against 24 at 50,257 x 768). Matrix · vector in parallel is Bands.matvec. Measured 2026-10-16 (NOTES.md).

def gemm_any2 source · line 1110 · raw

@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def gemm_any source · line 1113 · raw

@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

def mmul_fin source · line 1116 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMul<n, k, m>

def mmul_nt_fin source · line 1121 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMulNT<n, k, m>

def mmul_tn_fin source · line 1126 · raw

@-n:Nat -> @-k:Nat -> @-m:Nat -> @g:G -> MMulTN<n, k, m>

def split_pick source · line 1139 · raw

@big:Bool -> @d:Nat -> Nat

---- the split depth of a product (0.1.11.0): par is an upper bound, and a product too small to gain splits 0 times ----

A product of n x k by k x m splits into 2^par blocks only when it does at least 2^23 multiply-adds and each cell of C sums at least 64 of them; otherwise it runs sequentially. Measured on 16 threads (2026-10-16, 18 shapes): below 2^23 the split costs more than it saves, and with a short inner dimension (a weight gradient over a few tokens) every split was 2-3.6x slower, because each block hands its rows of C back as a list that is then written into C, a cost per cell of C that k multiply-adds per cell do not pay for. The result does not depend on the depth: the same bits at every depth (tests/t8.bend, tests/acc.bend, tests/split.bend).

def split_depth source · line 1146 · raw

@d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> Nat

def mm_a source · line 1149 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMul<n, k, m>

def Mat.matmul source · line 1154 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>

def Mat.matmul_acc source · line 1163 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMul<n, k, m>

def mm_nt source · line 1168 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMulNT<n, k, m>

def Mat.matmul_nt source · line 1173 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<m, k> -> MMulNT<n, k, m>

def Mat.matmul_nt_acc source · line 1180 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<m, k> -> @c:Mat<n, m> -> MMulNT<n, k, m>

def mm_tn source · line 1185 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @d:Nat -> @a:Array<F32> -> @b:Array<F32> -> MMulTN<n, k, m>

def Mat.matmul_tn source · line 1190 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<k, n> -> @b:Mat<k, m> -> MMulTN<n, k, m>

def Mat.matmul_tn_acc source · line 1197 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<k, n> -> @b:Mat<k, m> -> @c:Mat<n, m> -> MMulTN<n, k, m>

def sl_row source · line 1215 · raw

@k:Nat -> @+si:U32 -> @+di:U32 -> @d:Array<F32> -> @r:Pair(Array<F32>, F32) -> SL

d[di..] = src[si..], k numbers; r holds src[si], already read

def sl_rows source · line 1221 · raw

@r:Nat -> @+i:U32 -> @+w:Nat -> @+w32:U32 -> @+st:U32 -> @+o:U32 -> @sl:SL -> SL

rows i.. of a slice: w numbers a row from src[i*st + o], into d[i*w]

def blk_len source · line 1226 · raw

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

def mp_s4 source · line 1229 · raw

@a:Array<F32> -> @b:Array<F32> -> @g:G -> G

def mp_s3 source · line 1233 · raw

@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+w:Nat -> @+w32:U32 -> @+m32:U32 -> @a:Array<F32> -> @as:Array<F32> -> @c:Array<F32> -> @sb:SL -> G

def mp_s2 source · line 1237 · raw

@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+w:Nat -> @+w32:U32 -> @+m32:U32 -> @+o:U32 -> @c:Array<F32> -> @b:Array<F32> -> @sa:SL -> G

def mp_step source · line 1242 · raw

@+ac:Bool -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+k32:U32 -> @+bi:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> G

block bi: A's columns o..o+w (A is n x k) and B's rows o..o+w (B is k x m), multiplied into C (added when ac)

def mp_go source · line 1245 · raw

@nb:Nat -> @+bi:U32 -> @+par:Nat -> @+n:Nat -> @+m:Nat -> @+k32:U32 -> @g:G -> G

def mm_precise2 source · line 1250 · raw

@long:Bool -> @+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>

def Mat.matmul_precise source · line 1256 · raw

@+n:Nat -> @+k:Nat -> @+m:Nat -> @+par:Nat -> @a:Mat<n, k> -> @b:Mat<k, m> -> MMul<n, k, m>

C = A · B (n x k) · (k x m), k summed in blocks of 1024 (above); opt-in

def add_row2 source · line 1266 · raw

@-n:Nat -> @-m:Nat -> @+nn:Nat -> @+mm:Nat -> @cd:Array<F32> -> @al:AL -> MBias<n, m>

def Mat.add_row source · line 1271 · raw

@+n:Nat -> @+m:Nat -> @c:Mat<n, m> -> @b:Mat<1n, m> -> MBias<n, m>

def mpair_fin source · line 1281 · raw

@-r:Nat -> @-c:Nat -> @p:P2 -> MPair<r, c>

def relu_fin source · line 1286 · raw

@-r:Nat -> @-c:Nat -> @d:Array<F32> -> Mat<r, c>

def Mat.relu source · line 1289 · raw

@+r:Nat -> @+c:Nat -> @a:Mat<r, c> -> Mat<r, c>

def Mat.relu_bwd source · line 1296 · raw

@+r:Nat -> @+c:Nat -> @dy:Mat<r, c> -> @h:Mat<r, c> -> MPair<r, c>

def Mat.sgd source · line 1303 · raw

@+r:Nat -> @+c:Nat -> @+lr:F32 -> @w:Mat<r, c> -> @dw:Mat<r, c> -> MPair<r, c>

def Mat.add source · line 1310 · raw

@+r:Nat -> @+c:Nat -> @a:Mat<r, c> -> @b:Mat<r, c> -> MPair<r, c>

def colsum_fin source · line 1320 · raw

@-n:Nat -> @-m:Nat -> @g:G -> MColSum<n, m>

def Mat.col_sums source · line 1325 · raw

@+n:Nat -> @+m:Nat -> @a:Mat<n, m> -> MColSum<n, m>

def Mat.col_sums_acc source · line 1333 · raw

@+n:Nat -> @+m:Nat -> @a:Mat<n, m> -> @s:Mat<1n, m> -> MColSum<n, m>

def ce_fin source · line 1344 · raw

@-n:Nat -> @-c:Nat -> @+nf:F32 -> @r:L2 -> MCE<n, c>

def Mat.softmax_ce source · line 1349 · raw

@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> MCE<n, c>

def hits_fin source · line 1359 · raw

@-n:Nat -> @-c:Nat -> @r:CR -> MHits<n, c>

def Mat.count_correct source · line 1364 · raw

@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> MHits<n, c>

def row_fin source · line 1374 · raw

@-r:Nat -> @-c:Nat -> @al:AL -> MRow<r, c>

def Mat.read_row source · line 1379 · raw

@+r:Nat -> @+c:Nat -> @m:Mat<r, c> -> @+i:U32 -> MRow<r, c>

def Mat.write_row source · line 1384 · raw

@-r:Nat -> @+c:Nat -> @m:Mat<r, c> -> @+i:U32 -> @xs:List<&2, F32> -> Mat<r, c>

def labels_len_is source · line 1392 · raw

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

does the list of labels have exactly n entries (one per row)?

def ce_checked source · line 1401 · raw

@ok:Bool -> @+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> Maybe<&1, MCE<n, c>>

def Mat.softmax_ce_checked source · line 1409 · raw

@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @+labels:List<&2, Nat> -> Maybe<&1, MCE<n, c>>

like Mat.softmax_ce, but returns None unless labels has exactly n entries

def hits_checked source · line 1412 · raw

@ok:Bool -> @+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @labels:List<&2, Nat> -> Maybe<&1, MHits<n, c>>

def Mat.count_correct_checked source · line 1420 · raw

@+n:Nat -> @+c:Nat -> @z:Mat<n, c> -> @+labels:List<&2, Nat> -> Maybe<&1, MHits<n, c>>

like Mat.count_correct, but returns None unless labels has exactly n entries

def half.go source · line 1439 · raw

@n:Nat -> @acc:Nat -> Nat

n / 2 rounded down. A tail loop with an accumulator: a non-tail recursion here would put a continuation on every node of the band tree and keep the bands from running in parallel.

def half source · line 1450 · raw

@n:Nat -> Nat

def leb_refl source · line 1454 · raw

@+a:Nat -> {True{} == leb(a, a) : Bool}

a <= a

def add_succ_r source · line 1462 · raw

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

1 + (a + b) == a + (1 + b)

def add_zero_r source · line 1471 · raw

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

a + 0 == a

def half_go_le source · line 1480 · raw

@+n:Nat -> @+acc:Nat -> {True{} == leb(half.go(n, acc), Nat.add(n, acc)) : Bool}

half.go(n, acc) <= n + acc

def half_le source · line 1493 · raw

@+n:Nat -> {True{} == leb(half(n), n) : Bool}

half(n) <= n

def add_sub source · line 1498 · raw

@+a:Nat -> @+r:Nat -> @e:{True{} == leb(a, r) : Bool} -> {r == Nat.add(a, Nat.sub(r, a)) : Nat}

a <= r -> r == a + (r - a)

def Bands.zeros source · line 1525 · raw

@+d:Nat -> @+r:Nat -> @+c:Nat -> Bands<r, c>

r x c of zeros in 2^d bands (d = 0: a single band, the same as a Mat)

def eq_half source · line 1534 · raw

@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {half(a) == half(b) : Nat}

the row counts of the two bands follow the row count of the node: if rr == r at run time, the same holds for both halves (the bands carry r only in their type)

def eq_rest source · line 1538 · raw

@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {Nat.sub(a, half(a)) == Nat.sub(b, half(b)) : Nat}

def bfill_l source · line 1544 · raw

@+r:Nat -> @+c:Nat -> @+i:Nat -> @+n:Nat -> Bool

does a block of n numbers starting at flat index i end before the second band of a node with r rows? does it start in the second band?

def bfill_r source · line 1547 · raw

@+r:Nat -> @+c:Nat -> @+i:Nat -> Bool

def fill_n source · line 1551 · raw

@n:Nat -> @xs:List<&2, F32> -> @+i:U32 -> @a:Array<F32> -> Array<F32>

writes the first n numbers of xs at index i (and nothing past them)

def fill_band source · line 1558 · raw

@-r:Nat -> @-c:Nat -> @n:Nat -> @xs:List<&2, F32> -> @+i:U32 -> @m:Mat<r, c> -> Mat<r, c>

def bfill source · line 1566 · raw

@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @l:Bool -> @rt:Bool -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> Bands<r, c>

writes the first n numbers of xs at flat index i; l and rt say which bands they touch (bfill_l, bfill_r); rr is the row count r at run time. A block that crosses into the second band is not copied: the first band writes only its part, the second gets the rest by List.drop.

def bfill_top source · line 1577 · raw

@ok:Bool -> @+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> Bands<r, c>

def Bands.fill_at source · line 1586 · raw

@+r:Nat -> @+c:Nat -> @+i:Nat -> @+n:Nat -> @+xs:List<&2, F32> -> @b:Bands<r, c> -> Bands<r, c>

writes the n numbers of xs into b starting at flat index i (row-major, as Mat.fill_at); numbers past r*c are dropped. For loading a large matrix block by block.

def bv_g2 source · line 1599 · raw

@-r:Nat -> @-c:Nat -> @w:Array<F32> -> @al:AL -> BL<r, c>

def bv_g source · line 1605 · raw

@-r:Nat -> @-c:Nat -> @+m:Nat -> @g:G -> BL<r, c>

the band's 1 + m rows came out in an Array (one number per row); read them back as a list

def bv_gemm source · line 1613 · raw

@-r:Nat -> @+c:Nat -> @rows:Nat -> @+rr:Nat -> @x:Array<F32> -> @w:Array<F32> -> BL<r, c>

the rows of one band, one dot product each, with the gemm of Mat.matmul_nt (n = 1), so the results are identical. Writing the results into an Array and reading them back is faster than building the list row by row (NOTES.md, exp. 14).

def bv_mat source · line 1620 · raw

@-r:Nat -> @+c:Nat -> @+rr:Nat -> @x:Array<F32> -> @m:Mat<r, c> -> BL<r, c>

def bv_join source · line 1625 · raw

@-r:Nat -> @-c:Nat -> @p:BL<half(r), c> -> @q:BL<Nat.sub(r, half(r)), c> -> BL<r, c>

def bmv source · line 1634 · raw

@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> @xp:Pair(Array<F32>, Array<F32>) -> BL<r, c>

the two bands of a node run in parallel, each with its own copy of x (c numbers); xp is a pair of copies of x (a leaf uses one). xp comes after the erased e: up to Bend 2.0.36 an erased parameter at the end of the list turned the parallel let into two sequential calls (NOTES.md, exp. 11; fixed in 2.0.37 by bendlang/bend#1377). The order is kept, and harmless.

def bv_fin source · line 1642 · raw

@+r:Nat -> @-c:Nat -> @bl:BL<r, c> -> BV<r, c>

def Bands.matvec source · line 1649 · raw

@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @x:Mat<1n, c> -> BV<r, c>

y = B · x for B (r x c, one row per output) and x (1 x c); returns B intact and y (1 x r). The same product as Mat.matmul_nt(1n, c, r, ..), and the same numbers, bit for bit.

def Bands.matvec_l source · line 1656 · raw

@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+x:List<&2, F32> -> BL<r, c>

the same product with x and y as lists (x: c numbers, y: r numbers in row order), without the Mat conversions of Bands.matvec: for callers that already hold the vector as a list

def brow_fin source · line 1664 · raw

@-r:Nat -> @-c:Nat -> @al:AL -> BRow<r, c>

def brow_leaf2 source · line 1670 · raw

@-r:Nat -> @-c:Nat -> @n:Nat -> @+i:U32 -> @d:Array<F32> -> BRow<r, c>

n numbers starting at flat index i of one band (a band may have no rows)

def brow_leaf source · line 1677 · raw

@-r:Nat -> @-c:Nat -> @m:Mat<r, c> -> @+i:U32 -> @n:Nat -> BRow<r, c>

def brow_l source · line 1682 · raw

@-r:Nat -> @-c:Nat -> @p:BRow<half(r), c> -> @y:Bands<Nat.sub(r, half(r)), c> -> BRow<r, c>

def brow_r source · line 1687 · raw

@-r:Nat -> @-c:Nat -> @x:Bands<half(r), c> -> @q:BRow<Nat.sub(r, half(r)), c> -> BRow<r, c>

def brow source · line 1693 · raw

@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @left:Bool -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+i:Nat -> BRow<r, c>

row i; left says whether i is in the first band of the node (i < half(rr))

def Bands.read_row source · line 1703 · raw

@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> @+i:Nat -> BRow<r, c>

row i (c numbers), for example an embedding lookup

def bl_join source · line 1706 · raw

@-r:Nat -> @-c:Nat -> @p:BRow<half(r), c> -> @q:BRow<Nat.sub(r, half(r)), c> -> BRow<r, c>

def blist source · line 1711 · raw

@+c:Nat -> @-r:Nat -> @b:Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> BRow<r, c>

def Bands.to_list source · line 1719 · raw

@+r:Nat -> @+c:Nat -> @b:Bands<r, c> -> BRow<r, c>

all r*c numbers in row order (in BRow.row)

def bl_len source · line 1727 · raw

@-r:Nat -> @-c:Nat -> @bl:BL<r, c> -> Nat

def succ_out source · line 1733 · raw

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

s == a + (1 + b) -> s == 1 + (a + b)

def len_step source · line 1738 · raw

@-s:Nat -> @+p:Nat -> @+l:Nat -> @e:{s == 1n+Nat.add(p, 1n+l) : Nat} -> {s == 2n+Nat.add(p, l) : Nat}

s == 1 + (p + (1 + l)) -> s == 2 + (p + l)

def zero_out source · line 1743 · raw

@-s:Nat -> @+n:Nat -> @e:{s == Nat.add(n, 0n) : Nat} -> {s == n : Nat}

s == n + 0 -> s == n

def rev_len source · line 1748 · raw

@+xs:List<&2, F32> -> @+acc:List<&2, F32> -> {List.length(&2, F32, List.reverse.go(&2, F32, xs, acc)) == Nat.add(List.length(&2, F32, xs), List.length(&2, F32, acc)) : Nat}

reversing onto acc adds the lengths

def app_len source · line 1756 · raw

@+xs:List<&2, F32> -> @+ys:List<&2, F32> -> {List.length(&2, F32, List.append(&2, F32, xs, ys)) == Nat.add(List.length(&2, F32, xs), List.length(&2, F32, ys)) : Nat}

appending adds the lengths

def g_len source · line 1786 · raw

@-r:Nat -> @-c:Nat -> @+p:Nat -> @g:G -> {bl_len(r, c, bv_g(r, c, p, g)) == 1n+p : Nat}

whatever the gemm wrote, reading 1 + p rows back gives 1 + p numbers (G has one constructor, so matching g opens it, and leaf_len counts the list)

def join_len source · line 1812 · raw

@-r:Nat -> @-c:Nat -> @-h1:Nat -> @-h2:Nat -> @p:BL<half(r), c> -> @q:BL<Nat.sub(r, half(r)), c> -> @hp:{bl_len(half(r), c, p) == h1 : Nat} -> @hq:{bl_len(Nat.sub(r, half(r)), c, q) == h2 : Nat} -> {bl_len(r, c, bv_join(r, c, p, q)) == Nat.add(h1, h2) : Nat}

joining two halves of h1 and h2 numbers gives h1 + h2 numbers. (The statement for the whole tree, "Bands.matvec_l gives r numbers", would apply this to the results of the two recursive calls and to the induction hypotheses about the same calls; that uses each band's Array twice, which Bend's affine rules refuse. It stays open, covered by tests: NOTES.md, v3.1.)