Relay: A New IR for Machine Learning Frameworks

Jared Roesch, Steven Lyubomirsky, Logan Weber, Josh Pollock, Marisa Kirisame, Tianqi Chen, Zachary Tatlock

Introduction

The rapid evolution of machine learning (ML) has led to a divergence in framework design. Developers currently face a stark trade-off between efficiency and expressiveness. Static graph frameworks, such as early versions of TensorFlow, offer significant optimization opportunities but struggle with complex programming constructs like recursion or data-dependent control flow. Conversely, dynamic frameworks like PyTorch provide the flexibility of standard imperative programming but often incur runtime overhead and are difficult to deploy on specialized hardware due to their reliance on a Python interpreter.

Relay is introduced as a new high-level, purely functional, and statically-typed intermediate representation (IR) designed to bridge this gap. By treating ML models as programs rather than simple dataflow graphs, Relay enables sophisticated compiler optimizations and higher-order automatic differentiation while maintaining portability across diverse hardware targets including GPUs, FPGAs, and TPUs.

System Architecture and Integration

Relay serves as the high-level entry point for the TVM (Tensor Virtual Machine) compiler stack. It is designed to ingest models from various frameworks and provide a unified representation for optimization.

The Relay system architecture, showing the integration between high-level frameworks, the Relay IR, and the TVM compiler stack.

As shown in the architecture, Relay performs high-level transformations such as operator fusion, layout changes, and partial evaluation. A key feature is the separation of Control and Operators. The control flow is handled by the Relay runtime system, while individual tensor operators are described, scheduled, and lowered to hardware-specific implementations via the TVM stack. To ensure accessibility, a Python frontend using decorators (@relay) allows researchers to write standard Python code that is automatically translated into Relay’s internal C++ data structures.

Language Design: Models as Programs

The core philosophy of Relay is that a machine learning model is best represented as a functional program. This perspective allows the application of decades of programming language (PL) research to the domain of ML.

The formal grammar of the Relay language, emphasizing its design as a functional programming language.

The formal grammar of Relay supports first-class functions, closures, and recursion—constructs that are typically absent or difficult to represent in traditional dataflow IRs. This expressiveness allows Relay to represent complex differentiable programs. Furthermore, Relay features a shape-dependent type system. This system allows the compiler to reason about tensor dimensions at compile-time, which not only catches errors early but also serves as a powerful optimization tool. For instance, knowing dimensions allows the compiler to automatically adjust memory layouts or perform "auto-batching" without manual intervention.

Higher-Order Automatic Differentiation

Relay implements automatic differentiation (AD) as a local program transformation on the Abstract Syntax Tree (AST). Unlike simple "reverse-mode" accumulation found in many frameworks, Relay's AD can compute gradients of functions that take or return other functions. This capability is essential for the emerging field of "differentiable programming," where models may involve complex control structures and higher-order logic. The implementation is remarkably concise, with the core AD logic requiring fewer than 500 lines of C++.

Results and Future Directions

The authors have successfully integrated Relay into the open-source NNVM/TVM framework. To demonstrate its utility, a LeNet model was implemented and trained on the MNIST dataset entirely within the Relay ecosystem. The prototype demonstrates a complete end-to-end flow: defining a model in Python, automatically deriving gradients, and executing the result via a type-specialized operator compiler.

Future work for Relay includes:

  • Jagged Tensors: Extending the type system to support partially-specified shapes, which is critical for NLP tasks with varying sequence lengths.
  • Effect Systems: Developing a formal system to manage non-pure operations like random number generation or stateful I/O.
  • Numerical Stability: Incorporating automated tools to rewrite mathematical expressions for improved numerical accuracy or to trade precision for performance on low-power hardware.

By unifying the flexibility of dynamic frameworks with the performance of static ones, Relay provides a robust foundation for the next generation of machine learning compilers.

Summary updated

BibTeX
@inproceedings{roesch2018relay,
  author = {Jared Roesch and Steven Lyubomirsky and Logan Weber and Josh Pollock and Marisa Kirisame and Tianqi Chen and Zachary Tatlock},
  title = {{Relay: A New IR for Machine Learning Frameworks}},
  booktitle = {MAPL@PLDI},
  year = {2018},
  doi = {10.1145/3211346.3211348},
}
Figure