o
    ®Ÿ?e‘  ã                   @   sp   d Z ddlZddlZddlZddlZddlmZ ddlm	Z ddl
mZ G dd„ dejƒZG dd	„ d	ejƒZdS )
z@Implementation of the SessionRunHook for preemptible Cloud TPUs.é    N)Útpu_cluster_resolver)Ú
tf_logging)Úsession_run_hookc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCloudTPUPreemptedHooka  The SessionRunHook for preemptible Cloud TPUs.

  This is an implementation of SessionRunHook for the pre-emptible Google Cloud
  TPU service. It attempts to close the session if the TPU is preempted, and
  exits the coordinator process if the session cannot be closed.
  c                 C   s
   || _ d S ©N)Ú_cluster)ÚselfÚcluster© r
   úe/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/tpu/preempted_hook.pyÚ__init__#   s   
zCloudTPUPreemptedHook.__init__c                 C   s(   t  ¡ rt| j|ƒ| _| j ¡  d S d S r   )r   Úis_running_in_gceÚ_TPUPollingThreadr   Ú_tpu_pollerÚstart)r   ÚsessionZcoordr
   r
   r   Úafter_create_session&   s   þz*CloudTPUPreemptedHook.after_create_sessionc                 C   s   | j  ¡  d S r   )r   Ústop)r   r   r
   r
   r   Úend+   s   zCloudTPUPreemptedHook.endN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r
   r
   r
   r   r      s
    r   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )r   a%  A thread that polls the state of a TPU node.

  When the node transitions into a TERMINAL state (PREEMPTED, TERMINATED)
  that's considered as not recoverable by the underlying infrastructure,
  it attempts to close the session, and exits the entire process if the
  session.close() stucks.
  c                    sR   t t| ƒ ¡  d| _d| _d| _|| _|| _d| _dD ]}t	 
|¡ t	j¡ qd S )NTFé   )zgoogleapiclient.discoveryzoauth2client.client)Úsuperr   r   ÚdaemonÚ_runningÚ_session_closedr   Ú_sessionÚ	_intervalÚ_loggingÚ	getLoggerÚsetLevelÚWARNING)r   r	   r   Úname©Ú	__class__r
   r   r   8   s   ÿz_TPUPollingThread.__init__c                 C   s   d| _ d| _|  ¡  d S )NFT)r   r   Újoin)r   r
   r
   r   r   F   s   z_TPUPollingThread.stopc                 C   sp   t  ¡ st d¡ d| _d S | jr6| jj ¡ }|s+t d| jj| jj 	¡ ¡ t
 d¡ t | j¡ | jsd S d S )Nz@TPUPollingThread is running in a non-GCE environment, exiting...Fz)TPUPollingThread found TPU %s in state %sé   )r   r   ÚloggingÚwarningr   r   Z_cloud_tpu_clientÚrecoverableZ_tpuÚstateÚosÚ_exitÚtimeÚsleepr   )r   r+   r
   r
   r   ÚrunK   s    ÿþ
ùz_TPUPollingThread.run)r   r   r   r   r   r   r1   Ú__classcell__r
   r
   r%   r   r   /   s
    r   )r   r)   r    r-   Ú	threadingr/   Z-tensorflow.python.distribute.cluster_resolverr   Ztensorflow.python.platformr   Ztensorflow.python.trainingr   ZSessionRunHookr   ÚThreadr   r
   r
   r
   r   Ú<module>   s   