a
    –¥»aÞ  ã                   @   s  d dl Z d dlmZ d dlmZ ejjfdd„Zd ejjfdd„Z	d ejjfdd„Z
ejjejjfd	d
„Zejjfdd„Zejjfdd„Zejjejjfdd„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eƒZdS )é    N)ÚFunctionc                 C   s   t  ||| ¡S )a»  
    Broadcasts the tensor to the whole group.

    ``tensor`` must have the same number of elements in all processes
    participating in the collective.

    Arguments:
        tensor (Tensor): Data to be sent if ``src`` is the rank of current
            process.
        src (int): Source rank.
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        Tensor: Received tensor from the broadcast op.

    )Ú
_BroadcastÚapply)ÚtensorÚsrcÚgroup© r   úY/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributed/nn/functional.pyÚ	broadcast   s    r
   c                 C   s   t  ||| ¡S )aT  
    Gathers a list of tensors in a single process.

    Arguments:
        tensor (Tensor): Input tensor.
        dst (int, optional): Destination rank (default is 0).
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        tuple[Tensor]: List of appropriately-sized tensors with the gathered data.
    )Ú_Gatherr   )r   Údstr   r   r   r	   Úgather   s    r   c                 C   s   t j||g| ¢R Ž S )aö  
    Scatters a list of tensors to all processes in a group.

    Each process will receive exactly one tensor and store its data in the
    ``tensor`` argument.

    Arguments:
        tensors (list[Tensor]): List of tensors to scatter on the source rank.
            Receivers must pass ``None`.
        src (int, optional): Source rank (default is 0).
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        Tensor: Output tensor from the scatter operation.

    )Ú_Scatterr   )Útensorsr   r   r   r   r	   Úscatter)   s    r   c                 C   s   t  |||| ¡S )a  
    Reduces the tensor data across all machines.

    Only the process with rank ``dst`` is going to receive the final result.

    Arguments:
        tensor (Tensor): Input of the collective.
        dst (int): Destination rank.
        op (optional): One of the values from
            ``torch.distributed.ReduceOp``
            enum.  Specifies an operation used for element-wise reductions.
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        Tensor: Output of the collective.

    )Ú_Reducer   )r   r   Úopr   r   r   r	   Úreduce=   s    r   c                 C   s   t  || ¡S )a  
    Gathers tensors from the whole group in a list.

    Arguments:
        tensor (Tensor): Tensor to be broadcast from current process.
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        tuple[Tensor]): Output of the collective.

    )Ú
_AllGatherr   )r   r   r   r   r	   Ú
all_gatherR   s    r   c                 C   s   t j|g| ¢R Ž S )ai  
    Each process scatters list of input tensors to all processes in a group and
    return gathered list of tensors in output list.

    Arguments:
        tensors (list[Tensor]): List of tensors to scatter one per rank.
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        tuple[Tensor]): Output of the collective.

    )Ú	_AlltoAllr   )r   r   r   r   r	   Ú
all_to_alla   s    r   c                 C   s   t  ||| ¡S )a*  
    Reduces the tensor data across all machines in such a way that all get
    the final result.

    After the call the returned tensor is going to be bitwise
    identical in all processes.

    Arguments:
        tensor (Tensor): Input of the collective.
        op (optional): One of the values from
            ``torch.distributed.ReduceOp``
            enum.  Specifies an operation used for element-wise reductions.
        group (ProcessGroup, optional): The process group to work on.

    Returns:
        Tensor: Output of the collective

    )Ú
_AllReducer   )r   r   r   r   r   r	   Ú
all_reduceq   s    r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                 C   s2   || _ || _t ¡ | _| ¡ }tj|||d� |S )N©r   )r   r   ÚdistÚget_rankÚrankÚcloner
   )Úctxr   r   r   r   r   r	   Úforwardˆ   s    
z_Broadcast.forwardc                 C   s6   t  | jtjj| j|¡}| j| jkr,| ¡  d d |fS ©N)	r   r   r   r   ÚReduceOpÚSUMr   r   Zzero_)r   Úgrad_outputÚgxr   r   r	   Úbackward“   s    z_Broadcast.backwardN©Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr    r&   r   r   r   r	   r   ‡   s   

r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                    sh   || _ || _‡ fdd„ttj|d�ƒD ƒ}tj|d�|krNtjˆ |||d� ntjˆ d ||d� t|ƒS )Nc                    s   g | ]}t  ˆ ¡‘qS r   )ÚtorchÚ
zeros_like©Ú.0Úi©r   r   r	   Ú
<listcomp>¤   s   z#_Gather.forward.<locals>.<listcomp>r   )r   r   Úranger   Úget_world_sizer   r   Útuple)r   r   r   r   Ztensor_listr   r1   r	   r    œ   s    
ÿz_Gather.forwardc                 G   s   dt j| j| jg|¢R Ž f S ©N)NN)r   r   r   r   ©r   Úgrad_outputsr   r   r	   r&   ­   s    z_Gather.backwardNr'   r   r   r   r	   r   ›   s   
r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                    sr   || _ || _t‡ fdd„ˆ D ƒƒs&J ‚t ˆ d ¡}tj|d�|kr\tj|tˆ ƒ||d� ntj|d ||d� |S )Nc                 3   s"   | ]}|  ¡ ˆ d    ¡ kV  qdS )r   N)Úsize)r/   Út©r   r   r	   Ú	<genexpr>·   ó    z#_Scatter.forward.<locals>.<genexpr>r   r   )	r   r   Úallr,   r-   r   r   r   Úlist)r   r   r   r   Úoutputr   r;   r	   r    ³   s    z_Scatter.forwardc                 C   s   dt  | j| j|¡ S r6   )r   r   r   r   ©r   r$   r   r   r	   r&   ¿   s    z_Scatter.backwardNr'   r   r   r   r	   r   ²   s   
r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                 C   s*   || _ || _| ¡ }tj||||d� |S ©N)r   r   )r   r   r   r   r   )r   r   r   r   r   r   r   r	   r    Å   s
    z_Reduce.forwardc                 C   s   dt  | j| j|¡f S )N)NNN)r   r   r   r   rA   r   r   r	   r&   Í   s    z_Reduce.backwardNr'   r   r   r   r	   r   Ä   s   
r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                    s<   || _ ‡ fdd„ttj|d�ƒD ƒ}tj|ˆ |d� t|ƒS )Nc                    s   g | ]}t  ˆ ¡‘qS r   ©r,   Z
empty_liker.   r1   r   r	   r2   Ö   s   z&_AllGather.forward.<locals>.<listcomp>r   )r   r3   r   r4   r   r5   )r   r   r   Úout_tensor_listr   r1   r	   r    Ó   s    
ÿz_AllGather.forwardc                 G   s0   t j| jg|¢R Ž }tjt |¡dd�}d |fS )Nr   )Zdim)r   r   r   r,   ÚsumÚstack)r   r8   Zgxsr%   r   r   r	   r&   Ü   s    z_AllGather.backwardNr'   r   r   r   r	   r   Ò   s   
r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                    s´   || _ ‡ fdd„ttj|d�ƒD ƒ}d gtj|d� }tj|d�}tj|d�tjju r˜ttj|d�ƒD ].}d }||kr~tˆ ƒ}tj	|| |||d� qfntj
|tˆ ƒ|d� t|ƒS )Nc                    s   g | ]}t  ˆ | ¡‘qS r   rC   r.   r;   r   r	   r2   ç   s   z%_AlltoAll.forward.<locals>.<listcomp>r   )r   r3   r   r4   r   Úget_backendZBackendZGLOOr?   r   r   r5   )r   r   r   rD   ÚreqsZmy_rankr0   Zto_sendr   r;   r	   r    ä   s    
ÿz_AlltoAll.forwardc                 G   s   dt j| jg|¢R Ž  S )Nr!   )r   r   r   r7   r   r   r	   r&   ÷   s    z_AlltoAll.backwardNr'   r   r   r   r	   r   ã   s   
r   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r   c                 C   s(   || _ || _| ¡ }tj|||d� |S rB   )r   r   r   r   r   )r   r   r   r   r   r   r	   r    ý   s
    z_AllReduce.forwardc                 C   s   dt  | j| j|¡f S r6   )r   r   r   r   rA   r   r   r	   r&     s    z_AllReduce.backwardNr'   r   r   r   r	   r   ü   s   
r   )r,   Ztorch.autogradr   Ztorch.distributedZdistributedr   r   ZWORLDr
   r   r   r"   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   Ú<module>   s    