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

[nnx] add support for standalone Variables #4606

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

Conversation

cgarciae
Copy link
Collaborator

@cgarciae cgarciae commented Mar 7, 2025

What does this PR do?

Adds support for using Variables outside of graph nodes, meaning they can now be passed directly to transforms, including inside pytrees without a parent graph node, and the ability to call split, merge, and update on them directly.

rngs = nnx.Rngs(0)
w = nnx.Param(jax.random.normal(rngs(), (2, 3)))
b = nnx.Param(jnp.zeros((3,)))
count = nnx.Variable(jnp.array(0))

@nnx.jit
def linear(w, b, count, x):
  count += 1
  return x @ w + b[None]

x = jax.random.normal(rngs(), (1, 2))
y = linear(w, b, count, x)

assert count.value == 1
assert y.shape == (1, 3)

@cgarciae cgarciae force-pushed the nnx-standalone-variables branch 3 times, most recently from 21852a6 to fc95f37 Compare March 8, 2025 00:59
@cgarciae cgarciae marked this pull request as ready for review March 8, 2025 01:04
@cgarciae cgarciae force-pushed the nnx-standalone-variables branch 2 times, most recently from 191aa2e to a3f07ad Compare March 8, 2025 01:46
@cgarciae cgarciae force-pushed the nnx-standalone-variables branch from a3f07ad to 0100223 Compare March 10, 2025 00:15
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