Skip to content

nnx.clone: add arrays parameter to also copy underlying buffers - #5475

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
dparikh79:fix/5461-nnx-clone-shares-buffers
Jun 29, 2026
Merged

copybara-service[bot] merged 1 commit into
google:mainfrom
dparikh79:fix/5461-nnx-clone-shares-buffers

Conversation

@dparikh79

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds arrays=True to nnx.clone per #5461. When set, the underlying jax.Array buffers of each Variable are copied so the clone has independent memory and works with jit(donate_argnums=...). Default False so existing callers see no change. Setting arrays=True forces variables=True because a buffer copy without a fresh wrapper has nowhere to live.

Implementation: jax.tree.map(jax.numpy.copy, state) between split and merge. Used jnp.copy rather than jnp.array to preserve sharding on multi-device arrays.

Expanded the docstring to call out the default buffer-sharing per @samanklesaria's note that it should be better documented.

Two regression tests in tests/nnx/module_test.py. test_clone_arrays_distinct_buffers asserts unsafe_buffer_pointer() differs after arrays=True and matches for the default clone. test_clone_arrays_donate_argnums is @johnlyzhou's exact donate_argnums repro from the issue. Existing test_clone still passes. Full sweep: 77 passed / 1 skipped in module_test.py, 152 passed in graph_utils_test.py.

Fixes #5461

Checklist

@samanklesaria samanklesaria left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me!

Comment thread flax/nnx/graphlib.py
A deep copy of the :class:`Module` object.
"""
graphdef, state = split(node, graph=graph)
if arrays:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we should raise a ValueError if arrays=True and variables=False as this implementation currently does not support it.

Comment thread flax/nnx/graphlib.py Outdated
graphdef, state = split(node, graph=graph)
if arrays:
state = jax.tree.map(jax.numpy.copy, state)
variables = True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

since the jax.tree.map effectively creates copies of all the Variables then `you could actually set:

Suggested change
variables = True
variables = False

@vfdev-5

vfdev-5 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@dparikh79 please update the PR according to the suggestions. Thanks!

@vfdev-5 vfdev-5 self-assigned this Jun 26, 2026
@vfdev-5
vfdev-5 force-pushed the fix/5461-nnx-clone-shares-buffers branch from c2afbbb to b1b1c18 Compare June 29, 2026 21:01
@copybara-service
copybara-service Bot merged commit b196b01 into google:main Jun 29, 2026
26 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.

nnx.clone creates buffer copies with the same IDs, causing errors with donate_argnums

4 participants