
    ΑiZ#                    4   S SK J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  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Jr   SS jrS rS r\R6                  SSS4             SS jjr\R6                  SSS4S jrg)    )annotations)TYPE_CHECKINGN)	framework)data_feeder)_get_global_group_warn_cur_rank_not_in_group)ReduceOp_get_reduce_op)Sequence)Tensor)task)Group)	_ReduceOpc                    [        U5      nU(       a  UR                  R                  XU5      $ UR                  R                  XXt5      nU(       a  UR                  " 5         U$ N)r
   process_group$reduce_scatter_tensor_on_calc_streamreduce_scatter_tensorwait)	
out_tensor	in_tensoropgroupsync_opuse_calc_streamcallerop_typer   s	            v/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/communication/stream/reduce_scatter.py!_reduce_scatter_tensor_in_dygraphr   &   s_     R G""GG7
 	
 44wD 		K    c                    [        U5      nU(       a  UR                  R                  XU5      $ UR                  R                  XXd5      nU(       a  UR                  " 5         U$ r   )r
   r   reduce_scatter_on_calc_streamreduce_scatterr   )tensortensor_listr   r   r   r   r   r   s           r   _reduce_scatter_in_dygraphr&   ?   s_     R G""@@
 	
 --WD 		Kr    c                    Sn[         R                  " U S/ SQU5        [        R                  " U40 [	        5       D6nUc  SOUR
                  n[        R                  " 5       nUR                  USU/0SU /0UUS.S9  g )	Nr#   r$   )	float16float32float64int32int64int8uint8booluint16r   xout)ring_idnranks)typeinputsoutputsattrs)	r   check_variable_and_dtyper   LayerHelperlocalsiddistget_world_size	append_op)r$   tensor_or_tensor_listr   r   helperr3   r4   s          r   _reduce_scatter_in_static_moderB   R   s    G((
	
 	" ""77fh7F=aehhG  "F
+,-!
	  r    TFc                J   [        U5      (       a  gU(       d  U(       a  [        S5      e[        R                  " 5       (       aJ  Uc
  [	        5       OUn[
        R                  " U5      (       a  [        U UUUUU5      $ [        U UUUUU5      $ Ub   S5       e[        XU5      $ )a[  

Reduce, then 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.
        If it is a tensor, it should be correctly-sized. If it is a list, it should contain correctly-sized tensors.
    op (ReduceOp.SUM|ReduceOp.MAX|ReduceOp.MIN|ReduceOp.PROD, optional): The reduction used. If none is given, use ReduceOp.SUM 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([0, 1])
        ...     data2 = paddle.to_tensor([2, 3])
        >>> else:
        ...     data1 = paddle.to_tensor([4, 5])
        ...     data2 = paddle.to_tensor([6, 7])
        >>> dist.stream.reduce_scatter(data1, [data1, data2])
        >>> out = data1.numpy()
        >>> print(out)
        >>> # [4, 6]  (2 GPUs, out for rank 0)
        >>> # [8, 10] (2 GPUs, out for rank 1)
N5use_calc_stream can only be true in sync op behavior.z3Group can not be used in static graph mode for now.)
r   RuntimeErrorr   in_dynamic_moder   paddle	is_tensorr   r&   rB   )r$   r@   r   r   r   r   s         r   r#   r#   t   s    b #5))C
 	
   ""',}!#%1224%  .%  } 	
A	
} .5
 	
r    c           	         [        U5      (       a  gU(       d  U(       a  [        S5      e[        R                  " 5       (       a   Uc
  [	        5       OUn[        U UUUUUS5      $ [        S5      e)a)  

Reduce, then scatter a flattened tensor across devices.

Args:
    out_tensor (Tensor): The output tensor on each rank. The result will overwrite this tenor after communication. Support
        float16, float32, float64, int32 or int64 as the input data type.
    in_tensor (Tensor): The input tensor to reduce and scatter.
    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.

Warning:
    This API will be deprecated in the future, and 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)
NrD   _reduce_scatter_basezUpaddle.distributed.stream._reduce_scatter_base is only supported in dygraph mode now.)r   rE   r   rF   r   r   )r   r   r   r   r   r   s         r   rJ   rJ      s|    b #5))C
 	
   ""',}!#%0"
 	
 _ r    )r#   )r$   r   r@   zTensor | Sequence[Tensor]r   r   r   zGroup | Noner   r/   r   r/   returnztask | None)
__future__r   typingr   rG   paddle.distributeddistributedr=   r   paddle.baser   &paddle.distributed.communication.groupr   r   'paddle.distributed.communication.reducer	   r
   collections.abcr   r   paddle.base.corer   r   r   r   r&   rB   SUMr#   rJ    r    r   <module>rW      s    #    !  # M(%<A 2&J LL!S
S
4S
 	S
 	S

 S
 S
 S
r ||
Gr    