refactor(jax_profiling): migrate imaging/pixelization.py to pytree inputs#12
Merged
Merged
Conversation
…puts Replaces the flat-vector JIT/vmap pattern (model.instance_from_vector called inside the trace) with direct pytree-instance inputs, matching the mge.py migration shipped in autolens_workspace_developer#11. Changes: - Call autofit.jax.register_model(model) up front, then build a params_tree via jax.tree_util.tree_map(jnp.asarray, instance). - Two per-step JIT closures (blurred_image_from_params, blurred_mm_from_params) now take params_tree directly. No more rebuilding the ModelInstance from a flat vector inside the trace. - Full-pipeline PART C: Fitness wrapper replaced with AnalysisImaging(use_jax=True).log_likelihood_function(instance=...). - vmap PART D: broadcast pytree leaves via tree_map rather than tiling a flat vector; use jax.jit(jax.vmap(full_pipeline_from_params)). - Static memory PART E: reuse vmapped_full.lower(parameters). Per-step raw-array closures (ray_trace_raw, inversion linear algebra matrices, etc.) are unchanged — they don't take params. Both correctness assertions continue to pass end-to-end: - step-by-step log_evidence vs reference: rtol=1e-4 OK - vmap batch=3 vs single JIT: rtol=1e-4 OK Depends on PyAutoFit#1221 (fix for concrete Pixelization kwarg being traced under JIT instead of kept as aux_data). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
4 tasks
Jammy2211
pushed a commit
that referenced
this pull request
Apr 17, 2026
Matches the pattern shipped in mge.py (#11) and pixelization.py (#12): pass a ModelInstance pytree through jit/value_and_grad instead of a flat 1D vector + Fitness/instance_from_vector inside the trace. - register_model(model) once after eager instance build - params_tree = jax.tree_util.tree_map(jnp.asarray, instance) - per-step closures take params_tree and build Tracer from params_tree.galaxies (dropping inner instance_from_vector calls) - test_grad tree-flattens gradient output for statistics - PART C full pipeline uses AnalysisImaging(use_jax=True) .log_likelihood_function(instance=params_tree) in place of Fitness - NNLS kappa diagnostic (_diagnose_kappa) uses pytree params Verified: all 9 gradient tests PASS, all 4 NNLS kappa values report FULLY FINITE GRADIENTS.
4 tasks
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
Migrates
jax_profiling/imaging/pixelization.pyto pass aModelInstancepytree directly intojit/vmap, dropping the flat-vector +model.instance_from_vectorpattern that used to live inside the trace. This bringspixelization.pyin line with themge.pymigration shipped in #11 on 2026-04-16 and makes the profiling match how a real pytree-input sampler would actually run.Required paired library fix: PyAutoFit#1221 — without it,
Galaxy(pixelization=<Pixelization>)becomes a JAX tracer under JIT andfit.inversionsilently resolves toNone.Scripts Changed
jax_profiling/imaging/pixelization.py— callautofit.jax.register_model(model)up front; buildparams_tree = jax.tree_util.tree_map(jnp.asarray, instance); rewrite the two per-step closuresblurred_image_from_paramsandblurred_mm_from_paramsto takeparams_treedirectly (no moreinstance_from_vectorinside the trace); replace full-pipelineFitnesswrapper withAnalysisImaging(use_jax=True).log_likelihood_function(instance=...); broadcast pytree leaves viatree_mapfor the vmap step; reusevmapped_full.lower(parameters)for static-memory analysis. Per-step raw-array closures (ray-trace, inversion linear algebra) are unchanged — they don't take params.jax_profiling/imaging/results/pixelization_likelihood_summary_hst_v2026.4.13.6.json— regenerated.jax_profiling/imaging/results/pixelization_likelihood_summary_hst_v2026.4.13.6.png— regenerated.Upstream PR
PyAutoLabs/PyAutoFit#1221
Test Plan
NUMBA_CACHE_DIR=/tmp/numba_cache MPLCONFIGDIR=/tmp/matplotlib python jax_profiling/imaging/pixelization.py.log_evidencematchesFitImaging.log_evidencereference withinrtol=1e-4.log_evidencematches single-JIT result withinrtol=1e-4.TracerBoolConversionError—register_modelpartitionsredshift, pixelization mesh shape, and any concreteGalaxykwargs intoaux_data.🤖 Generated with Claude Code