o
    2hT                     @   sd   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 dG d	d
 d
e
ZdS )z!Manages a Trackable object graph.    N)base)	converter)object_identity)	tf_exportztrain.TrackableView)v1c                   @   sH   e Zd ZdZdd ZeejjfddZ	e
dd Zdd	 Zd
d ZdS )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                 C   s&   t |tjr|| _dS t|| _dS )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root r   f/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/checkpoint/trackable_view.py__init__7   s   zTrackableView.__init__c                 K   sF   |   i }|j|fi | D ]\}}tj||d}|||< q|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
   r   r   r   r   E   s   
zTrackableView.childrenc                 C   s,   t | jtjr|  }|d usJ |S | jS )N)r   r   r	   r
   )r   derefedr   r   r   r   Y   s
   zTrackableView.rootc                 C   s   |   d S )zKReturns a list of all nodes from self.root using a breadth first traversal.r   )_descendants_with_paths)r   r   r   r   descendantsb   s   zTrackableView.descendantsc                 C   s   g }t | jg}t }d|| j< |rA| }|| | | D ]\}}||vr>|| t	
||f ||< || q$|s||fS )zYReturns a list of all nodes and its paths from self.root using a breadth first traversal.r   )collectionsdequer   r   ObjectIdentityDictionarypopleftappendr   r   r   TrackableReference)r   
bfs_sortedto_visit
node_pathscurrent_trackabler   
dependencyr   r   r   r   f   s"   


	z%TrackableView._descendants_with_pathsN)__name__
__module____qualname____doc__r   classmethodr   SaveType
CHECKPOINTr   propertyr   r   r   r   r   r   r   r      s    
r   )r-   r   r	   tensorflow.python.trackabler   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   objectr   r   r   r   r   <module>   s    
