prompt.bend checks
raw source on the hub · import bend-ml-llama@0.3.0.0/prompt.bend as Prompt
bend-ml-llama, the prompt in one pass: the n positions of a prompt go through each layer together, so that every weight matrix is read once for all of them (main.bend's forward_pre reads it once per position). Between the products the positions sit in arrays (n rows, row-major) and are read one row at a time by the code main.bend already has (rmsnorm, RoPE, attention over the cache, SwiGLU, the residual sums), so every number is computed by the same operations in the same order as position by position: the caches and the last position's logits have the same bits (reference/bits.py and the generation tests go through this file).
import ./main.bend as M import ./prompt.bend as P P.forward_prompt(c, cap, 0n, n, n, {==}, le, model, M.caches(c, M.Cfg.layers(c), cap), ids) # P.OutP{lg, cs, m}
The product is tensor-array's gemm_t8 (cells of 8) with the weights as its first operand: each row of a weight matrix meets every position before the next row is read. It has the bits of the matrix · vector product (tensor-array's tests/t8.bend; a product x · y commutes exactly).
3 imports
import Base import bend-ml-tensor-array@0.1.20.0/main.bend as TA import ./main.bend as M
Types
type Leaf source · line 55 · raw
Type
one band's results: rr rows (the band's) by n columns (the positions), row j at j·n
Leaf@rr:Nat -> @out:Array<F32> -> Leaf
type BM source · line 59 · raw
@-r:Nat -> @-c:Nat -> Type
the bands handed back, and their results in band order
BM@-r:Nat -> @-c:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c> -> @outs:List<&1, Leaf> -> BM<r, c>
type RO source · line 108 · raw
Type
the bands' results handed back, and one position's vector
RO@outs:List<&1, Leaf> -> @v:List<&2, F32> -> RO
type AP source · line 160 · raw
@-cap:Nat -> @-t:Nat -> Type
the cache with the positions written, and their attention outputs (n rows of dim numbers)
AP@-cap:Nat -> @-t:Nat -> @kv:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> @at:Array<F32> -> AP<cap, t>
type BP source · line 223 · raw
@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> Type
the layer's outputs for the n positions (n rows of dim numbers), its cache grown by them, its weights handed back
BP@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @x:Array<F32> -> @kv:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> @lw:0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c> -> BP<c, cap, t>
type RP source · line 256 · raw
@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> Type
the last layer's outputs, the layers and the caches handed back
RP@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @x:Array<F32> -> @layers:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c>> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> RP<c, cap, t>
type EX source · line 279 · raw
@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> Type
the embedding handed back, and x with the embedding row of each id (row i at i·dim)
EX@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @emb:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.vocab(c), 0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.dim(c)> -> @x:Array<F32> -> EX<c>
type OutP source · line 296 · raw
@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> Type
what a prompt produces: the logits of its last position, the caches grown by its positions, the model handed back
OutP@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @lg:List<&2, F32> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> OutP<c, cap, t>
Definitions
def al_then source · line 25 · raw
@-R:Type -> @al:0x5c5507d77be847e1e454ec33ab54e087/main.AL -> @k:(@_:Array<F32> -> @_:List<&2, F32> -> R) -> R
continues with an array and the list read from it
def p2_then source · line 31 · raw
@-R:Type -> @p:0x5c5507d77be847e1e454ec33ab54e087/main.P2 -> @k:(@_:Array<F32> -> @_:Array<F32> -> R) -> R
continues with the two arrays
def with2 source · line 37 · raw
@-R:Type -> @p:Pair(Array<F32>, Array<F32>) -> @k:(@_:Array<F32> -> @_:Array<F32> -> R) -> R
continues with an array and its copy
def zeros source · line 43 · raw
@+n:Nat -> @+w:Nat -> Array<F32>
n rows of w zeros
def row source · line 47 · raw
@+w:Nat -> @+i:U32 -> @a:Array<F32> -> 0x5c5507d77be847e1e454ec33ab54e087/main.AL
row i of a (rows of w >= 1 numbers, row-major) as a list, and a handed back
def bm_then source · line 62 · raw
@-R:Type -> @-r:Nat -> @-c:Nat -> @bm:BM<r, c> -> @k:(@_:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c> -> @_:List<&1, Leaf> -> R) -> R
def bm_g source · line 67 · raw
@-r:Nat -> @-c:Nat -> @rr:Nat -> @g:0x5c5507d77be847e1e454ec33ab54e087/main.G -> BM<r, c>
def bm_mat source · line 73 · raw
@-r:Nat -> @+c:Nat -> @+rr:Nat -> @+n:Nat -> @x:Array<F32> -> @m:0x5c5507d77be847e1e454ec33ab54e087/main.Mat<r, c> -> BM<r, c>
a band (rr rows of c numbers) times the n rows of x: out[j·n + i] = row j of the band · row i of x
def bm_join source · line 78 · raw
@-r:Nat -> @-c:Nat -> @p:BM<0x5c5507d77be847e1e454ec33ab54e087/main.half(r), c> -> @q:BM<Nat.sub(r, 0x5c5507d77be847e1e454ec33ab54e087/main.half(r)), c> -> BM<r, c>
def bmm source · line 86 · raw
@+c:Nat -> @-r:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c> -> @+rr:Nat -> @-e:{rr == r : Nat} -> @+n:Nat -> @x:Array<F32> -> BM<r, c>the two bands of a node run in parallel, one with x and the other with a copy of it (n·c numbers, small next to the weights): a band's product owns its operand, so every band needs its own. A fork with two copies in hand (as tensor-array's bmv has them for one vector) would make a copy more than it uses at every node
def mm source · line 96 · raw
@+r:Nat -> @+c:Nat -> @b:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c> -> @+n:Nat -> @x:Array<F32> -> BM<r, c>
W (r x c, in bands) times the n rows of x (n x c): the bands handed back and every band's results
def col source · line 100 · raw
@mleft:Nat -> @+idx:U32 -> @+nu:U32 -> @acc:List<&2, F32> -> @rc:Pair(Array<F32>, F32) -> 0x5c5507d77be847e1e454ec33ab54e087/main.AL
one band's column: its numbers from idx down in steps of nu, consed onto acc, so they come out in row order
def ro_then source · line 111 · raw
@-R:Type -> @ro:RO -> @k:(@_:List<&1, Leaf> -> @_:List<&2, F32> -> R) -> R
def vec.go source · line 117 · raw
@rev:List<&1, Leaf> -> @+i:U32 -> @+nu:U32 -> @done:List<&1, Leaf> -> @acc:List<&2, F32> -> RO
rev: the bands' results, last band first; done collects them back in band order; acc is the vector's tail so far
def vec source · line 131 · raw
@outs:List<&1, Leaf> -> @+i:U32 -> @+nu:U32 -> RO
position i's vector (of the nu positions): every band's column i, as one list in row order, read from the last band's last row back so that nothing is appended or reversed; the results are handed back
def norms source · line 139 · raw
@left:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+g:List<&2, F32> -> @+w:Nat -> @+i:U32 -> @x:Array<F32> -> @nrm:Array<F32> -> 0x5c5507d77be847e1e454ec33ab54e087/main.P2
nrm's row i = rmsnorm of x's row i, for the rows i .. i + left - 1 (w numbers each); both arrays come back
def end_eq source · line 148 · raw
@t:Nat -> @-tT:Nat -> @e:{Nat.add(t, 0n) == tT : Nat} -> {t == tT : Nat}t + 0 = tT, so t = tT
def kv_cast source · line 154 · raw
@-cap:Nat -> @-t:Nat -> @-tT:Nat -> @-e:{t == tT : Nat} -> @kv:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, tT>a cache of t positions is a cache of tT positions when t = tT. The count is in the type only (KV.push rebuilds the cache at 1 + t the same way), so the proof is asked for and nothing is done with it at run time.
def ap_then source · line 163 · raw
@-R:Type -> @-cap:Nat -> @-t:Nat -> @ap:AP<cap, t> -> @k:(@_:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> @_:Array<F32> -> R) -> R
def atts source · line 179 · raw
@left:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @+t:Nat -> @-tT:Nat -> @-e:{Nat.add(t, left) == tT : Nat} -> @-le:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(tT, cap) : Bool} -> @+i:U32 -> @+nu:U32 -> @qo:List<&1, Leaf> -> @ko:List<&1, Leaf> -> @vo:List<&1, Leaf> -> @kv:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> @at:Array<F32> -> AP<cap, tT>Position by position, as main.bend's block does it: the queries and keys of position t rotated, its keys and values pushed into the cache, attention over positions 0..t, the output written to at's row i. qo, ko and vo are the three products' results; left positions remain, the cache holds t of them, and t + left = tT <= cap (e, le), which gives each push its proof of room (step_room, leb_trans) and the cache its final index (end_eq).
The key/value heads of a position run one after another here (attention's depth 0), whatever the model's depth: both orders compute every head the same way, so the bits are the same. In parallel, each position forks a few microseconds of work in the middle of a stretch that runs on one thread, and the runtime wakes every other thread for it: on 16 threads the 135M's 56 positions took 2.2 s of system time and 90,000 voluntary context switches with the heads in parallel, 0.4 s and 21,000 with them in sequence, and the pass was slower than on 4 threads (NOTES, 2026-11-12).
def res_norm source · line 192 · raw
@left:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+g:List<&2, F32> -> @+w:Nat -> @+i:U32 -> @+nu:U32 -> @ao:List<&1, Leaf> -> @x:Array<F32> -> @nrm:Array<F32> -> 0x5c5507d77be847e1e454ec33ab54e087/main.P2
x's row i becomes x + a (a: the output projection's results, position i), and nrm's row i its rmsnorm
def swis source · line 203 · raw
@left:Nat -> @+f:Nat -> @+i:U32 -> @+nu:U32 -> @go:List<&1, Leaf> -> @uo:List<&1, Leaf> -> @s:Array<F32> -> Array<F32>
s's row i = swiglu of the gate's and the up projection's results at position i (f numbers)
def res source · line 213 · raw
@left:Nat -> @+w:Nat -> @+i:U32 -> @+nu:U32 -> @mo:List<&1, Leaf> -> @x:Array<F32> -> Array<F32>
x's row i becomes x + m (m: the down projection's results, position i)
def bp_then source · line 226 · raw
@-R:Type -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @b:BP<c, cap, t> -> @k:(@_:Array<F32> -> @_:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t> -> @_:0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c> -> R) -> R
def block_p source · line 233 · raw
@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @lw:0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c> -> @-cap:Nat -> @+t0:Nat -> @+n:Nat -> @-tT:Nat -> @-e:{Nat.add(t0, n) == tT : Nat} -> @-le:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(tT, cap) : Bool} -> @kv:0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t0> -> @x:Array<F32> -> BP<c, cap, tT>The layer on the n positions t0 .. t0 + n - 1, whose inputs are x's rows: main.bend's block_gen with each of its seven products done once for every position. tT = t0 + n (e) and tT <= cap (le).
def rp_then source · line 259 · raw
@-R:Type -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @r:RP<c, cap, t> -> @k:(@_:Array<F32> -> @_:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c>> -> @_:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> R) -> R
def run_p source · line 265 · raw
@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @+t0:Nat -> @+n:Nat -> @-tT:Nat -> @-e:{Nat.add(t0, n) == tT : Nat} -> @-le:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(tT, cap) : Bool} -> @ls:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c>> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t0>> -> @al:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.LW<c>> -> @ac:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, tT>> -> @x:Array<F32> -> RP<c, cap, tT>every layer in turn; al and ac collect the layers and the caches already done, newest first
def brow_then source · line 273 · raw
@-R:Type -> @-r:Nat -> @-c:Nat -> @br:0x5c5507d77be847e1e454ec33ab54e087/main.BRow<r, c> -> @k:(@_:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<r, c> -> @_:List<&2, F32> -> R) -> R
def ex_then source · line 282 · raw
@-R:Type -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @ex:EX<c> -> @k:(@_:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.vocab(c), 0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.dim(c)> -> @_:Array<F32> -> R) -> R
def embed source · line 287 · raw
@ids:List<&2, Nat> -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+i:U32 -> @emb:0x5c5507d77be847e1e454ec33ab54e087/main.Bands<0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.vocab(c), 0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg.dim(c)> -> @x:Array<F32> -> EX<c>
def OutP.then source · line 300 · raw
@-R:Type -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @-t:Nat -> @o:OutP<c, cap, t> -> @k:(@_:List<&2, F32> -> @_:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> @_:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> R) -> R
continues with the fields of a prompt's result: logits, caches, model
def forward_prompt source · line 308 · raw
@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> @+t0:Nat -> @+n:Nat -> @-tT:Nat -> @-e:{Nat.add(t0, n) == tT : Nat} -> @-le:{True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(tT, cap) : Bool} -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t0>> -> @ids:List<&2, Nat> -> OutP<c, cap, tT>The n >= 1 tokens ids at positions t0 .. t0 + n - 1, with the caches of positions 0..t0-1 (one per layer) and room for cap positions: tT = t0 + n (e) and tT <= cap (le). The final norm and the product by the embedding run on the last position only, as main.bend's head_of does them. ids must hold n ids (a shorter list leaves rows of zeros).