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.
| Stage | What happens | Effect on performance |
|---|---|---|
| Capture | Framework traces operations into StableHLO | Exposes the whole graph to the compiler |
| HLO optimization | Simplification, constant folding, dead code removal | Removes redundant work |
| Fusion | Elementwise and reduction chains merged into kernels | Cuts memory reads and writes |
| Layout and buffer assignment | Chooses tensor layouts and reuses memory buffers | Lowers peak memory use |
| Code generation | LLVM or TPU backend emits device code | Produces hardware-specific kernels |
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.
| Optimization | Mechanism | Typical benefit |
|---|---|---|
| Operator fusion | Merges consecutive operations into one kernel | Largest gains on memory-bound layers |
| Layout assignment | Selects row, column or tiled tensor layouts | Better use of matrix units |
| Rematerialization | Recomputes values instead of storing them | Lower memory at modest compute cost |
| Autotuning | Benchmarks candidate GPU kernels | Faster matrix multiplications |
| SPMD partitioning | Splits one program across many devices | Scales training to large clusters |
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.
| Framework | How XLA is enabled | Typical use |
|---|---|---|
| JAX | Always, through jax.jit | Research and large-scale TPU or GPU training |
| TensorFlow | Optional jit_compile flag | Speeding up selected functions |
| PyTorch/XLA | Separate package with lazy tensors | Running PyTorch models on TPUs |
| Keras 3 | Through the JAX or TensorFlow backend | High-level models with compiled execution |
How XLA compares with related compilers
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.
| Tool | Primary focus | Main difference from XLA |
|---|---|---|
| TorchInductor | Compiler behind torch.compile | Native to PyTorch, generates Triton kernels |
| TensorRT | NVIDIA inference optimization | Inference only, NVIDIA GPUs only |
| Apache TVM | Deployment across many devices | Stronger focus on edge targets |
| ONNX Runtime | Running exported ONNX models | Executes a portable format, not a framework trace |
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.condorlax.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.