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

Efficient and Sound Differentiable Programming in a Functional Array-Processing Language

Amir Shaikhha, Mathieu Huot, Shabnam Ghasemirad, Andrew W. Fitzgibbon, Simon Peyton Jones, Dimitrios VytiniotisarXiv 2022 · DOI

Introduction

Automatic Differentiation (AD) is a fundamental technology underpinning modern machine learning, optimization, and scientific computing. Traditionally, AD is categorized into two primary modes: forward-mode and reverse-mode. Forward-mode AD is conceptually simple and easy to implement but is notoriously inefficient for functions with a large number of inputs, as it requires a separate pass for each input dimension. Conversely, reverse-mode AD is highly efficient for many inputs but introduces significant implementation complexity and memory overhead due to the need for a "tape" or "reverse pass" to store intermediate values.

This paper introduces dF, a system that challenges the conventional wisdom that forward-mode AD is inherently inefficient for gradients. By leveraging a functional array-processing language ($\tilde{F}$) and a suite of aggressive compiler optimizations, the authors demonstrate that naive forward-mode transformations can be automatically optimized into code that matches or exceeds the performance of state-of-the-art reverse-mode tools.

The Forward vs. Reverse Mode Dilemma

The efficiency gap between forward and reverse mode is often visualized through the Jacobian matrix $\mathbf{J}_f$ of a function $f: \mathbb{R}^n \to \mathbb{R}^m$. Forward-mode AD computes the Jacobian column-by-column, which is efficient when $n \ll m$. Reverse-mode AD computes it row-by-row, making it the preferred choice for gradients where $m=1$ and $n$ is large.

The Jacobian matrix $\mathbf{J}_f$ illustrating the column-wise approach of forward-mode AD versus the row-wise approach of reverse-mode AD.

In functional programming contexts, these challenges are compounded by the overhead of intermediate vector allocations and redundant computations. The dF system addresses these issues by ensuring that the AD process is both mathematically sound (handling nested derivatives correctly) and computationally efficient.

System Architecture: $\tilde{F}$ and the $M$ Library

At the heart of dF is $\tilde{F}$ (F-smooth), a core ML-like functional language specifically designed for array processing. $\tilde{F}$ is restricted to allow for high-performance compilation to C while remaining expressive enough for complex mathematical operations. Built on top of $\tilde{F}$ is the $M$ library, a high-level linear algebra library that provides familiar constructs for matrix and vector manipulation.

Definitions of vector and matrix operations within the $M$ library, implemented using functional primitives like `build` and `ifold`.

The $M$ library treats arrays as "pull arrays"—functions from indices to values. This representation is crucial for the subsequent optimization phase, as it makes loop fusion natural and highly effective.

Bridging the Efficiency Gap through Optimization

The core contribution of dF is its optimization pipeline, which transforms naive forward-mode AD code into highly efficient kernels. Rather than relying on AD-specific tricks, the system uses standard compiler optimizations:

  1. Loop Fusion: By combining multiple loops, the compiler eliminates the need for intermediate array allocations, which are a major source of overhead in functional array languages.
  2. Loop-Invariant Code Motion: This technique hoists computations that do not change within a loop to the outside, significantly reducing redundant work.
  3. Loop Fission and Tuple Partial Evaluation: These optimizations break apart complex loops and eliminate unused components of tuples, further streamlining the execution path.
  4. Destination-Passing Style (DPS): To avoid the costs associated with garbage collection, dF employs DPS, a memory management technique where memory is pre-allocated and passed into functions.

A key insight of this work is that these optimizations can "discover" the efficiencies of reverse-mode AD. For instance, aggressive loop fusion can effectively eliminate the $O(n)$ overhead typically associated with forward-mode gradients, allowing the compiler to automatically derive efficient matrix calculus identities.

Evaluation and Key Results

The dF system was evaluated against a variety of established tools, including DiffSharp, Tapenade, TensorFlow, and Futhark, across several benchmarks:

  • Non-Negative Matrix Factorization (NNMF): In this iterative machine learning task, dF's optimizations—particularly loop normalization—yielded performance improvements of two to three orders of magnitude.
  • Bundle Adjustment: For this classic computer vision problem, dF outperformed even the C-based Tapenade by up to three orders of magnitude. This was largely due to the system's ability to hoist shared computations out of the Jacobian loops, an optimization that many other tools fail to perform automatically.

The results demonstrate that dF achieves parity with, and often surpasses, the performance of hand-optimized C++ and specialized reverse-mode AD frameworks.

Conclusion and Future Work

The dF system proves that a high-level functional approach to differentiable programming can be both sound and exceptionally efficient. By treating AD as a source-to-source transformation followed by aggressive optimization, the authors have bridged the gap between the simplicity of forward-mode AD and the performance requirements of modern large-scale applications.

Future work involves exploring automated rewriting strategies, such as Monte-Carlo tree search, to find optimal optimization sequences for diverse programs. Additionally, the authors plan to expand the $M$ library to include broader linear algebra support, such as matrix decompositions, and to integrate more sophisticated data layout optimizations.

Summary updated

BibTeX
@misc{shaikhha2022efficient,
  author = {Amir Shaikhha and Mathieu Huot and Shabnam Ghasemirad and Andrew W. Fitzgibbon and Simon Peyton Jones and Dimitrios Vytiniotis},
  title = {{Efficient and Sound Differentiable Programming in a Functional Array-Processing Language}},
  year = {2022},
  eprint = {2212.10307},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2212.10307},
}
Figure