Which side of the Jacobian do you multiply on: JVP, VJP or the adjoint?
GeoStack · background · numerical methods · derivative products
Abstract¶
An EMIT overpass shows a methane enhancement map; we want the emission field that made it. Every derivative tool in JAX, from jvp to grad to the adjoint, is a product with one matrix, the Jacobian of the transport model. They differ only in which side you multiply on, and what that costs. The page expands two chapters of the numerical-methods lens: 1 · Calculus, autodiff, changes of variables, where JVP and VJP apply the Jacobian without forming it, and 26 · Adjoints, calculus of variations, where the gradient costs one backward run.
Every derivative is an operator¶
Each box is one computation with a fixed signature. Read down: each box comes from the one above by a single move, named in the line between them. The shape and unit of every row are the lesson; watch where units flip to their inverse.
· Forward model: emissions to enhancement map
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | emission rate per cell | (cell,) | kg s⁻¹ | ||
| External input | 10 m wind | () | m s⁻¹ | ||
| Parameter | none | — | nothing is fitted | ||
| Constant | , | upwind difference, Laplacian | (cell, cell) | — | stencils |
| , , | time step, cell size, cell area | () | s, m, m² | ||
| eddy diffusivity | () | m² s⁻¹ | |||
| observation step | (pixel, cell) | ppm·m (kg m⁻²)⁻¹ | |||
| Estimated | one transport step | (cell, cell) | — | eq. 1 | |
| column trajectory | (step, cell) | kg m⁻² | eq. 2–3 | ||
| Output | predicted enhancement | (pixel,) | ppm·m | eq. 4 |
To the next box: differentiate with respect to , wind held fixed.
· Jacobian: the object
The sum runs over the steps .
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | emission rate per cell | (cell,) | kg s⁻¹ | linearisation point | |
| External input | 10 m wind | () | m s⁻¹ | ||
| Constant | , , | … | as in box | ||
| Estimated | one transport step | (cell, cell) | — | box , eq. 1 | |
| Output | Jacobian of | (pixel, cell) | ppm·m (kg s⁻¹)⁻¹ | eq. 1 |
To the next box: multiply on the right by a tangent .
JVP · Push a tangent forward (tangent linear model)
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | emission rate per cell | (cell,) | kg s⁻¹ | linearisation point | |
| tangent | (cell,) | kg s⁻¹ | |||
| External input | 10 m wind | () | m s⁻¹ | ||
| Constant | , , , | … | as in box | ||
| Estimated | tangent columns | (cell,) | kg m⁻² | eq. 1–2, not stored | |
| Output | tangent of the map | (pixel,) | ppm·m | eq. 3 |
To the next box: transpose, and multiply on the left by a cotangent .
VJP · Pull a cotangent back (adjoint model)
The sum runs over .
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | emission rate per cell | (cell,) | kg s⁻¹ | forward sweep stores | |
| cotangent | (pixel,) | (ppm·m)⁻¹ | |||
| External input | 10 m wind | () | m s⁻¹ | ||
| Constant | , , , | … | as in box | ||
| Estimated | adjoint columns | (cell,) | (kg m⁻²)⁻¹ | eq. 1–2, backward in | |
| Output | pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3 |
To the next box: seed with .
· Gradient of the cost (the adjoint method)
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | emission rate per cell | (cell,) | kg s⁻¹ | ||
| observed enhancement | (pixel,) | ppm·m | |||
| External input | 10 m wind | () | m s⁻¹ | ||
| Parameter | none | — | prior and noise are fixed | ||
| Constant | observation-error covariance | (pixel, pixel) | (ppm·m)² | ||
| prior covariance | (cell, cell) | (kg s⁻¹)² | |||
| prior mean | (cell,) | kg s⁻¹ | |||
| Estimated | predicted enhancement | (pixel,) | ppm·m | eq. 1, box | |
| cotangent | (pixel,) | (ppm·m)⁻¹ | eq. 2, the seed | ||
| pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3, box VJP; applied, never formed | ||
| Output | gradient of the cost | (cell,) | (kg s⁻¹)⁻¹ | eq. 4 |
To the next box: differentiate the gradient along a direction .
HVP · Curvature along a direction
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | , | linearisation point, direction | (cell,), (cell,) | kg s⁻¹ | |
| External input | 10 m wind | () | m s⁻¹ | ||
| Constant | , | … | as in box | ||
| Estimated | tangent of the map | (pixel,) | ppm·m | eq. 1, one JVP | |
| cotangent | (pixel,) | (ppm·m)⁻¹ | eq. 2 | ||
| pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3, one VJP | ||
| Output | Hessian-vector product | (cell,) | (kg s⁻¹)⁻¹ | eq. 4 |
The kinds on this page¶
Table 1:The kinds of object on this page, with their dimensions.
| Kind | Dimensions | Here |
|---|---|---|
| scalar | () | wind , cost , total rate |
| field | (cell,) = (row, col) | emissions , columns , their tangents and cotangents |
| trajectory | (step, cell) | , what a VJP stores |
| vector | (pixel,) | clear-sky observations , , , |
| matrix / operator | (pixel, cell), (cell, cell) | , , , covariances |
The problem: where did this methane come from?¶
The retrieval example ended with an L2 product: an enhancement map in ppm·m from the matched filter, one value per 60 m EMIT pixel. A mass-balance estimate, as in the quantification example, gives one number for the whole plume. A source map asks for more: an emission rate in every grid cell, chosen so that wind and diffusion carry it into the map we see. That is an inverse problem, and every way of solving it runs on derivatives of a transport model.
The data are one enhancement map at the overpass time, with a block of cloud masked out. The unknown is an emission field on the same grid. The model releases every time step for 15 minutes, advects it with the 10 m wind and diffuses it. The truth used to make the synthetic map is one facility emitting 2.0 kg s⁻¹, the rate the quantification example found. The grid, noise and wind are real-scale; the scene is synthetic so that every number can be checked. It is a different synthetic scene from the one on the two plume pages, which share only the 60 m pixel, the 750 ppm·m threshold, and the 2.0 kg s⁻¹ rate with it, so its plume, noise and mass numbers differ from theirs.
indexes grid cells , ; indexes clear pixels in , ; indexes model steps. Coordinates are , with the wind along . A tangent (a small change of an input) is marked ; a cotangent (a weighting on an output, also called an adjoint variable) is marked .
Table 3:Notation.
| Symbol | Role | Shape | Unit |
|---|---|---|---|
| in · emission rate per cell, the unknown | (cell,) | kg s⁻¹ | |
| , | observation · enhancement at clear pixels; model prediction | (pixel,) | ppm·m |
| u · 10 m wind speed, given | m s⁻¹ | ||
| est · column mass enhancement at step | (cell,) | kg m⁻² | |
| , | const · upwind difference along ; five-point Laplacian stencil; zero inflow | — | |
| est · one transport step, linear in , depends on | — | ||
| const · keep clear pixels and divide by | ppm·m (kg m⁻²)⁻¹ | ||
| forward model , box | kg s⁻¹ → ppm·m | ||
| Jacobian of with respect to | ppm·m (kg s⁻¹)⁻¹ | ||
| , , | tangents: input direction and its images | shapes of , , | units of , , |
| , , | cotangents: output weighting and its pull-backs | shapes of , , | inverse units |
| const · observation-error covariance | (ppm·m)² | ||
| , | const · prior mean and covariance of | , | kg s⁻¹, (kg s⁻¹)² |
| cost: negative log posterior of | — | ||
| const · kg m⁻² per ppm·m, from Pa, K | kg m⁻² (ppm·m)⁻¹ |
Step 0 · The forward model is a loop of one linear step¶
Each step moves the column field half a cell downwind, smooths it slightly, and adds this step’s emissions . After steps the observation operator keeps the clear pixels and converts kg m⁻² to ppm·m. The model is linear in for a fixed wind, and nonlinear in the wind, because enters every one of the 90 steps.
# Shapes: D_x = 4,096 cells (row, col = 64 × 64); D_y = 3,900 clear pixels; T = 90 steps
# Units: [x] = kg s⁻¹ per cell; [c] = kg m⁻²; [α] = ppm·m
# Setup: the upper-case constants are the running-example table's; clear holds the
# D_y flat indices of the pixels outside the cloud block; y_obs is the observed
# map (D_y,) and mu_x the prior mean (D_x,)
# user
# shift by k cells along an axis; the k cells that enter are zero (clean-air inflow)
def shift(c, k, axis):
rolled = jnp.roll(c, k, axis) # (row, col) → (row, col) [c]
index = jnp.arange(c.shape[axis])
inflow = index < k if k > 0 else index >= c.shape[axis] + k
return jnp.where(jnp.expand_dims(inflow, 1 - axis), 0.0, rolled)
# user
# F(U) c + (Δt/w) x, with F(U) = I − (UΔt/Δs) D₁ + (K_h Δt/Δs²) L
def step(c, x, wind):
# D₁ c: upwind difference along s₁, zero (clean-air) inflow
upwind = c - shift(c, 1, 1) # (row, col) → (row, col) [c]
# L c: five-point Laplacian stencil
laplacian = (shift(c, 1, 0) + shift(c, -1, 0) # (row, col) → (row, col) [c]
+ shift(c, 1, 1) + shift(c, -1, 1) - 4 * c)
# c_t = c_{t−1} − (UΔt/Δs) D₁c + (K_hΔt/Δs²) Lc + (Δt/w) x
return (c - (wind * DT / DS) * upwind # → (row, col) [c]
+ (DIFFUSIVITY * DT / DS**2) * laplacian
+ (DT / PIXEL_AREA) * x)
# user
def forward(x, wind=WIND):
x_grid = x.reshape(N_ROW, N_COL) # (D_x,) → (row, col) [x]
# today (jax 0.12)
# c_t = F(U) c_{t−1} + (Δt/w) x, t = 1 … T, c_0 = 0
c_final, _ = jax.lax.scan( # → (row, col) [c]
lambda c, _: (step(c, x_grid, wind), None),
jnp.zeros((N_ROW, N_COL)), None, length=N_STEP,
)
# ŷ = P c_T: clear pixels, divided by κ
return (c_final / KAPPA).reshape(-1)[clear] # → (D_y,) [α]Program 1:The forward model: one linear transport step, scanned over the T = 90 steps, then the observation step.
Step 1 · The Jacobian is the object, not the computation¶
Entry answers one question: if cell emitted 1 kg s⁻¹ more, how many ppm·m more would pixel show at the overpass? Column is the footprint of cell on the map. Row is the set of cells that could have reached pixel . For this model the Jacobian has a closed form, and it does not depend on , because is linear in . It depends on the wind.
Table 4:The cheapest route to each derivative quantity, in passes of the model.
| What you need | Shape | Cheapest route | Passes |
|---|---|---|---|
| Footprint of one facility | (pixel,) | one JVP with | 1 |
| Sensitivity of the map to the wind | (pixel,) | one JVP in | 1 |
| Cells that could reach one pixel | (cell,) | one VJP with | 1 |
| Gradient of the scalar cost | (cell,) | one VJP | 1 |
| Curvature along one direction | (cell,) | one JVP and one VJP | 2 |
| The whole Jacobian | (pixel, cell) | products | 3 900 |
Step 2 · JVP: push an input direction forward¶
A JVP answers “if I nudge the inputs along , how does the output move?” It runs alongside the forward model: every intermediate value carries its own tangent , and both march forward together. Nothing is stored, so memory is about twice a forward run, and the cost is about one forward run no matter how many outputs there are. Data assimilation calls this recursion the tangent linear model.
Use · jax.jvp(forward, (x,), (dx,)), one call per input direction.
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | primal | (cell,) | kg s⁻¹ | where to linearise | |
| tangent | (cell,) | kg s⁻¹ | same shape and unit as the primal | ||
| External input | wind | () | m s⁻¹ | closed over: a default argument of forward | |
| Parameter | none | — | nothing is trained | ||
| Constant | , , , | … | baked into the traced function | ||
| Estimated | tangent columns | (cell,) | kg m⁻² | eq. 1–2, carried with , then dropped | |
| Output | , | primal output, tangent of the map | (pixel,), (pixel,) | ppm·m | the primal output and eq. 3, together |
Swap jax.jvp for jax.linearize to pay for the forward pass once and reuse the linear map for many tangents.
# Shapes: D_x = 4,096; D_y = 3,900
# Units: [x] = kg s⁻¹; [α] = ppm·m; [U] = m s⁻¹
# δx = e_n: one extra kg s⁻¹ at the facility cell (32, 8)
dx = jnp.zeros_like(x).at[32 * N_COL + 8].set(1.0) # → (D_x,) [x]
# today (jax 0.12)
# ŷ = h(x), δy = J_h δx: one forward pass carrying the tangent
y_hat, dy = jax.jvp(forward, (x,), (dx,)) # → (D_y,), (D_y,) [α]
# today (jax 0.12)
# δx ↦ J_h δx, linearised once, reused for many tangents
y_hat, jvp_fn = jax.linearize(forward, x) # → (D_y,), fn (D_x,) → (D_y,)
# today (jax 0.12)
# ∂ŷ/∂U: one scalar input, so one JVP gives the whole column
y_hat, dy_wind = jax.jvp( # () → (D_y,) [α] [U]⁻¹
lambda u: forward(x, u), (WIND,), (1.0,)
)Program 2:Three JVPs: the facility’s footprint, a linearised map reused for many tangents, and the map’s sensitivity to the wind.
Step 3 · VJP: pull an output weighting back¶
A VJP answers the reverse question: “given a weighting on the outputs, which inputs matter, and how much?” It runs the chain of step Jacobians from the observation end, transposed, back to the emissions. For that it needs every step’s linearisation point, so the forward pass runs first and stores the trajectory. The cost is about one forward run no matter how many inputs there are. Data assimilation calls the backward recursion the adjoint model; machine learning calls it backpropagation.
Use · jax.vjp(forward, x), one forward sweep, then one backward sweep per cotangent.
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | primal | (cell,) | kg s⁻¹ | forward sweep runs once and keeps the residuals | |
| cotangent | (pixel,) | (ppm·m)⁻¹ | passed to the returned vjp_fn; same shape as the output | ||
| External input | wind | () | m s⁻¹ | closed over; not differentiated: it is not a primal argument | |
| Parameter | none | — | nothing is trained | ||
| Constant | , , , | … | transposes are derived by JAX, never written by hand | ||
| Estimated | stored trajectory | (step, cell) | kg m⁻² | forward sweep; 90 × 4 096 floats here | |
| adjoint columns | (cell,) | (kg m⁻²)⁻¹ | eq. 1–2, backward in | ||
| Output | pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3; same shape as the input |
The returned vjp_fn can be called many times with different ; the forward sweep is paid once.
# Shapes: D_x = 4,096; D_y = 3,900; T = 90
# Units: [x] = kg s⁻¹; [α] = ppm·m
# today (jax 0.12)
# ŷ = h(x); store c_{0:T−1}; return y† ↦ J_hᵀ y†
y_hat, vjp_fn = jax.vjp(forward, x) # → (D_y,), fn (D_y,) → (D_x,)
# m: the clear-pixel index of pixel (32, 40)
pixel = jnp.searchsorted(clear, 32 * N_COL + 40) # → ()
# y† = e_m: unit weight on one pixel
y_adj = jnp.zeros_like(y_hat).at[pixel].set(1.0) # → (D_y,) [α]⁻¹
# x† = J_hᵀ y†: one backward pass
(x_adj,) = vjp_fn(y_adj) # (D_y,) → (D_x,) [x]⁻¹Program 3:One VJP: the forward sweep runs once, and one backward sweep returns the influence map of one pixel.
(cell,)
add every step
(step, cell)stored
: keep clear pixels, divide by
(pixel,)
seed with a cotangent
(pixel,)
(cell,)
, backward in ; sum over , times
(cell,)
The VJP of the forward model, with the shape and unit of every value. The forward sweep (plain boxes) adds the emissions every step and stores the column trajectory; the cotangent seeded on the map (shaded boxes) runs back through the transposed steps and comes out in the inverse unit of the emissions.
Step 4 · The gradient is one VJP, and the adjoint method is how it runs in time¶
To estimate we minimise a cost, a single number. Its gradient has entries but comes from one VJP, because the output is a scalar: seed the cotangent with the cost’s sensitivity to the predicted map and pull it back. The model below writes the cost before anything is assumed linear or Gaussian.
4 · Assumptions.
5 · Result (uses A1–A4).
where is the gradient, a column in emission space, in (kg s⁻¹)⁻¹. With A4 the product is the backward recursion of box VJP, seeded with . One forward run and one backward run give all 4 096 partial derivatives. That is the adjoint method. In Lagrangian form, is the multiplier that enforces A4 at step .
“Adjoint” names four related things, and papers switch between them without saying so.
Table 5:The four meanings of “adjoint”.
| Meaning | What it is | Here | In JAX |
|---|---|---|---|
| adjoint operator | the map with ; for a field operator, under an inner product | , , | jax.linear_transpose |
| adjoint model | the transposed tangent linear model, run backward in time | box VJP, eq. 1–2 | jax.vjp through lax.scan |
| adjoint variable | the cotangent of a state, a Lagrange multiplier of the model constraint | the carry of the backward scan | |
| adjoint method | a gradient from one forward and one backward run | box | jax.grad |
There is one more split. The discrete adjoint differentiates the code you ran, step by step; that is what jax.grad through lax.scan gives, and why it matches finite differences to rounding. The continuous adjoint derives an adjoint PDE or ODE first and then discretises it on its own, so its gradient is only consistent with the forward scheme as the step shrinks. In diffrax these are RecursiveCheckpointAdjoint and BacksolveAdjoint.
Use · jax.value_and_grad(cost)(x), once per optimiser iteration.
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | current emissions | (cell,) | kg s⁻¹ | the iterate | |
| observed map | (pixel,) | ppm·m | closed over by cost | ||
| External input | wind | () | m s⁻¹ | A5: given | |
| Parameter | none | — | , fixed; tuning them would be a loss one level up | ||
| Constant | , , | … | (ppm·m)², (kg s⁻¹)² | diagonal: ppm·m, kg s⁻¹, | |
| Estimated | , | prediction and seed | (pixel,) | ppm·m, (ppm·m)⁻¹ | eq. 1–2 |
| pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3, the adjoint run | ||
| Output | the cost value | () | — | for line searches | |
| gradient of the cost | (cell,) | (kg s⁻¹)⁻¹ | eq. 4 |
jax.grad requires a scalar output; for a vector output it is jax.vjp you want.
# Shapes: D_x = 4,096 (row, col = 64 × 64); D_y = 3,900; T = 90
# Units: [x] = kg s⁻¹; [c] = kg m⁻²; [α] = ppm·m
# user
# 𝒥(x) = ½ ‖y − h(x)‖²_{Σ_ε⁻¹} + ½ ‖x − μ_x‖²_{Σ_x⁻¹}
def cost(x, wind=WIND):
misfit = (y_obs - forward(x, wind)) / SIGMA_EPS # → (D_y,) —
departure = (x - mu_x) / SIGMA_X # → (D_x,) —
return 0.5 * misfit @ misfit + 0.5 * departure @ departure # → ()
# user
def gradient_by_adjoint(x, wind=WIND):
x_grid = x.reshape(N_ROW, N_COL) # (D_x,) → (row, col) [x]
def forward_body(c_prev, _):
# c_t = F(U) c_{t−1} + (Δt/w) x; emit c_{t−1} to store it
return step(c_prev, x_grid, wind), c_prev
# today (jax 0.12)
c_final, trajectory = jax.lax.scan( # → (row, col), (T, row, col)
forward_body, jnp.zeros((N_ROW, N_COL)), None, length=N_STEP
)
# ŷ = P c_T
y_hat = (c_final / KAPPA).reshape(-1)[clear] # → (D_y,) [α]
# y† = −Σ_ε⁻¹ (y − ŷ)
y_adj = -(y_obs - y_hat) / SIGMA_EPS**2 # → (D_y,) [α]⁻¹
# c†_T = Pᵀ y†
c_adj = (jnp.zeros(N_ROW * N_COL).at[clear].set(y_adj) / KAPPA
).reshape(N_ROW, N_COL) # → (row, col) [c]⁻¹
def backward_body(c_adj, c_prev):
# today (jax 0.12)
# c†_{t−1} = Fᵀ c†_t; (Δt/w) c†_t is this step's share of x†
_, step_vjp = jax.vjp(lambda c, q: step(c, q, wind), c_prev, x_grid)
c_adj_prev, x_adj_t = step_vjp(c_adj) # → (row, col), (row, col)
return c_adj_prev, x_adj_t
# today (jax 0.12)
# run t = T … 1
_, x_adj_steps = jax.lax.scan( # → (T, row, col) [x]⁻¹
backward_body, c_adj, trajectory, reverse=True
)
# x† = ∑_t (Δt/w) c†_t
x_adj = x_adj_steps.sum(0).reshape(-1) # → (D_x,) [x]⁻¹
# ∇𝒥 = x† + Σ_x⁻¹ (x − μ_x)
return x_adj + (x - mu_x) / SIGMA_X**2 # → (D_x,) [x]⁻¹
# today (jax 0.12)
# 𝒥(μ_x), ∇𝒥(μ_x): the same two sweeps, derived automatically
value, grad = jax.value_and_grad(cost)(mu_x) # → (), (D_x,)
grad_hand = gradient_by_adjoint(mu_x) # → (D_x,) # max rel. diff 7e-16Program 4:The adjoint method written by hand, two scans and one VJP per step, checked against jax.value_and_grad.
Step 5 · HVP: curvature without a Hessian¶
The gradient says which way is downhill; the Hessian says how far. With 4 096 unknowns the Hessian has 16.8 million entries, but Newton-type solvers and posterior uncertainty need only its products with vectors. An HVP is a derivative of the gradient along a direction, so it is a JVP of a VJP: about two gradients’ cost.
Use · jax.jvp(jax.grad(cost), (x,), (dx,)), once per conjugate-gradient iteration.
| Role | Symbol | Name | Shape | Unit | Note |
|---|---|---|---|---|---|
| Input | linearisation point | (cell,) | kg s⁻¹ | where the curvature is taken | |
| direction | (cell,) | kg s⁻¹ | the CG search direction | ||
| External input | wind | () | m s⁻¹ | given | |
| Parameter | none | — | nothing is trained | ||
| Constant | , | … | as in Step 4 | ||
| Estimated | , | tangent of the map, cotangent | (pixel,) | ppm·m, (ppm·m)⁻¹ | eq. 1–2 |
| pull-back onto emissions | (cell,) | (kg s⁻¹)⁻¹ | eq. 3 | ||
| Output | Hessian-vector product | (cell,) | (kg s⁻¹)⁻¹ | eq. 4; Hessian (kg s⁻¹)⁻² times direction kg s⁻¹ |
The eq. 1–4 stack is the Gauss–Newton form; the call named above is the exact form. They coincide here because is linear in .
The general Hessian of the cost has one more term, the curvature of weighted by the residual. Which instance applies depends on what you put in the control vector.
Linear in the control (this page)
Gauss–Newton is exact and the cost is quadratic, so one CG solve gives the posterior mean and the Laplace posterior is the true posterior.
Nonlinear control
Put in the control, or observe radiance through Beer–Lambert, and the last term appears, with , in (ppm·m)⁻¹, and the -th output of . Gauss–Newton drops it and stays positive definite; the exact HVP keeps it and can be indefinite far from the optimum.
# Shapes: D_x = 4,096; D_y = 3,900
# Units: [x] = kg s⁻¹; [α] = ppm·m
# user
def hvp(x, v):
# today (jax 0.12)
# ∇²𝒥 v = d/dε ∇𝒥(x + ε v): forward-over-reverse
return jax.jvp(jax.grad(cost), (x,), (v,))[1] # (D_x,), (D_x,) → (D_x,) [x]⁻¹
# user
def gauss_newton(x, v):
# today (jax 0.12)
# δy = J_h v
y_hat, dy = jax.jvp(forward, (x,), (v,)) # → (D_y,), (D_y,) [α]
# today (jax 0.12)
_, vjp_fn = jax.vjp(forward, x)
# x† = J_hᵀ Σ_ε⁻¹ δy
(x_adj,) = vjp_fn(dy / SIGMA_EPS**2) # (D_y,) → (D_x,) [x]⁻¹
# J_hᵀ Σ_ε⁻¹ J_h v + Σ_x⁻¹ v
return x_adj + v / SIGMA_X**2 # → (D_x,) [x]⁻¹
matvec = jax.jit(lambda v: hvp(mu_x, v))
# today (jax 0.12)
# ∇²𝒥 Δx = −∇𝒥(μ_x), x̂ = μ_x + Δx (one Newton step is exact: 𝒥 is quadratic)
increment, _ = jax.scipy.sparse.linalg.cg(matvec, -jax.grad(cost)(mu_x), tol=1e-6, maxiter=500) # → (D_x,)
x_hat = mu_x + increment # → (D_x,) [x]
# today (jax 0.12)
# z = Σ_x|y 1, 𝕍[Q | y] = 1ᵀ z
z, _ = jax.scipy.sparse.linalg.cg(matvec, jnp.ones_like(x_hat), tol=1e-6, maxiter=500) # → (D_x,)Program 5:The exact and the Gauss–Newton HVP, and the two conjugate-gradient solves for the posterior mean and the variance of the total.
Two tests every derivative must pass¶
A wrong adjoint still returns a vector of the right shape, so check it before trusting it. Both tests use the pairing that defines the transpose and cost a few model runs.
Dot-product test (is the VJP the transpose of the JVP?)
Random and . Both sides are ppm·m × (ppm·m)⁻¹ and kg s⁻¹ × (kg s⁻¹)⁻¹: dimensionless. Here 3.9 × 10⁻¹⁶.
Taylor test (is the gradient the derivative of the cost?)
Divided by , the remainder must settle to a constant, . Here 2.3073 × 10⁷ at .
# Shapes: D_x = 4,096; D_y = 3,900
# Units: [x] = kg s⁻¹; [α] = ppm·m
# δx, y†: random directions, one in each space
key_x, key_y = jax.random.split(jax.random.PRNGKey(1))
dx = jax.random.normal(key_x, x.shape) # → (D_x,) [x]
y_adj = jax.random.normal(key_y, y_obs.shape) # → (D_y,) [α]⁻¹
# today (jax 0.12)
# δy = J_h δx
_, dy = jax.jvp(forward, (x,), (dx,)) # (D_x,) → (D_y,) [α]
# today (jax 0.12)
_, vjp_fn = jax.vjp(forward, x)
# x† = J_hᵀ y†
(x_adj,) = vjp_fn(y_adj) # (D_y,) → (D_x,) [x]⁻¹
# |δyᵀ y† − δxᵀ x†| / |δyᵀ y†|
dot_gap = jnp.abs(dy @ y_adj - dx @ x_adj) / jnp.abs(dy @ y_adj) # → () 3.9e-16
# today (jax 0.12)
g = jax.grad(cost)(x) # → (D_x,) [x]⁻¹
for eps in (1e-1, 1e-2, 1e-3):
# [𝒥(x + εδx) − 𝒥(x) − ε δxᵀ∇𝒥] / ε² → ½ δxᵀ ∇²𝒥 δx
taylor = (cost(x + eps * dx) - cost(x) - eps * g @ dx) / eps**2 # → () 2.3073e7Program 6:The dot-product test and the Taylor test, run in float64.
Everything else in the grad space¶
The same objects go by different names in each community. This table is the crosswalk.
Table 6:One derivative object, four vocabularies, and its cost in model runs.
| Maths | JAX | Data assimilation | Machine learning | Cost |
|---|---|---|---|---|
jacfwd, jacrev | weighting function, sensitivity matrix | Jacobian | runs | |
jvp, linearize | tangent linear model | forward mode, pushforward | ≈ 1 run | |
vjp | adjoint model | reverse mode, backprop, pullback | ≈ 1 run + storage | |
grad, value_and_grad | adjoint method, 4D-Var gradient | gradient | ≈ 2 runs | |
jvp of grad | second-order adjoint | HVP | ≈ 4 runs | |
hessian | posterior precision at the optimum | Hessian | HVPs |
And the tools that change how a derivative is computed, not what it is:
Table 7:The tools that change how a derivative is computed.
| Tool | What it does | Use it for | Status |
|---|---|---|---|
jax.linearize | runs once and returns | many tangents at one point: ensemble perturbations, TLM validity checks | today (jax 0.12) |
jax.linear_transpose | transposes a function that is already linear, with no linearisation stored | observation operators, regridding, ; here it matched the VJP exactly | today (jax 0.12) |
jax.vmap of jvp or vjp | batches products; jacfwd and jacrev are exactly this | a handful of directions or pixels at once | today (jax 0.12) |
jax.custom_vjp, jax.custom_jvp | replaces JAX’s rule with yours | the implicit function theorem through an iterative solver, a hand-tuned legacy adjoint, a numerically stable gradient | today (jax 0.12) |
jax.checkpoint | recomputes forward values in the backward sweep instead of storing them | long assimilation windows and 3-D models | today (jax 0.12) |
jax.lax.stop_gradient | treats a value as a constant | plug-in estimates such as the matched filter’s background , | today (jax 0.12) |
lineax linear_solve | differentiates a solve by solving with the transpose | implicit diffusion steps, posterior covariance products | not pinned (lineax) |
diffrax RecursiveCheckpointAdjoint, BacksolveAdjoint | discrete adjoint with checkpointing; continuous adjoint ODE solved backward | continuous-time models in vardax | not pinned (diffrax) |
| finite differences, complex step | approximate a JVP from model runs | checking derivatives, never computing gradients in dimensions | — |
Worked numbers¶
Table 8:Every number on this page, by box: illustrative values from the source explainer’s run.
| Box | Quantity | Value | Unit |
|---|---|---|---|
| one forward run (CPU) | 0.58 | ms | |
| mass at overpass , released 1 800 | 1 799 | kg | |
| peak enhancement, facility cell | 15 530 | ppm·m | |
| size | 3 900 × 4 096 | — | |
| storage in float64; nonzero fraction | 128; 16 % | MB; — | |
build time, jacfwd / jacrev | 3.7 / 6.4 | s | |
| JVP | one call | 0.62 | ms |
| JVP | facility footprint, peak | 7 765 | ppm·m (kg s⁻¹)⁻¹ |
| JVP | footprint mass , expected 900 | 899.6 | s |
| JVP | wind sensitivity, min / max | −4 894 / 5 005 | ppm·m (m s⁻¹)⁻¹ |
| VJP | one call | 0.58 | ms |
| VJP | pixel (32, 40), cell (32, 8), by JVP and VJP | 2 696.4 | ppm·m (kg s⁻¹)⁻¹ |
| 33 796 | — | ||
| peak , facility cell | 31 978 | (kg s⁻¹)⁻¹ | |
hand adjoint vs jax.grad, max relative difference | 7 × 10⁻¹⁶ | — | |
| HVP | exact vs Gauss–Newton, relative difference | 1.1 × 10⁻¹⁶ | — |
| HVP | CG iterations to 10⁻³ / 10⁻⁶ | 127 / 322 | — |
| HVP | facility cell | 1.99 ± 0.05 | kg s⁻¹ |
| HVP | total , truth 2.0 | 2.09 ± 0.26 | kg s⁻¹ |
| HVP | , expected ≈ 1 950 | 24 | — |
| tests | dot-product gap | 3.9 × 10⁻¹⁶ | — |
| tests | Taylor remainder / | 2.3073 × 10⁷ | — |
Sources and status
From the artifact “JVP, VJP and the Adjoint”, headed “One Jacobian, five ways to use it” (updated 9 November 2026). It cites no GitHub issue or pull request. Its numbers are illustrative: they come from the explainer’s own run with JAX 0.12.0.1 on CPU in float64, nothing in this repository regenerates them, and the timings depend on the machine. The code blocks are pseudocode in the style-code-sketch form: each uses the names defined in the blocks above it and the constants of the running-example table. The blocks use plain jnp indexing so they run without einx. shift pads with zeros (clean-air inflow); the setup lines, not shown, define the constants in the running-example table, a cloud mask of 14 × 14 pixels, and noise from PRNGKey(0). The names marked not pinned were not checked against the lineax or diffrax repositories, and nothing on this page was re-run or checked against the submodule pins for it. The scene is synthetic and is not the scene of the retrieval and quantification examples. Sources: R. M. Errico (1997), What is an adjoint model?, Bulletin of the American Meteorological Society 78, 2577–2591; A. Griewank and A. Walther (2008), Evaluating Derivatives, 2nd ed., SIAM; P. Kidger’s DPhil thesis Kidger, 2021, on discrete and continuous adjoints; the JAX autodiff cookbook.
- Kidger, P. (2021). On Neural Differential Equations [Phdthesis]. University of Oxford.