
    BVha                     n    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 Z
d Zd	 Zd
 Z	 ddZddZy)z6Utilities for extracting and writing checkpoint info`.    )trackable_object_graph_pb2)resource_variable_ops)	variables)trackable_utils)object_identityc                    t        |       }t        j                         }|D ]   }dt        |      v s|j	                         }|D ]  }t        |      D ]  \  }}		 |j                  |	|      }
|
|
j                          |
j                         rt        d      |
|v rt        d|
 d      t        j                  ||	   ||   |      }|||
<   t        |       }|||
<   | j                  |
       t         j"                  j$                  j'                  |||      }|j)                  |g       j                  |         # |S # t        t        f$ r d}
Y w xY w)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.)variable_pathoptimizer_path	slot_name)r   original_variable_node_idslot_variable_node_id)listr   ObjectIdentityDictionarydirr	   	enumerateget_slotAttributeErrorKeyError_maybe_initialize_trackable_trackable_childrenNotImplementedErrorr   slot_variable_keylenappendr   TrackableObjectGraphTrackableObjectSlotVariableReference
setdefault)trackable_objectsnode_idsobject_namesnon_slot_objectsslot_variables	trackable
slot_namesr   r   original_variableslot_variablecheckpoint_namer   slot_variable_protos                 Q/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/checkpoint/util.pyserialize_slot_variablesr,      s   +,";;=.# )Oi 3y>)++-j! #O)<E= "	O8%'8!%../@)LM "

3
3
5..0%    h&%(/ *IIJ J ,==():;))4!#/ )8,}
%"%&7"8
$9(=
!

"
"=
1(==MM$$%,E(= % ?  
#
#Ir
2
9
9:M
NE"	O#O)OT 
? !(+ ! M!s   EE	E	c                 .    || S |j                  | |       S )zFReturns the mapped trackable if possible, otherwise returns trackable.)get)r%   
object_maps     r+   get_mapped_trackabler0   I   s    >>)Y//    c                     t        | t        j                        st        j                  |       syt        | dd      | j                  j                  S | j                  S )zGGets the full name of variable for name-based checkpoint compatibility. _save_slice_infoN)	
isinstancer   Variabler   is_resource_variablegetattrr4   	full_name_shared_name)vars    r+   get_full_namer<   Q   sS     c9--. 55c:S$d+7)))r1   c                    i }t        j                         }t               }t        | j                        D ]  \  }}|j
                  s|j                  s|j                  d      r|j                  |       |j                  D ]3  }|j                  }||vrt               ||<   ||   j                  |       5  t               }|j                  |       |rW|j                         }||vr|j                  |      }	|j                  |	       |	D ]  }
|
|v s|j                  |
        |rWt        | j                        D ]!  \  }}t        ||v       |j                  _        # y)zDetermines which objects have checkpoint values and save this to the proto.

  Args:
    object_graph_proto: A `TrackableObjectGraph` proto.
  registered_saverN)r   ObjectIdentitySetsetr   nodes
attributesr$   HasFieldaddchildrennode_idupdatepopboolhas_checkpoint_valuesvalue)object_graph_protoparentscheckpointed_trackablesrF   object_protochild_protochildto_visitr%   current_parentsparents              r+   add_checkpoint_values_checkrU   a   sf    '+==?  E();)A)AB "g|<#>#>01!!'*#,, "!!e	g	en!	"	" U(
//)*Ikk),O""?3! 	7	V 	  ));)A)AB ,g|/3**0,L&&,,r1   c                 `   | j                         \  }}t        j                         }|j                         D ]  \  }}t	        j
                  |      ||<    t        j                         }t        |      D ]
  \  }}	|||	<    |rt        j                         }
nt        |||      }
||||
|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.
    skip_slot_variables: If True does not return trackables for slot variable.
      Default False.

  Returns:
    A tuple of (trackable objects, paths from root for each object,
                object -> node id, slot variables, object_names)
  )r    r!   r"   )breadth_first_traversalr   r   itemsr   object_path_to_stringr   r,   )
graph_viewskip_slot_variablesr    
node_pathsr"   objpathr!   rF   noder$   s              r+   (objects_ids_and_slot_variables_and_pathsr`      s    $ #-"D"D"FZ 99;,##% Dic4'==dCLD557( !23 mgtHTN$==?N-+!N
 Z><	PPr1   c                 $    t        | |      d   }|S )z:Traverse the object graph and list all accessible objects.r   )r`   )rZ   r[   r    s      r+   list_objectsrb      s#    >% 
r1   N)F)__doc__tensorflow.core.protobufr   tensorflow.python.opsr   r   tensorflow.python.trackabler   tensorflow.python.utilr   r,   r0   r<   rU   r`   rb    r1   r+   <module>ri      sB    = ? 7 + 7 2.b0 *,\ BG!QHr1   