# 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. import Base import ./lanes.bend as L # 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 hits(cnt: Nat, +r: Nat, +t: Nat, +l: Nat) -> Nat: match cnt: case 0n: 0n case 1n+q: Nat.add(Bool.pick(Nat, Nat.is_eq(r, t), 1n, 0n), hits(q, Bool.pick(Nat, Nat.is_eq((r + 1n : Nat), l), 0n, (r + 1n : Nat)), t, l)) # whether leaves t .. t + k - 1 each get share() equal to that count, for n items on l leaves def agree(k: Nat, +t: Nat, +n: Nat, +l: Nat) -> Bool: match k: case 0n: True{} case 1n+q: Bool.and(Nat.is_eq(L.share(U32.from_nat(t), U32.from_nat(n), U32.from_nat(l)), hits(n, 0n, t, l)), agree(q, (t + 1n : Nat), n, l)) # 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_small: {agree(8n, 0n, 0n, 8n) == True{} : Bool} & {agree(8n, 0n, 3n, 8n) == True{} : Bool} & {agree(8n, 0n, 8n, 8n) == True{} : Bool} & {agree(8n, 0n, 16n, 8n) == True{} : Bool} & {agree(8n, 0n, 37n, 8n) == True{} : Bool} # 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 share_real: {L.share(14569, 30954, 16384) == 2n : Nat} & {L.share(14570, 30954, 16384) == 1n : Nat} & {L.share(16383, 30954, 16384) == 1n : Nat} & {L.share(5119, 4838400, 16384) == 296n : Nat} & {L.share(5120, 4838400, 16384) == 295n : Nat} & {L.share(16383, 4838400, 16384) == 295n : Nat} # 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_cells: {L.cell(30953, 768) == ((L.row(30953, 7) * L.width(768, 7) : U32) + L.col(30953, 768, 7) : U32) : U32} & {L.cell(30953, 1024) == ((L.row(30953, 7) * L.width(1024, 7) : U32) + L.col(30953, 1024, 7) : U32) : U32} & {L.cell(30953, 2048) == ((L.row(30953, 7) * L.width(2048, 7) : U32) + L.col(30953, 2048, 7) : U32) : U32} & {(L.cell(30954, 2048) / 2048 : U32) == 30954 : U32} & {(L.cell(123816, 4422) / 4422 : U32) == 123816 : U32} & {(L.cell(4838400, 8) / 8 : U32) == 4838400 : 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. law seg_rows: {L.leads(0, 7) == True{} : Bool} & {L.leads(6, 7) == False{} : Bool} & {L.first(6, 7) == 0 : U32} & {L.row(6, 7) == 0 : U32} & {L.leads(30947, 7) == True{} : Bool} & {L.leads(30948, 7) == False{} : Bool} & {L.leads(30953, 7) == False{} : Bool} & {L.first(30947, 7) == 30947 : U32} & {L.first(30953, 7) == 30947 : U32} & {L.row(30947, 7) == 4421 : U32} & {L.row(30953, 7) == 4421 : U32}