main.bend relies on unsafe/foreign
raw source on the hub · import bend-ml-tensor-fork@0.1.0.0/main.bend as Main
bend-ml-tensor-fork: bend-ml-tensor-array's products, with the parallel blocks sharing their operands.
import bend-ml-tensor-array@0.1.12.0/main.bend as TA import bend-ml-tensor-fork@0.1.0.0/main.bend as TF TF.matmul(n, k, m, par, a, b) # the same arguments, result type and bits as TA.Mat.matmul
The six products (matmul, matmul_nt, matmul_tn and their accumulating forms) take tensor-array's Mat values and return its result types. A product that splits (tensor-array's split_depth, two or more rows) packs A and B once into cells of 8, as the sequential path does, and every node of the fork tree shares the two packed operands and C through Base's Array.fork: two handles to one block, no copy. Each leaf runs tensor-array's cells (t8_rows, t8_cols) straight into its own rows (or columns) of C. tensor-array's parallel path instead clones an operand at every node and hands each block's cells back as a list that is then written into C. A product that does not split, and a product with one row, runs tensor-array's sequential code. The bits are those of tensor-array at depth 0, at every depth and thread count (tests/bits.bend).
Not proved, and marked so: Array.fork and Array.join are @unsafe in Base (two handles to one array break the single
owner that the proofs rely on), so the defs below that call them are @unsafe, and bend main.bend names every def
that relies on them. Nothing checks that a leaf never writes A or B, or that two leaves' blocks of C are disjoint;
the blocks are the halves of the fork tree, as in tensor-array's own split. tests/bits.bend checks both by sampling.
All of it is Bend: no foreign code.
2 imports
import Base import bend-ml-tensor-array@0.1.12.0/main.bend as TA
Definitions
def tg_c source · line 24 · raw
@g:0xf94fab1939abde90f6dd66413a7964d8/main.TG -> Array<F32>
def rblock source · line 30 · raw
@+ac:Bool -> @cnt:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i0:U32 -> @+mu:U32 -> @a:Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8> -> @b:Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8> -> @c:Array<F32> -> Array<F32>
rows i0 .. i0+cnt-1 of C, every column
def cbrows source · line 34 · raw
@+ac:Bool -> @n:Nat -> @+cnt:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i:U32 -> @+j0:U32 -> @+mu:U32 -> @g:0xf94fab1939abde90f6dd66413a7964d8/main.TG -> 0xf94fab1939abde90f6dd66413a7964d8/main.TG
columns j0 .. j0+cnt-1 of C, rows i.. (n more)
def gemm_rows source · line 84 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> 0xf94fab1939abde90f6dd66413a7964d8/main.G
two or more rows: the shared tree when d > 0, tensor-array's sequential cells at d = 0
def gemm_any2 source · line 91 · raw
@+ac:Bool -> @one:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> 0xf94fab1939abde90f6dd66413a7964d8/main.G
def gemm_any source · line 99 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> 0xf94fab1939abde90f6dd66413a7964d8/main.G
TA.gemm_any's arguments and result; one row runs the scalar loop at every depth, as in tensor-array 0.1.12.0
def matmul source · line 105 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, k> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, m> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMul<n, k, m>
C = A · B (n x k) · (k x m)
def matmul_acc source · line 111 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, k> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, m> -> @c:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, m> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMul<n, k, m>
C + A · B, into C
def matmul_nt source · line 117 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, k> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<m, k> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMulNT<n, k, m>
C = A · Bᵀ (n x k) · (m x k)ᵀ: weights with one row per output
def matmul_nt_acc source · line 123 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, k> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<m, k> -> @c:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, m> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMulNT<n, k, m>
C + A · Bᵀ, into C
def matmul_tn source · line 129 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, n> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, m> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMulTN<n, k, m>
C = Aᵀ · B (k x n)ᵀ · (k x m): a weight gradient, Xᵀ · dY
def matmul_tn_acc source · line 135 · raw
@+n:Nat -> @+k:Nat -> @+m:Nat -> @par:Nat -> @a:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, n> -> @b:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<k, m> -> @c:0xf94fab1939abde90f6dd66413a7964d8/main.Mat<n, m> -> 0xf94fab1939abde90f6dd66413a7964d8/main.MMulTN<n, k, m>
C + Aᵀ · B, into C: dW += Xᵀ · dY
Unsafe
unsafe rpar source · line 43 · raw
@+ac:Bool -> @d:Nat -> @+cnt:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+i0:U32 -> @+mu:U32 -> @pa:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pb:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pc:Pair(Array<F32>, Array<F32>) -> Array<F32>
2^d blocks of rows of C, halved at each node; each pair holds two handles to one block
unsafe cpar source · line 53 · raw
@+ac:Bool -> @d:Nat -> @+cnt:Nat -> @+n:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @+j0:U32 -> @+mu:U32 -> @pa:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pb:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pc:Pair(Array<F32>, Array<F32>) -> Array<F32>
2^d blocks of columns of C
unsafe t8_tree source · line 63 · raw
@byrows:Bool -> @+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @pa:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pb:Pair(Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>, Array<0xf94fab1939abde90f6dd66413a7964d8/main.T8>) -> @pc:Pair(Array<F32>, Array<F32>) -> Array<F32>
by rows when C has at least as many rows as columns, as tensor-array splits
unsafe t8_go source · line 72 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+tk:Nat -> @+tku:U32 -> @+rot:Nat -> @c:Array<F32> -> @p:0xf94fab1939abde90f6dd66413a7964d8/main.PKK -> 0xf94fab1939abde90f6dd66413a7964d8/main.G
the packed copies are dropped after the tree; A and B (unpacked) go back to the caller
unsafe gemm_fork source · line 80 · raw
@+ac:Bool -> @d:Nat -> @+n:Nat -> @+m:Nat -> @+k:Nat -> @+arow:U32 -> @+sa:U32 -> @+sb:U32 -> @+bcol:U32 -> @a:Array<F32> -> @b:Array<F32> -> @c:Array<F32> -> 0xf94fab1939abde90f6dd66413a7964d8/main.G
C (n x m) = A · B (or C + A · B when ac), n >= 2, with 2^d blocks sharing A, B and C; in any layout of TA.gemm_any (arow, sa, sb, bcol); A and B come back intact