Memory safe computations with XLA compiler

Artem Artemev, Yuze An, Tilman Roeder, Mark van der Wilk

Introduction: Breaking the Memory Wall

In the world of machine learning, speed is king, but memory is the gatekeeper. Modern frameworks like TensorFlow and PyTorch are heavily optimized for execution speed, often at the expense of memory efficiency. When a model tries to allocate more VRAM than a GPU possesses, the program simply crashes with an "Out-of-Memory" (OOM) error.

This "memory wall" is particularly brutal for algorithms that rely on pairwise operations (like k-Nearest Neighbors) or large attention mechanisms (Transformers). Traditionally, if your model was too big for your GPU, you had two choices: buy more expensive hardware or spend weeks writing complex, custom "minibatching" logic to manually swap data in and out of memory.

Researchers at Imperial College London have addressed this by asking: What if the compiler handled the memory management for us? They developed eXLA, an extension for the XLA (Accelerated Linear Algebra) compiler that automatically prevents OOM errors. By restructuring computational graphs to fit within user-defined memory limits, eXLA allows standard machine learning models to scale to massive datasets on a single GPU without changing a single line of model code.

How eXLA Works: Compiler-Level Optimization

The authors introduce eXLA by inserting three specific optimization "passes" into the standard XLA compilation pipeline:

  1. Match and Replace: The compiler searches for mathematically equivalent but more memory-efficient operations. For example, it can replace a naive Euclidean distance calculation (which creates a massive intermediate 3D tensor) with a decomposed version that uses much less space.
  2. Reordering: Leveraging the associativity of matrix multiplication, the compiler reorders operations to minimize the size of intermediate results. Multiplying a matrix chain in the right order can mean the difference between storing a massive matrix or a small vector.
  3. Data-flow Graph Splitting: This is the core innovation. The compiler performs a depth-first search to identify "bottleneck" tensors. It then automatically wraps these operations in a generated while loop, processing the data in smaller "chunks" (slices) that fit within a user-specified memory threshold. This acts as an automated, compiler-level MapReduce.
Data-flow graph splitting transformation

Key Insights and Results

The authors evaluated eXLA across several classic and modern ML tasks, including k-Nearest Neighbors (kNN), Sparse Gaussian Process Regression (SGPR), and Transformers. A major takeaway from the research is a validation of Richard Sutton’s "Bitter Lesson"—the idea that general methods leveraging computation are ultimately the most effective. By removing memory constraints, simple, classic algorithms can scale to levels where they outperform complex, modern approximations.

  • Gaussian Processes: Standard GPflow implementations failed at just 800 inducing points due to memory limits. With eXLA, the authors scaled a 13-year-old "simple" method (SGPR) to 10,000 inducing points, significantly improving predictive accuracy (RMSE and NLPD) and outperforming newer, highly complex state-of-the-art iterative methods.
RMSE and NLPD improvements as eXLA scales Gaussian Processes to 10,000 inducing points
  • k-Nearest Neighbors: On a single GPU, eXLA scaled kNN to 1 million data points. Standard JAX and TensorFlow implementations crashed due to OOM on the exact same hardware.
  • Transformers: Standard TensorFlow crashed when sequence lengths exceeded 2,000. eXLA allowed the same model to run with sequence lengths up to 7,000 on the same 32GB V100 GPU.

Crucially, unlike other memory-saving tools, eXLA requires zero changes to the user's Python code. You simply set an environment variable (e.g., XLA_FLAGS="--xla_tensor_size_threshold=1GB") and the compiler handles the rest. While splitting computations into chunks adds some overhead, the performance hit is minimal compared to the benefit of the code actually running instead of crashing.

Limitations and Future Work

Despite its success, the authors note a few hurdles for future development:

  • Weak Type Systems: Current ML frameworks treat matrices as simple arrays. A stronger linear algebra type system would allow the compiler to perform even more sophisticated "match and replace" optimizations (e.g., recognizing symmetric or diagonal matrices).
  • Greedy Memory Allocation: The default XLA memory manager isn't "aware" of the limits eXLA is trying to enforce, sometimes leading to OOMs if multiple split operations execute simultaneously. Users currently have to set conservative thresholds to avoid this.
  • Parameter Splitting: Currently, eXLA splits large intermediate expressions but cannot yet split the model parameters themselves (e.g., a massive weight matrix in a giant LLM). This remains a target for future compiler-level research.

Summary updated

BibTeX
@inproceedings{artemev2022memory,
  author = {Artem Artemev and Yuze An and Tilman Roeder and Mark van der Wilk},
  title = {{Memory safe computations with XLA compiler}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=2S_GtHBtTUP},
}
Figure