o
    ?eB"                  	   @   s   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ZdZdZdZdZdZedG dd dedg dZ		d ddZ dd Z!dd Z"dd Z#dS )!z+TPU system metadata and associated tooling.    N)
config_pb2)session)device_util)contextconfig)device)errors)ops)
tf_logging)tpu)	tf_exporti i   Z
tpu_workerZcoordinator) localz"tpu.experimental.TPUSystemMetadatac                       s    e Zd ZdZ fddZ  ZS )TPUSystemMetadataa  Describes some metadata about the TPU system.

  Attributes:
    num_cores: interger. Total number of TPU cores in the TPU system.
    num_hosts: interger. Total number of hosts (TPU workers) in the TPU system.
    num_of_cores_per_host: interger. Number of TPU cores per host (TPU worker).
    topology: an instance of `tf.tpu.experimental.Topology`, which describes the
      physical topology of TPU system.
    devices: a tuple of strings, which describes all the TPU devices in the
      system.
  c                    s   t t| | |||||S N)superr   __new__)cls	num_cores	num_hostsnum_of_cores_per_hosttopologydevices	__class__ j/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/tpu/tpu_system_metadata.pyr   =   s   
zTPUSystemMetadata.__new__)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r   (   s    	r   r   r   r   r   r   Fc              	   C   sL  d}g }t t}t rt }dd |D }nqd}	 td|  z>t	
  . tj| tt|d}| }	 W d   W d   W q1 sKw   Y  W d   n1 sZw   Y  W n( tjy   d	|  }	|tkrtd
|	 td|t |d7 }nt|	Y nw q|D ]}
tj|
j}|jdkr||j |j |d7 }qd}|rtdd | D }t |dkrt!d"||# }d}|r|st!d"| |t$| |}dd }t%t&||d}t'|t ||||d}|rtd td|j( td|j) td|j* |j+D ]	}
td|
 q|S td| |S )z<Automatically detects the TPU system metadata in the system.r   c                 S   s&   g | ]}t t|j|jd d qS )r   )session_libZ_DeviceAttributesr   Zcanonicalizenamedevice_type).0dr   r   r   
<listcomp>O   s
    z._query_tpu_system_metadata.<locals>.<listcomp>   Tz8Querying Tensorflow master (%s) for TPU system metadata.r   NzFailed to connect to the Tensorflow master. The TPU worker may not be ready (still scheduling) or the Tensorflow master address is incorrect: got (%s).z%szRetrying (%d/%d).ZTPUc                 S   s   g | ]}t |qS r   )len)r'   Zcore_idsr   r   r   r)   y   s    zHTPU cores on each host is not same. This should not happen!. devices: {}zCannot find any TPU cores in the system (master address {}). This usually means the master address is incorrect or the TPU worker has some problems. Available devices: {}c                 S   s&   t j| j}|j|j|j|j|jfS r   )		tf_device
DeviceSpecfrom_stringr%   jobZreplicataskr&   device_index)r   specr   r   r   	_sort_key   s   z-_query_tpu_system_metadata.<locals>._sort_key)keyr#   zFound TPU system:z*** Num TPU Cores: %dz*** Num TPU Workers: %dz *** Num TPU Cores Per Worker: %dz*** Available Device: %szFailed to find TPU: %s),collectionsdefaultdictlistr   Zexecuting_eagerlyr   Zlist_logical_deviceslogginginfor
   Graph
as_defaultr$   Sessionget_session_config_with_timeout_PINGING_MASTER_TIMEOUT_IN_MSZlist_devicesr	   DeadlineExceededError_RETRY_TIMESwarning
ValueErrorr,   r-   r.   r%   r&   r0   appendr1   setvaluesr+   RuntimeErrorformatpop_obtain_topologytuplesortedr   r   r   r   r   )master_addresscluster_defZquery_topologyZtpu_core_countr   Zdevice_dictZlogical_devicesZretry_countsessmsgr   r2   r   Znum_cores_per_host_setr   r3   metadatar   r   r   _query_tpu_system_metadataD   s   





rQ   c              	   C   s   zNt d|  t  7 tt|}tj| |d}|	t
 }|W  d   W  d   W S 1 s6w   Y  W d   W dS 1 sGw   Y  W dS  tjy\   td|  w )zObtains TPU fabric topology.zfInitializing TPU system (master: %s) to fetch topology for model parallelism. This might take a while.r   NzaFail to initialize TPU system with master (%s). Please double check the TPU system is functional.)r8   r9   r
   r:   r;   r=   !_INITIAL_TPU_SYSTEM_TIMEOUT_IN_MSr$   r<   runr   Zinitialize_systemr	   r?   rB   )rL   rM   Zsession_configrN   r   r   r   r   rI      s2   &rI   c                 C   s   t j| |d}|S )z>Returns a session given a timeout and a cluster configuration.)Zoperation_timeout_in_msrM   )r   ZConfigProto)Ztimeout_in_secsrM   Zconfig_protor   r   r   r=      s   r=   c                 C   s   | t v rdS |r|jstS tdd |jD }t|v rtdt|dkr+|jd jS t|dkr>t|v r>|t |	 S td)	a  Returns the canonical job name to use to place TPU computations on.

  Args:
    master: A `string` representing the TensorFlow master to use.
    cluster_def: A ClusterDef object describing the TPU cluster.

  Returns:
    A string containing the job name, or None if no job should be specified.

  Raises:
    ValueError: If the user needs to specify a tpu_job_name, because we are
      unable to infer the job name automatically, or if the user-specified job
      names are inappropriate.
  Nc                 s   s    | ]}|j V  qd S r   )r%   )r'   r/   r   r   r   	<genexpr>   s    zmaster_job.<locals>.<genexpr>z1Currently, tpu_worker is not an allowed job name.r*   r      zCould not infer TPU job name.)
_LOCAL_MASTERSr/   _DEFAULT_JOB_NAMErD   rB   r+   r%   _DEFAULT_COORDINATOR_JOB_NAMEremoverH   )ZmasterrM   Z	job_namesr   r   r   
master_job   s   

rZ   )NF)$r!   r5   Ztensorflow.core.protobufr   Ztensorflow.python.clientr   r$   Ztensorflow.python.distributer   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   r   r,   r	   r
   Ztensorflow.python.platformr   r8   Ztensorflow.python.tpur   Z tensorflow.python.util.tf_exportr   r>   r@   rR   rW   rX   rV   
namedtupler   rQ   rI   r=   rZ   r   r   r   r   <module>   s:   

c