o
    e6                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ dd	 Zd
d Zdd Zdd Zdd Zdd ZdS )z6Utilities for extracting and writing checkpoint info`.    )trackable_object_graph_pb2)resource_variable_ops)	variables)trackable_utils)	optimizer)object_identityc                 C   s   t | }t }|D ]}t|tjsdt|v r| }|D ]n}t|D ]g\}}	z|	|	|}
W n t
tfy=   d}
Y nw |
du rCq$|
  |
 rOtd|
|v r[td|
 dtj||	 || |d}|||
< t| }|||
< | |
 tjjj|||d}||g | q$qq
|S )zGather and name slot variables.get_slot_namesNzCurrently only variables with no dependencies can be saved as slot variables. File a feature request if this limitation bothers you.zCA slot variable was re-used as a dependency of a Trackable object: zW. This is not currently allowed. File a feature request if this limitation bothers you.)Zvariable_pathZoptimizer_path	slot_name)r	   original_variable_node_idslot_variable_node_id)listr   ObjectIdentityDictionary
isinstanceoptimizer_v1Z	Optimizerdirr   	enumerateZget_slotAttributeErrorKeyErrorZ_maybe_initialize_trackableZ_trackable_childrenNotImplementedErrorr   Zslot_variable_keylenappendr   ZTrackableObjectGraphZTrackableObjectZSlotVariableReference
setdefault)trackable_objectsnode_idsobject_namesZnon_slot_objectsslot_variables	trackableZ
slot_namesr	   r
   Zoriginal_variableZslot_variableZcheckpoint_namer   Zslot_variable_proto r   QD:\Projects\ConvertPro\env\Lib\site-packages\tensorflow/python/checkpoint/util.pyserialize_slot_variables   s`   
#r   c                 C   s   |du r| S | | | S )zFReturns the mapped trackable if possible, otherwise returns trackable.N)get)r   Z
object_mapr   r   r   get_mapped_trackableK   s   r!   c                 C   s8   t | tjst| sdS t| dddur| jjS | jS )zFGets the full name of variable for name-based checkpoint compatiblity. _save_slice_infoN)	r   r   Variabler   Zis_resource_variablegetattrr#   Z	full_nameZ_shared_name)varr   r   r   get_full_nameS   s   r'   c                 C   s   i }t  }t }t| jD ]-\}}|js|js|dr"|| |j	D ]}|j
}||vr3t ||< || | q%qt }|| |ri| }||vrOqD||}	||	 |	D ]}
|
|v rf||
 q[|sFt| jD ]\}}t||v |j_qndS )zDetermines which objects have checkpoint values and save this to the proto.

  Args:
    object_graph_proto: A `TrackableObjectGraph` proto.
  Zregistered_saverN)r   ZObjectIdentitySetsetr   nodes
attributesr   ZHasFieldaddchildrennode_idupdatepopboolZhas_checkpoint_valuesvalue)Zobject_graph_protoparentsZcheckpointed_trackablesr-   Zobject_protoZchild_protochildZto_visitr   Zcurrent_parentsparentr   r   r   add_checkpoint_values_checkc   s@   	







r5   c           
      C   sr   |   \}}t }| D ]\}}t|||< qt }t|D ]\}}|||< q"t|||d}	||||	|fS )a  Traverse the object graph and list all accessible objects.

  Looks for `Trackable` objects which are dependencies of
  `root_trackable`. Includes slot variables only if the variable they are
  slotting for and the optimizer are dependencies of `root_trackable`
  (i.e. if they would be saved with a checkpoint).

  Args:
    graph_view: A GraphView object.

  Returns:
    A tuple of (trackable objects, paths from root for each object,
                object -> node id, slot variables, object_names)
  )r   r   r   )Zbreadth_first_traversalr   r   itemsr   Zobject_path_to_stringr   r   )

graph_viewr   Z
node_pathsr   objpathr   r-   noder   r   r   r   (objects_ids_and_slot_variables_and_paths   s   
r;   c                 C   s   t | d }|S )z:Traverse the object graph and list all accessible objects.r   )r;   )r7   r   r   r   r   list_objects   s   r<   N)__doc__Ztensorflow.core.protobufr   Ztensorflow.python.opsr   r   Ztensorflow.python.trackabler   Ztensorflow.python.trainingr   r   Ztensorflow.python.utilr   r   r!   r'   r5   r;   r<   r   r   r   r   <module>   s   2-