Staging higher-order automatic differentiation with fusion
Automatic differentiation (AD) is a family of algorithms that efficiently compute derivatives by interpreting or transforming the source code of numeric programs. Owing to its efficiency and accuracy, AD has many applications in scientific computing and machine learning. The correctness and efficiency of AD algorithms has been the subject of much research, in particular that of reverse-mode AD, which is most efficient for computing gradients of functions in many variables. The correctness of simpler reverse-mode AD algorithms has been studied, but the performance of these algorithms lags behind that of mainstream AD frameworks, which often rely on auxiliary data structures or non-obvious optimizations. The aim of this paper is to reduce this performance gap by relating elegant interpreter-based AD variants to compilers which generate efficient derivative code that resembles tape-based AD. Our approach builds further on the Nagata numbers reverse-mode AD algorithm due to van den Berg et al., reverse-engineering their algorithm in terms of established program transformation techniques from abstract algebra and functional programming. We transform the existing interpreter-based approach into an efficient code generator by means of staging with Template Haskell, leveraging a fold fusion variant for staged programs, and algebraic preservation maps to perform correct-by-construction binding time improvements. The approach is adapted to a tower-based AD approach to support higher order derivatives, and extended to support additional primitives and custom differential operators, providing a correct-by-construction code generator for higher-order AD. Benchmarks show that the resulting generated code achieves speedups compared to the reference implementation of 14.6x up to 2206x on first-order derivatives, and 18.9x up to 4.73 * 10^3x on higher-order derivatives.
Authors
- Tom Schrijvers (ORCID: https://orcid.org/0000-0001-8771-5559)
- Samuel Klumpers (ORCID: https://orcid.org/0009-0006-4001-3609)
Institutions
- KU Leuven (BE)
Publication Details
- Journal
- Zenodo (CERN European Organization for Nuclear Research)
- Published
- 2026-12-02
- DOI
- https://doi.org/10.5281/zenodo.24682241
- Primary Topic
- Logic, programming, and type systems
- Type
- article
- Field-Weighted Citation Impact
- 0.00