
    קg!                         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	 d dl
mZ d dlmZ g dZ ee          ZdZdZd	Z	 	 	 	 	 	 ddededededededee         fdZd Zd Zdej        fdZdS )    N)closing)Optional)
get_logger)barrier)create_c10d_storeget_free_portget_socket_with_portzAddress already in usezSocket Timeoutz_tcp_store/num_members   X  T   	is_serverserver_addrserver_port
world_sizetimeoutwait_for_workers	use_libuvc           	      ,   |t                               d           t          j                            dd          dk    }|dk    r|dk    rt          d| d|           |dk    rt                               d|           |dk    r|nd}	 |dk    r|}	nt                      }	t                               d
||	|| ||           	 t          j	        ||	|| t          j        |          ||          }
|rt          |
||           t                               d           |
S # t          $ rc}t          |          t          k    r@||k     r#t                               d|	||           |dz  }nt          d| d|	 d          | Y d }~nd }~ww xY w)Nzargument use_libuv is deprecated and ignored. Set USE_LIBUV environment variable to "0" to disable libuv, or "1" to enable it. If the env var is not set, libuv will be used by default.	USE_LIBUV1r
   r   zCserver_port must be specified when world_size > 1, got server_port=z, world_size=z+sever_port: %s, specified, ignoring retriesTziCreating c10d store on %s:%s
  world_size  : %s
  is_server   : %s
  timeout(sec): %s
  use_libuv   : %s
)seconds)	host_nameportr   	is_masterr   r   r   )r   zSuccessfully created c10d storez)port: %s already in use, attempt: [%s/%s]zon z, port: z already in use)loggerwarningosenvironget
ValueErrorinfor   distTCPStoredatetime	timedelta_check_full_rankRuntimeErrorstr_ADDRESS_IN_USE)r   r   r   r   r   r   retriesr   attemptr   storees               g/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributed/elastic/utils/distributed.pyr   r      s&    9	
 	
 	
 
{C00C7IbZ!^^xR]xxlvxx
 
 	
 bA;OOO %**ggG7"DD ??D#
 	
 	
 	
#	M%%# *7;;;!1#  E   E 
GDDDDKK9:::L 	 	 	 1vv((W$$NNC	   qLGG&HkHH4HHH   GGGG	G7s   AD$ $
F.AFFc                     	 t          | |t          |           d S # t          $ r2}t          |          t          k    rt          d| d          | d }~ww xY w)N)
key_prefixbarrier_timeoutztimed out waiting for all z members to join)r   _TCP_STORE_INITr(   r)   _SOCKET_TIMEOUTTimeoutError)r-   r   r   r.   s       r/   r'   r'   v   s|    zowWWWWWW   q66_$$IZIII  s    
A-AAc                      t                      } t          |           5  |                                 d         cddd           S # 1 swxY w Y   dS )a  
    Returns an unused port on localhost.

    This function finds an unused port on localhost by opening to socket to bind
    to a port and then closing it.

    Returns:
        int: an unused port on localhost

    Example:
        >>> # xdoctest: +SKIP("Nondeterministic")
        >>> get_free_port()
        63976

    ..note:
        The port returned by :func:`get_free_port` is not reserved and may be
        taken by another process after this function returns.
    r   N)r	   r   getsockname)socks    r/   r   r      s    &  !!D	 % %!!!$% % % % % % % % % % % % % % % % % %s   AA	A	returnc                     t          j        ddt           j        t           j                  } | D ]}|\  }}}}}t          j         |||          }	 |                    d           |                    d           |c S # t          $ r:}|                                 t          	                    d|           Y d}~d}~ww xY wt          d          )	a  
    Returns a free port on localhost that is "reserved" by binding a temporary
    socket on it. Close the socket before passing the port to the entity
    that requires it. Usage example

    ::

    sock = _get_socket_with_port()
    with closing(sock):
        port = sock.getsockname()[1]
        sock.close()
        # there is still a race-condition that some other process
        # may grab this port before func() runs
        func(port)
    	localhostN)hostr   familytype)r;   r   r   zSocket creation attempt failed.)exc_infozFailed to create a socket)socketgetaddrinfo	AF_UNSPECSOCK_STREAMbindlistenOSErrorcloser   r   r(   )addrsaddrr=   r>   proto_sr.   s           r/   r	   r	      s    " tF,<6CU  E  	J 	J$(!eQM&$..	JFF#$$$HHQKKKHHH 	J 	J 	JGGIIINN<qNIIIIIIII	J 2
3
33s   +A>>
C0B==C)r
   r   r   Tr   N)r%   r   r@   
contextlibr   typingr   torch.distributeddistributedr#   'torch.distributed.elastic.utils.loggingr   %torch.distributed.elastic.utils.storer   __all____name__r   r*   r4   r3   boolr)   intfloatr   r'   r   r	        r/   <module>rZ      sm    				                          > > > > > > 9 9 9 9 9 9 I
H
H	H		*"* ! $U UUU U 	U
 U U ~U U U Up	 	 	% % %04fm 4 4 4 4 4 4rY   