Repository navigation
Description
Overview
Make fit.linear_light_profile_intensity_dict survive a jax.jit round-trip so the visualization path works for any model containing a linear light profile (al.lp_linear.Gaussian, al.lmp_linear.GaussianGradient, etc.). Currently, Part 2 of modeling_visualization_jit.py only works with parametric Sersic — any LightProfileLinear dies with KeyError inside the visualizer callback because the dict is identity-keyed and pytree unflatten produces fresh objects.
Plan
- Attach a stable
_pytree_token(monotonic counter) to everyLightProfileLinearinstance - Override
__hash__/__eq__onLightProfileLinearto use the token instead ofid() - Add
__exclude_identifier_fields__so the token never affects PyAutoFit's modelunique_id - Register
LightProfileLinearas a pytree withno_flatten=("_pytree_token",)so the token rides as static aux data across JIT - Revert Part 2 of
modeling_visualization_jit.pyto full MGE (parametric basis ofGaussianGradient, NFWSph mass, MGE source) and verifysubplot_fit.pnglands with noKeyError
Detailed implementation plan
Affected Repositories
- PyAutoGalaxy (primary — hosts
LightProfileLinear) - PyAutoLens (pytree registration site)
- autolens_workspace_test (Part 2 revert)
Work Classification
Both (library + workspace)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| PyAutoGalaxy | main | clean |
| PyAutoLens | main | clean |
| autolens_workspace_test | main | clean |
Suggested branch: feature/linear-light-profile-intensity-dict-pytree
Worktree root: ~/Code/PyAutoLabs-wt/linear-light-profile-intensity-dict-pytree/
Option chosen: B (stable identifier token)
Option B over A (structural path) and C (value eq/hash). A requires threading paths through linear_obj_func.light_profile_list construction — higher blast radius. C risks surprise collisions between profiles with identical params.
Implementation Steps
- In
PyAutoGalaxy/autogalaxy/profiles/light/linear/abstract.py, add toLightProfileLinear:import itertools _pytree_token_counter = itertools.count() __exclude_identifier_fields__ = ("_pytree_token",) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._pytree_token = next(LightProfileLinear._pytree_token_counter) def __hash__(self): return self._pytree_token def __eq__(self, other): return isinstance(other, LightProfileLinear) and self._pytree_token == other._pytree_token
- In
PyAutoLens/autolens/imaging/model/analysis.py_register_fit_imaging_pytrees(after line 151):register_instance_pytree(LightProfileLinear, no_flatten=("_pytree_token",)) - Revert Part 2 of
autolens_workspace_test/scripts/imaging/modeling_visualization_jit.pyto full MGE model (look at git log for the pre-split state). - Verify:
modeling_visualization_jit.pyruns end-to-end,subplot_fit.pnglands, noKeyErrormulti_galaxy_mge.pyregression identifier5a3c480de681f6958048b22b3db8ecf9unchanged (Option B audit confirmed identifier naturally filters_pytree_tokenbecause it's not an__init__arg;__exclude_identifier_fields__is belt-and-braces)- PyAutoGalaxy + PyAutoLens test suites green
Key Files
PyAutoGalaxy/autogalaxy/profiles/light/linear/abstract.py—LightProfileLinearclass +lp_instance_fromat line 148PyAutoGalaxy/autogalaxy/abstract_fit.py:122—linear_light_profile_intensity_dictconstructionPyAutoLens/autolens/imaging/model/analysis.py—_register_fit_imaging_pytreesautolens_workspace_test/scripts/imaging/modeling_visualization_jit.py— Part 2
Audits completed (de-risked Option B)
- Class structure:
LightProfileLinearis a plain class, no dataclass/slots. MRO override of__hash__/__eq__wins cleanly overGeometryProfileancestors. Minor caveat: pickled instances retain pickled token. - Call sites: the only dict that changes is the target one.
LinearObjsubclasses (Mapper,LightProfileLinearObjFuncList) are notLightProfileLinear; other identity-keyed dicts unaffected. - Identifier:
PyAutoFit/autofit/mapper/identifier.py:127-128filters__dict__to__init__args only, so_pytree_tokenis naturally excluded.__exclude_identifier_fields__is added explicitly for clarity + insurance against future code paths.
Original Prompt
Click to expand starting prompt
Make `fit.linear_light_profile_intensity_dict` survive a `jax.jit` round-trip so the visualization path works for any model containing a linear light profile. See `admin_jammy/prompt/issued/linear_light_profile_intensity_dict_pytree.md` for the full text.
Activity
Jammy2211 commentedon Apr 19, 2026
Library PR Created — 2026-04-19
PRs
Status
Workspace changes needed (Part 2 of
modeling_visualization_jit.pyreverted to full MGE). Next: /ship_workspaceAPI Changes
LightProfileLineargainspytree_token+ token-based__hash__/__eq__solinear_light_profile_intensity_dictsurvives pytree unflatten. Previously structural__dict__eq; now identity-equivalent (same object only).Jammy2211 commentedon Apr 19, 2026
Shipped — 2026-04-19
PRs
Summary
linear_light_profile_intensity_dictnow survivesjax.jitround-trip. Implemented Option B (stable identifier token) from the prompt: everyLightProfileLinearinstance gets a monotonicpytree_tokenat__init__;__hash__and__eq__route through the token; name has no leading underscore so PyAutoFit'sregister_modelpreserves it asattr_constacross pytree flatten/unflatten;__exclude_identifier_fields__keeps it out of the model unique_id hash. End-to-end validation: Nautilus runs full MGE model (GaussianGradient basis + NFWSph + MGE source),fit.pnglands during quick-update, noKeyError.Session Notes
linear_obj_func.light_profile_listconstruction — higher blast radius. C (value eq/hash) risks surprise collisions when two linear profiles have identical params._pytree_token(leading underscore) would be silently skipped byregister_modelatPyAutoFit/autofit/jax/pytrees.py:153— the token wouldn't ride pytree aux, breaking the fix.pytree_token(no underscore) is preserved via the defaultattr_constpath.PyAutoFit/autofit/mapper/identifier.py:127-128— filters__dict__to__init__args only, sopytree_tokenis naturally excluded.__exclude_identifier_fields__added belt-and-braces.multi_galaxy_mge.pyregression hash5a3c480de681f6958048b22b3db8ecf9confirmed unchanged.test__lp_linear_func_galaxy_dict_fromwas hidden by the old structural__dict__eq onLightProfileLinearwhich made all parameter-less instances compare equal. New token-based eq exposed off-by-one variable references at lines 112-113.LightProfileLinearinstances retain their pickled token. Acceptable for JIT usage; flag if staleness surfaces on long-lived pickles.This unblocks the 11 remaining pytree variant prompts that ship models containing linear light profiles.