Skip to content

fix: duplicate Galaxy pytree registration after a JAX Fitness #693

Closed

Description

@Jammy2211

Overview

Building a JAX Fitness registers the model's classes (including Galaxy) as JAX pytrees via autofit.jax.register_model. If the same process then calls autolens.jax.register_tracer_classes (the static-lattice / PointSolver JAX path), autoarray.abstract_ndarray.register_instance_pytree registers Galaxy a second time and JAX raises ValueError: Duplicate custom PyTreeDef type registration for Galaxy. PyAutoFit's side already tolerates the duplicate; autoarray's side does not. Found while shipping PyAutoFit#1649 / PyAutoLens#752, which worked around it with a subprocess.

Reproduced on main (PyAutoArray@102f0249, PyAutoFit@362ee746): build Fitness(model, AnalysisPoint(use_jax=True)), then register_tracer_classes(tracer) → the ValueError above, raised at autoarray/abstract_ndarray.py:136.

Plan

  • Make autoarray's two registration helpers idempotent across registration routes: when JAX reports the class is already registered (by PyAutoFit or anyone else), record it as registered and return, as PyAutoFit's register_model already does.
  • Only the duplicate-registration error is swallowed; any other error still raises.
  • The first registration of a class wins (JAX has no unregister). Document that in the docstrings.
  • Add a PyAutoArray unit test that registers a class through another route, then calls both helpers without error.
  • Check the Witness: a JAX Fitness for an AnalysisPoint model, then the 16 test_static_lattice_jax.py tests, all in one pytest process.
  • Tier: glance — merge mode: auto-merge on green if the Witness passes, post-merge summary

Detailed implementation plan

Affected repositories

  • PyAutoArray (primary)
  • PyAutoFit: no change needed. register_model / enable_pytrees already catch the duplicate ValueError.

Work classification: Library

Branch survey

Repository Current Branch Dirty?
PyAutoArray main clean

Suggested branch: feature/galaxy-duplicate-pytree-registration
Worktree root: ~/Code/PyAutoLabs-wt/galaxy-duplicate-pytree-registration/

Steps

  1. autoarray/abstract_ndarray.py: add a small helper _register_pytree_node_once(cls, flatten, unflatten). It calls autonerves.jax_wrapper.register_pytree_node, catches ValueError whose message contains "Duplicate custom PyTreeDef", and re-raises anything else. Both _register_as_pytree and register_instance_pytree use it and add cls to _pytree_registered_classes either way.
  2. Docstrings: say that a class registered first through another route (e.g. autofit.jax.register_model during a JAX Fitness build) keeps that registration.
  3. test_autoarray/test_abstract_ndarray_pytree.py (or the existing pytree test module): register a throwaway class directly with jax.tree_util.register_pytree_node, then register_instance_pytree(cls) must not raise and must mark it registered; a non-duplicate ValueError still propagates.
  4. Witness, in one process: build a JAX Fitness for the AnalysisPoint model from test_analysis_point_gradient_mode.py, then run test_autolens/point/triangles/test_static_lattice_jax.py (16 tests). All must pass.

Not in scope: removing the subprocess workaround from PyAutoLens test_analysis_point_gradient_mode.py. That subprocess also serves as the GPU-run entry point, and PyAutoLens is not in this task's repos. Raise it as a follow-up.

Key files

  • autoarray/abstract_ndarray.py: registration helpers
  • test module under test_autoarray/

Activity

  1. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    Resumed the 2026-11-14 shift worker's unpushed fix and rebased it onto current main. PR: #702

    • pytest test_autoarray: 2461 passed
    • Direct reproduction with real JAX (register_model registers Galaxy, then register_tracer_classes): raises Duplicate custom PyTreeDef type registration on main, passes on the branch
    • PyAutoLens static-lattice and point-gradient tests: 32 passed
    • Heart dev gate: GREEN

    API changes: none (internal changes only). Workspace impact: none expected; the workspace smoke run is in progress. The merge is human (/prm).

  2. added a commit that references this issue on Nov 18, 2026
  3. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #702 (5815019d). register_instance_pytree and _register_as_pytree now keep an existing JAX registration and no longer raise Duplicate custom PyTreeDef type registration, so building a JAX Fitness and then calling register_tracer_classes in the same process works.

    Evidence: autoarray 2461 passed; direct reproduction with real JAX (raises on old main, passes on the branch); PyAutoLens static-lattice and point-gradient tests 32 passed; workspace smoke all passed (autocti not run: no GSL on the laptop); Heart dev gate GREEN. The fix reaches users in the next release (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