- Home
- Library
- Computational Quantum Field Theory
- Differentiating and Optimizing Field Calculations
- Forward and Reverse Automatic Differentiation
Forward and Reverse Automatic Differentiation
A lattice action is a short program, but its derivative receives contributions from every link meeting a site. Automatic differentiation (AD) propagates the chain rule through that program. It can compute the force needed by an optimizer without choosing a finite-difference step, and it can differentiate an observable with respect to fields, couplings and sources in the same calculation.
We develop both forward and reverse propagation, implement them on a periodic scalar action, and compare their derivatives with an independent calculation. The comparison also shows what AD does not establish: the derivative of a finite lattice program is not automatically the derivative of a continuum observable.
Preparation: Multivariable differentiation and the scalar action in the conventions. The calculation uses real variables and a fixed lattice.
A scalar action as a differentiable program
Let be a real field on periodic sites, with spacing , circumference and . Define the action including its source term by
The minus sign on means that has the site's Euclidean source factor . The quartic normalization is . We also evaluate the neighboring-field quantity
Here is a function of one supplied field configuration. It is not an ensemble-averaged correlation function. Both outputs are built from additions, multiplications and constant powers. This makes them a useful first example of a program whose derivatives can be checked completely.
An independent differentiation gives
and
The derivative of is with respect to , and zero with respect to all the coupling and source coordinates. These formulas will test the AD implementation; they are not substituted for its output.
Forward propagation carries a direction
Suppose a program evaluates intermediate values from earlier values. Attach a tangent , representing the response to one chosen change of the input. For an elementary operation,
For example, the product rule updates a value–tangent pair as
This is ordinary arithmetic on dual numbers with . The symbol is algebraic; it is not a small floating-point perturbation. A smooth elementary function acts as . See Baydin et al., §3.1 and §3.1.1, Eqs.(4)–(5) (PDF).
Writing the Jacobian as , initialize the input tangents with a vector . One forward sweep returns
This Jacobian–vector product, or JVP, requires no explicit Jacobian. Seeding each coordinate direction in turn constructs the full Jacobian, one column per sweep. When there are few input directions and many outputs, forward mode is a natural choice.
Our companion's Dual type implements these rules for scalar arithmetic, constant powers, square root, logarithm and sine. Replacing the numerical inputs to the same action function by dual inputs executes the actual derivative calculation. An array of precomputed analytic gradients would not be this test.
Reverse propagation carries an output covector
For a scalar weighted output , associate an adjoint with each intermediate value. After evaluating and recording the forward operations, traverse them in reverse order:
The accumulation sign matters. An intermediate value may feed several later operations, and every path contributes. For , for instance, the reverse updates are and .
Initialize the output adjoints with and all other adjoints with zero. The result at the inputs is
This is a vector–Jacobian product (VJP), conventionally written when covectors are rows. We store it as a column. One reverse sweep gives all input sensitivities of one scalar objective. The price is access to the forward values needed by the local partial derivatives, through storage or recomputation. The tape construction and reverse accumulation are described in Baydin et al., §3.2 (PDF).
The implementation stores each scalar value, its parent node indices and the corresponding local partials. It performs Eq.(8) literally. A small field program is a useful place to inspect this mechanism before relying on a large AD library.
For inputs and outputs, a full Jacobian generally needs scalar-seeded forward sweeps or scalar-seeded reverse sweeps. A single JVP or VJP needs only its requested direction. Vectorization, storage, sparsity and recomputation affect the cost of each sweep.
A checked lattice calculation
Use , , and , in fixed numerical units. The supplied fields and sources are
There are input coordinates. In their stated Euclidean coordinate norm, choose the unit vector proportional to
Changing the units or scaling of these mixed field and coupling coordinates would change the meaning of this direction; none is left implicit. The calculated values are
The maximum entrywise differences from the analytic Jacobian are for forward mode and for reverse mode in this binary64 run. The pairing check
has absolute discrepancy . The derivatives of with respect to all ten non-field inputs are exactly zero in the recorded output. This last check tests that the program has not introduced a spurious dependency.
The recorded reverse tape has nodes, including input and constant nodes, with operation nodes and parent edges. Constructing the complete Jacobian takes forward sweeps or two reverse sweeps.
Finite differences test a different source of error
A centered directional difference estimates the same derivative by
For sufficiently smooth functions in exact arithmetic, its error is . Floating-point subtraction introduces an error that typically grows as decreases. AD has no step-size truncation error, but its values and local derivatives still accumulate rounding error.
The fixed action of Eq.(1), with the inputs and direction of Eqs.(10)–(11). The solid curve compares the directional finite difference with the AD JVP. The dashed line is the separate maximum entrywise forward-AD versus analytic-Jacobian discrepancy; it is a roundoff diagnostic, not a bound on every derivative calculation.
The reproducible autodiff.py, scalar_action.py, autodiff_checks.py and selected autodiff-results.json are in examples/computational-qft/optimization/. The primitive and shared-node tests supplement the field calculation. No sampling, minimization or continuum extrapolation is involved.
Which mathematical function is differentiated?
AD applies the chain rule to the smooth operations of the executed trace, evaluated with finite-precision numbers. It does not differentiate the discontinuous rounding map as a mathematical function. Nor does it establish that two programs which differ by clipping, a changed boundary condition or a stopped iterative solve represent the same target.
If a branch is locally unchanged, differentiating its executed smooth formula can be appropriate. At a switching surface the requested derivative may be one-sided, undefined or different from the derivative of that branch. Likewise, AD through solver iterations differentiates those iterations. The derivative of their limiting root requires convergence and conditioning arguments, developed in implicit differentiation. Time evolution introduces the analogous choice between the finite time-step map and the differential equation; adjoint field methods makes that distinction explicit.
Summary
Forward AD propagates an input direction and returns . Reverse AD accumulates an output covector and returns . Both differentiate the same finite program without a finite-difference step. The scalar example checks link contributions, source signs, coupling factors, zero dependencies and directional pairing independently. Accuracy of that derivative remains distinct from accuracy of the physical discretization or model.
Exercises
1. Derive the force and its free Fourier spectrum
Derive Eq.(3), including both adjacent links. Find the coordinate Hessian at and its eigenvalues on periodic Fourier modes. Explain the difference between the coordinate gradient and the gradient in the inner product .
Solution
The two gradient terms containing are and . Their derivatives add to . Adding the on-site derivatives gives Eq.(3); the quartic factor is and the source derivative is .
For arbitrary , the coordinate Hessian is
At , substituting gives the coordinate eigenvalue
The -weighted gradient is defined by , so it is times the coordinate gradient. Its linearization has eigenvalues . Mixing these two gradients would change an optimization step or force by a spacing-dependent factor.
2. Follow a shared intermediate in both directions
For and , compute the JVP along and the VJP with scalar output seed . Show why overwriting the reverse adjoint of gives a wrong answer, and verify the pairing identity.
Solution
Forward propagation gives
There are two paths out of : one through the square and one through the sine. In reverse order their contributions must add:
If the square is implemented as , both parent edges also contribute separately, each by . An assignment in place of accumulation would lose at least one path. Finally,
which proves the JVP/VJP pairing for arbitrary seeds. The retained test uses , and an actual sine primitive on the tape, rather than replacing this trace by its simplified derivative.
References
- Atılım Güneş Baydin, Barak A. Pearlmutter, Alexey Andreyevich Radul and Jeffrey Mark Siskind, Automatic Differentiation in Machine Learning: a Survey, JMLR 18(153), 1–43 (2018), §3.1, Eq.(4), §3.1.1, Eq.(5), and §3.2 (PDF). These sections explain forward dual propagation and reverse accumulation. The lattice program and its checks are derived here.
Scrollable formula. Use Left and Right arrow keys to reveal hidden content.
Scrollable table. Use Left and Right arrow keys to reveal hidden columns.
Scrollable figure. Use Left and Right arrow keys to reveal hidden content.