diff --git a/autofit/non_linear/search/nest/dynesty/search/abstract.py b/autofit/non_linear/search/nest/dynesty/search/abstract.py index 0263aa6e7..6f8605152 100644 --- a/autofit/non_linear/search/nest/dynesty/search/abstract.py +++ b/autofit/non_linear/search/nest/dynesty/search/abstract.py @@ -119,7 +119,8 @@ def __init__( iterations_per_full_update The number of iterations performed between every Dynesty back-up. number_of_cores - The number of cores sampling is performed using a Python multiprocessing Pool instance. + The number of cores sampling is performed using a Python multiprocessing Pool instance. A value of 1 + builds no pool and runs serially. silence If True, the default print output of the non-linear search is silenced. force_x1_cpu @@ -189,9 +190,12 @@ def _fit( Fit a model using the search and the Analysis class which contains the data and returns the log likelihood from instances of the model, which the `NonLinearSearch` seeks to maximize. - By default, Dynesty runs using an in-built multiprocessing Pool option. This occurs even - if `number_of_cores=1`, because the dynesty savestate includes this pool, meaning that a resumed run can - then increase the `number_of_cores`. + A multiprocessing pool is built only when `number_of_cores > 1`. At `number_of_cores=1` the search runs + fully serially with no pool, because a `Pool(1)` forces every likelihood call through a forked worker, + which deadlocks in XLA compilation when the likelihood touches JAX and hangs forever if that worker dies + (the same fix Nautilus received in #1442 / #1443; see #1630). Consequently, a run started with + `number_of_cores=1` cannot be resumed with more cores (`check_pool` raises a `SearchException`), exactly + as on the existing `force_x1_cpu` path. However, certain operating systems (e.g. Windows) do not support Python multiprocessing particularly well. This can cause Dynesty to crash when a pool is included. If this occurs (raising a `RunTimeException`) @@ -241,7 +245,7 @@ def _fit( while not finished: try: - if self.force_x1_cpu or analysis._use_jax: + if self.number_of_cores <= 1 or self.force_x1_cpu or analysis._use_jax: raise RuntimeError Pool = _fork_pool_cls() @@ -275,6 +279,10 @@ def _fit( self.logger.info( "Running Dynesty single-CPU per `force_x1_cpu=True` (no pool)." ) + elif self.number_of_cores <= 1: + self.logger.info( + "Running Dynesty single-CPU (number_of_cores=1, no pool)." + ) else: self.logger.info( f""" diff --git a/test_autofit/graphical/test_ep_no_multiprocessing.py b/test_autofit/graphical/test_ep_no_multiprocessing.py index c7c9f0d2f..a54c6b13c 100644 --- a/test_autofit/graphical/test_ep_no_multiprocessing.py +++ b/test_autofit/graphical/test_ep_no_multiprocessing.py @@ -87,11 +87,7 @@ def test__ep_refuses_multi_core_search(factor_model, no_forking): @pytest.mark.filterwarnings("ignore::RuntimeWarning") -def test__ep_single_core_search_unchanged(factor_model): - # No `no_forking` here: the guard is the only thing this change adds, and - # Dynesty's own single-core path still enters a `Pool(1)` of its own - # (`_fork_pool_cls`, with a serial RuntimeError fallback) — pre-existing - # behaviour this issue does not touch. +def test__ep_single_core_search_unchanged(factor_model, no_forking): search = af.DynestyStatic(maxcall=5, number_of_cores=1) result, status = search.optimise( diff --git a/test_autofit/non_linear/search/nest/test_dynesty.py b/test_autofit/non_linear/search/nest/test_dynesty.py index 4b62c8d25..94d5cc58b 100644 --- a/test_autofit/non_linear/search/nest/test_dynesty.py +++ b/test_autofit/non_linear/search/nest/test_dynesty.py @@ -1,3 +1,4 @@ +import numpy as np import pytest import autofit as af @@ -59,3 +60,46 @@ def test__explicit_params(): assert search.facc == 0.2 assert search.dlogz_init == 0.01 assert search.number_of_cores == 1 + + +@pytest.mark.parametrize("search_cls", [af.DynestyStatic, af.DynestyDynamic]) +def test__single_core_builds_no_pool(search_cls, monkeypatch): + """ + number_of_cores=1 must not construct a multiprocessing pool: dynesty treats + pool=None as fully serial, whereas a Pool(1) object forces every likelihood + call into a forked worker — which deadlocks in XLA compilation when the + likelihood touches JAX, and hangs forever if that single worker dies, + because the pool never re-issues a dead worker's in-flight task. Nautilus + received the same fix in #1442/#1443; this is #1630 for dynesty. + """ + import importlib + + def no_fork_context(*args, **kwargs): + raise AssertionError("fork_context must not be used when number_of_cores == 1") + + parallel = importlib.import_module("autofit.non_linear.parallel") + + # `_fork_pool_cls` imports `fork_context` from the package at call time, so + # patching the package attribute is what intercepts the pool build. + monkeypatch.setattr(parallel, "fork_context", no_fork_context) + + monkeypatch.setenv("PYAUTO_TEST_MODE", "1") + + model = af.Model(af.ex.Gaussian) + analysis = af.ex.Analysis( + data=np.full(100, 5.0), + noise_map=np.full(100, 1.0), + ) + + live_point_kwargs = ( + {"nlive": 10} if search_cls is af.DynestyStatic else {"nlive_init": 10} + ) + + search = search_cls( + name="dynesty_single_core", + unique_tag="single_core_no_pool_test", + number_of_cores=1, + **live_point_kwargs, + ) + + search.fit(model=model, analysis=analysis)