
    BVh                         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j                               Zy)z%Manages a graph of Trackable objects.    N)save_util_v1)trackable_view)base)	tf_exportz%__internal__.tracking.ObjectGraphView)v1c                        e Zd ZdZddZ fdZej                  j                  f fd	Z	ej                  j                  fdZ
ed        Zed        Zd Z fd	Zdd
Z	 	 	 ddZ xZS )ObjectGraphViewz'Gathers and serializes an object graph.c                     t         j                  j                  | |       t        |t        j
                        r|nt	        j
                  |      | _        || _        y)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_dependenciess      W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/checkpoint/graph_view.pyr   zObjectGraphView.__init__   sG       ))$5
 )w{{;d";;t, 	N"7D    c           	      ~   | j                         }|At        j                  ||      }t        j                  |      |t        | j                         <   t        |   t        |             }||t        |       <   t        |       j                         D ]&  \  }}t        ||t        j                  ||             ( |S N)r   copydeepcopyr   r   idsuper__new__typevarsitemssetattr)r   memostrong_rootstrong_copycopiedkeyvalue	__class__s          r   __deepcopy__zObjectGraphView.__deepcopy__0   s     .."KMM+t4k!([!9d2dnnW_T$Z(FDDN4j&&( 7
Ufc4==567Mr   c                    g }t        t        | 
  ||fi |j                         D ]*  \  }}|j	                  t        j                  ||             , || j                  u r'| j                  r|j                  | 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   TrackableReferencer   r   extend)r   obj	save_typekwargsr+   namer   r(   s          r   list_childrenzObjectGraphView.list_children?   s     H?**-yD<BDDIEG:	cood--dC89: diiD77ood112Or   c                 J    i } | j                   |fi |D ]
  \  }}|||<    |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.
    )r3   )r   r/   r0   r1   r+   r2   r   s          r   r+   zObjectGraphView.childrenU   s<     H'T''6v6 	chtnOr   c                     | 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   s    r   r   z%ObjectGraphView.attached_dependenciese   s     &&&r   c                     t        | j                  t        j                        r| j                         }|J |S | j                  S r   )r   r   r   r   )r   derefeds     r   r   zObjectGraphView.rootr   s;    $..'++. g   n^^r   c                 "    | j                         S r   )_breadth_first_traversalr6   s    r   breadth_first_traversalz'ObjectGraphView.breadth_first_traversal{   s    ((**r   c                 (    t         t        |          S )z5Find shortest paths to all dependencies of self.root.)r   r	   _descendants_with_paths)r   r(   s    r   r:   z(ObjectGraphView._breadth_first_traversal~   s    $?AAr   c                 B    t        j                  | |      \  }}}}|||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   -serialize_object_graph_with_registered_savers)r   saveables_cachenamed_saveable_objectsobject_graph_protofeed_additions_s         r   serialize_object_graphz&ObjectGraphView.serialize_object_graph   s7    6 	BB/	# B. "#5~EEr   c                 8    t        j                  | |||      d   S )z=Creates SaveableObjects with the current object graph frozen.r   )r   frozen_saveables_and_savers)r   
object_mapto_graphcall_with_mapped_capturess       r   frozen_saveable_objectsz'ObjectGraphView.frozen_saveable_objects   s+    
 33j($=??@B Br   r   )NNN)__name__
__module____qualname____doc__r   r)   r   SaveType
CHECKPOINTr3   r+   propertyr   r   r;   r:   rE   rK   __classcell__)r(   s   @r   r	   r	      s    /8& *.)A)A , %)MM$<$<   
' 
'  +BF@ *.'+8<Br   r	   )rO   r   r   tensorflow.python.checkpointr   r   tensorflow.python.trackabler    tensorflow.python.util.tf_exportr   r   r	    r   r   <module>rX      sK    +   5 7 , 6 2r:MBn22 MB ;MBr   