-
Notifications
You must be signed in to change notification settings - Fork 828
[nnx] add compat #3921
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
Merged
Merged
[nnx] add compat #3921
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
|
|
||
| from .module import ModuleMeta as ModuleMeta | ||
| from .module import Module as Module | ||
| from .module import Scope as Scope | ||
| from .module import compact as compact | ||
| from .wrappers import functional as functional | ||
| from .wrappers import LinenWrapper as LinenWrapper | ||
| from .wrappers import Functional as Functional | ||
| from .wrappers import NNXWrapper as NNXWrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| # Copyright 2024 The Flax Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections import defaultdict | ||
| import dataclasses | ||
| import functools | ||
| import threading | ||
| import typing as tp | ||
| import typing_extensions as tpe | ||
|
|
||
| from flax.experimental.nnx.nnx import graph, rnglib | ||
| import flax.experimental.nnx.nnx.module as nnx_module | ||
| from flax.experimental.nnx.nnx.proxy_caller import ( | ||
| CallableProxy, | ||
| DelayedAccessor, | ||
| ) | ||
| from flax.experimental.nnx.nnx.object import Object | ||
|
|
||
| M = tp.TypeVar('M', bound='Module') | ||
| F = tp.TypeVar('F', bound=tp.Callable[..., tp.Any]) | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class CompactContext: | ||
| module: 'Module' | ||
| type_counter: defaultdict[type, int] = dataclasses.field( | ||
| default_factory=lambda: defaultdict(lambda: 0) | ||
| ) | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class ModuleContext(threading.local): | ||
| parent_stack: list[tp.Optional[CompactContext]] = dataclasses.field( | ||
| default_factory=lambda: [None] | ||
| ) | ||
|
|
||
|
|
||
| MODULE_CONTEXT = ModuleContext() | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class Scope(Object): | ||
| rngs: rnglib.Rngs | ||
|
|
||
|
|
||
| @tp.runtime_checkable | ||
| class _HasSetup(tp.Protocol): | ||
| def setup(self) -> None: ... | ||
|
|
||
|
|
||
| class ModuleMeta(nnx_module.ModuleMeta): | ||
| if not tp.TYPE_CHECKING: | ||
|
|
||
| def __call__(cls, *args, **kwargs): | ||
| return _module_meta_call(cls, *args, **kwargs) | ||
|
|
||
|
|
||
| def _module_meta_call(cls: tp.Type[M], *args, **kwargs) -> M: | ||
| # compact behavior | ||
| parent_ctx = MODULE_CONTEXT.parent_stack[-1] | ||
| parent = None | ||
| module: M | ||
|
|
||
| if parent_ctx is not None: | ||
| if 'parent' in kwargs: | ||
| parent = kwargs.pop('parent') | ||
| if parent is not None: | ||
| raise ValueError( | ||
| f"'parent' can only be set to None, got {type(parent).__name__}" | ||
| ) | ||
| name = None | ||
| else: | ||
| type_index = parent_ctx.type_counter[cls] | ||
| parent_ctx.type_counter[cls] += 1 | ||
|
|
||
| # define the name | ||
| if 'name' in kwargs: | ||
| name = kwargs.pop('name') | ||
| if not isinstance(name, str): | ||
| raise ValueError(f"'name' must be a 'str', got {type(name).__name__}") | ||
| else: | ||
| name = f'{cls.__name__}_{type_index}' | ||
|
|
||
| parent = parent_ctx.module | ||
|
|
||
| if hasattr(parent, name): | ||
| module = getattr(parent, name) | ||
| return module | ||
| else: | ||
| name = None | ||
|
|
||
| module = nnx_module.ModuleMeta.__call__(cls, *args, **kwargs) | ||
| module.scope = None | ||
|
|
||
| if parent is not None: | ||
| assert name is not None | ||
| setattr(parent, name, module) | ||
| # adopt the parent scope | ||
| module.scope = parent.scope | ||
|
|
||
| if dataclasses.is_dataclass(module): | ||
| if isinstance(module, _HasSetup): | ||
| module.setup() | ||
|
|
||
| return module | ||
|
|
||
|
|
||
| class ModuleBase: | ||
| if tp.TYPE_CHECKING: | ||
| scope: Scope | None | ||
|
|
||
|
|
||
| @tpe.dataclass_transform(field_specifiers=(dataclasses.field,)) # type: ignore[not-supported-yet] | ||
| class Module(nnx_module.Module, ModuleBase, metaclass=ModuleMeta): | ||
| def _set_scope(self, scope: Scope | None): | ||
| """Recursively sets the scope for the Module and its children.""" | ||
| for _, value in graph.iter_graph(self): | ||
| if isinstance(value, Module): | ||
| value.scope = scope | ||
|
|
||
| @property | ||
| def init(self: M) -> M: | ||
| """Calls a method in initialization mode. | ||
|
|
||
| When a method is called using ``init``, the ``is_initializing`` method | ||
| will return ``True``. This is useful to implement Modules that support | ||
| lazy initialization. | ||
|
|
||
| Example:: | ||
|
|
||
| >>> from flax.experimental import nnx | ||
| >>> from flax.experimental.nnx import compat as nnc | ||
| >>> import jax | ||
| >>> import jax.numpy as jnp | ||
| ... | ||
| >>> class Linear(nnc.Module): | ||
| ... def __init__(self, dout, rngs: nnx.Rngs): | ||
| ... self.dout = dout | ||
| ... self.rngs = rngs | ||
| ... | ||
| ... def __call__(self, x): | ||
| ... if self.is_initializing(): | ||
| ... din = x.shape[-1] | ||
| ... if not hasattr(self, 'w'): | ||
| ... key = self.rngs.params() | ||
| ... self.w = nnx.Param(jax.random.uniform(key, (din, self.dout))) | ||
| ... if not hasattr(self, 'b'): | ||
| ... self.b = nnx.Param(jnp.zeros((self.dout,))) | ||
| ... | ||
| ... return x @ self.w + self.b | ||
| ... | ||
| >>> linear = Linear(3, nnx.Rngs(0)) | ||
| >>> x = jnp.ones((5, 2)) | ||
| >>> y = linear.init(x) | ||
| >>> linear.w.value.shape | ||
| (2, 3) | ||
| >>> linear.b.value.shape | ||
| (3,) | ||
| >>> y.shape | ||
| (5, 3) | ||
| """ | ||
|
|
||
| def _init_context(accessor: DelayedAccessor, *args, **kwargs): | ||
| for _, value in graph.iter_graph(self): | ||
| if isinstance(value, Object): | ||
| value._object__state._initializing = True | ||
|
|
||
| method = accessor(self) | ||
| try: | ||
| out = method(*args, **kwargs) | ||
| finally: | ||
| for _, value in graph.iter_graph(self): | ||
| if isinstance(value, Object): | ||
| value._object__state._initializing = False | ||
|
|
||
| return out | ||
|
|
||
| return CallableProxy(_init_context) # type: ignore | ||
|
|
||
| def is_initializing(self) -> bool: | ||
| """Returns whether the Module is initializing. | ||
|
|
||
| ``is_initializing`` returns ``True`` if the Module is currently being run | ||
| under ``init``. | ||
| """ | ||
|
|
||
| return self._object__state._initializing | ||
|
|
||
| def __init_subclass__(cls, experimental_pytree: bool = False) -> None: | ||
| super().__init_subclass__(experimental_pytree) | ||
|
|
||
| cls = dataclasses.dataclass(repr=False)(cls) | ||
|
|
||
|
|
||
| def compact(f: F) -> F: | ||
| @functools.wraps(f) | ||
| def compact_wrapper(self, *args, **kwargs): | ||
| if not isinstance(self, Module): | ||
| raise ValueError( | ||
| f"Expected 'self' to be a nnx.compat.Module, got {type(self).__name__}" | ||
| ) | ||
|
|
||
| MODULE_CONTEXT.parent_stack.append(CompactContext(self)) | ||
|
|
||
| try: | ||
| return f(self, *args, **kwargs) | ||
| finally: | ||
| MODULE_CONTEXT.parent_stack.pop() | ||
|
|
||
| return compact_wrapper # type: ignore | ||
|
|
||
|
|
||
| # register Module as a dataclass_transform | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For clarification:
__init__method, we will be able to instantiate module parameters using the.initmethod?setupmethod or wrap__call__withcompact, we will be able to instantiate the module parameters by calling the module on a sample input and invoking shape inference?__init__method, can we define asetupmethod or wrap__call__withcompactto use the.initmethod as well?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.
Sorry for the confusion here, this
initmethod is the currentinitwe have fornnx.Modulebut we are just moving it out tocompat.Module, however its still need to create refactor the method so it follows the Linen API as closely as possible in a subsequent PR.