Skip to content

About

Pure-Go CUDA compute backend for GoMLX (work in progress)

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

34 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

compute-cuda

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.

Key Features

  • Pure Go (CGO_ENABLED=0): Zero CGo, zero C/C++ compiler or nvcc required 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_75 PTX supports Turing (sm_75, e.g., RTX 2080 Ti) through Blackwell (sm_120, e.g., RTX 5090), with native BFloat16/Float16 Tensor Cores on Ampere+ (cc >= 80) and transparent software BFloat16 adaptation 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 (with cuBLASLt bias/ReLU/GELU epilogues & sibling Q+K+V/gate+up fusion), FusedAttentionQKVProjection, hybrid cuBLAS batched-GEMM MHA/GQA/MQA & online-softmax FusedScaledDotProductAttention, and FusedTopK.
    • Quantized ops: QuantizedGather, FusedQuantizedDense, and FusedQuantizedDenseVJP across QuantLinear, 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 + cuBLASLt prefill (M >= 16) and hardware __dp4a MMVQ decode (M < 16).
  • 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=2 default), CUDA 11.2+ cuMemAllocAsync/cuMemFreeAsync memory pools, ref-counted zero-copy views, and in-place buffer donation reuse.

Usage

With GoMLX Default Backends

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"
)

Configuration Options (GOMLX_BACKEND="cuda[:<options>]")

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"

Environment Variables

  • GOMLX_CUDA_LIB_DIR: Explicit directory override list searched first for libcuda, libcublas, libcublasLt, and optional libcudnn (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 (and CUDNN_PATH, CONDA_PREFIX, VIRTUAL_ENV for 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 every N executions; 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.

Direct Import (Pure-Go Binary)

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"
)

Runtime Requirements & Library Discovery

At runtime, the host needs:

  1. An NVIDIA GPU (sm_75 / Turing or newer) with a compatible NVIDIA driver (libcuda.so.1 on Linux, nvcuda.dll on Windows).
  2. NVIDIA cuBLAS (libcublas.so.13, libcublas.so.12, libcublas.so.11, or libcublas.so on Linux; cublas64_13.dll / 12 / 11 on Windows) for dense matrix multiplication (DotGeneral, FusedDense, ConvGeneral, GGML prefill).
  3. (Optional) NVIDIA cuDNN (libcudnn.so.9/.8 on Linux, cudnn64_9.dll/8.dll on Windows) for accelerating large 2D NHWC convolutions (im2col >= 20 MiB); if absent, built-in im2col + cuBLASLt / PTX convolution kernels are used transparently.

Libraries are searched in order:

  1. GOMLX_CUDA_LIB_DIR (explicit directory override list, searched first).
  2. OS dynamic linker paths (LD_LIBRARY_PATH, /etc/ld.so.cache, system dirs on Linux; PATH / System32 on Windows).
  3. CUDA_PATH, CUDA_HOME, CUDA_ROOT (and CONDA_PREFIX, VIRTUAL_ENV for cuBLAS).
  4. Standard OS 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).

Standard NVIDIA CUDA_VISIBLE_DEVICES is respected by the CUDA driver and reported in device-not-found diagnostics.

Limitations

  • FFT is not yet implemented (returns compute.ErrNotImplemented).
  • HasSharedBuffers() is false (discrete GPU VRAM; transfers use async H2D/D2H streams).
  • Native sub-byte dtypes (Int2, Uint2, Int4, Uint4) are supported via Quantization.PackedDType on Uint8 tensors rather than standalone DTypes.
  • Memory offloading / weight streaming (models larger than VRAM) is planned for future work.

Development & Testing

# 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/ptx

See 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.

Acknowledgments

License

This project is licensed under the Apache License, Version 2.0. See LICENSE for details.

About

Pure-Go CUDA compute backend for GoMLX (work in progress)

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages