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 (with cuBLASLt bias/ReLU/GELU epilogues & sibling Q+K+V/gate+up fusion), FusedAttentionQKVProjection, hybrid cuBLAS batched-GEMM MHA/GQA/MQA & online-softmax FusedScaledDotProductAttention, FusedTopK, QuantizedGather, and FusedQuantizedDense/VJP across QuantLinear, QuantNF4, and all 12 GGML/GGUF block formats (Q4_0–Q8_0, IQ4_NL, Q2_K–Q8_K, with BF16/TF32 Tensor Core + cuBLASLt prefill for M >= 16 and hardware __dp4a MMVQ decode for M < 16).
    • Broad GPU compatibility: Embedded compute_75 PTX supports Turing (sm_75, e.g., RTX 2080 Ti) through Blackwell (sm_120, e.g., RTX 5090), using native BFloat16/Float16 Tensor Cores on Ampere+ (cc >= 80) and transparent software BFloat16 adaptation 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=2 default), CUDA 11.2+ cuMemAllocAsync/cuMemFreeAsync, ref-counted zero-copy views (Reshape, Bitcast, Identity, contiguous Transpose/BroadcastInDim/Slice), and in-place donated-buffer reuse.
  • 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).
    • FFT is not yet implemented (compute.ErrNotImplemented), and HasSharedBuffers() is false (discrete GPU VRAM).

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). Use notf32 or tf32=false for 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 lane 0 for 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). Use nograph or graph=false to force eager stream dispatch (automatically bypassed when GOMLX_OP_PROFILE is active or when a graph contains host-sync ops like NonZero, Sort, If, While, or FusedTopK).
  • cudnn / cudnn=<bool> / nocudnn: Enable or disable optional NVIDIA cuDNN (libcudnn.so.9/.8 or cudnn64_9.dll, if installed) for accelerating large 2D NHWC convolutions (im2col >= 20 MiB) (default: true). Smaller convolutions use built-in direct/im2col + cuBLASLt kernels, and the backend transparently falls back to built-in kernels when cuDNN is absent. Use nocudnn or cudnn=false to disable cuDNN explicitly.

Examples

  • GOMLX_BACKEND=cuda
  • GOMLX_BACKEND=cuda:device=1
  • GOMLX_BACKEND=cuda:notf32
  • GOMLX_BACKEND=cuda:nograph
  • GOMLX_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 (and CUDNN_PATH, CONDA_PREFIX, VIRTUAL_ENV for cuBLAS/cuBLASLt/cuDNN).
  • Standard system and versioned CUDA Toolkit directories (e.g., /usr/local/cuda/lib64, /usr/local/cuda-*/lib64 newest-first, /usr/lib/wsl/lib on 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 every N executions; 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.
Last updated December 1, 2026