From 0659903d43923a42ca9ef72fddc62f8c634697d5 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sun, 4 Oct 2026 21:29:02 +0100 Subject: [PATCH] fix: finite forward-mode gradient for FitPositionsImagePairAll(Solved) (#767) The PointSolver pads its image positions with inf sentinel rows, and square_distance gave them a forward-mode tangent of 2*(d - inf)*0 = NaN, which exp carried into every component of jax.jacfwd -- the default AnalysisPoint gradient_mode="forward" -- so every gradient search on the image-plane all-pairs likelihood was stuck on NaN. Reverse mode happened to discard it. all_permutations_log_likelihoods now wraps the likelihoods in a jax.custom_jvp: the primal is the unchanged per-position computation (jitted log likelihoods stay bit-identical, fiducial 7.743201200876812), and the tangent is the JVP of a padding-safe vectorized form (finite placeholder + mask). A per-position double-where was not enough: XLA's CPU fusion of the jitted forward graph still leaked NaN from the discarded branch, and a fully vectorized primal moved the fiducial by 1 ULP. Tests: jitted jacfwd == grad == central FD over 8 PRNGKey draws with inf padding (both classes, red on unfixed source); Fitness forward/reverse parity over 17 vectors now also covers both pair-all classes with a real PointSolver. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01S11WE9oj7Mvkfhc4EPBnyN --- .../point/fit/positions/image/pair_all.py | 94 ++++++++++--- .../fit/positions/image/test_pair_all.py | 131 ++++++++++++++++++ .../test_analysis_point_gradient_mode.py | 77 +++++++--- 3 files changed, 267 insertions(+), 35 deletions(-) diff --git a/autolens/point/fit/positions/image/pair_all.py b/autolens/point/fit/positions/image/pair_all.py index 9a7295e98..a0dfb81f3 100644 --- a/autolens/point/fit/positions/image/pair_all.py +++ b/autolens/point/fit/positions/image/pair_all.py @@ -113,30 +113,64 @@ def all_permutations_log_likelihoods(self) -> np.ndarray: exact region gradient searches must traverse to find the basin. The shifted form is mathematically identical wherever the literal form is finite, and stays finite (the max term contributes exactly 0 after the shift) at arbitrarily large mismatch. + + Padded model positions -- the solver's `inf` sentinel rows, kept because `model_data` solves with + `remove_infinities=False` -- contribute `log_p = -inf`, i.e. nothing to the sum. Their value is right, + but their forward-mode tangent is not: `square_distance` gives `(d - inf) ** 2` a tangent of + `2 * (d - inf) * 0 = NaN`, which `jax.jacfwd` -- the default `gradient_mode="forward"` of + `AnalysisPoint` -- carried through `exp` into every gradient component (PyAutoLens#767). Under JAX the + likelihoods are therefore wrapped in a `jax.custom_jvp`: the primal is the unchanged per-position + computation below (so jitted log likelihoods stay bit-identical), and the tangent is the JVP of + `_log_likelihoods_padding_safe`, which scores padded rows on a finite placeholder and masks them out. """ model_data = self.model_data.array - def log_sum_exp(log_ps): - # `initial` covers the zero-model-positions case (an empty `log_ps`), where a bare `max` - # raises: the -inf sentinel is clamped to 0 below, giving `log(sum of nothing) = -inf`, - # exactly the literal form's result, which `chi_squared`'s `has_image` fallback replaces. - max_log_p = self._xp.max(log_ps, initial=-np.inf) - # With no finite model position every log_p is -inf, and shifting by a -inf max would - # produce NaN (`-inf - -inf`) inside `exp` — including under `jax.grad`, where a NaN in - # the branch `chi_squared`'s `xp.where` discards still poisons the gradient. Clamp the - # shift to 0 so this case reduces to the literal form's `log(0) = -inf`, which the - # `has_image` fallback in `chi_squared` then replaces. - max_log_p = self._xp.where( - self._xp.isfinite(max_log_p), max_log_p, 0.0 - ) - return max_log_p + self._xp.log( - self._xp.sum(self._xp.exp(log_ps - max_log_p)) + if self._xp is np: + return self._log_likelihoods_from(model_data) + + import jax + + @jax.custom_jvp + def log_likelihoods(model_data): + return self._log_likelihoods_from(model_data) + + @log_likelihoods.defjvp + def log_likelihoods_jvp(primals, tangents): + (model_data,), (model_data_dot,) = primals, tangents + _, log_likelihoods_dot = jax.jvp( + self._log_likelihoods_padding_safe, (model_data,), (model_data_dot,) ) + return self._log_likelihoods_from(model_data), log_likelihoods_dot + + return log_likelihoods(model_data) + def _log_sum_exp(self, log_ps): + """ + Max-shifted `log(sum(exp(log_ps)))` over the model positions (see `all_permutations_log_likelihoods`). + """ + # `initial` covers the zero-model-positions case (an empty `log_ps`), where a bare `max` + # raises: the -inf sentinel is clamped to 0 below, giving `log(sum of nothing) = -inf`, + # exactly the literal form's result, which `chi_squared`'s `has_image` fallback replaces. + max_log_p = self._xp.max(log_ps, initial=-np.inf) + # With no finite model position every log_p is -inf, and shifting by a -inf max would + # produce NaN (`-inf - -inf`) inside `exp` — including under `jax.grad`, where a NaN in + # the branch `chi_squared`'s `xp.where` discards still poisons the gradient. Clamp the + # shift to 0 so this case reduces to the literal form's `log(0) = -inf`, which the + # `has_image` fallback in `chi_squared` then replaces. + max_log_p = self._xp.where(self._xp.isfinite(max_log_p), max_log_p, 0.0) + return max_log_p + self._xp.log( + self._xp.sum(self._xp.exp(log_ps - max_log_p)) + ) + + def _log_likelihoods_from(self, model_data) -> np.ndarray: + """ + The per-observed-position log likelihoods for the given model positions (`inf` padding rows included): + the primal of `all_permutations_log_likelihoods`. + """ return self._xp.array( [ - log_sum_exp( + self._log_sum_exp( self._xp.array( [ self.log_p( @@ -152,6 +186,34 @@ def log_sum_exp(log_ps): ] ) + def _log_likelihoods_padding_safe(self, model_data) -> np.ndarray: + """ + `_log_likelihoods_from` with a forward-mode tangent that stays finite on padded model positions, used + only for the tangent of `all_permutations_log_likelihoods` under JAX. + + Padded (`inf`) rows are scored on a finite placeholder and masked back to `log_p = -inf` (the + double-`where` pattern), so no `inf` reaches `square_distance`. Each observed position is scored + against all model positions in one vectorized `log_p` call: with one scalar `where` per model + position instead, XLA's CPU fusion of the jitted forward-mode graph still leaked a non-finite tangent + out of the discarded branch. + """ + is_valid = self._xp.isfinite(model_data).all(axis=1) + # Transposed so `square_distance`'s `coord2[0]` / `coord2[1]` index all x / all y coordinates. + safe_model_data = self._xp.where(is_valid[:, None], model_data, 0.0).T + + return self._xp.array( + [ + self._log_sum_exp( + self._xp.where( + is_valid, + self.log_p(data_position, safe_model_data, sigma), + -np.inf, + ) + ) + for data_position, sigma in zip(self.data, self.noise_map) + ] + ) + @property def chi_squared(self) -> float: """ diff --git a/test_autolens/point/fit/positions/image/test_pair_all.py b/test_autolens/point/fit/positions/image/test_pair_all.py index a6bfbdab8..c853d0ce1 100644 --- a/test_autolens/point/fit/positions/image/test_pair_all.py +++ b/test_autolens/point/fit/positions/image/test_pair_all.py @@ -296,3 +296,134 @@ def test__model_positions_present__chi_squared_unchanged(data, noise_map): ) assert fit.chi_squared == -2.0 * -4.40375330990644 + + +# Real model positions of the forward-mode gradient tests below, followed by solver-style `inf` +# padding rows (the `PointSolver` pads to a fixed size and is called with `remove_infinities=False`). +_GRAD_BASE_POSITIONS = np.array( + [(-1.03, -1.09), (0.35, 1.63), (1.57, 0.43), (1.25, 1.22)] + + [(np.inf, np.inf)] * 6 +) +_GRAD_DATA = al.Grid2DIrregular([(-1.0, -1.1), (0.4, 1.6), (1.6, 0.45), (1.2, 1.25)]) +_GRAD_NOISE_MAP = al.ArrayIrregular([0.05, 0.05, 0.05, 0.05]) + + +class _PaddedSolver: + """Model positions that move smoothly with `theta` (scale, shift_0, shift_1), with `inf` padding + rows whose tangent is exactly 0 -- the shape `PointSolver.solve` returns under JAX.""" + + def __init__(self, theta): + self.theta = theta + + def solve(self, tracer, source_plane_coordinate, xp=np, plane_redshift=None, remove_infinities=True): + import jax.numpy as jnp + + base = jnp.asarray(_GRAD_BASE_POSITIONS) + is_padding = ~jnp.isfinite(base).all(axis=1) + moved = jnp.where(is_padding[:, None], 0.0, base) * self.theta[0] + self.theta[1:3] + return type("Positions", (), {"array": jnp.where(is_padding[:, None], jnp.inf, moved)})() + + +@pytest.mark.parametrize( + "fit_cls, point", + [ + (al.FitPositionsImagePairAll, al.ps.Point(centre=(0.07, 0.07))), + (al.FitPositionsImagePairAllSolved, al.ps.PointSolved()), + ], +) +def test__forward_mode_gradient_with_padded_model_positions__finite_and_matches_reverse_and_fd( + fit_cls, point +): + """ + Regression (PyAutoLens#767): `AnalysisPoint` differentiates with `jax.jacfwd` by default, and the + forward gradient of the all-pairs likelihood was NaN in every component. The padded (`inf`) model + positions gave `square_distance` a tangent of `2 * (d - inf) * 0 = NaN`, which `exp` carried into + the log likelihood; reverse mode happened to discard it. + + Jitted, as gradient searches run it (XLA's fusion of the jitted graph is where a per-position + guard still leaked NaN), over eight parameter draws: `jacfwd` must be finite, non-zero, equal to + `grad`, and equal to a central finite difference of the (smooth, mocked-solver) likelihood. + """ + jax = pytest.importorskip("jax") + import jax.numpy as jnp + + jax.config.update("jax_enable_x64", True) + + lens = al.Galaxy( + redshift=0.5, + mass=al.mp.Isothermal(centre=(0.0, 0.0), einstein_radius=1.6, ell_comps=(0.05, 0.0)), + ) + tracer = al.Tracer(galaxies=[lens, al.Galaxy(redshift=1.0, point_0=point)]) + + def log_likelihood(theta): + return fit_cls( + name="point_0", + data=_GRAD_DATA, + noise_map=_GRAD_NOISE_MAP, + tracer=tracer, + solver=_PaddedSolver(theta), + xp=jnp, + ).log_likelihood + + value = jax.jit(log_likelihood) + forward = jax.jit(jax.jacfwd(log_likelihood)) + reverse = jax.jit(jax.grad(log_likelihood)) + + for key in range(8): + theta = jnp.array([1.0, 0.0, 0.0]) + jax.random.uniform( + jax.random.PRNGKey(key), (3,), minval=-0.02, maxval=0.02 + ) + + grad_forward = np.asarray(forward(theta)) + grad_reverse = np.asarray(reverse(theta)) + + h = 1.0e-6 + grad_fd = np.array( + [ + (float(value(theta.at[i].add(h))) - float(value(theta.at[i].add(-h)))) + / (2.0 * h) + for i in range(3) + ] + ) + + assert np.all(np.isfinite(grad_forward)), (key, grad_forward) + assert np.any(grad_forward != 0.0) + + scale = np.max(np.abs(grad_reverse)) + np.testing.assert_allclose(grad_forward, grad_reverse, rtol=1e-6, atol=1e-12 * scale) + np.testing.assert_allclose(grad_forward, grad_fd, rtol=1e-6, atol=1e-6 * scale) + + +def test__padded_model_positions__log_likelihoods_identical_to_padding_removed(): + """ + The padding guard changes tangents only: under JAX (and NumPy) the per-position log likelihoods + with `inf` padding rows are exactly those of the real rows alone. + """ + jax = pytest.importorskip("jax") + import jax.numpy as jnp + + jax.config.update("jax_enable_x64", True) + + real = al.Grid2DIrregular(_GRAD_BASE_POSITIONS[:4]) + + def fit_for(solver, xp): + return al.FitPositionsImagePairAll( + name="point_0", + data=_GRAD_DATA, + noise_map=_GRAD_NOISE_MAP, + tracer=tracer, + solver=solver, + xp=xp, + ) + + expected = fit_for(al.mock.MockPointSolver(real), np).all_permutations_log_likelihoods() + + padded_np = fit_for( + al.mock.MockPointSolver(al.Grid2DIrregular(_GRAD_BASE_POSITIONS)), np + ).all_permutations_log_likelihoods() + padded_jax = jax.jit( + lambda theta: fit_for(_PaddedSolver(theta), jnp).all_permutations_log_likelihoods() + )(jnp.array([1.0, 0.0, 0.0])) + + np.testing.assert_array_equal(padded_np, expected) + np.testing.assert_allclose(np.asarray(padded_jax), expected, rtol=1e-14) diff --git a/test_autolens/point/model/test_analysis_point_gradient_mode.py b/test_autolens/point/model/test_analysis_point_gradient_mode.py index 80077e0f8..4ab3e4c12 100644 --- a/test_autolens/point/model/test_analysis_point_gradient_mode.py +++ b/test_autolens/point/model/test_analysis_point_gradient_mode.py @@ -5,7 +5,9 @@ mode runs reverse-over-forward through every mass profile; ``jax.jacfwd`` over the flat parameter vector was 2-4.5x faster and compiled up to 8x faster in autolens_profiling #327/#331. These tests pin that the declaration exists, that the forward gradient IS the reverse gradient on this -likelihood, and that a real ``MultiStartGradient`` fit runs end-to-end in forward mode. +likelihood -- and on the image-plane all-pairs likelihoods (``FitPositionsImagePairAll`` / +``FitPositionsImagePairAllSolved``), whose forward gradient was NaN in every component until +PyAutoLens#767 -- and that a real ``MultiStartGradient`` fit runs end-to-end in forward mode. The two JAX checks run in a subprocess, deliberately. Building a JAX ``Fitness`` registers the model's classes (``Galaxy`` included) through ``autofit.jax.register_model``, and JAX has no way to @@ -14,7 +16,7 @@ ``autoarray.abstract_ndarray.register_instance_pytree``, which raises on a class another route already registered, so running these checks in-process would break every test after them. A fresh interpreter keeps them independent of test order. The same entry point (``python -parity `` / ``python multi_start declared reverse``) is what a GPU run invokes. +parity []`` / ``python multi_start declared reverse``) is what a GPU run invokes. """ import os @@ -56,7 +58,7 @@ def _dataset(): ) -def _model(): +def _model(point_cls=al.ps.PointSolved): mass = af.Model(al.mp.Isothermal) mass.centre.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.005) mass.centre.centre_1 = af.GaussianPrior(mean=0.0, sigma=0.005) @@ -64,15 +66,37 @@ def _model(): mass.ell_comps.ell_comps_0 = af.GaussianPrior(mean=0.05263158, sigma=0.01) mass.ell_comps.ell_comps_1 = af.GaussianPrior(mean=0.0, sigma=0.01) lens = af.Model(al.Galaxy, redshift=0.5, mass=mass) - source = af.Model(al.Galaxy, redshift=1.0, point_0=af.Model(al.ps.PointSolved)) + point = af.Model(point_cls) + if point_cls is al.ps.Point: + # The free source-plane centre of the non-solved fits, near the dataset's (0.07, 0.07). + point.centre.centre_0 = af.GaussianPrior(mean=0.07, sigma=0.005) + point.centre.centre_1 = af.GaussianPrior(mean=0.07, sigma=0.005) + source = af.Model(al.Galaxy, redshift=1.0, point_0=point) return af.Collection(galaxies=af.Collection(lens=lens, source=source)) -def _analysis(): +# fit_positions_cls name -> the point profile its model pairs with. The image-plane fits run the +# `PointSolver`, whose padded (`inf`) image rows are what made their forward gradient NaN. +PARITY_FITS = { + "FitPositionsSourceSolved": al.ps.PointSolved, + "FitPositionsImagePairAllSolved": al.ps.PointSolved, + "FitPositionsImagePairAll": al.ps.Point, +} + + +def _analysis(fit_positions_cls=al.FitPositionsSourceSolved): + solver = None + if fit_positions_cls is not al.FitPositionsSourceSolved: + solver = al.PointSolver.for_grid( + grid=al.Grid2D.uniform(shape_native=(100, 100), pixel_scales=0.2), + pixel_scale_precision=1e-3, + magnification_threshold=0.1, + neighbor_degree=1, + ) return al.AnalysisPoint( dataset=_dataset(), - solver=None, - fit_positions_cls=al.FitPositionsSourceSolved, + solver=solver, + fit_positions_cls=fit_positions_cls, use_jax=True, ) @@ -88,14 +112,19 @@ def test__analysis_point_declares_forward_mode(): # -------------------------------------------------------------------------- -def _check_parity(): - """Forward == reverse ``(value, grad)`` of the ``FitPositionsSourceSolved`` likelihood through - ``Fitness`` on the flat vector: prior medians + one draw per ``PRNGKey(0..15)``.""" +def _check_parity(fit_positions_cls_name="FitPositionsSourceSolved"): + """Forward == reverse ``(value, grad)`` of the ``fit_positions_cls_name`` likelihood through + ``Fitness`` on the flat vector: prior medians + one draw per ``PRNGKey(0..15)``. + + The image-plane fits differentiate through the ``PointSolver``'s implicit-function gradient, + where forward and reverse agree to ~1e-10 relative rather than to round-off, so they are held + to ``rtol=1e-6``.""" from autofit.jax import register_model from autofit.jax.gradient import value_and_grad_from from autofit.non_linear.fitness import Fitness - model = _model() + fit_positions_cls = getattr(al, fit_positions_cls_name) + model = _model(PARITY_FITS[fit_positions_cls_name]) # Load-bearing: without registration jax.grad of an AnalysisPoint likelihood is silently # all-zero (Fitness registers it too on a JAX analysis; explicit so the check does not # depend on that). @@ -103,7 +132,7 @@ def _check_parity(): fitness = Fitness( model=model, - analysis=_analysis(), + analysis=_analysis(fit_positions_cls), fom_is_log_likelihood=False, convert_to_chi_squared=True, ) @@ -123,6 +152,8 @@ def _check_parity(): ) vectors.append(np.asarray(model.vector_from_unit_vector(list(unit)), dtype=float)) + rtol = 1e-8 if fit_positions_cls is al.FitPositionsSourceSolved else 1e-6 + worst = 0.0 for vector in vectors: value_r, grad_r = reverse(vector) @@ -132,16 +163,21 @@ def _check_parity(): assert np.isfinite(value_r) assert np.all(np.isfinite(grad_r)) assert np.any(grad_r != 0.0) + # Forward mode is the declared default: NaN here (PyAutoLens#767) stalls every gradient search. + assert np.all(np.isfinite(np.asarray(grad_f))), (fit_positions_cls_name, grad_f) np.testing.assert_allclose(value_f, value_r, rtol=1e-10) scale = np.max(np.abs(grad_r)) - np.testing.assert_allclose(grad_f, grad_r, rtol=1e-8, atol=1e-12 * scale) + np.testing.assert_allclose(grad_f, grad_r, rtol=rtol, atol=1e-12 * scale) np.testing.assert_allclose( - fitness_grad(vector), grad_r, rtol=1e-8, atol=1e-12 * scale + fitness_grad(vector), grad_r, rtol=rtol, atol=1e-12 * scale ) worst = max(worst, float(np.max(np.abs(np.asarray(grad_f) - grad_r)) / scale)) - print(f"PARITY_OK n_vectors={len(vectors)} max_rel_grad_diff={worst:.3e}") + print( + f"PARITY_OK {fit_positions_cls_name} n_vectors={len(vectors)} " + f"max_rel_grad_diff={worst:.3e}" + ) def _check_multi_start(gradient_mode=None): @@ -186,10 +222,13 @@ def _run(*args, tmp_path): return parsed, result.stdout -def test__forward_gradient_matches_reverse_through_fitness(tmp_path): - _, stdout = _run("parity", str(tmp_path), tmp_path=tmp_path) +@pytest.mark.parametrize("fit_positions_cls_name", list(PARITY_FITS)) +def test__forward_gradient_matches_reverse_through_fitness( + fit_positions_cls_name, tmp_path +): + _, stdout = _run("parity", str(tmp_path), fit_positions_cls_name, tmp_path=tmp_path) - assert "PARITY_OK n_vectors=17" in stdout + assert f"PARITY_OK {fit_positions_cls_name} n_vectors=17" in stdout def test__multi_start_gradient_point_source_fit_runs_in_forward_mode(tmp_path): @@ -213,7 +252,7 @@ def test__multi_start_gradient_point_source_fit_runs_in_forward_mode(tmp_path): conf.instance.push(new_path=TEST_DIR / "config", output_path=Path(sys.argv[2])) if command == "parity": - _check_parity() + _check_parity(*sys.argv[3:4]) elif command == "multi_start": # Each further argument is a mode to fit with; "declared" = no override. for mode in sys.argv[3:] or ["declared"]: