Skip to main content
QUICK REVIEW

[Paper Review] Tangent: Automatic differentiation using source-code transformation for dynamically typed array programming

Bart van Merriënboer, Dan Moldovan|arXiv (Cornell University)|Sep 25, 2018
Evolutionary Algorithms and ApplicationsComputer Science16 citations
TL;DR

Tangent is the first automatic differentiation library for Python that uses source-code transformation (SCT) in a dynamically typed language, enabling efficient, high-performance derivatives with full compatibility to the Python ecosystem. It achieves this through persistent data structures, lazy gradient evaluation, and static optimizations, supporting features like mutable arrays, inspectable derivative code, and gradient surgery—offering performance competitive with existing frameworks while enabling seamless debugging and profiling.

ABSTRACT

The need to efficiently calculate first- and higher-order derivatives of increasingly complex models expressed in Python has stressed or exceeded the capabilities of available tools. In this work, we explore techniques from the field of automatic differentiation (AD) that can give researchers expressive power, performance and strong usability. These include source-code transformation (SCT), flexible gradient surgery, efficient in-place array operations, higher-order derivatives as well as mixing of forward and reverse mode AD. We implement and demonstrate these ideas in the Tangent software library for Python, the first AD framework for a dynamic language that uses SCT.

Motivation & Objective

  • To develop a source-code transformation (SCT)-based automatic differentiation system for dynamically typed languages like Python, overcoming limitations of operator-overloading approaches.
  • To support expressive machine learning model development with features such as in-place array operations, higher-order derivatives, and mixed forward/reverse mode AD.
  • To ensure compatibility with Python’s full ecosystem, including debuggers, profilers, and introspection tools, by operating directly on source code.
  • To demonstrate that SCT in dynamic languages can achieve performance comparable to existing frameworks while enabling advanced debugging and optimization capabilities.
  • To explore the feasibility and implementation of general differentiable programming in Python using static analysis and code transformation.

Proposed method

  • Tangent performs source-code transformation on Python ASTs to generate derivative functions, reversing control flow to implement reverse-mode AD.
  • It uses persistent data structures to represent mutable multidimensional arrays efficiently in reverse-mode AD, minimizing memory overhead.
  • Lazy evaluation is employed to defer gradient computation until needed, reducing unnecessary computation and improving performance.
  • Multiple dispatch and static optimizations are applied to the transformed code to improve runtime efficiency and enable whole-program analysis.
  • The system supports gradient surgery by allowing users to inject arbitrary code into the backward pass, enabling fine-grained control over derivatives.
  • It enforces restrictions on in-place operations and closures to avoid perturbation confusion, ensuring correctness of the AD transformation.

Experimental results

Research questions

  • RQ1Can source-code transformation be effectively applied to a dynamically typed language like Python to enable high-performance automatic differentiation?
  • RQ2How can mutable multidimensional arrays be efficiently represented and differentiated in an SCT-based AD system?
  • RQ3To what extent can SCT-based AD frameworks support advanced features like gradient surgery, higher-order derivatives, and mixed forward/reverse mode?
  • RQ4Can SCT-based AD achieve performance competitive with existing operator-overloading frameworks while maintaining full compatibility with Python’s debugging and profiling tools?
  • RQ5What language-level restrictions are necessary to ensure correctness and feasibility of dataflow analysis in SCT-based AD for dynamic languages?

Key findings

  • Tangent successfully implements source-code transformation for automatic differentiation in Python, making it the first such framework in a dynamically typed language.
  • The system achieves competitive runtime performance compared to established frameworks like Autograd and TensorFlow, as demonstrated on MLP and lattice language model benchmarks.
  • Tangent enables full compatibility with Python’s debugging and profiling tools by operating directly on source code, allowing developers to use standard debugging and introspection features.
  • The use of persistent data structures and lazy gradient evaluation reduces memory overhead and improves performance in reverse-mode AD.
  • Tangent supports advanced features such as gradient surgery and in-place array operations, which are not natively supported in operator-overloading frameworks.
  • The framework imposes minimal restrictions on user code, allowing data-dependent control flow and normal Python syntax, while ensuring correctness through static analysis and code transformation.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.