# 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 | | `Wt.park(path, off_hi, off_lo, bytes)` | reads a weight into pinned host memory instead; answers its handle (since 0.1.3.0) | | `Wt.park_bf16` / `Wt.park_f16` | the same, widening to F32 | | `Wt.fetch(wid)` | queues a copy of a parked weight to a new GPU buffer, within the budget | | `Wt.drop(wid)` | releases a parked weight's GPU copy, after the GPU work already queued | | `Wt.budget(mib)` | the GPU bytes fetched copies may hold at once (4 GiB by default) | | `Wt.ready()` | waits until every load, park and fetch queued so far has arrived | 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. ## Parked weights: more weights than fit on the GPU A program that keeps running (a server) may need more weights than the GPU holds. `Wt.park` keeps a weight in pinned host memory (read in the background, like `Wt.load`); `Wt.fetch` copies it to the GPU when it is needed and `Wt.drop` frees the copy after its last use, as often as the program likes. GEMMs on a parked handle wait for its fetch the way they wait for a load. Fetches run in the order they are queued, each once the fetched bytes not yet dropped leave room under the budget. So a pass can queue every fetch at once and drop each weight after its last GEMM: the copies stream in while the GPU computes, and GPU memory never holds more than the budget. Fetched copies come from the device's stream-ordered memory pool, so a drop costs no synchronization and the next fetch reuses its memory. They go in 4 MB pieces with at most two in flight: the Bend runtime and these effects wait for the whole GPU after each kernel (`cuCtxSynchronize`), copies on other streams included, so one large copy in flight would stall every kernel behind it. Rules: drop in the order you fetch (a fetch waiting for room that only a later drop frees waits for ever), and fetch a weight again only after dropping it. On an RTX 5090 (PCIe 5.0 x16), a 50-layer text encoder whose 24.4 GB of int8 weights are parked and fetched layer by layer under a 5 GiB budget runs in 0.51-0.56 s per prompt, about 47 GB/s of copies hidden behind its compute, while the 19.4 GB of another model's weights stay loaded on the 32 GB GPU. ## 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. `test/park_test.bend` (run `test/run.sh`) checks parked weights against loaded ones: eight int8 weights fetched, multiplied and dropped three times under a budget that holds two, and a bf16 one, every product equal to NumPy's and to the loaded weight's.