# bend-gpu-cublas cuBLAS matrix products on Bend 2 arrays, as foreign effects, with no compiler fork. Includes a weight store that keeps immutable matrices in GPU memory outside Bend's heap (no power-of-two padding). ```python import bend-gpu-cublas@/cublas.bend as CB ``` | Effect | What it does | | - | - | | `Gemm.i8(a, b, c, n, m, k)` | C(n x m) = A(n x k) · B(m x k)^T, int8 in (4 a cell, row-major), int32 out; `cublasGemmEx` | | `Gemm.f32(a, b, c, ta, tb, m, n, k, lda, ldb, ldc, sa, sb, sc, batch)` | `cublasSgemmStridedBatched`, column-major, F32 bits in U32 cells | | `Gemm.f32o(..., oa, ob, oc)` | the same, each operand starting at a cell offset (chunks of one buffer) | | `Gemm.tf32o(..., oa, ob, oc)` | `Gemm.f32o` on tensor cores in TF32 (inputs rounded to a 10-bit mantissa, FP32 accumulation) | | `Wt.load(path, off_hi, off_lo, bytes)` | copies bytes from a file offset into a new GPU buffer; answers its handle (0, 1, 2, ...) | | `Wt.load_bf16` / `Wt.load_f16` | the same, widening bf16 / fp16 values to F32 on the way | | `Gemm.i8w(a, wid, c, n, m, k)` | `Gemm.i8` with B taken from a loaded int8 buffer | | `Gemm.f32w(a, wid, c, n, m, k)` | C = A · W^T in F32 against a widened buffer | | `Wt.free(wid)` | releases a buffer | Each call runs in place on the arrays it is given, waits for the GPU (`cuCtxSynchronize`), and hands the arrays back. The `Wt.load` family loads in the background (since 0.1.2.0): a call allocates the GPU buffer, queues the load and answers the handle at once, and one loader thread works through the queue in order. It reads each 16 MB chunk with 16 threads at once (`pread`) into pinned staging buffers, widens bf16 / fp16 there in parallel, and copies to the GPU asynchronously, overlapping the next read. `Gemm.i8w`, `Gemm.f32w` and `Wt.free` wait only for their own weight: until its copies are queued, then, on the GPU, for its last one. So a program can queue all its weights first and do other work (make arrays, run earlier layers) while they arrive: layer 0's GEMMs run as soon as layer 0's weights are in. On an RTX 5090 with the file in the page cache, the loader reads at about 15 GB/s (24.4 GB of int8 weights in 1.6 s). In a diffusion pipeline that queues each model's weights before its other work, background loading took prompt to image from 10.7 s to 9.1 s, with the same results bit for bit. ## Requirements - A **native CUDA build** of a program that contains a `!` call (`bend main.bend -o main`), so Bend's heap is CUDA managed memory. In a CPU build (no `!`, or `+RTS --gpu off`) the effects stop with a message; the JS stubs throw. - `libcublas` at run time, opened with `dlopen` from `$CUDA_HOME/lib64`, `/usr/local/cuda/lib64` or the loader path. - The C code reads Bend runtime internals (`blk_loc`, `blk_cls`, `e.mem`, `gpu_dev`). Tested with **Bend 2.0.40** only; recheck after every Bend release. ## How it was checked Foreign code: nothing here is proved. On an RTX 5090, `Gemm.i8` matched a plain-Bend int8 GEMM on every cell (249.6 million cells over four transformer shapes, 0 differing) at 743-789 TOPS, within a few percent of calling cuBLAS directly. `Gemm.i8w` matched `Gemm.i8` on all 1,376,256 cells of a 5,376 x 16,128 weight. The F32, TF32 and weight-store effects run a 50-block diffusion transformer that matches a NumPy reference at its int8 noise floor.