code: def _tree_check(node, all_nodes):
      subnodes, treedef = tree_flatten_one_level(node)
      # We allow duplicate leaves and empty containers, so don't raise an error with those
      if treedef != _leaf_treedef and treedef.num_leaves > 0:
          try:
              self_referential, type_string = all_nodes[id(node)]
          except KeyError:
              pass
          else:
              if self_referential:
                  raise ValueError(
                      f"PyTree node of type `{type_string}` is self-referential; that is "
                      "to say it appears somewhere within its own PyTree structure. This "
                      "is not allowed."
                  )
              else:
                  raise ValueError(
                      f"PyTree node of type `{type_string}` appears in the PyTree "
                      "multiple times. This is almost always an error, as these nodes "
                      "will turn into two duplicate copies after "
                      "flattening/unflattening, e.g. when crossing a JIT boundary."
                  )
          try:
              type_string = type(node).__name__
          except AttributeError:
              # AttributeError: in case we cannot get __name__ for some weird reason.
              type_string = "<unknown type>"
          all_nodes[id(node)] = (True, type_string)
          for subnode in subnodes:
              _tree_check(subnode, all_nodes)
          all_nodes[id(node)] = (False, type_string)
  
imports: from collections.abc import Callable, Sequence
  from typing import Any, Optional, TYPE_CHECKING, Union
  import jax.numpy
  import jax.tree_util
  import numpy
  from jaxtyping import Array, ArrayLike, Bool, Float, PyTree, PyTreeDef
  from ._custom_types import sentinel
  from ._doc_utils import doc_repr
  from ._filters import is_array
label_desc: Use image node_id 7 for calling a global function with example usage: _tree_check(node, all_nodes) without return types