Skip to content

fix: Model pytree round-trip drops fixed non-prior attributes #1780

Closed

Description

@Jammy2211

Overview

Follow-up to #1777 / #1779. Model.tree_flatten now carries a model's direct priors and TuplePrior attributes, but it still drops fixed non-prior attributes on the model, such as a fixed sky level. tree_unflatten rebuilds the model without them, so making an instance from the rebuilt model falls back to a prior-config lookup for the missing attribute, which a config-less class does not have.

Plan approved by the human on 2026-11-18 ("Do the small follow up after prm"), so the start_dev approval pause is skipped.

Plan

  • Carry the model's fixed direct attributes (not priors, TuplePriors or child models) in tree_flatten's aux data.
  • Restore them in tree_unflatten.
  • Extend test_autofit/mapper/test_model_pytree_tuple_prior.py with instance round-trips for cases (a)-(c), asserting the fixed value survives and the rebuilt model gives the same instance as the original.
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/model-pytree-fixed-attributes

Implementation Steps

  1. In autofit/mapper/prior_model/prior_model.py, Model.tree_flatten: collect direct __dict__ attributes that are not priors, TuplePriors or AbstractPriorModels (skipping cls, id and private names) and add them to the aux data.
  2. Model.tree_unflatten: set those attributes on the rebuilt model.
  3. Add instance round-trip tests for cases (a)-(c) to test_autofit/mapper/test_model_pytree_tuple_prior.py.

Key Files

Original Prompt

Click to expand starting prompt

Model pytree round-trip drops fixed non-prior attributes (e.g. a fixed sky level)

Type: bug
Target: PyAutoFit
Repos:

  • PyAutoFit
    Difficulty: small
    Autonomy: safe
    Priority: medium
    Status: formalised
    Consequence: glance
    Witness: after autofit.jax.register_model, a Model of a DatasetModel-shaped class with grid_offset free and a FIXED scalar attribute (e.g. a fixed sky level) round-trips through jax.tree_util.tree_flatten / tree_unflatten with the fixed value preserved, and instance_from_prior_medians() (or instance_from_vector) on the rebuilt model succeeds without looking up prior config for the fixed attribute and returns the same instance as the original model
    Review-minutes: 5
    Unattended: ready
    Lane: any

Follow-up to PyAutoFit#1777 / PR #1779 (merged 2026-11-18; record
complete/2026/11/dataset-model-free-grid-offset-pytree-roundtrip.md). Human request,
verbatim: "Do the small follow up after prm".

#1779 made Model.tree_flatten carry direct priors and TuplePrior attributes as pytree
children, but fixed non-prior attributes on the Model (a float or other constant set on
the model, e.g. a fixed sky level) are not carried in the aux data. tree_unflatten
therefore rebuilds the model without them, and making an instance from the rebuilt model
in case (b) of test_autofit/mapper/test_model_pytree_tuple_prior.py falls back to a
prior-config lookup for the missing attribute (which a config-less class does not have).

Fix: carry the Model's fixed (non-prior, non-TuplePrior) direct attributes in
tree_flatten's aux data and restore them in tree_unflatten
(autofit/mapper/prior_model/prior_model.py); extend the #1779 test file with an
instance round-trip for cases (a)-(c) asserting the fixed value survives. Out of scope:
nested child models beyond what the existing flatten already recurses into.

Activity

  1. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    PR: #1781 (label pending-release)

    Plan was approved by the human on 2026-11-18 ("Do the small follow up after prm"), so the start_dev approval pause was skipped.

    • Repro on the branch before the fix: cases (a) and (b) raised ConfigException (background_sky_level) when making an instance from the rebuilt model; (c) passed. After: all three match the original model.
    • Tests: new instance round-trip tests (fail 2/3 without the fix); pytree-related tests 190 passed; full test_autofit passes (parallel-run flakes rerun serially: 448 passed).
    • Smoke: autofit + autogalaxy pass.
    • Workspace impact: none (internal; no workspace script uses the Model pytree API).

    Tier glance; stopped at PR-open. Next: human /prm.

  2. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped in #1781 (merged 3ba1e6c3, 2026-11-18). Model.tree_flatten carries fixed direct attributes (e.g. a fixed sky level) in the aux data and tree_unflatten restores them, so instances from a round-tripped model match the original (instance_from_prior_medians / instance_from_vector, cases a-c). CI GREEN; pytree tests 190 passed; autofit + autogalaxy smoke passed. Model and search identifiers checked unchanged against pre-#1778 main for five models. Pending release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions