RAF: Holistic Compilation for Deep Learning Model Training

Cody Hao Yu, Haozheng Fan, Guangtai Huang, Zhen Jia, Yizhi Liu, Jie Wang, Zach Zheng, Yuan Zhou, Haichen Shen, Junru Shao, Mu Li, Yida WangarXiv 2023 · DOI

Introduction

Deep learning compilers (DLCs) have revolutionized model deployment, yet their primary focus has historically been on inference. Training presents a unique set of challenges, including automatic differentiation, complex memory management for intermediate tensors, and distributed communication. RAF is a holistic deep learning compiler designed specifically to address these complexities. By taking end-to-end control of the training stack, RAF achieves significant throughput improvements and enables larger batch sizes compared to established frameworks like PyTorch, XLA, and DeepSpeed.

The Limitations of Modular Training Stacks

Most existing DLCs, such as Apache TVM or XLA, were optimized for static forward-pass graphs. In a typical training workflow, different components handle differentiation, kernel optimization, and distributed scaling in isolation. This "piecemeal" approach often misses global optimization opportunities. For instance, a compiler might perform operator fusion that inadvertently hinders a distributed library's ability to overlap communication with computation. RAF addresses this by treating the entire training process as a single, optimizable graph.

The RAF Architecture

The core of RAF's performance lies in its "holistic" architecture, which is built upon three fundamental pillars: multi-phase graph optimization, a flexible operator dialect mechanism, and automated training-specific passes.

The RAF compilation pipeline transitions between A-normal form (ANF) and Dataflow IR to balance control flow representation with optimization ease.

Three-Phase Graph Optimization

RAF utilizes multiple Intermediate Representations (IRs) to optimize different aspects of the model. It switches between A-normal form (ANF), which is ideal for representing execution order and control flow, and Dataflow IR, which excels at pattern matching and operator fusion. This allows RAF to decouple high-level model logic optimizations (e.g., constant folding) from low-level execution scheduling (e.g., memory planning).

Operator Dialect Mechanism

To avoid vendor lock-in and leverage the best available kernels, RAF employs a "dialect" system. This mechanism allows the compiler to intelligently dispatch a single operation, such as a convolution, to the most efficient backend available—be it a hand-tuned library like cuDNN, a template-based library like CUTLASS, or a tensor compiler like TVM.

The operator dialect mechanism enables RAF to dispatch base operators to the most efficient backend implementation based on device and priority.

Automated Training-Specific Passes

RAF integrates training-heavy tasks directly into the compiler. This includes Automatic Rematerialization, which saves memory by recomputing intermediate tensors on the fly rather than storing them, and Distribution Optimization, which automatically applies ZeRO-style memory optimizations and overlaps data transfers with computation.

Key Insights: Holistic vs. Modular

A primary insight of the RAF project is that "holistic" control over the backward pass generation allows for more aggressive memory reuse. Because the compiler generates the training graph internally, it has full visibility into tensor lifecycles, enabling optimizations that are impossible when the backward pass is treated as a black box. Furthermore, the IR switching strategy ensures that the compiler maintains "memory safety" while still benefiting from the ease of fusion provided by dataflow representations.

Evaluation and Results

The researchers evaluated RAF using state-of-the-art Transformer models, including BERT, RoBERTa, and GPT-2, on NVIDIA V100 and A100 GPUs.

  • Throughput and Memory: On a single V100, RAF consistently outperformed PyTorch and XLA. Notably, its automated rematerialization allowed for significantly larger batch sizes; for BERT-Large, RAF successfully trained on batches that caused Out-of-Memory (OOM) errors in other frameworks.
  • Distributed Scaling: In an 8x A100 multi-GPU setup, RAF outperformed DeepSpeed by approximately 14% on a 1.5B parameter model. This gain is attributed to RAF's superior ability to hide communication latency by overlapping it with math operations.
  • Correctness: RAF maintains numerical parity with PyTorch, ensuring that its aggressive optimizations do not compromise model convergence.

Conclusion

RAF demonstrates that a holistic approach to compilation can unlock significant performance gains for deep learning training. While currently focused on data parallelism, future work aims to extend these optimizations to model and pipeline parallelism for extreme-scale models. By automating complex tasks like rematerialization and backend dispatching, RAF provides a powerful and user-friendly extension for the PyTorch ecosystem.

Summary updated

BibTeX
@misc{yu2023raf,
  author = {Cody Hao Yu and Haozheng Fan and Guangtai Huang and Zhen Jia and Yizhi Liu and Jie Wang and Zach Zheng and Yuan Zhou and Haichen Shen and Junru Shao and Mu Li and Yida Wang},
  title = {{RAF: Holistic Compilation for Deep Learning Model Training}},
  year = {2023},
  eprint = {2303.04759},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2303.04759},
}
Figure