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 of backends/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:

BackendGOMLX_BACKENDHardware & PlatformsCGoDynamic ShapesSummary
XLA (Default)xlaCPU, NVIDIA GPU, Cloud TPU (Linux, macOS, Windows)YesStatic onlyHighest-performance whole-graph JIT compiler powered by OpenXLA & PJRT; auto-installs runtime plugins.
Pure GogoCPU (amd64, arm64, etc.) & WebAssemblyNoNative100% portable pure Go; AVX2/AVX-512 & ARM64 NEON/SME SIMD, quantized GGUF/QLoRA, and browser WASM.
Pure-Go CUDA (Experimental)cudaNVIDIA GPUs (sm_75+, Linux & Windows)NoNativeZero-CGo direct CUDA Driver + cuBLAS/cuBLASLt backend with CUDA Graphs and quantized GGUF GPU kernels.
Apple MPSmpsApple Silicon GPU (macOS)YesStatic onlyBuilt on macOS’s native Metal MPSGraph framework with zero external dependencies and shared unified memory.
Apple MLXmlxApple Silicon GPU (macOS)YesStatic & NativePowered by Apple’s MLX (brew install mlx); supports both compiled static and dynamic-shape execution (mlx:dynamic).
ONNX RuntimeonnxCPU, NVIDIA GPU, AMD ROCm GPU, Browser WebGPU/WASMYes*NativeExecutes 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:

  1. Environment variable GOMLX_BACKEND (recommended — no code changes needed):
    GOMLX_BACKEND=go go run .
    GOMLX_BACKEND="xla:cpu" go run .
    
  2. 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 0 is always used by default—you don’t need to specify it.
  • Multi-Device Addressing: Device numbers range from 0 to backend.NumDevices() - 1 when placing tensors on specific devices (e.g., tensors.FromShapeForBackend) or running distributed training (see compute/distributed and gomlx/core/tensors/dtensor).

Implementing a custom compute.Backend? See Backend Compliance Testing in Developing in GoMLX.

Last updated December 1, 2026