
    BVh8                     &   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j.                  dg d      ZddZd Zd Z	 	 	 	 	 ddZd Z	 	 	 ddZd Z	 	 	 	 ddZ y)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           	      *   t        j                         }t        j                  t              }| j                         D ]  \  }}t        j                  ||      }t        j                  |      }|r	|||   |<   <g ||<   t        j                  |      j                         D ]j  \  }}	t        j                  |      xs |}
t        j                  ||
      }t        j                          s|
}||   j#                  t%        |	||             l  ||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	   get_registered_saver_namer   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_factory
key_suffixr   s               Y/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/checkpoint/save_util_v1.py!get_checkpoint_factories_and_keysr,   +   s   " +CCE*66t< , 2 2 4 0i ..y*EN77GJ =F ,[9*,Y'

>
>#eg0
 $  %66~FN$
(77ZPBBD $y)00"(-/	000> 
 !;	;;    c                     t        t        | |j                              D ]  \  }\  }}	||   |k(  rJ  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%   unused_object_protor#   r$   registered_saversnamed_saveable_objectsfeed_additionss                  r+   _add_attributes_to_object_graphr>   `   s     :C	
/556:8 05m5i!4I-///0
 (jA 54
 L "4h
L   68J (*6O /1 ). 
 1B	BBr-   c                 <   t        j                  t              }| j                         D ]o  \  }}|j                         D ]W  \  }}|j                  ||      }	||	j
                  _        ||	j
                  _        t        j                  ||      }
|
||   |<   Y q |S )zCFills the object graph proto with data about the registered savers.)
r   r   r   r   r1   registered_saverr   r&   r   r   )r$   r5   r6   r"   r;   r(   
trackablesr&   r%   object_protor'   s              r+   r2   r2   x   s     "--d3 : @ @ B Bj*","2"2"4 BY'--hy.ABl+5l##(2=l##/00JGn3A
#K0BB 
r-   c                 n   g }|d}ni }| j                         D ]  \  }}	|duxr |du}
|
r|j                  ||      }t        j                  ||      }||j	                  |i       }nd}|	D ]>  }|j
                  }|j                  }|j                  }|r|j                  |      nd}||D ]  }||j
                  vsd}||=  n |t        |      rt        j                  ||||      }n|}t        |t        j                        r|f}n t        t        j                   ||            }|D ]2  }||j
                  vst#        d| d|j
                   d| d| d	       ||||<   t        |t$        j&                        rLt)        |      dk(  sJ |d	   }||J |j+                         f}n|j-                  |j/                                |j1                  |       |
szt        |d	   t        j2                        rwt5        j6                         st5        j8                  |      N|d	   j;                         D ]6  \  }}j<                  j?                  ||t        j@                  |      
       8 j<                  j?                  ||t        j@                  |      
       A  ||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   	full_name)!r   r1   r   r   
setdefaultr   r   r   getcallabler   create_saveable_object
isinstancesaveable_object_libSaveableObjecttuplesaveable_objects_for_opAssertionErrorr   PythonStatelenfreezeupdatefeed_dict_additionsextendTrackableSaveabler   r   r   #get_proto_names_and_checkpoint_keys
attributesaddget_full_name)r#   r5   r6   r"   r7   r8   r<   r=   r%   factory_data_listfill_object_protorB   r'   cached_attributesfactory_datar   keyr)   	saveablessaveablemaybe_saveable
local_name	local_keys                          r+   r3   r3      s'     N
 N&<&B&B&D R:"i"*$6O84;O'--hy.ABl..y*EN")44^RH) H:d''c%-- 2C#''-i		! 	H% I!$'	 
	$%/FFC)+DF. ,.n&9&H&HI%')"::##/0) " 	)H% i[ )MM?"3D6 :  #uB() )	) ($-
D
!	NL$<$<	=9~"""Q<! (
((
  (*)


 < < >
?##I. 
Yq\#7#I#I
J>>@,,^<DaL<<>	<!J	

!
!
%
%&**>: & <	< 	##((8 	$ 	:KH:R:h 
 	//r-   c                 T   t        j                         }t        |      D ]  \  }}||   |k(  sJ |j                  j	                  |j                  |d            }| j                  |      D ]6  }|j                  j	                  ||j                     |j                         8  |S )z@Name non-slot `Trackable`s and add them to `object_graph_proto`. )slot_variables)node_idrd   )
r   TrackableObjectGraphr/   r1   rZ   rH   list_childrenchildrenrefr   )	
graph_viewr4   r6   rh   r5   r9   r%   rB   childs	            r+   _fill_object_graph_protorp      s    
 2FFH"+,="> !mYI-///%++//%)))R8 0 L )))4 !599%ZZ   !!! 
r-   c           	         | j                         \  }}t        j                         }|j                         D ]  \  }}t	        j
                  |      ||<    t        j                         }	t        |      D ]
  \  }
}|
|	|<    t        j                  ||	|      }t        | ||	|      }t        |||	||||      \  }}}t        j                  |       ||||fS )z7Create SaveableObjects and protos for gathered objects.)r4   r6   r!   )rn   r4   r6   rh   )r4   r5   r6   r!   r"   r7   r8   )breadth_first_traversalr   r   r   r   object_path_to_stringr/   r   serialize_slot_variablesrp   r>   add_checkpoint_values_check)rn   r"   r7   r8   r4   
node_pathsr!   objpathr6   ri   noderh   r5   r<   r=   r;   s                    r+   serialize_gathered_objectsrz     s   
 #-"D"D"FZ 99;,##% Dic4'==dCLD557( !23 mgtHTN00)!. 0)#	% &-/#$=)+ <.*; ""#56
 "4n

 r-   c                     t        | |      S )zEDetermine checkpoint keys for variables and build a serialized graph.)r8   )rz   )rn   r8   s     r+   -serialize_object_graph_with_registered_saversr|   &  s    	#J	PPr-   c                    |r|j                   }nt        j                  } |       5  t        | |||      \  }}}}	t        j                  d      5  t        j                  |j                         t        j                        }
ddd       |j                  t        j                  
t        j                               ddd       ||	fS # 1 sw Y   IxY w# 1 sw Y   	fS xY w)zDGenerates SaveableObjects and registered savers in the frozen graph.z/cpu:0)dtypeN)tensorr   )
as_defaultr   NullContextmanagerrz   devicer   constantSerializeToStringr   stringr    r
   NoRestoreSaveableOBJECT_GRAPH_PROTO_KEY)rn   r"   to_graphr7   r8   target_contextr<   graph_proto_r;   object_graph_tensors              r+   frozen_saveables_and_saversr   +  s     ((N++N 	K":z#<o	O >K,= 
H	 @'00

'
'
)@@ !!&T-H-H	JK	K 
 !2	22@ @		K 
 !2	22s#   )C4C<CC	CC$)N)NNNNN)NNN)NNNN)!__doc__r   tensorflow.core.protobufr   tensorflow.python.checkpointr   r   tensorflow.python.frameworkr   r   r   tensorflow.python.saved_modelr	   tensorflow.python.trackabler
   r   r   !tensorflow.python.training.savingr   rL   r   tensorflow.python.utilr   
namedtupler   r,   r>   r2   r3   rp   rz   r|   r   rg   r-   r+   <module>r      s     ? 8 - 3 . + 6 , 4 7 T B 2 0//CE 2<jC0  26'+)-8<.2e0P& +/9=/3"JQ ,0)-:>04	3r-   