3
O4W^/  ã               @   s    yd dl mZ W n  ek
r0   d dlmZ Y nX d dlZddlmZmZ i add„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zedeƒ edeƒ edeƒ dS )é    )ÚurlparseNé   )Ú	FileStoreÚTCPStorec             C   s"   | t krtdj| ƒƒ‚|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   ú=/tmp/pip-build-e66j97b2/torch/torch/distributed/rendezvous.pyÚregister_rendezvous_handler   s    r   c             K   s4   t | ƒ}|jtkr"tdj|jƒƒ‚t|j | f|ŽS )NzNo rendezvous handler for {}://)r   r	   r   r   r   )ÚurlÚkwargsÚresultr   r   r   Ú
rendezvous,   s    
r   c             C   s   t d|  ƒS )Nz+Error initializing torch.distributed using )Ú
ValueError)Úmsgr   r   r   Ú_rendezvous_error4   s    r   c             c   sœ   dd„ }t | ƒ}|j}|s"|dƒ‚tdd„ td |jjdƒƒD ƒƒ}d|krR|dƒ‚d	|krb|d
ƒ‚t|d ƒ}t|d	 ƒ}t||ƒ}|||fV  tdƒ‚d S )Nc             S   s   t d|  ƒS )Nzfile:// rendezvous: )r   )r   r   r   r   Ú_error9   s    z(_file_rendezvous_handler.<locals>._errorzpath missingc             s   s   | ]}|j d ƒV  qdS )ú=N)Úsplit)Ú.0Úpairr   r   r   ú	<genexpr>@   s    z+_file_rendezvous_handler.<locals>.<genexpr>ú&Úrankzrank parameter missingÚ
world_sizezworld size parameter missingz3Unable to perform rerendezvous using file:// method)	r   ÚpathÚdictÚfilterÚqueryr   Úintr   r   )r   r   r   r   r!   r   r   Ústorer   r   r   Ú_file_rendezvous_handler8   s     
r$   c             c   s¨   dd„ }t | ƒ}|js|dƒ‚tdd„ td |jjdƒƒD ƒƒ}d|krN|dƒ‚d	|k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: )r   )r   r   r   r   r   P   s    z'_tcp_rendezvous_handler.<locals>._errorzport number missingc             s   s   | ]}|j d ƒV  qdS )r   N)r   )r   r   r   r   r   r   V   s    z*_tcp_rendezvous_handler.<locals>.<genexpr>r   r   zrank parameter missingr   zworld size parameter missingr   z2Unable to perform rerendezvous using tcp:// method)
r   Úportr   r    r!   r   r"   r   Úhostnamer   )r   r   r   r!   r   r   Ústart_daemonr#   r   r   r   Ú_tcp_rendezvous_handlerO   s     r(   c       
      #   s8  dd„ ‰ ‡ fdd„}| j dƒs&ˆ dƒ‚t| ƒ}tdd„ td |jjd	ƒƒD ƒƒ}d
|krdt|d
 ƒ}ntjj	dd ƒ}|d kr‚|dƒ‚d|kr˜t|d ƒ}ntjj	dd ƒ}|d kr¶|dƒ‚tjj	dd ƒ}|d krÔ|dƒ‚tjj	dd ƒ}|d krò|dƒ‚t|ƒ}t|ƒ}t|ƒ}|dk}t
||||ƒ}	|	||fV  tdƒ‚d S )Nc             S   s   t d|  ƒS )Nzenv:// rendezvous: )r   )r   r   r   r   r   g   s    z'_env_rendezvous_handler.<locals>._errorc                s   ˆ d|  ƒS )Nz-environment variable %s expected, but not setr   )Úvar)r   r   r   Ú
_env_errorj   s    z+_env_rendezvous_handler.<locals>._env_errorzenv://zurl must be equal to `env://`c             s   s   | ]}|j d ƒV  qdS )r   N)r   )r   r   r   r   r   r   p   s    z*_env_rendezvous_handler.<locals>.<genexpr>r   r   ZRANKr   Z
WORLD_SIZEZMASTER_ADDRZMASTER_PORTr   z2Unable to perform rerendezvous using env:// method)Ú
startswithr   r   r    r!   r   r"   ÚosÚenvironÚgetr   r   )
r   r*   r   r!   r   r   Zmaster_addrZmaster_portr'   r#   r   )r   r   Ú_env_rendezvous_handlerf   s:    
 r/   ÚfileZtcpÚenv)Úurllib.parser   ÚImportErrorr,   Ú r   r   r   r   r   r   r$   r(   r/   r   r   r   r   Ú<module>   s   0

