Skip to content

fix: Model.tree_flatten handles zero direct priors and keeps TuplePrior children - #1779

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/model-pytree-tuple-prior
Nov 18, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/model-pytree-tuple-prior

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #1777. Model.tree_flatten built its pytree from direct_prior_tuples alone. Two things went wrong because of that:

  • A Model with no direct priors (the DatasetModel shape, whose grid_offset priors sit under a TuplePrior) raised ValueError: not enough values to unpack (expected 2, got 0).
  • A model with a free TuplePrior plus a free scalar lost the tuple priors on round-trip.

This is a latent fix. No search depends on it today, because Fitness closes over the model.

Before / after on a DatasetModel-shaped model, after autofit.jax.register_model:

case prior_count before after
(a) nothing free 0 ValueError 0 → 0
(b) grid_offset free 2 ValueError 2 → 2
(c) grid_offset + sky free 3 3 → 1 (config reload error with no prior config) 3 → 3

API Changes

No public API is removed or renamed. TuplePrior gains the tree_flatten / tree_unflatten pair that the other prior classes already have, and enable_pytrees() now registers TuplePrior as a JAX pytree node. Model.tree_flatten children now include direct TuplePrior attributes.
See full details below.

Test Plan

  • New test_autofit/mapper/test_model_pytree_tuple_prior.py: round-trip for cases (a)/(b)/(c), asserting prior_count 0/2/3 and that the rebuilt grid_offset is a TuplePrior, plus a direct TuplePrior round-trip with mixed free and fixed members. Without the source change, 3 of the 4 tests fail.
  • Full test_autofit: 4295 passed, 2 skipped, 1 xfailed.
  • Smoke (pyauto-heart smoke autofit autogalaxy --root <task worktree>): 26/26 PASS, including autogalaxy_workspace/scripts/multi_dataset/start_here.py (JAX FactorGraph).
  • Heart dev readiness (PyAutoFit): GREEN. Shown reasons, none blocking: manifest drift (end-at-deliverable blocks, 46 mismatches; generated hooks, 2 mismatches) and "release validation incomplete: no rehearsal for current source".

Out of scope, noted on the issue and not changed here:

  • Model.tree_flatten still omits direct child models and fixed constants. tree_unflatten re-creates them from the class defaults or prior config, so a fixed scalar can trigger a config lookup on the rebuilt model.
  • Collection.tree_flatten has the same empty-zip unpack when the collection is empty.
Full API Changes (for automation & release notes)

Added

  • autofit.mapper.prior.tuple_prior.TuplePrior.tree_flatten(): prior members become the children; their names and the fixed members go in the aux data.
  • TuplePrior.tree_unflatten(aux_data, children) (classmethod).
  • autofit.jax.enable_pytrees() registers TuplePrior.

Changed

  • Model.tree_flatten() children = direct priors + direct TuplePrior attributes. A model with neither returns empty children instead of raising.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

…or children

Model.tree_flatten built its pytree from direct_prior_tuples alone, so a
Model with no direct priors (a DatasetModel shape, whose grid_offset priors
sit under a TuplePrior) raised "not enough values to unpack", and a model
with a free TuplePrior plus a scalar lost the tuple priors on round-trip.

- Model.tree_flatten carries direct priors and direct TuplePrior attributes
  as children, and returns empty children when there are none.
- TuplePrior gains tree_flatten/tree_unflatten (prior members as children,
  fixed members in aux) and is registered in enable_pytrees.
- New round-trip test over a DatasetModel-shaped Model: prior_count 0, 2, 3
  survives tree_flatten/tree_unflatten.

Closes #1777

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Nov 18, 2026
@Jammy2211
Jammy2211 merged commit 17b87ed into main Nov 18, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/model-pytree-tuple-prior branch November 18, 2026 13:26
@pyautolabs-bot pyautolabs-bot Bot removed the pending-release PR queued for the next release build label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Model.tree_flatten fails on zero direct priors and drops TuplePrior children

1 participant