Skip to content

feat(analysis): LATENT_BATCH_MODE attribute (vmap default, jit option) - #1288

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/euclid-einstein-radius-zero-contour
May 21, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/euclid-einstein-radius-zero-contour

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented May 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds an Analysis.LATENT_BATCH_MODE class attribute that controls how compute_latent_samples wraps compute_latent_variables on the JAX path. Default is "vmap" (existing behaviour). Subclasses opt into "jit" (per-sample jax.jit) when their inner code calls JAX primitives that upstream documents as vmap-incompatible.

The motivating use case is lensing latents: LensCalc.einstein_radius_jit_from (added in the companion PyAutoGalaxy PR) wraps jax_zero_contour.ZeroSolver.zero_contour_finder, which upstream explicitly warns must not be combined with jax.vmap (uses jax.lax.cond / jax.lax.while_loop for early termination). PyAutoGalaxy's AnalysisDataset sets LATENT_BATCH_MODE = "jit" so all PyAutoGalaxy/PyAutoLens analyses inherit the per-sample jit path automatically; non-lensing PyAutoFit users keep their current vmap behaviour because the default is unchanged.

API Changes

New public class attribute Analysis.LATENT_BATCH_MODE: str (default "vmap"). Two values supported: "vmap" and "jit". Other values raise ValueError at compute_latent_samples time with a clear message.

See full details below.

Test Plan

  • New unit test: LATENT_BATCH_MODE defaults to "vmap" (existing subclasses unchanged).
  • New unit test: invalid LATENT_BATCH_MODE value raises ValueError with a clear message.
  • pytest test_autofit/analysis/ — 18 passed locally, 1 skipped.
  • End-to-end validation via the Euclid pipeline workspace PR (PyAutoLabs/euclid_strong_lens_modeling_pipeline#14) — confirms LATENT_BATCH_MODE = "jit" propagates through AnalysisDataset and produces a finite Einstein radius latent (2.1002 arcsec) on a real fit.
Full API Changes (for automation & release notes)

Added

  • Analysis.LATENT_BATCH_MODE: str — class attribute, default "vmap". Controls the JAX dispatch strategy in compute_latent_samples.

Changed Signature

  • None.

Changed Behaviour

  • Analysis.compute_latent_samples — when use_jax=True, branches on self.LATENT_BATCH_MODE:
    • "vmap" (default, unchanged behaviour): jax.jit(jax.vmap(compute_latent_for_model)).
    • "jit": per-sample jax.jit(compute_latent_for_model) with a Python loop over samples; JIT cache is reused across samples.
    • Other values: raise ValueError.

Migration

  • None required for existing subclasses (default is "vmap").
  • Lensing-style subclasses where compute_latent_variables calls into jax_zero_contour or other vmap-incompatible JAX code should set LATENT_BATCH_MODE = "jit".

Companion PRs

  • PyAutoGalaxy: https://github.com/PyAutoLabs/PyAutoGalaxy/pull/435 — adds LensCalc.einstein_radius_jit_from and sets AnalysisDataset.LATENT_BATCH_MODE = "jit".
  • Workspace: https://github.com/PyAutoLabs/euclid_strong_lens_modeling_pipeline/pull/15 — wires the Euclid effective_einstein_radius latent through the new path.

Closes PyAutoLabs/euclid_strong_lens_modeling_pipeline#14 alongside the companion PRs.

🤖 Generated with Claude Code

Adds Analysis.LATENT_BATCH_MODE class attribute that controls how
`compute_latent_samples` dispatches the JAX path:

- "vmap" (default): jax.jit(jax.vmap(compute_latent_for_model)) —
  unchanged behaviour, parallel-batched across samples.
- "jit": per-sample jax.jit with the JIT cache reused across samples.
  Use when the inner `compute_latent_variables` calls JAX code that
  upstream documents as vmap-incompatible (e.g. `jax_zero_contour`
  ZeroSolver uses lax.cond / lax.while_loop early-termination that
  is not safe under jax.vmap).

Subclasses opt into jit-per-sample by setting
`LATENT_BATCH_MODE = "jit"`. PyAutoGalaxy's AnalysisDataset does this
because lensing latents (Einstein radius via zero-contour) need
ZeroSolver under the hood. The default stays "vmap" so existing
non-lensing pyautofit users keep their batched behaviour unchanged.

Invalid values raise ValueError with a clear message instead of
silently falling through.

Companion PR: PyAutoLabs/PyAutoGalaxy#<TBD> adds the lensing helper
and the AnalysisDataset override. Workspace PR:
PyAutoLabs/euclid_strong_lens_modeling_pipeline#14 wires the
Euclid effective_einstein_radius latent through the new path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 21, 2026
@Jammy2211
Jammy2211 merged commit fe8f6d9 into main May 21, 2026
7 checks passed
@Jammy2211
Jammy2211 deleted the feature/euclid-einstein-radius-zero-contour branch May 21, 2026 19:53
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
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.

feat(latents): re-enable effective_einstein_radius via zero_contour

1 participant