U
    d##                     @   s   d dl mZmZ d dlmZmZmZmZmZ d dl	m
Z
 G dd deZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd dZeegef ZG dd dZe ZdS )    )ABCabstractmethod)AnyCallableDictOptionalTuple)Storec                   @   s   e Zd ZdZdS )RendezvousErrorz/Represents the base type for rendezvous errors.N__name__
__module____qualname____doc__ r   r   L/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributed/elastic/rendezvous/api.pyr
      s   r
   c                   @   s   e Zd ZdZdS )RendezvousClosedErrorz#Raised when a rendezvous is closed.Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )RendezvousTimeoutErrorz2Raised when a rendezvous did not complete on time.Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )RendezvousConnectionErrorz>Raised when the connection to a rendezvous backend has failed.Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )RendezvousStateErrorz1Raised when the state of a rendezvous is corrupt.Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZeedddZeeee	e	f dddZ
eedddZed	d
 Zee	dddZeedddZedddZdS )RendezvousHandlerzMain rendezvous interface.

    Note:
        Distributed Torch users normally **do not** need to implement their own
        ``RendezvousHandler``. An implementation based on C10d Store is already
        provided, and is recommended for most users.
    returnc                 C   s   dS )z+Returns the name of the rendezvous backend.Nr   selfr   r   r   get_backend*   s    zRendezvousHandler.get_backendc                 C   s   dS )a  Main entry-point into the rendezvous barrier.

        Blocks until the rendezvous is complete and the current process is
        included in the formed worker group, or a timeout occurs, or the
        rendezvous was marked closed.

        Returns:
            A tuple of :py:class:`torch.distributed.Store`, ``rank``, and
            ``world size``.

        Raises:
            RendezvousClosedError:
                The rendezvous is closed.
            RendezvousConnectionError:
                The connection to the rendezvous backend has failed.
            RendezvousStateError:
                The rendezvous state is corrupt.
            RendezvousTimeoutError:
                The rendezvous did not complete on time.
        Nr   r   r   r   r   next_rendezvous.   s    z!RendezvousHandler.next_rendezvousc                 C   s   dS )a  Checks whether the rendezvous has been closed.

        A closed rendezvous means all future attempts to re-rendezvous within
        same job will fail.

        ``is_closed()`` and :py:meth:`set_closed` have semantics of eventual
        propagation and should not be used for synchronization. The intention is
        that if at least one node decides the job is finished, it will close the
        rendezvous, and other nodes will soon observe this and stop running as
        well.
        Nr   r   r   r   r   	is_closedG   s    zRendezvousHandler.is_closedc                 C   s   dS )zMarks the rendezvous as closed.Nr   r   r   r   r   
set_closedU   s    zRendezvousHandler.set_closedc                 C   s   dS )aX  Returns the number of nodes who arrived late at the rendezvous
        barrier, hence were not included in the current worker group.

        Callers should periodically call this method to check whether new
        nodes are waiting to join the job and if so admit them by calling
        :py:meth:`next_rendezvous()` (re-rendezvous).
        Nr   r   r   r   r   num_nodes_waitingY   s    z#RendezvousHandler.num_nodes_waitingc                 C   s   dS )a  Returns the run id of the rendezvous.

        The run id is a user-defined id that uniquely identifies an instance of
        a distributed application. It typically maps to a job id and is used to
        allow nodes to join the correct distributed application.
        Nr   r   r   r   r   
get_run_idc   s    zRendezvousHandler.get_run_idc                 C   s   dS )a
  Closes all resources that were open for the rendezvous.

        Example::

            rdzv_handler = ...
            try:
                store, rank, world_size = rdzv_handler.next_rendezvous()
            finally:
                rdzv_handler.shutdown()
        Nr   r   r   r   r   shutdownl   s    zRendezvousHandler.shutdownN)r   r   r   r   r   strr   r   r	   intr   boolr   r   r   r    r!   r   r   r   r   r   !   s   
	r   c                   @   sr   e Zd ZdZeeeeedddZdeeedddZdee	e
 e	e
 dd	d
Zdee	e e	e dddZdS )RendezvousParametersa>  Holds the parameters to construct a :py:class:`RendezvousHandler`.

    Args:
        backend:
            The name of the backend to use to handle the rendezvous.
        endpoint:
            The endpoint of the rendezvous, usually in form <hostname>[:<port>].
        run_id:
            The id of the rendezvous.
        min_nodes:
            The minimum number of nodes to admit to the rendezvous.
        max_nodes:
            The maximum number of nodes to admit to the rendezvous.
        **kwargs:
            Additional parameters for the specified backend.
    )backendendpointrun_id	min_nodes	max_nodesc                 K   sj   |st d|dk r$t d| d||k rBt d| d| d|| _|| _|| _|| _|| _|| _d S )N7The rendezvous backend name must be a non-empty string.   z(The minimum number of rendezvous nodes (z) must be greater than zero.z(The maximum number of rendezvous nodes (zK) must be greater than or equal to the minimum number of rendezvous nodes (z).)
ValueErrorr&   r'   r(   r)   r*   config)r   r&   r'   r(   r)   r*   kwargsr   r   r   __init__   s     	
zRendezvousParameters.__init__N)keydefaultr   c                 C   s   | j ||S )zBReturns the value for ``key`` if ``key`` exists, else ``default``.)r.   get)r   r1   r2   r   r   r   r3      s    zRendezvousParameters.getc                 C   s   |  ||}|dkst|tr"|S t|trF|dkr8dS |dkrpdS n*t|trp| dkr`dS | dkrpdS td| d	dS )
z,Returns the value for ``key`` as a ``bool``.Nr,   Tr   F)1truetyesy)0falsefnon%The rendezvous configuration option 'z+' does not represent a valid boolean value.)r3   
isinstancer$   r#   r"   lowerr-   r   r1   r2   valuer   r   r   get_as_bool   s     


z RendezvousParameters.get_as_boolc                 C   sL   |  ||}|dkr|S z
t|W S  tk
rF   td| dY nX dS )z,Returns the value for ``key`` as an ``int``.Nr>   z+' does not represent a valid integer value.)r3   r#   r-   rA   r   r   r   
get_as_int   s    

zRendezvousParameters.get_as_int)N)N)N)r   r   r   r   r"   r#   r0   r   r3   r   r$   rC   rD   r   r   r   r   r%   y   s   r%   c                   @   sR   e Zd ZU dZeeef ed< ddddZeedddd	Z	e
ed
ddZdS )RendezvousHandlerRegistryz@Represents a registry of :py:class:`RendezvousHandler` backends.	_registryNr   c                 C   s
   i | _ d S )N)rF   r   r   r   r   r0      s    z"RendezvousHandlerRegistry.__init__)r&   creatorr   c                 C   sn   |st dz| j| }W n tk
r2   d}Y nX |dk	r`||kr`t d| d| d| d|| j|< dS )zRegisters a new rendezvous backend.

        Args:
            backend:
                The name of the backend.
            creater:
                The callback to invoke to construct the
                :py:class:`RendezvousHandler`.
        r+   NThe rendezvous backend 'z' cannot be registered with 'z$' as it is already registered with ''.)r-   rF   KeyError)r   r&   rG   Zcurrent_creatorr   r   r   register   s    

z"RendezvousHandlerRegistry.register)paramsr   c              	   C   sx   z| j |j }W n0 tk
r@   td|j d| jj dY nX ||}| |jkrttd|  d|j d|S )z,Creates a new :py:class:`RendezvousHandler`.rH   z-' is not registered. Did you forget to call `z`?z(' does not match the requested backend 'rI   )rF   r&   rJ   r-   rK   r   r   RuntimeError)r   rL   rG   handlerr   r   r   create_handler   s    
z(RendezvousHandlerRegistry.create_handler)r   r   r   r   r   r"   RendezvousHandlerCreator__annotations__r0   rK   r%   r   rO   r   r   r   r   rE      s
   
rE   N)abcr   r   typingr   r   r   r   r   Ztorch.distributedr	   	Exceptionr
   r   r   r   r   r   r%   rP   rE   Zrendezvous_handler_registryr   r   r   r   <module>   s   XT;