o
    #i                     @   s   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dZdd Zdd Zdejfd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              
   C   sB  |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||	|| || z/tjtj||	|| tj	|d|d	}
|d u rT|
 }n|
|d
}|rbt
|||d td |W S  ty } z*t|tkr||k rtd|	|| |d7 }ntd| d|	 d| W Y d }~nd }~ww q%)Nr	   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)
ValueErrorloggerinfor   	functoolspartialdistTCPStoredatetime	timedelta_check_full_rankRuntimeErrorstr_ADDRESS_IN_USEwarning)r   r   r   r   r   r   retriesr   attemptr   store_builderstoree r+   i/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/distributed/elastic/utils/distributed.pyr      sb   
	
	


r   c              
   C   sP   zt | |t|d W d S  ty' } zt|tkr"td| d| d }~w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*   r+   r+   r,   r!   h   s   
r!   c                  C   s>   t  } t|  |  d W  d    S 1 sw   Y  d S )Nr
   )r   r   getsockname)sockr+   r+   r,   r   t   s   

$r   returnc                  C   s   t jddt jt jd} | D ]<}|\}}}}}t  |||}z|d |d |W   S  tyI } z|  tj	d|d W Y d}~qd}~w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)r5   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addrr7   r8   proto_sr*   r+   r+   r,   r   z   s    


r   )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$   r0   r/   boolr#   intfloatr   r!   r   r   r+   r+   r+   r,   <module>   sJ   
K