a
    –¥»a»!  ã                   @   s4  zd dl mZmZ W n ey.   edƒ‚Y n0 d dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZ d dlmZ d dlmZmZmZmZ ddlmZ i ad	d
„ Zd!eeedœdd„Zdd„ Zedœdd„Zedœdd„Zedœdd„Z efeedœdd„Z!efeedœdd„Z"ede!ƒ ede"ƒ ed eƒ dS )"é    )ÚurlparseÚ
urlunparsezEurllib cannot be found, urlparse from python2 is no longer supported.N)Ú	timedelta)ÚDictÚOptionalÚUnion)Ú	FileStoreÚPrefixStoreÚStoreÚTCPStoreé   )Údefault_pg_timeoutc                 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.

    Args:
        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   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributed/rendezvous.pyÚregister_rendezvous_handler   s
    ÿr   éÿÿÿÿ)ÚurlÚrankÚ
world_sizec                 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>C   s   zrendezvous.<locals>.<genexpr>ú&r   r   zEThe url: {url} has node-specific arguments(rank, world_size) already.©r   z{}c                 S   s   g | ]\}}d   ||¡‘qS )z{}={})r   )r   ÚkÚvr   r   r   Ú
<listcomp>R   ó    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   Ú
rendezvous6   s@    ÿÿÿÿþÿÿr5   c                 C   s   t d|  ƒS )Nz+Error initializing torch.distributed using )Ú
ValueError©Úmsgr   r   r   Ú_rendezvous_error\   s    r9   r#   c                 k   sÖ   dd„ }t | ƒ}|j}tjdkrPdd l}|j|j }|j |¡}|rPtj 	|¡}|s\|dƒ‚t
dd„ td |j d¡ƒD ƒƒ}d	|vrŒ|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: ©r9   r7   r   r   r   Ú_errora   s    z(_file_rendezvous_handler.<locals>._errorÚwin32r   zpath missingc                 s   s   | ]}|  d ¡V  qdS r   r   r   r   r   r   r!   s   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ÚnetlocÚrequestÚurl2pathnameÚosÚnormpathr.   r/   r(   r   Úintr   r   )r   r3   r;   r4   r?   ÚurllibÚ	full_pathr(   r   r   Ústorer   r   r   Ú_file_rendezvous_handler`   s*    
 
rL   )Úreturnc                   C   s   t j dd ¡tdƒkS )NZTORCHELASTIC_USE_AGENT_STORET)rF   ÚenvironÚgetÚstrr   r   r   r   Ú_torchelastic_use_agent_store‚   s    rQ   c                 C   sP   t ƒ r0tjd }t| ||d|ƒ}td|› �|ƒS |dk}t| ||||dd�S dS )a  
    Smartly creates a c10d Store object on ``rank`` based on whether
    we need to re-use agent store. The TCPStore server is assumed to be hosted
    on ``hostname:port``.

    If ``torchelastic_use_agent_store()`` is ``True``, then it is assumed that
    the agent leader (node rank 0) hosts the TCPStore server (for which the
    endpoint is specified by the given ``hostname:port``). Hence
    ALL ranks will create and return a TCPStore client (e.g. ``start_daemon=False``).

    If ``torchelastic_use_agent_store()`` is ``False``, then rank 0 will host
    the TCPStore (with multi-tenancy) and it is assumed that rank 0's hostname
    and port are correctly passed via ``hostname`` and ``port``. All
    non-zero ranks will create and return a TCPStore client.
    ZTORCHELASTIC_RESTART_COUNTFz/worker/attempt_r   T)Zmulti_tenantN)rQ   rF   rN   r   r	   )ÚhostnameÚportr   r   ÚtimeoutÚattemptZ	tcp_storeZstart_daemonr   r   r   Ú_create_c10d_store†   s    
ÿrV   )r   rT   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	 ƒ}|jd us„J ‚t|j|j|||ƒ}|||fV  t	dƒ‚d S )Nc                 S   s   t d|  ƒS )Nztcp:// rendezvous: r:   r7   r   r   r   r;   ¥   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   r=   r   r>   z3Unable to perform re-rendezvous using tcp:// method)
r   rS   r.   r/   r(   r   rH   rR   rV   r   )	r   rT   r3   r;   r4   r(   r   r   rK   r   r   r   Ú_tcp_rendezvous_handler¢   s     rW   c                 +   sÐ   dd„ ‰‡fdd„‰ t t dœ‡ fdd„}t| ƒ}tdd	„ td |j d
¡ƒD ƒƒ}d|v rft|d ƒ}nt|dƒƒ}d|v rˆt|d ƒ}nt|dƒƒ}|dƒ}t|dƒƒ}	t||	|||ƒ}
|
||fV  tdƒ‚d S )Nc                 S   s   t d|  ƒS )Nzenv:// rendezvous: r:   r7   r   r   r   r;   Â   s    z'_env_rendezvous_handler.<locals>._errorc                    s   ˆ d|  ƒS )Nz-environment variable %s expected, but not setr   )Úvar)r;   r   r   Ú
_env_errorÅ   s    z+_env_rendezvous_handler.<locals>._env_error)Úenv_varrM   c                    s$   t j | d ¡}|sˆ | ƒ‚n|S d S )N)rF   rN   rO   )rZ   Zenv_val)rY   r   r   Ú_get_env_or_raiseÈ   s    
z2_env_rendezvous_handler.<locals>._get_env_or_raisec                 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_PORTz3Unable to perform re-rendezvous using env:// method)	rP   r   r.   r/   r(   r   rH   rV   r   )r   rT   r3   r[   r4   r(   r   r   Zmaster_addrZmaster_portrK   r   )rY   r;   r   Ú_env_rendezvous_handler¿   s      r\   ZtcpÚenvÚfile)r   r   )#Úurllib.parser   r   ÚImportErrorr,   rF   r@   Údatetimer   Útypingr   r   r   Z
torch._sixZ_sixr*   Ztorch.distributedr   r	   r
   r   Ú	constantsr   r   r   rP   rH   r5   r9   rL   ÚboolrQ   rV   rW   r\   r   r   r   r   Ú<module>   s>   ÿ
&"ÿÿÿÿ.

