Backend
Understand what a compute backend does, which backends GoMLX provides, and how to get started.
What is a Backend?
GoMLX separates model and math definitions from hardware execution: you write portable computation graphs in Go, and GoMLX lowers them to a compute.Backend engine that compiles and runs them on your target hardware (CPU, GPU, TPU, or WebAssembly in the browser) and manages device memory.
When writing GoMLX programs, you rarely interact with the compute.Backend API directly. You simply create a single backend instance at program startup and pass it to your executors (graph.NewExec, model.NewExec, or train.NewTrainer), reusing it throughout the lifetime of your application.
Getting Started: Linking & Creating a Backend
Like database/sql drivers or image/png decoders in Go’s standard library, GoMLX backends are modular packages that register themselves at startup via a blank (_) import:
- Import all default backends (easiest): Blank-importing
_ "github.com/gomlx/gomlx/backends/default"automatically registers all standard backends supported on your OS. - Import a single backend (optional): If you want minimal dependencies or a pure-Go (
CGO_ENABLED=0) / WebAssembly build, you can blank-import just the individual backend package you need instead ofbackends/default(listed at the top of each backend’s page).
Then call compute.New() (or compute.MustNew()) at startup:
import (
"github.com/gomlx/compute"
_ "github.com/gomlx/gomlx/backends/default" // Registers standard backends via "_" import.
)
func main() {
backend := compute.MustNew()
// Reuse `backend` across your application...
}
Learning GoMLX? Stick with backends/default and the default ("xla") backend for now. By default, compute.New() selects "xla", automatically picks the fastest available hardware on your machine (TPU → NVIDIA CUDA GPU → CPU), and downloads the required runtime plugin on first run.
You don’t need to configure anything else to get started. Whenever you want finer control—such as selecting a specific GPU or accelerator, tuning CPU parallelization, building without CGo (CGO_ENABLED=0), targeting Apple Silicon GPUs or WebAssembly, or exporting to .onnx—check the corresponding backend page below.
Available Backends
All backends below are included automatically by github.com/gomlx/gomlx/backends/default on their supported platforms, except ONNX Runtime (which is included only when building with --tags=onnx). Click any backend for its standalone _ import package, accelerator selection, parallelization, memory options, and environment variables:
| Backend | GOMLX_BACKEND | Hardware & Platforms | CGo | Dynamic Shapes | Summary |
|---|---|---|---|---|---|
| XLA (Default) | xla | CPU, NVIDIA GPU, Cloud TPU (Linux, macOS, Windows) | Yes | Static only | Highest-performance whole-graph JIT compiler powered by OpenXLA & PJRT; auto-installs runtime plugins. |
| Pure Go | go | CPU (amd64, arm64, etc.) & WebAssembly | No | Native | 100% portable pure Go; AVX2/AVX-512 & ARM64 NEON/SME SIMD, quantized GGUF/QLoRA, and browser WASM. |
| Pure-Go CUDA (Experimental) | cuda | NVIDIA GPUs (sm_75+, Linux & Windows) | No | Native | Zero-CGo direct CUDA Driver + cuBLAS/cuBLASLt backend with CUDA Graphs and quantized GGUF GPU kernels. |
| Apple MPS | mps | Apple Silicon GPU (macOS) | Yes | Static only | Built on macOS’s native Metal MPSGraph framework with zero external dependencies and shared unified memory. |
| Apple MLX | mlx | Apple Silicon GPU (macOS) | Yes | Static & Native | Powered by Apple’s MLX (brew install mlx); supports both compiled static and dynamic-shape execution (mlx:dynamic). |
| ONNX Runtime | onnx | CPU, NVIDIA GPU, AMD ROCm GPU, Browser WebGPU/WASM | Yes* | Native | Executes graphs with ONNX Runtime (included with --tags=onnx), runs on WebGPU in browsers, and exports models to .onnx. |
*WebAssembly (GOOS=js GOARCH=wasm) uses ONNX Runtime Web without CGo.
Switching Backends
You can override the default backend or pass backend-specific options (<backend>:<options>) in two ways:
- Environment variable
GOMLX_BACKEND(recommended — no code changes needed):GOMLX_BACKEND=go go run . GOMLX_BACKEND="xla:cpu" go run . - Programmatically with
compute.NewWithConfig:backend, err := compute.NewWithConfig("go:parallelism=8")
(In unit tests, testutil.BuildTestBackend() respects GOMLX_BACKEND in the same way.)
See each backend’s dedicated page above for its supported <options> (such as accelerator/device selection, thread pools, memory allocators, and profiling flags).
Devices and DeviceNum
A single backend can manage multiple accelerator devices (for example, a multi-GPU server or a TPU pod), addressed via compute.DeviceNum:
- Single-Device Default: If you are using a single CPU or GPU, device number
0is always used by default—you don’t need to specify it. - Multi-Device Addressing: Device numbers range from
0tobackend.NumDevices() - 1when placing tensors on specific devices (e.g.,tensors.FromShapeForBackend) or running distributed training (seecompute/distributedandgomlx/core/tensors/dtensor).
Implementing a custom compute.Backend? See Backend Compliance Testing in Developing in GoMLX.