3  Computational graphs and forward mode

A computer evaluates a formula one elementary operation at a time, an addition or an exponential, say. Each of these operations has a derivative that we know in closed form, and Chapter 2 showed how to combine them: by Theorem 2.2, a partial derivative of the whole computation is a sum, over paths through the intermediate variables, of products of local partial derivatives. Automatic differentiation is the bookkeeping that does this combining, inside the computer, while it evaluates the formula. It gives derivatives that are exact apart from rounding, and each pass through the computation costs a constant multiple of one evaluation of the function.

This chapter writes a function as a list of elementary steps, an evaluation trace, and as the directed graph that the list describes. It then carries a derivative along with each value through the list. That is forward mode, and dual numbers, an extension of the real numbers by a new element \(\varepsilon\) with \(\varepsilon^2 = 0\), give it an algebraic form. Forward mode computes one directional derivative per pass, so a gradient with respect to \(n\) inputs costs \(n\) passes. Chapter 4 removes that factor of \(n\).

3.1 Evaluation traces

How does a computer evaluate \(f(x, y) = x^2y + \sin(xy)\), the function of Example 1.2? It cannot work with the formula as a whole. It computes one operation at a time and gives each result a name, so that later operations can refer to it. At \((x, y) = (2, 0.5)\) the steps are those of Table 3.1.

Table 3.1: Evaluating \(x^2y + \sin(xy)\) at \((2, 0.5)\) one operation at a time, with values rounded to six decimal places.
variable operation value
\(v_1\) \(x\) \(2\)
\(v_2\) \(y\) \(0.5\)
\(v_3\) \(v_1^2\) \(4\)
\(v_4\) \(v_3 v_2\) \(2\)
\(v_5\) \(v_1 v_2\) \(1\)
\(v_6\) \(\sin v_5\) \(0.841471\)
\(v_7\) \(v_4 + v_6\) \(2.841471\)

So \(f(2, 0.5) = 2 + \sin 1 \approx 2.841471\). Each row applies one operation to variables computed in earlier rows. The variable \(v_1\) is an argument of two later rows, \(v_3\) and \(v_5\), and so is \(v_2\): the table computes \(v_1\) once and reuses it, where the formula mentions \(x\) twice.

The operations in such a list come from a short repertoire. The elementary operations of this book are addition, subtraction, multiplication and division of two numbers; adding a constant or multiplying by a constant; powers \(t \mapsto t^c\) with a constant exponent; and \(\exp\), \(\log\), \(\sin\) and \(\cos\). Each is a function of one or two variables with continuous partial derivatives on an open domain (division needs a non-zero denominator, and \(\log t\) and non-integer powers \(t^c\) need \(t > 0\)). So by Theorem 1.6 each is differentiable at every point of its domain, with partial derivatives we can write down. The function \(\max(t, 0)\), which has a corner at \(0\), waits until Chapter 7.

A list like Table 3.1 is a system of intermediate variables in the sense of Definition 2.1, in which every step is an elementary operation.

Definition 3.1 (Evaluation trace) An evaluation trace with inputs \(x_1, \dots, x_n\) is a system of intermediate variables \(v_1, \dots, v_N\) (Definition 2.1) with inputs \(v_j = x_j\) for \(j \le n\), in which each step \[ v_k = \varphi_k\bigl((v_i)_{i\in P(k)}\bigr) \qquad (k = n+1, \dots, N) \] applies an elementary operation \(\varphi_k\), together with a choice of \(m\) of the variables as outputs \(y_1, \dots, y_m\). We call step \(k\) line \(k\) of the trace, and the variables \(v_i\) with \(i \in P(k)\) its arguments. The trace is defined at \(\mathbf{a} \in \mathbb{R}^n\) if, with \(\mathbf{x} = \mathbf{a}\), every line applies its operation at a point of the operation’s domain. On the set \(D\) of such \(\mathbf{a}\), the trace computes the function \(f\colon D \to \mathbb{R}^m\) that sends \(\mathbf{a}\) to the values of the outputs.

The condition \(P(k) \subseteq \{1, \dots, k-1\}\) of Definition 2.1 says that a line uses only variables already computed. Constants live inside the operations: \(4t\) and \(1 - t\) are operations of one variable. In Table 3.1, \(n = 2\), \(N = 7\), the output is \(v_7\), and the parent sets are \(P(3) = \{1\}\), \(P(4) = \{2, 3\}\), \(P(5) = \{1, 2\}\), \(P(6) = \{5\}\) and \(P(7) = \{4, 6\}\). Each line names one operation and at most two earlier variables, so a trace is a program in three-address form: every instruction has at most two operands and one result. A trace is also called a Wengert list, after the program of Wengert (1964) that evaluated derivatives this way.

As in Definition 2.1, draw an arrow \(i \to k\) whenever \(v_i\) is an argument of line \(k\). Figure 3.1 shows the result for Table 3.1.

Figure 3.1: The computational graph of the trace in Table 3.1. Each arrow runs from an argument to the line that uses it.

The figure keeps what the computation needs, namely which values feed which operations, and drops the numbering. The numbering of Table 3.1 is one choice among several: \(v_5\) could be computed before \(v_3\), since it uses only the inputs, and the value of \(f\) would not change. A program that records a computation as it runs need not number its variables either: it can store, with each result, links to the arguments it was computed from, as the engines described in Section 4.8 do. So we want a description of the figure that does not depend on a numbering. Its boxes are called nodes and its arrows edges, and the next definition makes these terms precise.

Definition 3.2 (Directed acyclic graph, computational graph) A directed graph is a finite set of nodes together with a set of edges, each an ordered pair \((i, k)\) of distinct nodes, drawn as an arrow \(i \to k\). Then \(i\) is a parent of \(k\), and \(k\) a child of \(i\). A path from \(j\) to \(k\) is a sequence of edges \(j = i_0 \to i_1 \to \dots \to i_r = k\) with \(r \ge 0\), where \(r = 0\) gives the empty path from \(k\) to itself, as in Definition 2.1. A cycle is a path with \(r \ge 1\) from a node to itself. A graph with no cycle is acyclic. A directed graph that is acyclic is called a directed acyclic graph, or DAG.

A computational graph with inputs \(x_1, \dots, x_n\) is a DAG in which \(n\) nodes, labelled \(x_1, \dots, x_n\), have no parents, every other node \(k\) is labelled with an elementary operation \(\varphi_k\) whose arguments are the values at the parents of \(k\) (taken in a fixed order, which matters for operations such as \(t_1 - t_2\)), and some nodes are marked as outputs.

In Figure 3.1 the nodes are the seven variables. Node \(4\) has parents \(2\) and \(3\), node \(1\) has children \(3\) and \(5\), and there are two paths from node \(1\) to node \(7\), namely \(1 \to 3 \to 4 \to 7\) and \(1 \to 5 \to 6 \to 7\). Every edge goes from a smaller number to a larger one. The graph of any trace has this property, because an edge \(i \to k\) needs \(i < k\). So the numbers strictly increase along every non-empty path, no such path returns to its start, and the graph of a trace is a computational graph.

3.2 Topological orders

Going the other way, from a computational graph to a trace, means numbering the nodes so that every node comes after its parents. The graph of Figure 3.1 allows several such numberings. Its nodes could be computed in the order \(v_1, v_2, v_5, v_3, v_6, v_4, v_7\), for example, since \(v_5\) needs only the inputs and \(v_6\) needs only \(v_5\). The order \(v_1, v_2, v_4, v_3, \dots\) fails, because \(v_4\) uses \(v_3\), which would not yet exist.

Definition 3.3 (Topological order) A topological order of a directed graph with \(N\) nodes is a numbering of the nodes by \(1, \dots, N\) such that \(i < k\) for every edge \(i \to k\).

A graph with a cycle \(k_0 \to k_1 \to \dots \to k_r = k_0\) has no topological order, since the numbers would satisfy \(k_0 < k_1 < \dots < k_r = k_0\). The converse is the main result of this section. Its proof needs one fact about DAGs.

Lemma 3.1 Every DAG with at least one node has a node with no parents.

Proof. Suppose that every node has a parent, and let the graph have \(N\) nodes. Start at any node \(u_0\), let \(u_1\) be a parent of \(u_0\), \(u_2\) a parent of \(u_1\), and so on, up to \(u_N\). These are \(N + 1\) nodes from a set of \(N\), so by the pigeonhole principle \(u_i = u_j\) for some \(i < j\). Then \(u_j \to u_{j-1} \to \dots \to u_i = u_j\) is a cycle, which is impossible.

Theorem 3.1 (Topological orders exist) Every DAG with at least one node has a topological order, and it has one in which all the nodes with no parents come first.

Proof. By induction on the number of nodes \(N\). A graph with one node has no edges, and numbering the node \(1\) is a topological order.

Let \(N \ge 2\) and suppose the result for graphs with \(N - 1\) nodes. Let \(S\) be the set of nodes without parents, which is not empty by Lemma 3.1, and choose \(s\) in \(S\). Remove \(s\) and its edges. The remaining graph has \(N-1\) nodes and no cycle, since a cycle in it would be a cycle in the whole graph. By the induction hypothesis it has a topological order in which its nodes without parents come first. These include the nodes of \(S\) other than \(s\), and possibly children of \(s\) that had no other parent. No edge ends at a node without parents, so rearranging this first block among itself keeps the order topological, and we may put the nodes of \(S\) other than \(s\) at its start. Number \(s\) as \(1\) and add \(1\) to every other number. An edge either leaves \(s\), and then goes from \(1\) to a larger number, or joins two remaining nodes, and then goes forward because the order of the remaining graph is topological. The nodes of \(S\) receive the numbers \(1, \dots, |S|\).

The proof is an algorithm: repeatedly remove a node with no parents and give it the next number. Kahn (1962) described this method of topological sorting. Chapter 4 gives a second algorithm, based on depth-first search. The file lean/AutodiffBook/Ch03.lean checks the theorem by machine.

A topological order that numbers the inputs first turns a computational graph into an evaluation trace: line \(k\) applies \(\varphi_k\) to the values at the parents of node \(k\), and those parents have smaller numbers. The graph of Figure 3.1 has \(12\) topological orders that number the inputs first (Exercise 3.3 counts them). The value at each node does not depend on which one we use.

Corollary 3.1 (Evaluation is well defined) Let a computational graph be numbered by two topological orders, each with the inputs first, and evaluate the two resulting traces at the same input \(\mathbf{a}\). Every node receives the same value in both, and one trace is defined at \(\mathbf{a}\) exactly when the other is.

Proof. List the nodes in the first order and use strong induction along this list. An input node gets its component of \(\mathbf{a}\) in both traces. An operation node \(k\) gets \(\varphi_k\) applied to the values at its parents in both traces. Its parents come before \(k\) in the first order, so by the induction hypothesis their values agree, and so does the value at \(k\), or the fact that it is undefined.

From now on, a function is given by a trace \(v_1, \dots, v_N\) in a topological order, with inputs \(v_1, \dots, v_n\).

3.3 Forward mode

In Table 3.1, give \(x\) an increment \(h\) and hold \(y\) fixed. Every variable that depends on \(x\) changes, and for small \(h\) each changes by about a fixed multiple of \(h\), its rate of change. These rates can be found line by line, in the same order as the values, using the partial derivatives of each operation.

  • \(v_1 = x\) changes at rate \(1\), and \(v_2 = y\) at rate \(0\).
  • \(v_3 = v_1^2\) has derivative \(2v_1 = 4\) with respect to \(v_1\), so it changes at rate \(4 \cdot 1 = 4\).
  • \(v_4 = v_3v_2\) changes at rate \(v_2 \cdot 4 + v_3 \cdot 0 = 2\), by the product rule (Theorem 2.3).
  • \(v_5 = v_1v_2\) changes at rate \(v_2\cdot 1 + v_1 \cdot 0 = 0.5\).
  • \(v_6 = \sin v_5\) changes at rate \(\cos(v_5)\cdot 0.5 = 0.5\cos 1\).
  • \(v_7 = v_4 + v_6\) changes at rate \(2 + 0.5\cos 1 \approx 2.270151\).

The formula of Example 1.2 gives \(\partial f/\partial x = 2xy + y\cos(xy)\), which at \((2, 0.5)\) is \(2 + 0.5\cos 1\), the rate found in the last line. Each rate was the sum, over the arguments of the line, of the rate of the argument times the local partial derivative of the operation. That recipe is forward mode.

For each \(k\) and each \(\mathbf{x}\) in the set \(D\) where the trace is defined, write \(F_k(\mathbf{x})\) for the value of \(v_k\), as in Definition 2.1. So \(F_j(\mathbf{x}) = x_j\) for \(j \le n\), and the outputs are the components of \(f\). As in Theorem 2.2, write \(d_{ki} = \partial\varphi_k/\partial v_i\) for the local partial derivative of line \(k\) with respect to its argument \(v_i\), evaluated at the values of the arguments of line \(k\).

Definition 3.4 (Seed and tangents) Let a trace be defined at \(\mathbf{a}\), and let \(\mathbf{u} \in \mathbb{R}^n\), called the seed. The tangents \(\dot v_1, \dots, \dot v_N\) are the numbers given by \(\dot v_j = u_j\) for \(j \le n\) and \[ \dot v_k = \sum_{i \in P(k)} d_{ki}\,\dot v_i \qquad (k = n+1, \dots, N), \tag{3.1}\] with the local partial derivatives evaluated at \(\mathbf{x} = \mathbf{a}\). The forward pass computes each value \(v_k\) and then its tangent \(\dot v_k\), for \(k = 1, \dots, N\).

In words, Equation 3.1 says that the tangent of a line is the sum, over its arguments, of the argument’s tangent times the local partial derivative. For an operation of one variable it is the one-variable chain rule: if \(v_k = \sin v_i\) then \(\dot v_k = \cos(v_i)\,\dot v_i\). For a product \(v_k = v_iv_l\) it is the product rule, \(\dot v_k = v_l\dot v_i + v_i\dot v_l\). In the example above the seed was \(\mathbf{u} = \mathbf{e}_1\), and \(\dot v_k\) was the rate of change of \(v_k\).

The seed says in which direction the inputs move. Along the line \(\mathbf{x} = \mathbf{a} + s\mathbf{u}\), each variable becomes a function \(s \mapsto F_k(\mathbf{a} + s\mathbf{u})\) of one real variable \(s\), and Theorem 3.2 below, with Theorem 1.7, shows that \(\dot v_k\) is its derivative at \(s = 0\): the slope of the tangent line to its graph, which is where the name comes from. The dot follows Newton’s notation for a derivative with respect to a single parameter. With seed \(\mathbf{u} = (0.3, -1.7)\) in Table 3.1, for instance, both inputs move at once, and \(\dot v_7 = 0.3\,(2 + 0.5\cos 1) - 1.7\,(4 + 2\cos 1)\) is the rate of change of \(f\) along that line.

Theorem 2.2 assumes that the inputs range over an open set on which every line is defined. The set \(D\) is such a set.

Lemma 3.2 Let \(D \subseteq \mathbb{R}^n\) be the set of points at which an evaluation trace with inputs \(x_1, \dots, x_n\) is defined. Then \(D\) is open, and each \(F_k\) is continuous on \(D\).

Proof. Let \(D_k\) be the set of \(\mathbf{x}\) at which lines \(1, \dots, k\) are defined, so \(D_n = \mathbb{R}^n\) and \(D_N = D\). We show by induction on \(k\) that \(D_k\) is open and \(F_1, \dots, F_k\) are continuous on it. The case \(k = n\) holds because each coordinate function is continuous. For \(k > n\), let \(G(\mathbf{x}) = (F_i(\mathbf{x}))_{i \in P(k)}\) and let \(U_k\) be the open domain of \(\varphi_k\). Then \(D_k\) is the set of \(\mathbf{x}\) in \(D_{k-1}\) with \(G(\mathbf{x}) \in U_k\). Take such an \(\mathbf{x}\). There is a disc of radius \(\rho > 0\) around \(G(\mathbf{x})\) inside \(U_k\), and since \(G\) is continuous at \(\mathbf{x}\) there is a \(\delta > 0\) such that every \(\mathbf{x}'\) in \(D_{k-1}\) within \(\delta\) of \(\mathbf{x}\) has \(G(\mathbf{x}')\) within \(\rho\) of \(G(\mathbf{x})\). Shrinking \(\delta\) so that the disc of radius \(\delta\) around \(\mathbf{x}\) lies in the open set \(D_{k-1}\), that whole disc lies in \(D_k\). So \(D_k\) is open, and \(F_k = \varphi_k \circ G\) is continuous on it as a composition of continuous functions.

Theorem 3.2 (Forward mode computes Jacobian–vector products) Let an evaluation trace with inputs \(x_1, \dots, x_n\) compute \(f\colon D \to \mathbb{R}^m\), let \(\mathbf{a} \in D\), and let \(\mathbf{u} \in \mathbb{R}^n\). Then every \(F_k\) is differentiable at \(\mathbf{a}\), and the tangents of Definition 3.4 with seed \(\mathbf{u}\) satisfy \[ \dot v_k = \nabla F_k(\mathbf{a}) \cdot \mathbf{u} \qquad (k = 1, \dots, N). \] In particular \(f\) is differentiable at \(\mathbf{a}\), and the tangents of the outputs \(y_1, \dots, y_m\) are the components of the Jacobian–vector product \(J_f(\mathbf{a})\,\mathbf{u}\).

Proof. By Lemma 3.2 the inputs range over an open set on which every line is defined, and every elementary operation is differentiable on its domain. So Theorem 2.2 applies: every \(F_k\) is differentiable at \(\mathbf{a}\), and \(\partial F_k/\partial x_j(\mathbf{a})\) is the sum over paths from \(j\) to \(k\) of the products of local partial derivatives. For \(k > n\), every path from an input \(j\) to \(k\) is non-empty, and grouping the paths by their last arrow \(i \to k\) gives \[ \frac{\partial F_k}{\partial x_j}(\mathbf{a}) = \sum_{i\in P(k)} d_{ki}\,\frac{\partial F_i}{\partial x_j}(\mathbf{a}) , \] which is Equation 2.5. Multiplying by \(u_j\) and adding over \(j\), \[ \nabla F_k(\mathbf{a})\cdot\mathbf{u} = \sum_{i\in P(k)} d_{ki}\,\nabla F_i(\mathbf{a})\cdot\mathbf{u} . \] So the numbers \(\nabla F_k(\mathbf{a})\cdot\mathbf{u}\) satisfy the recurrence Equation 3.1. They also agree with the tangents on the inputs, since \(\nabla F_j(\mathbf{a}) = \mathbf{e}_j\) and \(\mathbf{e}_j\cdot\mathbf{u} = u_j\) for \(j \le n\). The recurrence determines \(\dot v_k\) from the earlier values, so by induction on \(k\), \(\dot v_k = \nabla F_k(\mathbf{a})\cdot\mathbf{u}\) for every \(k\).

The components of \(f\) are the functions \(F_k\) of the output variables. Each is differentiable at \(\mathbf{a}\), so \(f\) is (Lemma 2.1), and component \(l\) of \(J_f(\mathbf{a})\mathbf{u}\) is \(\nabla F_k(\mathbf{a})\cdot\mathbf{u}\) for the variable \(v_k\) that is output \(y_l\).

So one forward pass gives the product of the Jacobian with one vector, without forming the Jacobian. With \(\mathbf{u} = \mathbf{e}_j\) it gives column \(j\) of \(J_f(\mathbf{a})\), the partial derivatives of every output with respect to \(x_j\). With a unit vector it gives a directional derivative (Theorem 1.7).

Example 3.1 Run the trace of Table 3.1 at \((2, 0.5)\) with the seeds \(\mathbf{e}_1\) and \(\mathbf{e}_2\). Write \(c = \cos 1 \approx 0.540302\).

variable tangent rule \(\dot v\) for \(\mathbf{e}_1\) \(\dot v\) for \(\mathbf{e}_2\)
\(v_1\) seed \(1\) \(0\)
\(v_2\) seed \(0\) \(1\)
\(v_3\) \(2v_1\dot v_1\) \(4\) \(0\)
\(v_4\) \(v_2\dot v_3 + v_3\dot v_2\) \(2\) \(4\)
\(v_5\) \(v_2\dot v_1 + v_1\dot v_2\) \(0.5\) \(2\)
\(v_6\) \(\cos(v_5)\,\dot v_5\) \(0.5c\) \(2c\)
\(v_7\) \(\dot v_4 + \dot v_6\) \(2 + 0.5c\) \(4 + 2c\)

The first column of tangents repeats the computation that opened this section. The last row gives \(\nabla f(2, 0.5) = (2 + 0.5\cos 1,\; 4 + 2\cos 1) \approx (2.270151, 5.080605)\), and the formula \(\partial f/\partial y = x^2 + x\cos(xy)\) of Example 1.2 gives the same second component at \((2, 0.5)\).

Each tangent line costs a few arithmetic operations more than the value line beside it: the partial derivatives of the operation, then one multiplication and one addition for each argument. So a forward pass costs a constant multiple of evaluating \(f\), and Chapter 4 makes the constant precise. A gradient of \(f\colon\mathbb{R}^n \to \mathbb{R}\) needs \(n\) passes, one for each \(\mathbf{e}_j\), and costs about \(n\) times as much as \(f\). For the language model of Chapter 11, \(n = 4192\).

3.4 Dual numbers

Each line of a forward pass handles a pair: a value and its tangent. Pairs of this kind form a number system of their own, in which the rules for tangents become ordinary arithmetic. To see it, take \(p(t) = t^3 - 2t\) and evaluate it at \(2 + \varepsilon\), where \(\varepsilon\) is a formal symbol, an increment too small for its square to matter. Expanding and then discarding every term with \(\varepsilon^2\) or a higher power, \[ p(2 + \varepsilon) = (8 + 12\varepsilon + 6\varepsilon^2 + \varepsilon^3) - (4 + 2\varepsilon) \;\longrightarrow\; 4 + 10\varepsilon . \] The number without \(\varepsilon\) is \(p(2) = 4\), and the coefficient of \(\varepsilon\) is \(p'(2) = 3\cdot 2^2 - 2 = 10\). Discarding \(\varepsilon^2\) keeps exactly the first-order part of the increment, which is the derivative. Making “discard \(\varepsilon^2\)” a rule of arithmetic gives the dual numbers.

Definition 3.5 (Dual numbers) A dual number is an expression \(a + b\varepsilon\) with \(a\), \(b\) real. Its real part is \(a\) and its \(\varepsilon\)-part is \(b\), and two dual numbers are equal when both parts are equal. Dual numbers are added and multiplied as polynomials in \(\varepsilon\), followed by the rule \(\varepsilon^2 = 0\): \[ (a + b\varepsilon) + (c + d\varepsilon) = (a + c) + (b + d)\varepsilon, \qquad (a + b\varepsilon)(c + d\varepsilon) = ac + (ad + bc)\varepsilon . \] We write \(\mathbb{R}[\varepsilon]\) for the set of dual numbers, and identify a real number \(a\) with \(a + 0\varepsilon\).

The \(\varepsilon\)-part of a product is the product rule. If \(a\) and \(b\) are the value and the derivative of a function \(p\) at some point, and \(c\) and \(d\) those of \(q\), then \(ac\) and \(ad + bc\) are the value and the derivative of \(pq\).

Dual numbers have a model in \(2\times2\) matrices. Let \[ E = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix}, \qquad M(a + b\varepsilon) = aI + bE = \begin{pmatrix} a & b \\ 0 & a \end{pmatrix}. \] The matrix \(M(a + b\varepsilon)\) holds the real part twice on its diagonal and the \(\varepsilon\)-part in its top right corner, and \(E = M(\varepsilon)\). Then \(E^2 = 0\), and expanding \((aI + bE)(cI + dE)\) gives \(acI + (ad + bc)E\). So \(M\) sends sums to sums and products to products, and different dual numbers go to different matrices. The associative and distributive laws of matrix arithmetic therefore hold for dual numbers. The formula for the product is symmetric in the two factors, so multiplication of dual numbers is also commutative, which matrix multiplication in general is not. In particular a polynomial \(p(t) = c_0 + c_1t + \dots + c_dt^d\) with real coefficients has a well-defined value \(p(a + b\varepsilon)\).

A dual number \(a + b\varepsilon\) with \(a \ne 0\) has an inverse, since \[ (a + b\varepsilon)\Bigl(\frac1a - \frac{b}{a^2}\varepsilon\Bigr) = 1 + \Bigl(\frac{b}{a} - \frac{b}{a}\Bigr)\varepsilon = 1 . \] A dual number \(b\varepsilon\) with zero real part has none, because \((b\varepsilon)(c + d\varepsilon) = bc\,\varepsilon\) is never \(1\).

Lemma 3.3 For every integer \(k \ge 1\) and all real \(a\), \(b\), \((a + b\varepsilon)^k = a^k + k a^{k-1} b\,\varepsilon\).

Proof. For \(k = 1\) both sides are \(a + b\varepsilon\). If the formula holds for \(k\), then \[ (a + b\varepsilon)^{k+1} = (a^k + k a^{k-1}b\varepsilon)(a + b\varepsilon) = a^{k+1} + (a^k b + k a^{k}b)\varepsilon = a^{k+1} + (k+1)a^k b\,\varepsilon . \]

Part (a) of the next theorem says that the opening example is no accident: for polynomials, dual arithmetic computes the value and the derivative. For any other operation, such as \(\sin\) or \(\exp\), the value at a dual number is not yet defined, and we define it by the same formula: for a function \(g\) of one variable, differentiable at \(a\), \[ g(a + b\varepsilon) = g(a) + g'(a)\,b\,\varepsilon , \tag{3.2}\] so the real part is the value of \(g\) and the \(\varepsilon\)-part is the derivative times \(b\). For an operation \(\varphi\) of two variables, differentiable at \((a_1, a_2)\), \[ \varphi(a_1 + b_1\varepsilon,\; a_2 + b_2\varepsilon) = \varphi(a_1, a_2) + \Bigl(\frac{\partial\varphi}{\partial t_1}\,b_1 + \frac{\partial\varphi}{\partial t_2}\,b_2\Bigr)\varepsilon , \] with the partial derivatives evaluated at \((a_1, a_2)\): the \(\varepsilon\)-part is the directional derivative of \(\varphi\) in the direction \((b_1, b_2)\). For addition and multiplication this agrees with Definition 3.5, and for division it agrees with multiplying by the inverse (Exercise 3.4). Parts (b) and (c) say that the definition is consistent with composition, so that any function built from elementary operations obeys the same formula.

Theorem 3.3 (Derivatives from dual numbers)  

  1. For every polynomial \(p\) with real coefficients and all real \(a\) and \(b\), \[ p(a + b\varepsilon) = p(a) + p'(a)\,b\,\varepsilon . \]

  2. Let \(h\) be differentiable at \(a\) and \(g\) differentiable at \(h(a)\), both extended to dual numbers by Equation 3.2. Then \(g(h(a + b\varepsilon)) = (g\circ h)(a) + (g\circ h)'(a)\,b\,\varepsilon\): extending \(g\circ h\) directly gives the same dual number as extending \(h\) and \(g\) and composing.

  3. Let an evaluation trace with inputs \(x_1, \dots, x_n\) compute \(f\colon D \to \mathbb{R}^m\), and let \(\mathbf{a} \in D\) and \(\mathbf{u} \in \mathbb{R}^n\). Evaluate the trace in dual arithmetic, with each operation extended as above and inputs \(x_j = a_j + u_j\varepsilon\). Then output \(l\) is \(f_l(\mathbf{a}) + \bigl(J_f(\mathbf{a})\mathbf{u}\bigr)_l\,\varepsilon\). In particular, for \(n = m = 1\), \(f(a + b\varepsilon) = f(a) + f'(a)\,b\,\varepsilon\).

Proof.

  1. Write \(p(t) = \sum_{k=0}^d c_k t^k\). By Lemma 3.3, and because the term \(k = 0\) is the constant \(c_0\), \[ p(a + b\varepsilon) = c_0 + \sum_{k=1}^d c_k\bigl(a^k + k a^{k-1}b\varepsilon\bigr) = \sum_{k=0}^d c_k a^k + \Bigl(\sum_{k=1}^d k c_k a^{k-1}\Bigr) b\,\varepsilon = p(a) + p'(a)\,b\,\varepsilon . \]

  2. Apply Equation 3.2 twice and then the chain rule of one-variable calculus: \[ g(h(a + b\varepsilon)) = g\bigl(h(a) + h'(a)b\,\varepsilon\bigr) = g(h(a)) + g'(h(a))\,h'(a)\,b\,\varepsilon = (g\circ h)(a) + (g\circ h)'(a)\,b\,\varepsilon . \]

  3. We show by induction on \(k\) that the dual value of \(v_k\) is \(v_k + \dot v_k\varepsilon\), where \(v_k\) and \(\dot v_k\) are the value and the tangent of Definition 3.4 with seed \(\mathbf{u}\). For \(k \le n\) this is the choice of inputs. For \(k > n\), the extended operation applied to the dual arguments \(v_i + \dot v_i\varepsilon\), \(i \in P(k)\), has real part \(\varphi_k((v_i)_{i\in P(k)}) = v_k\) and \(\varepsilon\)-part \(\sum_{i\in P(k)} d_{ki}\,\dot v_i\), which is \(\dot v_k\) by Equation 3.1. The claim about the outputs is now Theorem 3.2.

So a program that knows nothing about derivatives computes them when it receives dual numbers instead of real ones, provided each elementary operation knows its own derivative. The file lean/AutodiffBook/Ch03.lean checks part (a) by machine, and also the product rule in the \(\varepsilon\)-part of a product.

Example 3.2 Take \(f(t) = e^{\sin t}\) at \(t = 0\), which is not a polynomial. By Equation 3.2, \(\sin(0 + \varepsilon) = \sin 0 + \cos(0)\, \varepsilon = \varepsilon\), and then \(\exp(\varepsilon) = e^0 + e^0 \varepsilon = 1 + \varepsilon\). By Theorem 3.3 (b), \(f(0) = 1\) and \(f'(0) = 1\), which agrees with \(f'(t) = \cos(t)\,e^{\sin t}\).

3.5 Forward mode in code

A dual number is a pair of floating-point numbers, and the rules of Definition 3.5 and Equation 3.2 take a few lines each. The class below holds the real part in a and the \(\varepsilon\)-part in b, and it tells Python what +, -, * and / mean for such pairs (programmers call this operator overloading). An ordinary function written with these symbols then works on dual numbers unchanged.

import math


class Dual:
    """The dual number a + b eps, where eps**2 = 0."""

    def __init__(self, a, b=0.0):
        self.a, self.b = a, b

    def __add__(self, other):
        other = as_dual(other)
        return Dual(self.a + other.a, self.b + other.b)

    def __mul__(self, other):
        other = as_dual(other)
        return Dual(self.a * other.a, self.a * other.b + self.b * other.a)

    def __neg__(self):
        return Dual(-self.a, -self.b)

    def __sub__(self, other):
        return self + (-other)

    def __truediv__(self, other):
        other = as_dual(other)
        return self * Dual(1 / other.a, -other.b / other.a**2)

    def __rsub__(self, other):
        return as_dual(other) - self

    def __rtruediv__(self, other):
        return as_dual(other) / self

    __radd__ = __add__
    __rmul__ = __mul__

    def __repr__(self):
        return f"{self.a:.6f} + {self.b:.6f} eps"


def as_dual(x):
    """Treat a real number x as x + 0 eps."""
    return x if isinstance(x, Dual) else Dual(x)


def extend(g, dg):
    """Extend g, with derivative dg, to dual numbers:
    g(a + b eps) = g(a) + g'(a) b eps."""
    def g_dual(x):
        x = as_dual(x)
        return Dual(g(x.a), dg(x.a) * x.b)
    return g_dual


exp = extend(math.exp, math.exp)
log = extend(math.log, lambda a: 1 / a)
sin = extend(math.sin, math.cos)
cos = extend(math.cos, lambda a: -math.sin(a))

The methods __add__ and __mul__ are the two formulas of Definition 3.5. Subtraction adds the negative, and division multiplies by the inverse \(1/c - (d/c^2)\varepsilon\) found above, so it needs a non-zero real part in the divisor. The methods whose names begin with __r handle a real number on the left, as in 1 - x, by first turning it into the dual number \(a + 0\varepsilon\) with as_dual. The function extend takes a function \(g\) and its derivative \(g'\) and returns the extension Equation 3.2, and the last four lines use it for \(\exp\), \(\log\), \(\sin\) and \(\cos\).

One forward pass with seed \(\mathbf{u}\) gives \(f(\mathbf{a})\) and \(J_f(\mathbf{a})\mathbf{u}\), by Theorem 3.3 (c). For a function with one output, \(n\) passes give the gradient. Below, jvp builds the inputs \(a_j + u_j\varepsilon\), evaluates \(f\) on them and returns the two parts of the output, and gradient calls it once with each seed \(\mathbf{e}_j\).

def jvp(f, a, u):
    """Return f(a) and the directional derivative grad f(a) . u,
    by one forward pass."""
    out = f(*[Dual(ai, ui) for ai, ui in zip(a, u)])
    return out.a, out.b


def gradient(f, a):
    """Return grad f(a) by n forward passes with seeds e_1, ..., e_n."""
    n = len(a)
    seeds = [[1.0 if i == j else 0.0 for i in range(n)] for j in range(n)]
    return [jvp(f, a, e)[1] for e in seeds]

Applied to the function of Table 3.1, the passes reproduce the tangent table of Example 3.1.

def f(x, y):
    return x * x * y + sin(x * y)


print(f(Dual(2.0, 1.0), Dual(0.5, 0.0)))   # seed e_1
print(f(Dual(2.0, 0.0), Dual(0.5, 1.0)))   # seed e_2
print(gradient(f, [2.0, 0.5]))
2.841471 + 2.270151 eps
2.841471 + 5.080605 eps
[2.27015115293407, 5.0806046117362795]

3.6 Symbolic differentiation and expression swell

A computer algebra system differentiates formulas by the rules of calculus and returns a formula. That is exact, but the formula can be far larger than the computation it describes, because a formula written out in full repeats every subexpression each time it is used, while a trace computes it once and refers to it by name.

Example 3.3 (Expression swell) Let \(g_0(x) = x\) and \(g_k(x) = 4\,g_{k-1}(x)\bigl(1 - g_{k-1}(x)\bigr)\) for \(k \ge 1\), the \(k\)th iterate of the logistic map. A trace computes \(g_k\) with \(3k\) operations, three per step: \[ u = 1 - v, \qquad w = v\,u, \qquad v_{\text{new}} = 4w . \] Written out as a formula, \(g_k\) contains \(g_{k-1}\) twice, so the number \(L_k\) of occurrences of \(x\) satisfies \(L_k = 2L_{k-1}\) with \(L_0 = 1\), and \(L_k = 2^k\). Differentiating by the product and constant-multiple rules, without simplifying, \[ g_k' = 4\bigl(g_{k-1}'\,(1 - g_{k-1}) + g_{k-1}\,(-g_{k-1}')\bigr), \] which contains \(g_{k-1}'\) twice and \(g_{k-1}\) twice. The number \(D_k\) of occurrences of \(x\) in the formula for \(g_k'\) therefore satisfies the first-order recurrence \[ D_k = 2D_{k-1} + 2L_{k-1} = 2D_{k-1} + 2^k, \qquad D_0 = 0, \] because \(g_0' = 1\) contains no \(x\). Dividing by \(2^k\) gives \(D_k/2^k = D_{k-1}/2^{k-1} + 1\), so \(D_k = k\,2^k\). For \(k = 10\) the formula for \(g_{10}\) contains \(x\) \(1024\) times and the formula for its derivative \(10\,240\) times, while forward mode computes both with a trace of \(30\) operations and \(30\) tangent lines. Expanding \(g_k\) into powers of \(x\) does not help, since \(g_k\) is a polynomial of degree \(2^k\).

Computer algebra systems store formulas with shared subexpressions, which removes much of this growth, and differentiating a formula with sharing while keeping the sharing amounts to automatic differentiation. The remaining difference is that automatic differentiation never builds a formula for the derivative at all: it computes numbers, at one point.

3.7 Finite differences

The simplest way to estimate a derivative numerically is a difference quotient. For \(f\) of one variable and a small step \(h > 0\), the forward difference and central difference are \[ \frac{f(a + h) - f(a)}{h} \qquad\text{and}\qquad \frac{f(a + h) - f(a - h)}{2h} . \] If \(f''\) is continuous on \([a, a+h]\), Taylor’s theorem with the Lagrange remainder gives \(f(a+h) = f(a) + f'(a)h + \tfrac12 f''(\xi)h^2\) for some \(\xi\) in \((a, a+h)\), so the forward difference differs from \(f'(a)\) by \(\tfrac12 f''(\xi)h\): the truncation error is of order \(h\). The central difference cancels the \(h^2\) terms and has truncation error of order \(h^2\) (Exercise 3.8).

Making \(h\) small reduces the truncation error but not the total error. A computer stores \(f(a+h)\) and \(f(a)\) with a relative rounding error of about \(10^{-16}\), and when \(h\) is small these two numbers agree in most of their digits, so their difference keeps only a few correct ones. The rounding error in the quotient grows like \(10^{-16}/h\). The two errors balance near \(h = 10^{-8}\) for the forward difference, where the total error is about \(10^{-8}\), so at best about half the digits of the derivative are correct. Table 3.2 shows both effects, and Theorem A.2 in Appendix A proves the estimates.

Table 3.2: Errors of difference quotients for the derivative of \(e^x\) at \(x = 1\), computed in double precision.
\(h\) forward difference error central difference error
\(10^{-1}\) \(1.4 \times 10^{-1}\) \(4.5 \times 10^{-3}\)
\(10^{-2}\) \(1.4 \times 10^{-2}\) \(4.5 \times 10^{-5}\)
\(10^{-4}\) \(1.4 \times 10^{-4}\) \(4.5 \times 10^{-9}\)
\(10^{-6}\) \(1.4 \times 10^{-6}\) \(1.6 \times 10^{-10}\)
\(10^{-8}\) \(6.6 \times 10^{-9}\) \(6.6 \times 10^{-9}\)
\(10^{-10}\) \(1.5 \times 10^{-6}\) \(6.7 \times 10^{-7}\)
\(10^{-12}\) \(4.3 \times 10^{-4}\) \(2.1 \times 10^{-4}\)

A forward difference for each input costs \(n + 1\) evaluations of \(f\) for a gradient, about the same as \(n\) forward passes, and gives around eight correct digits instead of sixteen. Finite differences still have a use: they need no access to how \(f\) is computed, which makes them a check on a gradient computed some other way. The source of this book uses central differences to test the gradients that automatic differentiation produces.

3.8 Notes and further reading

Wengert (1964) described a program that splits a function into elementary steps and differentiates each step, which is forward mode. Griewank and Walther (2008) is the standard reference on automatic differentiation; its early chapters treat evaluation traces and forward mode, with their cost, in far more detail than here. The survey of Baydin et al. (2018) compares automatic differentiation with symbolic and numerical differentiation for readers from machine learning, and uses the logistic map of Example 3.3 as its example of expression swell. Dual numbers go back to Clifford (1871). The source-removal algorithm in the proof of Theorem 3.1 is the one of Kahn (1962).

3.9 Exercises

Exercise 3.1 Write an evaluation trace for \(f(x_1, x_2, x_3) = (x_1 + x_2)(x_2 + x_3)\,e^{x_1}\), draw its computational graph, and evaluate it at \((0, 1, 2)\).

Exercise 3.2 For the trace of Exercise 3.1, run one forward pass at \((0, 1, 2)\) with seed \(\mathbf{u} = (1, -1, 2)\). Check the result against \(\nabla f(0, 1, 2)\cdot\mathbf{u}\) computed from the formula.

Exercise 3.3 Show that the graph of Figure 3.1 has exactly \(12\) topological orders that number the two inputs first. (Hint: the nodes \(3\) and \(4\) form one chain, \(5\) and \(6\) another, and node \(7\) comes last.)

Exercise 3.4  

  1. Show that if \(c \ne 0\), \((a + b\varepsilon)/(c + d\varepsilon) = a/c + \bigl((bc - ad)/c^2\bigr)\varepsilon\), where division means multiplication by the inverse.

  2. Deduce that for polynomials \(p\) and \(q\) with \(q(a) \ne 0\), the rational function \(r = p/q\) satisfies \(r(a + b\varepsilon) = r(a) + r'(a)\,b\,\varepsilon\), and that this agrees with Equation 3.2.

Exercise 3.5 Extend \(g(t) = \sqrt t\) to dual numbers with \(a > 0\) by Equation 3.2. Show that \(\bigl(\sqrt{a + b\varepsilon}\bigr)^2 = a + b\varepsilon\), and compute \(\sqrt{4 + \varepsilon}\).

Exercise 3.6 Let \(\mathbb{R}[\varepsilon]_3\) be the numbers \(a + b\varepsilon + c\varepsilon^2\), multiplied as polynomials in \(\varepsilon\) with the rule \(\varepsilon^3 = 0\). Show that for every polynomial \(p\), \[ p(a + \varepsilon) = p(a) + p'(a)\,\varepsilon + \tfrac12 p''(a)\,\varepsilon^2 , \] and use it to find \(p''(1)\) for \(p(t) = t^4 - 3t^2\).

Exercise 3.7 Let \(s_0(x) = x\) and \(s_k(x) = s_{k-1}(x)\cdot s_{k-1}(x) + x\).

  1. Find the number of occurrences of \(x\) in the formula for \(s_k\) written out in full.

  2. Differentiating by the sum and product rules without simplifying gives \(s_k' = s_{k-1}'\,s_{k-1} + s_{k-1}\,s_{k-1}' + 1\). Find the number of occurrences of \(x\) in the formula for \(s_k'\).

  3. How many operations does a trace for \(s_k\) need, and how many tangent lines does forward mode add?

Exercise 3.8 Suppose \(f'''\) is continuous on \([a - h, a + h]\) and \(|f'''(t)| \le M\) there. Show that \[ \left|\frac{f(a+h) - f(a-h)}{2h} - f'(a)\right| \le \frac{M h^2}{6} . \]