
    BVhT                     x    d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	  e	dg        G d	 d
e
             Zy)z!Manages a Trackable object graph.    N)base)	converter)object_identity)	tf_exportztrain.TrackableView)v1c                   n    e Zd ZdZd Zeej                  j                  fd       Z	e
d        Zd Zd Zy)TrackableViewa   Gathers and serializes a trackable view.

  Example usage:

  >>> class SimpleModule(tf.Module):
  ...   def __init__(self, name=None):
  ...     super().__init__(name=name)
  ...     self.a_var = tf.Variable(5.0)
  ...     self.b_var = tf.Variable(4.0)
  ...     self.vars = [tf.Variable(1.0), tf.Variable(2.0)]

  >>> root = SimpleModule(name="root")
  >>> root.leaf = SimpleModule(name="leaf")
  >>> trackable_view = tf.train.TrackableView(root)

  Pass root to tf.train.TrackableView.children() to get the dictionary of all
  children directly linked to root by name.
  >>> trackable_view_children = trackable_view.children(root)
  >>> for item in trackable_view_children.items():
  ...   print(item)
  ('a_var', <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=5.0>)
  ('b_var', <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=4.0>)
  ('vars', ListWrapper([<tf.Variable 'Variable:0' shape=() dtype=float32,
  numpy=1.0>, <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>]))
  ('leaf', ...)

  c                 |    t        |t        j                        r|| _        yt        j                  |      | _        y)zConfigure the trackable view.

    Args:
      root: A `Trackable` object whose variables (including the variables of
        dependencies, recursively) should be saved. May be a weak reference.
    N)
isinstanceweakrefref	_root_ref)selfroots     [/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/checkpoint/trackable_view.py__init__zTrackableView.__init__7   s+     )w{{;dDN";;t, 	N    c                     |j                          i } |j                  |fi |j                         D ]!  \  }}t        j                  ||      }|||<   # |S )aA  Returns all child trackables attached to obj.

    Args:
      obj: A `Trackable` object.
      save_type: A string, can be 'savedmodel' or 'checkpoint'.
      **kwargs: kwargs to use when retrieving the object's children.

    Returns:
      Dictionary of all children attached to the object with name to trackable.
    )parent)_maybe_initialize_trackable_trackable_childrenitemsr   convert_to_trackable)clsobj	save_typekwargschildrennamer   s          r   r   zTrackableView.childrenE   se     ##%H,S,,YA&AGGI 	c**3s;chtn Or   c                     t        | j                  t        j                        r| j                         }|J |S | j                  S )N)r   r   r   r   )r   derefeds     r   r   zTrackableView.rootY   s;    $..'++. g   n^^r   c                 (    | j                         d   S )zKReturns a list of all nodes from self.root using a breadth first traversal.r   )_descendants_with_paths)r   s    r   descendantszTrackableView.descendantsb   s    '')!,,r   c                    g }t        j                  | j                  g      }t        j                         }d|| j                  <   |r|j                         }|j                  |       | j                  |      j                         D ];  \  }}||vs||   t        j                  ||      fz   ||<   |j                  |       = |r||fS )zYReturns a list of all nodes and its paths from self.root using a breadth first traversal. )collectionsdequer   r   ObjectIdentityDictionarypopleftappendr   r   r   TrackableReference)r   
bfs_sortedto_visit
node_pathscurrent_trackabler   
dependencys          r   r#   z%TrackableView._descendants_with_pathsf   s    J  $))-H 99;JJtyy
"**,)*"mm,=>DDF &
$
Z'*+&&tZ8:; Z
  //*
%&  z!!r   N)__name__
__module____qualname____doc__r   classmethodr   SaveType
CHECKPOINTr   propertyr   r$   r#   r&   r   r   r	   r	      sK    8. #'==#;#;  &  -"r   r	   )r5   r'   r   tensorflow.python.trackabler   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   objectr	   r&   r   r   <module>r>      sB    '   , 1 2 6  R(["F [" )["r   