a
    NSicf%                     @   sL  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
 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ee
eef dddZd&eeedddZ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)	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   X/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/distributed/rendezvous.pyregister_rendezvous_handler   s
    r   )queryreturnc                 C   s(   t dd dd td | dD D S )Nc                 s   s   | ]}|d  |d fV  qdS )r   r
   Nr   .0pairr   r   r   	<genexpr>8       z!_query_to_dict.<locals>.<genexpr>c                 s   s   | ]}| d V  qdS )=N)splitr   r   r   r   r   8   r   &)dictfilterr   r   r   r   r   _query_to_dict7   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rt	|j
}d|vrd|vsJ dj| d|dkr||d< |dkr||d< |jd	d
dd | D d}t|} |jtvrt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"   r$   r%   zEThe url: {url} has node-specific arguments(rank, world_size) already.r#   {}r   c                 S   s   g | ]\}}d  ||qS z{}={}r   r   kvr   r   r   
<listcomp>T   r   zrendezvous.<locals>.<listcomp>r    zNo rendezvous handler for {}://)
isinstancesixstring_classesr   r   typenumbersIntegralr   r!   r   _replacejoinitemsr   r   r   )r#   r$   r%   kwargsresult
query_dictr   r   r   
rendezvous:   s<    

r:   c                 C   s   t | j}d}|jdkr8tjd|}ttjd|}t|j}|dkrVt	||d< t	||d< |j
ddd	d
 | D d}t|}|jtvrtd|jtt|j |\}}}|S )Nr"   envRANK
WORLD_SIZEr$   r%   r'   r   c                 S   s   g | ]\}}d  ||qS r(   r)   r*   r   r   r   r-   p   r   z._create_store_from_options.<locals>.<listcomp>r    zNo handler for {}://)r   init_methodr   osenvirongetintr!   r   strr4   r   r5   r6   r   r   r   next)Zbackend_optionsr$   r8   r%   r9   r#   store_r   r   r   _create_store_from_options]   s&    



rG   c                 C   s   t d|  S )Nz+Error initializing torch.distributed using )
ValueErrormsgr   r   r   _rendezvous_errorz   s    rK   r&   c                 k   s   dd }t | }|j}tjdkrPdd l}|j|j }|j|}|rPtj	|}|s\|dt
|j}d|vrv|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: rK   rI   r   r   r   _error   s    z(_file_rendezvous_handler.<locals>._errorwin32r   zpath missingr$   rank parameter missingr%   world size parameter missingz3Unable to perform rerendezvous using file:// method)r   pathsysplatformurllib.requestnetlocrequesturl2pathnamer?   normpathr!   r   rB   r   r   )r#   r7   rM   r8   rQ   urllib	full_pathr9   r$   r%   rE   r   r   r   _file_rendezvous_handler~   s*    


r[   )r   c                   C   s   t jdd tdkS )NZTORCHELASTIC_USE_AGENT_STORET)r?   r@   rA   rC   r   r   r   r   _torchelastic_use_agent_store   s    r\   c                 C   sv   d|  krdk s&n t d| dt rVt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.
    r   i   z-port must have value from 0 to 65535 but was .ZTORCHELASTIC_RESTART_COUNTFz/worker/attempt_T)Zmulti_tenantN)rH   r\   r?   r@   r	   r   )hostnameportr$   r%   timeoutattemptZ	tcp_storeZstart_daemonr   r   r   _create_c10d_store   s    
rb   )r#   r`   c           	      k   s   dd }t | }|js|dt|j}d|vr8|dd|vrH|dt|d }t|d }|jd usnJ t|j|j|||}|||fV  tdd S )	Nc                 S   s   t d|  S )Nztcp:// rendezvous: rL   rI   r   r   r   rM      s    z'_tcp_rendezvous_handler.<locals>._errorzport number missingr$   rO   r%   rP   z3Unable to perform re-rendezvous using tcp:// method)r   r_   r!   r   rB   r^   rb   r   )	r#   r`   r7   rM   r8   r9   r$   r%   rE   r   r   r   _tcp_rendezvous_handler   s    
rc   c                 +   s   dd fdd t t d fdd}t| }t|j}d|v rPt|d }nt|d	}d
|v rrt|d
 }nt|d}|d}t|d}	t||	|||}
|
||fV  tdd S )Nc                 S   s   t d|  S )Nzenv:// rendezvous: rL   rI   r   r   r   rM      s    z'_env_rendezvous_handler.<locals>._errorc                    s    d|  S )Nz-environment variable %s expected, but not setr   )var)rM   r   r   
_env_error   s    z+_env_rendezvous_handler.<locals>._env_error)env_varr   c                    s$   t j| d }|s | n|S d S )N)r?   r@   rA   )rf   Zenv_val)re   r   r   _get_env_or_raise   s    
z2_env_rendezvous_handler.<locals>._get_env_or_raiser$   r<   r%   r=   ZMASTER_ADDRZMASTER_PORTz3Unable to perform re-rendezvous using env:// method)rC   r   r!   r   rB   rb   r   )r#   r`   r7   rg   r8   r9   r$   r%   Zmaster_addrZmaster_portrE   r   )re   rM   r   _env_rendezvous_handler   s     
rh   tcpr;   file)r"   r"   )#urllib.parser   r   ImportErrorr2   r?   rR   datetimer   typingr   
torch._six_sixr/   Ztorch.distributedr   r   r   r	   	constantsr   r   r   rC   r!   rB   r:   rG   rK   r[   boolr\   rb   rc   rh   r   r   r   r   <module>   sB   
 #  -

