o
    2h                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZdd ZG dd	 d	eZd
d Z	dZ
e
d Ze
d Ze
d Zdd Zdd Zdd Zdd ZdddZdd ZdS )z/Utility methods for the trackable dependencies.    )absolute_import)division)print_functionNc                 C   s    | sdS dd dd | D  S )Nzroot objectzroot..c                 S   s   g | ]}|j qS  name).0pr   r   f/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/trackable/trackable_utils.py
<listcomp>   s    z*pretty_print_node_path.<locals>.<listcomp>join)pathr   r   r   pretty_print_node_path   s   r   c                       s   e Zd Z fddZ  ZS )CyclicDependencyErrorc                    s   || _ tt|   dS )z$Creates a CyclicDependencyException.N)leftover_dependency_mapsuperr   __init__)selfr   	__class__r   r   r       s   zCyclicDependencyError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c           
         s  t t |  D ]\}}|D ]	} | | qq	  |   }|r+td| g } fdd| D }|rb|d}|| t| | D ]} | }|	| |s_||  | qH|s8 rt t
}  D ]\}}	|	D ]	}|| | qsqmt|t|S )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: c                    s   g | ]}| vr|qS r   r   )r	   xreverse_dependency_mapr   r   r   L   s    z'order_by_dependency.<locals>.<listcomp>r   )collectionsdefaultdictsetitemsaddkeys
ValueErrorpopappendremovelistr   reversed)
dependency_mapr   depsdepunknown_keysreversed_dependency_arrto_visitedgesr   xsr   r   r   order_by_dependency'   s>   







r3   r   OPTIMIZER_SLOT
ATTRIBUTESTENSORSc                 C   s   |  ttt  dtd S )N/S)replace_ESCAPE_CHARr   r   r   r   escape_local_namex   s   r;   c                 C   s   d dd | D S )z%Converts a list of nodes to a string.r7   c                 s   s    | ]}t |jV  qd S N)r;   r   )r	   	trackabler   r   r   	<genexpr>   s    z(object_path_to_string.<locals>.<genexpr>r   )node_path_arrr   r   r   object_path_to_string   s   r@   c                 C   s(   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_suffixr   r   r   checkpoint_key   s   rG   c                 C   s   dt  }| d| | S )z=Substrings the checkpoint key to the start of "/.ATTRIBUTES".r7   N)rC   index)key
search_keyr   r   r   extract_object_name   s   rK   c                 C   sH   |pd}t d | }z| | |t| d W S  ty#   |  Y S w )zEReturns the substring after the "/.ATTIBUTES/" in the checkpoint key.rA   r7   N)rC   rH   lenr%   )rI   prefixrJ   r   r   r   extract_local_name   s   rN   c                 C   s   |  dt  d| dt| S )z+Returns checkpoint key for a slot variable.r7   )_OPTIMIZER_SLOTS_NAMEr;   )variable_pathoptimizer_path	slot_namer   r   r   slot_variable_key   s   rS   r<   )__doc__
__future__r   r   r   r   r   	Exceptionr   r3   r:   rO   rC   rB   r;   r@   rG   rK   rN   rS   r   r   r   r   <module>   s$   	;	

