Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion autogalaxy/operate/_zero_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@lru_cache(maxsize=1)
def zero_solver_type():
"""Return a solver class whose Newton loop accepts JAX 0.11 callables.
"""Return a solver class compatible with JAX transformations.

jax-zero-contour 2.0 carries the function supplied by ``custom_root`` in
its ``while_loop`` state. JAX 0.11 can supply a plain functools.partial,
Expand All @@ -16,10 +16,39 @@ def zero_solver_type():
solver type identity. The upstream class and numerical algorithm remain
untouched.
"""
import jax
import jax.numpy as jnp
from jax.tree_util import Partial
from jax_zero_contour import ZeroSolver

class CompatibleZeroSolver(ZeroSolver):
@Partial(jax.vmap, in_axes=(None, None, 0, None))
def newton(self, f, init_guess, factor):
# custom_root traces the tangent RHS with the shape of its guess
# on JAX 0.12. Embed the scalar contour equation in R² so the
# traced and actual RHS shapes agree under batched transposition.
def residual(point):
value = f(point)
return jnp.stack((value, jnp.zeros_like(value)))

def solve(equations, guess):
return self.step_parallel_tol(
lambda point: equations(point)[0], guess, factor=factor
)

def tangent_solve(linearized, rhs):
# The second equation is identically zero. Use the upstream
# minimum-norm solution along the first equation's normal.
jacobian = (
jax.jacfwd if self.forward_mode_differentiation else jax.jacrev
)
normal = jacobian(lambda point: linearized(point)[0])(rhs)
return rhs[0] * normal / jnp.sum(normal**2)

return jax.lax.custom_root(
residual, init_guess, solve, tangent_solve, has_aux=True
)

def step_parallel_tol(self, f, init_guess, factor=1):
return super().step_parallel_tol(
Partial(f), init_guess, factor=factor
Expand Down
Loading