Skip to content

fix(nautilus): train neural bounds serially so a dead pool worker cannot hang the fit #1547

Closed

Description

@Jammy2211

Overview

Nautilus.fit_multiprocessing hands its fork-context Pool(number_of_cores) to nautilus as pool=pool, so nautilus uses the same pool for likelihood calls and for sampler calculations, including NeuralNetworkEmulator.train (nautilus/neural.py:96), which pool.maps n_networks sklearn MLP fits. If any worker dies (e.g. a per-job OOM kill on a cluster), multiprocessing.Pool spawns a replacement but never re-issues the lost task, so pool.map blocks forever. Observed 2026-08-29 on RAL: three 8-core subhalo SLaM runs idle for hours at ~0.7 % CPU, py-spy showing main in neural.py:96 train → pool.map, all workers waiting on the task queue, and a _repopulate_pool replacement worker per job.

Plan

  • Pass pool=(pool, None) to nautilus when number_of_cores > 1: likelihoods stay parallel, bound training runs serially in the parent (4 small MLPs on ≤ a few hundred points — negligible cost).
  • Keep the number_of_cores <= 1 path exactly as is (pool=None, fix: single-core Nautilus fits build no pool (release-integrate corrective, #1442) #1443).
  • Unit test that the tuple is passed and that the single-core path still passes None.
  • Comment explaining the dead-worker hang so the tuple is not "simplified" away.
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/nautilus-serial-bound-training

Implementation Steps

  1. autofit/non_linear/search/nest/nautilus/search.py fit_multiprocessing (~L341-352): replace pool=pool with pool=(pool, None) if pool is not None else None (or build sampler_pool next to pool_context), and extend the existing comment block (L332-340) with the dead-worker rationale.
  2. test_autofit/non_linear/search/nest/test_nautilus.py: add test__multi_core_passes_serial_sampler_pool — monkeypatch search.sampler_cls with a stub capturing kwargs (and a run/result stub, or intercept before run), number_of_cores=2, assert kwargs["pool"][1] is None and kwargs["pool"][0] is a pool. Mirror the style of test__single_core_builds_no_pool (L69-104).
  3. Run pytest test_autofit/non_linear/search/nest/test_nautilus.py.

Key Files

  • autofit/non_linear/search/nest/nautilus/search.py — pool construction and sampler kwargs
  • test_autofit/non_linear/search/nest/test_nautilus.py — existing pool tests
  • nautilus sampler.py:281-295 (pool tuple semantics), neural.py:96 (bound training map) — reference only

Original Prompt

Click to expand starting prompt

PyAutoFit: Nautilus multiprocessing — run neural-bound training serially in the parent (pass pool=(pool, None)) so a dead fork-pool worker cannot hang Pool.map during bound construction; keep likelihood evaluation parallel. Found on RAL 2026-08-29 in subhalo_validation job B (py-spy: main in nautilus/neural.py:96 train → pool.map, workers idle, _repopulate_pool replacement worker). Change is at autofit/non_linear/search/nest/nautilus/search.py ~L352 (pool=pool).

Activity

  1. Jammy2211 commented on Aug 29, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    PR: #1548 (branch feature/nautilus-serial-bound-training, commit 1f7808e)

    fit_multiprocessing now passes nautilus pool=(pool, None) — the fork pool stays on likelihood evaluation (pool_l), while sampler calculations including NeuralNetworkEmulator.train run serially in the parent (pool_s = None). The number_of_cores <= 1 path is unchanged (pool=None). The comment block records the dead-worker hang so the tuple is not simplified away.

    Tests (in the task worktree, activate.sh sourced):

    • pytest test_autofit/non_linear/search/nest/test_nautilus.py -q → 5 passed
    • pytest test_autofit/non_linear/search -q -x → 325 passed, 2 skipped

    Heart readiness at ship time: YELLOW (workspace validation not passing (0 failed, 1 timeout, cloud#33229145647: autolens_test scripts/multi_dataset/delaunay_mge.py); stale: release validation incomplete: no rehearsal for current source) — both unrelated to this change. Not merged.

  2. Jammy2211 commented on Aug 30, 2026

    @Jammy2211
    CollaboratorAuthor

    Second root cause on the same code path: the Fitness is pickled into every task

    The serial-bound-training fix (1f7808e) removes the hang, but py-spy on the RAL runs showed a second, independent problem on the same line of nautilus.

    Evidence. Sampling workers were not sitting in the likelihood at all — at minute 22 of a run a worker was inside psf.reversed_kernel building a Convolver, i.e. constructing the dataset, which should happen once per fit and never inside a likelihood evaluation. That is a fresh dataset being materialised per call.

    Cause. Nautilus.fit_multiprocessing has passed a Pool object since #1439 / e6279c5 (2026-07-31, the Py3.14 fork-context pin). In nautilus/pool.py, only an int pool takes the Pool(n, initializer=initialize_worker, initargs=(likelihood,)) branch, after which sampler.likelihood is swapped for the module-level likelihood_worker and tasks carry nothing but parameter vectors. A pool object falls straight through to self.pool.map(func, iterable), and sampler.py calls it as self.pool_l.map(self.likelihood, args) with self.likelihood = fitness.call_wrap. multiprocessing pickles that bound method into every task chunk, so the parent serialises — and each worker unpickles — the entire Fitness: analysis, dataset (with the PSF Convolver), sparse operator, adapt images, grids, for every likelihood call.

    Symptoms this explains.

    • Worker RSS climbing ~5 MB per likelihood call, until 8-core jobs hit the 96 GB cgroup limit and were OOM-killed.
    • Only ~2.5 of 8 cores busy: the parent is pegged pickling the analysis and cannot keep the pool fed.

    Fix (56fb8b6, on feature/nautilus-serial-bound-training): build the fork pool with nautilus's own initialize_worker and initargs=(fitness.call_wrap,) — under fork the initargs are inherited by the child, never pickled — and hand nautilus a small _LikelihoodWorkerPool wrapper whose map ignores the function it is given and dispatches likelihood_worker, restoring exactly what the pool=<int> path did before #1439. pool_s stays None, so the first fix is unchanged.

    Regression test test__likelihood_pool_does_not_pickle_fitness maps a likelihood whose __getstate__ raises through the wrapper on a real fork Pool(2); a control run confirms the same object mapped through a plain pool.map does raise, so the test is not vacuous.

    Commit: 56fb8b63b — PR #1548.

  3. added a commit that references this issue on Aug 30, 2026
  4. Jammy2211 commented on Aug 30, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #1548 (e6ff1e65 on main):

    • _LikelihoodWorkerPool + initialize_worker: nautilus's likelihood pool now dispatches likelihood_worker, so the Fitness (analysis, dataset, PSF, sparse operator, adapt images — 173.7 MB at HST/3.5") is no longer pickled into every task chunk. Measured: +59.5 MB/call/worker linear → flat; per-call 1.7 s → 0.25 s in the harness; on RAL 8-core runs went from ~2.4 to ~6 busy cores with memory flat at ~6 GB/task (was 100 GB → OOM).
    • pool=(pool, None): neural-bound training runs serially in the parent, so a dead worker can no longer hang Pool.map.
      Regression introduced by fix: pin multiprocessing pools to the fork start method (Python 3.14) #1439 (2026-07-31). Tests: 6 nautilus, 326 non_linear/search passing; CI green on 3.12 / 3.13 / nojax.
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