Repository navigation
Conversation
optax.named_chain keeps its state in an OrderedDict. Only the exact type dict was registered, so to_state_dict returned the OrderedDict unchanged, to_bytes failed in msgpack, and from_state_dict returned the given state dict instead of rebuilding the target.
This branch has not been deployed
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.
What does this PR do?
optax.named_chainkeeps its state in acollections.OrderedDict(google-deepmind/optax#1397, released in optax 0.2.6, changed it from a dict).flax.serializationonly registers the exact typedict, soto_state_dictreturns anOrderedDictunchanged,to_bytesfails withTypeError: can not serialize 'collections.OrderedDict' object, andfrom_state_dictreturns the given state dict instead of rebuilding the target.Repro:
Fix: register
collections.OrderedDictinflax/serialization.pywith the dict state-dict function; restoring rebuilds anOrderedDictin the target's key order.Tests: new
test_ordered_dict_serializationintests/serialization_test.py(named_chain state -> to_bytes -> from_bytes, type, key order, leaves and tree structure). On main it fails with the msgpack TypeError; with the change it passes.pytest tests/serialization_test.py: 88 passed, 4 skipped.ruff checkon the two files is clean. Not run: the rest of the test suite, pre-commit hooks.Behaviour change: states containing an
OrderedDictthat previously could not be written withto_bytesnow can.from_state_dicton an OrderedDict target now rebuilds an OrderedDict in the target's key order instead of returning the given state dict. Plaindicthandling is unchanged.Fixes #5671
Checklist
The implementation and tests were written with AI assistance.