o
    ?e                     @   sf   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j
ZdS )z%Manages a graph of Trackable objects.    N)save_util_v1)trackable_view)base)	tf_exportz%__internal__.tracking.ObjectGraphView)v1c                       s   e Zd ZdZdddZ fddZejjf fdd	Z	ejjfd	d
Z
edd Zedd Zdd Z fddZdddZ			dddZ  ZS )ObjectGraphViewz'Gathers and serializes an object graph.Nc                 C   s4   t j| | t|tjr|nt|| _|| _dS )a  Configure the graph view.

    Args:
      root: A `Trackable` object whose variables (including the variables of
        dependencies, recursively) should be saved. May be a weak reference.
      attached_dependencies: List of dependencies to attach to the root object.
        Used when saving a Checkpoint with a defined root object. To avoid
        reference cycles, this should use the WeakTrackableReference class.
    N)r   TrackableView__init__
isinstanceweakrefref	_root_ref_attached_dependencies)selfrootattached_dependencies r   h/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/checkpoint/graph_view.pyr	      s
   

zObjectGraphView.__init__c                    sz   |   }|d urt||}t||t| j < t t| }||t| < t	| 
 D ]\}}t||t|| q,|S N)r   copydeepcopyr   r   idsuper__new__typevarsitemssetattr)r   memoZstrong_rootZstrong_copyZcopiedkeyvalue	__class__r   r   __deepcopy__0   s   zObjectGraphView.__deepcopy__c                    s^   g }t t| j||fi | D ]\}}|t|| q|| ju r-| jr-|	| j |S )a,  Returns list of 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:
      List of all children attached to the object.
    )
r   r   childrenr   appendr   ZTrackableReferencer   r   extendr   obj	save_typekwargsr$   namer   r!   r   r   list_children?   s   
zObjectGraphView.list_childrenc                 K   s,   i }| j |fi |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.
    )r,   r'   r   r   r   r$   U   s   
zObjectGraphView.childrenc                 C   s   | j S )a"  Returns list of dependencies that should be saved in the checkpoint.

    These dependencies are not tracked by root, but are in the checkpoint.
    This is defined when the user creates a Checkpoint with both root and kwargs
    set.

    Returns:
      A list of TrackableReferences.
    )r   r   r   r   r   r   e   s   z%ObjectGraphView.attached_dependenciesc                 C   s,   t | jtjr|  }|d usJ |S | jS r   )r
   r   r   r   )r   Zderefedr   r   r   r   r   s
   zObjectGraphView.rootc                 C   s   |   S r   )_breadth_first_traversalr-   r   r   r   breadth_first_traversal{   s   z'ObjectGraphView.breadth_first_traversalc                    s   t t|  S )z5Find shortest paths to all dependencies of self.root.)r   r   Z_descendants_with_pathsr-   r!   r   r   r.   ~   s   z(ObjectGraphView._breadth_first_traversalc                 C   s   t | |\}}}}|||fS )ag  Determine checkpoint keys for variables and build a serialized graph.

    Non-slot variables are keyed based on a shortest path from the root saveable
    to the object which owns the variable (i.e. the one which called
    `Trackable._add_variable` to create it).

    Slot variables are keyed based on a shortest path to the variable being
    slotted for, a shortest path to their optimizer, and the slot name.

    Args:
      saveables_cache: An optional cache storing previously created
        SaveableObjects created for each Trackable. Maps Trackables to a
        dictionary of attribute names to Trackable.

    Returns:
      A tuple of (named_variables, object_graph_proto, feed_additions):
        named_variables: A dictionary mapping names to variable objects.
        object_graph_proto: A TrackableObjectGraph protocol buffer
          containing the serialized object graph and variable references.
        feed_additions: A dictionary mapping from Tensors to values which should
          be fed when saving.

    Raises:
      ValueError: If there are invalid characters in an optimizer's slot names.
    )r   Z-serialize_object_graph_with_registered_savers)r   Zsaveables_cacheZnamed_saveable_objectsZobject_graph_protoZfeed_additions_r   r   r   serialize_object_graph   s
   

z&ObjectGraphView.serialize_object_graphc                 C   s   t | |||d S )z=Creates SaveableObjects with the current object graph frozen.r   )r   Zfrozen_saveables_and_savers)r   Z
object_mapZto_graphZcall_with_mapped_capturesr   r   r   frozen_saveable_objects   s
   z'ObjectGraphView.frozen_saveable_objectsr   )NNN)__name__
__module____qualname____doc__r	   r#   r   ZSaveType
CHECKPOINTr,   r$   propertyr   r   r/   r.   r1   r2   __classcell__r   r   r!   r   r      s"    



 r   )r6   r   r   Ztensorflow.python.checkpointr   r   Ztensorflow.python.trackabler   Z tensorflow.python.util.tf_exportr   r   r   r   r   r   r   <module>   s    
