~/bend-docscommunity

cublas.bend source

cublas.bend on the hub · documented module

# bend-gpu-cublas: cuBLAS GEMMs on Bend arrays, as foreign effects, plus a weight store in GPU memory outside Bend's heap.## Every function here needs a native CUDA build of a program that uses `!` (so Bend's heap is CUDA managed memory) and# libcublas at run time (found under $CUDA_HOME, /usr/local/cuda, or the loader path; it is opened with dlopen, so# nothing extra is linked). Arrays are Array<U32>: int8 values packed 4 a cell, int32 and F32 values one a cell (F32 as# its bits). The C code reads Bend runtime internals (blk_loc, blk_cls, e.mem, gpu_dev); tested with Bend 2.0.40 only.# Foreign code: nothing here is proved. Each GEMM is tested against a plain-Bend or NumPy reference (see README.md).##   import bend-gpu-cublas@<version>/cublas.bend as CBimport Base# ---- int8 ----# cuBLAS int8 GEMM as a Bend effect: C(n x m) = A(n x k) . B(m x k)^T, int8 inputs packed 4 per U32 cell (row-major,# little-endian, so an Array<U32> of k/4 words a row is exactly a row-major int8 matrix), int32 outputs in U32 cells.# Foreign code: tested against a plain-Bend int8 GEMM cell for cell (README.md), not proved. Needs a CUDA build of a program that uses# `!` (so Bend's heap is CUDA managed memory) and libcublas (found under $CUDA_HOME, /usr/local/cuda, or the loader path).# k must be a multiple of 4; the arrays must hold at least n*k/4, m*k/4 and n*m cells.def Gemm.i8(a: Array<U32>, b: Array<U32>, c: Array<U32>, n: U32, m: U32, k: U32) -> IO(Array<U32> & Array<U32> & Array<U32>):  import "./effs/gemm_i8.c"  import "./effs/gemm_i8.js"# ---- F32 and TF32, strided-batched, column-major ----# cuBLAS FP32 strided-batched GEMM as a Bend effect, column-major like cublasSgemmStridedBatched itself:# for each b < batch, C_b(m x n) = op(A_b) . op(B_b), alpha 1, beta 0; ta/tb 0 = N, 1 = T; leading dimensions and# strides in cells. Arrays hold F32 bits as U32 cells. Same requirements as Gemm.i8.def Gemm.f32(a: Array<U32>, b: Array<U32>, c: Array<U32>, ta: U32, tb: U32, m: U32, n: U32, k: U32, lda: U32, ldb: U32, ldc: U32, sa: U32, sb: U32, sc: U32, batch: U32) -> IO(Array<U32> & Array<U32> & Array<U32>):  import "./effs/gemm_f32.c"  import "./effs/gemm_f32.js"# Gemm.f32o: the same with A, B and C starting at cell offsets oa, ob, oc of their arrays (head chunks of one buffer).def Gemm.f32o(a: Array<U32>, b: Array<U32>, c: Array<U32>, ta: U32, tb: U32, m: U32, n: U32, k: U32, lda: U32, ldb: U32, ldc: U32, sa: U32, sb: U32, sc: U32, batch: U32, oa: U32, ob: U32, oc: U32) -> IO(Array<U32> & Array<U32> & Array<U32>):  import "./effs/gemm_f32.c"  import "./effs/gemm_f32o.js"# Gemm.tf32o: Gemm.f32o on tensor cores in TF32 (A and B rounded to a 10-bit mantissa, FP32 accumulation and output).def Gemm.tf32o(a: Array<U32>, b: Array<U32>, c: Array<U32>, ta: U32, tb: U32, m: U32, n: U32, k: U32, lda: U32, ldb: U32, ldc: U32, sa: U32, sb: U32, sc: U32, batch: U32, oa: U32, ob: U32, oc: U32) -> IO(Array<U32> & Array<U32> & Array<U32>):  import "./effs/gemm_f32.c"  import "./effs/gemm_tf32o.js"# ---- weight store: immutable buffers in GPU memory, by handle ----# Immutable int8 weights in GPU memory owned by foreign code (outside Bend's heap, so no power-of-two padding).# Wt.load copies `bytes` bytes of a file, from byte offset off_hi * 2^32 + off_lo, into a new device buffer and answers# its handle (0, 1, 2, ... in load order). Gemm.i8w is Gemm.i8 with B taken from a loaded buffer:# C(n x m) = A(n x k) . W(m x k)^T, int8 in, int32 out, in place. Needs a CUDA build of a program that uses `!`.# Wt.load_bf16: the same, from bf16 values widened to F32 (exactly: the bits shifted up 16), 2 * bytes on the GPU.# Wt.free releases a buffer (its handle must not be used again). Gemm.f32w: C(n x m) = A(n x k) . W(m x k)^T in F32# (cublasSgemm), A and C F32 bits in Array<U32>, W a buffer from Wt.load_bf16.def Wt.load_bf16(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32):  import "./effs/weights.c"  import "./effs/weights_load_bf16.js"# Wt.load_f16: the same from fp16 values, widened to F32 exactly.def Wt.load_f16(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32):  import "./effs/weights.c"  import "./effs/weights_load_f16.js"def Wt.free(wid: U32) -> IO(Unit):  import "./effs/weights.c"  import "./effs/weights_free.js"def Gemm.f32w(a: Array<U32>, wid: U32, c: Array<U32>, n: U32, m: U32, k: U32) -> IO(Array<U32> & Array<U32>):  import "./effs/weights.c"  import "./effs/weights_gemm_f32.js"def Wt.load(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32):  import "./effs/weights.c"  import "./effs/weights_load.js"def Gemm.i8w(a: Array<U32>, wid: U32, c: Array<U32>, n: U32, m: U32, k: U32) -> IO(Array<U32> & Array<U32>):  import "./effs/weights.c"  import "./effs/weights_gemm.js"