
    Αi                        S SK Jr  S SKrS SKJ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Jr  \(       a  S SKJr  S SKJr  S S	KJr  S S
KJr  S rS rS r     S             SS jjrg)    )annotationsN)TYPE_CHECKING)	framework)data_feeder)_get_global_group_get_or_throw_group_rank_warn_cur_rank_not_in_group)Sequence)Tensor)task)Groupc                    UR                   nU(       a  UR                  R                  XU5      $ UR                  R                  XX$5      nU(       a  UR                  " 5         U$ )N)nranksprocess_groupscatter_tensor_on_calc_streamscatter_tensorwait)
out_tensor	in_tensorsrc_rank_in_groupgroupsync_opuse_calc_streamr   r   s           o/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/communication/stream/scatter.py_scatter_tensor_in_dygraphr   &   s`     \\F""@@#4
 	
 --0D 		K    c                `   UR                   nUR                  U:X  a  [        U5      S:X  a  [        S5      eO[	        U5       Vs/ s H  opPM     nnU(       a  UR
                  R                  XU5      $ UR
                  R                  XX$5      nU(       a  UR                  " 5         U$ s  snf )Nr   0The tensor_list should not be empty on src rank.)	r   ranklenRuntimeErrorranger   scatter_on_calc_streamscatterr   )	tensortensor_listr   r   r   r   r   _r   s	            r   _scatter_in_dygraphr(   9   s     \\Fzz&&{q B  !
 (-V}5}!v}5""99!2
 	
 &&.D 		K 6s   B+c           
        Uc  [         R                  " 5       OUR                  n[         R                  " 5       nUn[	        U[
        5      (       a  Un	Xr:X  a  [        U	5      S:X  a  [        S5      eO[        U5       V
s/ s H  oPM     n	n
[        U	S   R                  5      S:X  a  [        R                  " U	SS9nO[        R                  " U	SS9nUc  SOUR                  n[        R                  " U S/ SQS5        Sn[         R"                  " U40 [%        5       D6nUR'                  USU/0S	U /0UUUUS
.S9  g s  sn
f )Nr   r   )axisr%   )float16float32float64int32int64int8uint8boolr$   	c_scatterXOut)ring_idrootr   r   )typeinputsoutputsattrs)distget_world_sizer   get_rank
isinstancelistr    r!   r"   shapepaddlestackconcatidr   check_variable_and_dtyper   LayerHelperlocals	append_op)r%   tensor_or_tensor_listr   r   r   r   r   r   input_tensorr&   r'   r6   op_typehelpers                 r   _scatter_in_static_moderN   S   sC    ',mT  "F==?D(L'..+$;1$"F  %
 ,1=9=a6=K9{1~##$)!<<!<L!==1=L=aehhG((		
 	  G""77fh7F
l^$!%&	
	  
7 :s    D?c                   [        U5      (       a  gU(       d  U(       a  [        S5      eU[        R                  " 5       :w  a  Ub  [        R
                  " S5        / n[        R                  " 5       (       aU  Uc
  [        5       OUn[        X#5      n[        R                  " U5      (       a  [        U UUUUU5      $ [        U UUUUU5      $ Ub   S5       e[        U UUUUU5      $ )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   in_dynamic_moder   r   rB   	is_tensorr   r(   rN   )r%   rJ   srcr   r   r   r   s          r   r$   r$      s   d #5))C
 	
 dmmo ,MM\ !#  ""',}!#%4S@122-%!  '%!  } 	
A	
} '!
 	
r   )Nr   NTF)r%   r   rJ   z Tensor | Sequence[Tensor] | NonerT   intr   zGroup | Noner   r2   r   r2   returnztask | None)
__future__r   rP   typingr   rB   paddle.distributeddistributedr<   r   paddle.baser   &paddle.distributed.communication.groupr   r   r	   collections.abcr
   r   paddle.base.corer   r   r   r(   rN   r$    r   r   <module>r`      s    #     !  #  (%<&49| ?C!g
g
;g
 
g
 	g

 g
 g
 g
r   