
    Ǆg&              	          U d dl mZmZ 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mZmZ d dlmZmZmZmZ ddlmZ i aeeed	eeeeef      f   f   ed
<   ddgZd Zdedeeef   fdZdeeef   defdZdededee   fdZ d$dededefdZ!d Z"d Z#defdZ$defdZ%	 d%defdZ&efdede	fdZ'efdede	fd Z( ed!e'        ed"e(        ed#e$       y# e$ rZ ed      edZ[ww xY w)&    )urlparse
urlunparsezEurllib cannot be found, urlparse from python2 is no longer supported.N)	timedelta)CallableDictIteratorOptionalTuple)	FileStorePrefixStoreStoreTCPStore   )default_pg_timeout._rendezvous_handlersregister_rendezvous_handler
rendezvousc                 D    | t         v rt        d|  d      |t         | <   y)a  
    Register 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 z:// already registeredN)r   RuntimeError)schemehandlers     d/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributed/rendezvous.pyr   r      s.    2 %%4VH<RSTT#*     queryreturnc                 z    d t        d | j                  d            D        D ci c]  }|d   |d    c}S c c}w )Nc              3   >   K   | ]  }|j                  d         yw)=N)split).0pairs     r   	<genexpr>z!_query_to_dict.<locals>.<genexpr><   s     PTZZ_Ps   &r   r   )filterr   )r   r!   s     r   _query_to_dictr%   9   sG     Qekk#>N1OP 	Qa  s   8
query_dictc                 h    | j                  dt        j                  j                  dd            dk(  S )N	use_libuv	USE_LIBUV1)getosenviron)r&   s    r   _get_use_libuv_from_query_dictr.   @   s)     >>+rzz~~k3'GHCOOr   urlrankworld_size_optc                    t        |       }|dd}|j                  dk(  rUt        t        j                  j                  d|            }t        t        j                  j                  d|            }n|}|dk7  s|dk7  s|t        |j                        }d|vrd|vsJ d|  d       |dk7  rt        |      |d<   |dk7  s|t        |      |d<   |j                  d	j                  |j                         D cg c]  \  }}| d
|  c}}             }t        |      } |j                  t        vrt        d|j                   d      t        |j                     | fi |S c c}}w )NenvRANK
WORLD_SIZEr0   
world_sizez	The url: z7 has node-specific arguments(rank, world_size) already.r#   r   )r   zNo rendezvous handler for z://)r   r   intr,   r-   r+   r%   r   str_replacejoinitemsr   r   r   )	r/   r0   r1   kwargsresultr7   r&   kvs	            r   _rendezvous_helperrA   F   sj   c]F
==E!rzz~~fd34DRZZ^^L*EFJ#
rzZ2%)?#FLL1
*$Z)G	TseRS	TG2:!$TJv~5'*:J|$XXj6F6F6HIda!AaSzIJK ! 
  }}007cJKK.s=f== Js   9E'r7   c                    t        | t        t        f      st        dt	        |        d|        t        |t
        j                        st        d|       t        |t
        j                        st        d|       t        | ||fi |S )Nz`url` must be a string. z: z`rank` must be an integer. z!`world_size` must be an integer. )
isinstancer9   bytesr   typenumbersIntegralrA   )r/   r0   r7   r=   s       r   r   r   c   s    cC<(5d3i[3%HIIdG,,-8?@@j'"2"23>zlKLLc4>v>>r   c                 N    t        t        | j                  |d             \  }}}|S N)nextrA   init_method)backend_optionsr0   store_s       r   _create_store_from_optionsrO   p   s'    )/*E*EtTRSKE1aLr   c                     t        d| z         S )Nz+Error initializing torch.distributed using )
ValueErrormsgs    r   _rendezvous_errorrT   u   s    CcIJJr   c              +     K   d }t        |       }|j                  }t        j                  dk(  rYdd l}|j
                  |j                  z   }|j                  j                  |      }|rt        j                  j                  |      }|s |d      t        |j                        }d|vr |d      d|vr |d      t        |d         }t        |d         }	t        ||	      }
|
||	f t        d	      w)
Nc                     t        d| z         S )Nzfile:// rendezvous: rT   rR   s    r   _errorz(_file_rendezvous_handler.<locals>._errorz   s     !7#!=>>r   win32r   zpath missingr0   rank parameter missingr7   world size parameter missingz3Unable to perform rerendezvous using file:// method)r   pathsysplatformurllib.requestnetlocrequesturl2pathnamer,   normpathr%   r   r8   r   r   )r/   r=   rX   r>   r\   urllib	full_pathr&   r0   r7   rM   s              r   _file_rendezvous_handlerrf   y   s     ? c]F;;D
||wMMFKK/	~~**9577##D)D^$$-JZ-..:%344z&!"DZ-.JdJ'E$

## L
MMs   C8C:c                  Z    t         j                  j                  dd       t        d      k(  S )NTORCHELASTIC_USE_AGENT_STORET)r,   r-   r+   r9    r   r   _torchelastic_use_agent_storerj      s!    ::>>8$?3t9LLr   c           	          d|cxk  rdk  sn t        d| d      t               r1t        j                  d   }t	        | ||d|      }t        d| |      S |dk(  }t	        | ||||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``.

    By default, the TCPStore server uses the asynchronous implementation
    ``LibUVStoreDaemon`` which utilizes libuv.

    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 .TORCHELASTIC_RESTART_COUNTFz/worker/attempt_T)multi_tenantr(   )rQ   rj   r,   r-   r   r   )	hostnameportr0   r7   timeoutr(   attempt	tcp_storestart_daemons	            r   _create_c10d_storeru      s    . uHaPQQ$&**9:XtZH	-gY7CCqy
 	
r   rq   c              +   ~  K   d }t        |       }|j                  s |d      t        |j                        }d|vr |d      d|vr |d      t	        |d         }t	        |d         }t        |      }|j                  J t        |j                  |j                  ||||      }	|	||f t        d      w)Nc                     t        d| z         S )Nztcp:// rendezvous: rW   rR   s    r   rX   z'_tcp_rendezvous_handler.<locals>._error        !6!<==r   zport number missingr0   rZ   r7   r[   z3Unable to perform re-rendezvous using tcp:// method)	r   rp   r%   r   r8   r.   ro   ru   r   )
r/   rq   r=   rX   r>   r&   r0   r7   r(   rM   s
             r   _tcp_rendezvous_handlerry      s     > c]F;;*++-JZ-..:%344z&!"DZ-.J.z:I??&&&dJE $

## L
MMs   B;B=c              +     K   d fddt         dt         ffd}t        |       }t        |j                        }d|v rt	        |d         }nt	         |d            }d|v rt	        |d         }nt	         |d	            } |d
      }t	         |d            }	t        |      }
t        ||	||||
      }|||f t        d      w)Nc                     t        d| z         S )Nzenv:// rendezvous: rW   rR   s    r   rX   z'_env_rendezvous_handler.<locals>._error   rx   r   c                      d|  d      S )Nzenvironment variable z expected, but not setri   )varrX   s    r   
_env_errorz+_env_rendezvous_handler.<locals>._env_error   s    -cU2HIJJr   env_varr   c                 \    t         j                  j                  | d       }|s |       |S rI   )r,   r-   r+   )r   env_valr~   s     r   _get_env_or_raisez2_env_rendezvous_handler.<locals>._get_env_or_raise   s*    **..$/W%%Nr   r0   r5   r7   r6   MASTER_ADDRMASTER_PORTz3Unable to perform re-rendezvous using env:// method)r9   r   r%   r   r8   r.   ru   r   )r/   rq   r=   r   r>   r&   r0   r7   master_addrmaster_portr(   rM   r~   rX   s               @@r   _env_rendezvous_handlerr      s     >K3 3  c]F-J :f%&$V,-z!L12
*<89
#M2K'67K.z:I[$
GYE $

## L
MMs   C	Ctcpr4   file)r3   r3   )T))urllib.parser   r   ImportErrorerF   r,   r]   datetimer   typingr   r   r   r	   r
   torch.distributedr   r   r   r   	constantsr   r   r9   r8   __annotations____all__r   r%   boolr.   rA   r   rO   rT   rf   rj   ru   ry   r   ri   r   r   <module>r      s  1  	 
  < < E E ) TV d3huUC_7M.N)N OOP U(,
7+@# $sCx. PtCH~ P$ P>C >s >HSM >:
?C 
?s 
?S 
?
KN# N@Mt M
 :>(

(
X $6N	N N@ $6-N	-N -N` E#: ; E#: ; F$< =k  
Os   C C,	C''C,