Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flax/nnx/graphlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,11 @@ def _graph_flatten(

is_graph_node_ = type(node_impl) is GraphNodeImpl
is_variable = isinstance(node, Variable)
is_array_ref = variablelib.is_array_ref(node)
is_array_ref = (
False
if is_graph_node_ or is_variable or is_pytree_node_
else variablelib.is_array_ref(node)
)

# only cache graph nodes, we don't add array refs here
# as they are added in the make_mutable_arraydef function
Expand Down
12 changes: 9 additions & 3 deletions flax/nnx/variablelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,15 @@ def var_defaults_wrapper(*args, **kwargs):


def is_array_ref(x) -> tp.TypeGuard[Ref]:
return isinstance(x, jax.Array | AbstractRef | Ref) and isinstance(
jax.typeof(x), AbstractRef | Ref
)
if isinstance(x, Ref):
return True
if isinstance(x, AbstractRef):
return isinstance(jax.typeof(x), AbstractRef | Ref)
if not isinstance(x, jax.Array):
return False
if not isinstance(x, jax.core.Tracer):
return False
return isinstance(jax.typeof(x), AbstractRef | Ref)


@dataclasses.dataclass
Expand Down