o
    Me                     @   sH   d dl Z d dlm  mZ d dlmZ d dlmZ ejddfddZ	dS )    N)stream)ReduceOpTc                 C   s   t  stj| |||ddS |}|du rdn|j}|tjkr(tj	| d|d|S |tj
kr7tj| d|d|S |tjkrFtj| d|d|S |tjkrUtj| d|d|S td|)a  

    Reduce a tensor over all ranks so that all get the result.
    As shown below, one process is started with a GPU and the data of this process is represented
    by its group rank. The reduce operator is sum. Through all_reduce operator,
    each GPU will have the sum of the data from all GPUs.

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

    Args:
        tensor (Tensor): The input Tensor. It also works as the output Tensor. Its data type
            should be float16, float32, float64, int32, int64, int8, uint8 or bool.
        op (ReduceOp.SUM|ReduceOp.MAX|ReduceOp.MIN|ReduceOp.PROD, optional): The operation used. Default value is ReduceOp.SUM.
        group (Group, optional): The group instance return by new_group or None for global default group.
        sync_op (bool, optional): Wether this op is a sync op. Default value is True.

    Returns:
        Return a task object.

    Examples:
        .. code-block:: python

            # required: 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)
            print(data)
            # [[5, 7, 9], [5, 7, 9]] (2 GPUs)
    F)opgroupsync_opuse_calc_streamNr   r   ring_idzUnknown parameter: {}.)	frameworkZ_in_legacy_dygraphr   
all_reduceidr   SUMpaddleZ_legacy_C_opsZc_allreduce_sum_MAXZc_allreduce_max_ZMINZc_allreduce_min_ZPRODZc_allreduce_prod_
ValueErrorformat)Ztensorr   r   r   r   r    r   [D:\Projects\ConvertPro\env\Lib\site-packages\paddle/distributed/communication/all_reduce.pyr
      s<   '







r
   )
r   Zpaddle.fluid.frameworkZfluidr	   Z paddle.distributed.communicationr   Z'paddle.distributed.communication.reducer   r   r
   r   r   r   r   <module>   s
   