main.bend checks
raw source on the hub · import mlbend-generative@0.1.1.0/main.bend as Main
mlbend-generative: the generative skeleton, natively in Bend 2.
import mlbend-generative@0.1.1.0/main.bend as G
What a generative model needs besides the network: randomness that a verifier can draw again, a noise schedule, and a time embedding. The randomness is a pure function of counters — murmur3's fmix32 finaliser over (step salt · φ, index), Box-Muller over uniforms on a 24-bit grid — so a NumPy twin draws the same numbers bit for bit. The schedule is the cosine one (Nichol & Dhariwal): ᾱ_t = f(t)/f(0) in closed form, so every entry is computed on its own and the table needs no running product.
What is proved: the shape — the noise of any counter is exactly m numbers (noise_length), a schedule over t steps is exactly t (alphas_length), an embedding of k rungs is exactly 2k (temb_length), a strided walk over n entries emits exactly the entries its count says (strided_length), and a DDIM step over n elements is n elements (ddim_length). The builders recurse on the count left, the current index riding after it, and the law's variables are ordered the same way — the structural variable before the riding one — which is what lets the checker follow the induction (NOTES.md, 2026-10-25). What is tested: every F32 number, against the NumPy twin (reference/test_generative.py), and the bijection of mix32: the inverse is closed form (the half-width xor-shift is an involution, the 13-shift inverts by the triple xor, the multiplies by their modular inverses mod 2^32) and generative/tests/ok.bend checks it on a grid — a law would need bit-level equational reasoning the kernel has not been asked to do, so the bijection is the debt of issue #10, not a claim.
1 import
import Base
Laws
law noise_length provedsource · line 101 · raw
@+s:Nat -> @+m:Nat -> @+from:Nat -> {List.length(&2, F32, noise_from(s, m, from)) == m : Nat}LAW 1 (noise_length): m numbers asked for are m numbers built.
law alphas_length provedsource · line 152 · raw
@+t:Nat -> @+left:Nat -> @+from:Nat -> {List.length(&2, F32, alphas_from(t, left, from)) == left : Nat}LAW 2 (alphas_length): a schedule of t entries is t entries.
law lin_alphas_length provedsource · line 197 · raw
@+t:Nat -> @+left:Nat -> @+from:Nat -> @+acc:F32 -> {List.length(&2, F32, lin_alphas_from(t, left, from, acc)) == left : Nat}LAW 4 (lin_alphas_length): a linear schedule of t entries is t entries.
law temb_length provedsource · line 248 · raw
@+t:Nat -> @+steps:Nat -> @+k:Nat -> @+p:Nat -> {List.length(&2, F32, temb_from(t, steps, k, p)) == Nat.mul(k, 2n) : Nat}LAW 3 (temb_length): an embedding of k rungs is 2k numbers.
law strided_length provedsource · line 310 · raw
@-A:Data -> @+xs:List<&2, A> -> @+k:Nat -> @+skip:Nat -> {List.length(&2, A, strided_from(A, xs, k, skip)) == groups_of(List.length(&2, A, xs), k, skip) : Nat}LAW 5 (strided_length): every k-th entry of n entries is exactly the number the count says.
law ddim_length provedsource · line 407 · raw
@+co:Step -> @+x:List<&2, F32> -> @+eps:List<&2, F32> -> @+z:List<&2, F32> -> {List.length(&2, F32, ddim(co, x, eps, z)) == List.length(&2, F32, x) : Nat}LAW 6 (ddim_length): a step over n elements is n elements.
Types
type Step source · line 358 · raw
Data
The five coefficients of one step: the four roots and the noise scale. They are computed once per step, not once per element — the CIFAR probe measures 786k elements in a step, and five sqrts each would be paid 786k times. Every radicand is clamped at 0, so float rounding cannot hand sqrt a negative and turn a whole grid into NaN.
Step@c0:F32 -> @c1:F32 -> @c2:F32 -> @c3:F32 -> @sig:F32 -> Step
Definitions
def mix32 source · line 36 · raw
@+z:U32 -> U32
murmur3's fmix32 finaliser, over U32 arithmetic that wraps
def unxor13 source · line 44 · raw
@+y:U32 -> U32
the 13-shift undone: xor the shifted value twice more
def mix32_inv source · line 51 · raw
@+z:U32 -> U32
the inverse of mix32, in closed form: the 16-shift is an involution on 32 bits; the 13-shift inverts by the triple xor; the multiplies invert by their modular inverses mod 2^32
def hash_of source · line 60 · raw
@+s:Nat -> @+i:Nat -> U32
the hash of one counter: the step salt times the golden ratio, the index added beside it
def uniform source · line 66 · raw
@+s:Nat -> @+i:Nat -> F32
a uniform in (0, 1]: the low 24 bits of the hash plus one, exactly representable, so log(u) is never log(0)
def gauss_body source · line 73 · raw
@is_even:Bool -> @+r:F32 -> @+ang:F32 -> F32
one Gaussian of a counter, by parity: the even index of the pair brings the radius, the odd one the angle
def gauss_at source · line 80 · raw
@+s:Nat -> @+j:Nat -> F32
def noise_from source · line 89 · raw
@+s:Nat -> @+left:Nat -> @+from:Nat -> List<&2, F32>
The noise of counter s, left numbers from index from on. The count
left is the recursion; the index rides after it.
def noise_of source · line 97 · raw
@+s:Nat -> @+m:Nat -> List<&2, F32>
the noise of counter s: m numbers, the index 0 first
def cos_f source · line 123 · raw
@+t:Nat -> @+steps:Nat -> F32
def alpha_at source · line 129 · raw
@+t:Nat -> @+steps:Nat -> F32
ᾱ_t of the cosine schedule over steps steps (t = 1 .. steps)
def beta_at source · line 135 · raw
@+t:Nat -> @+steps:Nat -> F32
β_t, the ratio the reverse step removes: 1 - ᾱ_t/ᾱ_{t-1}, clamped at 0.99 — the cosine schedule's last ratio is 1 in F32 (ᾱ_T ~ 1e-15), and the reverse step's 1/sqrt(1-β) would amplify any prediction error
def alphas_from source · line 140 · raw
@+steps:Nat -> @+left:Nat -> @+from:Nat -> List<&2, F32>
The schedule from step from on, left entries. The count left is the
recursion; the step rides after it.
def alphas source · line 148 · raw
@+steps:Nat -> List<&2, F32>
ᾱ_1 .. ᾱ_steps, in order
def lin_beta_at source · line 178 · raw
@+t:Nat -> @+steps:Nat -> F32
β_t of the linear schedule over steps steps (t = 1 .. steps)
def lin_alphas_from source · line 184 · raw
@+steps:Nat -> @+left:Nat -> @+from:Nat -> @+acc:F32 -> List<&2, F32>
The schedule ᾱ_1 .. from from on, left entries, the running product
riding beside. The count left is the recursion; the step and the
product ride after it.
def lin_alphas source · line 193 · raw
@+steps:Nat -> List<&2, F32>
ᾱ_1 .. ᾱ_steps of the linear schedule, in order
def freq_of source · line 219 · raw
@+p:Nat -> F32
the frequency of rung p: a decade ladder 0.001 .. 1.0
def temb_from source · line 233 · raw
@+t:Nat -> @+steps:Nat -> @+left:Nat -> @+p:Nat -> List<&2, F32>
The embedding of step t over steps, from rung p on, left rungs —
each rung a sine and a cosine. The rungs left are the recursion; the
rung index rides after them.
def temb_of source · line 244 · raw
@+t:Nat -> @+steps:Nat -> @+k:Nat -> List<&2, F32>
the embedding of step t: k rungs, the lowest frequency first
def groups_of source · line 279 · raw
@+n:Nat -> @+k:Nat -> @+skip:Nat -> Nat
How many entries the walk emits over n entries. The count recurses on the count left, exactly as the walk recurses on the list left, and the two matches (on the count, on the skip) are in the same order — that is what lets the law below follow both by induction.
def strided_from source · line 293 · raw
@-A:Data -> @xs:List<&2, A> -> @+k:Nat -> @+skip:Nat -> List<&2, A>
Every k-th entry of xs, the (skip+1)-th first: drop skip, emit one,
drop k-1, emit one, and so on to the end of the list. The list is
matched first (binders in order), the skip beside it.
def strided source · line 305 · raw
@-A:Data -> @xs:List<&2, A> -> @+k:Nat -> List<&2, A>
every k-th entry of xs, the k-th first
def sigma_of source · line 348 · raw
@+abar:F32 -> @+abar_prev:F32 -> @+eta:F32 -> F32
def step_of source · line 361 · raw
@+abar:F32 -> @+abar_prev:F32 -> @+eta:F32 -> Step
def ddim_elem source · line 370 · raw
@+co:Step -> @+x:F32 -> @+eps:F32 -> @+z:F32 -> F32
one element of the step: x0 folded into x_prev
def head_or_zero source · line 379 · raw
@xs:List<&2, F32> -> F32
the head of a list, or 0.0 when it is empty: the missing eps or z of a step whose z is not drawn at all (eta = 0 passes Nil, and sigma·0.0 is 0.0)
def tail_or_nil source · line 387 · raw
@xs:List<&2, F32> -> List<&2, F32>
the rest of a list, or empty: the tail beside head_or_zero above
def ddim source · line 398 · raw
@+co:Step -> @x:List<&2, F32> -> @+eps:List<&2, F32> -> @+z:List<&2, F32> -> List<&2, F32>
One step over the whole sample: elementwise, and the shape follows x — n elements in, n elements out (LAW 6). eps and z ride beside it: a shorter list pads with 0.0, which is exactly right for the z of an eta = 0 step and is caught by the twin's numbers otherwise.