o
    MeO  ã                   @   s^   d dl Z d dlm  mZ d dlmZ ddd„Zddd„Zdd	„ Zd
d„ Z				ddd„Z
dS )é    N)Ú
collectiveé   c                 C   s   | j |kr	tdƒ‚d S )Nz/The tensor for alltoall is not correctly-sized.)ÚshapeÚRuntimeError)Útensorr   Únranks© r   ú`D:\Projects\ConvertPro\env\Lib\site-packages\paddle/distributed/communication/stream/alltoall.pyÚ_check_tensor_shape   s   
ÿr
   c                 C   s4   t | ƒ|kr
tdƒ‚| D ]}|j|krtdƒ‚qd S )Nz4The tensor_list for alltoall is not correctly-sized.)Úlenr   r   )Ztensor_listr   r   r   r   r   r	   Ú_check_tensor_list_shape   s   ÿ
ÿÿÿr   c                 C   sV   |d u rt  ¡ n|}t| |j|jƒ |r|j || ¡S |j || |¡}|r)| ¡  |S )N)	r   Ú_get_default_groupr
   r   r   Úprocess_groupZalltoall_tensor_on_calc_streamZalltoall_tensorÚwait)Z
out_tensorZ	in_tensorÚgroupÚsync_opÚuse_calc_streamÚtaskr   r   r	   Ú_alltoall_tensor_in_dygraph#   s   ÿr   c                 C   sŽ   |d u rt  ¡ n|}t|ƒdkrtdƒ‚t| ƒdkr$| dd„ |D ƒ7 } n
t| |d j|jƒ |r7|j || ¡S |j 	|| |¡}|rE| 
¡  |S )Nr   z*The input tensor_list should not be empty.c                 S   s   g | ]}t  |¡‘qS r   )ÚpaddleZ
empty_like)Ú.0r   r   r   r	   Ú
<listcomp><   s    
ÿz(_alltoall_in_dygraph.<locals>.<listcomp>)r   r   r   r   r   r   r   r   Zalltoall_on_calc_streamÚalltoallr   )Zout_tensor_listZin_tensor_listr   r   r   r   r   r   r	   Ú_alltoall_in_dygraph4   s(   
ÿÿÿ
ÿr   TFc                 C   s¤   |dur|  ¡ stdƒ‚|s|rtdƒ‚| du rtdƒ‚|du r$tdƒ‚t ¡ rNt | ¡}t |¡}|r>|r>t| ||||ƒS |sJ|sJt| ||||ƒS tdƒ‚tdƒ‚)aÞ  

    Scatter a tensor (or a tensor list) across devices and gather outputs to another tensor (or a tensor list, respectively).

    Args:
        out_tensor_or_tensor_list (Union[Tensor, List[Tensor]]): The output. If it is a tensor, it should be correctly-sized.
        If it is a list, it should be empty or contain correctly-sized tensors. Its data type should be the same as the input.
        in_tensor_or_tensor_list (Union[Tensor, List[Tensor]]): The input to scatter (must be specified on the source rank).
            If it is a tensor, it should be correctly-sized. If it is a list, it should contain correctly-sized tensors. Support
            float16, float32, float64, int32, int64, int8, uint8 or bool as the input data type.
        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.

    Warning:
        This API only supports the dygraph mode now.

    Examples:
        .. code-block:: python

            # required: distributed
            import paddle
            import paddle.distributed as dist

            dist.init_parallel_env()
            out_tensor_list = []
            if dist.get_rank() == 0:
                data1 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
                data2 = paddle.to_tensor([[7, 8, 9], [10, 11, 12]])
            else:
                data1 = paddle.to_tensor([[13, 14, 15], [16, 17, 18]])
                data2 = paddle.to_tensor([[19, 20, 21], [22, 23, 24]])
            task = dist.stream.alltoall(out_tensor_list, [data1, data2], sync_op=False)
            task.wait()
            print(out_tensor_list)
            # [[[1, 2, 3], [4, 5, 6]], [[13, 14, 15], [16, 17, 18]]]    (2 GPUs, out for rank 0)
            # [[[7, 8, 9], [10, 11, 12]], [[19, 20, 21], [22, 23, 24]]] (2 GPUs, out for rank 1)
    NzjThe group should not be None and all ranks which invoke this operation should be the member of this group.z5use_calc_stream can only be true in sync op behavior.zThe output should be specified.zThe input should be specified.z:The output and input should be both tensor or tensor list.zIpaddle.distributed.stream.alltoall is only supported in dygraph mode now.)Z	is_memberr   Ú	frameworkZin_dygraph_moder   Z	is_tensorr   r   )Zout_tensor_or_tensor_listZin_tensor_or_tensor_listr   r   r   Zout_is_tensorZin_is_tensorr   r   r	   r   O   s>   /ÿÿ

þþÿÿr   )r   )NTF)r   Zpaddle.fluid.frameworkZfluidr   Zpaddle.distributedr   r
   r   r   r   r   r   r   r   r	   Ú<module>   s   


ü