Repository navigation
nnx.clone: add arrays parameter to also copy underlying buffers - #5475
Merged
copybara-service[bot] merged 1 commit intoJun 29, 2026
Merged
copybara-service[bot] merged 1 commit into
copybara-service[bot] merged 1 commit into
Conversation
samanklesaria
approved these changes
Jun 1, 2026
samanklesaria
left a comment
Collaborator
There was a problem hiding this comment.
Looks good to me!
cgarciae
reviewed
Jun 5, 2026
| A deep copy of the :class:`Module` object. | ||
| """ | ||
| graphdef, state = split(node, graph=graph) | ||
| if arrays: |
Collaborator
There was a problem hiding this comment.
we should raise a ValueError if arrays=True and variables=False as this implementation currently does not support it.
cgarciae
reviewed
Jun 5, 2026
| graphdef, state = split(node, graph=graph) | ||
| if arrays: | ||
| state = jax.tree.map(jax.numpy.copy, state) | ||
| variables = True |
Collaborator
There was a problem hiding this comment.
since the jax.tree.map effectively creates copies of all the Variables then `you could actually set:
Suggested change
| variables = True | |
| variables = False |
cgarciae
requested changes
Jun 5, 2026
Collaborator
|
@dparikh79 please update the PR according to the suggestions. Thanks! |
vfdev-5
force-pushed
the
fix/5461-nnx-clone-shares-buffers
branch
from
June 29, 2026 21:01
c2afbbb to
b1b1c18
Compare
cgarciae
approved these changes
Jun 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds
arrays=Truetonnx.cloneper #5461. When set, the underlyingjax.Arraybuffers of eachVariableare copied so the clone has independent memory and works withjit(donate_argnums=...). DefaultFalseso existing callers see no change. Settingarrays=Trueforcesvariables=Truebecause a buffer copy without a fresh wrapper has nowhere to live.Implementation:
jax.tree.map(jax.numpy.copy, state)betweensplitandmerge. Usedjnp.copyrather thanjnp.arrayto 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_buffersassertsunsafe_buffer_pointer()differs afterarrays=Trueand matches for the default clone.test_clone_arrays_donate_argnumsis @johnlyzhou's exactdonate_argnumsrepro from the issue. Existingtest_clonestill passes. Full sweep: 77 passed / 1 skipped inmodule_test.py, 152 passed ingraph_utils_test.py.Fixes #5461
Checklist