fix: don't register JAX-leaf types (int) as pytree nodes (shapelet vmap crash)#1366
Merged
Merged
Conversation
register_model walked every Model.cls into a pytree registration, including the af.Model(int) sub-models autofit auto-creates for int-typed constructor args (e.g. a shapelet's n/m). Registering builtins.int made JAX call the instance flatten _partition(5) on every int leaf, and vars(5) raised 'TypeError: vars() argument must have __dict__ attribute', crashing jax.vmap(jax.jit(...)) for any JAX shapelet fit (release-validation run 29266305445). Gate registration on _instances_carry_dict(cls): only classes whose instances carry a __dict__ (real model components) are registered; native JAX leaves (int/float/str/...) and purely __slots__ classes are left as opaque leaves. Producer-side fix — no silent fallback inside _partition. Fixes #1365 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TDCsgNCXacXqiWAPTswWCt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
JAX-fitting a shapelet model crashed in
jax.vmap(jax.jit(self.call))(Fitness._vmap) withTypeError: vars() argument must have __dict__ attribute(release-validation run29266305445).Root cause (differs from the issue's initial hypothesis)
Not a
__slots__/array-like component.af.Model(ShapeletPolar)auto-wraps itsint-typedn/margs asaf.Model(int)sub-models, andregister_model._walkregisteredbuiltins.intas a pytree node. JAX then ran the instance-flatten_partition(5)on everyintleaf →vars(5)crash. Confirmed by walking the tree (int ∈ _REGISTERED_INSTANCE_CLASSES) and reproduced in pure autofit viaaf.Model(int).int(likefloat/str) is a native JAX leaf and must never be registered.Fix
autofit/jax/pytrees.py: new helper_instances_carry_dict(cls)(any("__dict__" in vars(k) for k in cls.__mro__));register_model._walknow gates theregister_pytree_nodeblock on it. Leaf types and purely__slots__classes are left as opaque JAX leaves; real model components (which have__dict__) register as before. Producer-side — no silent fallback inside_partition(per the no-silent-guards principle).Verification
tree_flattens cleanly (was crashing onmain).autofit_workspace_testPR): leaf types not registered,tree_flatten/tree_unflattenround-trip.jax_assertions/pytrees.py+enable_pytrees.pystill pass (no regression).pytest test_autofit/→ 1473 passed, 1 skipped.API Changes
None — one private helper added; public surface unchanged.
Fixes #1365
🤖 Generated with Claude Code
https://claude.ai/code/session_01TDCsgNCXacXqiWAPTswWCt