Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that assumed frozen-dict keys were strings. #3692

Merged
1 commit merged into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flax/core/frozen_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _frozen_dict_state_dict(xs):


def _restore_frozen_dict(xs, states):
diff = set(map(str, xs.keys())).difference(states.keys())
diff = set(map(str, xs.keys())).difference(map(str, states.keys()))
if diff:
raise ValueError(
'The target dict keys and state dict keys do not match, target dict'
Expand Down
12 changes: 12 additions & 0 deletions tests/linen/linen_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import contextlib
import copy
import dataclasses
import enum
import functools
import gc
import inspect
Expand Down Expand Up @@ -3037,6 +3038,17 @@ def __call__(self, x):
self.assertIn('next_layer_1', variables['params'])
self.assertNotIn('child_template', variables['params'])

def test_nonstring_keys_in_dict_on_module(self):
class MyEnum(str, enum.Enum):
a = 'a'
b = 'b'
class MyModule(nn.Module):
config: dict[MyEnum, int]
def __call__(self, inputs):
return inputs
module = MyModule(config={MyEnum.a: 1, MyEnum.b: 2})
variables = module.init(jax.random.key(0), jnp.zeros([0]))


class FrozenDictTests(absltest.TestCase):
def test_frozendict_flag(self):
Expand Down
Loading