Repository navigation
Description
Overview
af.DynestyStatic(number_of_cores=1) / af.DynestyDynamic(number_of_cores=1) still enter _fork_pool_cls() and build a real forked Pool(1) (autofit/non_linear/search/nest/dynesty/search/abstract.py, the fork-context subclass from #1439), dropping to serial only when pool construction raises RuntimeError (or when force_x1_cpu / analysis._use_jax pre-empt it). A Pool(1) forces every likelihood call into a forked worker: it deadlocks in XLA compilation when the likelihood touches JAX without use_jax=True set on the analysis, and a dead worker is silently replaced with its task never re-issued. Nautilus was fixed for the same defect in #1442/#1443 (number_of_cores <= 1 → no pool); Dynesty was not. Found while writing test__ep_single_core_search_unchanged for #1608, which still carries a comment excusing the Pool(1).
Witness correction. The prompt's witness names a use_jax=True analysis, but the existing guard already sends _use_jax=True analyses serial, so that witness passes today. The witness that fails today is the Nautilus-shaped one: a plain af.ex.Analysis, number_of_cores=1, fork_context monkeypatched to raise AssertionError (not RuntimeError, so the fallback does not catch it). That is the regression test.
Plan
- At
number_of_cores <= 1, Dynesty builds no pool and runs serially; the fork-contextForkPoolstays for genuine multi-core runs. - Keep the existing
RuntimeErrorfallback for the multi-core path (Windows / broken multiprocessing). - Update the
fitdocstring, which currently justifies thePool(1)("so a resumed run can then increasenumber_of_cores") — that justification is the trade-off being dropped, same as Nautilus. - Add
test__single_core_builds_no_poolfor Dynesty (Static and Dynamic) mirroring the Nautilus test; confirm it fails onmainfirst. - Let the EP single-core test use its
no_forkingfixture and delete the excusing comment. - Ship via
ship_library; library-only, no workspace API impact (behaviour change only).
Trade-off: a Dynesty run started with number_of_cores=1 and resumed with more cores now raises the existing check_pool SearchException instead of silently speeding up. Nautilus accepted the same trade-off in #1443; JAX-safety and no silent hangs win.
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main @ 27d41e7 (Release 2026.9.15.1) | clean |
Conflict guard fired: PyAutoFit is also claimed by task howtofit-mode (autofit_assistant#42, README-prompt propagation, zero diff vs main so far). File sets are disjoint (README/docs vs dynesty search + tests), so this task runs in its own parallel worktree, recorded as a parallel-claim: on its active.md entry.
Suggested branch: feature/dynesty-single-core-no-pool
Worktree root: ~/Code/PyAutoLabs-wt/dynesty-single-core-no-pool/
Implementation Steps
autofit/non_linear/search/nest/dynesty/search/abstract.py,AbstractDynesty.fit:- Extend the serial pre-emption (currently
if self.force_x1_cpu or analysis._use_jax: raise RuntimeError) to also fire onself.number_of_cores <= 1. Keep the existing try/except shape (minimal diff, the fallback logging block already exists) and add a fourthlogger.infobranch in theexceptfor thenumber_of_cores <= 1case, e.g. "Running Dynesty single-CPU (number_of_cores=1, no pool)." so the log no longer claims the pool "could not be created". - Docstring: replace the paragraph "This occurs even if
number_of_cores=1, because the dynesty savestate includes this pool, meaning that a resumed run can then increase thenumber_of_cores" with: a pool is built only fornumber_of_cores > 1; single-core runs are fully serial (aPool(1)forces every likelihood call through a forked worker, which deadlocks in XLA compilation and hangs on a dead worker — Nautilus 1-core fits deadlock under JAX: fork Pool(1) bypasses nautilus's serial-pool guard (release-integrate RED) #1442/fix: single-core Nautilus fits build no pool (release-integrate corrective, #1442) #1443); a run started single-core cannot be resumed multi-core (check_poolraisesSearchException, as on the existingforce_x1_cpupath). Keep the WindowsRuntimeErrorfallback paragraph. number_of_coresparameter docstring: one line noting1builds no pool.- No change to
_fork_pool_cls,static.py,dynamic.py,check_pool,write_uses_pool— thepool=None, queue_size=Nonebranch already exists and writesuses_pool=False.
- Extend the serial pre-emption (currently
test_autofit/non_linear/search/nest/test_dynesty.py: addtest__single_core_builds_no_pool, parametrised overaf.DynestyStaticandaf.DynestyDynamic, mirroringtest_nautilus.py::test__single_core_builds_no_pool: monkeypatchfork_contextonautofit.non_linear.paralleland on the dynesty abstract module (the module imports it inside_fork_pool_cls, so patch both, as the EPno_forkingfixture does) to raiseAssertionError;PYAUTO_TEST_MODE=1;af.Model(af.ex.Gaussian)+af.ex.Analysis(data=np.full(100, 5.0), noise_map=np.full(100, 1.0));nlive=10,number_of_cores=1;search.fit(...)must complete. Run it onmainfirst and confirm it fails (red → green).test_autofit/graphical/test_ep_no_multiprocessing.py::test__ep_single_core_search_unchanged: take theno_forkingfixture, delete the comment excusing thePool(1).
Testing
source $WT_ROOT/activate.sh; pytest test_autofit/non_linear/search/nest/test_dynesty.py test_autofit/non_linear/search/nest/test_nautilus.py test_autofit/graphical/test_ep_no_multiprocessing.py -q; then the full test_autofit/non_linear directory.
Key Files
autofit/non_linear/search/nest/dynesty/search/abstract.py—_fork_pool_cls,AbstractDynesty.fit(pool branch + fallback),check_poolautofit/non_linear/search/nest/nautilus/search.py— the fix: single-core Nautilus fits build no pool (release-integrate corrective, #1442) #1443 fix shape being mirrored (number_of_cores <= 1→nullcontext(None))test_autofit/non_linear/search/nest/test_nautilus.py—test__single_core_builds_no_poolto mirrortest_autofit/non_linear/search/nest/test_dynesty.py— new regression testtest_autofit/graphical/test_ep_no_multiprocessing.py—no_forkingfixture, EP single-core test
Original Prompt
Click to expand starting prompt
Dynesty single-core path still builds a Pool(1) instead of running serially
Type: bug
Target: PyAutoFit
Repos:
- PyAutoFit
Difficulty: small
Autonomy: supervised
Priority: medium
Status: formalised
Consequence: judge
Witness: a number_of_cores=1 DynestyStatic fit over a use_jax=True analysis completes under PYAUTO_TEST_MODE=1 with fork_context monkeypatched to raise, i.e. without forking.
Review-minutes: 20
Unattended: ready
Dynesty single-core path still builds a Pool(1) instead of running serially
Type: bug
Target: PyAutoFit
Difficulty: small
Autonomy: supervised
Priority: medium
Witness: a number_of_cores=1 DynestyStatic fit over a use_jax=True analysis completes under PYAUTO_TEST_MODE=1 with fork_context monkeypatched to raise, i.e. without forking.
Found 2026-09-11 while adding test__ep_single_core_search_unchanged for PyAutoFit#1608: af.DynestyStatic(number_of_cores=1) still enters its own _fork_pool_cls() Pool(1) (autofit/non_linear/search/nest/dynesty/search/abstract.py, the fork-context subclass from PyAutoFit#1439), falling back to serial only if the pool raises RuntimeError. This is the same shape as PyAutoFit#1442/#1443 for Nautilus: a real Pool(1) forces every likelihood call into a forked worker, which deadlocks in XLA compilation when the likelihood touches JAX (a forked child of a JAX-initialised parent cannot compile), and a dead worker is silently replaced with its task never re-issued. Nautilus was fixed to pass pool=None at number_of_cores<=1 (#1443); Dynesty was not.
Fix: at number_of_cores<=1 build no pool at all and run dynesty serially, keeping the fork-context pool for genuine multi-core runs. Regression test mirroring test__single_core_builds_no_pool in test_autofit/non_linear/search/nest/test_nautilus.py (monkeypatch fork_context to raise; a number_of_cores=1 DynestyStatic fit must complete without touching it). The EP single-core test in test_autofit/graphical/test_ep_no_multiprocessing.py can then also use its no_forking fixture.
Activity
Jammy2211 commentedon Sep 15, 2026
Library PR Created
pending-release)feature/dynesty-single-core-no-pool@ b404cd3Witness (red → green):
test__single_core_builds_no_pool[DynestyStatic|DynestyDynamic]fails onmainwithAssertionError: fork_context must not be used when number_of_cores == 1raised fromForkPool.__enter__, and passes on the branch. Note the witness correction from the plan: ause_jax=Trueanalysis was already routed serial by the existing guard, so the failing witness is a plainaf.ex.Analysisatnumber_of_cores=1.Gate:
pytest test_autofit/ -x: 2854 passed, 2 skipped (215 s).pyauto-heart smoke autofit --root <worktree>: Passed 10 | Failed 0 | Total 10 (8 scripts + 2 notebooks). Caveat: the smoke profile runs atPYAUTO_TEST_MODE=2, which bypasses the sampler, sosearches/nest.pyconfirms imports and script flow only; the no-pool path is exercised by the unit test atPYAUTO_TEST_MODE=1.stale(post-2026.9.15.1 release, "no rehearsal for current source"); red and yellow reason lists empty.API impact: none (behaviour only). Workspace grep found no script or prose relying on starting a Dynesty run single-core and resuming multi-core, so no workspace follow-up. Trade-off recorded on the PR: such a resume now raises
check_pool'sSearchException, as on theforce_x1_cpupath.Next:
/prmonce CI is green (merge stays human).Jammy2211 commentedon Sep 15, 2026
Shipped
PyAutoFit#1632 merged the one-core Dynesty serial path. At
number_of_cores <= 1, DynestyStatic and DynestyDynamic now build no fork-context pool; multi-core and Windows fallback behavior remain unchanged. The docstrings explain that a search started single-core cannot later resume multi-core. The regression test exercises the no-pool path for both samplers, and the EP single-core test uses its no-forking fixture.