Repository navigation
💥 feat!: flatten ImmutableMap leaves in sorted-key order - #80
Merged
Merged
Conversation
Match how JAX flattens dict and the built-in frozendict (JAX 0.11.2+), so switching from ImmutableMap to frozendict on Python 3.15+ keeps leaf order. Unflattening now rebuilds the map in sorted order, and keys that cannot be ordered against each other raise TypeError on flatten. BREAKING CHANGE: jax.tree.leaves(ImmutableMap(...)) order changes for maps whose insertion order is not sorted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nstarman
force-pushed
the
feat/sorted-leaf-order
branch
from
October 10, 2026 22:14
160466a to
2be4807
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #80 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 3
Lines ? 75
Branches ? 0
========================================
Hits ? 75
Misses ? 0
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
JAX 0.11.2 registers the built-in
frozendict(PEP 814) as a PyTree and flattens it likedict: leaves in sorted-key order, unflatten rebuilds in sorted order, and keys that can't be ordered raise an error.ImmutableMapflattened in insertion order, so switching to the built-in on Python 3.15+ silently reordered leaves. This PR makesImmutableMap.tree_flattensort its keys so the two types agree.jax.tree.leaves(ImmutableMap(b=1, a=2))was[1, 2]and is now[2, 1]. This affects code that indexes leaves, saved optimizer states, and checkpoints.tree.map/jitround-trip, iteration order is now sorted rather than insertion order. Equality and hashing are already order-independent.{1: …, "b": …}) now raiseTypeErroron flatten. Native JAX raisesValueErrorfor the same case ondictandfrozendict. I didn't wrap it to match.The treedefs still differ (
CustomNode(ImmutableMap[...])vsfrozendict({...})), and keypaths are unchanged.Changes
src/xmmutablemap/_core.py:tree_flattensorts its keys. The docstring and doctest are updated.tests/test_immutablemap.py: new tests for sorted leaves, the sorted round-trip, and unorderable keys.README.md: the leaf-order warning added in 📝 docs: prefer the built-in frozendict on Python 3.15+ #79 is rewritten to sayImmutableMapmatches the built-in and notes the change from earlier releases.Verification
pytestpasses on 3.10 and 3.13 (116 tests).prek run --all-filespasses, including mypy.ImmutableMap(b=1, a=2, c=3)andfrozendict(b=1, a=2, c=3)give identicaljax.tree.leaves([2, 1, 3]) and identical round-tripped key order.🤖 Generated with Claude Code