
    BVh                         d Z ddlmZ ddlmZ ddlmZ ddlZd Z G d de      Zd	 Z	d
Z
e
dz   Ze
dz   Ze
dz   Zd Zd Zd Zd ZddZd Zy)z/Utility methods for the trackable dependencies.    )absolute_import)division)print_functionNc                 j    | syddj                  | D cg c]  }|j                   c}      z   S c c}w )Nzroot objectzroot..)joinname)pathps     [/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/trackable/trackable_utils.pypretty_print_node_pathr      s.    	SXXt4!qvv45554s   0
c                        e Zd Z fdZ xZS )CyclicDependencyErrorc                 8    || _         t        t        |           y)z$Creates a CyclicDependencyException.N)leftover_dependency_mapsuperr   __init__)selfr   	__class__s     r   r   zCyclicDependencyError.__init__    s     $;D 	
/1    )__name__
__module____qualname__r   __classcell__)r   s   @r   r   r      s    2 2r   r   c                    t        j                  t              }| j                         D ]   \  }}|D ]  }||   j	                  |        " |j                         | j                         z
  }|rt        d|       g }| D cg c]	  }||vs| }}|rs|j                  d      }|j                  |       t        | |         D ]=  }||   }|j                  |       |r|j                  |       |j                  |       ? |rs|rWt        j                  t              }	|j                         D ]   \  }}
|
D ]  }|	|   j                  |        " t        |	      t        |      S c c}w )a*  Topologically sorts the keys of a map so that dependencies appear first.

  Uses Kahn's algorithm:
  https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm

  Args:
    dependency_map: a dict mapping values to a list of dependencies (other keys
      in the map). All keys and dependencies must be hashable types.

  Returns:
    A sorted array of keys from dependency_map.

  Raises:
    CyclicDependencyError: if there is a cycle in the graph.
    ValueError: If there are values in the dependency map that are not keys in
      the map.
  z7Found values in the dependency map which are not keys: r   )collectionsdefaultdictsetitemsaddkeys
ValueErrorpopappendremovelistr   reversed)dependency_mapreverse_dependency_mapxdepsdepunknown_keysreversed_dependency_arrto_visitedgesr   xss              r   order_by_dependencyr2   '   s   ( '2237%%' )ga )S!%%a())
 (,,.1D1D1FF,
N$~' ( (
  (KA14J+JaK(KQA""1%>!$% ($S)ell1o""3'( 	 )55d;)//1 /R /!"))#.//   7
88	)	**' Ls   	E-E-r   OPTIMIZER_SLOT
ATTRIBUTESTENSORSc                 r    | j                  t        t        t        z         j                  dt        dz         S )N/S)replace_ESCAPE_CHAR)r	   s    r   escape_local_namer;   x   s1     ,,|\L%@
A
I
I
L3  !r   c                 2    dj                  d | D              S )z%Converts a list of nodes to a string.r7   c              3   F   K   | ]  }t        |j                          y wN)r;   r	   ).0	trackables     r   	<genexpr>z(object_path_to_string.<locals>.<genexpr>   s     HY(Hs   !)r   )node_path_arrs    r   object_path_to_stringrC      s    	H-H
J Jr   c                 J    t        |      }|t        k(  rd}|  dt         d| S )z>Returns the checkpoint key for a local attribute of an object. r7   )r;   SERIALIZE_TO_TENSORS_NAMEOBJECT_ATTRIBUTES_NAME)object_path
local_name
key_suffixs      r   checkpoint_keyrK      s5     ,*,, J=01:,	??r   c                 <    dt         z   }| d| j                  |       S )z=Substrings the checkpoint key to the start of "/.ATTRIBUTES".r7   N)rG   index)key
search_keys     r   extract_object_namerP      s$    ++*	#cii
#	$$r   c                     |xs d}t         dz   |z   }	 | | j                  |      t        |      z   d S # t        $ r | cY S w xY w)zEReturns the substring after the "/.ATTIBUTES/" in the checkpoint key.rE   r7   N)rG   rM   lenr"   )rN   prefixrO   s      r   extract_local_namerT      sU     <R&%+f4*syy$s:6788	 Js   4 AAc           	      6    |  dt          d| dt        |       S )z+Returns checkpoint key for a slot variable.r7   )_OPTIMIZER_SLOTS_NAMEr;   )variable_pathoptimizer_path	slot_names      r   slot_variable_keyrZ      s/     O1231^4DAy)*, -r   r>   )__doc__
__future__r   r   r   r   r   	Exceptionr   r2   r:   rV   rG   rF   r;   rC   rK   rP   rT   rZ    r   r   <module>r_      s|    6 &  % 62I 28+v  %'77  &4  )94 !J	@%	-r   