Dual-Numbers Reverse AD, Efficiently

Tom Smeding, Matthijs Vákár
5/23/2022
cs.PL

Official code: tomsmeding/ad-dualrev-th (6 stars, Haskell, MIT, last commit 2025-12-10).

Abstract

Where dual-numbers forward-mode automatic differentiation (AD) pairs each scalar value with its tangent derivative, dual-numbers /reverse-mode/ AD attempts to achieve reverse AD using a similarly simple idea: by pairing each scalar value with a backpropagator function. Its correctness and efficiency on higher-order input languages have been analysed by Brunel, Mazza and Pagani, but this analysis was on a custom operational semantics for which it is unclear whether it can be implemented efficiently.

Show full abstract

We take inspiration from their use of /linear factoring/ to optimise dual-numbers reverse-mode AD to an algorithm that has the correct complexity and enjoys an efficient implementation in a standard functional language with resource-linear types, such as Haskell. Aside from the linear factoring ingredient, our optimisation steps consist of well-known ideas from the functional programming community. Furthermore, we observe a connection with classical imperative taping-based reverse AD, as well as Kmett's 'ad' Haskell library, recently analysed by Krawiec et al. We demonstrate the practical use of our technique by providing a performant implementation that differentiates most of Haskell98.

View on arXivView PDF

Code Implementations(1)

MIT

Implementation of dual-numbers reverse AD with TemplateHaskell

60Haskell, PythonApr 4, 202211 months agoMIT

Similar Papers

Cite this paper

@article{smeding2022dualnumbers,
  title  = {Dual-Numbers Reverse AD, Efficiently},
  author = {Tom Smeding and Matthijs Vákár},
  year   = {2022},
  eprint = {2205.11368},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2205.11368}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2205.11368.svg)](https://codeofpaper.com/paper/2205.11368)

Discussion