Row-wise Residuals (vmap_rows)
Simulated-path collocation models frequently evaluate a low-dimensional residual map independently across \(m\) states generated by an upstream sequential scan:
where \(z_s(\theta)\) denotes row-mapped inputs (in_axes != None) and \(w(\theta)\) denotes shared inputs (in_axes=None).
Linearization and chain-rule contractions
vmap_rows(f=None, *, in_axes=None, chunk_size=None) wraps jax.experimental.hijax.HiPrim with custom jvp, lin, transpose, and nested vmap rules:
- During primal evaluation, direct
jax.jvporjax.jacfwdcalls, and geodesic second directional derivatives \(D^2 r(\theta)[d, d]\),vmap_rowsexecutesjax.vmap(g)directly without materializing row Jacobians. - Under
jax.linearize(used during dense Jacobian assembly and implicit differentiation),vmap_rowsevaluates each row's derivatives \(G_{z,s} = D_z g(z_s, w)\) and \(G_{w,s} = D_w g(z_s, w)\) once via per-rowjax.jacrevand stores those row Jacobians as linearization state instead of the full batched forward tape ofg. Note that \(G_{z,s}\) is compact whenever each mapped state \(z_s\) is low-dimensional, whereas \(G_{w,s}\) has column dimension matching any differentiated shared leaf \(w\).
Combined with JAX's linearization of the upstream path \(z_s(\theta)\), tangent pushforwards and cotangent pullbacks satisfy
For scalar row outputs, dense Jacobian assembly evaluates jacrev(g) once per row (\(m\) row pullbacks total) rather than re-running g across \(m\) cotangents or \(n\) tangents. When per-row reverse-mode intermediates are large, pass chunk_size=k so the per-row jacrev pass runs via jax.lax.map(..., batch_size=k). Pair this with jacobian_chunk=c on LevenbergMarquardt or Newton to bound peak memory along the upstream scan.
Mixed-mode accumulation in overdetermined systems
Under jacobian_mode="fwd" (\(n \le m d_r\), where \(d_r\) is the output size of a single row), dense Jacobian assembly still traces through jax.linearize, producing mixed-mode differentiation. Upstream state paths push \(n\) tangents forward, while each row \(g(z_s, w) \in \mathbb{R}^{d_r}\) preaccumulates its local Jacobian in reverse mode via jacrev and contracts it with incoming tangents. Because both plain forward mode and vmap_rows visit all \(m\) rows, per-row derivative work scales with \(d_r\) rather than \(n\), so vmap_rows remains faster whenever \(d_r < n\) even when total residuals \(m d_r \gg n\).
Example
import jax
import jax.numpy as jnp
from flax import nnx
from nlls_gram import LevenbergMarquardt, vmap_rows
@vmap_rows(in_axes=(0, 0, None, None, None, None))
def euler_residual(X, u, next_shocks, weights, policy, p):
X_next = u + (1.0 - p["delta"]) * X[None] + next_shocks
u_next = jax.vmap(policy)(X_next)
return p["gamma"] * u - p["beta"] * (weights @ (price(X_next, p) + u_next))
def residual(theta, args, p):
policy = nnx.merge(args.graphdef, theta)
X, u = simulate(args.X_0, args.shocks, policy, p)
r = euler_residual(X, u, args.next_shocks, args.weights, policy, p)
return r / jnp.sqrt(r.size)
solver = LevenbergMarquardt(residual)
Contract
- Row
sdepends only on slicesof mapped inputs (in_axes != None) and shared unmapped inputs (in_axes=None). - Pass differentiated quantities as positional arguments (pytrees, dicts, tuples, Flax NNX modules, and integer or boolean arrays are supported).
- All outputs are stacked along leading axis
0. Arbitrary output pytrees (including(residual_row, aux_row)tuples with non-floating leaves) compose withjax.jit,jax.lax.scan, and outerjax.vmap.
References
- Griewank and Walther (2008) for local Jacobian preaccumulation in automatic differentiation. Full citation in References.