
    `i                      6   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 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dS )    N)	timedelta)EventThread)AnyCallableOptionalUnion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          ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/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
   C   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      r4   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_hostnamerK   t   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)rL   s    r   _delayrT      sH     '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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   rO   r   dictrC   r   r5   r   r   _ContextrX      s`         9%%%%CHoS#Xr   re   _name_thread
_finalizer_ctxrY   r[   rZ   r\   r]   r   Nc                    d | _         |                                 | _        |                                | j        _        || j        _        |pd| j        _        |pi | j        _        t                      | j        _	        d | _
        d | _        d S )Nr5   )rf   re   ri   total_secondsrY   r[   r\   r]   r   r^   rg   rh   )selfrY   r[   r\   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.)rf   rl   s    r   namez_PeriodicTimer.name   s     zr   rp   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)rg   RuntimeErrorrf   )rl   rp   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.rr   PeriodicTimerT)targetrp   r\   daemonFN)rg   rs   r   _runrf   ri   weakreffinalize_stop_threadr^   rh   atexitstartro   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)rh   ro   s    r   cancelz_PeriodicTimer.cancel  s,    ? 	OO	 	r   c                     | j                             | j                  s8 | j        | j        i | j         | j                             | j                  6d S d S N)r^   waitrY   r[   r\   r]   )ctxs    r   ry   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   r|   z_PeriodicTimer._stop_thread  s#    r   )r   N)r_   r`   ra   __doc__re   r   rC   rc   r   rz   r{   r   r   r   rm   propertyrp   rt   r~   r   staticmethodry   r|   r5   r   r   rV   rV      s                 C=f)**** NNN 9% 	
  
   & hsm    X	S 	T 	 	 	 	   4   
 2 2 2 \2   \  r   rV   )r:   rP   r   r=   rR   rz   datetimer   	threadingr   r   typingr   r   r   r	   __all__rC   rd   r   r!   r"   rO   r
   boolrK   rb   rT   rV   r5   r   r   <module>r      s        				          # # # # # # # # 1 1 1 1 1 1 1 1 1 1 1 1 '
'# #c3h # # # #Lc hsm    .sm.+..
38_. . . .b1C 1D 1 1 1 1hE%ue|!445 $    e e e e e e e e e er   