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

Fixes #1925 non-str dict keys not suppoted in module state #4563

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

muhrin
Copy link

@muhrin muhrin commented Feb 21, 2025

In PR #2007, the issue of having a dict with non-string keys as linen module parameters was fixed but the related issue of having non-string keys as part of the state dict was not. Specifically, these cases did not work:

a = {1: 2}

class Foo(nn.Module):
  def setup(self):
    self.a = FrozenDict(a)  # int as key.

class Foo(nn.Module):
  a: dict[int, int]  # int as key.

this is because serialization for non-int keys was fixes for dict but not for FrozenDict. This uses the code from dict to add the same behaviour to FrozenDict.

What does this PR do?

Fixes #1925

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs.
  • This change is discussed in a Github issue/
    discussion .
  • The documentation and docstrings adhere to the
    documentation guidelines.
  • This change includes necessary high-coverage tests.

In PR google#2007, the issue of having a dict with non-string keys as linen
module parameters was fixed but the related issue of having non-string
keys as part of the state dict was not.  Specifically, these cases did
not work:

    a = {1: 2}

    class Foo(nn.Module):
      def setup(self):
        self.a = FrozenDict(a)  # int as key.

    class Foo(nn.Module):
      a: dict[int, int]  # int as key.

this is because serialization for non-int keys was fixes for ``dict``
but not for ``FrozenDict``.  This uses the code from ``dict`` to add the
same behaviour to ``FrozenDict``.
Copy link

google-cla bot commented Feb 21, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@muhrin muhrin changed the title Fixes #1925 Fixes #1925 non-str dict keys not suppoted in module state Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not assign dict whos key is not string as module attribute
1 participant