Skip to content

[nnx] add Pytree #4625

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

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

[nnx] add Pytree #4625

wants to merge 1 commit into from

Conversation

cgarciae
Copy link
Collaborator

What does this PR do?

Adds the nnx.Pytree type that can be used to interact more easily with certain JAX APIs that expect callable stateless pytrees such as odeint or even other pytree-based Module systems. nnx.Pytree is a frozen view of any NNX object that is both a pytree and acts as a proxy over the object's methods.

from flax import nnx
from jax.experimental import ode

model = nnx.Linear(2, 2, rngs=nnx.Rngs(0)),
jax_model = nnx.Pytree(model)

y0 = jax.numpy.array([0.5, -0.5])

def dy_dt(y, t, jax_model):
  return jax_model(y)

t = jax.numpy.linspace(0, 1, 10)
y = ode.odeint(dy_dt, y0, t, jax_model)

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

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