# bend-gpu-kernels Data-parallel GPU kernels written in plain Bend 2 (no foreign code), for neural-network inference on `Array` with F32 values stored as bits. ```python import bend-gpu-kernels@/bang.bend as B # the bang machinery: AR, bang, g2, fv / uv import bend-gpu-kernels@/nn.bend as N # numeric helpers and kernels ``` `lanes.bend` holds the index arithmetic both use (which items a lane runs, which cells an item owns), in safe Bend so `LAWS.bend` can check it. `bang.bend`: one `!` bang over a fork tree of up to 2^14 leaves, each leaf running items t, t + l, t + 2l, ... over up to six shared arrays (record `AR`). `nn.bend`: `i2f`, round half to even `rne`, int8 `q8`, int32 dequantize `dq`, `silu`; kernels `lin_item` (small dense layer), row-segmented `ss_item` (sums of squares), `ns_item` (RMSNorm with modulation), `rz_item` (gated residual with partial sums), `am_item` + `qs_item` (two-bang int8 row quantization), `sg_item` (SwiGLU), and `sn_item` (normalized online softmax). Depends on `bend-ml-tensor-array@0.1.20.0`. A kernel step, as an IO step of its own so it runs on the GPU: ```python @unsafe def norm(st: B.AR) -> IO(B.AR): # 4,422 rows of 5,376 in 7 segments of 768; arrays x, p (norm weight), m (scale, shift), h (out), ps (partial sums) IO.pure(B.AR, B.bang(~N.ENs, ~N.ns_item, 30954n, N.ENs{768, 7, 326, 0.00001, 0, 5376, 0, 32256}, st)) ``` The kernels share arrays through Base's `@unsafe` `Array.fork`, and each was tested against a NumPy reference inside a 50-block diffusion transformer, at its int8 noise floor. ## Laws `bend PROOF.bend` checks the laws in `LAWS.bend` about `lanes.bend`, in well under a second. They are checks at literal values, not proofs for every input: Base has no theory of U32 division or overflow yet. (`bend package.bend` reports SOME PROOFS FAIL, because the package entry imports the `@unsafe` kernels; the gate is `bend PROOF.bend`.) | Law | What it checks | | - | - | | `share_small` | for 0, 3, 8, 16 and 37 items on 8 lanes, each lane runs exactly the items congruent to it, so every item runs once | | `share_real` | the boundary lanes of 30,954 and 4,838,400 items on 16,384 lanes get the right counts | | `seg_cells` | at rows of 768, 1,024 and 2,048 cells a segment, an item's cells (`i * seg`) are where its row and column say, and the largest real indices do not wrap | | `seg_rows` | in the last row of 7 segments, one item leads, and all find the row's partials and per-row values | What is not checked: that `bang`'s tree numbers its leaves 0 .. l - 1, and that the kernels write only through these defs (both are in `@unsafe` code). A row-segmented kernel's row width is `seg * nseg` by construction, so a caller cannot give segments that overlap; `qs_item` needs `seg` to be a multiple of 4. ## Tests `test/q8_test.bend` runs `q8` (int8 quantization) on 26 cases against values from PyTorch (`torch.clamp(torch.round(v / s), -128, 127)`, float32): rounding halves to even, values one ULP either side of a half, clamping at both ends, the smallest scale, and negative zero. Build and run it from `test/` with `bend q8_test.bend -o q8_test && ./q8_test`; it prints each mismatch and exits 1 if there is any. It is a test, not a law, because the proof checker does not evaluate F32 arithmetic. A version of `q8` that rounds halves up fails 4 of the cases. ## Kernel conventions - Every array is an `Array`; F32 values are stored as their bits. - A kernel is an item function `(+i: U32, +e: E, ar: B.AR) -> B.AR` plus a `Data` record `E` of its parameters. `B.bang(~E, ~item, n, e, ar)` runs items 0..n-1 in one bang; call it as its own IO step so it runs on the GPU. - Each item writes only its own cells (the arrays are shared through Base's `@unsafe` `Array.fork`), at positions from `lanes.bend`. The kernels themselves are not proved; each is tested against a NumPy reference through the programs that use it. - Row-segmented kernels take `seg` and `nseg`, and a row is `seg * nseg` cells; item i is row i / nseg, segment i % nseg, and owns cells i * seg .. i * seg + seg - 1. Rows times segments should reach about 16,384 items so every lane has work. Row reductions go through per-segment partials (`ps`, `pam`) written by the kernel before. ## What is fast in Bend 2.0.40 on CUDA (measured on an RTX 5090) - A bang has a fixed cost of about 1 ms, and it always runs 16,384 lanes. Fewer bangs, and enough items to fill the lanes, matter more than anything inside a kernel. - Each lane issues one load at a time: unrolling a loop to read 4 or 8 independent cells per step changed nothing. A lane walking its own contiguous row reads at about 440 GB/s overall; lanes walking interleaved cells (lane t reads t, t + 16384, ...) read at half that, but write at about 1.1 TB/s. - Lanes run in a run-time order within each 128-lane group, so the memory layout cannot be arranged for coalescing.