o
    "j                     @   s`   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 dd Z
dd Zejd	d
dfddZd	S )    )	framework)data_feeder)_get_global_group_warn_cur_rank_not_in_group)ReduceOp_get_reduce_opc                 C   s<   t |d}|r|j| |S |j| ||}|r|  |S )N	allreduce)r   Zprocess_groupZall_reduce_on_calc_stream
all_reducewait)tensoropgroupsync_opuse_calc_streamop_typetask r   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distributed/communication/stream/all_reduce.py_all_reduce_in_dygraph   s   
r   c                 C   s~   t | dg dd t|d}|d u rdn|j}t|ts!tdtj|fi t	 }|j
|d| gid| gi||d	d
 d S )Nr   )	Zfloat16Zfloat32Zfloat64Zint32Zint64Zint8Zuint8boolZuint16r	   r   r   z3The type of 'ring_id' for all_reduce should be int.XZOut)ring_idr   )typeZinputsZoutputsattrs)r   Zcheck_variable_and_dtyper   id
isinstanceint
ValueErrorr   ZLayerHelperlocalsZ	append_op)r   r   r   r   r   r   r   helperr   r   r   _all_reduce_in_static_mode%   s"   


r    NTFc                 C   sf   t |rdS |s|rtdt r#|du rt n|}t| ||||S |du s+J dt| ||||S )a  

    Perform specific reduction (for example, sum, max) on inputs across devices.

    Args:
        tensor (Tensor): The input tensor on each rank. The result will overwrite this tenor after communication. Support
            float16, float32, float64, int32, int64, int8, uint8 or bool as the input data type.
        op (ReduceOp.SUM|ReduceOp.MAX|ReduceOp.MIN|ReduceOp.PROD, optional): The reduction used. If none is given, use ReduceOp.SUM as default.
        group (Group, optional): Communicate in which group. If none is given, use the global group as default.
        sync_op (bool, optional): Indicate whether the communication is sync or not. If none is given, use true as default.
        use_calc_stream (bool, optional): Indicate whether the communication is done on calculation stream. If none is given, use false as default. This
            option is designed for high performance demand, be careful to turn it on except you are clearly know its meaning.

    Returns:
        Return a task object.

    Examples:
        .. code-block:: python

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

            >>> dist.init_parallel_env()
            >>> local_rank = dist.get_rank()
            >>> data = None
            >>> if local_rank == 0:
            ...     data = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
            >>> else:
            ...     data = paddle.to_tensor([[1, 2, 3], [1, 2, 3]])
            >>> task = dist.stream.all_reduce(data, sync_op=False)
            >>> task.wait()
            >>> out = data
            >>> print(out)
            [[5, 7, 9], [5, 7, 9]]
    Nz5use_calc_stream can only be true in sync op behavior.z3Group can not be used in static graph mode for now.)r   RuntimeErrorr   Zin_dynamic_moder   r   r    )r   r   r   r   r   r   r   r   r	   H   s"   '


r	   )Zpaddler   Zpaddle.baser   Z&paddle.distributed.communication.groupr   r   Z'paddle.distributed.communication.reducer   r   r   r    ZSUMr	   r   r   r   r   <module>   s   
$