Simple Algorithm
Deep Learning · Emergence · 28 min

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, ∂z⋆/∂θ=(I−Jf)−1∂f/∂θ\partial\mathbf z^\star/\partial\boldsymbol\theta = (I - J_f)^{-1}\partial f/\partial\boldsymbol\theta, 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 z⋆(θ)\mathbf z^\star(\boldsymbol\theta) is defined only implicitly, as a root of F(z,θ)=0F(\mathbf z,\boldsymbol\theta) = \mathbf 0. A fixed point z=f(z,x;θ)\mathbf z = f(\mathbf z, \mathbf x;\boldsymbol\theta) is the root of F=f−zF = f - \mathbf z; the minimizer of a smooth inner objective g(z,θ)g(\mathbf z,\boldsymbol\theta) is a root of F=∇zgF = \nabla_{\mathbf z}g. The question of this lesson is how z⋆\mathbf z^\star moves when θ\boldsymbol\theta does.

DerivationFrom the theorem to a backward pass
  1. ∂F∂z ∂z⋆∂θ+∂F∂θ=0\frac{\partial F}{\partial\mathbf z}\,\frac{\partial\mathbf z^\star}{\partial\boldsymbol\theta} + \frac{\partial F}{\partial\boldsymbol\theta} = 0

    Existence and smoothness are the hard part of the theorem. Granting them, the formula is the chain rule applied to the identity F(z⋆(θ),θ)≡0F(\mathbf z^\star(\boldsymbol\theta),\boldsymbol\theta)\equiv\mathbf 0:

  2. ∂z⋆∂θ=(I−Jf)−1 ∂f∂θ\frac{\partial\mathbf z^\star}{\partial\boldsymbol\theta} = (I - J_f)^{-1}\,\frac{\partial f}{\partial\boldsymbol\theta}

    For a fixed point, F=f−zF = f - \mathbf z gives ∂F/∂z=Jf−I\partial F/\partial\mathbf z = J_f - I with Jf=∂f/∂zJ_f = \partial f/\partial\mathbf z at z⋆\mathbf z^\star. Invertibility of I−JfI - J_f is exactly the theorem's condition:

  3. u⊤(I−Jf)=g⊤,∂ℓ∂θ=u⊤∂f∂θ\mathbf u^\top(I - J_f) = \mathbf g^\top,\qquad\frac{\partial\ell}{\partial\boldsymbol\theta} = \mathbf u^\top\frac{\partial f}{\partial\boldsymbol\theta}

    Training needs a VJP, not this matrix. With g=∂ℓ/∂z⋆\mathbf g = \partial\ell/\partial\mathbf z^\star, define the adjoint u\mathbf u by one linear solve; the parameter gradient is then one ordinary VJP through a single evaluation of ff:

  4. u=∑k=0∞(Jf⊤)kgwhen ρ(Jf)<1\mathbf u = \sum_{k=0}^{\infty}\big(J_f^\top\big)^k\mathbf g\qquad\text{when }\rho(J_f) < 1

    Rewrite the solve as a fixed point, u=Jf⊤u+g\mathbf u = J_f^\top\mathbf u + \mathbf g, and iterate. Each step is one VJP with ff at z⋆\mathbf z^\star, and the iteration converges from any start if and only if the spectral radius ρ(Jf)<1\rho(J_f) < 1. Unrolled, it is the Neumann series (I−Jf)−1=∑kJfk(I - J_f)^{-1} = \sum_kJ_f^k. This is the recurrent backpropagation of Almeida (1987) and Pineda (1987). Memory: z⋆\mathbf z^\star and one evaluation of ff, whatever the number of forward iterations.

Predict before you look

A scalar fixed point z=0.9z+θz = 0.9z + \theta has z⋆=10θz^\star = 10\theta, so dz⋆/dθ=1/(1−0.9)=10dz^\star/d\theta = 1/(1 - 0.9) = 10. 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

∑k=090.9k=(1−0.910)/(1−0.9)≈6.51\sum_{k=0}^{9}0.9^k = (1 - 0.9^{10})/(1 - 0.9)\approx6.51: about 35% too small. Truncation biases the gradient toward zero, and the slower the contraction (ρ(Jf)\rho(J_f) near 1), the longer the unroll must be. The implicit gradient gets 1010 from the solution alone.

Deep equilibrium models

Stack the same layer ff forever, feeding the input x\mathbf x to every copy: zk+1=f(zk,x;θ)\mathbf z_{k+1} = f(\mathbf z_k,\mathbf x;\boldsymbol\theta). If the iteration settles, the output of this infinitely deep weight-tied network is a fixed point z⋆=f(z⋆,x;θ)\mathbf z^\star = f(\mathbf z^\star,\mathbf x;\boldsymbol\theta). A deep equilibrium model (DEQ; Bai, Kolter & Koltun, 2019) computes z⋆\mathbf z^\star directly with a root-finder on f(z)−zf(\mathbf z) - \mathbf z, 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 ff 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).

python
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 z

The pattern of the "Deep Implicit Layers" tutorial (Kolter, Duvenaud & Johnson, NeurIPS 2020), simplified. The hook swaps the incoming gradient g\mathbf g for the adjoint u\mathbf u, and ordinary backpropagation through the one tracked call of ff then delivers u⊤∂f/∂θ\mathbf u^\top\partial f/\partial\boldsymbol\theta. 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:

DerivationWhy the parametrization works
  1. z⊤(I−W)z=m∥z∥2+∥Az∥2+z⊤(B⊤−B)z=m∥z∥2+∥Az∥2 ≥ m∥z∥2\mathbf z^\top(I - W)\mathbf z = m\norm{\mathbf z}^2 + \norm{A\mathbf z}^2 + \mathbf z^\top(B^\top - B)\mathbf z = m\norm{\mathbf z}^2 + \norm{A\mathbf z}^2\ \ge\ m\norm{\mathbf z}^2

    A skew-symmetric matrix has a vanishing quadratic form, z⊤(B⊤−B)z=0\mathbf z^\top(B^\top - B)\mathbf z = 0, so for every z\mathbf z:

  2. So any unconstrained AA and BB give a strongly monotone I−WI - W: gradient descent on AA and BB can never leave the safe set. Conversely, split I−W−mII - W - mI into its symmetric part, positive semidefinite and hence of the form A⊤AA^\top A, and its skew part B⊤−BB^\top - B.

The second remedy acts on ordinary DEQs. During training, ρ(Jf)\rho(J_f) at the equilibrium tends to creep toward 11, so both solves need ever more iterations and can fail. Bai, Koltun & Kolter (2021) add the penalty γ ∥ϵ⊤Jf(z⋆)∥2/d\gamma\,\norm{\boldsymbol\epsilon^\top J_f(\mathbf z^\star)}^2/d with ϵ∼N(0,I)\boldsymbol\epsilon\sim\mathcal N(\mathbf 0, I), an unbiased one-sample (Hutchinson) estimate of ∥Jf∥F2/d\norm{J_f}_F^2/d that costs one extra VJP. Since ρ(Jf)≤∥Jf∥2≤∥Jf∥F\rho(J_f)\le\norm{J_f}_2\le\norm{J_f}_F, 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.

Check your understanding

A DEQ's forward solver takes 40 iterations of ff 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?

  1. It is about 70 layers' worth, since both solves must be stored
  2. It is about 40 layers' worth, the same as unrolling
  3. It is about one evaluation of ff plus z⋆\mathbf z^\star, independent of both iteration counts
  4. It is zero, since the gradient is computed analytically
Show the answer

C. It is about one evaluation of ff plus z⋆\mathbf z^\star, independent of both iteration counts

Only z⋆\mathbf z^\star and the graph of one call of ff at z⋆\mathbf z^\star 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, z⋆(θ)=arg min⁡zg(z,θ)\mathbf z^\star(\boldsymbol\theta) = \argmin_{\mathbf z}g(\mathbf z,\boldsymbol\theta). Without constraints, z⋆\mathbf z^\star is a root of F=∇zgF = \nabla_{\mathbf z}g, and the theorem applies with ∂F/∂z=∇zz2g\partial F/\partial\mathbf z = \nabla^2_{\mathbf z\mathbf z}g, the inner Hessian, invertible when gg is strongly convex near the solution:

∂z⋆∂θ=−(∇zz2g)−1∇zθ2g,∂ℓ∂θ=−(∇θz2g) u,∇zz2g  u=∂ℓ∂z⋆\frac{\partial\mathbf z^\star}{\partial\boldsymbol\theta} = -\big(\nabla^2_{\mathbf z\mathbf z}g\big)^{-1}\nabla^2_{\mathbf z\boldsymbol\theta}g,\qquad\frac{\partial\ell}{\partial\boldsymbol\theta} = -\big(\nabla^2_{\boldsymbol\theta\mathbf z}g\big)\,\mathbf u,\quad\nabla^2_{\mathbf z\mathbf z}g\;\mathbf u = \frac{\partial\ell}{\partial\mathbf z^\star}
The backward pass is one Hessian solve, done with conjugate gradients and Hessian–vector products, never by forming the Hessian.
Check your understanding

A ridge-regression layer returns z⋆(λ)=arg min⁡z∥Xz−y∥2+λ∥z∥2\mathbf z^\star(\lambda) = \argmin_{\mathbf z}\norm{X\mathbf z - \mathbf y}^2 + \lambda\norm{\mathbf z}^2. Its optimality condition is 2X⊤(Xz−y)+2λz=02X^\top(X\mathbf z - \mathbf y) + 2\lambda\mathbf z = \mathbf 0. What is dz⋆/dλd\mathbf z^\star/d\lambda?

  1. −z⋆/λ-\mathbf z^\star/\lambda
  2. (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top\mathbf y
  3. −2λ z⋆-2\lambda\,\mathbf z^\star
  4. −(X⊤X+λI)−1z⋆-(X^\top X + \lambda I)^{-1}\mathbf z^\star
Show the answer

D. −(X⊤X+λI)−1z⋆-(X^\top X + \lambda I)^{-1}\mathbf z^\star

Here ∂F/∂z=2(X⊤X+λI)\partial F/\partial\mathbf z = 2(X^\top X + \lambda I) and ∂F/∂λ=2z\partial F/\partial\lambda = 2\mathbf z, so dz⋆/dλ=−(X⊤X+λI)−1z⋆d\mathbf z^\star/d\lambda = -(X^\top X + \lambda I)^{-1}\mathbf z^\star. Check in one dimension with X⊤X=4X^\top X = 4, X⊤y=8X^\top\mathbf y = 8, λ=0\lambda = 0: z⋆=8/(4+λ)z^\star = 8/(4 + \lambda) has derivative −0.5=−z⋆/4-0.5 = -z^\star/4. Shrinkage increases with λ\lambda, as it should.

Constraints. For min⁡zg(z,θ)\min_{\mathbf z}g(\mathbf z,\boldsymbol\theta) subject to Az=bA\mathbf z = \mathbf b and Gz≤hG\mathbf z\le\mathbf h, the solution and its multipliers satisfy the KKT conditions: stationarity, primal feasibility and complementary slackness λi(Gz−h)i=0\lambda_i(G\mathbf z - \mathbf h)_i = 0. These form a square system of equations in (z⋆,ν⋆,λ⋆)(\mathbf z^\star,\boldsymbol\nu^\star,\boldsymbol\lambda^\star), 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

PropertyUnrolled: backprop through KK solver stepsImplicit: theorem at the solution
Differentiatesthe KK-step procedure, including its initialization and step sizesthe exact solution z⋆(θ)\mathbf z^\star(\boldsymbol\theta)
MemoryO(K)\order{K} stored iterates (or recomputation)z⋆\mathbf z^\star and one step, independent of KK
Backward computeKK VJPsa linear solve: a few to tens of VJPs or Hessian–vector products
Requirementsnone beyond differentiable stepsconvergence, and an invertible ∂F/∂z\partial F/\partial\mathbf z at the solution
Typical failuretruncation bias when KK is smallwrong gradient when the forward solve has not converged or the solution is not unique
The two agree in the limit: unrolling KK steps of a contraction around z⋆\mathbf z^\star is a truncated Neumann series of the implicit formula.

Bilevel problems. Hyperparameter optimization is an optimization layer in disguise: the inner problem fits the weights, w⋆(λ)=arg min⁡wLtrain(w,λ)\mathbf w^\star(\boldsymbol\lambda) = \argmin_{\mathbf w}\loss_{\text{train}}(\mathbf w,\boldsymbol\lambda), and the outer problem tunes λ\boldsymbol\lambda on validation data. The hypergradient is

dLvaldλ=∂Lval∂λ−∂Lval∂w(∇w2Ltrain)−1∇wλ2Ltrain∣w=w⋆\frac{d\loss_{\text{val}}}{d\boldsymbol\lambda} = \frac{\partial\loss_{\text{val}}}{\partial\boldsymbol\lambda} - \frac{\partial\loss_{\text{val}}}{\partial\mathbf w}\big(\nabla^2_{\mathbf w}\loss_{\text{train}}\big)^{-1}\nabla^2_{\mathbf w\boldsymbol\lambda}\loss_{\text{train}}\Big|_{\mathbf w = \mathbf w^\star}
Lorraine, Vicol & Duvenaud (2020) approximate the inverse Hessian with a truncated Neumann series H−1≈α∑j<i(I−αH)jH^{-1}\approx\alpha\sum_{j<i}(I - \alpha H)^j, equivalent to unrolling ii steps around w⋆\mathbf w^\star, and tuned millions of hyperparameters, such as every weight of a data-augmentation network, at a few times the cost of ordinary training.

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 ϕ⋆=arg min⁡ϕLtask(ϕ)+λ2∥ϕ−θ∥2\boldsymbol\phi^\star = \argmin_{\boldsymbol\phi}\loss_{\text{task}}(\boldsymbol\phi) + \tfrac\lambda2\norm{\boldsymbol\phi - \boldsymbol\theta}^2, whose meta-gradient Jacobian ∂ϕ⋆/∂θ=(I+∇2Ltask/λ)−1\partial\boldsymbol\phi^\star/\partial\boldsymbol\theta = (I + \nabla^2\loss_{\text{task}}/\lambda)^{-1} is applied with conjugate gradients, with memory independent of the number of inner steps.

Key takeaways

  • The implicit function theorem gives ∂z⋆/∂θ=−(∂F/∂z)−1∂F/∂θ\partial\mathbf z^\star/\partial\boldsymbol\theta = -(\partial F/\partial\mathbf z)^{-1}\partial F/\partial\boldsymbol\theta; for a fixed point, the VJP needs one linear solve u=Jf⊤u+g\mathbf u = J_f^\top\mathbf u + \mathbf g and then a single VJP through ff, 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.

Question 1

For a fixed point z⋆=f(z⋆;θ)\mathbf z^\star = f(\mathbf z^\star;\boldsymbol\theta) with Jf=∂f/∂zJ_f = \partial f/\partial\mathbf z at z⋆\mathbf z^\star, which is ∂z⋆/∂θ\partial\mathbf z^\star/\partial\boldsymbol\theta?

  1. ∂f/∂θ\partial f/\partial\boldsymbol\theta, since z⋆\mathbf z^\star is just ff evaluated once more
  2. (I−Jf)−1 ∂f/∂θ(I - J_f)^{-1}\,\partial f/\partial\boldsymbol\theta
  3. (I+Jf) ∂f/∂θ(I + J_f)\,\partial f/\partial\boldsymbol\theta
  4. Jf−1 ∂f/∂θJ_f^{-1}\,\partial f/\partial\boldsymbol\theta
Show the answer

B. (I−Jf)−1 ∂f/∂θ(I - J_f)^{-1}\,\partial f/\partial\boldsymbol\theta

Differentiate z⋆=f(z⋆;θ)\mathbf z^\star = f(\mathbf z^\star;\boldsymbol\theta): dz⋆=Jf dz⋆+(∂f/∂θ) dθd\mathbf z^\star = J_f\,d\mathbf z^\star + (\partial f/\partial\boldsymbol\theta)\,d\boldsymbol\theta, so (I−Jf) dz⋆=(∂f/∂θ) dθ(I - J_f)\,d\mathbf z^\star = (\partial f/\partial\boldsymbol\theta)\,d\boldsymbol\theta. The inverse sums the effect of the change through every pass of the loop: ∑kJfk\sum_kJ_f^k.

Question 2

What does the parametrization W=(1−m)I−A⊤A+B−B⊤W = (1 - m)I - A^\top A + B - B^\top buy a monotone operator DEQ?

  1. For every AA, BB the matrix I−WI - W is strongly monotone, so (with a proximal activation) the equilibrium exists and is unique throughout training
  2. It makes WW orthogonal, so the forward pass preserves norms
  3. It makes the Jacobian of ff zero, so no backward solve is needed
  4. It guarantees that naive fixed-point iteration converges for any step size
Show the answer

A. For every AA, BB the matrix I−WI - W is strongly monotone, so (with a proximal activation) the equilibrium exists and is unique throughout training

z⊤(I−W)z=m∥z∥2+∥Az∥2\mathbf z^\top(I - W)\mathbf z = m\norm{\mathbf z}^2 + \norm{A\mathbf z}^2, so strong monotonicity holds by construction, for any values the optimizer gives AA and BB. Existence and uniqueness follow from monotone operator theory.

Question 3

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?

  1. It gives the same answer as the implicit hypergradient, only more cheaply
  2. It is unbiased but has high variance
  3. It is biased toward step sizes that are too small, because a short horizon rewards greedy, low-noise progress
  4. 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.

Question 4

How does an optimization layer such as OptNet compute gradients with respect to the problem's parameters?

  1. By finite differences on the solver's output
  2. By differentiating the KKT optimality conditions at the solution and solving the resulting linear system, reusing the solver's factorization
  3. By backpropagating through every iteration of the interior-point solver
  4. 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 (z⋆,ν⋆,λ⋆)(\mathbf z^\star,\boldsymbol\nu^\star,\boldsymbol\lambda^\star) 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).