
    sg                         d dl Z d dl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	m
Z
 d dlmZmZmZmZmZmZ dgZdedeeef   fdZd	edee   fd
Zdee   dedeeef   fdZdedefdZdeeeeef   f   ddfdZ G d d      Zy)    N)	timedelta)EventThread)AnyCallableDictOptionalTupleUnionparse_rendezvous_endpoint
config_strreturnc                     i }| j                         } | s|S | j                  d      }|D ]a  }|j                  dd      ^}}|j                         }|st        d      |r|d   j                         }nd}|st        d| d      |||<   c |S )	zExtract key-value pairs from a rendezvous configuration string.

    Args:
        config_str:
            A string in format <key1>=<value1>,...,<keyN>=<valueN>.
    ,=   zZThe rendezvous configuration string must be in format <key1>=<value1>,...,<keyN>=<valueN>.r   Nz%The rendezvous configuration option 'z' must have a value specified.)stripsplit
ValueError)r   config
key_valueskvkeyvaluesvalues          ]/var/www/html/venv/lib/python3.12/site-packages/torch/distributed/elastic/rendezvous/utils.py_parse_rendezvous_configr      s      F!!#J!!#&J xxQ'fiik7  1IOO%EE7u<Z[  s)* M    port_strc                 J    | r!t        j                  d|       rt        |       S y)z1Try to extract the port number from ``port_str``.z^[0-9]{1,5}$N)rematchint)r   s    r   _try_parse_portr$   <   s    BHH_h78}r   endpointdefault_portc                    | | j                         } | sd|fS | d   dk(  r| d   dk(  r	| gg ^}}n| j                  dd      ^}}t        |      dkD  r|d   dk(  r|d   dk(  r|dd }t        |      dk(  r$t        |d         }||dk\  rt	        d	|  d
      |}t        j                  d|      st	        d|  d      ||fS )a4  Extract the hostname and the port number from a rendezvous endpoint.

    Args:
        endpoint:
            A string in format <hostname>[:<port>].
        default_port:
            The port number to use if the endpoint does not include one.

    Returns:
        A tuple of hostname and port number.
    	localhostr   []:r   i   z,The port number of the rendezvous endpoint 'z)' must be an integer between 0 and 65536.z^[\w\.:-]+$z)The hostname of the rendezvous endpoint 'zN' must be a dot-separated list of labels, an IPv4 address, or an IPv6 address.)r   rsplitlenr$   r   r!   r"   )r%   r&   hostrestports        r   r   r   C   s    >>#\** {chrlc1mmtooc1-t 4y1}aCDHOAbz
4yA~tAw'<45=>xj I' ' 
 88ND)7z B; ;
 	

 :r   r/   c                 ~   | dk(  ry	 t        j                  |       }|r|j                  ry	 t	        j
                  | dt        j                  t        j                        }|D cg c]
  }|d   d    }}t	        j                         }| |k(  ryt	        j
                  |dt        j                  t        j                        }|D ]5  }|d   r
|d   | k(  r y|r|d   d   t        |      k(  r y|d   d   |v s5 y y# t        $ r d}Y w xY w# t        t        j                  f$ r}g }Y d}~d}~ww xY wc c}w )	a<  Indicate whether ``host`` matches the hostname of this machine.

    This function compares ``host`` to the hostname as well as to the IP
    addresses of this machine. Note that it may return a false negative if this
    machine has CNAME records beyond its FQDN or IP addresses assigned to
    secondary NICs.
    r(   TN)protoflags   r      F)	ipaddress
ip_addressr   is_loopbacksocketgetaddrinfoIPPROTO_TCPAI_CANONNAMEgaierrorgethostnamestr)	r/   addrhost_addr_list_host_addr_infohost_ip_list	this_host	addr_list	addr_infos	            r   _matches_machine_hostnamerI   t   sS    {##D)   ++$f008K8K
 @NN^N1%a(NLN""$Iy""4v119L9LI  	Q<IaLD0 IaLOs4y0 Q<?l* I   (  Os.   D 5D &D:DDD7+D22D7secondsc                 ~    t        | t              rt        j                  |  } | dk\  rt	        j
                  |        yy)zSuspend the current thread for ``seconds``.

    Args:
        seconds:
            Either the delay, in seconds, or a tuple of a lower and an upper
            bound within which a random delay will be picked.
    g{Gz?N)
isinstancetuplerandomuniformtimesleep)rJ   s    r   _delayrR      s4     '5!..'*$

7 r   c            
           e Zd ZU dZ G d d      Zee   ed<   ee   ed<   ee	j                     ed<   eed<   ded	ed
   dededdf
dZedee   fd       ZdeddfdZddZddZedd       Zed        Zy)_PeriodicTimerzRepresent a timer that periodically runs a specified function.

    Args:
        interval:
            The interval, in seconds, between each run.
        function:
            The function to run.
    c                   Z    e Zd ZU eed<   ed   ed<   eedf   ed<   ee	ef   ed<   e
ed<   y)	_PeriodicTimer._Contextinterval.Nfunction.argskwargs
stop_eventN)__name__
__module____qualname__float__annotations__r   r
   r   r   r@   r    r   r   _ContextrV      s4    9%%CHoS#Xr   rc   _name_thread
_finalizer_ctxrW   rY   rX   rZ   r[   r   Nc                 >   d | _         | j                         | _        |j                         | j                  _        || j                  _        |xs d| j                  _        |xs i | j                  _        t               | j                  _	        d | _
        d | _        y )Nrb   )rd   rc   rg   total_secondsrW   rY   rZ   r[   r   r\   re   rf   )selfrW   rY   rZ   r[   s        r   __init__z_PeriodicTimer.__init__   st     
MMO	%335		%				!<R		$w		r   c                     | j                   S )zGet the name of the timer.)rd   rj   s    r   namez_PeriodicTimer.name   s     zzr   rn   c                 @    | j                   rt        d      || _        y)zSet the name of the timer.

        The specified name will be assigned to the background thread and serves
        for debugging and troubleshooting purposes.
        The timer has already started.N)re   RuntimeErrorrd   )rj   rn   s     r   set_namez_PeriodicTimer.set_name   s     <<?@@
r   c                    | j                   rt        d      t        | j                  | j                  xs d| j
                  fd      | _         t        j                  | | j                  | j                   | j
                  j                        | _
        d| j                  _        | j                   j                          y)zStart the timer.rp   PeriodicTimerT)targetrn   rZ   daemonFN)re   rq   r   _runrd   rg   weakreffinalize_stop_threadr\   rf   atexitstartrm   s    r   r|   z_PeriodicTimer.start   s    <<?@@99.))	
 "**$##T\\4993G3G
 "'r   c                 >    | j                   r| j                          yy)z'Stop the timer at the next opportunity.N)rf   rm   s    r   cancelz_PeriodicTimer.cancel  s    ??OO r   c                     | j                   j                  | j                        sM | j                  | j                  i | j
                   | j                   j                  | j                        sLy y N)r\   waitrW   rY   rZ   r[   )ctxs    r   rw   z_PeriodicTimer._run  sK    ..%%cll3CLL#((1cjj1 ..%%cll3r   c                 D    |j                          | j                          y r   )setjoin)threadr\   s     r   rz   z_PeriodicTimer._stop_thread  s    r   )r   N)r]   r^   r_   __doc__rc   r	   r@   ra   r   rx   ry   r   r   r   rk   propertyrn   rr   r|   r~   staticmethodrw   rz   rb   r   r   rT   rT      s      C=f))** N 9% 	
  
& hsm  	S 	T 	4
 2 2  r   rT   )r7   rN   r!   r:   rP   rx   datetimer   	threadingr   r   typingr   r   r   r	   r
   r   __all__r@   r   r#   r$   r   boolrI   r`   rR   rT   rb   r   r   <module>r      s      	     # > > '
'# #c3h #Lc hsm .sm.+..
38_.b1C 1D 1hE%ue|!445 $ e er   