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

[nnx] add submodule iterator #3581

Merged
merged 1 commit into from
Jan 4, 2024
Merged

Conversation

cgarciae
Copy link
Collaborator

What does this PR do?

Adds method to iterator over all unique submodules.

@codecov-commenter
Copy link

codecov-commenter commented Dec 21, 2023

Codecov Report

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

Comparison is base (85eb8c0) 56.31% compared to head (1e75509) 56.46%.

Files Patch % Lines
flax/experimental/nnx/nnx/graph_utils.py 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3581      +/-   ##
==========================================
+ Coverage   56.31%   56.46%   +0.15%     
==========================================
  Files         100      100              
  Lines       11973    11994      +21     
==========================================
+ Hits         6742     6773      +31     
+ Misses       5231     5221      -10     

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

@@ -482,29 +482,10 @@ def sow(
reduced_value = reduce_fn(init_fn(), value)
setattr(self, name, variable_type(reduced_value))

def for_each(
Copy link
Member

@superbobry superbobry Dec 21, 2023

Choose a reason for hiding this comment

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

Should this be called submodules and not module? Or better yet iter_submodules?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Called it modules() as it would be familiar to Pytorch users. Wondering if we should try to follow their conventions when possible?

Copy link
Member

@superbobry superbobry left a comment

Choose a reason for hiding this comment

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

I would recommend adding a few tests for the new API.

Comment on lines +724 to +732
def iter_nodes(node: tp.Any) -> tp.Iterator[tuple[Path, tp.Any]]:
visited: set[int] = set()
path_parts: PathParts = ()
yield from _iter_nodes(node, visited, path_parts)


def _iter_nodes(
node: tp.Any, visited: set[int], path_parts: PathParts
Copy link
Collaborator

@chiamp chiamp Dec 21, 2023

Choose a reason for hiding this comment

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

can the node args have a more specific type annotation than tp.Any?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

problem is that a node could be of any type that is registered.

@cgarciae
Copy link
Collaborator Author

cgarciae commented Jan 3, 2024

@superbobry added test.

@copybara-service copybara-service bot merged commit 3b23351 into main Jan 4, 2024
21 checks passed
@copybara-service copybara-service bot deleted the nnx-submodule-iterator branch January 4, 2024 10:06
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.

4 participants