Skip to content

feat: add autofit.jax opt-in pytree registration API - #1220

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/imaging-mge-pytree-migration
Apr 16, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/imaging-mge-pytree-migration

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Introduce autofit.jax.enable_pytrees() and autofit.jax.register_model(model) so callers can register PyAutoFit prior/model classes — and the user classes referenced by Model.cls (e.g. Gaussian, Galaxy) — as JAX pytree nodes on demand.

PyAutoFit deliberately does not register these classes at import time (the Nov 2025 cleanup removed eager register_pytree_node_class calls to keep JAX an optional dependency). This PR re-enables the functionality without bringing the eager JAX import back: the registration is lazy and opt-in.

The killer feature is register_model(model) → _build_instance_pytree_funcs(model), which partitions each Model.cls constructor's arguments:

  • Constants from the model definition (e.g. af.Constant, Galaxy(redshift=0.5)) → JAX aux_data, so they stay as concrete Python values inside a jax.jit trace.
  • Prior-derived arguments → children, so they become JAX tracers.

Without this partition, constants would become tracers and code like sorted(galaxies, key=lambda g: g.redshift) in Tracer would raise TracerBoolConversionError under JIT.

API Changes

One new public sub-package autofit.jax with two functions: enable_pytrees() registers the library's prior/model classes with jax.tree_util; register_model(model) walks a Model / Collection and registers every Model.cls it finds.

Both are no-ops if JAX is missing. Both are idempotent — re-registering a class is silently tolerated. See full details below.

Test Plan

  • pytest test_autofit/ passes (1223 tests)
  • New test_autofit/jax/test_enable_pytrees.py covers: round-trip flatten/unflatten, JIT compatibility, constants-stay-static under JIT, idempotency, and Collection round-trip
  • Verified end-to-end in autolens_workspace_developer/jax_profiling/imaging/mge.py — jax.jit(jax.vmap(full_pipeline_from_params)) runs with pytree inputs
Full API Changes (for automation & release notes)

Added

  • autofit.jax — new sub-package.
  • autofit.jax.enable_pytrees() -> bool — registers GaussianPrior, UniformPrior, LogGaussianPrior, LogUniformPrior, TruncatedGaussianPrior, Model, Collection, ModelInstance with jax.tree_util. Returns True on success, False if JAX is missing. Idempotent.
  • autofit.jax.register_model(model) -> bool — calls enable_pytrees() then walks model (a Model or Collection) and registers each concrete Model.cls. The per-class flatten partitions constants (from model.direct_instance_tuples) into aux_data and priors into children. Returns True on success, False if JAX is missing. Idempotent.

Removed / Renamed / Changed Signature / Changed Behaviour

None.

Migration

Previously, callers who wanted JAX integration had to hand-register classes themselves (e.g. jax.tree_util.register_pytree_node_class(af.GaussianPrior)). Now:

  • Before: ad-hoc jax.tree_util.register_pytree_node_class(...) calls per class
  • After: import autofit as af; af.jax.register_model(model) before crossing a jit / vmap boundary

Legacy hand-registration still works; register_model tolerates classes already registered.

🤖 Generated with Claude Code

Introduces autofit.jax.enable_pytrees() and autofit.jax.register_model(model)
so callers can register PyAutoFit prior/model classes (and the user classes
referenced by model.cls) with jax.tree_util without paying an eager JAX
import at every "import autofit".

register_model walks a Model / Collection and builds per-class flatten /
unflatten functions that partition constructor arguments: constants (from
model.direct_instance_tuples) go into JAX aux_data so they stay as concrete
Python values inside jit traces, while prior-derived arguments become
children (and therefore tracers). This keeps constants like Galaxy.redshift
usable in Python control flow (e.g. sorted(...) by redshift) under JIT.

Both functions are no-ops if JAX is not installed. Re-registration of a
class that is already registered is tolerated silently.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autolens_workspace_developer#11

@Jammy2211
Jammy2211 merged commit 09b6e34 into main Apr 16, 2026
2 checks passed
@Jammy2211
Jammy2211 deleted the feature/imaging-mge-pytree-migration branch April 16, 2026 20:56
@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.

1 participant