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

Bart van Merrienboer, Olivier Breuleux, Arnaud Bergeron, Pascal Lamblin

This paper addresses a fundamental tension in machine learning (ML) software: the trade-off between the flexibility of "define-by-run" frameworks (like PyTorch) and the performance of "graph-based" frameworks (like TensorFlow). The authors introduce Myia, a compiler toolchain that uses a functional, graph-based intermediate representation (IR) to provide the best of both worlds.

The Problem with Current AD Systems

Most ML frameworks rely on Automatic Differentiation (AD) to compute gradients. Currently, there are two main ways to do this:

  1. Operator Overloading (OO): Used by PyTorch and Autograd. It’s easy to use and supports standard Python control flow, but it incurs runtime overhead because it "traces" the program every time it runs, making ahead-of-time (AOT) optimization difficult.
  2. Source Transformation (ST): Used by older frameworks like Theano or newer ones like Tangent. It transforms the source code into a "gradient" program. While faster, these often rely on a "tape" (a global stack) to store intermediate values, which makes it hard to optimize and difficult to implement higher-order derivatives or recursion.

The authors identify a gap: we need a system that is as flexible as a general-purpose programming language (supporting recursion and closures) but as optimizable as a static computation graph.

The Myia Compiler and Functional IR

The core of the paper is the introduction of a new Graph-based Intermediate Representation (IR) and the Myia compiler:

  • Functional IR: Unlike the "sea-of-nodes" or simple dataflow graphs used in TensorFlow, Myia’s IR is purely functional. It represents functions as graphs where edges represent data dependencies.
  • First-Class Closures: Myia treats functions and their environments (closures) as first-class objects. This allows the IR to naturally represent nested scopes and recursion.
  • Closure-based AD: Instead of using a "tape" to store values for the backward pass, Myia uses closures. When a function is called, it returns its result and a "backpropagator" closure that captures only the specific intermediate values needed for the gradient.
  • Type and Shape Inference: Since Python is dynamically typed, Myia uses a sophisticated inference engine to determine types and tensor shapes at compile-time, allowing it to generate specialized, high-performance code.

Key Insights

  • Closures > Tapes: By using closures to capture the environment for the backward pass, the authors eliminate the need for a global "tape." This makes the adjoint (gradient) program look like a standard functional program, which can be optimized using well-known techniques from the functional programming community.
  • The Power of Purity: By restricting the IR to be purely functional (no side effects or mutation), the authors make AD much more robust. Mutation is a major headache for reverse-mode AD because you have to ensure values aren't changed before the backward pass needs them.
  • Specialization via Inference: Myia doesn't require users to write type annotations. It "specializes" functions based on the input types at the call site, providing the performance of a statically typed language with the feel of Python.

Results and Optimization Efficiency

The paper demonstrates a "proof of concept" rather than a production-ready benchmark, focusing on the correctness and optimizability of the IR using a simple Python example.

Myia compilation pipeline
  • Qualitative Result: The authors show that a simple Python function computing a gradient can be parsed, transformed into an adjoint program, and then optimized.
  • Optimization Efficiency: As illustrated above, after optimization (inlining, constant propagation, and pruning unused code), the complex graph generated by the AD process collapses into an efficient expression essentially identical to what a human would write by hand.
  • Expressiveness: The system successfully represents and differentiates through higher-order functions and recursion, which are major pain points in traditional frameworks.

Limitations and Future Work

  • Python Subset: Myia currently only supports a subset of Python. Specifically, it forbids mutation (like x[i] = v), which is common in many imperative ML scripts.
  • Complexity of Inference: Performing type and shape inference on recursive, higher-order functions is computationally expensive and complex to implement for the full Python language.
  • Hardware Integration: While the paper mentions a prototype that compiles to TVM (a tensor compiler), full integration with high-performance kernels (like those in CUDA) for all operations is a massive engineering undertaking.
  • Future Directions: The authors suggest exploring "uniqueness typing" to safely allow some forms of mutation and using monads to handle things like random number generation in a purely functional way.

Summary updated

BibTeX
@inproceedings{merrienboer2018automatic,
  author = {Bart van Merrienboer 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