
    Wj                          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	 d dl
mZmZ d dlmZ dgZdedeeef         fd	Zd
ededz  fdZdedz  dedeeef         fdZdedefdZdeeeef         z  ddfdZ G d d          ZdS )    N)Callable)	timedelta)EventThread)Anyparse_rendezvous_endpoint
config_strreturnc                 Z   i }|                                  } | s|S |                     d          }|D ]x}|                    dd          ^}}|                                 }|st          d          |r|d                                          }nd}|st          d| d          |||<   y|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          o/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torch/distributed/elastic/rendezvous/utils.py_parse_rendezvous_configr      s      F!!##J !!#&&J  xxQ''fiikk 	7    	1IOO%%EEE 	[[[[   sM    port_strc                 R    | r$t          j        d|           rt          |           S dS )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.     BH_h77 8}}4r   endpointdefault_portc                    | |                                  } | sd|fS | d         dk    r| d         dk    r	| gg R ^}}n|                     dd          ^}}t          |          dk    r"|d         dk    r|d         dk    r
|dd         }t          |          dk    r1t          |d                   }||d	k    rt	          d
|  d          n|}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.
    N	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   D   sT    >>## +\** {chrlc11mmmttooc1--t 4yy1}}aCDHOOAbDz
4yyA~~tAw''<45=='x ' ' '   ) 8ND)) 
; ; ; ;
 
 	

 :r   r+   c                 n   | dk    rdS 	 t          j        |           }n# t          $ r d}Y nw xY w|r	|j        rdS 	 t	          j        | dt          j        t          j                  }n%# t          t          j        f$ r}g }Y d}~nd}~ww xY wd |D             }t	          j	                    }| |k    rdS t	          j        |dt          j        t          j                  }|D ]P}|d         r|d         | k    r dS |r"|d         d         t          |          k    r dS |d         d         |v r dS Qd	S )
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flagsc                 *    g | ]}|d          d         S )   r    ).0host_addr_infos     r   
<listcomp>z-_matches_machine_hostname.<locals>.<listcomp>   s"    NNN^N1%a(NNNr      r2   r   F)	ipaddress
ip_addressr   is_loopbacksocketgetaddrinfoIPPROTO_TCPAI_CANONNAMEgaierrorgethostnamestr)r+   addrhost_addr_list_host_ip_list	this_host	addr_list	addr_infos           r   _matches_machine_hostnamerI   u   s    {t#D))       t+$f08K
 
 
 (    ON~NNNL"$$Iyt"4v19L  I   	Q< 	IaLD0044  	IaLOs4yy0044 Q<?l**44 + 5s#    ..,A* *B BBsecondsc                     t          | t                    rt          j        |  } | dk    rt	          j        |            dS dS )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      sH     '5!! +.'*$
7 r   c            
       
   e Zd ZU dZ G d d          Zedz  ed<   edz  ed<   ej	        dz  ed<   eed<   d	e
d
ed         dededdf
dZededz  fd            ZdeddfdZddZddZedd            Zed             ZdS )_PeriodicTimerzRepresent a timer that periodically runs a specified function.

    Args:
        interval:
            The interval, in seconds, between each run.
        function:
            The function to run.
    c                   n    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<   dS )	_PeriodicTimer._Contextinterval.Nfunction.argskwargs
stop_eventN)__name__
__module____qualname__float__annotations__r   rM   r   dictrA   r   r3   r   r   _ContextrV      s`         9%%%%CHoS#Xr   rc   N_name_thread
_finalizer_ctxrW   rY   rX   rZ   r[   r
   c                    d | _         |                                 | _        |                                | j        _        || j        _        |pd| j        _        |pi | j        _        t                      | j        _	        d | _
        d | _        d S )Nr3   )rd   rc   rg   total_secondsrW   rY   rZ   r[   r   r\   re   rf   )selfrW   rY   rZ   r[   s        r   __init__z_PeriodicTimer.__init__   su     
MMOO	%3355	%		!<R	$ww	r   c                     | j         S )zGet the name of the timer.)rd   rj   s    r   namez_PeriodicTimer.name   s     zr   rn   c                 @    | j         rt          d          || _        dS )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)     < 	A?@@@


r   c                 0   | j         rt          d          t          | j        | j        pd| j        fd          | _         t          j        | | j        | j         | j        j	                  | _
        d| j
        _        | j                                          dS )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    < 	A?@@@9.)	
 
 
 "*$#T\493G
 
 "'r   c                 @    | j         r|                                   dS dS )z'Stop the timer at the next opportunity.N)rf   rm   s    r   cancelz_PeriodicTimer.cancel  s,    ? 	OO	 	r   c                     | j                             | j                  s8 | j        | j        i | j         | j                             | j                  6d S d S N)r\   waitrW   rY   rZ   r[   )ctxs    r   rw   z_PeriodicTimer._run  sg    .%%cl33 	2CL#(1cj111 .%%cl33 	2 	2 	2 	2 	2r   c                 V    |                                  |                                  d S r   )setjoin)threadr\   s     r   rz   z_PeriodicTimer._stop_thread  s#    r   )r
   N)r]   r^   r_   __doc__rc   rA   ra   r   rx   ry   r   r   r   rk   propertyrn   rr   r|   r~   staticmethodrw   rz   r3   r   r   rT   rT      s                 :d] 4'''' NNN 9% 	
  
   & cDj    X	S 	T 	 	 	 	   4   
 2 2 2 \2   \  r   rT   )r8   rN   r   r;   rP   rx   collections.abcr   datetimer   	threadingr   r   typingr   __all__rA   rb   r   r   r    rM   r   boolrI   r`   rR   rT   r3   r   r   <module>r      s        				    $ $ $ $ $ $       # # # # # # # #       '
'# #c3h # # # #Lc cDj    .Dj.(+.
38_. . . .b1C 1D 1 1 1 1hEE%,// D    e e e e e e e e e er   