o
    "j                      @   sj   d dl mZ d dlmZ G dd dZG dd deZG dd deZG d	d
 d
eZG dd deZ	dS )    N)RNGStatesTrackerc                   @   sP   e Zd ZdZdd Zdd Zdd Zedd	 Zed
d Z	dd Z
dd ZdS )StrategyGroupBasea  
    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                 C   sF   t  sJ dt|sJ dt  | _|| _|  | _t | _	dS )z5
        Initialize the communication group.
        z6The global communication group need to be initialized.z#The list_of_ranks can not be empty.N)
distZis_initializedlenZget_rank_rank_list_of_ranks_create_group_groupr   random_states_trackerselfZlist_of_ranks r   m/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distributed/fleet/base/strategy_group.py__init__.   s   

zStrategyGroupBase.__init__c                 C   s   | j || dS )z3
        Add random seed for current rank.
        N)r
   add)r   nameseedr   r   r   add_random_seed;   s   z!StrategyGroupBase.add_random_seedc                 C      | j S )z0
        Get the random states tracker.
        )r
   r   r   r   r   get_random_states_trackerA   s   z+StrategyGroupBase.get_random_states_trackerc                    sD   g  | j D ]	} t| qt fdd D }|r  d S  S )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   s    | ]	}| d  kV  qdS )r   Nr   ).0
world_sizeZworld_size_listr   r   	<genexpr>R   s    
z/StrategyGroupBase.world_size.<locals>.<genexpr>r   )r   appendr   all)r   ranksZis_valuer   r   r   r   G   s   
zStrategyGroupBase.world_sizec                 C   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   r   r   r   groupW      zStrategyGroupBase.groupc                 C   s\   g | _ | jD ]}tj|d}| j|v r| j | q| j sd S t| j dkr+| j d S | j S )Nr      r   )list_of_groupr   r   	new_groupr   r   r   )r   r   r   r   r   r   r   a   s   

zStrategyGroupBase._create_groupc                 C   sT   d| j  d}| js|d S tt| jD ]}|d| dt| j|  d7 }q|S )Nzseed: z; z	No group.zGroup[z]: )Z_seedr"   ranger   str)r   Z	debug_strir   r   r   __repr__q   s   "zStrategyGroupBase.__repr__N)__name__
__module____qualname____doc__r   r   r   propertyr   r   r   r'   r   r   r   r   r      s    

	r   c                           e Zd ZdZ fddZ  ZS )DPGroupa!  
    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  | t| jtrJ d| j dd S )NRank z belongs to multi dp groupssuperr   
isinstancer   listr   r   	__class__r   r   r         zDPGroup.__init__r(   r)   r*   r+   r   __classcell__r   r   r5   r   r.   z       r.   c                       r-   )MPGroupa#  
    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                    r/   )Nr0   z belongs to multi mp groupsr1   r   r5   r   r   r      r7   zMPGroup.__init__r8   r   r   r5   r   r;      r:   r;   c                       r-   )ShardingGroupa)  
    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                    r/   )Nr0   z! belongs to multi sharding groupsr1   r   r5   r   r   r      r7   zShardingGroup.__init__r8   r   r   r5   r   r<      r:   r<   c                       sL   e Zd ZdZ fddZedd Zedd Zedd	 Zd
d Z	  Z
S )PPGroupa)  
    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                    sh   t  | t| jtrJ d| j dd | _d | _d | _d | _	d | _
d | _| jdkr2|   d S d S )Nr0   z belongs to multi pp groupsr!   )r2   r   r3   r   r4   r   _send_next_group_send_prev_group_recv_next_group_recv_prev_group_rank_of_next_stage_rank_of_prev_stager   _create_p2p_groupr   r5   r   r   r      s   
zPPGroup.__init__c                 C   r   )z
        Rank of the previous pp stage.

        Returns:
            The global rank of previous pp stage. `None` if without previous.
        )rC   r   r   r   r   rank_of_prev_stage   r   zPPGroup.rank_of_prev_stagec                 C   r   )z
        Rank of the next pp stage.

        Returns:
            The global rank of next pp stage. `None` if without next.
        )rB   r   r   r   r   rank_of_next_stage   r   zPPGroup.rank_of_next_stagec                 C   s   | j | j| j| jfS )z
        Communication subgroup in order to switch data with previous and next stage.

        Returns:
            Four subgroups including send/recv to/from prev/next.
        )r>   r?   r@   rA   r   r   r   r   
p2p_groups   s
   	zPPGroup.p2p_groupsc           	      C   s   | j }| jD ]X}t|D ]Q\}}||d |  }||d |  }| j|kr+|| _|| _tj||gd}| j|kr<|| _n| j|krD|| _	tj||gd}| j|krU|| _
q| j|kr]|| _qq| jrk| j
rk| jrk| j	stJ d| j dd S )Nr!   r    z/Error occurs while creating p2p group for rank .)r   r   	enumerater   rB   rC   r   r#   r>   rA   r?   r@   )	r   Zdegreer   idxrankZ	next_rankZ	prev_rankZ
next_groupZ
prev_groupr   r   r   rD      s<   





zPPGroup._create_p2p_group)r(   r)   r*   r+   r   r,   rE   rF   rG   rD   r9   r   r   r5   r   r=      s    
	
	
r=   )
Zpaddle.distributeddistributedr   Z#paddle.distributed.fleet.layers.mpur   r   r.   r;   r<   r=   r   r   r   r   <module>   s   f