o
    i 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   
tpu_worker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__ X/var/www/myenv/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 nB1 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_lib_DeviceAttributesr   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).TPUc                 S   s   g | ]}t |qS r   )len)r+   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)   job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   executing_eagerlyr   list_logical_deviceslogginginfor
   Graph
as_defaultr&   Sessionget_session_config_with_timeout_PINGING_MASTER_TIMEOUT_IN_MSlist_devicesr	   DeadlineExceededError_RETRY_TIMESwarning
ValueErrorr2   r3   r4   r)   r*   r7   appendr8   setvaluesr0   RuntimeErrorformatpop_obtain_topologytuplesortedr   r   r   r   r   )master_addresscluster_defquery_topologytpu_core_countr   device_dictlogical_devicesretry_countsessmsgr   r9   r   num_cores_per_host_setr   r:   metadatar   r   r   _query_tpu_system_metadataD   s   





ra   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.)rA   rB   r
   rC   rD   rF   !_INITIAL_TPU_SYSTEM_TIMEOUT_IN_MSr&   rE   runr   initialize_systemr	   rI   rL   )rV   rW   session_configr]   r   r   r   r   rS      s2   &rS   c                 C   s   t j| |d}|S )z>Returns a session given a timeout and a cluster configuration.)operation_timeout_in_msrW   )r   ConfigProto)timeout_in_secsrW   config_protor   r   r   rF      s   rF   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+   r5   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_MASTERSr5   _DEFAULT_JOB_NAMErN   rL   r0   r)   _DEFAULT_COORDINATOR_JOB_NAMEremoverR   )masterrW   	job_namesr   r   r   
master_job   s   

rr   )NF)$r#   r<   tensorflow.core.protobufr   tensorflow.python.clientr   r&   tensorflow.python.distributer   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r2   r	   r
   tensorflow.python.platformr   rA   tensorflow.python.tpur    tensorflow.python.util.tf_exportr   rG   rJ   rb   rm   rn   rl   
namedtupler   ra   rS   rF   rr   r   r   r   r   <module>   s:   

c