Pure-Go CUDA Backend ("cuda")
Experimental CGO-free direct NVIDIA GPU backend with native dynamic shapes, CUDA Graphs, cuBLAS/cuBLASLt, and quantized GGUF kernels.
Importing
- Included by default in
backends/default? Yes — automatically included on Linux (amd64/arm64) and Windows (amd64) when importing_ "github.com/gomlx/gomlx/backends/default"(exclude at build time with--tags=nocuda, saving ~4.5 MiB of embedded PTX kernels). - Standalone blank import (links only the pure-Go CUDA backend,
CGO_ENABLED=0):import _ "github.com/gomlx/compute-cuda"
Overview
The "cuda" backend (Experimental) is a Pure-Go (CGO_ENABLED=0) direct CUDA backend implemented in github.com/gomlx/compute-cuda. It dynamically loads NVIDIA’s Driver API (libcuda.so.1 / nvcuda.dll) and cuBLAS (libcublas.so.* / cublas64_*.dll) at runtime and launches precompiled embedded PTX kernels (compute_75) directly on NVIDIA GPUs—requiring no CGo and no build-time CUDA Toolkit (nvcc).
- Pros:
- Zero CGo & zero build-time CUDA dependencies: Compiles cleanly with
CGO_ENABLED=0. - Native dynamic shapes (
DynamicShapesNative): Executes variable batch sizes and sequence lengths with zero JIT recompilation stalls (especially beneficial for embeddings, variable-length inference, and GGUF models). - Full fused & quantized GPU kernels: Native CUDA implementations of
FusedActivation(all 12 activations incl.SwiGLU),FusedSoftmax,FusedLayerNorm/RMSNorm& VJP (with compile-time pre-/post-norm residual-add fusion),FusedRoPE(128-bit split-half & general/partial/interleaved),FusedDense/VJP (withcuBLASLtbias/ReLU/GELU epilogues & siblingQ+K+V/gate+upfusion),FusedAttentionQKVProjection, hybrid cuBLAS batched-GEMM MHA/GQA/MQA & online-softmaxFusedScaledDotProductAttention,FusedTopK,QuantizedGather, andFusedQuantizedDense/VJP acrossQuantLinear,QuantNF4, and all 12 GGML/GGUF block formats (Q4_0–Q8_0,IQ4_NL,Q2_K–Q8_K, with BF16/TF32 Tensor Core +cuBLASLtprefill forM >= 16and hardware__dp4aMMVQ decode forM < 16). - Broad GPU compatibility: Embedded
compute_75PTX supports Turing (sm_75, e.g., RTX 2080 Ti) through Blackwell (sm_120, e.g., RTX 5090), using nativeBFloat16/Float16Tensor Cores on Ampere+ (cc >= 80) and transparent softwareBFloat16adaptation on pre-Ampere GPUs (cc < 80). - CUDA Graph capture/replay, multi-stream lanes & stream-ordered VRAM: Automatically captures and replays graphs using a per-lane grow-only shared VRAM arena (including dynamic shapes and zero-copy in-place donated KV caches via
cuGraphExecUpdate_v2), bounded multi-stream execution lanes (streams=2default), CUDA 11.2+cuMemAllocAsync/cuMemFreeAsync, ref-counted zero-copy views (Reshape,Bitcast,Identity, contiguousTranspose/BroadcastInDim/Slice), and in-place donated-buffer reuse.
- Zero CGo & zero build-time CUDA dependencies: Compiles cleanly with
- Cons / Limitations:
- Freshly made and experimental; does not perform whole-graph JIT kernel fusion like XLA (relies on cuBLAS/cuBLASLt, compile-time pattern fusion passes, CUDA Graphs, and GoMLX
FusedOps). FFTis not yet implemented (compute.ErrNotImplemented), andHasSharedBuffers()isfalse(discrete GPU VRAM).
- Freshly made and experimental; does not perform whole-graph JIT kernel fusion like XLA (relies on cuBLAS/cuBLASLt, compile-time pattern fusion passes, CUDA Graphs, and GoMLX
Configuration Options
Configure via GOMLX_BACKEND="cuda[:<options>]" or compute.NewWithConfig(...) using comma- or semicolon-separated options:
device=<N>: CUDA device ordinal to target (default:0).tf32/tf32=<bool>/notf32: Enable or disable TF32 Tensor Core math for FP32 GEMMs and BF16 Tensor Core prefill for low-bit (<= 6-bit) GGML weights on Ampere+ GPUs (cc >= 80) (default:true). Usenotf32ortf32=falsefor strict IEEE FP32 GEMMs.streams=<1..8>: Number of bounded concurrent execution lanes (dedicated CUDA streams + cuBLAS/cuBLASLt handles + 64 MiB workspaces, default:2). Single-threaded callers reuse lane0for CUDA Graph cache locality while concurrent callers overlap across lanes.graph/graph=<bool>/nograph: Enable or disable automatic CUDA Graph capture and replay backed by a per-lane shared VRAM arena (default:true). Usenographorgraph=falseto force eager stream dispatch (automatically bypassed whenGOMLX_OP_PROFILEis active or when a graph contains host-sync ops likeNonZero,Sort,If,While, orFusedTopK).cudnn/cudnn=<bool>/nocudnn: Enable or disable optional NVIDIA cuDNN (libcudnn.so.9/.8orcudnn64_9.dll, if installed) for accelerating large 2D NHWC convolutions (im2col>= 20 MiB) (default:true). Smaller convolutions use built-in direct/im2col+cuBLASLtkernels, and the backend transparently falls back to built-in kernels when cuDNN is absent. Usenocudnnorcudnn=falseto disable cuDNN explicitly.
Examples
GOMLX_BACKEND=cudaGOMLX_BACKEND=cuda:device=1GOMLX_BACKEND=cuda:notf32GOMLX_BACKEND=cuda:nographGOMLX_BACKEND="cuda:device=0,streams=4,notf32"
Runtime Requirements & Environment Variables
Requires an NVIDIA GPU (sm_75 / Turing or newer) with a compatible NVIDIA driver (libcuda.so.1 / nvcuda.dll) and cuBLAS (libcublas.so.13/.12/.11/.so or cublas64_*.dll; libcublasLt is loaded optionally for fused bias/activation GEMM epilogues, and libcudnn.so.9/.8 is loaded optionally for large 2D NHWC convolutions).
Library Discovery Order
Dynamic library discovery searches in order and reports every checked variable and directory if a library is missing:
GOMLX_CUDA_LIB_DIR: Explicit directory override list (searched first, before OS dynamic linker paths).LD_LIBRARY_PATH(Linux) /PATH(Windows) and OS dynamic linker cache (/etc/ld.so.cache,System32).CUDA_PATH,CUDA_HOME,CUDA_ROOT(andCUDNN_PATH,CONDA_PREFIX,VIRTUAL_ENVfor cuBLAS/cuBLASLt/cuDNN).- Standard system and versioned CUDA Toolkit directories (e.g.,
/usr/local/cuda/lib64,/usr/local/cuda-*/lib64newest-first,/usr/lib/wsl/libon WSL2,/usr/lib/x86_64-linux-gnu). CUDA_VISIBLE_DEVICES: Respected by the NVIDIA driver to restrict visible GPU ordinals (included in device-not-found / out-of-range error messages).
Diagnostics & Profiling
GOMLX_OP_PROFILE: Opt-in per-op CUDA event profiler ("1","true", or<N>to print a per-op GPU timing summary everyNexecutions; disables CUDA Graph capture while active).GOMLX_CUDA_GRAPH_DEBUG: When set to any non-empty value (e.g.,1), logs CUDA Graph capture/update/eligibility diagnostics to stderr.