Skip to content

fix(ep): release each factor search's internals so JAX executables are collectable - #1634

Merged
Jammy2211 merged 1 commit into
mainfrom
claude/vmapped-likelihood-jit-compiles-bhog4o
Sep 17, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
claude/vmapped-likelihood-jit-compiles-bhog4o

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #1631.

An EP run keeps every factor step's Status in EPHistory, and each Status pins its Result. The Result's in-memory search internal (the nautilus / dynesty sampler) holds the Fitness, and under JAX the Fitness holds every compiled likelihood executable of that search through its per-instance _vmap / _jit / _grad caches. Nothing in EP reads the sampler afterwards, so an N-factor × M-step run retained N·M sets of executables: slope_hierarchy_scale job 342410 (25 factors, 64 GB) held 76 searches' executables after ~3 steps and died with LLVM ERROR: Unable to allocate section memory!.

AbstractSearch.optimise now calls the new Result.release_search_internal() before pinning the result on the Status. Child results of a CombinedResult share the same sampler object, so they are released too. Ordinary search.fit() is untouched.

Cross-step compile reuse and the vmap(jit) batch-length recompiles are real but secondary (a compile is ~2–3 s of an ~8 min factor search); both are filed as follow-up prompts in PyAutoMind, alongside a latent bug found while building the witness (af.Nautilus as an EP default_optimiser reads analysis._use_jax unguarded and crashes on PriorFactors).

API Changes

No signature changes. One behavioural change on the EP path only: after optimise, status.result.search_internal is served from the on-disk search_internal.dill (or is None when output.search_internal is off in config) rather than from memory. No consumer in this repo reads it; downstream EP callers were not checked.
See full details below.

Test Plan

  • New test_autofit/graphical/test_ep_release_search_internal.py fails on main (assert 1 == 0 live Fitness; _search_internal is a MultiEllipsoidSampler) and passes with the fix
  • python -m pytest test_autofit -q serially: 2858 passed, 2 skipped
  • test_autofit/graphical + non_linear/test_jax_compile.py + non_linear/test_fitness_jax_dispatch.py + non_linear/result serially: 333 passed
  • Empirical witness (below)
  • CI matrix (3.12 / 3.13) green

Evidence: 5-factor JAX witness

5 AnalysisFactors, af.ex.Analysis(use_jax=True), af.Nautilus(n_live=50, n_batch=10, number_of_cores=1) per factor, LaplaceOptimiser on the prior factors, 4 EP steps, JAX_PLATFORMS=cpu, ~3.5 min per run. Live Fitness counted via gc.get_objects() after each step.

step main RSS MB main live Fitness fix RSS MB fix live Fitness
1 529.4 5 462.6 0
2 607.8 10 462.7 0
3 617.4 11 463.0 0
4 632.9 12 464.0 0

Note on the test: a weakref on Fitness.__init__ alone cannot probe this, because a dynesty search that resumes from its checkpoint restores the sampler by unpickling and the restored Fitness never passes __init__; the test therefore counts live Fitness objects as a delta against a pre-measured baseline. test_autofit/graphical and test_autofit/non_linear/search are already flaky under pytest -n auto on main (the autouse do_remove_output fixture wipes the shared test_autofit/output across workers); serial runs are clean.

Full API Changes (for automation & release notes)

Removed

  • none

Added

  • autofit.non_linear.result.Result.release_search_internal() — drops the in-memory sampler reference on the result and on its child_results; the search_internal property keeps its on-disk fallback.

Changed

  • autofit.non_linear.search.abstract_search.NonLinearSearch.optimise — calls result.release_search_internal() (guarded with getattr) before status.result = result.

Migration

  • None required. An EP caller that pulled the sampler out of an in-memory latest_result should enable output.search_internal in config and read it through the search_internal property, which loads the dill.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_018HHD38RW1y1VFEvjd7QutS


Generated by Claude Code

…e collectable

EP keeps every factor step's Status in EPHistory, and each Status pins its
Result. The Result's in-memory search internal (the nautilus/dynesty sampler)
holds the Fitness, and under JAX the Fitness holds every compiled likelihood
executable of that search. Nothing in EP reads the sampler afterwards, so an
N-factor x M-step run retained N*M sets of executables: slope_hierarchy_scale
job 342410 (25 factors, 64 GB) held 76 searches' executables after ~3 steps
and died with `LLVM ERROR: Unable to allocate section memory!`.

`AbstractSearch.optimise` now calls the new `Result.release_search_internal()`
before pinning the result on the Status. The `search_internal` property keeps
its contract (it falls back to the on-disk dill); child results of a
CombinedResult, which share the same sampler object, are released too.

Regression test (dynesty, JAX-free): two optimise calls on one AnalysisFactor
leave no Fitness alive and `status.result._search_internal is None`, while
`.projected_model` / `.samples` / `.model` still resolve. 5-factor JAX Nautilus
witness over 4 EP steps: live Fitness 5 -> 12 and RSS 529 -> 633 MB on main;
0 and flat at ~463 MB with the fix.

Follow-ups filed in PyAutoMind: analysis-level compile cache across EP steps;
vmap(jit) recompiling per Nautilus batch length.

Refs PyAutoFit#1631

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018HHD38RW1y1VFEvjd7QutS
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 17, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit fa2d540 into main Sep 17, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the claude/vmapped-likelihood-jit-compiles-bhog4o branch September 17, 2026 13:24
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(ep): release each factor search's internals so JAX executables are collectable

2 participants