# bend_tensors: dense linear algebra with shapes in the types. # # A Vec(n) holds 4 * 2^n floats and a Mat(r, c) 4 * 2^r rows of 4 * 2^c # floats, both as balanced trees of 4x4 register blocks. The tree is the # fork tree: every op below a node is a parallel let, and the matrix # product recurses on the tree, cache-oblivious, down to a 64-multiply # kernel. A shape mismatch is a checker error, not a runtime one. # # Nothing here matches on a computed value, recurses mutually, or opens # an Array: those are the rules the compiler makes fast code from. Read # AGENTS.md before editing. This file is assembled from gen/ by # gen/assemble.py; edit the parts, not this file. bend_tensors.bend is # the release (CPU BLAS); bend_tensors_gpu.bend adds the GPU paths. import Base # Base's F32.max and F32.show take + parameters, which a template slot # does not accept; these take plain ones. def F32.max.of(a: F32, b: F32) -> F32: F32.max(a, b) def F32.show.of(x: F32) -> String: F32.show(x) # Blocks # ====== # # A Blk is a 4x4 block of F32 in row-major order, a Vec4 four F32. Both # are Data records of scalars: they ride in registers, and the trees # built from them are shared through + binders with no copy. A block # product is 64 fused multiply-adds with no memory traffic: the kernel # every larger op bottoms out in. type Blk is Data: Blk{a00: F32, a01: F32, a02: F32, a03: F32, a10: F32, a11: F32, a12: F32, a13: F32, a20: F32, a21: F32, a22: F32, a23: F32, a30: F32, a31: F32, a32: F32, a33: F32} type Vec4 is Data: Vec4{x0: F32, x1: F32, x2: F32, x3: F32} # Vec4 # ---- def Vec4.full(+x: F32) -> Vec4: Vec4{x, x, x, x} def Vec4.zeros() -> Vec4: Vec4{0.0, 0.0, 0.0, 0.0} # x, x + 1, x + 2, x + 3 def Vec4.arange(+x: F32) -> Vec4: Vec4{x, (x + 1.0 : F32), (x + 2.0 : F32), (x + 3.0 : F32)} def Vec4.map(~f: F32 -> F32, v: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = v Vec4{f(x0), f(x1), f(x2), f(x3)} def Vec4.zip(~f: F32 -> F32 -> F32, a: Vec4, b: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = a Vec4{y0, y1, y2, y3} = b Vec4{f(x0, y0), f(x1, y1), f(x2, y2), f(x3, y3)} def Vec4.add(a: Vec4, b: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = a Vec4{y0, y1, y2, y3} = b Vec4{(x0 + y0 : F32), (x1 + y1 : F32), (x2 + y2 : F32), (x3 + y3 : F32)} def Vec4.sub(a: Vec4, b: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = a Vec4{y0, y1, y2, y3} = b Vec4{(x0 - y0 : F32), (x1 - y1 : F32), (x2 - y2 : F32), (x3 - y3 : F32)} def Vec4.mul(a: Vec4, b: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = a Vec4{y0, y1, y2, y3} = b Vec4{(x0 * y0 : F32), (x1 * y1 : F32), (x2 * y2 : F32), (x3 * y3 : F32)} def Vec4.scale(+k: F32, v: Vec4) -> Vec4: Vec4{x0, x1, x2, x3} = v Vec4{(k * x0 : F32), (k * x1 : F32), (k * x2 : F32), (k * x3 : F32)} # the sum, as (x0 + x1) + (x2 + x3): every reduction in this file # pairs its terms the same way, so laws can equate them def Vec4.sum(v: Vec4) -> F32: Vec4{x0, x1, x2, x3} = v ((x0 + x1) + (x2 + x3) : F32) def Vec4.dot(a: Vec4, b: Vec4) -> F32: Vec4{x0, x1, x2, x3} = a Vec4{y0, y1, y2, y3} = b ((x0 * y0 + x1 * y1) + (x2 * y2 + x3 * y3) : F32) def Vec4.max(v: Vec4) -> F32: Vec4{x0, x1, x2, x3} = v F32.max(F32.max(x0, x1), F32.max(x2, x3)) def Vec4.min(v: Vec4) -> F32: Vec4{x0, x1, x2, x3} = v F32.min(F32.min(x0, x1), F32.min(x2, x3)) # the index (0-3) and value of the largest element; the first wins ties def Vec4.argmax.pick(c: Bool, +i: U32, +x: F32, +j: U32, +y: F32) -> U32 & F32: match c: case True{}: (i, x) case False{}: (j, y) def Vec4.argmax.two(ix: U32 & F32, jy: U32 & F32) -> U32 & F32: (i, x) = ix (j, y) = jy +x2 = x +y2 = y Vec4.argmax.pick((x2 >= y2 : F32), i, x2, j, y2) def Vec4.argmax(v: Vec4) -> U32 & F32: Vec4{x0, x1, x2, x3} = v Vec4.argmax.two(Vec4.argmax.two((0, x0), (1, x1)), Vec4.argmax.two((2, x2), (3, x3))) def Vec4.to_list(v: Vec4, acc: List<&2, F32>) -> List<&2, F32>: Vec4{x0, x1, x2, x3} = v x0 <> x1 <> x2 <> x3 <> acc # the first four of xs, padded with 0.0, and the rest def Vec4.from_list(xs: List<&2, F32>) -> Vec4: match xs: case x0 <> x1 <> x2 <> x3 <> rest: Vec4{x0, x1, x2, x3} case x0 <> x1 <> x2 <> []: Vec4{x0, x1, x2, 0.0} case x0 <> x1 <> []: Vec4{x0, x1, 0.0, 0.0} case x0 <> []: Vec4{x0, 0.0, 0.0, 0.0} case []: Vec4{0.0, 0.0, 0.0, 0.0} def Vec4.show(v: Vec4) -> String: Vec4{x0, x1, x2, x3} = v F32.show(x0) ++ " " ++ F32.show(x1) ++ " " ++ F32.show(x2) ++ " " ++ F32.show(x3) # Blk # --- def Blk.full(+x: F32) -> Blk: Blk{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x} def Blk.zeros() -> Blk: Blk.full(0.0) def Blk.eye() -> Blk: Blk{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0} def Blk.map(~f: F32 -> F32, m: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{f(a00), f(a01), f(a02), f(a03), f(a10), f(a11), f(a12), f(a13), f(a20), f(a21), f(a22), f(a23), f(a30), f(a31), f(a32), f(a33)} def Blk.zip(~f: F32 -> F32 -> F32, m: Blk, n: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{b00, b01, b02, b03, b10, b11, b12, b13, b20, b21, b22, b23, b30, b31, b32, b33} = n Blk{f(a00, b00), f(a01, b01), f(a02, b02), f(a03, b03), f(a10, b10), f(a11, b11), f(a12, b12), f(a13, b13), f(a20, b20), f(a21, b21), f(a22, b22), f(a23, b23), f(a30, b30), f(a31, b31), f(a32, b32), f(a33, b33)} def Blk.add(m: Blk, n: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{b00, b01, b02, b03, b10, b11, b12, b13, b20, b21, b22, b23, b30, b31, b32, b33} = n Blk{(a00 + b00 : F32), (a01 + b01 : F32), (a02 + b02 : F32), (a03 + b03 : F32), (a10 + b10 : F32), (a11 + b11 : F32), (a12 + b12 : F32), (a13 + b13 : F32), (a20 + b20 : F32), (a21 + b21 : F32), (a22 + b22 : F32), (a23 + b23 : F32), (a30 + b30 : F32), (a31 + b31 : F32), (a32 + b32 : F32), (a33 + b33 : F32)} def Blk.sub(m: Blk, n: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{b00, b01, b02, b03, b10, b11, b12, b13, b20, b21, b22, b23, b30, b31, b32, b33} = n Blk{(a00 - b00 : F32), (a01 - b01 : F32), (a02 - b02 : F32), (a03 - b03 : F32), (a10 - b10 : F32), (a11 - b11 : F32), (a12 - b12 : F32), (a13 - b13 : F32), (a20 - b20 : F32), (a21 - b21 : F32), (a22 - b22 : F32), (a23 - b23 : F32), (a30 - b30 : F32), (a31 - b31 : F32), (a32 - b32 : F32), (a33 - b33 : F32)} def Blk.mul(m: Blk, n: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{b00, b01, b02, b03, b10, b11, b12, b13, b20, b21, b22, b23, b30, b31, b32, b33} = n Blk{(a00 * b00 : F32), (a01 * b01 : F32), (a02 * b02 : F32), (a03 * b03 : F32), (a10 * b10 : F32), (a11 * b11 : F32), (a12 * b12 : F32), (a13 * b13 : F32), (a20 * b20 : F32), (a21 * b21 : F32), (a22 * b22 : F32), (a23 * b23 : F32), (a30 * b30 : F32), (a31 * b31 : F32), (a32 * b32 : F32), (a33 * b33 : F32)} def Blk.scale(+k: F32, m: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{(k * a00 : F32), (k * a01 : F32), (k * a02 : F32), (k * a03 : F32), (k * a10 : F32), (k * a11 : F32), (k * a12 : F32), (k * a13 : F32), (k * a20 : F32), (k * a21 : F32), (k * a22 : F32), (k * a23 : F32), (k * a30 : F32), (k * a31 : F32), (k * a32 : F32), (k * a33 : F32)} def Blk.transpose(m: Blk) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Blk{a00, a10, a20, a30, a01, a11, a21, a31, a02, a12, a22, a32, a03, a13, a23, a33} # m * n: 64 multiply-adds, each cell as (a0 b0 + a1 b1) + (a2 b2 + a3 b3) def Blk.matmul(m: Blk, n: Blk) -> Blk: Blk{+a00, +a01, +a02, +a03, +a10, +a11, +a12, +a13, +a20, +a21, +a22, +a23, +a30, +a31, +a32, +a33} = m Blk{+b00, +b01, +b02, +b03, +b10, +b11, +b12, +b13, +b20, +b21, +b22, +b23, +b30, +b31, +b32, +b33} = n Blk{((a00 * b00 + a01 * b10) + (a02 * b20 + a03 * b30) : F32), ((a00 * b01 + a01 * b11) + (a02 * b21 + a03 * b31) : F32), ((a00 * b02 + a01 * b12) + (a02 * b22 + a03 * b32) : F32), ((a00 * b03 + a01 * b13) + (a02 * b23 + a03 * b33) : F32), ((a10 * b00 + a11 * b10) + (a12 * b20 + a13 * b30) : F32), ((a10 * b01 + a11 * b11) + (a12 * b21 + a13 * b31) : F32), ((a10 * b02 + a11 * b12) + (a12 * b22 + a13 * b32) : F32), ((a10 * b03 + a11 * b13) + (a12 * b23 + a13 * b33) : F32), ((a20 * b00 + a21 * b10) + (a22 * b20 + a23 * b30) : F32), ((a20 * b01 + a21 * b11) + (a22 * b21 + a23 * b31) : F32), ((a20 * b02 + a21 * b12) + (a22 * b22 + a23 * b32) : F32), ((a20 * b03 + a21 * b13) + (a22 * b23 + a23 * b33) : F32), ((a30 * b00 + a31 * b10) + (a32 * b20 + a33 * b30) : F32), ((a30 * b01 + a31 * b11) + (a32 * b21 + a33 * b31) : F32), ((a30 * b02 + a31 * b12) + (a32 * b22 + a33 * b32) : F32), ((a30 * b03 + a31 * b13) + (a32 * b23 + a33 * b33) : F32)} # m * v: each row dotted with v def Blk.matvec(m: Blk, v: Vec4) -> Vec4: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{+y0, +y1, +y2, +y3} = v Vec4{((a00 * y0 + a01 * y1) + (a02 * y2 + a03 * y3) : F32), ((a10 * y0 + a11 * y1) + (a12 * y2 + a13 * y3) : F32), ((a20 * y0 + a21 * y1) + (a22 * y2 + a23 * y3) : F32), ((a30 * y0 + a31 * y1) + (a32 * y2 + a33 * y3) : F32)} # row i's sum, as (a_i0 + a_i1) + (a_i2 + a_i3) def Blk.sum_rows(m: Blk) -> Vec4: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{((a00 + a01) + (a02 + a03) : F32), ((a10 + a11) + (a12 + a13) : F32), ((a20 + a21) + (a22 + a23) : F32), ((a30 + a31) + (a32 + a33) : F32)} # the sum of every cell, as the sum of the row sums def Blk.sum(m: Blk) -> F32: Vec4.sum(Blk.sum_rows(m)) # column j's sum, as (a_0j + a_1j) + (a_2j + a_3j): the row sums of the transpose def Blk.sum_cols(m: Blk) -> Vec4: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{((a00 + a10) + (a20 + a30) : F32), ((a01 + a11) + (a21 + a31) : F32), ((a02 + a12) + (a22 + a32) : F32), ((a03 + a13) + (a23 + a33) : F32)} def Blk.max_rows(m: Blk) -> Vec4: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{F32.max(F32.max(a00, a01), F32.max(a02, a03)), F32.max(F32.max(a10, a11), F32.max(a12, a13)), F32.max(F32.max(a20, a21), F32.max(a22, a23)), F32.max(F32.max(a30, a31), F32.max(a32, a33))} # v added to every row def Blk.add_row(m: Blk, v: Vec4) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{+y0, +y1, +y2, +y3} = v Blk{(a00 + y0 : F32), (a01 + y1 : F32), (a02 + y2 : F32), (a03 + y3 : F32), (a10 + y0 : F32), (a11 + y1 : F32), (a12 + y2 : F32), (a13 + y3 : F32), (a20 + y0 : F32), (a21 + y1 : F32), (a22 + y2 : F32), (a23 + y3 : F32), (a30 + y0 : F32), (a31 + y1 : F32), (a32 + y2 : F32), (a33 + y3 : F32)} # v subtracted from every row def Blk.sub_row(m: Blk, v: Vec4) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{+y0, +y1, +y2, +y3} = v Blk{(a00 - y0 : F32), (a01 - y1 : F32), (a02 - y2 : F32), (a03 - y3 : F32), (a10 - y0 : F32), (a11 - y1 : F32), (a12 - y2 : F32), (a13 - y3 : F32), (a20 - y0 : F32), (a21 - y1 : F32), (a22 - y2 : F32), (a23 - y3 : F32), (a30 - y0 : F32), (a31 - y1 : F32), (a32 - y2 : F32), (a33 - y3 : F32)} # row i subtracted by v_i (the column vector v broadcast along rows) def Blk.sub_col(m: Blk, v: Vec4) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{+y0, +y1, +y2, +y3} = v Blk{(a00 - y0 : F32), (a01 - y0 : F32), (a02 - y0 : F32), (a03 - y0 : F32), (a10 - y1 : F32), (a11 - y1 : F32), (a12 - y1 : F32), (a13 - y1 : F32), (a20 - y2 : F32), (a21 - y2 : F32), (a22 - y2 : F32), (a23 - y2 : F32), (a30 - y3 : F32), (a31 - y3 : F32), (a32 - y3 : F32), (a33 - y3 : F32)} # row i scaled by v_i def Blk.scale_rows(m: Blk, v: Vec4) -> Blk: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m Vec4{+y0, +y1, +y2, +y3} = v Blk{(y0 * a00 : F32), (y0 * a01 : F32), (y0 * a02 : F32), (y0 * a03 : F32), (y1 * a10 : F32), (y1 * a11 : F32), (y1 * a12 : F32), (y1 * a13 : F32), (y2 * a20 : F32), (y2 * a21 : F32), (y2 * a22 : F32), (y2 * a23 : F32), (y3 * a30 : F32), (y3 * a31 : F32), (y3 * a32 : F32), (y3 * a33 : F32)} # the 16 cells in row-major order, before acc def Blk.to_list(m: Blk, acc: List<&2, F32>) -> List<&2, F32>: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m a00 <> a01 <> a02 <> a03 <> a10 <> a11 <> a12 <> a13 <> a20 <> a21 <> a22 <> a23 <> a30 <> a31 <> a32 <> a33 <> acc # a block from its four rows def Blk.from_vec4s(v0: Vec4, v1: Vec4, v2: Vec4, v3: Vec4) -> Blk: Vec4{a00, a01, a02, a03} = v0 Vec4{a10, a11, a12, a13} = v1 Vec4{a20, a21, a22, a23} = v2 Vec4{a30, a31, a32, a33} = v3 Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} # a block from four rows of four; a short row pads with 0.0 def Blk.from_rows(r0: List<&2, F32>, r1: List<&2, F32>, r2: List<&2, F32>, r3: List<&2, F32>) -> Blk: Blk.from_vec4s(Vec4.from_list(r0), Vec4.from_list(r1), Vec4.from_list(r2), Vec4.from_list(r3)) def Blk.row(m: Blk, i: Nat) -> Vec4: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = m match i: case 0n: Vec4{a00, a01, a02, a03} case 1n: Vec4{a10, a11, a12, a13} case 2n: Vec4{a20, a21, a22, a23} case _: Vec4{a30, a31, a32, a33} # Masks # ----- # # k counts the valid elements (or rows, or columns) of a block; the # rest are padding, set to 0.0 or left out of a reduction. A k of 0 # only reaches a reduction whose result the caller discards. # elements at index >= k set to 0.0 def Vec4.mask(k: Nat, v: Vec4) -> Vec4: match k v: case 0n Vec4{x0, x1, x2, x3}: Vec4{0.0, 0.0, 0.0, 0.0} case 1n Vec4{x0, x1, x2, x3}: Vec4{x0, 0.0, 0.0, 0.0} case 2n Vec4{x0, x1, x2, x3}: Vec4{x0, x1, 0.0, 0.0} case 3n Vec4{x0, x1, x2, x3}: Vec4{x0, x1, x2, 0.0} case _ v2: v2 # the largest of the first k elements def Vec4.max_upto(k: Nat, v: Vec4) -> F32: match k v: case 1n Vec4{x0, x1, x2, x3}: x0 case 2n Vec4{x0, x1, x2, x3}: F32.max(x0, x1) case 3n Vec4{x0, x1, x2, x3}: F32.max(F32.max(x0, x1), x2) case _ v2: Vec4.max(v2) # the smallest of the first k elements def Vec4.min_upto(k: Nat, v: Vec4) -> F32: match k v: case 1n Vec4{x0, x1, x2, x3}: x0 case 2n Vec4{x0, x1, x2, x3}: F32.min(x0, x1) case 3n Vec4{x0, x1, x2, x3}: F32.min(F32.min(x0, x1), x2) case _ v2: Vec4.min(v2) # the index and value of the largest of the first k elements def Vec4.argmax_upto(k: Nat, v: Vec4) -> U32 & F32: match k v: case 1n Vec4{x0, x1, x2, x3}: (0, x0) case 2n Vec4{x0, x1, x2, x3}: Vec4.argmax.two((0, x0), (1, x1)) case 3n Vec4{x0, x1, x2, x3}: Vec4.argmax.two(Vec4.argmax.two((0, x0), (1, x1)), (2, x2)) case _ v2: Vec4.argmax(v2) # rows at index >= k set to 0.0 def Blk.mask_rows(k: Nat, m: Blk) -> Blk: match k m: case 0n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} case 1n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, a01, a02, a03, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} case 2n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, a01, a02, a03, a10, a11, a12, a13, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} case 3n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, 0.0, 0.0, 0.0, 0.0} case _ m2: m2 # columns at index >= k set to 0.0 def Blk.mask_cols(k: Nat, m: Blk) -> Blk: match k m: case 0n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} case 1n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, 0.0, 0.0, 0.0, a10, 0.0, 0.0, 0.0, a20, 0.0, 0.0, 0.0, a30, 0.0, 0.0, 0.0} case 2n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, a01, 0.0, 0.0, a10, a11, 0.0, 0.0, a20, a21, 0.0, 0.0, a30, a31, 0.0, 0.0} case 3n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Blk{a00, a01, a02, 0.0, a10, a11, a12, 0.0, a20, a21, a22, 0.0, a30, a31, a32, 0.0} case _ m2: m2 # rows at index >= ki and columns at index >= kj set to 0.0 def Blk.mask(ki: Nat, kj: Nat, m: Blk) -> Blk: Blk.mask_cols(kj, Blk.mask_rows(ki, m)) # each row's largest among its first k columns def Blk.max_rows_upto(k: Nat, m: Blk) -> Vec4: match k m: case 1n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Vec4{a00, a10, a20, a30} case 2n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Vec4{F32.max(a00, a01), F32.max(a10, a11), F32.max(a20, a21), F32.max(a30, a31)} case 3n Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33}: Vec4{F32.max(F32.max(a00, a01), a02), F32.max(F32.max(a10, a11), a12), F32.max(F32.max(a20, a21), a22), F32.max(F32.max(a30, a31), a32)} case _ m2: Blk.max_rows(m2) # Vectors # ======= # # VecT(n) is a balanced binary tree of depth n over Vec4 leaves: 4 * 2^n # floats. The depth is a live Nat, so every op matches on it first and # then opens the constructor the depth implies, as Base does for Word. # Every recursion below a node is a parallel let: the tree is the fork # tree, balanced by construction. law VecT: for n: Nat Data type VecT.Leaf is Data: VecL{v: Vec4} type VecT.Node<-p: Nat> is Data: VecN{l: VecT(p), r: VecT(p)} def VecT(n): match n: case 0n: VecT.Leaf case 1n+p: VecT.Node
# the element count of a VecT(n): 4 * 2^n def VecT.size(+n: Nat) -> Nat: match n: case 0n: 4n case 1n+p: +h = VecT.size(p) Nat.add(h, h) def VecT.full(+n: Nat, +x: F32) -> VecT(n): match n: case 0n: VecL{Vec4.full(x)} case 1n+p: l r = VecT.full(p, x) VecT.full(p, x) VecN{l, r} def VecT.zeros(+n: Nat) -> VecT(n): VecT.full(n, 0.0) def VecT.ones(+n: Nat) -> VecT(n): VecT.full(n, 1.0) # x, x + 1, x + 2, .. def VecT.arange.from(+n: Nat, +x: F32) -> VecT(n): match n: case 0n: VecL{Vec4.arange(x)} case 1n+p: +h = F32.from_nat(VecT.size(p)) l r = VecT.arange.from(p, x) VecT.arange.from(p, (x + h : F32)) VecN{l, r} # 0, 1, 2, .. def VecT.arange(+n: Nat) -> VecT(n): VecT.arange.from(n, 0.0) def VecT.map(~f: F32 -> F32, +n: Nat, v: VecT(n)) -> VecT(n): match n: case 0n: VecL{x} = v VecL{Vec4.map(~f, x)} case 1n+p: VecN{l, r} = v a b = VecT.map(~f, p, l) VecT.map(~f, p, r) VecN{a, b} def VecT.zip(~f: F32 -> F32 -> F32, +n: Nat, a: VecT(n), b: VecT(n)) -> VecT(n): match n: case 0n: VecL{x} = a VecL{y} = b VecL{Vec4.zip(~f, x, y)} case 1n+p: VecN{al, ar} = a VecN{bl, br} = b l r = VecT.zip(~f, p, al, bl) VecT.zip(~f, p, ar, br) VecN{l, r} def VecT.add(+n: Nat, a: VecT(n), b: VecT(n)) -> VecT(n): VecT.zip(~F32.add, n, a, b) def VecT.sub(+n: Nat, a: VecT(n), b: VecT(n)) -> VecT(n): VecT.zip(~F32.sub, n, a, b) # elementwise product def VecT.mul(+n: Nat, a: VecT(n), b: VecT(n)) -> VecT(n): VecT.zip(~F32.mul, n, a, b) def VecT.exp(+n: Nat, v: VecT(n)) -> VecT(n): VecT.map(~F32.exp, n, v) def VecT.scale(+n: Nat, +k: F32, v: VecT(n)) -> VecT(n): match n: case 0n: VecL{x} = v VecL{Vec4.scale(k, x)} case 1n+p: VecN{l, r} = v a b = VecT.scale(p, k, l) VecT.scale(p, k, r) VecN{a, b} # k added to every element def VecT.shift(+n: Nat, +k: F32, v: VecT(n)) -> VecT(n): match n: case 0n: VecL{x} = v Vec4{x0, x1, x2, x3} = x VecL{Vec4{(x0 + k : F32), (x1 + k : F32), (x2 + k : F32), (x3 + k : F32)}} case 1n+p: VecN{l, r} = v a b = VecT.shift(p, k, l) VecT.shift(p, k, r) VecN{a, b} # the sum, left half plus right half def VecT.sum(+n: Nat, v: VecT(n)) -> F32: match n: case 0n: VecL{x} = v Vec4.sum(x) case 1n+p: VecN{l, r} = v a b = VecT.sum(p, l) VecT.sum(p, r) (a + b : F32) def VecT.dot(+n: Nat, a: VecT(n), b: VecT(n)) -> F32: match n: case 0n: VecL{x} = a VecL{y} = b Vec4.dot(x, y) case 1n+p: VecN{al, ar} = a VecN{bl, br} = b l r = VecT.dot(p, al, bl) VecT.dot(p, ar, br) (l + r : F32) def VecT.max(+n: Nat, v: VecT(n)) -> F32: match n: case 0n: VecL{x} = v Vec4.max(x) case 1n+p: VecN{l, r} = v a b = VecT.max(p, l) VecT.max(p, r) F32.max(a, b) def VecT.min(+n: Nat, v: VecT(n)) -> F32: match n: case 0n: VecL{x} = v Vec4.min(x) case 1n+p: VecN{l, r} = v a b = VecT.min(p, l) VecT.min(p, r) F32.min(a, b) # the two halves' winners, the right one's index offset by the half's size def VecT.argmax.two(+off: U32, ix: U32 & F32, jy: U32 & F32) -> U32 & F32: (i, x) = ix (j, y) = jy Vec4.argmax.two((i, x), ((j + off : U32), y)) # the index and value of the largest element; the first wins ties def VecT.argmax(+n: Nat, v: VecT(n)) -> U32 & F32: match n: case 0n: VecL{x} = v Vec4.argmax(x) case 1n+p: VecN{l, r} = v a b = VecT.argmax(p, l) VecT.argmax(p, r) VecT.argmax.two(U32.from_nat(VecT.size(p)), a, b) # the elements in order, before acc # the tree is read borrowed (+v, matched in place), never consumed: a # reader that consumes a shared tree makes the compiler copy it, and # bend 2.0.16 miscompiles that copy when the depth is a literal (the # bias of a model: memory faults in the loop; issues/shared_vec_loop_*) def VecT.to_list.go(+n: Nat, +v: VecT(n), acc: List<&2, F32>) -> List<&2, F32>: match n v: case 0n VecL{x}: Vec4.to_list(x, acc) case 1n+p VecN{l, r}: VecT.to_list.go(p, l, VecT.to_list.go(p, r, acc)) def VecT.to_list(+n: Nat, +v: VecT(n)) -> List<&2, F32>: VecT.to_list.go(n, v, []) # the first 4 * 2^n elements of xs, padded with 0.0 def VecT.from_list(+n: Nat, +xs: List<&2, F32>) -> VecT(n): match n: case 0n: VecL{Vec4.from_list(xs)} case 1n+p: +h = VecT.size(p) l r = VecT.from_list(p, List.take(&2, F32, xs, h)) VecT.from_list(p, List.drop(&2, F32, xs, h)) VecN{l, r} def VecT.softmax.fin(+n: Nat, +e: VecT(n)) -> VecT(n): VecT.scale(n, F32.div(1.0, VecT.sum(n, e)), e) # exp(v - max v) / sum def VecT.softmax(+n: Nat, +v: VecT(n)) -> VecT(n): VecT.softmax.fin(n, VecT.exp(n, VecT.shift(n, (0.0 - VecT.max(n, v) : F32), v))) def VecT.show(+n: Nat, v: VecT(n)) -> String: List.show(~&2, ~F32, ~F32.show.of, VecT.to_list(n, v)) # Matrices # ======== # # MatT(r, c) has 4 * 2^r rows and 4 * 2^c columns of F32, as a tree of Blk # leaves. While both depths are positive a node is a quadrant split # (MatQ); when only rows remain it splits top and bottom (MatR); when # only columns remain, left and right (MatC). The block product recurses # on that tree, so it is cache-oblivious: every level halves the working # set, down to a 4x4 kernel in registers. Operands are + and shared, never # copied. The shape is the type: MatT.matmul only accepts MatT(r, k) and # MatT(k, c), and answers MatT(r, c). law MatT: for r: Nat for c: Nat Data type MatT.Leaf is Data: MatB{b: Blk} type MatT.Rows<-p: Nat, -c: Nat> is Data: MatR{top: MatT(p, c), bot: MatT(p, c)} type MatT.Cols<-r: Nat, -q: Nat> is Data: MatC{left: MatT(r, q), right: MatT(r, q)} type MatT.Quad<-p: Nat, -q: Nat> is Data: MatQ{tl: MatT(p, q), tr: MatT(p, q), bl: MatT(p, q), br: MatT(p, q)} def MatT(r, c): match r c: case 0n 0n: MatT.Leaf case 1n+p 0n: MatT.Rows
case 0n 1n+q: MatT.Cols<0n, q> case 1n+p 1n+q: MatT.Quad
def MatT.full(+r: Nat, +c: Nat, +x: F32) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{Blk.full(x)}
case 1n+p 0n:
t b = MatT.full(p, 0n, x) MatT.full(p, 0n, x)
MatR{t, b}
case 0n 1n+q:
l r2 = MatT.full(0n, q, x) MatT.full(0n, q, x)
MatC{l, r2}
case 1n+p 1n+q:
a b c2 d = MatT.full(p, q, x) MatT.full(p, q, x) MatT.full(p, q, x) MatT.full(p, q, x)
MatQ{a, b, c2, d}
def MatT.zeros(+r: Nat, +c: Nat) -> MatT(r, c):
MatT.full(r, c, 0.0)
def MatT.ones(+r: Nat, +c: Nat) -> MatT(r, c):
MatT.full(r, c, 1.0)
# the identity of MatT(n, n)
def MatT.eye(+n: Nat) -> MatT(n, n):
match n:
case 0n:
MatB{Blk.eye()}
case 1n+p:
a b c d = MatT.eye(p) MatT.zeros(p, p) MatT.zeros(p, p) MatT.eye(p)
MatQ{a, b, c, d}
def MatT.map(~f: F32 -> F32, +r: Nat, +c: Nat, m: MatT(r, c)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{b} = m
MatB{Blk.map(~f, b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.map(~f, p, 0n, t) MatT.map(~f, p, 0n, b)
MatR{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.map(~f, 0n, q, l) MatT.map(~f, 0n, q, r2)
MatC{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
w x y z = MatT.map(~f, p, q, a) MatT.map(~f, p, q, b) MatT.map(~f, p, q, c2) MatT.map(~f, p, q, d)
MatQ{w, x, y, z}
def MatT.zip(~f: F32 -> F32 -> F32, +r: Nat, +c: Nat, m: MatT(r, c), n: MatT(r, c)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{x} = m
MatB{y} = n
MatB{Blk.zip(~f, x, y)}
case 1n+p 0n:
MatR{mt, mb} = m
MatR{nt, nb} = n
x y = MatT.zip(~f, p, 0n, mt, nt) MatT.zip(~f, p, 0n, mb, nb)
MatR{x, y}
case 0n 1n+q:
MatC{ml, mr} = m
MatC{nl, nr} = n
x y = MatT.zip(~f, 0n, q, ml, nl) MatT.zip(~f, 0n, q, mr, nr)
MatC{x, y}
case 1n+p 1n+q:
MatQ{ma, mb, mc, md} = m
MatQ{na, nb, nc, nd} = n
w x y z = MatT.zip(~f, p, q, ma, na) MatT.zip(~f, p, q, mb, nb) MatT.zip(~f, p, q, mc, nc) MatT.zip(~f, p, q, md, nd)
MatQ{w, x, y, z}
def MatT.add(+r: Nat, +c: Nat, m: MatT(r, c), n: MatT(r, c)) -> MatT(r, c):
MatT.zip(~F32.add, r, c, m, n)
def MatT.sub(+r: Nat, +c: Nat, m: MatT(r, c), n: MatT(r, c)) -> MatT(r, c):
MatT.zip(~F32.sub, r, c, m, n)
# elementwise (Hadamard) product; the matrix product is MatT.matmul
def MatT.mul(+r: Nat, +c: Nat, m: MatT(r, c), n: MatT(r, c)) -> MatT(r, c):
MatT.zip(~F32.mul, r, c, m, n)
def MatT.exp(+r: Nat, +c: Nat, m: MatT(r, c)) -> MatT(r, c):
MatT.map(~F32.exp, r, c, m)
def MatT.scale(+r: Nat, +c: Nat, +k: F32, m: MatT(r, c)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{b} = m
MatB{Blk.scale(k, b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.scale(p, 0n, k, t) MatT.scale(p, 0n, k, b)
MatR{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.scale(0n, q, k, l) MatT.scale(0n, q, k, r2)
MatC{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
w x y z = MatT.scale(p, q, k, a) MatT.scale(p, q, k, b) MatT.scale(p, q, k, c2) MatT.scale(p, q, k, d)
MatQ{w, x, y, z}
def MatT.transpose(+r: Nat, +c: Nat, m: MatT(r, c)) -> MatT(c, r):
match r c:
case 0n 0n:
MatB{b} = m
MatB{Blk.transpose(b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.transpose(p, 0n, t) MatT.transpose(p, 0n, b)
MatC{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.transpose(0n, q, l) MatT.transpose(0n, q, r2)
MatR{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
w x y z = MatT.transpose(p, q, a) MatT.transpose(p, q, c2) MatT.transpose(p, q, b) MatT.transpose(p, q, d)
MatQ{w, x, y, z}
# the four quadrant sums of a product node, in one balanced fork
def MatT.add4(+p: Nat, +q: Nat, a0: MatT(p, q), b0: MatT(p, q), a1: MatT(p, q), b1: MatT(p, q),
a2: MatT(p, q), b2: MatT(p, q), a3: MatT(p, q), b3: MatT(p, q)) -> MatT(1n+p, 1n+q):
w x y z = MatT.add(p, q, a0, b0) MatT.add(p, q, a1, b1) MatT.add(p, q, a2, b2) MatT.add(p, q, a3, b3)
MatQ{w, x, y, z}
def MatT.add2r(+p: Nat, a0: MatT(p, 0n), b0: MatT(p, 0n), a1: MatT(p, 0n), b1: MatT(p, 0n)) -> MatT(1n+p, 0n):
x y = MatT.add(p, 0n, a0, b0) MatT.add(p, 0n, a1, b1)
MatR{x, y}
def MatT.add2c(+q: Nat, a0: MatT(0n, q), b0: MatT(0n, q), a1: MatT(0n, q), b1: MatT(0n, q)) -> MatT(0n, 1n+q):
x y = MatT.add(0n, q, a0, b0) MatT.add(0n, q, a1, b1)
MatC{x, y}
# a * b for square operands of one depth: the same recursion with one
# depth riding instead of three, which measures 3x faster per node
def MatT.matmul.sq(+d: Nat, +a: MatT(d, d), +b: MatT(d, d)) -> MatT(d, d):
match d:
case 0n:
MatB{x} = a
MatB{y} = b
MatB{Blk.matmul(x, y)}
case 1n+p:
MatQ{a0, a1, a2, a3} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 p2 q2 p3 q3 = MatT.matmul.sq(p, a0, b0) MatT.matmul.sq(p, a1, b2) MatT.matmul.sq(p, a0, b1) MatT.matmul.sq(p, a1, b3) MatT.matmul.sq(p, a2, b0) MatT.matmul.sq(p, a3, b2) MatT.matmul.sq(p, a2, b1) MatT.matmul.sq(p, a3, b3)
MatT.add4(p, p, p0, q0, p1, q1, p2, q2, p3, q3)
# a * b for any shapes: MatT(r, k) times MatT(k, c) is MatT(r, c). A node
# forks its whole burst of products at once (eight at a quadrant), then
# joins them with a fork of adds. Both operands are shared through +,
# never copied. Three depths ride every call, which is what the square
# path below avoids.
def MatT.matmul.any(+r: Nat, +k: Nat, +c: Nat, +a: MatT(r, k), +b: MatT(k, c)) -> MatT(r, c):
match r k c:
case 0n 0n 0n:
# the leaf goes through the square path on purpose: compiled
# without MatT.matmul.sq reachable, this recursion fails at runtime
# from depth 2 on (a bend 2.0.16 bug, see README); with it, it runs
MatT.matmul.sq(0n, a, b)
case 1n+p 0n 0n:
MatR{at, ab} = a
x y = MatT.matmul.any(p, 0n, 0n, at, b) MatT.matmul.any(p, 0n, 0n, ab, b)
MatR{x, y}
case 0n 0n 1n+s:
MatC{bl, br} = b
x y = MatT.matmul.any(0n, 0n, s, a, bl) MatT.matmul.any(0n, 0n, s, a, br)
MatC{x, y}
case 0n 1n+q 0n:
MatC{al, ar} = a
MatR{bt, bb} = b
x y = MatT.matmul.any(0n, q, 0n, al, bt) MatT.matmul.any(0n, q, 0n, ar, bb)
MatT.add(0n, 0n, x, y)
case 1n+p 1n+q 0n:
MatQ{a0, a1, a2, a3} = a
MatR{bt, bb} = b
p0 q0 p1 q1 = MatT.matmul.any(p, q, 0n, a0, bt) MatT.matmul.any(p, q, 0n, a1, bb) MatT.matmul.any(p, q, 0n, a2, bt) MatT.matmul.any(p, q, 0n, a3, bb)
MatT.add2r(p, p0, q0, p1, q1)
case 0n 1n+q 1n+s:
MatC{al, ar} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 = MatT.matmul.any(0n, q, s, al, b0) MatT.matmul.any(0n, q, s, ar, b2) MatT.matmul.any(0n, q, s, al, b1) MatT.matmul.any(0n, q, s, ar, b3)
MatT.add2c(s, p0, q0, p1, q1)
case 1n+p 0n 1n+s:
MatR{at, ab} = a
MatC{bl, br} = b
w x y z = MatT.matmul.any(p, 0n, s, at, bl) MatT.matmul.any(p, 0n, s, at, br) MatT.matmul.any(p, 0n, s, ab, bl) MatT.matmul.any(p, 0n, s, ab, br)
MatQ{w, x, y, z}
case 1n+p 1n+q 1n+s:
MatQ{a0, a1, a2, a3} = a
MatQ{b0, b1, b2, b3} = b
p0 q0 p1 q1 p2 q2 p3 q3 = MatT.matmul.any(p, q, s, a0, b0) MatT.matmul.any(p, q, s, a1, b2) MatT.matmul.any(p, q, s, a0, b1) MatT.matmul.any(p, q, s, a1, b3) MatT.matmul.any(p, q, s, a2, b0) MatT.matmul.any(p, q, s, a3, b2) MatT.matmul.any(p, q, s, a2, b1) MatT.matmul.any(p, q, s, a3, b3)
MatT.add4(p, s, p0, q0, p1, q1, p2, q2, p3, q3)
# a * b: MatT(r, k) times MatT(k, c) is MatT(r, c). For two square
# operands of one depth, MatT.matmul.sq is the same recursion with one
# depth riding instead of three, and 3x faster per node; a runtime
# dispatch to it is not possible without a cast, and any def that returns
# or rebuilds a MatT parameter makes the compiler reference-count every
# MatT node in the program (see AGENTS.md), so the choice is the caller's.
def MatT.matmul(+r: Nat, +k: Nat, +c: Nat, +a: MatT(r, k), +b: MatT(k, c)) -> MatT(r, c):
MatT.matmul.any(r, k, c, a, b)
def VecT.add2(+p: Nat, a0: VecT(p), b0: VecT(p), a1: VecT(p), b1: VecT(p)) -> VecT(1n+p):
x y = VecT.add(p, a0, b0) VecT.add(p, a1, b1)
VecN{x, y}
# m * v: MatT(r, c) times VecT(c) is VecT(r)
def MatT.matvec(+r: Nat, +c: Nat, +m: MatT(r, c), +v: VecT(c)) -> VecT(r):
match r c:
case 0n 0n:
MatB{b} = m
VecL{x} = v
VecL{Blk.matvec(b, x)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.matvec(p, 0n, t, v) MatT.matvec(p, 0n, b, v)
VecN{x, y}
case 0n 1n+q:
MatC{l, r2} = m
VecN{vl, vr} = v
x y = MatT.matvec(0n, q, l, vl) MatT.matvec(0n, q, r2, vr)
VecT.add(0n, x, y)
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
VecN{vl, vr} = v
p0 q0 p1 q1 = MatT.matvec(p, q, a, vl) MatT.matvec(p, q, b, vr) MatT.matvec(p, q, c2, vl) MatT.matvec(p, q, d, vr)
VecT.add2(p, p0, q0, p1, q1)
# the sum of every cell
def MatT.sum(+r: Nat, +c: Nat, m: MatT(r, c)) -> F32:
match r c:
case 0n 0n:
MatB{b} = m
Blk.sum(b)
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.sum(p, 0n, t) MatT.sum(p, 0n, b)
(x + y : F32)
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.sum(0n, q, l) MatT.sum(0n, q, r2)
(x + y : F32)
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
w x y z = MatT.sum(p, q, a) MatT.sum(p, q, b) MatT.sum(p, q, c2) MatT.sum(p, q, d)
((w + x) + (y + z) : F32)
# each row's sum: VecT(r)
def MatT.sum_rows(+r: Nat, +c: Nat, m: MatT(r, c)) -> VecT(r):
match r c:
case 0n 0n:
MatB{b} = m
VecL{Blk.sum_rows(b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.sum_rows(p, 0n, t) MatT.sum_rows(p, 0n, b)
VecN{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.sum_rows(0n, q, l) MatT.sum_rows(0n, q, r2)
VecT.add(0n, x, y)
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
p0 q0 p1 q1 = MatT.sum_rows(p, q, a) MatT.sum_rows(p, q, b) MatT.sum_rows(p, q, c2) MatT.sum_rows(p, q, d)
VecT.add2(p, p0, q0, p1, q1)
# each column's sum: VecT(c)
def MatT.sum_cols(+r: Nat, +c: Nat, m: MatT(r, c)) -> VecT(c):
match r c:
case 0n 0n:
MatB{b} = m
VecL{Blk.sum_cols(b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.sum_cols(p, 0n, t) MatT.sum_cols(p, 0n, b)
VecT.add(0n, x, y)
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.sum_cols(0n, q, l) MatT.sum_cols(0n, q, r2)
VecN{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
p0 q0 p1 q1 = MatT.sum_cols(p, q, a) MatT.sum_cols(p, q, c2) MatT.sum_cols(p, q, b) MatT.sum_cols(p, q, d)
VecT.add2(q, p0, q0, p1, q1)
def VecT.max2(+p: Nat, a0: VecT(p), b0: VecT(p), a1: VecT(p), b1: VecT(p)) -> VecT(1n+p):
x y = VecT.zip(~F32.max.of, p, a0, b0) VecT.zip(~F32.max.of, p, a1, b1)
VecN{x, y}
# each row's largest element: VecT(r)
def MatT.max_rows(+r: Nat, +c: Nat, m: MatT(r, c)) -> VecT(r):
match r c:
case 0n 0n:
MatB{b} = m
VecL{Blk.max_rows(b)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.max_rows(p, 0n, t) MatT.max_rows(p, 0n, b)
VecN{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.max_rows(0n, q, l) MatT.max_rows(0n, q, r2)
VecT.zip(~F32.max.of, 0n, x, y)
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
p0 q0 p1 q1 = MatT.max_rows(p, q, a) MatT.max_rows(p, q, b) MatT.max_rows(p, q, c2) MatT.max_rows(p, q, d)
VecT.max2(p, p0, q0, p1, q1)
# v added to every row: v is a VecT(c)
def MatT.add_row(+r: Nat, +c: Nat, m: MatT(r, c), +v: VecT(c)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{b} = m
VecL{x} = v
MatB{Blk.add_row(b, x)}
case 1n+p 0n:
MatR{t, b} = m
x y = MatT.add_row(p, 0n, t, v) MatT.add_row(p, 0n, b, v)
MatR{x, y}
case 0n 1n+q:
MatC{l, r2} = m
VecN{vl, vr} = v
x y = MatT.add_row(0n, q, l, vl) MatT.add_row(0n, q, r2, vr)
MatC{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
VecN{vl, vr} = v
w x y z = MatT.add_row(p, q, a, vl) MatT.add_row(p, q, b, vr) MatT.add_row(p, q, c2, vl) MatT.add_row(p, q, d, vr)
MatQ{w, x, y, z}
# row i minus v_i: v is a VecT(r), one scalar per row
def MatT.sub_col(+r: Nat, +c: Nat, m: MatT(r, c), +v: VecT(r)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{b} = m
VecL{x} = v
MatB{Blk.sub_col(b, x)}
case 1n+p 0n:
MatR{t, b} = m
VecN{vt, vb} = v
x y = MatT.sub_col(p, 0n, t, vt) MatT.sub_col(p, 0n, b, vb)
MatR{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.sub_col(0n, q, l, v) MatT.sub_col(0n, q, r2, v)
MatC{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
VecN{vt, vb} = v
w x y z = MatT.sub_col(p, q, a, vt) MatT.sub_col(p, q, b, vt) MatT.sub_col(p, q, c2, vb) MatT.sub_col(p, q, d, vb)
MatQ{w, x, y, z}
# row i times v_i: v is a VecT(r), one scalar per row
def MatT.scale_rows(+r: Nat, +c: Nat, m: MatT(r, c), +v: VecT(r)) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{b} = m
VecL{x} = v
MatB{Blk.scale_rows(b, x)}
case 1n+p 0n:
MatR{t, b} = m
VecN{vt, vb} = v
x y = MatT.scale_rows(p, 0n, t, vt) MatT.scale_rows(p, 0n, b, vb)
MatR{x, y}
case 0n 1n+q:
MatC{l, r2} = m
x y = MatT.scale_rows(0n, q, l, v) MatT.scale_rows(0n, q, r2, v)
MatC{x, y}
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
VecN{vt, vb} = v
w x y z = MatT.scale_rows(p, q, a, vt) MatT.scale_rows(p, q, b, vt) MatT.scale_rows(p, q, c2, vb) MatT.scale_rows(p, q, d, vb)
MatQ{w, x, y, z}
def MatT.softmax_rows.fin(+r: Nat, +c: Nat, +e: MatT(r, c)) -> MatT(r, c):
MatT.scale_rows(r, c, e, VecT.map(~(s => F32.div(1.0, s)), r, MatT.sum_rows(r, c, e)))
# softmax along each row: exp(row - max row) / sum
def MatT.softmax_rows(+r: Nat, +c: Nat, +m: MatT(r, c)) -> MatT(r, c):
MatT.softmax_rows.fin(r, c, MatT.exp(r, c, MatT.sub_col(r, c, m, MatT.max_rows(r, c, m))))
# Conversions
# -----------
# the first w of every row
def MatT.rows_take(+w: Nat, rows: List<&2, List<&2, F32>>) -> List<&2, List<&2, F32>>:
match rows:
case []:
[]
case row <> rest:
List.take(&2, F32, row, w) <> MatT.rows_take(w, rest)
# every row past its first w
def MatT.rows_drop(+w: Nat, rows: List<&2, List<&2, F32>>) -> List<&2, List<&2, F32>>:
match rows:
case []:
[]
case row <> rest:
List.drop(&2, F32, row, w) <> MatT.rows_drop(w, rest)
# the rows of xs side by side with the rows of ys
def MatT.rows_cat(xs: List<&2, List<&2, F32>>, ys: List<&2, List<&2, F32>>) -> List<&2, List<&2, F32>>:
match xs ys:
case x <> xt y <> yt:
List.append(&2, F32, x, y) <> MatT.rows_cat(xt, yt)
case _ _:
[]
# a leaf from up to four rows; missing rows and cells pad with 0.0
def MatT.from_rows.leaf(rows: List<&2, List<&2, F32>>) -> Blk:
match rows:
case r0 <> r1 <> r2 <> r3 <> rest:
Blk.from_rows(r0, r1, r2, r3)
case r0 <> r1 <> r2 <> []:
Blk.from_rows(r0, r1, r2, [])
case r0 <> r1 <> []:
Blk.from_rows(r0, r1, [], [])
case r0 <> []:
Blk.from_rows(r0, [], [], [])
case []:
Blk.zeros()
# a MatT(r, c) from its rows, row-major; short or missing rows pad with 0.0
def MatT.from_rows(+r: Nat, +c: Nat, +rows: List<&2, List<&2, F32>>) -> MatT(r, c):
match r c:
case 0n 0n:
MatB{MatT.from_rows.leaf(rows)}
case 1n+p 0n:
+h = VecT.size(p)
t b = MatT.from_rows(p, 0n, List.take(&2, List<&2, F32>, rows, h)) MatT.from_rows(p, 0n, List.drop(&2, List<&2, F32>, rows, h))
MatR{t, b}
case 0n 1n+q:
+w = VecT.size(q)
l r2 = MatT.from_rows(0n, q, MatT.rows_take(w, rows)) MatT.from_rows(0n, q, MatT.rows_drop(w, rows))
MatC{l, r2}
case 1n+p 1n+q:
+h = VecT.size(p)
+w = VecT.size(q)
+top = List.take(&2, List<&2, F32>, rows, h)
+bot = List.drop(&2, List<&2, F32>, rows, h)
a b c2 d = MatT.from_rows(p, q, MatT.rows_take(w, top)) MatT.from_rows(p, q, MatT.rows_drop(w, top)) MatT.from_rows(p, q, MatT.rows_take(w, bot)) MatT.from_rows(p, q, MatT.rows_drop(w, bot))
MatQ{a, b, c2, d}
# the rows of a MatT(r, c), row-major
def MatT.to_rows(+r: Nat, +c: Nat, m: MatT(r, c)) -> List<&2, List<&2, F32>>:
match r c:
case 0n 0n:
MatB{b} = m
Blk{a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33} = b
[[a00, a01, a02, a03], [a10, a11, a12, a13], [a20, a21, a22, a23], [a30, a31, a32, a33]]
case 1n+p 0n:
MatR{t, b} = m
List.append(&2, List<&2, F32>, MatT.to_rows(p, 0n, t), MatT.to_rows(p, 0n, b))
case 0n 1n+q:
MatC{l, r2} = m
MatT.rows_cat(MatT.to_rows(0n, q, l), MatT.to_rows(0n, q, r2))
case 1n+p 1n+q:
MatQ{a, b, c2, d} = m
List.append(&2, List<&2, F32>, MatT.rows_cat(MatT.to_rows(p, q, a), MatT.to_rows(p, q, b)), MatT.rows_cat(MatT.to_rows(p, q, c2), MatT.to_rows(p, q, d)))
# xs cut into n rows of w
def MatT.chunks(+n: Nat, +w: Nat, +xs: List<&2, F32>) -> List<&2, List<&2, F32>>:
match n:
case 0n:
[]
case 1n+p:
List.take(&2, F32, xs, w) <> MatT.chunks(p, w, List.drop(&2, F32, xs, w))
# a MatT(r, c) from 4 * 2^r times 4 * 2^c floats in row-major order
def MatT.from_list(+r: Nat, +c: Nat, xs: List<&2, F32>) -> MatT(r, c):
+r2 = r
MatT.from_rows(r2, c, MatT.chunks(VecT.size(r2), VecT.size(c), xs))
# the cells in row-major order
def MatT.to_list(+r: Nat, +c: Nat, m: MatT(r, c)) -> List<&2, F32>:
List.concat(&2, F32, MatT.to_rows(r, c, m))
def MatT.show.row(row: List<&2, F32>) -> String:
List.show(~&2, ~F32, ~F32.show.of, row)
def MatT.show(+r: Nat, +c: Nat, m: MatT(r, c)) -> String:
List.show(~&2, ~List<&2, F32>, ~MatT.show.row, MatT.to_rows(r, c, m))
# Sizes
# =====
#
# The public types. A Vec