Automatic differentiation (AD) is a technique for computing (transposed) derivatives of functions implemented by computer programs, essentially by applying the chain-rule across program code. It is typically the method of choice for computing derivatives in machine learning and scientific computing because of its efficiency and numerical stability.
AD works by calculating the (transposed) derivative of a composite program in terms of the (transposed) derivatives of the parts, by using the chain-rule. The distinction between derivatives and transposed derivatives leads to the main distinction in automatic differentiation modes:
forward mode AD: this implements the tangent bundle functor and calculates derivatives by a forward pass to calculate function values (primals), followed by another forward pass to calculate derivative values (tangents); these two forward passes can be interleaved into a single forward pass for efficiency.
reverse mode AD: this implements the cotangent bundle functor and calculates transposed derivatives by a forward pass to calculate function values (primals), followed by a reverse pass (inverting the control flow of the original program) to calculate transposed derivative values (cotangents); these two passes cannot easily be interleaved due to their differing direction.
When calculating a derivative of a program that implements a function , reverse mode tends to be the more efficient algorithm if and forward mode tends to be more efficient if . Seeing that many tasks in machine learning and statistics require the calculation of derivatives (for use in gradient-based optimization or Monte-Carlo sampling) of functions (e.g. probability density functions) for very large, reverse mode AD tends to be the most popular algorithm.
Let us fix some class of categories with stuff . We will call its members -categories. For example, for our purposes, might include properties like
and structure like
The idea behind CHAD will be to view forward and reverse AD as the unique structure (stuff) preserving functor (homomorphism of -categories) from the initial -category to two suitably chosen -categories and .
Consider the initial -category (put differently, the S-properties category that is freely generated from -structure). We can think of this category as a programming language: its objects are types and its morphisms are programs modulo beta-equivalence and eta-equivalence. In fact, for a wide class of programming languages, we can find a suitable choice of stuff , such that the programming language arises as the initial -category. We will refer to this category as the source language of our AD transformation: its morphisms are the programs we want to differentiate.
For example, if we choose the property part of to consist of Cartesian closure and the structure part of consists of designated objects and and morphisms , , , and , then is the simply typed lambda-calculus with base types and and the primitive operations , , , and .
Given a strictly indexed category , we can form its Grothendieck construction , which is a split fibration over . Similarly, we can take the opposite category of and form from that.
For most natural choices of stuff , we can find elegant sufficient conditions on and that guarantee that and are both -categories. Let us call a strictly indexed category satisfying these conditions a -category (where we think of again as stuff). We give the corresponding for some examples for different choices of :
Let us write for the initial -category. We think of this category as the target language of automatic differentiation, in the sense that the forward/reverse derivatives of programs (morphisms) in the source language with consist of an associated primal program that is a morphism in and an associated tangent/cotangent program that is a morphism in /. As a programming language, is a linear dependent type theory over the Cartesian type theory .
Indeed, as for any -category , and are -categories, it follows that and are, in particular, -categories. Seeing that is the initial -category, we obtain unique morphisms of -categories:
The category of sets and functions gives another example of an -category, for a lot of choices of (if we choose the sets and functions that we would like to denote with the types and operations in the structure part of ). Moreover, the strictly indexed category of families of commutative monoids tends to give an example of an -category. By initiality of and , we obtain
In particular, we see that source language programs get interpreted as a function . Similarly, programs in the target language with Cartesian type get interpreted as a function . Finally, programs in the target language with linear type get interpreted as a function : families of monoid homomorphisms.
We say that CHAD calculates the correct derivative (resp. transposed derivative) of a program if the semantics of the program (resp. ) equals the pair of the semantics of and the derivative (resp. transposed derivative ) of the semantics of . CHAD is correct in the sense that it calculates the correct (transposed) derivative of any composite (possibly higher-order) program between first-order types (meaning: types built using only positive type formers), provided that it calculates the correct (transposed) derivatives of all primitive operations like that we used to generate the source language. That is, CHAD is a valid way for compositionally calculating (transposed) derivatives of composite computer programs, as long as we correctly implement the derivatives for all primitive operations (basic mathematical functions like multiplication, addition, sine, cosine) in the language.
We can prove this by a standard logical relations argument, relating smooth curves to their primal and (co)tangent curves. Viewed more abstractly, the proof follows automatically because the Artin gluing along a representable functor (like the hom out of the real numbers) of an -category is itself again an -category, for common most choices of .
Forward mode automatic differentiation was introduced by Robert Edwin Wengert in
An early description of reverse mode automatic differentiation can be found in
but it was already described earlier by others such as Seppo Linnainmaa:
A categorical analysis of (non-interleaved) forward and reverse AD is given by
Matthijs Vákár, CHAD: Combinatory Homomorphic Automatic Differentiation. 2021. arXiv preprint 2103.15776 Haskell implementationunded by European Union’s Horizon 2020 research and innovation programme under the Marie Skłodowska-Curie grant agreement No. 895827)
Fernando Lucatelli Nunes, Matthijs Vákár, CHAD for Expressive Total Languages. 2021. arXiv preprint 2110.00446unded by European Union’s Horizon 2020 research and innovation programme under the Marie Skłodowska-Curie grant agreement No. 895827)
A categorical analysis of (interleaved) forward mode AD for calculating higher order derivatives is given by
Last revised on November 16, 2021 at 12:00:38. See the history of this page for a list of all contributions to it.