
    Αi                        S SK Jr  S SKJrJr  \(       a  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKrS SKrS SKJr  S SKJr  S S	KJr  S
SKJrJr      S           SS jjr   S         SS jjrg)    )annotations)TYPE_CHECKINGAny)Sequence)Tensor)task)GroupN)	framework)stream   )convert_object_to_tensorconvert_tensor_to_objectc                2    [         R                  " XX#U5      $ )a  

Scatter a tensor to all participators. As shown below, one process is started with a GPU and the source of the scatter
is GPU0. Through scatter operator, the data in GPU0 will be sent to all GPUs averagely.

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

Args:
    tensor (Tensor): The output Tensor. Its data type
        should be float16, float32, float64, int32, int64, int8, uint8, bool or bfloat16.
    tensor_list (list|tuple): A list/tuple of Tensors to scatter. Every element in the list must be a Tensor whose data type
        should be float16, float32, float64, int32, int64, int8, uint8, bool or bfloat16. Default value is None.
    src (int): The source rank id. Default value is 0.
    group (Group, 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:
    None.

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.scatter(data1, src=1)
        >>> else:
        ...     data1 = paddle.to_tensor([1, 2, 3])
        ...     data2 = paddle.to_tensor([4, 5, 6])
        ...     dist.scatter(data1, tensor_list=[data1, data2], src=1)
        >>> print(data1, data2)
        >>> # [1, 2, 3] [10, 11, 12] (2 GPUs, out for rank 0)
        >>> # [4, 5, 6] [4, 5, 6] (2 GPUs, out for rank 1)
)r   scatter)tensortensor_listsrcgroupsync_ops        h/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/communication/scatter.pyr   r   '   s    b >>&s7CC    c                \   [         R                  " 5       (       d   S5       e[        R                  " 5       n/ n/ nXB:X  aD  U H2  n[	        U5      u  pUR                  U5        UR                  U	5        M4     [        U5      n
O[        R                  " / SS9n
[        R                  " X5        [        U
R                  5       5      n/ nU HQ  nUR                  5       n[        R                  " X/5      n[        R                   " U5      nUR                  U5        MS     [        R                  " U/SS9n[#        UXB:X  a  UOSX#5        [        R                  " / SS9n[#        UXB:X  a  UOSX#5        U R%                  5         U R                  ['        UUR                  5       5      5        g)a  

Scatter picklable objects from the source to all others. Similar to scatter(), but python object can be passed in.

Args:
    out_object_list (list): The list of objects to store the scattered objects.
    in_object_list (list): The list of objects to scatter. Only objects on the src rank will be scattered.
    src (int): The source rank in global view.
    group (Group): The group instance return by new_group or None for global default group.

Returns:
    None.

Warning:
    This API only supports the dygraph mode.

Examples:
    .. code-block:: python

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

        >>> dist.init_parallel_env()
        >>> out_object_list = [] # type: ignore
        >>> if dist.get_rank() == 0:
        ...     in_object_list = [{'foo': [1, 2, 3]}, {'foo': [4, 5, 6]}]
        >>> else:
        ...     in_object_list = [{'bar': [1, 2, 3]}, {'bar': [4, 5, 6]}]
        >>> dist.scatter_object_list(out_object_list, in_object_list, src=1)
        >>> print(out_object_list)
        >>> # [{'bar': [1, 2, 3]}] (2 GPUs, out for rank 0)
        >>> # [{'bar': [4, 5, 6]}] (2 GPUs, out for rank 1)
z6scatter_object_list doesn't support static graph mode.int64)dtypeuint8N)r
   in_dynamic_modedistget_rankr   appendmaxpaddleemptyr   	broadcastintitemnumpynpresize	to_tensorr   clearr   )out_object_listin_object_listr   r   rankin_obj_tensorsin_obj_sizesobj
obj_tensorobj_sizemax_obj_size_tensormax_obj_sizein_tensor_listr   
numpy_data	in_tensor
out_tensorout_tensor_sizes                     r   scatter_object_listr:   [   ss   N $$&& @& ==?DNL{!C#;C#@ J!!*-) " ",/$ll2W=
(.*//12L N \\^
YYz>:
$$Z0	i(	 !
 |nG<JJ$+4Lll2W5OOT[\dCO _-A-A-CDr   )Nr   NT)r   r   r   zSequence[Tensor] | Noner   r$   r   Group | Noner   boolreturnztask | None)Nr   N)
r+   z	list[Any]r,   zlist[Any] | Noner   r$   r   r;   r=   None)
__future__r   typingr   r   collections.abcr   r!   r   paddle.base.corer   &paddle.distributed.communication.groupr	   r&   r'   paddle.distributeddistributedr   r
    paddle.distributed.communicationr   serialization_utilsr   r   r   r:    r   r   <module>rI      s    # %(%<   !  3 ,01D1D(1D 
1D 	1D
 1D 1Dl (,	JJ$J 
J 	J
 
Jr   