Skip to content

fix(dynesty): run serially at number_of_cores=1, build no Pool(1) #1630

Closed

Description

@Jammy2211

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-context ForkPool stays for genuine multi-core runs.
  • Keep the existing RuntimeError fallback for the multi-core path (Windows / broken multiprocessing).
  • Update the fit docstring, which currently justifies the Pool(1) ("so a resumed run can then increase number_of_cores") — that justification is the trade-off being dropped, same as Nautilus.
  • Add test__single_core_builds_no_pool for Dynesty (Static and Dynamic) mirroring the Nautilus test; confirm it fails on main first.
  • Let the EP single-core test use its no_forking fixture 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

  1. 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 on self.number_of_cores <= 1. Keep the existing try/except shape (minimal diff, the fallback logging block already exists) and add a fourth logger.info branch in the except for the number_of_cores <= 1 case, 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 the number_of_cores" with: a pool is built only for number_of_cores > 1; single-core runs are fully serial (a Pool(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_pool raises SearchException, as on the existing force_x1_cpu path). Keep the Windows RuntimeError fallback paragraph.
    • number_of_cores parameter docstring: one line noting 1 builds no pool.
    • No change to _fork_pool_cls, static.py, dynamic.py, check_pool, write_uses_pool — the pool=None, queue_size=None branch already exists and writes uses_pool=False.
  2. test_autofit/non_linear/search/nest/test_dynesty.py: add test__single_core_builds_no_pool, parametrised over af.DynestyStatic and af.DynestyDynamic, mirroring test_nautilus.py::test__single_core_builds_no_pool: monkeypatch fork_context on autofit.non_linear.parallel and on the dynesty abstract module (the module imports it inside _fork_pool_cls, so patch both, as the EP no_forking fixture does) to raise AssertionError; 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 on main first and confirm it fails (red → green).
  3. test_autofit/graphical/test_ep_no_multiprocessing.py::test__ep_single_core_search_unchanged: take the no_forking fixture, delete the comment excusing the Pool(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_pool
  • autofit/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_pool to mirror
  • test_autofit/non_linear/search/nest/test_dynesty.py — new regression test
  • test_autofit/graphical/test_ep_no_multiprocessing.py — no_forking fixture, 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

  1. Jammy2211 commented on Sep 15, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    Witness (red → green): test__single_core_builds_no_pool[DynestyStatic|DynestyDynamic] fails on main with AssertionError: fork_context must not be used when number_of_cores == 1 raised from ForkPool.__enter__, and passes on the branch. Note the witness correction from the plan: a use_jax=True analysis was already routed serial by the existing guard, so the failing witness is a plain af.ex.Analysis at number_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 at PYAUTO_TEST_MODE=2, which bypasses the sampler, so searches/nest.py confirms imports and script flow only; the no-pool path is exercised by the unit test at PYAUTO_TEST_MODE=1.
    • Heart readiness: 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's SearchException, as on the force_x1_cpu path.

    Next: /prm once CI is green (merge stays human).

  2. Jammy2211 commented on Sep 15, 2026

    @Jammy2211
    CollaboratorAuthor

    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.

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