o
    pi                     @  s   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZ erJd dlmZ d dlmZ d d	lmZ d d
lmZ dd Zdd Zdd Z		 			d"d#d d!ZdS )$    )annotationsN)TYPE_CHECKING)	framework)data_feeder)_get_global_group_get_or_throw_group_rank_warn_cur_rank_not_in_group)Sequence)Tensor)task)Groupc                 C  s<   |j }|r|j| ||S |j| |||}|r|  |S )N)nranksprocess_groupZscatter_tensor_on_calc_streamZscatter_tensorwait)Z
out_tensorZ	in_tensorsrc_rank_in_groupgroupsync_opuse_calc_streamr   r    r   v/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/distributed/communication/stream/scatter.py_scatter_tensor_in_dygraph&   s   r   c                   sr   |j }|j|krt|dkrtdn fddt|D }|r(|j ||S |j |||}|r7|  |S )Nr   0The tensor_list should not be empty on src rank.c                      g | ]} qS r   r   .0_tensorr   r   
<listcomp>C       z'_scatter_in_dygraph.<locals>.<listcomp>)	r   ranklenRuntimeErrorranger   Zscatter_on_calc_streamscatterr   )r   tensor_listr   r   r   r   r   r   r   r   r   _scatter_in_dygraph9   s$   
r&   c              	     s   |d u rt  n|j}t  }|}t|trJ|}	||kr't|	dkr&tdn fddt|D }	t|	d j	dkrCt
j|	dd}nt
j|	dd}|d u rPdn|j}
t dg dd d	}tj|fi t }|j|d
|gid gi|
|||dd d S )Nr   r   c                   r   r   r   r   r   r   r   r   g   r   z+_scatter_in_static_mode.<locals>.<listcomp>)Zaxisr   )Zfloat16Zfloat32Zfloat64Zint32Zint64Zint8Zuint8boolr$   Z	c_scatterXZOut)ring_idrootr   r   )typeZinputsZoutputsattrs)distZget_world_sizer   get_rank
isinstancelistr!   r"   r#   shapepaddlestackconcatidr   Zcheck_variable_and_dtyper   ZLayerHelperlocalsZ	append_op)r   tensor_or_tensor_listr   r   r   r   r   r    Zinput_tensorr%   r)   Zop_typehelperr   r   r   _scatter_in_static_modeS   sD   


r9   TFr   r
   r7    Tensor | Sequence[Tensor] | Nonesrcintr   Group | Noner   r'   r   returntask | Nonec                 C  s   t |rdS |s|rtd|t kr|durtd g }t rH|du r*t n|}t	||}t
|r?t| |||||S t| |||||S |du sPJ dt| |||||S )a'  

    Scatter a tensor (or a tensor list) across devices.

    Args:
        tensor (Tensor): The output 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.
        tensor_or_tensor_list (Union[Tensor, List[Tensor]]): The input to scatter (default is `None`, 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.
        src (int, optional): Rank of the source device. If none is given, use `0` as default.
        group (Group|None, 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

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

            >>> dist.init_parallel_env()
            >>> if dist.get_rank() == 0:
            ...     data1 = paddle.to_tensor([7, 8, 9])
            ...     data2 = paddle.to_tensor([10, 11, 12])
            ...     dist.stream.scatter(data1, src=1)
            >>> else:
            ...     data1 = paddle.to_tensor([1, 2, 3])
            ...     data2 = paddle.to_tensor([4, 5, 6])
            ...     dist.stream.scatter(data1, [data1, data2], src=1)
            >>> out = data1.numpy()
            >>> print(out)
            >>> # [1, 2, 3] (2 GPUs, out for rank 0)
            >>> # [4, 5, 6] (2 GPUs, out for rank 1)
    Nz5use_calc_stream can only be true in sync op behavior.zJSpecific `tensor_or_tensor_list` is meaningless for rank which is not src.z3Group can not be used in static graph mode for now.)r   r"   r-   r.   warningswarnr   Zin_dynamic_moder   r   r2   Z	is_tensorr   r&   r9   )r   r7   r;   r   r   r   r   r   r   r   r$      sV   2	

	
	r$   )Nr   NTF)r   r
   r7   r:   r;   r<   r   r=   r   r'   r   r'   r>   r?   )
__future__r   r@   typingr   r2   Zpaddle.distributeddistributedr-   r   Zpaddle.baser   Z&paddle.distributed.communication.groupr   r   r   collections.abcr	   r
   Zpaddle.base.corer   r   r   r&   r9   r$   r   r   r   r   <module>   s,   >