XLA Backend ("xla")
GoMLX's default, highest-performance JIT compiler backend powered by OpenXLA and PJRT for CPU, NVIDIA CUDA GPUs, and Google Cloud TPUs.
Importing
- Included by default in
backends/default? Yes — automatically included on Linux (amd64/arm64), macOS (arm64), and Windows (amd64) when importing_ "github.com/gomlx/gomlx/backends/default"(exclude at build time with--tags=noxla). - Standalone blank import (to include only the XLA backend, with PJRT auto-installation):(Or import
import _ "github.com/gomlx/go-xla/compute/xla/autoinstall"_ "github.com/gomlx/go-xla/compute/xla"to link XLA without the PJRT auto-installer.)
Overview
"xla" is the default and highest-performance backend in GoMLX, implemented in github.com/gomlx/go-xla. It calls OpenXLA (the compiler powering JAX, TensorFlow, and PyTorch/XLA) to compile computation graphs into optimized native machine code.
- Pros: Very fast execution, supports CPU, NVIDIA GPUs (CUDA), and Google Cloud TPUs, and performs whole-graph operator fusion and memory layout optimizations automatically.
- Cons: Relies on CGo (requiring C/C++ runtime libraries) and only supports static shapes (compilation is tied to fixed input dimensions; see Dynamic Shapes & Bucketing).
Selecting an Accelerator & Configuration Options
By default, GOMLX_BACKEND=xla (or compute.New() with default imports) attempts to use TPU, CUDA (NVIDIA GPU), or CPU in that order.
You can configure the plugin and options explicitly using the format:
xla:<plugin>[,<option>=<value>]...
where <plugin> can be "cpu", "cuda", "tpu", or an explicit filesystem path to a PJRT plugin library.
Supported Options
tf32(boolean, defaulttrue): Controls whether to use TF32 Tensor Cores forDotGeneraloperations onfloat32tensors (faster on modern NVIDIA GPUs).shared_buffer(boolean, defaulttrueon"cpu"): Controls whether to use host/device shared buffers on CPU (device=CPU).preallocate(boolean, defaultfalse): Whether the CUDA PJRT plugin preallocates a large fraction of GPU memory at startup.memory_fraction(float, default0.75): Fraction of GPU memory to preallocate whenpreallocate=true(CUDA only).allocator(string, default"default"): Which CUDA memory allocator to use (CUDA only):"default"(alias for"bfc") /"bfc": Best-fit with coalescing, avoids fragmentation."cuda_async": Dynamic stream-ordered allocation without preallocation."platform": Unbuffered driver allocation (slow, useful for debugging)."vmm": CUDA Virtual Memory Management allocator.
visible_devices(semicolon-separated integers, e.g."0;1;2"): Restricts the device IDs made visible to the backend.use_tfrt_gpu_client(boolean, defaultfalse): Uses the TFRT GPU client dispatcher.
Examples
GOMLX_BACKEND=xla:cpu: Force CPU execution via XLA.GOMLX_BACKEND=xla:cuda: Force NVIDIA CUDA GPU execution via XLA.GOMLX_BACKEND="xla:cuda,preallocate=true": Use XLA CUDA and preallocate 75% (the defaultmemory_fraction) of VRAM for faster memory management.
Low-Level XLA_FLAGS
PJRT plugins also read the XLA_FLAGS environment variable for lower-level XLA compiler flags. Set XLA_FLAGS=--help when running your program to print all supported XLA flags.
Pluggable PJRT Plugin Architecture
XLA uses a modular plugin architecture: it defines a standard C API called PJRT (“Pretty much Just another RunTime”) and an intermediate representation (StableHLO) to express computations, which hardware-specific plugins (open- or closed-source) implement.
If you are only using GoMLX, you don’t need to worry about PJRT internals, rendered below for reference:
graph TD
GoMLX["GoMLX App"] --> ComputeAPI["compute.Backend Interface"]
ComputeAPI --> XLA["xla Backend (CGO)"]
XLA --> PJRTLoader["PJRT Dynamic Linker"]
PJRTLoader -->|Load plugin.so| CPU["CPU PJRT Plugin"]
PJRTLoader -->|Load plugin.so| CUDA["CUDA (Nvidia) PJRT Plugin"]
PJRTLoader -->|Load plugin.so| TPU["TPU (Google Cloud) PJRT Plugin"]PJRT plugins are dynamically loaded shared libraries (.so on Linux, .dylib on macOS, .dll on Windows), typically one plugin per target hardware accelerator.
PJRT Auto-Installation
To simplify setup, GoMLX includes an auto-installer (github.com/gomlx/go-xla/compute/xla/autoinstall, imported by backends/default). At startup, the xla backend checks whether a compatible PJRT plugin is installed locally; if not, it downloads and caches the required binaries in:
- Linux:
~/.local/lib/go-xla/ - macOS:
~/Library/Application Support/go-xla/ - Windows:
~\AppData\Local\go-xla\
Disabling Auto-Installation
For offline environments or reproducible production builds (such as Docker images), auto-installation can be disabled:
- Via Environment Variable: Set
GOMLX_NO_AUTO_INSTALL=1. - Programmatically: Call
xla.EnableAutoInstall(false)before initializing the backend.
See github.com/gomlx/go-xla for manual pre-installation instructions in custom or containerized environments.