NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

AutoGraph: Imperative-style Coding with Graph-based Performance

Dan Moldovan, James M. Decker, Fei Wang, Andrew A. Johnson, Brian K. Lee, Zachary Nado, D. Sculley, Tiark Rompf, Alexander B. Wiltschko

TL;DR

AutoGraph eliminates the classic trade-off between the usability of imperative machine learning frameworks and the performance of graph-based execution. By automatically transforming standard Python code into dataflow graphs via source-to-source compilation, it allows developers to write clean, idiomatic code that executes with whole-program optimizations.

What Problem Does This Paper Address?

In machine learning, programmers typically face a choice between two paradigms:

  1. Imperative programming (Define-by-run): Used by frameworks like PyTorch or TensorFlow Eager. It is highly intuitive, easy to debug, and allows native Python control flow (like standard if statements and while loops). However, it suffers from high Python runtime overhead and is difficult to deploy in production or mobile environments.
  2. Graph-based programming (Define-and-run): Used by traditional TensorFlow. It builds a static intermediate representation (IR) of the computation graph. This allows powerful whole-program optimizations, parallel distribution, and easy deployment, but forces developers to write complex, non-idiomatic functional code (e.g., using tf.cond() and tf.while_loop()) to handle data-dependent control flow.

AutoGraph addresses this friction by providing a midpoint: a system that automatically compiles idiomatic, imperative Python code into optimized, scalable graph-based IRs.

Methods

AutoGraph achieves this blend of usability and performance through Source Code Transformation (SCT) and Dynamic Dispatch:

  • AST Transformation: Python natively allows operator overloading (e.g., redefining + via __add__), but it does not allow overloading control flow syntax like if, for, or while. AutoGraph circumvents this by parsing Python code into an Abstract Syntax Tree (AST), performing static analysis (such as control flow graph construction, activity analysis, and liveness analysis), and rewriting the control flow statements into functional equivalents.
  • Dynamic Dispatch: To handle cases where control flow depends on a configuration variable (like a hyperparameter) versus an actual data tensor, AutoGraph delays staging decisions until runtime. If a conditional predicate is a standard Python boolean, it executes normally; if it is a Tensor, it dispatches to a staged graph operation (like tf.cond).
  • Backend Agnosticism: While primarily integrated with TensorFlow, AutoGraph's compiler engine is backend-agnostic. The authors demonstrated this by targeting Lantern, an alternative Lisp-like S-expression IR that compiles to C++ and supports features absent in TensorFlow graphs, such as recursion.

Datasets

The authors evaluated AutoGraph using two primary tasks:

  • MNIST: Used to evaluate the performance of an entirely in-graph training loop using Stochastic Gradient Descent (SGD).
  • Stanford Sentiment Treebank: Used to evaluate a recursive TreeLSTM model, which requires data-dependent recursive structures.

Key Insights and Ideas

  • Control Flow Overloading via SCT: The core "aha" moment is that while Python doesn't let you overload if or while, you can write a compiler pass that intercepts the code before execution and replaces those blocks with overloadable function calls (ag.if_stmt, ag.while_stmt).
  • Amortizing Runtime Overhead: Although dynamic dispatch introduces a slight runtime check overhead in Python, this cost is completely amortized because the resulting code is lowered into a high-performance IR that executes independently of the Python runtime.
  • Unlocking In-Graph Training: Traditionally, training loops are kept in Python, which forces data to repeatedly cross the Python/TensorFlow boundary. AutoGraph easily moves the entire training loop inside the graph, significantly boosting execution speed.

Key Results

  • RNN Performance: AutoGraph matched the speed of official, hand-written TensorFlow graph implementations for Recurrent Neural Networks (RNNs) while dramatically improving code readability.
Performance comparison of RNN cells across different execution modes, showing AutoGraph matching native graph speeds.
  • Training Speedups: For the MNIST training loop, moving the entire process into a TensorFlow graph via AutoGraph yielded a 30% speedup over a traditional Python training loop and a 127% speedup over TensorFlow Eager execution.
  • TreeLSTM with Lantern: When paired with the Lantern backend to handle recursive structures, the AutoGraph-generated C++ code achieved 36.75 SGD steps per second, running approximately 2.38x faster than a comparable PyTorch implementation (15.41 steps/sec).

Limitations and Future Work

  • Incomplete Python Coverage: AutoGraph does not yet support the entire Python language; notable omissions include associative data structures (like dictionaries) and try/except error handling blocks.
  • Debugging Obscurity: Because staging decisions happen automatically under the hood, it can be difficult for a user to diagnose why a piece of code didn't compile into the graph (for instance, if they accidentally passed a Python boolean instead of a Tensor).
  • Type System Mismatches: TensorFlow's lack of support for nullable types forces AutoGraph to impose extra constraints, such as requiring all code paths to explicitly initialize a variable. Advanced type inference is flagged as a key area for future work.

Summary updated

BibTeX
@inproceedings{moldovan2019autograph,
  author = {Dan Moldovan and James M. Decker and Fei Wang and Andrew A. Johnson and Brian K. Lee and Zachary Nado and D. Sculley and Tiark Rompf and Alexander B. Wiltschko},
  title = {{AutoGraph: Imperative-style Coding with Graph-based Performance}},
  booktitle = {SysML},
  year = {2019},
}
Figure