fix(nnx): ensure ToLinen args and kwargs are hashable - #5598
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
Coerce args to a tuple and kwargs to a FrozenDict during ToLinen initialization so that wrapped Linen modules can be hashed and passed as static arguments to jax.jit. Fixes google#4156 Signed-off-by: Reginald Alfret <reginaldalfret@gmail.com>
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.
Fixes #4156
Description
ToLinenmodules should be hashable like standard Linen modules, allowing them to be passed as static arguments tojax.jitand stored in sets/dicts.Previously, if
kwargswas supplied as a standard Python dictionary (or ifargswas supplied as a sequence like a list),hash(model)failed withTypeError: unhashable type: 'dict'because Linen dataclasses calculate__hash__across all module attributes.Changes
__post_init__toToLineninflax/nnx/bridge/wrappers.pyto:argsto atupleif it is not already one.kwargsto aFrozenDict(which recursively freezes nested dicts) if it is not already one.test_to_linen_hashableintests/nnx/bridge/wrappers_test.pyverifying:ToLinenis hashable.ToLinenwith a mutable dict forkwargsis automatically converted toFrozenDictand hashable.ToLinenwith a list forargsis automatically converted totupleand hashable.ToLinencan be safely passed as a static argument (static_argnums=(0,)) tojax.jit.