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

Add StateVariablesMapping #3523

Merged
merged 3 commits into from
Dec 16, 2023
Merged

Add StateVariablesMapping #3523

merged 3 commits into from
Dec 16, 2023

Conversation

cgarciae
Copy link
Collaborator

@cgarciae cgarciae commented Nov 30, 2023

What does this PR do?

  • Renames VariablesMapping to ModuleVariablesMapping
  • State.variables now returns a StateVariablesMapping with a similar behavior to ModuleVariablesMapping.
  • Adds State.raw_mapping which exposes ._mapping

@codecov-commenter
Copy link

codecov-commenter commented Nov 30, 2023

Codecov Report

Attention: 18 lines in your changes are missing coverage. Please review.

Comparison is base (c0a2657) 56.21% compared to head (b4f7bed) 56.23%.

Files Patch % Lines
flax/experimental/nnx/nnx/state.py 64.10% 14 Missing ⚠️
flax/experimental/nnx/nnx/module.py 66.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3523      +/-   ##
==========================================
+ Coverage   56.21%   56.23%   +0.02%     
==========================================
  Files         100      100              
  Lines       11849    11882      +33     
==========================================
+ Hits         6661     6682      +21     
- Misses       5188     5200      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cgarciae cgarciae marked this pull request as ready for review November 30, 2023 21:39
raise AttributeError(f"Variable '{name}' not found.")
return value

def __setattr__(self, name: str, value: Variable[tp.Any]) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend to constrain the API and allow either ["x"] or .x but not both (possibly in a separate PR).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this dual API in a couple of place in NNX, the main idea is to make intuitive both hard-coded and programmatic access, e.g:

# hard-coded
x = state.linear.kernel
# programmatic
x = state[module_name][param_name]

@@ -72,9 +124,13 @@ def __init__(
super().__setattr__('_mapping', dict(mapping))

@property
def variables(self) -> dict[Key, dict[str, tp.Any] | tp.Any]:
def raw_mapping(self) -> dict[Key, dict[str, tp.Any] | tp.Any]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another naming idea: storage.

@@ -72,9 +124,13 @@ def __init__(
super().__setattr__('_mapping', dict(mapping))

@property
def variables(self) -> dict[Key, dict[str, tp.Any] | tp.Any]:
def raw_mapping(self) -> dict[Key, dict[str, tp.Any] | tp.Any]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to return a read-only view of the underlying mapping, e.g. wrapped in types.MappingProxyType?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we a embracing mutability in case the user wants to do some surgery.

@copybara-service copybara-service bot merged commit b468207 into main Dec 16, 2023
21 checks passed
@copybara-service copybara-service bot deleted the improve-variables-mapping branch December 16, 2023 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants