
    zj                        U d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z	 er%d dl
mZ d dlmZ d dlmZ d d	lmZ ed
         Zded<    G d d          Zd Zd Zej        ddfddZd dZdS )!    )annotations)TYPE_CHECKINGClassVarLiteralN)	framework)stream)	TypeAlias)Tensor)task)Group)r               r	   	_ReduceOpc                  Z    e Zd ZU dZdZded<   dZded<   dZd	ed
<   dZded<   dZ	ded<   dS )ReduceOpa  

    Specify the type of operation used for element-wise reductions.
    It should be one of the following values:

        ReduceOp.SUM

        ReduceOp.MAX

        ReduceOp.MIN

        ReduceOp.PROD

    Examples:
        .. code-block:: python

            >>> # doctest: +REQUIRES(env: DISTRIBUTED)
            >>> import paddle
            >>> import paddle.distributed as dist

            >>> dist.init_parallel_env()
            >>> if dist.get_rank() == 0:
            ...     data = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
            >>> else:
            ...     data = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
            >>> dist.all_reduce(data, op=dist.ReduceOp.SUM)
            >>> print(data)
            >>> # [[5, 7, 9], [5, 7, 9]] (2 GPUs)
    r   zClassVar[Literal[0]]SUMr   zClassVar[Literal[1]]MAXr   zClassVar[Literal[2]]MINr   zClassVar[Literal[3]]PRODr   zClassVar[Literal[4]]AVGN)
__name__
__module____qualname____doc__r   __annotations__r   r   r   r        w/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distributed/communication/reduce.pyr   r   !   ss          < !"C!!!! !C!!!! !C!!!!!"D"""" !C!!!!!!r   r   c                   | t           j        k    rt          j        j         j        S | t           j        k    rt          j        j         j        S | t           j        k    rt          j        j         j        S | t           j        k    rt          j        j         j        S | t           j        k    rt          j        j         j        S t          d|  d          )NzUnknown reduce_op type for .)
r   r   r   corer   r   r   PRODUCTr   
ValueError)	reduce_ops    r    _get_reduce_opr'   G   s    HL  ~&**	hl	"	"~&**	hl	"	"~&**	hm	#	#~&..	hl	"	"~&**
?9???
@
@@r   c                    |  dS )N_r   )op_names    r    _to_inplace_opr+   V   s    ===r   Ttensorr
   dstintopgroupGroup | Nonesync_opboolreturnr   c                B   |t           j        k    rvt                      sh|#t          j        j                                        n|}|                     d|j        z             t          j
        | |t           j        ||d          S t          j
        | ||||d          S )a  

    Reduce a tensor to the destination from all others. As shown below, one process is started with a GPU and the data of this process is represented
    by its group rank. The destination of the reduce operator is GPU0 and the process is sum. Through reduce operator,
    the GPU0 will owns the sum of all data from all GPUs.

    .. image:: https://githubraw.cdn.bcebos.com/PaddlePaddle/docs/develop/docs/api/paddle/distributed/img/reduce.png
        :width: 800
        :alt: reduce
        :align: center

    Args:
        tensor (Tensor): The output Tensor for the destination and the input Tensor otherwise. Its data type
            should be float16, float32, float64, int32, int64, int8, uint8, bool or bfloat16.
        dst (int): The destination rank id.
        op (ReduceOp.SUM|ReduceOp.MAX|ReduceOp.MIN|ReduceOp.PROD|ReduceOp.AVG, optional): The operation used. Default value is ReduceOp.SUM.
        group (Group|None, optional): The group instance return by new_group or None for global default group.
        sync_op (bool, optional): Whether this op is a sync op. The default value is True.

    Returns:
        Return a task object.

    Examples:
        .. code-block:: python

            >>> # doctest: +REQUIRES(env: DISTRIBUTED)
            >>> import paddle
            >>> import paddle.distributed as dist

            >>> dist.init_parallel_env()
            >>> if dist.get_rank() == 0:
            ...     data = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
            >>> else:
            ...     data = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
            >>> dist.reduce(data, dst=0)
            >>> print(data)
            >>> # [[5, 7, 9], [5, 7, 9]] (2 GPUs, out for rank 0)
            >>> # [[1, 2, 3], [1, 2, 3]] (2 GPUs, out for rank 1)
    Ng      ?F)r-   r/   r0   r2   use_calc_stream)r   r   is_avg_reduce_op_supportedpaddledistributed
collective_get_global_groupscale_nranksr   reducer   	is_memberidget_group_rankr   r   r   _C_opsr%   )r,   r-   r/   r0   r2   r6   ring_idgdsts           r    r>   r>   Z   s    ^ 
X\#=#?#? } );;=== 	
 	cEL()))}|!
 
 
 	
 =   r   c                 z    t          j                    r't           j        j                                        dk    S dS )NiR  F)r8   is_compiled_with_cudabaser#   nccl_versionr   r   r    r7   r7      s4    #%% {,,..%77ur   )r,   r
   r-   r.   r/   r   r0   r1   r2   r3   r4   r   )r4   r3   )
__future__r   typingr   r   r   r8   r    paddle.distributed.communicationr   typing_extensionsr	   r
   paddle.base.corer   &paddle.distributed.communication.groupr   r   r   r   r'   r+   r   r>   r7   r   r   r    <module>rO      sa   # " " " " " " 3 3 3 3 3 3 3 3 3 3        3 3 3 3 3 3 2++++++%%%%%%<<<<<<"=1I1111#" #" #" #" #" #" #" #"LA A A   La6 a6 a6 a6 a6H     r   