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

Differentiating a Tensor Language

Gilbert Bernstein, Michael Mara, Tzu-Mao Li, Dougal Maclaurin, Jonathan Ragan-KelleyarXiv 2020

Abstract

How does one compile derivatives of tensor programs, such that the resulting code is purely functional (hence easier to optimize and parallelize) and provably efficient relative to the original program? We show that naively differentiating tensor code—as done in popular systems like Tensorflow and PyTorch—can cause asymptotic slowdowns in pathological cases, violating the Cheap Gradients Principle. However, all existing automatic differentiation methods that guarantee this principle (for variable size data) do so by relying on += mutation through aliases/pointers—which complicates downstream optimization. We provide the first purely functional, provably efficient, adjoint/reverse-mode derivatives of array/tensor code by explicitly accounting for sparsity. We do this by focusing on the indicator function from Iverson's APL. We also introduce a new "Tensor SSA" normal form and a new derivation of reverse-mode automatic differentiation based on the universal property of inner-products.

Introduction: The Challenge of Efficient Gradients

In optimization and deep learning, we rely on the Cheap Gradients Principle: the idea that computing the gradient of a function should cost only a small constant factor more than computing the function itself. However, when dealing with tensors (multi-dimensional arrays), naive automatic differentiation (AD) often fails this test.

Consider a program that takes the trace of a diagonal matrix. Evaluating the function is $O(N)$, but a naive AD system might generate a gradient that materializes a full $N \times N$ matrix of zeros, turning an $O(N)$ problem into an $O(N^2)$ bottleneck. To avoid this, most systems resort to "imperative" tricks like += mutation (scatter-add). While this fixes the speed issue, it destroys code purity, making it harder for compilers to parallelize or optimize. This research demonstrates that it is possible to achieve purely functional code that is also provably efficient.

Methods: Compiler Transformations and Tensor SSA

The authors propose a formal "Tensor Language" and a series of compiler transformations to handle derivatives efficiently:

  1. Iverson Brackets for Sparsity: By using the notation [p] (1 if $p$ is true, 0 otherwise), logic is linked directly to linear algebra. This allows the compiler to symbolically represent and simplify sparse structures that arise during differentiation.
  2. The Universal Property of Inner Products: Instead of standard backpropagation, adjoint derivatives are derived using the algebraic identity: $\langle dy | Df(x, dx) \rangle = \langle D^Tf(x, dy) | dx \rangle$. This treats differentiation as a series of rigorous code rewrites.
  3. Normalization Passes: The code is managed through four transformation passes: Let-Lifting, Pair-Elimination, Gen-Pushout, and a final Tensor SSA (Single Static Assignment) form.
The forward computation graph and its corresponding SSA code.

The Tensor SSA form, illustrated above, enables common sub-expression elimination and provides a clear structure for the subsequent adjoint transformation.

Key Insights: The Geometry of Adjoints

A central observation of this work is the inherent symmetry in program structures during differentiation:

  • Sparsity is Ubiquitous: Differentiation naturally reveals sparse structures. If a compiler cannot "see" this sparsity, it generates inefficient code.
  • The Adjoint of Fan-out is Summation: When a variable is used multiple times (fan-out) in the forward pass, its adjoint in the reverse pass is the summation of the gradients from those uses.
  • Global Preservation of Work: While differentiation might move addition operations around "non-locally," the total number of additions is preserved globally.
The generated adjoint program and its reversed computational graph, demonstrating how fan-out becomes summation.

By keeping the derivative code purely functional, downstream compilers (like Halide or XLA) can apply advanced optimizations like loop fusion and tiling without being blocked by pointer aliasing or side effects.

Results: Provable Efficiency

The paper provides formal proofs for the efficiency of their approach:

  • Forward Mode Efficiency (Theorem 6.1): The total derivative of a program in SSA form is at most 4x the cost of the original program.
  • Reverse Mode Efficiency (Theorem 6.35): This provides the first formal proof that reverse-mode AD for a functional tensor language can satisfy the Cheap Gradients Principle without relying on mutation.
  • Asymptotic Fixes: The system automatically simplifies cases that would otherwise cause $O(N)$ to $O(N^2)$ slowdowns in frameworks like PyTorch or JAX (when used without XLA).

Limitations and Future Work

While the arithmetic cost model is robust, it does not yet account for memory movement, cache locality, or data layout—factors critical for real-world hardware performance. Additionally, the current language is not Turing-complete and lacks support for complex bulk operators like Fast Fourier Transforms (FFTs). Future work will focus on full implementation and empirical benchmarking against industry-standard frameworks.

Summary updated

BibTeX
@misc{bernstein2020differentiating,
  author = {Gilbert Bernstein and Michael Mara and Tzu-Mao Li and Dougal Maclaurin and Jonathan Ragan-Kelley},
  title = {{Differentiating a Tensor Language}},
  year = {2020},
  eprint = {2008.11256},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2008.11256},
}
Figure