a
    0SicQ                     @   sL  d 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 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lm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Zdd Zdd Zd0ddZ dd Z!dd Z"dd Z#d d! Z$d"d# Z%d1d$d%Z&d2d&d'Z'd(d) Z(d3d*d+Z)d,d- Z*d.d/ Z+dS )4z*Utilities for extracting checkpoint info`.    N)trackable_object_graph_pb2)saveable_compat)constant_op)dtypes)ops)resource_variable_ops)	variables)registration)base)python_state)trackable_utils)	optimizer)saveable_object)saveable_object_util)object_identity_CheckpointFactoryDatafactorynamecheckpoint_keyc                 C   s"  t | }t }|D ]}t|tjs2dt|v r| }|D ]}t|D ]\}}	z|	|	|}
W n t
tfy|   d}
Y n0 |
du rqJ|
  |
 rtd|
|v rtd|
 dtj||	 || |d}|||
< t| }|||
< | |
 tjjj|||d}||g | qJq>q|S )zGather and name slot variables.Z _create_or_restore_slot_variableNzCurrently 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.)variable_pathoptimizer_path	slot_name)r   original_variable_node_idslot_variable_node_id)listr   ObjectIdentityDictionary
isinstanceoptimizer_v1	OptimizerdirZget_slot_names	enumerateZget_slotAttributeErrorKeyError_maybe_initialize_trackable_trackable_childrenNotImplementedErrorr   slot_variable_keylenappendr   TrackableObjectGraphTrackableObjectSlotVariableReference
setdefault)trackable_objectsnode_idsobject_namesZnon_slot_objectsslot_variables	trackableZ
slot_namesr   r   Zoriginal_variableZslot_variablecheckpoint_namer   Zslot_variable_proto r4   ]/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/checkpoint/util.py_serialize_slot_variables)   sZ    



r6   c                 C   s   |du r| S | | | S dS )zFReturns the mapped trackable if possible, otherwise returns trackable.N)get)r2   
object_mapr4   r4   r5   _get_mapped_trackable[   s    r9   c              	   C   s   t  }tt}|  D ]\}}t||}t|}|rH||| |< qg ||< t	
| D ]:\}}	t|pr|}t||}
|| t|	||
d q^q||fS )a  Gets a map of saveable factories and corresponding checkpoint keys.

  Args:
    object_names: a dictionary that maps `Trackable` objects to auto-generated
      string names.
    object_map: a dictionary mapping `Trackable` to copied `Trackable` objects.
      The copied objects are generated from `Trackable._map_resources()` which
      copies the object into another graph. Generally only resource objects
      (e.g. Variables, Tables) will be in this map.

  Returns:
    A tuple of (
      Dictionary mapping trackable -> list of _CheckpointFactoryData,
      Dictionary mapping registered saver name -> {object name -> trackable})
  r   )r   r   collectionsdefaultdictdictitemsr9   r	   get_registered_saver_namer   saveable_objects_from_trackabler   get_saveable_namer   r   r)   r   )r0   r8   checkpoint_factory_mapunmapped_registered_saversr2   object_nameobject_to_save
saver_namer   saveable_factoryr   r4   r4   r5   !get_checkpoint_factories_and_keysc   s,    



rG   c                 C   sf   t t}|  D ]N\}}| D ]<\}}|j||  }	||	j_||	j_t||}
|
|| |< q"q|S )zCFills the object graph proto with data about the registered savers.)	r:   r;   r<   r=   nodesregistered_saverr   rC   r9   )rB   object_graph_protor/   r8   registered_saversrE   
trackablesrC   r2   object_protorD   r4   r4   r5   5_add_attributes_to_object_graph_for_registered_savers   s    

rN   c                 C   s<   t | tjst| sdS t| dddur2| jjS | jS dS )zFGets the full name of variable for name-based checkpoint compatiblity. _save_slice_infoN)	r   r   Variabler   is_resource_variablegetattrrP   	full_name_shared_name)varr4   r4   r5   _get_full_name   s    rW   c                 C   sj   t t| |jD ]\}\}}	|| |ksJ qt||\}
}t||||}t|
|||||\}}|||fS )zECreate saveables/savers and corresponding protos in the object graph.)r!   ziprH   rG   rN   4_add_attributes_to_object_graph_for_saveable_objects)r.   rJ   r/   r0   r8   call_with_mapped_capturessaveables_cachecheckpoint_idr2   Zunused_object_protorA   rB   rK   named_saveable_objectsfeed_additionsr4   r4   r5   _add_attributes_to_object_graph   s    
r_   c                 C   s.  g }|du rd}ni }|   D ]\}}	|j||  }
t||}|durV||i }nd}|	D ]}|j}|j}|j}|r||nd}|dur|D ]}||jvrd}||=  qq|du rLt|rt	
|||}n|}t|tjr|f}ntt	j||d}|D ]6}||jvrtd| d|j d| d| d	q|durL|||< t|tjrt|dkslJ |d	 }|du r|du sJ | f}n||  || t|d	 t	jrt st|du r|d	  D ] \}}|
jj||t|d
 qq^|
jj||t|d
 q^q||fS )zACreate SaveableObjects and corresponding SerializedTensor protos.N)opr   zThe object z& produced a SaveableObject with name 'z' for attribute 'z'. Expected a name containing 'z'.   r   )r   r   rT   ) r=   rH   r9   r-   r   r   r   r7   callabler   create_saveable_objectr   saveable_object_libSaveableObjecttuplesaveable_objects_for_opAssertionErrorr   PythonStater(   freezeupdatefeed_dict_additionsextendTrackableSaveabler   #force_checkpoint_conversion_enabledr@   #get_proto_names_and_checkpoint_keys
attributesaddrW   )rA   rJ   r/   r8   rZ   r[   r]   r^   r2   Zfactory_data_listrM   rD   Zcached_attributesZfactory_datar   keyrF   	saveablessaveablemaybe_saveable
local_nameZ	local_keyr4   r4   r5   rY      s    









rY   c                 C   s  t  }t  }t| |jD ]`\}}|js:|js:|drD|| |j	D ]0}| |j
 }||vrlt  ||< || | qJqt  }|| |r| }||vrq||}	||	 |	D ]}
|
|v r||
 qqt| D ]\}}t||v |j| j_qdS )zDetermines which objects have checkpoint values and saves to the proto.

  Args:
    trackable_objects: A list of all trackable objects.
    object_graph_proto: A `TrackableObjectGraph` proto.
  rI   N)r   r   ObjectIdentitySetrX   rH   rq   r1   HasFieldrr   childrennode_idrk   popr!   boolhas_checkpoint_valuesvalue)r.   rJ   parentsZcheckpointed_trackablesr2   rM   child_protochildto_visitZcurrent_parentsparentr{   r4   r4   r5   _add_checkpoint_values_check(  s:    






r   c           
      C   s   |du rt  }t|D ]d\}}|| |ks0J |j }|j||d | |D ]"}|j	 }	||j
 |	_|j|	_qXq|S )z@Name non-slot `Trackable`s and add them to `object_graph_proto`.Nr4   )r   r*   r!   rH   rr   r1   rm   r7   list_childrenrz   refr{   r   rw   )

graph_viewr.   r/   r1   rJ   r\   r2   rM   r   r   r4   r4   r5   _fill_object_graph_protoV  s    

r   c              	   C   s   |   \}}t }| D ]\}}t|||< qt }	t|D ]\}
}|
|	|< qDt||	|d}t| ||	|d}t	|||	||||d\}}}t
|| ||||fS )z7Create SaveableObjects and protos for gathered objects.r.   r/   r0   )r   r.   r/   r1   )r.   rJ   r/   r0   r8   rZ   r[   )breadth_first_traversalr   r   r=   r   object_path_to_stringr!   r6   r   r_   r   )r   r8   rZ   r[   r.   
node_pathsr0   objpathr/   r{   noder1   rJ   r]   r^   rK   r4   r4   r5   serialize_gathered_objectsi  s@    

r   c                 C   s   t | |dS )zEDetermine checkpoint keys for variables and build a serialized graph.)r[   )r   )r   r[   r4   r4   r5   -serialize_object_graph_with_registered_savers  s    r   c              	   C   s   |r|j }ntj}| z t| |||\}}}}	td$ tj| tj	d}
W d   n1 sd0    Y  |
tj|
tjd W d   n1 s0    Y  ||	fS )zDGenerates SaveableObjects and registered savers in the frozen graph.z/cpu:0)dtypeN)tensorr   )
as_defaultr   NullContextmanagerr   devicer   constantSerializeToStringr   stringr)   r
   NoRestoreSaveableOBJECT_GRAPH_PROTO_KEY)r   r8   to_graphrZ   r[   Ztarget_contextr]   graph_proto_rK   object_graph_tensorr4   r4   r5   frozen_saveables_and_savers  s$    

$"r   c           
      C   sr   |   \}}t }| D ]\}}t|||< qt }t|D ]\}}|||< qDt|||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   r   r=   r   r   r!   r6   )
r   r.   r   r0   r   r   r/   r{   r   r1   r4   r4   r5   (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   )r   r.   r4   r4   r5   list_objects  s    r   )N)N)NNN)NNNN),__doc__r:   tensorflow.core.protobufr   tensorflow.python.checkpointr   tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   r   tensorflow.python.saved_modelr	   tensorflow.python.trackabler
   r   r   tensorflow.python.trainingr   r   !tensorflow.python.training.savingr   rd   r   tensorflow.python.utilr   
namedtupler   r6   r9   rG   rN   rW   r_   rY   r   r   r   r   r   r   r   r4   r4   r4   r5   <module>   sR   2
-a2 
   
%    
