# 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: 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@/cublas.bend as CB import 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 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, b: Array, c: Array, n: U32, m: U32, k: U32) -> IO(Array & Array & Array): 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, b: Array, c: Array, 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 & Array & Array): 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, b: Array, c: Array, 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 & Array & Array): 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, b: Array, c: Array, 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 & Array & Array): 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) at once: the copy happens in the background, in load order, and a GEMM or # Wt.free on the handle waits for it (so queue weights early and compute while they arrive). 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, 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, wid: U32, c: Array, n: U32, m: U32, k: U32) -> IO(Array & Array): 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, wid: U32, c: Array, n: U32, m: U32, k: U32) -> IO(Array & Array): import "./effs/weights.c" import "./effs/weights_gemm.js" # ---- parked weights: kept in pinned host memory, fetched to the GPU when needed (since 0.1.3.0) ---- # For a program that keeps running (a server) with more weights than fit on the GPU. Wt.park reads a weight into # pinned host memory, in the background like Wt.load, and answers its handle. Wt.fetch(wid) queues a copy of it to a # new GPU buffer and returns at once; Gemm.i8w / Gemm.f32w on the handle wait for that copy, as they wait for a load. # Wt.drop(wid) releases the GPU copy after the GPU work already queued (it must come after the GEMMs that use it); # fetch it again for the next use. Fetches run in the order they are queued, and a fetch waits until the fetched # bytes not yet dropped leave room for it under the budget (Wt.budget, MiB, 4 GiB by default; a weight larger than the # budget is fetched when nothing else is held). So a program can queue every fetch of a pass at once and drop each # weight after its last use: the copies stream in as room frees up, while the GPU computes. A fetch that needs room # only a later drop would free waits for ever: drop in the order you fetch. # Wt.ready() waits until every load, park and fetch queued so far has reached its destination. def Wt.park(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32): import "./effs/weights.c" import "./effs/weights_park.js" def Wt.park_bf16(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32): import "./effs/weights.c" import "./effs/weights_park_bf16.js" def Wt.park_f16(path: String, off_hi: U32, off_lo: U32, bytes: U32) -> IO(U32): import "./effs/weights.c" import "./effs/weights_park_f16.js" def Wt.fetch(wid: U32) -> IO(Unit): import "./effs/weights.c" import "./effs/weights_fetch.js" def Wt.drop(wid: U32) -> IO(Unit): import "./effs/weights.c" import "./effs/weights_drop.js" def Wt.budget(mib: U32) -> IO(Unit): import "./effs/weights.c" import "./effs/weights_budget.js" def Wt.ready(u: Unit) -> IO(Unit): import "./effs/weights.c" import "./effs/weights_ready.js"