Skip to content

af.SMC: blackjax adaptive tempered SMC search #1544

Closed

Description

@Jammy2211

Context

The JAX-native posterior sampler wave validated a gradient SMC prototype on the autolens_workspace_developer branch feature/blackjax-smc-gradient-kernel (searches_minimal/blackjax_smc_grad.py, _warm_start.py, smc_gradient_findings.md). Warm-started it samples: acceptance 0.80 → 0.17 across the tempering path, einstein_radius 1.5998 against a truth of 1.6, and all three warm arms' log Z bracket the Nautilus bar within ±0.8 nats — a gradient sampler that also yields the evidence.

That prototype lives in a workspace script. It has to become a first-class PyAutoFit search (af.SMC) before the profiling workspace can build an SMC cell and put SMC runs on the A100.

Prompt: PyAutoMind/active/blackjax_smc_search.md. Rung 2 of the overflow-flood fix wave (rung 1 is #1543).

High-level plan

  • Add af.SMC, a BlackJAX adaptive tempered SMC search with a gradient inner kernel (MALA by default, HMC optional), sitting beside the existing af.BlackJAXNUTS under autofit/non_linear/search/mcmc/blackjax/.
  • Sample in a whitened space. Warm-started, whiten by the Cholesky factor of a previous result's full covariance (prior-whitening does not whiten the posterior — 269× anisotropy measured); cold, whiten by the prior's per-parameter quantile width.
  • Keep SMC's free log-evidence valid under a warm start by tempering from a normalised Gaussian reference rather than dropping particles at the MLE: λ still starts at 0, and the bridge weight restores the true log Z.
  • Scale the inner-kernel step to the posterior width, honouring MALA's squared-length step_size. No per-temperature kernel adaptation in this wave — it collapses acceptance (phase-0 finding).
  • Record the λ schedule, per-step acceptance and ESS in search_internal so a run is judged by the acceptance trace, never by "tempering reached λ=1".
  • Test the evidence against an analytic Gaussian target, plus warm/cold/test-mode shape contracts; add the docs autosummary entry and a release-notes feature entry.

Detailed plan

New package autofit/non_linear/search/mcmc/blackjax/smc/:

  • search.py — class SMC(AbstractMCMC), __identifier_fields__ = ("num_particles", "kernel", "num_mcmc_steps", "target_ess", "inverse_mass_matrix").
    • __init__(num_particles=256, kernel="mala", num_mcmc_steps=5, num_integration_steps=8, target_ess=0.5, step_size=None, whiten_inflate=2.0, max_smc_steps=200, seed=42, initializer=None, inverse_mass_matrix=None, ...). Default initializer is InitializerPrior() (cold SMC's evidence requires particles drawn from the prior — the AbstractMCMC InitializerBall default would silently invalidate it).
    • _fit: refuses a non-JAX Analysis (same message shape as BlackJAXNUTS); builds Fitness(fom_is_log_likelihood=True); draws start points via initializer.samples_from_model; resolves whitening via chains.resolve_inverse_mass_matrix; assembles blackjax.adaptive_tempered_smc with a MALA/HMC inner kernel; runs the tempering loop in Python, calling perform_update on the iterations_per_full_update chunk boundary and pickling search_internal each step.
    • Cold path: logprior_fn = Σ log_prior_from_value(x(z)), loglikelihood_fn = fitness.call(x(z)); log Z = Σ log_likelihood_increment (the SMC estimator is a ratio, so the whitening Jacobian and the prior normalisation cancel).
    • Warm path (whitening spec given): logprior_fn = log N(0, I)(z), loglikelihood_fn = log prior_normalised + log L − log g; log Z = Σ increments + log|det L|.
    • apply_test_mode shrinks to num_particles<=16, num_mcmc_steps=2, max_smc_steps=5.
    • samples_info_from / samples_via_internal_from mirror the NUTS pattern; particles at λ=1 carry their normalised tempered weights.
  • samples.py — SamplesSMC(SamplesMCMC) overriding log_evidence to read samples_info["log_evidence"] (SamplesMCMC.log_evidence is None).

Shared/edited:

  • autofit/__init__.py — "SMC" in _LAZY_ATTRS.
  • docs/api/searches.rst — SMC (and BlackJAXNUTS) under the MCMC autosummary.
  • docs/release_notes.md (or the repo's notes file) — new-feature entry.

Tests — test_autofit/non_linear/search/mcmc/test_blackjax_smc.py, following test_blackjax_nuts.py's requires_blackjax skipif so the no-extras matrix leg stays green:

  • explicit-params / identifier-fields / test-mode construction tests (no jax needed).
  • _build_search_internal shape contract (pure numpy).
  • End-to-end Gaussian target: uniform priors, a JAX-traceable Gaussian likelihood; assert the recovered log_evidence is within 0.1 nat of 0.5·d·log(2π) + d·log σ − Σ log(b−a), for both a cold run and a warm run seeded with an explicit covariance.

Trade-offs / risks: no per-temperature step adaptation (measured to collapse acceptance); the cold auto step targets a fixed fraction of the prior width, matching the prototype default, and cold runs on a sharply peaked likelihood are documented as not the representative regime. The config/non_linear/mcmc/SMC.yaml item in the wave plan is dropped: PyAutoFit #1202 replaced per-search YAML with explicit Python defaults, and BlackJAXNUTS carries no YAML either.

Branch: feature/blackjax-smc-search

Activity

  1. Jammy2211 commented on Aug 29, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    PR: #1546 (feature/blackjax-smc-search, commit b1e3b02, label pending-release)

    CI: all four legs green — unittest (3.12) pass, unittest (3.13) pass, unittest-nojax pass, docs / docs-build pass.

    What landed

    af.SMC — a BlackJAX adaptive tempered SMC search with a MALA (default) or HMC gradient inner kernel, plus SamplesSMC. Lazily exported via _LAZY_ATTRS, so nothing at import time pulls jax or blackjax.

    The evidence is asserted against a closed form (isotropic Gaussian likelihood under Uniform(-5, 5) priors, analytic log Z = −2.7673):

    arm recovered log Z Δ acceptance trace
    cold (prior-whitened) −2.8493 −0.082 0.88 → 0.96
    warm (bridge + Jacobian) −2.7869 −0.020 0.80 → 0.24
    HMC warm −2.7532 +0.014 0.96 → 0.96

    All inside the 0.1-nat bar; the HMC arm reproduces the phase-0 finding that HMC holds acceptance where MALA's decays. Full suite 2336 passed / 3 skipped; Sphinx warning count 31, exactly the recorded baseline.

    Deviation from the wave plan

    The planned config/non_linear/mcmc/SMC.yaml + search.yaml entry was dropped: that layout no longer exists — #1202 ("replace YAML config with explicit Python defaults in search classes") removed it, and BlackJAXNUTS carries no YAML either. Defaults are explicit Python arguments, mirroring the sibling search. Release-notes content is the PR's ## API Changes section (PyAutoFit has no in-repo notes file).

    Downstream impact

    Purely additive — no existing signature, default or behaviour changes, so no workspace migration is needed and no smoke test is implicated (nothing downstream calls af.SMC yet). The workspace half is Wave B: build_smc in autolens_profiling's _samplers.py, the scripts/imaging/searches/smc/mge.py leaf, and the A100 warm mala/hmc probe — all gated on this merging and on HPCPullPyAuto refreshing the RAL stack.

    Ship gate

    Heart readiness was YELLOW, not GREEN, on two pre-existing reasons unrelated to this change:

    • workspace validation not passing (0 failed, 1 timeout, cloud#33229145647: autolens_test scripts/multi_dataset/delaunay_mge.py)
    • release validation incomplete: no rehearsal for current source

    PR-open only; merge is human (/prm).

  2. Jammy2211 commented on Aug 29, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged as PR #1546 (b70cf7f). af.SMC — blackjax adaptive tempered SMC (mala/hmc inner kernel, no step adaptation by default, full-covariance whitening from a warm-start Result/Samples, prior-drawn cold start) with SamplesSMC (log_evidence, lambda/acceptance/ESS traces). Analytic Gaussian: cold −0.082 / warm −0.020 / hmc +0.014 nat. Profiling cell + A100 probe follow in autolens_profiling (Wave B).

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