Skip to content

fix: tolerate a class already registered as a JAX pytree elsewhere (#693) - #702

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/galaxy-duplicate-pytree-registration
Nov 18, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/galaxy-duplicate-pytree-registration

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Building a JAX Fitness registers Galaxy as a pytree through autofit.jax.register_model. A later autolens.jax.register_tracer_classes in the same process then reaches autoarray.abstract_ndarray.register_instance_pytree(Galaxy), and JAX raised ValueError: Duplicate custom PyTreeDef type registration for Galaxy. Both autoarray registration helpers now go through _register_pytree_node_once. If JAX reports a duplicate registration, the helper keeps the existing one, so the first route wins (autofit already does the same in reverse). Any other ValueError still raises.

Fixes #693. The work was done by a shift worker that crashed on 2026-11-14 before pushing. This session resumed it and rebased it onto current main.

API Changes

None — internal changes only. register_instance_pytree and _register_as_pytree no longer raise when another route has already registered the class.
See full details below.

Test Plan

  • pytest test_autoarray: 2461 passed (rebased onto e81c6e16)
  • New test_autoarray/test_abstract_ndarray_pytree_duplicate.py: duplicate error tolerated on both helpers; any other ValueError still raises (NumPy-only, stubbed JAX call)
  • Direct reproduction with real JAX: register_model(af.Collection(lens=af.Model(ag.Galaxy, mass=ag.mp.Isothermal))), then register_tracer_classes(tracer), then jax.tree_util.tree_leaves(tracer). Control on main: raises Duplicate custom PyTreeDef type registration for Galaxy. This branch: passes.
  • PyAutoLens Witness tests (test_analysis_point_gradient_mode.py, test_static_lattice_jax.py, test_solver_spurious_core.py in one session): 32 passed. These also pass on main, because the gradient-mode test does not register Galaxy through register_model, so the direct reproduction above is the real control.
  • Heart readiness --dev --repos PyAutoArray: GREEN. Non-blocking reasons shown: manifest drift (end-at-deliverable blocks: 46 mismatches; generated hooks: 2 mismatches vs PyAutoMind/repos.yaml); release validation incomplete (no rehearsal for current source).
Full API Changes (for automation & release notes)

Added

  • autoarray.abstract_ndarray._register_pytree_node_once(cls, flatten, unflatten): private helper. Registers with jax.tree_util and ignores JAX's duplicate-registration ValueError.

Changed Behaviour

  • autoarray.abstract_ndarray.register_instance_pytree(cls, no_flatten=()): no longer raises when cls is already registered by another route (e.g. autofit.jax.register_model); the existing registration is kept.
  • autoarray.abstract_ndarray._register_as_pytree(cls): same.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

Building a JAX Fitness registers Galaxy via autofit.jax.register_model;
autolens.jax.register_tracer_classes then reaches register_instance_pytree
for the same class and JAX raised "Duplicate custom PyTreeDef type
registration for Galaxy". Both autoarray registration helpers now keep the
existing registration and record the class as registered; any other
ValueError still raises.

Fixes #693

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

Copy link
Copy Markdown
Collaborator Author

Workspace smoke (pyauto-heart smoke, run with PYAUTO_ROOT set to the task worktree so it used this branch): all passed. autofit 8+2nb, autogalaxy 14+2nb, autolens 42+2nb, autolens_workspace_test 39, euclid 9, HowToLens 41. autocti and autocti_test did not run: the laptop has no GSL headers (libgsl-dev), so their environment never built. That's an environment problem, not a code result. Ready for /prm.

@Jammy2211
Jammy2211 merged commit 5815019 into main Nov 18, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/galaxy-duplicate-pytree-registration branch November 18, 2026 11:59
@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: duplicate Galaxy pytree registration after a JAX Fitness

1 participant