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

Tangent: Automatic Differentiation Using Source Code Transformation in Python

Bart van Merriënboer, Alexander B. Wiltschko, Dan MoldovanarXiv 2017

Introduction

Tangent is a library that introduces Source Code Transformation (SCT) to the Python automatic differentiation (AD) ecosystem. While popular tools like PyTorch and Autograd rely on runtime tracing, and early versions of TensorFlow utilized complex graph-building, Tangent generates readable, standalone Python source code for gradients. This approach offers high performance and a significantly improved debugging experience by keeping the gradient logic within the native Python environment.

The Automatic Differentiation Landscape

In machine learning, calculating gradients is essential for optimization. Most Python libraries currently handle this through two primary methods:

  1. Tracing (e.g., Autograd, PyTorch): These libraries record operations on a "tape" during runtime and then traverse it backward. While flexible, this adds runtime overhead and complicates debugging, as the gradient logic is hidden within the library's internals.
  2. Dataflow Graphs (e.g., early TensorFlow, Theano): Users define models in a specialized domain-specific language. This is computationally efficient but often results in verbose, non-idiomatic code that feels disconnected from standard Python syntax.

Tangent addresses this gap by performing AD through the direct rewriting of Python source code into new Python source code, combining the performance of graph-based systems with the readability of native Python.

Methodology: Source Code Transformation

Tangent’s core innovation lies in its use of Python’s built-in machinery to introspect and transform the Abstract Syntax Tree (AST).

  • AST Rewriting: When a function is passed to Tangent, it parses the code into a tree structure and applies "adjoint" rules—mathematical templates for derivatives—to each node, including loops, conditionals, and NumPy calls.
  • Ahead-of-Time (AOT) Generation: The gradient function is generated once before execution, eliminating the "interpretive overhead" inherent in tracing systems.
  • Compiler Optimizations: By treating Python as a compiler would, Tangent performs optimizations such as dead code elimination (removing original function parts unnecessary for the gradient) and algebraic simplification (e.g., simplifying x + 0 to x).
  • Custom Logic Injection: Tangent introduces a novel syntax using context managers (with grad_of(x) as dx:) that allows users to inject custom logic, such as gradient clipping or logging, directly into the generated code.

Performance Evaluation

The authors evaluated Tangent using performance benchmarks rather than standard ML training datasets, focusing on two primary architectures:

  1. Multi-Layer Perceptron (MLP): A standard neural network implemented in NumPy.
  2. Simple Loop Benchmark: A synthetic test case involving nested loops and conditional logic to evaluate how the library handles complex control flow.

Key Results

Tangent demonstrated significant performance advantages over tracing-based methods. On the MLP benchmark, Tangent outperformed Autograd, particularly for smaller models where tracing overhead typically dominates. It also largely matched or occasionally exceeded TensorFlow’s performance across various model sizes.

Performance comparison on a Multi-Layer Perceptron (MLP) benchmark across different model sizes.

In the simple loop benchmark, Tangent proved faster than both Autograd and TensorFlow across all tested sizes. This demonstrates the efficiency of the source-to-source approach when dealing with standard Pythonic control flow, which often requires specialized, less efficient functions (like tf.while_loop) in graph-based frameworks.

Performance comparison on a synthetic loop benchmark, highlighting Tangent's efficiency with control flow.

Discussion and Limitations

One of Tangent's primary benefits is that its generated gradient code is standard Python. Users can print the code, step through it with a debugger, and observe exactly how gradients are calculated. By performing the heavy lifting before execution, Tangent ensures the backward pass is as fast as a manually written derivative.

However, Tangent currently supports only a subset of Python. It faces challenges with functions involving side effects, closures, or complex class structures. While it integrates seamlessly with NumPy, future work is required to extend support to other numeric libraries, particularly those offering GPU acceleration. Despite these limitations, Tangent represents a significant step toward making high-performance automatic differentiation more transparent and accessible to Python developers.

Summary updated

BibTeX
@misc{merrienboer2017tangent,
  author = {Bart van Merriënboer and Alexander B. Wiltschko and Dan Moldovan},
  title = {{Tangent: Automatic Differentiation Using Source Code Transformation in Python}},
  year = {2017},
  eprint = {1711.02712},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1711.02712},
}
Figure