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 NNX transforms nnx.while_loop and nnx.switch #4343

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

IvyZX
Copy link
Collaborator

@IvyZX IvyZX commented Oct 28, 2024

  • Implemented nnx.switch similar to nnx.cond

  • Implemented nnx.while_loop

    • Due to constraints of jax.lax.while_loop, no reference structure change is allowed inside nnx.while_loop for NNX objects.

global_index_mapping = {}
if not isinstance(ns, extract.NodeStates):
return ns
assert isinstance(ns._graphdef, graph.NodeDef)
Copy link
Collaborator

@cgarciae cgarciae Oct 31, 2024

Choose a reason for hiding this comment

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

When you have a repeated NNX object in the pytree processed by to_tree you will get a NodeRef here. I tested this example that fails (can we add it to the test cases?).

  def test_repeated_object(self):
    m = nnx.Linear(10, 10, rngs=nnx.Rngs(0))

    def body_fn(val):
      count, m, _ = val
      return count + 1, m, m

    count, m, _ = nnx.while_loop(
      lambda val: val[0] < 2,
      body_fn,
      (0, m, m),
    )

To fix it you can probably just return.

Suggested change
assert isinstance(ns._graphdef, graph.NodeDef)
if not isinstance(ns._graphdef, graph.NodeDef):
return ns

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great catch, thanks!

def per_node_state(ns: extract.NodeStates | tp.Any):
if not isinstance(ns, extract.NodeStates):
return ns
assert isinstance(ns._graphdef, graph.NodeDef)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as before.

init_val: T) -> T:
"""NNX transform of `jax.lax.while_loop`.

See: https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.while_loop.html
Copy link
Collaborator

Choose a reason for hiding this comment

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

More friendly link.

Suggested change
See: https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.while_loop.html
See `jax.lax.while_loop <https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.while_loop.html>`_.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or maybe we link be above mention?

"""NNX transform of `jax.lax.while_loop`.

See: https://jax.readthedocs.io/en/latest/_autosummary/jax.lax.while_loop.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a simple example usage here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@cgarciae
Copy link
Collaborator

BTW: Can we move while_loop to iteration.py? Been meaning to remove transforms.py which previously contained all the transforms in single file that became too big.

@copybara-service copybara-service bot merged commit 7234157 into google:main Oct 31, 2024
17 checks passed
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