
    Αi,                    $   S SK Jr  S SKrS SKJrJr  S SKrS SKJr	  S SKJ
r
  \(       a  S SKJr  S SKJr   " S S5      r " S	 S
5      rS rS rS rS rS rSS jrSSS jjrSSS jjrS rSS jr S       SS jjrSSS jjrSSS jjrg)     )annotationsN)TYPE_CHECKINGLiteral)	framework)Tensor)ProcessGroupc                     \ rS rSrSr  S           SS jjr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SS	 j5       r\SS
 j5       r\SS j5       r\SS j5       rSS jrSS jrSS jrSS jrSrg)Group   z'
The abstract representation of group.
Nc                p    Xl         US:  a  [        U5      OSU l        X l        X0l        X@l        XPl        g )Nr   )_rank_in_grouplen_world_size_id_ranks_pg_name)selfrank_in_groupidrankspgnames         f/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/communication/group.py__init__Group.__init__"   s3     ,)6!);3u:
    c                    U R                   $ N)r   r   s    r   rank
Group.rank1   s    """r   c                    U R                   $ r    )r   r!   s    r   r   Group.ranks5   s    {{r   c                ,    [        U R                  5      $ r    )r   r   r!   s    r   nranksGroup.nranks9   s    4;;r   c                    U R                   $ r    )r   r!   s    r   r   
Group.name=   s    zzr   c                    U R                   $ r    )r   r!   s    r   process_groupGroup.process_groupA       xxr   c                    U R                   $ r    )r   r!   s    r   
world_sizeGroup.world_sizeE   s    r   c                6    U R                   R                  5       $ r    )r   r   r!   s    r   backendGroup.backendI   s    xx}}r   c                    U R                   $ r    )r   r!   s    r   r   Group.idM   r.   r   c                H    U R                   S:  a  gU R                  S:  a  gg)Nr   F   T)r"   r'   r!   s    r   	is_memberGroup.is_memberQ   s!    99q=;;?r   c                d    U R                  5       (       a  U R                  R                  U5      $ g)Nr   )r9   r   indexr   r"   s     r   get_group_rankGroup.get_group_rankX   s'    >>::##D))r   c                L    U R                  5       (       a  U R                  U   $ g)z
Get the global rank of a process within a group.

Args:
    rank (int): The local rank within the group.

Returns:
    If the current process is a member of the group, returns the corresponding global rank;
    otherwise returns -1.

r   )r9   r   r=   s     r   get_global_rankGroup.get_global_rank^   s#     >>::d##r   c                   SU R                    SU R                   SU R                   S3nUSR                  [	        [
        U R                  5      5      -  nUS-  nXR                  (       a  U R                  -  nU$ S-  nU$ )Nzrank: z
, nranks: z, id: z	, ranks: z, z; name: None)r"   r'   r   joinmapstrr   r   )r   	debug_strs     r   __repr__Group.__repr__o   s}    TYYKz$++fTWWIYO 	 	TYYs3

344	Z	))TYY7	 287	r   )r   r   r   r   r   r   )NN)r   intr   rK   r   	list[int]r   zProcessGroup | Noner   
str | NonereturnrD   )rN   rK   )rN   rL   )rN   rM   )rN   r   )rN   rG   rN   bool)r"   rK   rN   zint | Literal[-1])__name__
__module____qualname____firstlineno____doc__r   propertyr"   r   r'   r   r,   r0   r3   r   r9   r>   rA   rI   __static_attributes__ r   r   r
   r
      s    #'  	
    
 # #                  "r   r
   c                      \ rS rSrSr0 rSrg)_GroupManagery   r   rX   N)rQ   rR   rS   rT   global_group_idgroup_map_by_idrW   rX   r   r   rZ   rZ   y   s    OOr   rZ   c                     [         R                  [         R                  ;  a  [        S5      e[         R                  [         R                     $ )Nz$The global group is not initialized.)rZ   r\   r]   RuntimeErrorrX   r   r   _get_global_groupr`   ~   s9    $$M,I,IIABB(()F)FGGr   c                    U R                   [        R                  ;   a  [        SU R                    S35      eU [        R                  U R                   '   g )NzThe group with id z already exist.)r   rZ   r]   r_   groups    r   _add_new_grouprd      sA    xx=000/zIJJ.3M!!%((+r   c                <    U R                   [        R                  :H  $ r    )r   rZ   r\   rb   s    r   _is_global_grouprf      s    88}4444r   c                    [         R                  " 5       nU (       a<  U R                  5       (       d'  [        R                  " SU SU R
                   35        gg)NzCurrent global rank z is not in group TF)distget_rankr9   warningswarnr   )rc   global_ranks     r   _warn_cur_rank_not_in_grouprm      sE    --/KU__&&";-/@M	
 r   c                b    UR                  U 5      nUS:  d   SU  SUR                   35       eU$ )Nr   zThe input rank z# can not be found inside the group )r>   r   )rl   rc   
group_ranks      r   _get_or_throw_group_rankrp      sB    %%k2J? 
+&I%**V? r   c                 D    [         R                  [         R                  ;   $ )a  

Check whether the distributed environment has been initialized

Returns:
    `True` if distributed environment has been initialized, otherwise `False`.

Warning:
    This API only supports the dygraph mode.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)
        >>> import paddle

        >>> print(paddle.distributed.is_initialized())
        False

        >>> paddle.distributed.init_parallel_env()
        >>> print(paddle.distributed.is_initialized())
        True

)rZ   r\   r]   rX   r   r   is_initializedrr      s    2 ((M,I,IIIr   c                   U c
  [        5       OU n U R                  [        R                  ;   d   SU R                   S35       e[	        U 5      (       a  [        R                  R                  5         g[        R                  U R                  	 g)a-  
Destroy a given group for communication

Args:
    group (Group, optional): The group to be destroyed. All of process groups, including
                                    the default group, will be destroyed and the distributed
                                    environment will be deinitialized.

Returns : None

Warning:
    This API only supports the dygraph mode.

Examples:
    .. code-block:: python

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

        >>> dist.init_parallel_env()
        >>> group = dist.new_group([0, 1])

        >>> dist.destroy_process_group(group)
        >>> print(dist.is_initialized())
        True
        >>> dist.destroy_process_group()
        >>> print(dist.is_initialized())
        False

NzDestroy group with id z is invalid.)r`   r   rZ   r]   rf   clearrb   s    r   destroy_process_groupru      st    @ $)=eE88}444 
 
,74 %%++-))%((3r   c                    U [         R                  ;   a  [         R                  U    $ [        R                  " SU  S35        g)a  

Get group instance by group id.

Args:
    id (int): the group id. Default value is 0.

Returns:
    Group: the group instance.

Examples:
    .. code-block:: python

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

        >>> dist.init_parallel_env()
        >>> gid = paddle.distributed.new_group([2,4,6])
        >>> paddle.distributed.get_group(gid.id)

zGroup z is not initialized.N)rZ   r]   rj   rk   )r   s    r   	get_grouprw      s<    0 
]***,,R00MMF2$234r   c                    [         R                  " 5       (       a  [        R                  R	                  U 5      $ Sn[         R
                  " U40 [        5       D6nUR                  USU /0SU /0S9  g )Nc_sync_calc_streamXOut)typeinputsoutputs)r   in_dynamic_modepaddle_C_opssync_calc_streamLayerHelperlocals	append_op)tensorop_typehelpers      r   _sync_calc_streamr     sk      ""}}--f55&&&w;&(;&?VH% 	 	
r   c                    [         R                  " 5       (       a!  [        R                  R	                  U /U5      $ Sn[         R
                  " U40 [        5       D6nUR                  USU /0SU /0SU0S9  g )Nc_sync_comm_streamrz   r{   ring_idr|   r}   r~   attrs)r   r   r   r   sync_comm_streamr   r   r   )r   r   r   r   s       r   _sync_comm_streamr     sw      ""}}--vh@@&&&w;&(;&?VH%g&	 	 	
r   c                    Ub  UR                  5       (       d  gU(       a  [        U 5        gUc  SOUR                  n[        X5        g)al  

wait to sync stream for group.

Args:
    tensor (Tensor): The Tensor used before sync.
    group (Group): The Group instance to perform sync.
    use_calc_stream (bool): Whether to use calculation stream (True) or communication stream (False).
        Default to True.

Returns:
    None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)
        >>> import paddle

        >>> paddle.distributed.init_parallel_env()
        >>> tindata = paddle.randn(shape=[2, 3])
        >>> paddle.distributed.all_reduce(tindata, sync_op=True)
        >>> paddle.distributed.wait(tindata)

Nr   )r9   r   r   r   )r   rc   use_calc_streamr   s       r   waitr     s;    8 !2!2&!}!%((&*r   c                v   U b  U R                  5       (       d  g[        R                  " 5       (       a  U c
  [        5       OU n [        R                  " 5       n[        U[        R                  5      (       a  U R                  R                  5       nO+UR                  5       nU R                  R                  U5      nUR                  5         gU c  SOU R                  n[        R                  " S/SSS9n[        R                  " 5       (       aO  [        R                  R                  R                  5       (       d!  [        R                   R                  XUSU5      $ gSn[        U["        5      (       d  [%        S5      e[        R&                  " U40 [)        5       D6nUR+                  US	U/0S
U/0SU0S9  g)a  

Barrier among all participators in the group.

Args:
    group (Group): The group instance return by new_group or None for global default group.

Returns:
    None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)
        >>> import paddle
        >>> from paddle.distributed import init_parallel_env

        >>> paddle.set_device(f'gpu:{paddle.distributed.ParallelEnv().dev_id}')
        >>> init_parallel_env()
        >>> paddle.distributed.barrier()
Nr      int32)dtyper   barrierz,The type of 'group' for barrier must be int.rz   r{   r   )r9   r   r   r`   _current_expected_place
isinstanceCPUPlacer,   r   get_device_idr   r   r   fullcoreis_compiled_with_xpu_legacy_C_opsrK   
ValueErrorr   r   r   )rc   placetask	device_idr   barrier_tensorr   r   s           r   r   r   E  sy   , !2!2  ""',}!#%113eY//00&&..0D++-I&&..y9D		=aehhG[[!aw7N  ""$$99;;''//	7  <
 '3''KLL&&w;&(;.)*^,-g&	 	 	
r   c                    [        U 5      (       a  [        S5      eU c  [        5       n U R                  $ U n U R                  $ )a  
Get the backend of given group.

Args:
    group (Group): The group to work on. Use the global group as default.

Returns:
    Returns the name of the given group backend.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)
        >>> import paddle

        >>> paddle.distributed.init_parallel_env()
        >>> paddle.distributed.get_backend()
        NCCL
zInvalid group specified)rm   r_   r`   r3   rb   s    r   get_backendr     sC    ( #5))455#(=E== 7<E==r   rO   r    )rc   Group | NonerN   rD   )r   )r   rK   rN   r
   )NT)r   r   rc   r   r   rP   rN   rD   )rc   r   rN   rG   )
__future__r   rj   typingr   r   r   paddle.distributeddistributedrh   r   r   paddle.base.corer   r
   rZ   r`   rd   rf   rm   rp   rr   ru   rw   r   r   r   r   r   rX   r   r   <module>r      s    #  )  ! -Y Yx 
H45J8'4T<


 IM#+#+'#+AE#+	#+L7
tr   