Commit 48bcb80
fix(jax): keep parameterization cache off ModelInstance + auto-register pytrees
Two coupled fixes restoring the JAX `jit(fit_from)` path that broke
when commit 4564ae9 made `AbstractPriorModel.parameterization` a
`functools.cached_property`.
`cached_property` writes to `self.__dict__["parameterization"]`. After
any `model.info` access, `Collection._instance_for_arguments` (which
iterates `__dict__` and skips only underscore-prefixed keys) propagates
the cached string onto every `ModelInstance`. The string then surfaces
as a non-array JAX pytree leaf (autogalaxy_workspace_test +
autolens_workspace_test `jax_likelihood_functions/*` — 38 scripts) and
makes `for x in instance:` yield strings instead of profiles
(autofit_workspace `overview/overview_1_the_basics.py`).
Fix 1: store the cache under the underscore-prefixed key
`_parameterization_cache` so both `Collection._instance_for_arguments`
and `ModelInstance.dict` filter it out. Preserves the 2.7s → 0.05s
perf win from 4564ae9.
Fix 2: auto-call `enable_pytrees() + register_model(self.model)` from
`Fitness.__init__` whenever `analysis._use_jax=True`. Both helpers are
idempotent, so workspaces that still call them explicitly keep
working. New JAX-enabled workspaces don't need the boilerplate.
Verified locally:
- 1413/1413 PyAutoFit unit tests pass + new
`test_parameterization_cache_does_not_leak_into_instance` regression
- `autofit_workspace/scripts/overview/overview_1_the_basics.py` runs
to completion (cluster C4 reproducer)
- `autolens_workspace_test/scripts/jax_likelihood_functions/imaging/rectangular.py`
prints "PASS: jit(fit_from) round-trip matches NumPy scalar"
(cluster C1 reproducer)
Follow-up: a structural defense across the four `__dict__`-iterators
in `autofit/mapper/` plus `autoarray/abstract_ndarray.py` will ship as
a separate PR — a `_cached_property_names(cls)` classmethod applied
as an extra filter at every leak site so the next future
`@cached_property` on a model class cannot reintroduce this bug.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent f24e915 commit 48bcb80
3 files changed
Lines changed: 59 additions & 4 deletions
File tree
- autofit
- mapper/prior_model
- non_linear
- test_autofit/mapper
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
| |||
1860 | 1859 | | |
1861 | 1860 | | |
1862 | 1861 | | |
1863 | | - | |
| 1862 | + | |
1864 | 1863 | | |
1865 | 1864 | | |
1866 | 1865 | | |
1867 | | - | |
1868 | | - | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
1869 | 1882 | | |
1870 | 1883 | | |
1871 | 1884 | | |
| |||
1900 | 1913 | | |
1901 | 1914 | | |
1902 | 1915 | | |
| 1916 | + | |
1903 | 1917 | | |
1904 | 1918 | | |
1905 | 1919 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
125 | 131 | | |
126 | 132 | | |
127 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
144 | 179 | | |
145 | 180 | | |
146 | 181 | | |
| |||
0 commit comments