Efficient Automatic Differentiation of Implicit Functions

Charles C. Margossian, Michael BetancourtarXiv 2021 · DOI

Abstract

Derivative-based algorithms are ubiquitous in statistics, machine learning, and applied mathematics. Automatic differentiation offers an algorithmic way to efficiently evaluate these derivatives from computer programs that execute relevant functions. Implementing automatic differentiation for programs that incorporate implicit functions, such as the solution to an algebraic or differential equation, however, requires particular care. Contemporary applications typically appeal to either the application of the implicit function theorem or, in certain circumstances, specialized adjoint methods. In this paper we show that both of these approaches can be generalized to any implicit function, although the generalized adjoint method is typically more effective for automatic differentiation. To showcase the relative advantages and limitations of the two methods we demonstrate their application on a suite of common implicit functions.

Introduction

Modern computational frameworks like PyTorch and JAX rely heavily on automatic differentiation (AD) to compute gradients for optimization and inference. While these tools handle explicit functions—where each step is a direct calculation—with ease, they often struggle with implicit functions. An implicit function $y(x)$ is defined by a constraint $c(x, y) = 0$ rather than an explicit formula. Common examples include the roots of algebraic equations, the optima of a loss function, or the trajectories defined by ordinary differential equations (ODEs).

Strategies for Implicit Differentiation

There are three primary ways to differentiate through an implicit step:

  1. The Trace Method: This involves differentiating directly through the internal iterations of a numerical solver (e.g., the steps of a Newton-Raphson solver). While simple to implement in standard AD libraries, it is often computationally inefficient and memory-intensive.
  2. Generalized Implicit Function Theorem (IFT): By differentiating the constraint equation $c(x, y) = 0$, one can derive a relationship between the sensitivities of the input and output. This method is mathematically direct but often requires the construction and inversion of large Jacobian matrices.
  3. The Generalized Adjoint Method: This is the focus of the paper. By using an augmented Lagrangian approach, the authors derive a "shadow" or adjoint system. This allows for the calculation of gradients without explicitly forming the full Jacobian, making it significantly more efficient for reverse-mode AD.
Implicit functions can be viewed as complex mappings that transform and warp the underlying coordinate space of the input parameters.

Unification and Generalization

A key contribution of this work is the unification of various "adjoint" methods used across different fields. The authors demonstrate that the discrete adjoint methods used in engineering and the continuous adjoint methods used in physics are specific instances of a single generalized Lagrangian framework.

To handle complex systems like ODEs, the framework is extended to infinite-dimensional spaces (such as Banach and Sobolev spaces). This is achieved using Fréchet derivatives, which generalize the concept of a derivative to functions acting on these spaces.

The definition of a derivative in generalized spaces relies on analyzing the response of a system to a small perturbation $\Delta x$ in the input space.

By treating entire trajectories as single objects in the AD graph, the authors provide a rigorous way to differentiate through continuous-time models. They show that the adjoint method naturally exploits the structure of these problems—such as the sequential nature of time—to optimize the order of linear algebra operations.

Conclusion

The generalized adjoint method provides a high-performance "recipe" for differentiating through implicit functions. By avoiding expensive Jacobian constructions and leveraging problem structure, it offers a scalable solution for modern statistical and machine learning applications. While some human intuition is still required to engineer the appropriate functionals for new problem types, this framework provides the mathematical foundation for more automated and efficient AD libraries.

Summary updated

BibTeX
@misc{margossian2021efficient,
  author = {Charles C. Margossian and Michael Betancourt},
  title = {{Efficient Automatic Differentiation of Implicit Functions}},
  year = {2021},
  eprint = {2112.14217},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.14217},
}
Figure