Implicit Differentiation & Equilibrium Models
Differentiating through fixed points and optimization layers without unrolling.
Some layers are not a fixed list of operations but the solution of an equation: a fixed point, the minimizer of an inner optimization problem, the equilibrium of a dynamical system. You could backpropagate through every iteration of the solver and store them all. The implicit function theorem offers something better: the derivative of a solution depends only on the solution, not on how it was found.
What you’ll learn
- State the implicit function theorem and derive the gradient of a fixed point, , and its vector–Jacobian form, solved with memory independent of the number of forward iterations.
- Describe deep equilibrium models: root-finding forward, implicit differentiation backward, and the conditions and regularizers that make the equilibrium exist, be unique and stay stable.
- Differentiate through optimization layers via their optimality (KKT) conditions, as in OptNet and differentiable convex optimization layers.
- Compare unrolled and implicit gradients (what each differentiates, its memory, its bias, how it fails), and apply implicit differentiation to hyperparameter optimization and meta-learning.
The implicit function theorem
Suppose a layer's output is defined only implicitly, as a root of . A fixed point is the root of ; the minimizer of a smooth inner objective is a root of . The question of this lesson is how moves when does.
Existence and smoothness are the hard part of the theorem. Granting them, the formula is the chain rule applied to the identity :
For a fixed point, gives with at . Invertibility of is exactly the theorem's condition:
Training needs a VJP, not this matrix. With , define the adjoint by one linear solve; the parameter gradient is then one ordinary VJP through a single evaluation of :
Rewrite the solve as a fixed point, , and iterate. Each step is one VJP with at , and the iteration converges from any start if and only if the spectral radius . Unrolled, it is the Neumann series . This is the recurrent backpropagation of Almeida (1987) and Pineda (1987). Memory: and one evaluation of , whatever the number of forward iterations.
A scalar fixed point has , so . Backpropagating through only the last 10 iterations of the forward solver amounts to keeping the first 10 terms of the Neumann series. What derivative does that give?
Show the answer
6.5
: about 35% too small. Truncation biases the gradient toward zero, and the slower the contraction ( near 1), the longer the unroll must be. The implicit gradient gets from the solution alone.
Deep equilibrium models
Stack the same layer forever, feeding the input to every copy: . If the iteration settles, the output of this infinitely deep weight-tied network is a fixed point . A deep equilibrium model (DEQ; Bai, Kolter & Koltun, 2019) computes directly with a root-finder on , such as Broyden's quasi-Newton method or Anderson acceleration, and trains through it by implicit differentiation. Training memory no longer grows with the effective depth. The original DEQs used a transformer layer or a trellis network as for language modeling and matched explicit models of similar size at a fraction of the training memory; multiscale DEQs extended the idea to ImageNet classification and Cityscapes segmentation (Bai, Koltun & Kolter, 2020).
import torch
def solve(g, z, iters=100, tol=1e-6): # naive fixed-point iteration
for _ in range(iters):
z_new = g(z)
if (z_new - z).norm() <= tol * (1 + z.norm()):
return z_new
z = z_new
return z
class DEQ(torch.nn.Module):
def __init__(self, f): # f(z, x): one weight-tied layer
super().__init__()
self.f = f
def forward(self, x, z_init):
with torch.no_grad(): # forward solve: nothing stored
z = solve(lambda z: self.f(z, x), z_init)
z = self.f(z, x) # one tracked step: gives u^T df/dθ
z0 = z.detach().requires_grad_()
f0 = self.f(z0, x) # for VJPs with J_f at z*
if z.requires_grad: # replace g by u = J_f^T u + g
z.register_hook(lambda g: solve(
lambda u: torch.autograd.grad(f0, z0, u, retain_graph=True)[0] + g, g))
return zThe pattern of the "Deep Implicit Layers" tutorial (Kolter, Duvenaud & Johnson, NeurIPS 2020), simplified. The hook swaps the incoming gradient for the adjoint , and ordinary backpropagation through the one tracked call of then delivers . Real implementations use Broyden or Anderson solvers in both directions.
Nothing so far guarantees that a fixed point exists, that it is unique, or that the solver finds it. Two remedies stand out:
A skew-symmetric matrix has a vanishing quadratic form, , so for every :
So any unconstrained and give a strongly monotone : gradient descent on and can never leave the safe set. Conversely, split into its symmetric part, positive semidefinite and hence of the form , and its skew part .
The second remedy acts on ordinary DEQs. During training, at the equilibrium tends to creep toward , so both solves need ever more iterations and can fail. Bai, Koltun & Kolter (2021) add the penalty with , an unbiased one-sample (Hutchinson) estimate of that costs one extra VJP. Since , keeping the Frobenius norm small keeps the equilibrium attracting; they report regularized DEQs running two to three times faster than earlier ones, while noting that this alleviates rather than removes the growing instability.
A DEQ's forward solver takes 40 iterations of on one batch, and the backward fixed-point solve takes 30 VJPs. How does the activation memory of the implicit backward pass compare with backpropagating through the 40 forward iterations?
- It is about 70 layers' worth, since both solves must be stored
- It is about 40 layers' worth, the same as unrolling
- It is about one evaluation of plus , independent of both iteration counts
- It is zero, since the gradient is computed analytically
Show the answer
C. It is about one evaluation of plus , independent of both iteration counts
Only and the graph of one call of at are kept; each backward iteration reuses that graph for a VJP. Iteration counts change the compute, not the memory.
Optimization layers
A layer can also output the solution of an optimization problem, . Without constraints, is a root of , and the theorem applies with , the inner Hessian, invertible when is strongly convex near the solution:
A ridge-regression layer returns . Its optimality condition is . What is ?
Show the answer
D.
Here and , so . Check in one dimension with , , : has derivative . Shrinkage increases with , as it should.
Constraints. For subject to and , the solution and its multipliers satisfy the KKT conditions: stationarity, primal feasibility and complementary slackness . These form a square system of equations in , and differentiating it is again the implicit function theorem. OptNet (Amos & Kolter, 2017) made quadratic programs a network layer this way, with a batched primal–dual interior-point solver on the GPU whose factorization makes the backward pass nearly free once the forward solve is done. Agrawal et al. (2019) generalized the idea to any convex problem written in a disciplined parametrized form, compiled to an affine map, a cone solver and another affine map, each differentiable (the cvxpylayers library).
Unrolling versus implicit gradients
| Property | Unrolled: backprop through solver steps | Implicit: theorem at the solution |
|---|---|---|
| Differentiates | the -step procedure, including its initialization and step sizes | the exact solution |
| Memory | stored iterates (or recomputation) | and one step, independent of |
| Backward compute | VJPs | a linear solve: a few to tens of VJPs or Hessian–vector products |
| Requirements | none beyond differentiable steps | convergence, and an invertible at the solution |
| Typical failure | truncation bias when is small | wrong gradient when the forward solve has not converged or the solution is not unique |
Bilevel problems. Hyperparameter optimization is an optimization layer in disguise: the inner problem fits the weights, , and the outer problem tunes on validation data. The hypergradient is
Unrolling short horizons is not just expensive but biased. Wu, Ren, Liao & Grosse (2018) showed that meta-optimizing a learning rate through short unrolls systematically picks step sizes that are too small, by orders of magnitude in their experiments even with 100-step horizons: a short horizon rewards greedy, low-noise progress. In meta-learning, implicit MAML (Rajeswaran, Finn, Kakade & Levine, 2019) adapts to each task by solving , whose meta-gradient Jacobian is applied with conjugate gradients, with memory independent of the number of inner steps.
Key takeaways
- The implicit function theorem gives ; for a fixed point, the VJP needs one linear solve and then a single VJP through , with memory independent of the number of forward iterations.
- Deep equilibrium models find the fixed point of an infinitely deep weight-tied network by root-finding and train it implicitly; monotone parametrizations guarantee a unique equilibrium, and Jacobian regularization keeps it attracting and cheap to find.
- Optimization layers differentiate their optimality (KKT) conditions: one Hessian or KKT solve, valid at nondegenerate exact solutions and undefined where the active set changes.
- Unrolling differentiates a truncated procedure and is biased when short (toward small learning rates in meta-optimization); implicit gradients differentiate the solution but are only as good as the solve. Hypergradients and implicit MAML use them at scale.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
For a fixed point with at , which is ?
- , since is just evaluated once more
Show the answer
B.
Differentiate : , so . The inverse sums the effect of the change through every pass of the loop: .
What does the parametrization buy a monotone operator DEQ?
- For every , the matrix is strongly monotone, so (with a proximal activation) the equilibrium exists and is unique throughout training
- It makes orthogonal, so the forward pass preserves norms
- It makes the Jacobian of zero, so no backward solve is needed
- It guarantees that naive fixed-point iteration converges for any step size
Show the answer
A. For every , the matrix is strongly monotone, so (with a proximal activation) the equilibrium exists and is unique throughout training
, so strong monotonicity holds by construction, for any values the optimizer gives and . Existence and uniqueness follow from monotone operator theory.
You tune a learning rate by backpropagating a validation loss through the last 20 steps of training. What does the evidence say about this short unroll?
- It gives the same answer as the implicit hypergradient, only more cheaply
- It is unbiased but has high variance
- It is biased toward step sizes that are too small, because a short horizon rewards greedy, low-noise progress
- It is biased toward step sizes that are too large, because it ignores instability
Show the answer
C. It is biased toward step sizes that are too small, because a short horizon rewards greedy, low-noise progress
Wu et al. (2018) found that short-horizon meta-objectives prefer learning rates that are much too small, by orders of magnitude in their experiments. A truncated unroll differentiates the short procedure, not the long-run outcome you care about.
How does an optimization layer such as OptNet compute gradients with respect to the problem's parameters?
- By finite differences on the solver's output
- By differentiating the KKT optimality conditions at the solution and solving the resulting linear system, reusing the solver's factorization
- By backpropagating through every iteration of the interior-point solver
- By the straight-through estimator, since the argmin is not differentiable
Show the answer
B. By differentiating the KKT optimality conditions at the solution and solving the resulting linear system, reusing the solver's factorization
At a nondegenerate solution the KKT conditions define implicitly as a function of the parameters, so the implicit function theorem applies. Its linear system shares the matrix factored in the solver's last iteration.
Further reading
- S. Bai, J. Z. Kolter & V. Koltun, "Deep equilibrium models," NeurIPS (2019).
- E. Winston & J. Z. Kolter, "Monotone operator equilibrium networks," NeurIPS (2020).
- B. Amos & J. Z. Kolter, "OptNet: Differentiable optimization as a layer in neural networks," ICML (2017).
- J. Lorraine, P. Vicol & D. Duvenaud, "Optimizing millions of hyperparameters by implicit differentiation," AISTATS (2020).
- M. Blondel, Q. Berthet, M. Cuturi, R. Frostig, S. Hoyer, F. Llinares-López, F. Pedregosa & J.-P. Vert, "Efficient and modular implicit differentiation," NeurIPS (2022).