Skip to content

feat: number_of_cores="auto", parallel banner and factor-graph setups (5D) #1794

Done

Description

@Jammy2211

Overview

Likelihood-lifecycle epic, phase 5D (PyAutoFit). This adds opt-in number_of_cores="auto", resolved at fit start after the analysis has resolved its compute setup. It replaces the stale fit-start JAX/cores line with one honest parallel banner, records the resolved parallelism and setup in the search metadata, and makes factor graphs forward modify_before_fit to each factor's analysis, so setup= works inside af.FactorGraphModel with an agreement check.

Plan

  • Accept number_of_cores="auto" on SettingsSearch and every search, validating an int ≥ 1 or "auto". The default stays 1 (R6).
  • Resolve "auto" in PoolFactory, after modify_before_fit, to the detected cores (SLURM_CPUS_PER_TASK → affinity → cpu_count); 1 for JAX (fork rule) and EP factor searches. Cap it by min(cores / cpus_per_eval, available memory / peak_rss) only when the analysis supplies resources_per_eval().
  • One banner line after modify_before_fit: Parallel: N workers (requested auto; capped by cores|memory|JAX) · setup <setup_id>. The stale log_fit_start claim is removed.
  • Metadata: the resolved count, cap reason and setup_id go into search.summary and files/parallel.json; search.json keeps the request.
  • Factor graphs: FactorGraphModel.modify_before_fit forwards to each factor's analysis (its own model and sub-paths, rebinding both analysis references), and check_setup_agreement sits beside check_backend_agreement.

Design calls (human-approved 2026-11-24):

  1. No η-based cap in 5D: the only measured curve (imaging/rectangular numba) has cores × η rising at every step up to 32, so throughput favours all cores.
  2. The memory cap is a hook (duck-typed analysis.resources_per_eval()), live once 5E ships runtime_estimates.yaml, plus an available-memory read (MemAvailable, falling back to total).
  3. "auto" is validated everywhere; the thread warning is deferred until the count is resolved.
  4. Factor-graph forwarding lives in PyAutoFit; Galaxy/Lens are untouched. The Lens SetupException guard stays for EP.
  • Tier + merge mode: effective level: supervised; merge mode: human /prm
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/likelihood-lifecycle-p5d-fit

Implementation Steps

  1. Validation:
    • SettingsSearch.__init__ (autofit/non_linear/settings.py:16) and NonLinearSearch.__init__ (abstract_search.py:98, :168-169) accept an int ≥ 1, None (→ 1) or "auto" (case-insensitive), and raise a clear error otherwise.
    • warn_if_threads_unpinned (runtime.py:97-114) only runs for an int, and runs for the resolved count at fit start.
    • Searches that pass number_of_cores through (dynesty static/dynamic, nautilus, zeus, blackjax, abstract_mcmc) keep working with "auto".
  2. Resolution:
    • Add resolve_number_of_cores(requested, analysis, is_jax, where) (parallel/pool.py, next to effective_number_of_cores). For "auto": detected cores via autonerves.environment.detect_cores. JAX gives 1; EP factor searches give 1 (check_factor_search_cores).
    • If getattr(analysis, "resources_per_eval", None) returns cpus_per_eval / peak_rss_gb, cap by min(cores // cpus_per_eval, floor(available_gb / peak_rss_gb)), never below 1.
    • Return (n, reason).
    • PoolFactory uses it, so .number_of_cores, .summary and __call__ all use the resolved count.
  3. Available memory: a small helper (autofit or autonerves.environment if appropriate; autofit-local if simpler) reading /proc/meminfo MemAvailable, then cgroup v2 memory.max when lower, then total RAM.
  4. Banner: after modify_before_fit (abstract_search.py _fit_and_output ~:317, or in start_resume_fit after PoolFactory), log Parallel: N workers (requested R; <reason>) · setup <setup_id>. The setup_id comes from getattr(analysis, "setup", None), duck-typed with no autoarray import. Remove or trim the stale count/JAX claim in log_fit_start (runtime.py:242-263), keeping anything else it reports.
  5. Metadata: search._parallel_summary / PoolFactory.summary includes the resolved count, request, reason and setup_id, and paths.save_json("parallel", {...}) writes files/parallel.json (directory and database paths).
  6. Factor graphs:
    • FactorGraphModel.modify_before_fit(paths, model) (graphical/declarative/collection.py) loops over model_factors with sub-paths (paths.for_sub_analysis("analyses/analysis_i"), as _for_each_analysis does) and each factor's own prior model. It calls factor.analysis.modify_before_fit(child_paths, factor_model), rebinds factor.analysis and the FactorCallable.analysis (factor/analysis.py:21, :74) to the returned object, and returns self.
    • check_setup_agreement (collection.py, next to :111-142) compares getattr(analysis, "setup", None).setup_id across factors, ignoring None, and raises a clear error on disagreement. It is called at the end of the forwarding and in preflight (search/preflight.py:345-351).
    • Confirm HierarchicalFactor / _flat_factors are handled.
  7. Tests:
    • test_autofit/non_linear/test_fork_rule.py and test_parallel.py: auto resolution, JAX → 1, EP → 1, memory cap with a fake resources_per_eval, a cgroup/MemAvailable helper with fake files, validation errors.
    • search/test_abstract_search.py: "auto" through SettingsSearch and constructors; the warning is deferred.
    • test_logging.py / test_capabilities.py: banner and summary.
    • paths tests: parallel.json.
    • graphical: forwarding calls each factor's modify_before_fit once with its model and sub-paths, rebinds both references, and agreement passes or fails. A dummy analysis with setup_id-carrying setup attributes; no autoarray dependency.
  8. Full suite plus pyauto-heart smoke --affected-by PyAutoFit.

Key Files

  • autofit/non_linear/settings.py, autofit/non_linear/search/abstract_search.py, autofit/non_linear/runtime.py
  • autofit/non_linear/parallel/pool.py, autofit/non_linear/capabilities.py, autofit/non_linear/paths/*
  • autofit/graphical/declarative/collection.py, autofit/graphical/declarative/factor/analysis.py, autofit/non_linear/search/preflight.py

Original Prompt

Click to expand starting prompt

Likelihood lifecycle phase 5D: PyAutoFit number_of_cores="auto", a post-setup parallel banner, parallel metadata, and factor graphs that forward modify_before_fit with a setup-agreement check

Type: feature
Target: PyAutoFit
Repos:

  • PyAutoFit
    Themes:
  • hpc-gpu
  • profiling
    Difficulty: large
    Autonomy: supervised
    Priority: high
    Status: draft
    Consequence: judge
    Witness: PyAutoFit main accepts number_of_cores="auto" on SettingsSearch and every search constructor, validating an int ≥ 1 or "auto". The default stays 1 (R6). "auto" is resolved in PoolFactory, after analysis.modify_before_fit, to the detected cores (SLURM_CPUS_PER_TASK → CPU affinity → cpu_count), and to 1 for JAX analyses (the existing fork rule) and for EP factor searches. When the analysis supplies resources_per_eval() (cpus_per_eval, peak_rss_gb), the count is capped at min(cores / cpus_per_eval, available memory / peak_rss); otherwise there is no cap. One banner line after modify_before_fit reports the effective workers, the request, any cap reason and the setup_id; the stale log_fit_start JAX/cores claim is gone. The resolved count, cap reason and setup_id are written to search.summary and files/parallel.json. FactorGraphModel.modify_before_fit forwards to each factor's analysis (its own model and sub-paths, rebinding both analysis references), and a check_setup_agreement beside check_backend_agreement refuses graphs whose factors resolved different setups. Full test suite green; pyauto-heart smoke --affected-by PyAutoFit passes.
    Review-minutes: 30
    Unattended: no
    Lane: any
    Gate: 5C-3 MERGED (PyAutoGalaxy#753, PyAutoLens#886); 5C setup resolution in modify_before_fit is on Galaxy/Lens main. Plan and design calls approved by the human 2026-11-24.
    Epic: likelihood-lifecycle
    Phase: 5
    Parent: draft/research/autolens_profiling/likelihood_lifecycle_epic.md
    Filed: 2026-11-24

Context

  • Report §3.5, PyAutoFit row: SettingsSearch(number_of_cores="auto") → setup threads; the fork rule is unchanged; the banner is printed at fit start; the resolved setup is stored in the search metadata. The node-throughput amendment caps workers at min(cores / cpus_per_eval, memory / peak_rss).
  • Survey 2026-11-24:
    • number_of_cores is unvalidated (settings.py:16; abstract_search.py:98, :168-169).
    • warn_if_threads_unpinned compares <= 1 (runtime.py:103), so "auto" would raise in the constructor.
    • Fork rule: effective_number_of_cores (parallel/pool.py:60-90). PoolFactory (:137-230) is built in start_resume_fit (abstract_search.py:424) after modify_before_fit. log_fit_start (runtime.py:242-263) runs before it (:307 vs :317).
    • Metadata: search.json comes from to_dict(search) (paths/directory.py:382-401); search.summary from run_summary_from → search._parallel_summary (capabilities.py:281-287, abstract_search.py:425).
    • Factor graphs: FactorGraphModel inherits the no-op modify_before_fit (analysis.py:301-309). There are two analysis references, AnalysisFactor.analysis (factor/analysis.py:74) and FactorCallable.analysis (:21). Preflight runs check_backend_agreement (search/preflight.py:326-356; collection.py:111-142).
    • Phase 4 measured η only for imaging/rectangular numba (autolens_inference scripts/misc/wall/model.py:99: 1.0, 0.68, 0.62, 0.55, 0.36, 0.28 at 1–32 cores). Cores × η is monotone increasing, so throughput favours all cores.
    • peak_rss_gb exists only in autolens_profiling runtime_estimates.yaml, which no library reads until 5E. Nerves has total ram_gb but no available-memory probe.
  • Design calls (human-approved 2026-11-24):
    1. No η-based cap in 5D. The one measured curve favours all cores; record η context in the metadata note.
    2. The memory cap applies only when the analysis supplies resources_per_eval(). It goes live when 5E ships runtime_estimates.yaml. Add an available-memory read: MemAvailable, cgroup-aware if simple, falling back to total.
    3. "auto" is accepted and validated everywhere; the thread warning is deferred to resolution.
    4. Factor-graph setup forwarding lands in PyAutoFit; Galaxy/Lens are untouched. The PyAutoLens SetupException guard stays for EP, which never calls modify_before_fit.

Out of scope

  • The runtime estimator and copying runtime_estimates.yaml into PyAutoLens (5E).
  • Workspace prose (5F).
  • Flipping the default to auto (R6).
  • Slimming the Analysis setup to delegate numpy operators to sparse_operator_for (at the R6 flip).

Activity

  1. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    5D shipped as #1795 (pending-release): number_of_cores="auto" (validated; resolved after modify_before_fit), Parallel: banner, files/parallel.json, factor-graph modify_before_fit forwarding with check_setup_agreement. Gate: 4376 passed, Galaxy/Lens compat green, Heart dev GREEN, smoke 8/8.

  2. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    #1795 (23b7dfe): number_of_cores="auto" (validated; resolved after modify_before_fit; JAX/EP → 1; memory cap via resources_per_eval()), the Parallel: banner, files/parallel.json, and factor-graph modify_before_fit forwarding with check_setup_agreement. Pending release. Next: 5E (runtime estimator; PyAutoLens implements resources_per_eval() so the memory cap goes live).

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