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

Upgrade Flax NNX Bridge API doc #4198

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

Conversation

8bitmp3
Copy link
Collaborator

@8bitmp3 8bitmp3 commented Sep 16, 2024

No description provided.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB


Since Linen modules may require an input to create variables, we semi-formally supported lazy initialization in the NNX modules converted from Linen. The Linen variables are created when you give it a sample input.
Since `flax.linen.Module`s may require an input to create variables, the Flax team semi-formally supports lazy initialization in the `flax.nnx.Module`s converted from Flax Linen. The Flax Linen variables are created when you give it a sample input. For you, it's calling [`nnx.bridge.lazy_init`](https://flax.readthedocs.io/en/latest/api_reference/flax.nnx/bridge.html#flax.nnx.bridge.ToNNX.lazy_init) (`nnx.bridge.ToNNX.lazy_init`) where you call `module.init()` in the Flax Linen code.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cgarciae We should mention the ToNNX bridge here since it's used in the code snippet below.


### NNX to Linen

If you are not using any metadata feature of the `nnx.Variable` (i.e., no sharding annotation, no registered hooks), the converted Linen module will not add a metadata wrapper to your NNX variable, and you don't need to worry about it.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since this part was rewritten, will revert to the new version too @cgarciae

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you are not using any metadata features of the [`nnx.Variable`](https://flax.readthedocs.io/en/latest/api_reference/flax.nnx/variables.html#flax.nnx.Variable) - that is, no sharding annotation, no registered hooks, then the converted `flax.linen.Module` will not add a metadata wrapper to your Flax NNX variable, and you won't need to worry about it. (Recall that all Flax NNX variables are wrapped with `nnx.Variable` box.

But if you did add sharding annotations to your Flax NNX variables, then [`nnx.bridge.ToLinen`](https://flax.readthedocs.io/en/latest/api_reference/flax.nnx/bridge.html#flax.nnx.bridge.ToLinen) will convert them to a default Flax Linen partition metadata class called [`flax.nnx.bridge.NNXMeta`](https://flax.readthedocs.io/en/latest/api_reference/flax.nnx/bridge.html#flax.nnx.bridge.NNXMeta), retaining all the metadata you put into the NNX variable.

Similar to any Flax Linen metadata wrappers, you can use `flax.linen.unbox()` ([`flax.linen.meta.unbox`](https://github.com/google/flax/blob/5d31452889b8d106d7c722b5eaac14cb9784fec2/flax/core/meta.py#L160)) to get the raw [`jax.Array`](https://jax.readthedocs.io/en/latest/key-concepts.html#jax-arrays-jax-array) [pytree](https://jax.readthedocs.io/en/latest/glossary.html#term-pytree).

@8bitmp3 8bitmp3 force-pushed the upgrade-nnx-bridge branch 2 times, most recently from b62ae11 to 2f6e944 Compare October 7, 2024 12:53
@8bitmp3
Copy link
Collaborator Author

8bitmp3 commented Oct 8, 2024

TODO Fix the imports section, fix Markdown in the basic example, fix certain URL rendering.

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.

1 participant