Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion autofit/config/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ test:
check_likelihood_function: true # if True, when a search is resumed the likelihood of a previous sample is recalculated to ensure it is consistent with the previous run.
exception_override: false
lh_timeout_seconds: # If a float is input, the log_likelihood_function call is timed out after this many seconds, to diagnose infinite loops. Default is None, meaning no timeout.
log_likelihood_ceiling: 1.0e+20 # If a float is input, log likelihoods whose magnitude exceeds it are treated as numerical garbage and replaced by the resample figure of merit. Blank (null) or inf disables the guard.
log_likelihood_ceiling: # If a float is input, log likelihoods whose magnitude exceeds it are treated as numerical garbage and replaced by the resample figure of merit. Blank (null) or inf disables the guard, which is the default: a log likelihood scales with the noise-map units, so a fixed magnitude can reject a legitimate fit on a badly-scaled dataset. Opt in where the units are known (e.g. autolens_profiling sets 1.0e+20).
parallel_profile: false
126 changes: 108 additions & 18 deletions autofit/non_linear/fitness.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,79 @@ def get_timeout_seconds():
timeout_seconds = get_timeout_seconds()

#: Ceiling used when ``general.test.log_likelihood_ceiling`` is absent from the config (e.g. a
#: workspace whose ``general.yaml`` pre-dates the key). Chosen far above any physically meaningful
#: log likelihood, so it only ever catches numerical garbage.
LOG_LIKELIHOOD_CEILING_DEFAULT = 1.0e20
#: workspace whose ``general.yaml`` pre-dates the key). ``inf`` -- i.e. the guard is **off** --
#: because the packaged default is off, and a config that never mentions the key must inherit
#: today's default rather than the one that shipped in PyAutoFit 2025.x.
LOG_LIKELIHOOD_CEILING_DEFAULT = float("inf")

#: Set the first time the magnitude guard actually rejects a log likelihood on the numpy path, so
#: the warning below is emitted once per process rather than once per sample. Module level, not
#: per-`Fitness`: a search rebuilds its fitness object on resume, and one warning per run is the
#: point.
_log_likelihood_ceiling_warning_emitted = False


def _warn_log_likelihood_ceiling_fired(log_likelihood, ceiling: float):
"""
Warn, once per process, that the magnitude guard has rejected a log likelihood.

The guard is deliberately silent about *which* of the two things it caught: numerical garbage
(what it exists for), or a legitimate likelihood on a dataset whose noise-map units make
``|log_likelihood|`` genuinely enormous (what makes the ceiling unsafe as a default -- see
`get_log_likelihood_ceiling`). Only the user knows their units, so the warning names the value
and the ceiling and leaves the judgement to them.

**Numpy path only.** `Fitness.call` also runs under ``jax.jit`` / ``jax.vmap``, where the
rejection is an ``xp.where`` on a *tracer*: there is no Python-visible moment at which the guard
"fires", so this cannot be called, and those paths reject silently by construction. Nor is a
fire *counter* offered for them -- incrementing one from traced code needs a host callback or a
donated buffer, which perturbs the very code being profiled and does not survive ``vmap`` /
``grad``. A counter that only ever counted the numpy path would read ``0`` on a jitted run that
rejected every sample, which is worse than no counter at all.
"""
global _log_likelihood_ceiling_warning_emitted

if _log_likelihood_ceiling_warning_emitted:
return

_log_likelihood_ceiling_warning_emitted = True

logger.warning(
f"A log likelihood of {log_likelihood} exceeded the configured magnitude ceiling of "
f"{ceiling} and was replaced by the resample figure of merit, so the search will not "
"sample this model. This is usually numerical garbage (e.g. an fp64 Cholesky on a "
"non-positive-definite matrix), but a log likelihood scales with the noise-map units, so a "
"badly-scaled dataset can exceed the ceiling legitimately -- check the units before "
"trusting the rejection. Set general.test.log_likelihood_ceiling higher, or blank to "
"disable the guard. This warning is issued once per run; JAX-traced runs (jit / vmap) "
"cannot issue it at all."
)


def get_log_likelihood_ceiling() -> float:
"""
The largest ``|log_likelihood|`` a fitness function accepts before treating the value as
numerical garbage and substituting the resample figure of merit.

`Fitness.call` maps ``NaN`` and ``inf`` log likelihoods to ``resample_figure_of_merit``, but a
*finite* value of, say, ``3e+303`` passes straight through and the search accepts it as its best
point. That is not hypothetical: a non-positive-definite regularization matrix makes an fp64
Cholesky return finite garbage, a nested sampler treats it as the peak of the posterior, its
shell log evidence explodes to ``~1e56`` and the termination criterion never fires -- the run
burns its wall clock without ever converging.

The ceiling closes that hole. It is read **once** from
**Off by default.** The packaged ``general.yaml`` ships this blank, so the guard is disabled
unless a config opts in. It is off because the ceiling is a bare magnitude in *unspecified
units*: a log likelihood is ``-0.5 * chi_squared - 0.5 * noise_normalization``, and both terms
scale with the noise map -- ``chi_squared`` as ``noise ** -2``, ``noise_normalization``
linearly in the number of pixels. A dataset whose noise map is expressed in a small unit can
therefore produce a *legitimate* log likelihood above any fixed threshold, and every model
would be silently rejected, leaving the search nothing but the resample sentinel. The guard
cannot tell that case from the one below, because a magnitude is the only signal it reads.

What it exists for, when a config does opt in: `Fitness.call` maps ``NaN`` and ``inf`` log
likelihoods to ``resample_figure_of_merit``, but a *finite* value of, say, ``3e+303`` passes
straight through and the search accepts it as its best point. That is not hypothetical: a
non-positive-definite regularization matrix makes an fp64 Cholesky return finite garbage, a
nested sampler treats it as the peak of the posterior, its shell log evidence explodes to
``~1e56`` and the termination criterion never fires -- the run burns its wall clock without ever
converging. `autolens_profiling` enables the ceiling for exactly this reason (run ``341908_5``);
a science analysis should enable it only once its own units are known to be safe.

The value is read **once** from
``conf.instance["general"]["test"]["log_likelihood_ceiling"]`` and returned as a static Python
float, because the guard it feeds runs inside JAX-traced code, where a Python ``if`` on a traced
value is illegal and the comparison must be a static-vs-traced ``xp.where``.
Expand All @@ -59,8 +114,8 @@ def get_log_likelihood_ceiling() -> float:

Returns
-------
The ceiling as a float. ``inf`` disables the guard, which is what a ``null``, non-positive or
unparseable config entry maps to, since ``abs(x) > inf`` is never true.
The ceiling as a float. ``inf`` disables the guard, which is what an absent, ``null``,
non-positive or unparseable config entry maps to, since ``abs(x) > inf`` is never true.
"""
try:
ceiling = conf.instance["general"]["test"]["log_likelihood_ceiling"]
Expand Down Expand Up @@ -266,6 +321,16 @@ def _warmup_visualization(self):
def _xp(self):
return self.analysis._xp

@property
def _is_jax(self) -> bool:
"""
Whether the array backend is JAX, and therefore whether `call` may be running on tracers.

Read by the log-likelihood magnitude guard, which warns from Python and so must stay on the
numpy path — under `jax.jit` / `jax.vmap` a Python branch on a traced value is illegal.
"""
return self._xp.__name__.startswith("jax")

def call(self, parameters):
"""
A private method that calls the fitness function with the given parameters and additional keyword arguments.
Expand All @@ -278,8 +343,11 @@ def call(self, parameters):
the resample sentinel and never select the point. The magnitude ceiling exists because the NaN/inf checks
miss the failure mode that actually kills long runs: an fp64 Cholesky on a non-positive-definite matrix
returns *finite* garbage (log likelihoods up to `3e+303`), which a nested sampler happily accepts as its
best point and then never terminates on. It is a static Python float (see `get_log_likelihood_ceiling`),
so `inf` makes the `where` a no-op and the comparison stays legal under `jax.jit` / `jax.vmap`. Gradient
best point and then never terminates on. It is **off unless a config opts in** — the threshold is a bare
magnitude and a log likelihood scales with the noise-map units, so a fixed ceiling can reject a legitimate
fit (`get_log_likelihood_ceiling` carries the argument). It is a static Python float, so the default `inf`
makes the `where` a no-op and the comparison stays legal under `jax.jit` / `jax.vmap`. When it does fire,
the numpy path warns once per process; the traced paths cannot warn at all. Gradient
consumers get no such protection: under `jax.grad`, reverse-mode differentiates *both* branches of an
`xp.where` and multiplies the unselected one by zero, so if the likelihood's derivative is also non-finite
the guard yields `0 * NaN = NaN` and the returned gradient is NaN even though the value looks handled.
Expand Down Expand Up @@ -309,7 +377,7 @@ def call(self, parameters):
-------
The figure of merit returned to the non-linear search, which is either the log likelihood or log posterior.
"""
if self._xp.__name__.startswith("jax"):
if self._is_jax:

# Get instance from model (must be side-effect free and exception-free under JAX)
instance = self.model.instance_from_vector(vector=parameters, xp=self._xp)
Expand All @@ -333,14 +401,36 @@ def call(self, parameters):

# Penalize finite-but-impossible log-likelihoods (e.g. 3e+303 out of an fp64 Cholesky on a
# non-positive-definite matrix), which the isnan/isinf guards above let through. Same
# value-only caveat. The ceiling is a static float, so `inf` makes this a no-op and the
# value-only caveat. The ceiling is a static float, and it is `inf` unless a config opts in
# (see `get_log_likelihood_ceiling`), so by default this is a no-op; when it is set, the
# resample sentinels (-inf, -1e99, -1e30) map to themselves.
raw_log_likelihood = log_likelihood

over_ceiling = self._xp.abs(log_likelihood) > self.log_likelihood_ceiling

log_likelihood = self._xp.where(
self._xp.abs(log_likelihood) > self.log_likelihood_ceiling,
over_ceiling,
self.resample_figure_of_merit,
log_likelihood,
)

# Tell the user the first time the guard actually rejects something, because a rejection is
# indistinguishable from "the model is bad" from inside the search. Numpy path only:
# `over_ceiling` is a tracer under jit / vmap, so a Python `if` on it is illegal there and
# those runs reject silently (documented in `_warn_log_likelihood_ceiling_fired`). The
# ceiling test comes first so the default (disabled) config short-circuits before touching
# the array at all, and `_warning_emitted` takes this off the hot path after the first fire.
if (
self.log_likelihood_ceiling != np.inf
and not _log_likelihood_ceiling_warning_emitted
and not self._is_jax
):
if bool(np.any(over_ceiling)):
_warn_log_likelihood_ceiling_fired(
log_likelihood=raw_log_likelihood,
ceiling=self.log_likelihood_ceiling,
)

# Determine final figure of merit
if self.fom_is_log_likelihood:
figure_of_merit = log_likelihood
Expand Down
22 changes: 15 additions & 7 deletions autofit/non_linear/search/nest/nss/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,18 @@ def nss_log_likelihood_from(model, analysis, log_likelihood_ceiling=None):

Two things are rejected, both mapped to `NSS_INVALID_LOG_LIKELIHOOD`:

- non-finite values (``NaN`` / ``inf``), as before;
- finite values whose magnitude exceeds the configured ceiling. An fp64 Cholesky on a
non-positive-definite matrix returns finite garbage (log likelihoods up to ``3e+303``), which
a nested sampler otherwise accepts as its highest-likelihood live point; the shell log
evidence then explodes and the termination criterion never fires.
- non-finite values (``NaN`` / ``inf``), always;
- finite values whose magnitude exceeds the configured ceiling, **if** a config has opted in.
An fp64 Cholesky on a non-positive-definite matrix returns finite garbage (log likelihoods up
to ``3e+303``), which a nested sampler otherwise accepts as its highest-likelihood live point;
the shell log evidence then explodes and the termination criterion never fires. The ceiling is
off in the packaged config, because a log likelihood scales with the noise-map units and a
fixed magnitude can therefore reject a legitimate fit
(`autofit.non_linear.fitness.get_log_likelihood_ceiling` carries the full argument).

This closure is JAX-only, so it cannot warn when the ceiling fires — the rejection is an
``jnp.where`` on a tracer. `Fitness.call`'s numpy path warns once per process; ``af.NSS`` does
not sample through it.

Parameters
----------
Expand All @@ -70,8 +77,9 @@ def nss_log_likelihood_from(model, analysis, log_likelihood_ceiling=None):
The analysis supplying `log_likelihood_function`.
log_likelihood_ceiling
The magnitude ceiling. Defaults to the configured value
(`autofit.non_linear.fitness.get_log_likelihood_ceiling`); ``inf`` disables the check. It
must be a static Python float, because JAX traces the comparison it feeds.
(`autofit.non_linear.fitness.get_log_likelihood_ceiling`), which is ``inf`` — the check
disabled — unless a config opts in. It must be a static Python float, because JAX traces
the comparison it feeds.

Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion test_autofit/config/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ test:
preloads_check_threshold: 1.0 # If the figure of merit of a fit with and without preloads is greater than this threshold, the check preload test fails and an exception raised for a model-fit. # If True, perform a sanity check that the likelihood using preloads is identical to the likelihood not using preloads.
exception_override: false
lh_timeout_seconds: # If a float is input, the log_likelihood_function call is timed out after this many seconds, to diagnose infinite loops. Default is None, meaning no timeout.
log_likelihood_ceiling: 1.0e+20 # If a float is input, log likelihoods whose magnitude exceeds it are treated as numerical garbage and replaced by the resample figure of merit. Blank (null) or inf disables the guard.
log_likelihood_ceiling: # If a float is input, log likelihoods whose magnitude exceeds it are treated as numerical garbage and replaced by the resample figure of merit. Blank (null) or inf disables the guard, which is the default: a log likelihood scales with the noise-map units, so a fixed magnitude can reject a legitimate fit on a badly-scaled dataset. Opt in where the units are known (e.g. autolens_profiling sets 1.0e+20).
parallel_profile: false
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

PARAMETERS = [1.0, 1.0, 1.0]

#: The ceiling `autolens_profiling` opts into. PyAutoFit ships the guard off, so every
#: enabled-path test below passes it explicitly.
CEILING = 1.0e20

#: Above the 1e20 ceiling, inside float32 range — so the value is rejected by the magnitude guard
#: and not by the isfinite guard that precedes it.
OVER_CEILING = 1.0e30
Expand All @@ -45,13 +49,13 @@ def test_nss_closure_rejects_a_log_likelihood_above_the_ceiling():
The failure this exists for: a finite `3e+303` out of an fp64 Cholesky becomes the
highest-likelihood live point, the shell log evidence explodes and the run never terminates.
"""
log_likelihood = _log_likelihood_from(OVER_CEILING, log_likelihood_ceiling=1.0e20)
log_likelihood = _log_likelihood_from(OVER_CEILING, log_likelihood_ceiling=CEILING)

assert float(log_likelihood(jnp.array(PARAMETERS))) == NSS_INVALID_LOG_LIKELIHOOD


def test_nss_closure_passes_a_log_likelihood_below_the_ceiling():
log_likelihood = _log_likelihood_from(UNDER_CEILING, log_likelihood_ceiling=1.0e20)
log_likelihood = _log_likelihood_from(UNDER_CEILING, log_likelihood_ceiling=CEILING)

assert float(log_likelihood(jnp.array(PARAMETERS))) == pytest.approx(
UNDER_CEILING, rel=1.0e-5
Expand All @@ -64,7 +68,7 @@ def test_nss_closure_still_rejects_non_finite_log_likelihoods(log_likelihood):
The magnitude guard is added *after* the isfinite guard; the pre-existing behaviour must be
unchanged.
"""
closure = _log_likelihood_from(log_likelihood, log_likelihood_ceiling=1.0e20)
closure = _log_likelihood_from(log_likelihood, log_likelihood_ceiling=CEILING)

assert float(closure(jnp.array(PARAMETERS))) == NSS_INVALID_LOG_LIKELIHOOD

Expand All @@ -75,7 +79,7 @@ def test_nss_closure_sentinel_maps_to_itself():
idempotent against the value it substitutes.
"""
closure = _log_likelihood_from(
NSS_INVALID_LOG_LIKELIHOOD, log_likelihood_ceiling=1.0e20
NSS_INVALID_LOG_LIKELIHOOD, log_likelihood_ceiling=CEILING
)

assert float(closure(jnp.array(PARAMETERS))) == NSS_INVALID_LOG_LIKELIHOOD
Expand All @@ -89,11 +93,26 @@ def test_nss_closure_with_the_ceiling_disabled_passes_any_finite_value():
)


def test_nss_closure_defaults_to_the_configured_ceiling():
def test_nss_closure_defaults_to_the_configured_ceiling_which_is_disabled():
"""
With no explicit ceiling the closure reads the config, so `af.NSS` picks the guard up without
the search having to thread it.
With no explicit ceiling the closure reads the config — and the packaged config ships the guard
**off**, because the threshold is a bare magnitude and a log likelihood scales with the
noise-map units. So `af.NSS` picks up "disabled" by default and an enormous finite value passes
through, exactly as it did before the guard existed. A config that opts in (as
`autolens_profiling` does) is what turns the tests above into the live behaviour.
"""
closure = _log_likelihood_from(OVER_CEILING)

assert float(closure(jnp.array(PARAMETERS))) == pytest.approx(
OVER_CEILING, rel=1.0e-5
)


def test_nss_closure_still_rejects_non_finite_values_with_the_ceiling_disabled():
"""
Turning the magnitude guard off must not turn the isfinite guard off with it — blackjax's
nested-sampler arithmetic still cannot see a `NaN`.
"""
closure = _log_likelihood_from(np.nan)

assert float(closure(jnp.array(PARAMETERS))) == NSS_INVALID_LOG_LIKELIHOOD
Loading
Loading