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

Tangent: Automatic differentiation using source-code transformation for dynamically typed array programming

Bart van Merrienboer, Dan Moldovan, Alexander B. Wiltschko

TL;DR

Tangent is the first automatic differentiation (AD) library for Python that uses Source-Code Transformation (SCT) to generate readable, high-performance derivative code. It combines the usability of "eager" frameworks like PyTorch with the optimization potential of "graph-based" frameworks like TensorFlow, all while allowing users to debug their gradients using standard Python tools.

What Problem Does This Paper Address?

In machine learning, calculating derivatives (gradients) of complex programs is essential for training models. Historically, Python developers had to choose between two trade-offs:

  1. Operator Overloading (OO): Used by libraries like PyTorch and Autograd. It is highly "Pythonic" and easy to use, but it incurs runtime overhead because it must record every operation on a "tape" during execution. It is also a "black box"—you cannot easily inspect or optimize the gradient code itself.
  2. Source-Code Transformation (SCT) / Graph-based: Used by frameworks like TensorFlow and Theano. These are fast because they optimize the entire computation graph at once, but they require "metaprogramming." Users write code that builds a graph rather than pure Python, which is notoriously difficult to debug and feels unidiomatic.

The authors of Tangent asked: Can we get the performance and optimization of SCT while keeping the flexibility and "pure Python" feel of OO?

Methods

Tangent works by analyzing your Python source code and literally writing a new Python function that calculates the derivative.

  • AST Transformation: It uses Python’s built-in Abstract Syntax Tree (AST) tools to parse your code, apply the chain rule line-by-line, and output a new Python script.
  • Multiple Dispatch: Since Python is dynamically typed, Tangent doesn't know if x * y is a scalar or a massive matrix until runtime. It generates code that uses "multiple dispatch" to choose the correct derivative logic based on the actual types encountered during execution.
  • Lazy Evaluation: To save memory, Tangent uses a ZeroGrad object. Instead of allocating a giant array of zeros for a gradient that hasn't been updated yet, it passes a placeholder that acts like zero until a real value is added.
  • Persistent Data Structures: One of the hardest parts of AD is handling "in-place" updates (like x[i] = v). If you change x, the original value is lost, but the backward pass might need it. Tangent uses persistent data structures that efficiently store "deltas" (changes), allowing the backward pass to "roll back" the array to previous states without making expensive full copies.
Handling in-place array updates with persistent data structures.

Key Insights and Ideas

  • Gradients are just code: By treating the derivative as a standalone Python function, you can use standard debuggers (pdb), profilers, and even other compilers (like Numba) directly on your gradients.
  • The "Context Manager" for Surgery: Tangent introduces a novel with insert_grad_of(x) as dx: syntax. This allows researchers to "inject" custom logic (like gradient clipping) directly into the generated backward pass code in a highly readable way.
  • Solving the Mutability Problem: Most AD frameworks force you to use immutable arrays. Tangent’s use of persistent data structures proves you can support idiomatic, mutable Python code without a massive performance hit.

Key Results

  • Lower Overhead: On a tight loop that doesn't do much math (where framework overhead is most visible), Tangent was 30% faster than PyTorch because it didn't have to manage a runtime "tape."
  • Algorithmic Speedup: For models involving many array updates (like neural lattice language models), Tangent’s persistent arrays reduced the time complexity from $O(n^2md)$ to $O(ndm)$, a massive win for long sequences.
Runtime comparison showing the efficiency of persistent arrays over immutable arrays.
  • Competitive Performance: On standard deep learning workloads (like Feedforward MLPs), Tangent performs on par with TensorFlow and Autograd, proving that the SCT approach doesn't sacrifice raw speed for its debugging benefits.

Limitations and Future Work

While powerful, Tangent currently operates on a subset of Python:

  • In-place restrictions: Functions that modify variables in-place must return those variables to maintain clear data flow.
  • No Closures or Object Methods: Tangent cannot currently track derivatives through member variables of classes or through nested function closures.
  • Static Resolution: The function being differentiated must be resolvable at the time the transformation is called; you cannot easily differentiate through highly dynamic higher-order functions.
  • Syntax Gaps: Some Python features like try/except blocks and break/continue in loops are not yet supported in the transformation engine.

The authors suggest that future work could involve applying additional AD passes at runtime to overcome some of these static resolution limits.

Summary updated

BibTeX
@inproceedings{merrienboer2018tangent,
  author = {Bart van Merrienboer and Dan Moldovan and Alexander B. Wiltschko},
  title = {{Tangent: Automatic differentiation using source-code transformation for dynamically typed array programming}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
  url = {https://openreview.net/forum?id=r1EENvZ_ZS},
}
Figure