feat: PointSolver(use_jax=True) + autolens.jax.register_tracer_classes#538
Merged
Conversation
Adds use_jax constructor flag to PointSolver (via AbstractSolver). When True, solve() defaults xp=jnp and remove_infinities=False, honouring the JAX static-shape contract for jit-compatible point-source solves. Also adds autolens.jax module with register_tracer_classes(tracer), the explicit pytree-registration entry point users call once before wrapping solver.solve in their own @jax.jit. The walker mirrors the existing AnalysisImaging._register_fit_imaging_pytrees pattern for the Tracer + Galaxy + profile classes a concrete tracer carries. First PR of Phase 2 in z_features/jax_user_intro.md. Follow-ups: SimulatorImaging.use_jax=True, SimulatorInterferometer.use_jax=True, and the xp=np + jnp-grid mismatch ValueError land in subsequent PRs. Design doc: admin_jammy/notes/jax_interface.md Issue: PyAutoArray#334 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
First PR of Phase 2 of
z_features/jax_user_intro.md. Addsuse_jax=Trueconstructor flag toPointSolver, which makessolver.solve(tracer, source_plane_coordinate)JAX-native by default —xpfalls back tojnpandremove_infinitiesdefaults toFalsefor static-shape JIT compatibility.Also adds a new
autolens.jaxmodule exposingregister_tracer_classes(tracer), the one-line setup users call once before wrapping the solver in their own@jax.jit. The walker mirrors the auto-registration thatAnalysisImaging._register_fit_imaging_pytrees()already performs on the modeling path.Authoritative design doc:
admin_jammy/notes/jax_interface.md(admin_jammy mainf381393).API Changes
PointSolver(use_jax=False)constructor flag, forwarded byfor_grid()andfor_limits_and_scale().PointSolver._xpproperty (returnsjnpifuse_jax, elsenp).PointSolver.solve(xp=None, remove_infinities=None)— both default toNone, meaning "auto fromself.use_jax". Existing explicit values still honoured.autolens.jaxmodule +register_tracer_classes(tracer)public function. Users call once before@jax.jitto registerTracer+Galaxy+ concrete profile classes as JAX pytrees.See full details below.
Test Plan
test_autolens/point/triangles/test_use_jax.py— constructor wiring, defaults,tree_flatten/tree_unflattenroundtrip withuse_jax, defaultremove_infinitieson NumPy path.autolens_workspace_test/scripts/point_source/solver_use_jax_parity.pyconfirms NumPy and JAX positions agree toatol=1e-3, including inside@jax.jit. Script ships in a follow-up workspace_test PR since it depends on this library API landing first.Full API Changes (for automation & release notes)
Added
autolens.jax(new module).autolens.jax.register_tracer_classes(tracer)— register every concrete class reachable fromtracer(Galaxy + light/mass/point profiles + Tracer itself) as a JAX pytree. Idempotent. No-op if JAX not installed.PointSolver(use_jax=False)constructor parameter (defined onAbstractSolver).PointSolver._xpproperty — returnsjax.numpyifself.use_jax, elsenumpy.PointSolver.for_grid(use_jax=False)andPointSolver.for_limits_and_scale(use_jax=False)— forwarding.Changed signature
PointSolver.solve(tracer, source_plane_coordinate, xp=None, plane_redshift=None, remove_infinities=None)—xpandremove_infinitiesnow default toNone. Behaviour:xp=None→ usesself._xp(i.e.,jnpif constructed withuse_jax=True, elsenp).remove_infinities=None→ defaults tonot self.use_jax(stripsinfrows on NumPy path; keeps them on JAX path for static-shape JIT compatibility).xp=...andremove_infinities=...are still honoured.Changed behaviour
AbstractSolver.tree_flatten/tree_unflattennow includeuse_jaxin aux_data so the flag round-trips across JAX pytree boundaries.Migration
solver.solve(tracer, coord),for_grid(...)) continue to work unchanged —use_jaxdefaults toFalse.cluster/simulator.pypattern ofregister_instance_pytree(Tracer, ...)+register_model(af.Collection(...))) becomes unnecessary once Phase 2 PRs 2-4 land.Out of scope (separate PRs)
SimulatorImaging.use_jax=True+ xp-conversion of simulator internals (next PR).SimulatorInterferometer.use_jax=True+via_image_fromsignature fix.xp=np+ jnp-backed-grid mismatchValueErrorinAbstractMaker.__init__(PyAutoArray-only).🤖 Generated with Claude Code