diff --git a/autofit/non_linear/result.py b/autofit/non_linear/result.py index 8f2140fdc..86fd454e7 100644 --- a/autofit/non_linear/result.py +++ b/autofit/non_linear/result.py @@ -450,6 +450,32 @@ def search_internal(self): except FileNotFoundError: pass + def release_search_internal(self): + """ + Drop the in-memory reference to the non-linear search's internal sampler. + + The internal sampler (e.g. the `nautilus.Sampler` or the dynesty sampler) + holds its likelihood callable, which is a `Fitness`, which in turn owns + that fit's compiled JAX executables via its `_vmap` / `_jit` / `_grad` + `cached_property` caches. A `Result` that is kept alive therefore keeps + an entire fit's compiled code alive with it. + + This only releases the *in-memory* reference: the `search_internal` + property falls back to `paths.load_search_internal()`, so the sampler is + still reachable for any caller that needs it, provided the fit wrote its + `search_internal.dill` to disk. + + Child results (a `CombinedResult` shares one search internal with every + child) are released too. + """ + self._search_internal = None + + for child_result in self.child_results or []: + release = getattr(child_result, "release_search_internal", None) + + if release is not None: + release() + @property def projected_model(self) -> AbstractPriorModel: """ diff --git a/autofit/non_linear/search/abstract_search.py b/autofit/non_linear/search/abstract_search.py index 960edbef2..6cfdc5276 100644 --- a/autofit/non_linear/search/abstract_search.py +++ b/autofit/non_linear/search/abstract_search.py @@ -453,6 +453,34 @@ def optimise( log_norm=log_evidence if log_evidence is not None else 0.0, ) + # Release the in-memory search internal before the result is pinned on + # the `Status`. + # + # `EPHistory`/`FactorHistory` append every `(approx, status)` pair of + # the run, so every `Status`, and therefore every `Result`, lives until + # the EP fit ends. `Result._search_internal` is the sampler, the + # sampler's likelihood callable is a `Fitness`, and a `Fitness` owns + # that factor step's compiled JAX executables (its `_vmap` / `_jit` / + # `_grad` `cached_property` caches). Left attached, an EP run therefore + # accumulates the compiled code of every factor search it has ever run: + # RAL job 342410 (`slope_hierarchy_scale`, 25 analysis factors) held 76 + # searches' executables and died with `LLVM ERROR: Unable to allocate + # section memory!` inside 64 GB, after ~3 EP steps. + # + # Nothing in `autofit.graphical` reads the sampler afterwards (the + # consumers of `latest_result` use `.projected_model`, `.samples` and + # `.model`), and `Result.search_internal` still falls back to + # `paths.load_search_internal()`, so the sampler remains reachable from + # the on-disk dill for anything that does want it. + # + # Guarded because `optimise` does not require a `Result`: the regression + # suite's `StaticSearch` returns a bare object carrying only + # `projected_model` (as the `getattr` on `samples` above allows for). + release_search_internal = getattr(result, "release_search_internal", None) + + if release_search_internal is not None: + release_search_internal() + status.result = result return new_model_dist, status diff --git a/test_autofit/graphical/test_ep_release_search_internal.py b/test_autofit/graphical/test_ep_release_search_internal.py new file mode 100644 index 000000000..a2bc9018d --- /dev/null +++ b/test_autofit/graphical/test_ep_release_search_internal.py @@ -0,0 +1,140 @@ +""" +An EP factor step must not retain its search's internal sampler (#1631). + +RAL job `slope_hierarchy_scale` 342410 (25 `AnalysisFactor`s, a JAX likelihood, +Nautilus with `number_of_cores=1`, 64 GB) died with +`LLVM ERROR: Unable to allocate section memory!` after roughly three EP steps +(76 factor searches). The cause is retention, not compile time: + + EPHistory/FactorHistory keeps every (approx, Status) pair + -> Status.result (set by `AbstractSearch.optimise`) + -> Result._search_internal (the nautilus/dynesty sampler) + -> the sampler's likelihood callable, a `Fitness` + -> `Fitness._vmap` / `._jit` / `._grad`, per-instance + `cached_property` JIT caches holding compiled XLA + executables + +so every executable compiled anywhere in the run stayed alive for the whole +run. Nothing in `autofit/graphical` reads `search_internal` afterwards, and +`Result.search_internal` already falls back to the on-disk dill, so `optimise` +releases the in-memory reference before pinning the result on the `Status`. +""" + +import gc +import weakref + +import numpy as np +import pytest + +import autofit as af +import autofit.graphical as g +from autofit.non_linear.fitness import Fitness +from test_autofit.graphical.gaussian.model import Analysis, Gaussian, make_data + +pytestmark = pytest.mark.filterwarnings("ignore::FutureWarning") + + +@pytest.fixture(name="factor_model") +def make_factor_model(): + x = np.arange(100) + y = make_data(Gaussian(centre=50.0, normalization=25.0, sigma=10.0), x) + + prior_model = af.Model( + Gaussian, + centre=af.GaussianPrior(mean=50, sigma=20), + normalization=af.GaussianPrior(mean=25, sigma=10), + sigma=af.GaussianPrior(mean=10, sigma=10), + ) + + return g.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y)) + + +@pytest.fixture(name="fitness_refs") +def make_fitness_refs(monkeypatch): + """ + Record a `weakref` to every `Fitness` built while the test runs. + + The search modules import `Fitness` by name, so the class attribute + `Fitness.__init__` is patched rather than any module-level binding. + """ + refs = [] + original_init = Fitness.__init__ + + def recording_init(self, *args, **kwargs): + refs.append(weakref.ref(self)) + return original_init(self, *args, **kwargs) + + monkeypatch.setattr(Fitness, "__init__", recording_init) + + return refs + + +def live_fitness_count(): + """ + How many `Fitness` objects are currently alive. + + This counts rather than tracking weakrefs from `Fitness.__init__` because a + dynesty search that resumes from its checkpoint restores its sampler by + unpickling it, and the `Fitness` inside the restored sampler is therefore + built through `__setstate__` and never passes through `__init__`. + """ + return sum(isinstance(obj, Fitness) for obj in gc.get_objects()) + + +@pytest.mark.filterwarnings("ignore::RuntimeWarning") +def test__ep_factor_step_does_not_retain_fitness(factor_model, fitness_refs): + search = af.DynestyStatic(maxcall=5, number_of_cores=1) + + factor_approx = factor_model.mean_field_approximation().factor_approximation( + factor_model + ) + + gc.collect() + fitness_before = live_fitness_count() + + # Two EP steps. `optimise` bumps `optimisation_counter` and re-derives + # `self.paths` as a fresh `SubDirectoryPaths` each call, so the same search + # object runs both steps without a path collision. + model_dist, status = search.optimise(factor_approx) + del model_dist, status + + model_dist, status = search.optimise(factor_approx) + + del factor_approx + + # Defensive: refcounting alone should suffice, but the search and its paths + # may participate in cycles. + gc.collect() + + # The searches really ran (they were not bypassed in test mode) ... + assert len(fitness_refs) >= 2 + + # ... and neither step left a `Fitness`, nor therefore its compiled JAX + # executables, alive behind the retained `Status`. + assert all(ref() is None for ref in fitness_refs) + assert live_fitness_count() == fitness_before + + assert status.result._search_internal is None + + assert status.result.projected_model is not None + assert status.result.samples is not None + assert status.result.model is not None + + # The on-disk fallback must still be reachable without raising. With the + # default test config `output.search_internal` is off, so `None` is the + # expected answer here — the point is that reading it is safe. + status.result.search_internal + + +def test__release_search_internal_clears_in_memory_reference(): + result = af.m.MockResult() + + result._search_internal = object() + child = af.m.MockResult() + child._search_internal = object() + result.child_results = [child] + + result.release_search_internal() + + assert result._search_internal is None + assert child._search_internal is None