# 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 reads the file 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; a GEMM or `Wt.free` waits for copies still in flight. On an RTX 5090 (pinned copies at 24 GB/s) with the file in the page cache, that is about 11-12 GB/s: 24.4 GB of int8 weights in 2.2 s, three times faster than 0.1.0.0's one-thread loader. ## 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.