Skip to content

fix(dynesty)+perf(ep): converged search runs once; ndtr in truncated-normal maths (#1642, PR 1 of 3) - #1643

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/ep-factor-search-overhead
Sep 24, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/ep-factor-search-overhead

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

PR 1 of 3 under #1642: cutting per-factor overhead in EP (expectation propagation) factor searches. PR 2 switches the EP visuals, and PR 3 follows with the mapper fast path.

  • 1a. Dynesty finished-detection (0e2c09748). Under the default iterations_per_full_update = 1e99, a Dynesty search that converged on its first run_nested pass was not recognised as finished. It then did an intermediate perform_update(during_analysis=True), a checkpoint restore and a second run_nested that made no likelihood calls. AbstractDynesty.run_search_internal now recognises "dynesty stopped itself inside its call budget" as converged, so every converged run finishes in one pass. The docstring explains why this works: dynesty's per-run call counter resets on each run_nested and it stops only when the counter is strictly above maxcall.
  • 1b. EP message maths (16789050a).
    • TruncatedNormalMessage.log_partition, kl and _normal_gradient_hessian_from now call scipy.special.ndtr through the module-level bindings in _erf_helpers, instead of scipy.stats.norm.cdf. The result is bit-identical but skips the rv_continuous argument-checking overhead.
    • Fitness.manage_quick_update now returns before any per-call bookkeeping when quick updates are disabled (cadence >= ITERATIONS_NEVER, which covers the packaged 1e99 default). This method runs once per likelihood evaluation.

Harness: an ep_toy_gaussian copy with N=5, max_steps=3, nlive=100, rwalk, fully seeded. Baseline (a73684012) and modified runs were interleaved ABAB on the same laptop.

baseline a736840 after 1678905
run_nested calls (15 factor searches) 30 15
intermediate perform_update(during_analysis=True) 15 (4.9 s / 5.3 s total) 0
per-search wall (search.fit, mean) 1.93 s / 2.21 s 1.57 s / 1.83 s (about −18 %)
EP run total 34.2 s / 38.7 s 26.9 s / 30.9 s
parent mean / sigma 51.0432 ± 4.7828 / 9.9075 ± 3.8431 identical (bit-for-bit)
centre moments (5 × mean/std) — max abs diff 0.0
ep_history flags SUCCESS 26, NO_CHANGE 15, BAD_PROJECTION 10 identical

Full evidence, including the harness-determinism notes and the dynesty 2.1.5 semantics probe, is in #1642 (comment).

API Changes

There are no public signature changes. Three things behave differently:

  • A converged Dynesty search (static or dynamic) with a non-null paths now finishes after one run_nested pass. It no longer does the intermediate perform_update, checkpoint restore and second run_nested. Final results are unchanged; the difference is that during_analysis=True output is no longer written for converged runs. Finite-cadence chunking and the global-maxcall stop still work, and a global maxcall now counts as reached when sum(ncall) >= maxcall rather than == maxcall.
  • ITERATIONS_NEVER has moved to autofit.non_linear.fitness. It is still re-exported from autofit.non_linear.search.abstract_search, so existing imports keep working.
  • When quick updates are disabled, Fitness.manage_quick_update returns before updating its running max-likelihood and quick-update counter.

See full details below.

Test Plan

  • test_dynesty.py has 5 new tests (6 cases, since the real-dynesty test is parametrized over static and dynamic):

    • fake-sampler cases: converged, chunk-limited, and global maxcall;
    • a finite-cadence _fit loop (3 runs, 2 intermediate updates);
    • real dynesty, static and dynamic, each with exactly one run_nested and no during_analysis=True update.

    All of them fail on the old code.

  • test__ndtr_swap_bit_identical checks 72 grid cases, including infinite limits. It asserts exact equality against scipy.stats.norm.cdf for _norm_cdf, log_partition, _normal_gradient_hessian_from and kl.

  • test__disabled_cadence_returns_before_any_bookkeeping (1e99 and 1e100) fails on the old code.

  • Full suite, run serially (pytest test_autofit -p no:xdist -x): 2881 passed, 2 skipped.

  • EP harness: the answer is bit-for-bit identical to baseline, with 15 fewer run_nested calls and about 18 % less wall time per search.

Full API Changes (for automation & release notes)

Moved

  • ITERATIONS_NEVER (value 1e90): moved from autofit.non_linear.search.abstract_search to autofit.non_linear.fitness. It is re-exported from abstract_search, so from autofit.non_linear.search.abstract_search import ITERATIONS_NEVER still works.

Changed Behaviour

  • AbstractDynesty.run_search_internal(search_internal, ...): the "finished" criteria are now checked in this order:

    1. NullPaths;
    2. global maxcall set and sum(results.ncall) >= maxcall (previously == maxcall);
    3. the pass added no more calls than its budget iterations, meaning dynesty stopped itself on dlogz, maxiter or similar;
    4. legacy: the pass made no new calls.

    In practice, a converged search with default config finishes in one pass, with no intermediate perform_update(during_analysis=True), checkpoint restore or second run_nested. A pass that overruns a finite iterations_per_full_update chunk still loops through perform_update.

  • Fitness.manage_quick_update(parameters, log_likelihood): when iterations_per_quick_update >= ITERATIONS_NEVER, it returns immediately without updating quick_update_max_lh or quick_update_count (previously only None short-circuited). Those values only ever fed the quick-update body, which cannot fire at that cadence.

  • TruncatedNormalMessage.log_partition, .kl, ._normal_gradient_hessian_from: these now use scipy.special.ndtr via autofit.mapper.prior._erf_helpers._norm_cdf in place of scipy.stats.norm.cdf. The output is bit-identical.

  • autofit.mapper.prior._erf_helpers._norm_cdf / _norm_ppf (private): the NumPy branch now uses module-level scipy.special.ndtr / ndtri bindings.

Migration

  • None required.

Heart RED override (development only)

This PR is opened under the PyAutoBrain/AUTONOMY.md "Human override for Heart RED (development only)". It is a development-shipping override only. It does not claim to fix Heart, and Heart remains RED for release purposes. Merge still needs a separate explicit human /prm with every required check green.

  • Authorisation (live human, 2026-09-24, Claude Code CLI session): the human selected "Yes, override and open PR 1" to the question authorising the development-only Heart RED override for PR 1 of refactor(ep): cut per-factor-search wrapper overhead (dynesty single pass, EP visuals off, mapper fast path) #1642.
  • Exact current RED reasons (pyauto-heart readiness --json, verdict red, score 45, ts 2026-09-24T15:11:12.973142+00:00):
    1. release validation FAILED (stage integrate)
    2. workspace validation not passing (4 failed, cloud#35579888156: autolens notebooks/cluster/modeling.ipynb, autolens notebooks/weak/a2744.ipynb, autolens scripts/cluster/modeling.py, +1 more)
    3. manifest drift: hub organism blurb (organs present) — 7 mismatch(es) vs PyAutoMind/repos.yaml
  • Branch gates passed: full serial test_autofit 2881 passed / 2 skipped on head 1678905; autofit workspace smoke 8/8 scripts + 2/2 notebooks; PyAutoFit main CI green; downstream workspace impact (iii) none.
  • Permitted scope: commit, push, and opening this pending-release PR. No merge, release or release rehearsal, and no bypassing of tests, checks or branch protection.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

Jammy2211 and others added 2 commits September 24, 2026 16:47
run_search_internal only treated a pass as final when it added zero
likelihood calls, so every converged dynesty search ran an intermediate
perform_update, a checkpoint restore and a second no-op run_nested.

dynesty's per-run call counter resets on each run_nested and stops on the
budget only once it is strictly above maxcall, while sum(results.ncall)
grows by at least that counter. A pass that added no more calls than the
budget it was handed therefore stopped on dynesty's own criterion. The
finish test is now: NullPaths; global maxcall reached; dynesty stopped
under budget; legacy no-progress. Finite iterations_per_full_update
cadences still loop through perform_update.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… updates (#1642)

- TruncatedNormalMessage.log_partition / kl / _normal_gradient_hessian_from use
  scipy.special.ndtr via _erf_helpers._norm_cdf instead of scipy.stats.norm.cdf:
  the same Cephes routine without the rv_continuous wrapper, bit-identical
  (test__ndtr_swap_bit_identical, incl. infinite limits). truncnorm.stats is
  untouched. _erf_helpers binds ndtr/ndtri once at module level for NumPy.
- Fitness.manage_quick_update returns before any bookkeeping when
  iterations_per_quick_update is None or >= ITERATIONS_NEVER (the packaged
  1e99 default); ITERATIONS_NEVER moves to fitness.py and is re-exported from
  abstract_search.py for existing importers.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant