o
    2ha                     @   sz   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 Z
d	d
 Zdd Zdd Z	dddZdddZdS )z6Utilities for extracting and writing checkpoint info`.    )trackable_object_graph_pb2)resource_variable_ops)	variables)trackable_utils)object_identityc                 C   s  t | }t }|D ]}}dt|v r| }|D ]n}t|D ]g\}}	z||	|}
W n ttfy7   d}
Y nw |
du r=q|
	  |

 rItd|
|v rUt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.)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_proto r)   \/var/www/html/chatgem/venv/lib/python3.10/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#   
object_mapr)   r)   r*   get_mapped_trackableI   s   r.   c                 C   s8   t | tjst| sdS t| dddur| jjS | jS )zGGets the full name of variable for name-based checkpoint compatibility. _save_slice_infoN)	
isinstancer   Variabler   is_resource_variablegetattrr0   	full_name_shared_name)varr)   r)   r*   get_full_nameQ   s   r8   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.
  registered_saverN)r   ObjectIdentitySetsetr   nodes
attributesr"   HasFieldaddchildrennode_idupdatepopboolhas_checkpoint_valuesvalue)object_graph_protoparentscheckpointed_trackablesrA   object_protochild_protochildto_visitr#   current_parentsparentr)   r)   r*   add_checkpoint_values_checka   s@   	







rP   Fc                 C   s   |   \}}t }| D ]\}}t|||< qt }t|D ]\}}	|||	< q"|r2t }
n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.
    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   rA   noder"   r)   r)   r*   (objects_ids_and_slot_variables_and_paths   s   

rZ   c                 C   s   t | |d }|S )z:Traverse the object graph and list all accessible objects.r   )rZ   )rT   rU   r   r)   r)   r*   list_objects   s   r[   N)F)__doc__tensorflow.core.protobufr   tensorflow.python.opsr   r   tensorflow.python.trackabler   tensorflow.python.utilr   r+   r.   r8   rP   rZ   r[   r)   r)   r)   r*   <module>   s   1.
$