Repository navigation
Description
Overview
Model.tree_flatten (autofit/mapper/prior_model/prior_model.py) builds the pytree from direct_prior_tuples alone. A Model with zero direct priors (e.g. af.Model(al.DatasetModel), whose grid_offset priors sit under a TuplePrior) crashes with ValueError: not enough values to unpack (expected 2, got 0), and a model with a free TuplePrior plus a scalar silently loses the tuple priors on round-trip (prior_count 3 → 1). Nothing reaches a search today (Fitness closes over the model), so this is a latent correctness fix.
Reproduced on fresh main (PyAutoFit@ac881b5c) with a DatasetModel-shaped class after autofit.jax.register_model:
a (nothing free) 0 -> ValueError not enough values to unpack (expected 2, got 0)
b (grid_offset free) 2 -> ValueError not enough values to unpack (expected 2, got 0)
c (grid_offset + sky) 3 -> 1
Plan
- Make
Model.tree_flattencope with zero direct priors (empty children, empty names) instead of unpacking an emptyzip. - Carry the model's
TuplePriorattributes as pytree children alongside its direct priors, sotree_unflattenrebuilds them. - Give
TuplePriorits owntree_flatten/tree_unflatten(prior members as children, fixed members in aux data) and register it inenable_pytrees, so JAX recurses into it rather than treating it as an opaque leaf. - Add a round-trip test over a
DatasetModel-shapedModelfor the three cases, assertingprior_countis 0, 2, 3 before and after. - Tier: glance — merge mode: auto-merge on green if the Witness passes, post-merge summary
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./fit/PyAutoFit | main (behind 3) | clean |
PyAutoFit is claimed by active task ep-projected-model-shared-prior (PyAutoFit#1773, branch feature/ep-projected-model-shared-prior), which edits autofit/non_linear/samples/samples.py and test_autofit/graphical/test_unification.py. This task touches none of those files.
Suggested branch: feature/model-pytree-tuple-prior
Implementation Steps
autofit/mapper/prior_model/prior_model.pyModel.tree_flatten:tuples = list(self.direct_prior_tuples) + list(self.direct_tuple_priors);names = tuple(n for n, _ in tuples),children = tuple(v for _, v in tuples); returnchildren, (names, self.cls).tree_unflattenis unchanged (Model(cls_, **arguments)already acceptsTuplePriorkwargs — checked: rebuilds prior_count 0/2/3).autofit/mapper/prior/tuple_prior.py: addtree_flatten(children = prior members, aux = prior names + fixed(name, value)members excludingid) and classmethodtree_unflatten(TuplePrior(**priors, **fixed)).autofit/jax/pytrees.pyenable_pytrees: addTuplePriorto the registered classes.- Test
test_autofit/jax/test_model_pytree_tuple_prior.py(or alongside the existing pytree tests):pytest.importorskip("jax"), a localDatasetModel(background_sky_level=0.0, grid_offset=(0.0, 0.0))class, cases (a)/(b)/(c),register_model,tree_flatten→tree_unflatten, assertprior_countequal (0, 2, 3) and the rebuiltgrid_offsetis aTuplePrior.
Out of scope (noted, not changed)
Model.tree_flattenalso omits direct child models and fixed constants (they are re-created from defaults on unflatten);Collection.tree_flattenhas the same empty-zipunpack on an empty collection.
Key Files
autofit/mapper/prior_model/prior_model.py—Model.tree_flattenautofit/mapper/prior/tuple_prior.py—TuplePriorpytree methodsautofit/jax/pytrees.py—enable_pytreesregistration
Original Prompt
Click to expand starting prompt
DatasetModel with a free grid_offset cannot round-trip autofit.jax.register_model
Type: bug
Target: autofit
Repos:
- PyAutoFit
Themes: - jax
Difficulty: small
Autonomy: safe
Priority: normal
Status: draft
Consequence: glance
Witness: A round-trip test throughjax.tree_util.tree_flatten/tree_unflattenafterautofit.jax.register_model, over aModelwith zero direct priors and aTuplePriorchild (theDatasetModelshape) with (a) nothing free, (b) the tuple free, (c) the tuple plus one scalar free, passes withprior_countequal before and after (0, 2, 3).
Review-minutes: 3
Lane: any
Filed: 2026-09-03
Symptom
A bare af.Model(al.DatasetModel) cannot be round-tripped through
jax.tree_util.tree_flatten / tree_unflatten after
autofit.jax.register_model: flattening raises
ValueError: not enough values to unpack (expected 2, got 0)
(autofit/mapper/prior_model/prior_model.py:331, Model.tree_flatten)
Corrected 2026-09-04 while triaging autolens_workspace#524. Three facts replace
the original description:
- The failure is a
ValueErrorinModel.tree_flatten, not aTypeErrorin
Prior.tree_unflatten(that line is never reached). - It is not caused by a free
grid_offset: the same model with no free
parameters fails identically. The trigger is aModelwith zero direct
priors — thegrid_offsetpriors live under aTuplePrior, which
direct_prior_tuplesexcludes. - Related silent defect: with
grid_offset_0/1andbackground_sky_level
free (prior_count 3), the round-trip succeeds but the rebuilt model has
prior_count 1 —Model.tree_flattendropsTuplePriorchildren.
None of this reaches a search: Fitness (autofit/non_linear/fitness.py:245)
captures the model as a closure constant and only traces the parameter vector.
A free grid_offset inside a FactorGraphModel(use_jax=True) compiles and runs
under the vmapped Nautilus fitness (verified on the corrected
multi_dataset/start_here.py). It blocks nothing today.
Originally found while building the free-grid_offset control for
gaussian-precompute-p2 (record complete/2026/09/gaussian-precompute-p2.md).
Fix
Make Model.tree_flatten handle zero direct priors and include TuplePrior
children (so tree_unflatten rebuilds them), and add a round-trip test over a
DatasetModel with (a) nothing free, (b) a free grid_offset, (c) a free
grid_offset plus background_sky_level, asserting prior_count survives.
Activity
Jammy2211 commentedon Nov 16, 2026
Parked by shift worker 2026-11-16-pm — question for the human.
What was done. The premise check passed and the bug reproduces on fresh main (PyAutoFit@ac881b5c): cases (a) and (b) raise
ValueError: not enough values to unpack (expected 2, got 0)inModel.tree_flatten, and case (c) round-trips withprior_count3 → 1. The fix and its test are planned in full above. No branch or worktree has been made.Why it stopped. PyAutoFit is claimed in
active.mdbyep-projected-model-shared-prior(PyAutoFit#1773, branchfeature/ep-projected-model-shared-prior, human-run, judge tier). The claim guard says two tasks on one repo must run one after the other unless you approve an override. That task changesautofit/non_linear/samples/samples.pyandtest_autofit/graphical/test_unification.py. This one would changeautofit/mapper/prior_model/prior_model.py,autofit/mapper/prior/tuple_prior.py,autofit/jax/pytrees.pyand one new test file, so no file overlaps. You approved the same kind of override forthree-autos-public-framingon 2026-11-13.Options.
mainforfeature/model-pytree-tuple-prioralongside fix: EP projection KeyError for a prior shared across model paths #1773, since no files overlap. The next shift implements the fix and opens a glance-tier PR.Fitnesscloses over the model), so shelve it.Resume after an answer: option 1 →
conflict-override:line on theactive.mdrow,start_library, then implement as above. Option 2 → move toplanned.mdwithblocked-by: ep-projected-model-shared-prior.Jammy2211 commentedon Nov 16, 2026
Use 1
Jammy2211 commentedon Nov 18, 2026
Unblocked 2026-11-18. #1773 has shipped as PR #1778 (awaiting /prm), and the override answered above stands: work on a separate worktree off
main(feature/model-pytree-tuple-prior). There is no file overlap with #1778. Mindactive.mdnow lists this task as ready.Jammy2211 commentedon Nov 18, 2026
Library PR opened: #1779 (branch
feature/model-pytree-tuple-prior).grid_offsetis rebuilt as a TuplePrior.test_autofit4295 passed, 2 skipped, 1 xfailed. The new round-trip test fails 3 of 4 without the fix./prm.Jammy2211 commentedon Nov 18, 2026
Shipped in #1779 (merged
17b87edb, 2026-11-18).Model.tree_flattenhandles zero direct priors and carriesTuplePriorattributes as pytree children;TuplePriorhas its own flatten/unflatten and is registered inenable_pytrees. Round-trip keepsprior_count0/2/3. Tests: fulltest_autofit4295 passed; autofit + autogalaxy smoke 26/26. Pending release. Follow-up (fixed non-prior attributes such as a fixed sky level are not carried through the round-trip) is being filed as its own task.