-
Notifications
You must be signed in to change notification settings - Fork 663
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,29 +482,10 @@ def sow( | |
reduced_value = reduce_fn(init_fn(), value) | ||
setattr(self, name, variable_type(reduced_value)) | ||
|
||
def for_each( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be called There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
self, module_type: tp.Type[M], fn: tp.Callable[[M], None] | ||
) -> None: | ||
visited: tp.Set[ids.UUID] = set() | ||
self._on_all(module_type, fn, visited) | ||
|
||
def _on_all( | ||
self, | ||
module_type: tp.Type[M], | ||
fn: tp.Callable[[M], None], | ||
visited: tp.Set[ids.UUID], | ||
) -> None: | ||
if self._module__state.id in visited: | ||
return | ||
|
||
visited.add(self._module__state.id) | ||
|
||
if isinstance(self, module_type): | ||
fn(self) | ||
|
||
for value in vars(self).values(): | ||
def modules(self) -> tp.Iterator[tuple[Path, Module]]: | ||
for path, value in graph_utils.iter_nodes(self): | ||
if isinstance(value, Module): | ||
value._on_all(module_type, fn, visited) | ||
yield path, value | ||
|
||
def __init_subclass__(cls, experimental_pytree: bool = False) -> None: | ||
super().__init_subclass__() | ||
|
@@ -603,7 +584,7 @@ def first_from(arg_name: str, *args: tp.Optional[A]) -> A: | |
|
||
|
||
def merge( | ||
state_and_def: tuple[tpe.Unpack[tuple[State, ...]], GraphDef[M]] | ||
state_and_def: tuple[tpe.Unpack[tuple[State, ...]], GraphDef[M]], | ||
) -> M: | ||
*states, graphdef = state_and_def | ||
return graphdef.merge(*states) |
There was a problem hiding this comment.
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 thantp.Any
?There was a problem hiding this comment.
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.