
    Αi                          S SK Jr  S SKJr   " S S5      r " S S\5      r " S S\5      r " S	 S
\5      r " S S\5      r	g)    N)RNGStatesTrackerc                   V    \ rS rSrSrS rS rS r\S 5       r	\S 5       r
S rS	 rS
rg)StrategyGroupBase   a  
The base class of communication group with distributed strategy.

Args:
    list_of_ranks: A 2D-array, such as `[[0, 1, 2, 3], [4, 5, 6, 7]]`. Ranks in sublist represents
they are in the same communication group.

Returns:
    The instance of strategy group.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)
        >>> import paddle.distributed as dist
        >>> from paddle.distributed.fleet.base.strategy_group import StrategyGroupBase

        >>> dist.init_parallel_env()
        >>> strategy_group = dist.fleet.base.strategy_group.StrategyGroupBase([[0, 1], [2, 3]])
        >>> print(strategy_group.world_size)
        2


c                     [         R                  " 5       (       d   S5       e[        U5      (       d   S5       e[         R                  " 5       U l        Xl        U R                  5       U l        [        5       U l	        g)z%
Initialize the communication group.
z6The global communication group need to be initialized.z#The list_of_ranks can not be empty.N)
distis_initializedlenget_rank_rank_list_of_ranks_create_group_groupr   random_states_tracker)selflist_of_rankss     l/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/fleet/base/strategy_group.py__init__StrategyGroupBase.__init__.   sj     ""$$ 	
D	
$ =!!H#HH!]]_
+((*%5%7"    c                 :    U R                   R                  X5        g)z#
Add random seed for current rank.
N)r   add)r   nameseeds      r   add_random_seed!StrategyGroupBase.add_random_seed;   s     	""&&t2r   c                     U R                   $ )z 
Get the random states tracker.
)r   r   s    r   get_random_states_tracker+StrategyGroupBase.get_random_states_trackerA   s     )))r   c                    ^ / mU R                    H  nTR                  [        U5      5        M     [        U4S jT 5       5      nU(       a  TS   $ T$ )z
The world size of communication group.

Returns:
    Integer if the world_size of each group are equal, or a list of world_size if they are not equal.
c              3   2   >#    U  H  oTS    :H  v   M     g7f)r   N ).0
world_sizeworld_size_lists     r   	<genexpr>/StrategyGroupBase.world_size.<locals>.<genexpr>R   s      
?N/!,,s   r   )r   appendr
   all)r   ranksis_valuer&   s      @r   r%   StrategyGroupBase.world_sizeG   sW     ((E""3u:. ) 
?N
 
 &.q!B?Br   c                     U R                   $ )z
The communication group which current rank belongs to.

Returns:
    Group if current rank only belong to single communication group, or a list of Group if it belongs many.
)r   r   s    r   groupStrategyGroupBase.groupW   s     {{r   c                 D   / U l         U R                   HD  n[        R                  " US9nU R                  U;   d  M)  U R                   R                  U5        MF     U R                   (       d  g [        U R                   5      S:X  a  U R                   S   $ U R                   $ )Nr+      r   )list_of_groupr   r   	new_groupr   r)   r
   )r   r+   r/   s      r   r   StrategyGroupBase._create_groupa   s    ((ENN/EzzU"""))%0 )
 !! t))*a/ ""1% ''r   c                     SU R                    S3nU R                  (       d  US-   $ [        [        U R                  5      5       H  nUSU SU R                  U    S3-  nM     U$ )Nzseed: z; z	No group.zGroup[z]: )_seedr4   ranger
   )r   	debug_stris      r   __repr__StrategyGroupBase.__repr__q   sm    TZZL+	!!{**s4--./A6!C(:(:1(='>bAAI 0r   )r   r   r   r4   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   propertyr%   r/   r   r<   __static_attributes__r#   r   r   r   r      sJ    283* C C   r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )DPGroupz   a  
The communication group strategy for data parallel.

Args:
    list_of_ranks: A 2D-array, such as `[[0, 1, 2, 3], [4, 5, 6, 7]]`. Ranks in sublist represents
they are in the same communication group.

Returns:
    The instance of data parallel strategy group.
c                    > [         TU ]  U5        [        U R                  [        5      (       a   SU R
                   S35       eg )NRank z belongs to multi dp groupssuperr   
isinstancer/   listr   r   r   	__class__s     r   r   DPGroup.__init__   B    'djj$// 	
DJJ<:;	
//r   r#   r>   r?   r@   rA   rB   r   rD   __classcell__rO   s   @r   rF   rF   z       	
 
r   rF   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )MPGroup   a  
The communication group strategy for model parallel.

Args:
    list_of_ranks: A 2D-array, such as `[[0, 1, 2, 3], [4, 5, 6, 7]]`. Ranks in sublist represents
they are in the same communication group.

Returns:
    The instance of model parallel strategy group.
c                    > [         TU ]  U5        [        U R                  [        5      (       a   SU R
                   S35       eg )NrI   z belongs to multi mp groupsrJ   rN   s     r   r   MPGroup.__init__   rQ   r   r#   rR   rT   s   @r   rW   rW      rU   r   rW   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ShardingGroup   a  
The communication group strategy for sharding parallel.

Args:
    list_of_ranks: A 2D-array, such as `[[0, 1, 2, 3], [4, 5, 6, 7]]`. Ranks in sublist represents
they are in the same communication group.

Returns:
    The instance of sharding parallel strategy group.
c                    > [         TU ]  U5        [        U R                  [        5      (       a   SU R
                   S35       eg )NrI   z! belongs to multi sharding groupsrJ   rN   s     r   r   ShardingGroup.__init__   sB    'djj$// 	
DJJ<@A	
//r   r#   rR   rT   s   @r   r\   r\      rU   r   r\   c                   b   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r\S 5       r	S r
SrU =r$ )	PPGroup   a  
The communication group strategy for pipeline parallel.

Args:
    list_of_ranks: A 2D-array, such as `[[0, 1, 2, 3], [4, 5, 6, 7]]`. Ranks in sublist represents
they are in the same communication group.

Returns:
    The instance of pipeline parallel strategy group.
c                 "  > [         TU ]  U5        [        U R                  [        5      (       a   SU R
                   S35       eS U l        S U l        S U l        S U l	        S U l
        S U l        U R                  S:  a  U R                  5         g g )NrI   z belongs to multi pp groupsr3   )rK   r   rL   r/   rM   r   _send_next_group_send_prev_group_recv_next_group_recv_prev_group_rank_of_next_stage_rank_of_prev_stager%   _create_p2p_grouprN   s     r   r   PPGroup.__init__   s    'djj$// 	
DJJ<:;	
/ !% $ $ $#' #' ??Q""$ r   c                     U R                   $ )zp
Rank of the previous pp stage.

Returns:
    The global rank of previous pp stage. `None` if without previous.
)ri   r   s    r   rank_of_prev_stagePPGroup.rank_of_prev_stage        '''r   c                     U R                   $ )zd
Rank of the next pp stage.

Returns:
    The global rank of next pp stage. `None` if without next.
)rh   r   s    r   rank_of_next_stagePPGroup.rank_of_next_stage   ro   r   c                 ^    U R                   U R                  U R                  U R                  4$ )z
Communication subgroup in order to switch data with previous and next stage.

Returns:
    Four subgroups including send/recv to/from prev/next.
)rd   re   rf   rg   r   s    r   
p2p_groupsPPGroup.p2p_groups   s2     !!!!!!!!	
 	
r   c                    U R                   nU R                   H  n[        U5       H  u  p4X#S-   U-     nX#S-
  U-     nU R                  U:X  a  XPl        X`l        [        R                  " XE/S9nU R                  U:X  a  Xpl        OU R                  U:X  a  Xpl	        [        R                  " Xd/S9nU R                  U:X  a  Xl
        M  U R                  U:X  d  M  Xl        M     M     U R                  (       a3  U R                  (       a"  U R                  (       a  U R                  (       d   SU R                   S35       eg )Nr3   r2   z/Error occurs while creating p2p group for rank .)r%   r   	enumerater   rh   ri   r   r5   rd   rg   re   rf   )	r   degreer+   idxrank	next_rank	prev_rank
next_group
prev_groups	            r   rj   PPGroup._create_p2p_group   s   ((E&u-	!7f"45	!7f"45	::%/8,/8,!^^42CD
::%,6)ZZ9,,6)!^^92CD
::%,6)ZZ9,,6)' . ). !!%%%%%%		K
 =TZZLJ	K& &r   )rh   ri   rf   rg   rd   re   )r>   r?   r@   rA   rB   r   rC   rm   rq   rt   rj   rD   rS   rT   s   @r   ra   ra      sU    	%  ( ( ( ( 
 
K Kr   ra   )
paddle.distributeddistributedr   #paddle.distributed.fleet.layers.mpur   r   rF   rW   r\   ra   r#   r   r   <module>r      sR     " @c cL
 
&
 
&
% 
&\K \Kr   