a
    ÞñhÆ  ã                   @   sô   zd dl mZmZ W n" ey6   d dlmZmZ Y n0 d dlmZ d dlZd dlZd dl	Z	ddl
mZ ddlmZ e	jdkrŠddl
mZ i add	„ Zddd„Zdd„ Zdd„ Zefdd„Zefdd„Ze	jdkræedeƒ edeƒ edeƒ dS )é    )ÚurlparseÚ
urlunparseNé   )Ú	FileStore)Údefault_pg_timeoutÚwin32)ÚTCPStorec                 C   s"   | t v rtd | ¡ƒ‚|t | < dS )aÊ  Registers a new rendezvous handler.

    Before we can run collective algorithms, participating processes
    need to find each other and exchange information to be able to
    communicate. We call this process rendezvous.

    The outcome of the rendezvous process is a triplet containing a
    shared key/value store, the rank of the process, and the total
    number of participating processes.

    If none of the bundled rendezvous methods apply to your execution
    environment you can opt to register your own rendezvous handler.
    Pick a unique name and use the URL scheme to identify it when
    calling the `rendezvous()` function.

    Arguments:
        scheme (str): URL scheme to identify your rendezvous handler.
        handler (function): Handler that is invoked when the
            `rendezvous()` function is called with a URL that uses
            the corresponding scheme. It must be a generator function
            that yields the triplet.
    z/Rendezvous handler for {}:// already registeredN)Ú_rendezvous_handlersÚRuntimeErrorÚformat)ÚschemeÚhandler© r   ú\/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/distributed/rendezvous.pyÚregister_rendezvous_handler   s
    ÿr   éÿÿÿÿc                 K   s,  t | tjƒs td t| ƒ| ¡ƒ‚t |tjƒs:td |¡ƒ‚t |tjƒsTtd |¡ƒ‚t| ƒ}|dksl|dkrút	dd„ t
d |j d¡ƒD ƒƒ}d|vrœd	|vs¬J d
j| d�ƒ‚|dkr¼||d< |dkrÌ||d	< |jd d dd„ | ¡ D ƒ¡¡d�}t|ƒ} |jtv�rtd |j¡ƒ‚t|j | fi |¤ŽS )Nz`url` must be a string. {}: {}z`rank` must be an integer. {}z#`world_size` must be an integer. {}r   c                 s   s   | ]}|  d ¡V  qdS ©ú=N©Úsplit©Ú.0Úpairr   r   r   Ú	<genexpr>?   s   zrendezvous.<locals>.<genexpr>ú&ÚrankÚ
world_sizezEThe url: {url} has node-specific arguments(rank, world_size) already.)Úurlz{}c                 S   s   g | ]\}}d   ||¡‘qS )z{}={})r   )r   ÚkÚvr   r   r   Ú
<listcomp>M   ó    zrendezvous.<locals>.<listcomp>)ÚqueryzNo rendezvous handler for {}://)Ú
isinstanceÚsixZstring_classesr
   r   ÚtypeÚnumbersÚIntegralr   ÚdictÚfilterr"   r   Ú_replaceÚjoinÚitemsr   r   r	   )r   r   r   ÚkwargsÚresultZ
query_dictr   r   r   Ú
rendezvous2   s<    ÿÿÿÿþÿr/   c                 C   s   t d|  ƒS )Nz+Error initializing torch.distributed using )Ú
ValueError©Úmsgr   r   r   Ú_rendezvous_errorV   s    r3   c           
      k   s¼   dd„ }t | ƒ}|j}tjdkr6dd l}|j |j¡}|sB|dƒ‚tdd„ td |j	 
d¡ƒD ƒƒ}d	|vrr|d
ƒ‚d|vr‚|dƒ‚t|d	 ƒ}t|d ƒ}t||ƒ}	|	||fV  tdƒ‚d S )Nc                 S   s   t d|  ƒS )Nzfile:// rendezvous: ©r3   r1   r   r   r   Ú_error[   s    z(_file_rendezvous_handler.<locals>._errorr   r   zpath missingc                 s   s   | ]}|  d ¡V  qdS r   r   r   r   r   r   r   f   r!   z+_file_rendezvous_handler.<locals>.<genexpr>r   r   úrank parameter missingr   úworld size parameter missingz3Unable to perform rerendezvous using file:// method)r   ÚpathÚsysÚplatformÚurllib.requestÚrequestÚurl2pathnamer(   r)   r"   r   Úintr   r
   )
r   r-   r5   r.   r8   Úurllibr"   r   r   Ústorer   r   r   Ú_file_rendezvous_handlerZ   s$    
 
rA   c           
      k   sª   dd„ }t | ƒ}|js|dƒ‚tdd„ td |j d¡ƒD ƒƒ}d|vrN|dƒ‚d	|vr^|d
ƒ‚t|d ƒ}t|d	 ƒ}|dk}t|j|j|||ƒ}	|	||fV  t	dƒ‚d S )Nc                 S   s   t d|  ƒS )Nztcp:// rendezvous: r4   r1   r   r   r   r5   v   s    z'_tcp_rendezvous_handler.<locals>._errorzport number missingc                 s   s   | ]}|  d ¡V  qdS r   r   r   r   r   r   r   |   r!   z*_tcp_rendezvous_handler.<locals>.<genexpr>r   r   r6   r   r7   r   z2Unable to perform rerendezvous using tcp:// method)
r   Úportr(   r)   r"   r   r>   r   Úhostnamer
   )
r   Útimeoutr-   r5   r.   r"   r   r   Ústart_daemonr@   r   r   r   Ú_tcp_rendezvous_handleru   s     rF   c                 +   s(  dd„ ‰ ‡ fdd„}t | ƒ}tdd„ td |j d¡ƒD ƒƒ}d|v rRt|d ƒ}ntj d	d ¡}|d u rp|d	ƒ‚d
|v r†t|d
 ƒ}ntj dd ¡}|d u r¤|dƒ‚tj dd ¡}|d u rÂ|dƒ‚tj dd ¡}	|	d u rà|dƒ‚t|ƒ}t|ƒ}t|	ƒ}	|dk}
t	||	||
|ƒ}|||fV  t
dƒ‚d S )Nc                 S   s   t d|  ƒS )Nzenv:// rendezvous: r4   r1   r   r   r   r5   �   s    z'_env_rendezvous_handler.<locals>._errorc                    s   ˆ d|  ƒS )Nz-environment variable %s expected, but not setr   )Úvar©r5   r   r   Ú
_env_error�   s    z+_env_rendezvous_handler.<locals>._env_errorc                 s   s   | ]}|  d ¡V  qdS r   r   r   r   r   r   r   ”   r!   z*_env_rendezvous_handler.<locals>.<genexpr>r   r   ZRANKr   Z
WORLD_SIZEZMASTER_ADDRZMASTER_PORTr   z2Unable to perform rerendezvous using env:// method)r   r(   r)   r"   r   r>   ÚosÚenvironÚgetr   r
   )r   rD   r-   rI   r.   r"   r   r   Zmaster_addrZmaster_portrE   r@   r   rH   r   Ú_env_rendezvous_handlerŒ   s6     rM   ZtcpÚenvÚfile)r   r   )Úurllib.parser   r   ÚImportErrorZ
torch._sixZ_sixr$   r&   rJ   r9   Ú r   Ú	constantsr   r:   r   r	   r   r/   r3   rA   rF   rM   r   r   r   r   Ú<module>   s,   

$-


