Note
Experimental: This backend is freshly made and experimental. It passes the full compute/support/backendtest compliance suite and real-model parity tests (BERT/MiniLM/BGE FP32 & GGUF embedders, Timestral, Qwen 3.5 4B, Gemma 4 E4B, KaLM-Gemma3 12B), while performance tuning and API refinements are still ongoing.
github.com/gomlx/compute-cuda is a Pure-Go (CGO_ENABLED=0) direct CUDA compute backend (package cudabackend, registered as "cuda") for GoMLX / github.com/gomlx/compute.
It dynamically loads NVIDIA's Driver API (libcuda.so.1 / nvcuda.dll) and cuBLAS (libcublas.so.* / cublas64_*.dll) at runtime without CGo or a build-time CUDA Toolkit, and executes precompiled embedded PTX kernels (compute_75) directly on NVIDIA GPUs.
- Pure Go (
CGO_ENABLED=0): Zero CGo, zero C/C++ compiler ornvccrequired at build time. - Native Dynamic Shapes (
DynamicShapesNative): Runs variable batch sizes and sequence lengths with zero JIT recompilation stalls. - Broad NVIDIA GPU Support: Precompiled
compute_75PTX supports Turing (sm_75, e.g., RTX 2080 Ti) through Blackwell (sm_120, e.g., RTX 5090), with nativeBFloat16/Float16Tensor Cores on Ampere+ (cc >= 80) and transparent softwareBFloat16adaptation on Turing (cc < 80). - Full
StandardOps, Control Flow &FusedOps:- Element-wise, broadcasting, shape/slicing/indexing, reductions, scans, RNG (
Philox,ThreeFry,SplitMix64),DotGeneral(cuBLAS),ConvGeneral(im2col+cuBLAS & direct), pooling, BatchNorm, and closures/control flow (Call,If,While,Sort). - Transformer fused ops: all 12 activations/VJP (including
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, andFusedTopK. - Quantized ops:
QuantizedGather,FusedQuantizedDense, andFusedQuantizedDenseVJPacrossQuantLinear,QuantNF4, and all 12 GGML/GGUF formats (Q4_0,Q4_1,Q5_0,Q5_1,Q8_0,IQ4_NL,Q2_K,Q3_K,Q4_K,Q5_K,Q6_K,Q8_K), with GPU BF16/TF32 Tensor Core +cuBLASLtprefill (M >= 16) and hardware__dp4aMMVQ decode (M < 16).
- Element-wise, broadcasting, shape/slicing/indexing, reductions, scans, RNG (
- CUDA Graph Capture/Replay, Multi-Stream Lanes & Stream-Ordered VRAM: Automatic CUDA Graph capture/replay backed by a per-lane grow-only shared VRAM arena (supporting both static and dynamic shapes and in-place donated KV caches via
cuGraphExecUpdate_v2), bounded multi-stream execution lanes (streams=2default), CUDA 11.2+cuMemAllocAsync/cuMemFreeAsyncmemory pools, ref-counted zero-copy views, and in-place buffer donation reuse.
On Linux (amd64/arm64) and Windows (amd64), "cuda" is included automatically when importing GoMLX's default backends (exclude at build time with -tags=nocuda):
import (
"github.com/gomlx/compute"
_ "github.com/gomlx/gomlx/backends/default"
)Comma- or semicolon-separated options can be passed via GOMLX_BACKEND or compute.NewWithConfig:
| Option | Default | Description |
|---|---|---|
device=<N> |
0 |
CUDA device ordinal to target (0 .. deviceCount-1). |
tf32, tf32=<bool>, notf32 |
true |
Enable TF32 Tensor Core math for FP32 GEMMs and BF16 Tensor Core prefill for low-bit (<= 6-bit) GGML weights on Ampere+ GPUs (cc >= 80). Use notf32 / tf32=false for strict IEEE FP32 GEMMs. |
streams=<1..8> |
2 |
Number of concurrent execution lanes (dedicated CUDA streams + cuBLAS/cuBLASLt handles + 64 MiB workspaces). Single-threaded callers reuse lane 0 for CUDA Graph locality while concurrent callers overlap across lanes. |
graph, graph=<bool>, nograph |
true |
Enable automatic CUDA Graph capture/replay using a per-lane shared VRAM arena. Use nograph / graph=false to force eager kernel dispatch (automatically bypassed when GOMLX_OP_PROFILE is set or a graph contains host-sync ops). |
cudnn, cudnn=<bool>, nocudnn |
true |
Use optional NVIDIA cuDNN (libcudnn.so.9/.8 or cudnn64_9.dll, if installed) for large 2D NHWC convolutions (im2col >= 20 MiB); smaller convolutions use built-in direct/im2col + cuBLASLt kernels. Silently falls back to built-in kernels when cuDNN is absent; use nocudnn / cudnn=false to disable. |
Examples:
export GOMLX_BACKEND=cuda
# Target a specific GPU ordinal:
export GOMLX_BACKEND="cuda:device=1"
# Disable TF32 / BF16 Tensor Core fast-math for strict IEEE FP32 GEMMs:
export GOMLX_BACKEND="cuda:notf32"
# Disable CUDA Graph capture/replay (force eager stream dispatch):
export GOMLX_BACKEND="cuda:nograph"
# Combine multiple options:
export GOMLX_BACKEND="cuda:device=0,streams=4,notf32"GOMLX_CUDA_LIB_DIR: Explicit directory override list searched first forlibcuda,libcublas,libcublasLt, and optionallibcudnn(before OS dynamic linker paths).CUDA_VISIBLE_DEVICES: Standard NVIDIA GPU visibility filter (respected by the CUDA driver and reported in device-not-found diagnostics).CUDA_PATH,CUDA_HOME,CUDA_ROOT(andCUDNN_PATH,CONDA_PREFIX,VIRTUAL_ENVfor cuBLAS/cuBLASLt/cuDNN): Fallback library discovery roots.GOMLX_OP_PROFILE: Opt-in per-op CUDA event profiler ("1","true", or<N>to print a per-op GPU timing table everyNexecutions; disables CUDA Graph capture while enabled).GOMLX_CUDA_GRAPH_DEBUG: When set to any non-empty value (e.g.,1), logs CUDA Graph capture/update/eligibility diagnostics to stderr.
Import compute-cuda directly alongside gobackend (or alone) for a CGO_ENABLED=0 binary without XLA/ONNX dependencies:
import (
"github.com/gomlx/compute"
_ "github.com/gomlx/compute-cuda"
)At runtime, the host needs:
- An NVIDIA GPU (
sm_75/ Turing or newer) with a compatible NVIDIA driver (libcuda.so.1on Linux,nvcuda.dllon Windows). - NVIDIA cuBLAS (
libcublas.so.13,libcublas.so.12,libcublas.so.11, orlibcublas.soon Linux;cublas64_13.dll/12/11on Windows) for dense matrix multiplication (DotGeneral,FusedDense,ConvGeneral, GGML prefill). - (Optional) NVIDIA cuDNN (
libcudnn.so.9/.8on Linux,cudnn64_9.dll/8.dllon Windows) for accelerating large 2D NHWC convolutions (im2col>= 20 MiB); if absent, built-inim2col+cuBLASLt/ PTX convolution kernels are used transparently.
Libraries are searched in order:
GOMLX_CUDA_LIB_DIR(explicit directory override list, searched first).- OS dynamic linker paths (
LD_LIBRARY_PATH,/etc/ld.so.cache, system dirs on Linux;PATH/System32on Windows). CUDA_PATH,CUDA_HOME,CUDA_ROOT(andCONDA_PREFIX,VIRTUAL_ENVfor cuBLAS).- Standard OS 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).
Standard NVIDIA CUDA_VISIBLE_DEVICES is respected by the CUDA driver and reported in device-not-found diagnostics.
FFTis not yet implemented (returnscompute.ErrNotImplemented).HasSharedBuffers()isfalse(discrete GPU VRAM; transfers use async H2D/D2H streams).- Native sub-byte dtypes (
Int2,Uint2,Int4,Uint4) are supported viaQuantization.PackedDTypeonUint8tensors rather than standaloneDTypes. - Memory offloading / weight streaming (models larger than VRAM) is planned for future work.
# Pure-Go build & full compliance test suite
CGO_ENABLED=0 go test -v ./...
# Concurrency / race detector verification
go test -race ./...
# Regenerate embedded PTX kernels after editing kernels/gomlx/*.cu (requires nvcc)
go generate ./internal/ptxSee docs/Plan.md for the architecture plan and design decisions log, and docs/Optimizations.md for the performance optimization log, profiling findings, and future ideas.
- ggml / llama.cpp for the GGML quantization block format specifications (
Q4_0..Q8_K,Q8_1) implemented inkernels/gomlx/quant.cu. - ebitengine/purego for Pure-Go (
CGO_ENABLED=0) dynamic library loading.
This project is licensed under the Apache License, Version 2.0. See LICENSE for details.