o
    ?e8                     @   s  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 edg dZd"ddZdd Zdd Z					d#ddZdd Z			d$ddZdd Z				d%d d!Z dS )&zExtracts tensors for checkpointing while updating a TrackableObjectGraph.

This is labelled "v1" because the methods here use SaveableObject, which will
soon be deprecated.
    N)trackable_object_graph_pb2)saveable_compat)util)constant_op)dtypes)ops)registration)base)python_state)trackable_utils)saveable_object)saveable_object_util)object_identity_CheckpointFactoryDatafactorynamecheckpoint_keyc              	   C   s   t  }tt}|  D ]G\}}t||}t	|}|r%||| |< qg ||< t
| D ]#\}}	t|p:|}
t||
}t sG|
}|| t|	||d q0q||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.
      _export_to_saved_model_graph()` 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   ObjectIdentityDictionarycollectionsdefaultdictdictitemsr   get_mapped_trackabler   Zget_registered_saver_namer   Zsaveable_objects_from_trackabler   get_saveable_namer   r   #force_checkpoint_conversion_enabledappendr   )object_names
object_mapcheckpoint_factory_mapunmapped_registered_savers	trackableobject_nameobject_to_save
saver_namer   saveable_factoryZ
key_suffixr    r&   j/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/checkpoint/save_util_v1.py!get_checkpoint_factories_and_keys+   s4   


r(   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.)	enumeratezipnodesr(   5_add_attributes_to_object_graph_for_registered_saversgenerate_saveable_objects)trackable_objectsobject_graph_protonode_idsr   r   call_with_mapped_capturessaveables_cachecheckpoint_idr!   Zunused_object_protor   r    registered_saversnamed_saveable_objectsfeed_additionsr&   r&   r'   _add_attributes_to_object_graph`   s   

r7   c                 C   sh   t t}|  D ](\}}| D ]\}}|j||  }	||	j_||	j_t	||}
|
|| |< qq	|S )zCFills the object graph proto with data about the registered savers.)
r   r   r   r   r+   Zregistered_saverr   r"   r   r   )r    r/   r0   r   r4   r$   Z
trackablesr"   r!   object_protor#   r&   r&   r'   r,   x   s   
r,   c                 C   s:  g }|du r	d}ni }|   D ]\}}	|duo|du}
|
r%|j||  }t||}|dur6||i }nd}|	D ]}|j}|j}|j}|rL||nd}|durc|D ]}||jvrbd}||=  qcqT|du rt	|rtt
||||}n|}t|tjr|f}n	tt
j||d}|D ]}||jvrtd| d|j d| d| d	q|dur|||< t|tjrt|dksJ |d	 }|du r|du sJ | f}n||  || |
sq: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   Z	full_name)!r   r+   r   r   
setdefaultr   r   r   getcallabler   Zcreate_saveable_object
isinstancesaveable_object_libZSaveableObjecttupleZsaveable_objects_for_opAssertionErrorr
   ZPythonStatelenfreezeupdateZfeed_dict_additionsextendZTrackableSaveabler   r   r   Z#get_proto_names_and_checkpoint_keys
attributesaddZget_full_name)r   r/   r0   r   r1   r2   r5   r6   r!   Zfactory_data_listZfill_object_protor8   r#   Zcached_attributesZfactory_datar   keyr%   Z	saveablesZsaveableZmaybe_saveable
local_nameZ	local_keyr&   r&   r'   r-      s   




Jr-   c           	      C   sl   t  }t|D ]+\}}|| |ksJ |jj||dd}| |D ]}|jj||j |j	d q$q|S )z@Name non-slot `Trackable`s and add them to `object_graph_proto`.r&   )slot_variables)node_idrI   )
r   ZTrackableObjectGraphr)   r+   rG   r<   Zlist_childrenchildrenrefr   )	
graph_viewr.   r0   rJ   r/   r3   r!   r8   childr&   r&   r'   _fill_object_graph_proto   s   
rP   c              	   C   s   |   \}}t }| D ]\}}t|||< qt }	t|D ]\}
}|
|	|< q"tj||	|d}t	| ||	|d}t
|||	||||d\}}}t| ||||fS )z7Create SaveableObjects and protos for gathered objects.)r.   r0   r   )rN   r.   r0   rJ   )r.   r/   r0   r   r   r1   r2   )Zbreadth_first_traversalr   r   r   r   Zobject_path_to_stringr)   r   Zserialize_slot_variablesrP   r7   Zadd_checkpoint_values_check)rN   r   r1   r2   r.   Z
node_pathsr   objpathr0   rK   noderJ   r/   r5   r6   r4   r&   r&   r'   serialize_gathered_objects  s@   

rT   c                 C   s   t | |dS )zEDetermine checkpoint keys for variables and build a serialized graph.)r2   )rT   )rN   r2   r&   r&   r'   -serialize_object_graph_with_registered_savers&  s   rU   c              	   C   s   |r|j }ntj}| @ t| |||\}}}}	td tj| tj	d}
W d   n1 s2w   Y  |
tj|
tjd W d   ||	fS 1 sOw   Y  ||	fS )zDGenerates SaveableObjects and registered savers in the frozen graph.z/cpu:0)ZdtypeN)Ztensorr   )Z
as_defaultr   ZNullContextmanagerrT   Zdevicer   ZconstantZSerializeToStringr   stringr   r	   ZNoRestoreSaveableZOBJECT_GRAPH_PROTO_KEY)rN   r   Zto_graphr1   r2   Ztarget_contextr5   Zgraph_proto_r4   Zobject_graph_tensorr&   r&   r'   frozen_saveables_and_savers+  s,   




rX   )N)NNNNN)NNN)NNNN)!__doc__r   Ztensorflow.core.protobufr   Ztensorflow.python.checkpointr   r   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.saved_modelr   Ztensorflow.python.trackabler	   r
   r   Z!tensorflow.python.training.savingr   r?   r   Ztensorflow.python.utilr   
namedtupler   r(   r7   r,   r-   rP   rT   rU   rX   r&   r&   r&   r'   <module>   sL   
5
h
%