Abstract
Automatic differentiation (AD) aims to compute derivatives of user-defined functions, but in Turing-complete languages, this simple specification does not fully capture AD's behavior: AD sometimes disagrees with the true derivative of a differentiable program, and when AD is applied to non-differentiable or effectful programs, it is unclear what guarantees (if any) hold of the resulting code. We study an expressive differentiable programming language, with piecewise-analytic primitives, higher-order functions, and general recursion. Our main result is that even in this general setting, a version of Lee et al. [2020]'s correctness theorem (originally proven for a first-order language without partiality or recursion) holds: all programs denote so-called $\omega$PAP functions, and AD computes correct intensional derivatives of them. Mazza and Pagani [2021]'s recent theorem, that AD disagrees with the true derivative of a differentiable recursive program at a measure-zero set of inputs, can be derived as a straightforward corollary of this fact. We also apply the framework to study probabilistic programs, and recover a recent result from Mak et al. [2021] via a novel denotational argument.
Introduction
Automatic Differentiation (AD) is the foundational engine of modern artificial intelligence, yet its mathematical justification has historically been limited to "straight-line" code—simple sequences of differentiable operations. Real-world programs are significantly more complex, featuring branching logic, loops, recursion, and higher-order functions.
In these expressive settings, AD can exhibit counter-intuitive behavior. For instance, a program might be mathematically differentiable, yet AD may return a value that disagrees with the true derivative at certain "boundary" points. Furthermore, for recursive programs, it is not immediately obvious if the derivative program generated by AD will even terminate on the same inputs as the original. This paper establishes a rigorous denotational semantics to answer a fundamental question: What exactly is AD computing when the code gets complicated, and can we trust it?
The $\omega$PAP Framework and Denotational Semantics
To handle the nuances of recursion and partiality (functions that may not terminate), the authors introduce $\omega$PAP functions. This concept extends "Piecewise Analytic under Analytic Partition" (PAP) functions to accommodate the complexities of Turing-complete languages. The necessity of this extension is illustrated by the "Cantor Roadblock": simpler models of piecewise functions fail to capture the behavior of recursive programs that might approximate non-standard mathematical objects like the Cantor staircase function.
By combining standard computer science domain theory (which handles program termination) with "diffeologies" (a framework for smooth structures on unconventional spaces), the authors create a model capable of representing higher-order functions and general recursion while maintaining a robust notion of differentiability.
Correctness and Intensional Derivatives
A central insight of the paper is that AD computes intensional derivatives. Rather than finding the "true" mathematical derivative of the function's output, AD computes the derivative of the specific execution path or branch taken by the program.
The authors prove a version of the AD correctness theorem for a Turing-complete language, demonstrating that AD correctly computes these intensional derivatives for the entire class of $\omega$PAP functions. Crucially, they also establish termination parity: a derivative program generated by AD will halt if and only if the original program halts, providing a vital safety guarantee for recursive differentiable code.
Comparison with Prior Work
The proposed framework is uniquely comprehensive, unifying several recent high-profile results in the field. While previous research addressed subsets of these features—such as piecewise functions in first-order languages or higher-order functions without recursion—this work provides a singular, general theory.
As shown in the table above, this framework is the first to simultaneously support piecewise logic, recursion, and higher-order functions within a denotational approach that covers both AD and Probabilistic Programming Languages (PPLs). Results from Mazza & Pagani (2021) and Mak et al. (2021) can now be derived as straightforward corollaries of this more general theorem.
Applications to Probabilistic Programming
The framework's utility is particularly evident in the context of PPLs. In these systems, derivatives are often used for "Jacobian determinant" corrections during density calculations. The authors demonstrate that even when AD is "wrong" at a measure-zero set of points (such as at a branch boundary), the resulting intensional derivatives are still mathematically consistent for density calculations. They further show that the density functions of probabilistic program traces are themselves $\omega$PAP, providing a novel denotational proof for known operational results regarding differentiability.
Limitations and Future Work
While this work provides a robust foundation for forward-mode AD, several areas remain for future exploration:
- Reverse-Mode AD: Extending these formal proofs to backpropagation in higher-order, recursive settings.
- Variational Inference: The current model does not yet handle "gradients of expectations," as the expectation operator can smooth out discontinuities in a way the current $\omega$PAP framework isn't designed to track.
- Extensional Monads: The authors are working on a more "extensional" version of their model to more naturally incorporate measure theory for broader classes of probabilistic programs.
Summary updated