~/bend-docscommunity

LAWS.bend open laws/TODOs

raw source on the hub · import bend-gpu-kernels@0.1.0.0/LAWS.bend as LAWS

bend-gpu-kernels: the laws about lanes.bend, the index arithmetic every kernel writes through. They are checks at literal values: the real shapes of a 2048 x 2048 diffusion transformer (rows of 5,376, 7,168 and 14,336 cells in 7 segments, 4,422 rows, 16,384 lanes) and small cases taken whole. Base has no theory of U32 division or overflow yet, so no law here covers every input. Laws: share_small, share_real, seg_cells, seg_rows.

2 imports
import Base
import ./lanes.bend as L

Laws

law share_small provedin PROOF.bendsource · line 33 · raw

Pair({agree(8n, 0n, 0n, 8n) == True{} : Bool}, Pair({agree(8n, 0n, 3n, 8n) == True{} : Bool}, Pair({agree(8n, 0n, 8n, 8n) == True{} : Bool}, Pair({agree(8n, 0n, 16n, 8n) == True{} : Bool}, {agree(8n, 0n, 37n, 8n) == True{} : Bool}))))

Guarantees: on 8 leaves, for 0, 3, 8, 16 and 37 items, every leaf's count from share is the number of items below n that it owns (those congruent to it mod 8). A leaf runs its items in order from itself, so it runs exactly those, and the leaves together run every item once. Why it matters: bang gives each lane share() items; one too many runs an item past the end (a write outside the arrays), one too few silently leaves cells unwritten. The cases cover fewer items than lanes, exact multiples, and a remainder.

law share_real provedin PROOF.bendsource · line 41 · raw

Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.share(14569, 30954, 16384) == 2n : Nat}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.share(14570, 30954, 16384) == 1n : Nat}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.share(16383, 30954, 16384) == 1n : Nat}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.share(5119, 4838400, 16384) == 296n : Nat}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.share(5120, 4838400, 16384) == 295n : Nat}, {0x9ee7342e5aba51ed171798b850c1c311/lanes.share(16383, 4838400, 16384) == 295n : Nat})))))

Guarantees: on 16,384 lanes, 30,954 items (4,422 rows in 7 segments) give lanes 0 .. 14,569 two items and lanes 14,570 .. 16,383 one; 4,838,400 items (one adaLN pass over 50 blocks) give lanes 0 .. 5,119 296 items and the rest 295. Both splits add up to the item count. Why it matters: the real launches are far larger than the small cases; this checks share's U32 arithmetic at the sizes the kernels run at, where the boundary lane is easy to get off by one.

law seg_cells provedin PROOF.bendsource · line 50 · raw

Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(30953, 768) == U32.add(U32.mul(0x9ee7342e5aba51ed171798b850c1c311/lanes.row(30953, 7), 0x9ee7342e5aba51ed171798b850c1c311/lanes.width(768, 7)), 0x9ee7342e5aba51ed171798b850c1c311/lanes.col(30953, 768, 7)) : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(30953, 1024) == U32.add(U32.mul(0x9ee7342e5aba51ed171798b850c1c311/lanes.row(30953, 7), 0x9ee7342e5aba51ed171798b850c1c311/lanes.width(1024, 7)), 0x9ee7342e5aba51ed171798b850c1c311/lanes.col(30953, 1024, 7)) : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(30953, 2048) == U32.add(U32.mul(0x9ee7342e5aba51ed171798b850c1c311/lanes.row(30953, 7), 0x9ee7342e5aba51ed171798b850c1c311/lanes.width(2048, 7)), 0x9ee7342e5aba51ed171798b850c1c311/lanes.col(30953, 2048, 7)) : U32}, Pair({U32.div(0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(30954, 2048), 2048) == 30954 : U32}, Pair({U32.div(0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(123816, 4422), 4422) == 123816 : U32}, {U32.div(0x9ee7342e5aba51ed171798b850c1c311/lanes.cell(4838400, 8), 8) == 4838400 : U32})))))

Guarantees: for rows of 768, 1,024 and 2,048 cells a segment (7 segments a row), the last of 30,954 items starts at the cell its row and column give (row * row width + column), and the cell just past the last item, 30,954 * seg, is computed without wrapping; so is the cell past the last softmax row (123,816 rows of 4,422) and past the adaLN output (4,838,400 items of 8). Item i owns cells i * seg .. i * seg + seg - 1, so no two items share a cell. Why it matters: a kernel writes an item's cells at i * seg but reads its weights and modulation by row and column; if those disagreed it would apply the wrong weights, and a wrapped index would write over the start of the array.

law seg_rows provedin PROOF.bendsource · line 58 · raw

Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.leads(0, 7) == True{} : Bool}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.leads(6, 7) == False{} : Bool}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.first(6, 7) == 0 : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.row(6, 7) == 0 : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.leads(30947, 7) == True{} : Bool}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.leads(30948, 7) == False{} : Bool}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.leads(30953, 7) == False{} : Bool}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.first(30947, 7) == 30947 : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.first(30953, 7) == 30947 : U32}, Pair({0x9ee7342e5aba51ed171798b850c1c311/lanes.row(30947, 7) == 4421 : U32}, {0x9ee7342e5aba51ed171798b850c1c311/lanes.row(30953, 7) == 4421 : U32}))))))))))

Guarantees: in the first row (items 0 .. 6) and the last (30,947 .. 30,953) of 7 segments, the first item leads its row and the others do not, and every item finds the row's partials at its first item (0, 30,947) and the row's per-row values at its row (0, 4,421). Why it matters: the row scale of int8 quantization is written by the leading item only; two leaders would race on it, and an item reading another row's partials would normalize with the wrong row's sum.

Definitions

def hits source · line 12 · raw

@cnt:Nat -> @+r:Nat -> @+t:Nat -> @+l:Nat -> Nat

how many of the items 0 .. cnt - 1 are congruent to t mod l, walking them with their residue r (an obvious count, with no division)

def agree source · line 20 · raw

@k:Nat -> @+t:Nat -> @+n:Nat -> @+l:Nat -> Bool

whether leaves t .. t + k - 1 each get share() equal to that count, for n items on l leaves