gpukit/gpukit.nu — the ergonomic GPU-compute facade over the gpu package.
The gpu package is the low-level interface: open a device, compile a CUDA-C kernel (JIT via NVRTC, or the host-C++ CPU backend), allocate device buffers, upload/download, build the argument vector, compute a grid, launch, sync, free. Every GPU-using package (onnx, objdet, anomaly, yoloe) hand- writes the same ~35 lines of that marshalling for each kernel it runs:
: GpuBuffer b_x ( gpu_alloc g ( n 8) ) // one per array … ( gpu_upload b_x (# u (vec_data [f] xs)) ) // upload each input … : (Vec i) args (vec_new [i]) // build the arg vector … ( vec_push [i] args (gpu_arg_buffer b_x) ) … ( gpu_launch k (gpu_grid n 256) 256 args ) ( gpu_sync g ) ( gpu_download (# *u (vec_data [f] out)) b_out ) ( gpu_free b_x ) … // free each buffer
gpukit collapses that to a list of typed bindings and one gk_run:
: GpuKit kit ( gk_open 0 ) : (Vec GkArg) call ( vec_new [GkArg] ) ( vec_push [GkArg] call ( gk_in_f xs ) ) // input double[] ( vec_push [GkArg] call ( gk_i64 n ) ) // long long scalar ( vec_push [GkArg] call ( gk_out_f out) ) // output double[] ( gk_run kit src my_kernel ( gk_grid n 256 ) 256 call ) ( vec_free [GkArg] call )
gk_run compiles-and-caches the kernel by name, allocates a device buffer per buffer binding, uploads inputs, builds the arg vector in binding order, launches, syncs, downloads outputs, and frees every device buffer. Kernel sources are cached on the kit, so a hot path compiles each kernel once.
gpukit adds no numerics of its own — it only marshals — so a kernel runs bit-for-bit the same through gk_run as through hand-written gpu_* calls, and the gpu package's CUDA / CPU-backend / pure equivalence is preserved.
Memory: GpuKit carries a stable kernel-cache Vec, so it is passed by value and mutated across calls (like an ArgParser). Free it with gk_close, which releases the cached kernels and the device.
: GkArg: GkArg {
i kind
*u host // host data pointer for a buffer binding
i bytes // buffer byte size
i argval // pre-encoded scalar arg (gpu_arg_*) for a scalar binding
}
A single argument to a kernel launch. kind 0 = input buffer 1 = output buffer 2 = scalar
: GkKernelEntry: GkKernelEntry {
String name
GpuKernel kernel
}
: GpuKit: GpuKit {
Gpu gpu
b ok
( Vec GkKernelEntry ) cache
}
@ gk_open i ordinal → *GpuKitOpen device ordinal (CUDA when present, else the gpu package's CPU backend). Returns a heap *GpuKit so it can be held as a long-lived singleton (its kernel cache persists across calls). Check gk_ok; even a failed open is safe to gk_close.
@ gk_ok *GpuKit kit → b@ gk_backend *GpuKit kit → s"cuda" or "cpu".
@ gk_device_name *GpuKit kit → s@ gk_close *GpuKit kit → vRelease every cached kernel, close the device, and free the kit.
@ gk_grid i n i block → iGrid size for n threads at the given block size (re-export of gpu_grid).
@ gk_in_f ( Vec f ) v → GkArg@ gk_in_i ( Vec i ) v → GkArg@ gk_out_f ( Vec f ) v → GkArgOutput buffers must be pre-sized by the caller; results are copied back in.
@ gk_out_i ( Vec i ) v → GkArg@ gk_buf_in *u host i bytes → GkArgRaw buffers, for element layouts other than f64/i64 (e.g. a packed float32 host buffer): pass the host pointer and byte size directly.
@ gk_buf_out *u host i bytes → GkArg@ gk_i64 i v → GkArg@ gk_i32 i v → GkArg@ gk_f32 f v → GkArg@ gk_compile *GpuKit kit s src s name → bWarm the cache: compile src (entry name) into the kit now and report whether it succeeded, so a long-lived caller can detect a bad kernel at setup instead of on the first launch.
@ gk_run *GpuKit kit s src s name i grid i block ( Vec GkArg ) call → bCompile-cached, marshal, launch, sync, download, free. call lists the kernel's arguments in declaration order (buffers and scalars interleaved exactly as the kernel signature expects). Returns F on any device error.
gpukit/kernels.nu — a small library of ready-made f64 kernels over gk_run.
These cover the operations ML code writes over and over. They generate the CUDA-C source, cache it on the kit (by a fixed entry name), and marshal through gk_run — so a caller never writes a kernel or a device buffer for the common cases.
Numerics: f is a C double, so every buffer is float64. Elementwise ops and gk_matmul_f accumulate in the same order a sequential host loop would, so they are bit-identical across the CUDA / CPU / pure backends and to a naive host implementation. gk_reduce_sum_f / gk_dot_f combine per-thread partial sums (a parallel order), so they match a host sum to rounding but are not guaranteed bit-identical to a strictly sequential accumulation.
@ gk_add_f *GpuKit kit ( Vec f ) out ( Vec f ) a ( Vec f ) b → b@ gk_sub_f *GpuKit kit ( Vec f ) out ( Vec f ) a ( Vec f ) b → b@ gk_mul_f *GpuKit kit ( Vec f ) out ( Vec f ) a ( Vec f ) b → b@ gk_div_f *GpuKit kit ( Vec f ) out ( Vec f ) a ( Vec f ) b → b@ gk_map_f *GpuKit kit s kname ( Vec f ) out ( Vec f ) in s expr → b── Unary map: out[i] = <expr>, with x bound to in[i] ──────────────── kname is the CUDA entry name (a valid C identifier, stable per expr so caching works). expr is C over the local double x, e.g. x*x, 1.0/(1.0+exp(-x)), x>0.0?x:0.0.
@ gk_matmul_f *GpuKit kit ( Vec f ) c ( Vec f ) a ( Vec f ) b i m i k i n → b── Matrix multiply: C[M×N] = A[M×K] · B[K×N] (row-major) ────────────── Each output element sums t=0..K in order, exactly as a host loop — so this is bit-identical to a naive sequential matmul.
@ gk_reduce_sum_f *GpuKit kit ( Vec f ) x → ?fSum of x. None on a device error.
@ gk_dot_f *GpuKit kit ( Vec f ) a ( Vec f ) b → ?fDot product a·b (equal lengths assumed). None on a device error.