This paper introduces a novel approach to automatic differentiation (AD) that bridges the gap between the flexibility of "define-by-run" frameworks (like PyTorch) and the performance of "define-then-run" frameworks (like TensorFlow). By leveraging classic programming language theory—specifically Continuation-Passing Style (CPS) and multi-stage programming—the authors demonstrate that backpropagation can be implemented efficiently without the need for auxiliary data structures like a "tape" or "trace."
The Expressivity vs. Efficiency Trade-off
In the world of differentiable programming, there is a persistent tension between expressivity and efficiency:
- Define-by-run (e.g., PyTorch): These frameworks are highly flexible, allowing for dynamic control flow such as loops, recursion, and branches. However, they typically rely on an auxiliary data structure called a "tape" to record operations during the forward pass so they can be reversed during the backward pass. This dynamic tracking adds runtime overhead.
- Define-then-run (e.g., TensorFlow): These frameworks build a static computation graph first, which allows for aggressive compiler optimizations. However, handling dynamic structures (like recursive neural networks) is often clunky, requiring specialized APIs or extensions.
The authors aim to eliminate this trade-off by providing a framework that handles dynamic control flow naturally while generating highly optimized native code.
Methods: Lantern and the Power of Continuations
The core innovation of the proposed framework, Lantern, lies in combining two powerful concepts from programming language (PL) research:
- Continuation-Passing Style (CPS) & Callbacks: The authors realize that the forward/backward pattern of backpropagation is essentially a sequence of function calls and returns. In their model, every operation is a function call. The forward pass happens when the function is entered; the backward pass happens when the function returns. By using delimited continuations (specifically
shiftandresetoperators), they allow users to write code in a standard "direct" style while the compiler automatically transforms it into a chain of callbacks. - Multi-Stage Programming (Staging): Using a technique called Lightweight Modular Staging (LMS) in Scala, the framework "stages" the code. It evaluates high-level abstractions (like objects and closures) at compile-time and generates optimized, low-level C++ code for the actual tensor computations. This allows the framework to produce a "define-then-run" style optimized graph from "define-by-run" style source code.
Key Insights
- The Stack is the Tape: Most AD frameworks manually manage a "tape" (a list of operations) on the heap. Lantern realizes that the computer's native function call stack is already a perfectly managed data structure for this purpose. When a function returns, the memory for its intermediate values is naturally freed.
- AD as a Program Transformation: Instead of treating AD as a library feature, the authors treat it as a fundamental program transformation. By using CPS, any program—including those with recursion and complex branches—can be mechanically converted into a gradient-calculating version.
Key Results and Performance
The authors evaluated Lantern against several standard architectures (Vanilla RNN, LSTM, CNN) and one complex recursive model (TreeLSTM) using the Stanford Sentiment Treebank (SST). The TreeLSTM served as a "stress test" because processing data-dependent tree structures is notoriously difficult for static graph frameworks.
- Standard Architectures: In benchmarks for Vanilla RNNs, LSTMs, and CNNs, Lantern's training time was significantly lower than PyTorch's and comparable to (or faster than) TensorFlow's.
- Recursive Efficiency: For the TreeLSTM model, Lantern outperformed PyTorch and DyNet. It also outperformed TensorFlow Fold (a specialized extension for dynamic graphs), proving that a general PL-based approach can beat specialized ML-specific workarounds.
- Correctness: The authors verified that the training loss decay in Lantern matched established frameworks, confirming the mathematical correctness of their callback-based backpropagation.
Limitations and Future Work
While Lantern presents a compelling foundation, the authors note a few limitations:
- Higher-Order Gradients: While Hessians (second-order derivatives) can be computed by nesting AD modes, the current Scala implementation of
shift/resetdoes not easily support the naive nesting required for pure reverse-mode higher-order gradients. - Backend Optimizations: The current version uses a relatively simple C++ backend. Integrating with more advanced tensor compilers like XLA (Accelerated Linear Algebra) would further boost performance.
- Language Barrier: The prototype is implemented in Scala. While powerful for research, bringing these ideas to the Python ecosystem (potentially via tools like AutoGraph) is necessary for broader adoption.
Summary updated