Automatic differentiation in ML: Where we are and where we should be going

Bart van Merriënboer, Olivier Breuleux, Arnaud Bergeron, Pascal Lamblin

TL;DR

Machine learning frameworks often force a trade-off between the flexibility of operator overloading (e.g., PyTorch) and the performance of static dataflow graphs (e.g., TensorFlow). This paper bridges the gap by introducing a novel, purely functional graph-based intermediate representation (IR) that supports fully-general automatic differentiation (AD)—including recursion and higher-order functions—without a runtime tape. The authors demonstrate this approach through Myia, a proof-of-concept compiler toolchain with a Python frontend.

What Problem Does This Paper Address?

To compute gradients, modern ML frameworks rely on Automatic Differentiation (AD), typically split into two camps:

  1. Operator Overloading (OO): Used by frameworks like PyTorch, this tracks operations at runtime on a "tape." While highly flexible and easy to write, it incurs significant runtime overhead and prevents ahead-of-time (AOT) compiler optimizations.
  2. Source Transformation (ST) / Dataflow Graphs: Used by early TensorFlow and Theano, this builds a static computation graph. It allows aggressive optimization but traditionally sacrifices language expressiveness, lacking native support for recursion, loops, or higher-order functions.

Furthermore, traditional tape-based source transformation creates highly non-local data structures that break standard compiler optimization passes. The authors address this divide by looking to functional programming languages to build an IR that is both maximally expressive and highly optimizable.

Methods

The authors propose a new graph-based, purely functional, strongly typed Intermediate Representation (IR). Its core features include:

  • Closure-Based AD: Instead of using a runtime tape to track intermediate variables for the backward pass, the framework uses source transformation to generate a chain of closures (functions that capture their environment). This allows the forward pass to cleanly package the data needed by the backward pass.
  • Functional Control Flow: Rather than using specialized graph operators for loops (like TensorFlow's while_loop), control flow is represented naturally using recursion and higher-order functions.
  • The Myia Toolchain: A proof-of-concept compiler that compiles a pure subset of Python 3.6 (designated via a @myia decorator) into this functional IR.
  • Advanced Type Inference: Because Python is dynamically typed, Myia implements a call-site specialization algorithm using Python coroutines (async def) to infer concrete types, shapes, and values ahead of time.

Datasets

Not discussed in the paper. As a programming language and compiler design paper, it does not evaluate standard machine learning datasets or introduce new empirical benchmarks.

Key Insights and Ideas

  • ML Frameworks are Compilers: The authors argue that the ML community has developed frameworks in isolation from the programming language and AD communities, missing out on decades of compiler research.
  • Tape-Free Reverse AD: By leveraging closures, the generated derivative (adjoint) program requires no AD-specific compiler passes. A standard functional language compiler can optimize the derivative code using standard techniques like inlining and dead-code elimination.
  • Graph-Based Nesting: By representing free variables as direct pointers to nodes in parent graphs, the IR creates an implicit nesting relationship. This makes it much easier for the compiler to optimize closures jointly with the functions they are nested inside.

Key Results

  • Qualitative Expressiveness: The authors successfully designed an IR closest to A-normal form (ANF) but graphical rather than syntactic, making it easier to manipulate algorithmically while retaining support for complex features like higher-order derivatives (reverse-over-reverse).
  • Graph Optimization: The paper demonstrates (visually in Figure 1) how a highly complex, automatically generated gradient graph containing redundant tuple packing and constant gradients can be optimized and inlined down to an expression identical to what a human would write by hand.
  • Backend Integration: The authors implemented a prototype that compiles straight-line parts of the optimized graph using TVM, proving the viability of compiling a functional IR down to high-performance hardware kernels.

Limitations and Future Work

  • No Mutability: Because reverse-mode AD requires access to unchanged intermediate variables from the forward pass, Myia strictly forbids mutation and side effects. Common Python operations like index assignment (x[i] = v) or augmented assignment (x += y) are currently unsupported, though the authors suggest "uniqueness typing" as a potential future solution.
  • Not for Low-Level Kernels: Myia is designed as a high-level coordinator, not for writing low-level compute kernels. It still relies on external primitives (like CUDA or XLA) for heavy matrix operations.
  • Complexity of Inference: Type and shape inference becomes significantly more complex when supporting recursive calls and higher-order functions, requiring ongoing development to ensure robustness.

Summary updated

BibTeX
@inproceedings{merrienboer2018automatic,
  author = {Bart van Merriënboer and Olivier Breuleux and Arnaud Bergeron and Pascal Lamblin},
  title = {{Automatic differentiation in ML: Where we are and where we should be going}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
  url = {https://openreview.net/forum?id=r1ZTGdbO-B},
}
Figure