XLA

Also known as: Accelerated Linear Algebra · OpenXLA

On this page 7

XLA, short for Accelerated Linear Algebra, is an open-source domain-specific compiler for machine learning that takes a whole computation graph, optimizes it and emits fast machine code for CPUs, GPUs and TPUs. Among AI frameworks & tools, it sits below the user-facing libraries: JAX, TensorFlow and PyTorch hand it their operations, and XLA decides how those operations are actually executed on the hardware.

Google created XLA for TensorFlow, and it is now developed under the OpenXLA project together with other hardware and cloud vendors. Its main value is operator fusion: instead of launching one kernel per operation and writing every intermediate result to memory, XLA merges chains of operations into fewer, larger kernels, which reduces memory traffic and often shortens training and inference time in artificial intelligence workloads.

How XLA compiles a model

XLA works as a multi-stage compiler pipeline that turns a traced program into optimized device code. A framework first traces or captures the model and expresses it in StableHLO, a portable, versioned operation set built on MLIR. XLA then converts it into its internal HLO (High Level Operations) representation, where most optimization happens.

Optimization is split into target-independent passes, such as common subexpression elimination and algebraic simplification, and target-dependent passes, such as fusion decisions, memory layout assignment and kernel selection that depend on the specific accelerator. The final backend generates code through LLVM for CPUs and GPUs or through a dedicated backend for TPUs.

The following table summarizes the main stages and what each one contributes to the final program.

StageWhat happensEffect on performance
CaptureFramework traces operations into StableHLOExposes the whole graph to the compiler
HLO optimizationSimplification, constant folding, dead code removalRemoves redundant work
FusionElementwise and reduction chains merged into kernelsCuts memory reads and writes
Layout and buffer assignmentChooses tensor layouts and reuses memory buffersLowers peak memory use
Code generationLLVM or TPU backend emits device codeProduces hardware-specific kernels
Main stages of the XLA compilation pipeline

Why operator fusion and static shapes matter

Most deep learning operations are memory-bound rather than compute-bound: an activation function or a normalization step reads and writes far more bytes than it computes. When XLA fuses a matrix multiplication with the bias addition and activation that follow it, intermediate tensors stay in registers or on-chip memory instead of traveling through high-bandwidth memory, and kernel launch overhead disappears.

XLA relies on static tensor shapes known at compile time. Fixed shapes let it plan buffers exactly, unroll loops and pick tiling strategies, but every new input shape triggers a recompilation. Dynamic shape support exists in limited form, so practitioners usually pad or bucket inputs to a small set of sizes to keep the number of compiled programs manageable.

The table below lists the most important optimization techniques and typical conditions under which they pay off.

OptimizationMechanismTypical benefit
Operator fusionMerges consecutive operations into one kernelLargest gains on memory-bound layers
Layout assignmentSelects row, column or tiled tensor layoutsBetter use of matrix units
RematerializationRecomputes values instead of storing themLower memory at modest compute cost
AutotuningBenchmarks candidate GPU kernelsFaster matrix multiplications
SPMD partitioningSplits one program across many devicesScales training to large clusters
Key XLA optimizations and when they help

XLA in JAX, TensorFlow and PyTorch

XLA reaches developers through several front-end frameworks, each with a different degree of integration. In JAX, every jax.jit call compiles through XLA by default, which makes XLA the core execution engine. In TensorFlow, it is opt-in through jit_compile=True, and in PyTorch it is available through the separate PyTorch/XLA package, used mainly on TPUs.

Hardware vendors connect their devices through PJRT, a plugin runtime interface that lets one framework target new accelerators without framework-specific code. Large-model training also uses XLA sharding, based on GSPMD and its successor Shardy, so that one program annotated with partitioning hints runs across hundreds or thousands of chips.

FrameworkHow XLA is enabledTypical use
JAXAlways, through jax.jitResearch and large-scale TPU or GPU training
TensorFlowOptional jit_compile flagSpeeding up selected functions
PyTorch/XLASeparate package with lazy tensorsRunning PyTorch models on TPUs
Keras 3Through the JAX or TensorFlow backendHigh-level models with compiled execution
How major frameworks use XLA

XLA belongs to a family of machine learning compilers and runtimes, but each tool targets a different stage of the model life cycle. XLA is a training and inference compiler tied closely to its front ends, whereas several neighbors focus mainly on deployment-time inference optimization for a specific vendor or format.

ToolPrimary focusMain difference from XLA
TorchInductorCompiler behind torch.compileNative to PyTorch, generates Triton kernels
TensorRTNVIDIA inference optimizationInference only, NVIDIA GPUs only
Apache TVMDeployment across many devicesStronger focus on edge targets
ONNX RuntimeRunning exported ONNX modelsExecutes a portable format, not a framework trace
XLA compared with neighboring tools

In practice these tools often coexist rather than compete: a team may train with JAX and XLA on TPUs and later export the model for inference on NVIDIA hardware with TensorRT, choosing each component for the stage where it is strongest.

Limitations and common pitfalls

XLA delivers its benefits only when programs fit its compilation model, and several issues appear repeatedly in production use. The most frequent problems and their usual remedies are listed below.

  • Compilation time: large models can take seconds to minutes to compile, so a persistent compilation cache is worth enabling.
  • Recompilation storms: changing batch sizes or sequence lengths produce new programs; shape bucketing limits them.
  • Python control flow: data-dependent branches must use structured primitives such as lax.cond or lax.while_loop.
  • Harder debugging: fused kernels hide individual operations, so HLO dumps and profilers are needed to locate slow code.
  • Uneven gains: models dominated by large matrix multiplications may see only small speedups, since those kernels were already efficient.

Speedups therefore vary widely by model and hardware, from negligible to several times faster for memory-bound workloads. Reliable conclusions come only from benchmarking the actual model with and without compilation on the target device.

The simple explanation

Think of a model as a long recipe with many small steps. A slow cook does each step separately and washes the bowl every time. XLA reads the whole recipe first. It then combines steps that can share one bowl.

The result is the same dish. It simply takes less time and less cleanup. XLA does this for computer chips. It groups many small math steps into a few large ones, so the chip spends more time computing and less time waiting for data. The catch is that XLA must plan ahead. If the recipe changes size, it has to plan again.

How it is used in practice

A research team fine-tunes a 1-billion-parameter language model with JAX on a cloud TPU slice of 32 chips. Without compilation, each training step would execute hundreds of separate small operations, many of them normalizations and activations that mostly move data. With jax.jit, XLA fuses these operations and shards the model across all chips using sharding annotations on the parameters.

The first step takes roughly two minutes to compile, and every later step reuses the cached program. To avoid recompilation, the team pads all sequences to 512 or 2,048 tokens, so only two programs exist. After profiling the HLO output, they find that the attention layers run close to hardware limits, and the full fine-tuning run finishes noticeably faster and with lower memory use than an uncompiled baseline on the same hardware.