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

Dual-Numbers Reverse AD for Functional Array Languages

Tom Smeding, Mikolaj Konarski, Simon Peyton Jones, Andrew W. FitzgibbonarXiv 2025 · DOI

Abstract

This paper introduces a high-performance approach to reverse-mode automatic differentiation (AD) for functional array languages by lifting the classic "dual-numbers" construction to work on entire arrays at once. By combining a novel "bulk-operation transform" (BOT) with symbolic interpretation, the authors eliminate the massive overhead typically associated with differentiating scalar operations individually, producing efficient, compile-time gradient code.

Introduction: The Scalar Bottleneck in AD

Automatic Differentiation (AD) is the foundational engine of modern machine learning. However, a persistent tension exists between simplicity and performance. The "dual-numbers" technique is mathematically elegant and easy to prove correct, but it has historically performed poorly on array-heavy programs like neural networks.

The primary issue is that naive dual-numbers treat arrays as collections of independent scalars. For an array with millions of elements, this approach tracks millions of individual derivative "traces," leading to two critical problems:

  1. Delta Explosion: Memory requirements for derivative information (the "Delta" terms) grow exponentially with the number of scalar operations.
  2. The One-Hot Problem: Updating a single element in a large array derivative often results in creating a "one-hot" array (all zeros except for one value), which is computationally expensive to process and sum.

Methodology: The Three-Stage Pipeline

To address these bottlenecks while maintaining mathematical simplicity, the authors propose a three-stage transformation pipeline:

1. Bulk-Operation Transform (BOT)

The BOT acts as a sophisticated vectorization pass. It identifies code written using element-wise construction (such as map or build) and "pushes" these operations down into bulk array primitives like gather, scatter, and replicate. This ensures the AD engine processes bulk operations rather than a multitude of tiny scalar ones.

2. Dual Arrays

Instead of standard scalar dual numbers $(x, dx)$, the system utilizes "Dual Arrays" $(A, dA)$. Here, $A$ represents an entire multidimensional array, and $dA$ is a symbolic representation of its derivative. The authors define specific rules for how array-level operations, such as transpositions and sums, propagate these derivatives.

3. Symbolic Reverse Pass

Traditional reverse AD often involves interpreting a trace of operations at runtime. In this approach, the reverse pass is performed symbolically at compile-time. The derivative logic is extracted into a standalone program and optimized using monad laws and algebraic identities.

A chain of addition operations representing the accumulation of gradient terms in a reverse-mode AD trace.

Evaluation and Results

The method was evaluated using micro-benchmarks, including dot-product and self-convolution implementations, to demonstrate the successful elimination of the "Delta Explosion." The Haskell implementation, horde-ad, was compared against the standard ad library, showing favorable performance.

Key results include:

  • Elimination of Interpretation Overhead: The symbolic reverse pass converts a runtime-heavy process into a compile-time transformation.
  • Efficient Gradient Code: The generated gradient programs for operations like dot products are as efficient as hand-written versions, utilizing simple bulk multiplications and additions.
  • Formalized Pipeline: The paper provides a complete formal grammar and typing rules, bridging high-level functional programming with low-level array performance.

Discussion and Limitations

While this approach significantly improves performance, several limitations remain:

  • Static Shapes: Array dimensions must be known at compile-time; the system cannot handle "jagged" arrays or data-dependent sizes (e.g., from a filter operation).
  • General Folds: While specific reductions like sum are supported, general fold operations—which are harder to vectorize—are not yet implemented.
  • Higher-Order Functions: Some support for higher-order functions was sacrificed to facilitate the "lifting" to arrays.
  • Comparison to State-of-the-Art: While outperforming existing Haskell libraries, matching the performance of highly tuned frameworks like PyTorch or JAX remains a future objective.

Summary updated

BibTeX
@misc{smeding2025dual,
  author = {Tom Smeding and Mikolaj Konarski and Simon Peyton Jones and Andrew W. Fitzgibbon},
  title = {{Dual-Numbers Reverse AD for Functional Array Languages}},
  year = {2025},
  eprint = {2507.12640},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.12640},
}
Figure