Skip to content

fix: Fitness rejects implausibly large finite log-likelihoods #1543

Closed

Description

@Jammy2211

Overview

Fitness.call guards NaN and inf log-likelihoods but passes any finite
value straight through, however physically impossible its magnitude. RAL pilot
341908_5 (slam_source_pix_nn, free AdaptSplit on DelaunayNN) was killed by
exactly that hole: a non-positive-definite regularization matrix made the fp64
Cholesky return finite garbage (log_l up to 3e+303), Nautilus accepted it as
the best point, shell_log_l reached ~1e56 and f_live never terminated — the
run was ledgered as "0 calls / thrashes" when it had in fact made 90,000 calls.

This adds a magnitude ceiling to Fitness.call, so any |log_likelihood|
above the ceiling is mapped to resample_figure_of_merit exactly as NaN/inf
already are. Because the guard lives inside call, every consumer inherits it —
numpy, _jit, _vmap, Nautilus n_batch, BlackJAXNUTS. af.NSS bypasses
Fitness.call with its own inline JAX closure, so the same ceiling is applied
there too.

Plan

  • Read a static Python float ceiling once in Fitness.__init__ from
    conf.instance["general"]["test"]["log_likelihood_ceiling"] (default 1e20;
    null / inf disables the guard).
  • Add a third xp.where after the existing isnan/isinf wheres in Fitness.call
    mapping |log_likelihood| > ceiling to resample_figure_of_merit. Static
    float, xp.where, no Python if — safe under jax.jit / jax.vmap.
  • Apply the same ceiling in the af.NSS inline log-likelihood closure, which
    does not route through Fitness.call.
  • Mirror the new key into the packaged autofit/config/general.yaml test:
    block (and the test config) so downstream workspaces inherit it.
  • Note in the existing gradient-caveat docstring that the guard is value-only,
    like the NaN/inf wheres it sits beside.
  • Tests over the numpy, jax.jit and jax.vmap paths plus the NSS closure.
Detailed implementation plan

Work Classification

Library

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/fitness-log-likelihood-ceiling

Worktree root: ~/Code/PyAutoLabs-wt/fitness-log-likelihood-ceiling/

Implementation Steps

  1. autofit/non_linear/fitness.py: add get_log_likelihood_ceiling() beside
    get_timeout_seconds(), reading
    conf.instance["general"]["test"]["log_likelihood_ceiling"] and coercing with
    float(...) (PyYAML parses a bare 1e20 as a string). Missing key → 1e20;
    null / inf / none → inf (guard disabled, since abs(x) > inf is never
    true).
  2. Fitness.__init__: self.log_likelihood_ceiling = get_log_likelihood_ceiling()
    — read once, so it is a static Python float by the time any traced code sees it.
  3. Fitness.call: after the isinf where, add
    log_likelihood = self._xp.where(self._xp.abs(log_likelihood) > self.log_likelihood_ceiling, self.resample_figure_of_merit, log_likelihood).
    Idempotent against the -inf / -1e99 / -1e30 sentinels (they re-map to
    themselves).
  4. autofit/non_linear/search/nest/nss/search.py: hoist
    ceiling = get_log_likelihood_ceiling() above the inline log_likelihood
    closure and add the matching jnp.where after the existing
    jnp.where(jnp.isfinite(raw), raw, -1e30).
  5. Config: add log_likelihood_ceiling: 1.0e+20 to the test: block of
    autofit/config/general.yaml and test_autofit/config/general.yaml.
  6. Docs: extend the value-only caveat paragraph in Fitness.call's docstring to
    cover the ceiling.

Key Files

  • autofit/non_linear/fitness.py — the guard, the config read, the docstring.
  • autofit/non_linear/search/nest/nss/search.py — the NSS inline closure.
  • autofit/config/general.yaml, test_autofit/config/general.yaml — the key.
  • test_autofit/non_linear/test_fitness_assertions.py — numpy / jit / vmap tests.
  • test_autofit/non_linear/search/nest/test_nss.py (or a new module) — NSS closure test.

Testing

  • +1e266 → resample_figure_of_merit; 1e19 passes unchanged; ceiling
    disabled (inf) lets 1e266 through.
  • Same three assertions through use_jax_jit=True and use_jax_vmap=True.
  • One NSS test that the inline closure maps an over-ceiling likelihood to the
    -1e30 sentinel.
  • Full pytest test_autofit/.

Original Prompt

Click to expand starting prompt

do the 5 things above listed and make sure we have some SMC runs going soon

Rung 1 of a five-rung overflow-flood wave. Diagnosis (2026-08-29, checkpoint.hdf5
of RAL pilot 341908_5): Adapt* regularization squares the coefficient twice
(λ⁴), the regularization matrix goes non-PD from c≈1e4 under
LogUniform(1e-6, 1e6), fp64 Cholesky returns finite garbage (log_l up to
3e+303), PyAutoFit's Fitness passes any finite value through, and Nautilus
accepts it as the best point → shell_log_l ≈ 1e56, f_live never terminates.
The same pathology explains the knn reference's −480-nat deficit.

Activity

  1. Jammy2211 commented on Aug 29, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    PR: #1545 (feature/fitness-log-likelihood-ceiling, labelled pending-release)

    What landed

    • Fitness.call gains a third xp.where after the isnan/isinf pair: a finite |log_likelihood| above the ceiling maps to resample_figure_of_merit. Inside call, so numpy, _jit, _vmap, Nautilus n_batch and BlackJAXNUTS all inherit it.
    • Ceiling is a static Python float read once in __init__ via the new get_log_likelihood_ceiling(), from general.test.log_likelihood_ceiling. Default 1e20; blank (null) or inf disables. Coerced with float — PyYAML parses a bare 1e20 as a string.
    • Key mirrored into the packaged autofit/config/general.yaml and the test config; a workspace config that pre-dates the key inherits the default rather than losing the guard.
    • af.NSS's inline closure (which bypasses Fitness.call) moved into a module-level nss_log_likelihood_from factory carrying the same ceiling, with -1e30 named NSS_INVALID_LOG_LIKELIHOOD. One implementation, tested directly rather than via a lookalike.
    • The call docstring's gradient caveat extended: the ceiling is value-only, exactly like the guards beside it.

    Tests — full suite 2337 passed, 3 skipped. Numpy-path cases in test_fitness_assertions.py (both signs rejected, ~3e4 passes untouched, disabled-by-inf, idempotent against a -1e99 resample sentinel, config-reader coercion); jax.jit + jax.vmap cases in the new test_fitness_ceiling_jax.py (importorskip-gated, values inside float32 range so the magnitude guard is what fires, not an overflow to inf); NSS closure cases in the new test_log_likelihood_ceiling.py.

    Workspace impact: none — behavioural backstop, default ~15 orders of magnitude above any real fit, no API migration.

    Not merged. Merge and close-out stay with the human (/prm).

  2. Jammy2211 commented on Aug 29, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged as PR #1545 (f71b91e). Fitness.call now maps finite |log_likelihood| above general.test.log_likelihood_ceiling (default 1e20; null/inf disables) to resample_figure_of_merit, inside the jit/vmap region so Nautilus n_batch and BlackJAXNUTS inherit it; NSS's closure shares the ceiling via nss_log_likelihood_from. Motivated by RAL 341908_5 (finite log_l up to 3e+303 accepted as best point). Full suite 2337 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions