o
    "j/                     @   s   d dl Z d dlZd dlZd dlmZ dadai add Z	dd Z
dd Zd	d
 Zdd Zdd ZG dd dejZdd Zdd ZdS )    N)corec                   C      t S N)_g_current_process_mesh r   r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distributed/auto_parallel/process_mesh.pyget_current_process_mesh      r   c                 C   s   t a| a d S r   )r   _g_previous_process_mesh)process_meshr   r   r   set_current_process_mesh"   s   r   c                   C   s   t ad S r   )r
   r   r   r   r   r   reset_current_process_mesh)   s   r   c                 C   s<   d|  d| }|t v rt | }|S tt d }|t |< |S )Nshape , process_ids    )_g_unique_process_mesh_maplen)shapeprocess_idskey	unique_idr   r   r   get_unique_id_for_process_mesh/   s   r   c                 C   s$   d|  d| }|t v sJ t | S )Nr   r   r   )r   r   r   r   r   r   "retrive_unique_id_for_process_mesh;   s   r   c                   C   r   r   r   r   r   r   r   get_unique_process_mesh_mapB   r	   r   c                   @   s   e Zd ZdZdddZedd Zedd Zed	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )ProcessMesha  
    The `ProcessMesh` object describes the Cartesian topology of the used processes.

    Args:
        mesh (list|numpy.array): an n-dimensional array describes the topology
            of the processes.
        dim_names (list, optional): the i-th element of this list gives the name of the
            i-th dimension of the mesh.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import paddle.distributed as dist

            >>> mesh = dist.ProcessMesh([[2, 4, 5], [0, 1, 3]], dim_names=["x", "y"])
            >>> assert mesh.shape == [2, 3]
            >>> assert mesh.process_ids == [2, 4, 5, 0, 1, 3]

    Nc                 C   s  |d u r|d us
J |d usJ t ||}t|ts't|t js'tdt|tr1t |}|d ur>t|ts>td|| _t| jj| _	| j
  | _tdd | jD s^J dt| jdksiJ dt| j}t|t| jks{J d|d urt|t| j	ksJ d	t|| _nd
d tt| j	D | _t| j}t|t| jksJ d| dtj| | j	| j| j ddlm} | }||  ddlm}	 |	d}
|
| j t| j	| j| _ d S )Nz3The mesh must be an instance of list or np.ndarray.z*The dim_names must be an instance of list.c                 s   s    | ]}t |tV  qd S r   )
isinstanceint).0pr   r   r   	<genexpr>s   s    

z'ProcessMesh.__init__.<locals>.<genexpr>z(All elements of the mesh must be integerr   z&All elements of the mesh must be >= 0.z(All elements of the mesh must be unique.z?The length of dims_names must be same as the shape of the mesh.c                 S   s   g | ]}d t | qS )d)strr   ir   r   r   
<listcomp>   s    z(ProcessMesh.__init__.<locals>.<listcomp>zAll dim_names z must be unique.r   get_default_distributed_context)get_process_group)!nparrayZreshaper   listZndarray
ValueError_meshr   _shapeflattentolistZ_process_idsallminsetr   copydeepcopy
_dim_namesranger   r   __init__static.dist_contextr'   Zadd_process_meshZstatic.process_groupr(   Z	add_ranksr   r   
_unique_id)selfmesh	dim_namesr   r   Zunique_process_idsZunique_dim_namesr'   Zdefault_dist_cxtr(   Zpg0r   r   r   r8   ]   sn   







zProcessMesh.__init__c                 C      | j S )z9
        Get the underlying mesh of ProcessMesh.
        )r-   r;   r   r   r   r<         zProcessMesh.meshc                 C   r>   )zD
        Get the underlying dimension names of ProcessMesh.
        )r6   r?   r   r   r   r=      r@   zProcessMesh.dim_namesc                 C   r>   )z
        Get the unique id of ProcessMesh.
        NOTE
        Unique id only take process_ids and shape into account.
        Different ProcessMesh with same process_ids and shape have same unique id.
        )r:   r?   r   r   r   r      s   zProcessMesh.unique_idc                 C   s   t |tr/g }t|D ]\}}t |tr|| j|  q| j| }|jr*t||S t|gS t |trA| j| }| j}t||S | j| }| jdd  }|jrUt||S t|gS )Nr   )	r   tuple	enumeratesliceappendr6   r-   r   r   )r;   indexnew_dim_namesr$   itemnew_meshr   r   r   __getitem__   s&   










zProcessMesh.__getitem__c                 C   s    || j v sJ | j| j | S r   )r6   r.   rE   )r;   dim_namer   r   r   get_dim_size   s   zProcessMesh.get_dim_sizec                    sv    | j v sJ   d| j  gfddtt| j D  } g fdd| j D  }| j|}t||S )Nz is not a valid dim name.c                       g | ]}| kr|qS r   r   r#   )
index_axisr   r   r%          z1ProcessMesh.get_mesh_with_dim.<locals>.<listcomp>c                    rL   r   r   )r   dim)rJ   r   r   r%      rN   )r6   rE   r7   r   r-   Z	transposer   )r;   rJ   Z	new_orderrF   rH   r   )rJ   rM   r   get_mesh_with_dim   s   
zProcessMesh.get_mesh_with_dimc                 C   s:   t |  tj }| }t|j | _t	|j
| _d S r   )r   paddlestaticdefault_main_programcurrent_blockr+   varskeys_old_var_namesr   ops_old_op_size)r;   default_prog	cur_blockr   r   r   	__enter__   s
   
zProcessMesh.__enter__c                 C   sR  ddl m} ddlm} tj }| }t|j	
 }t|j}	ddlm}
 |
 }|D ]<}|| jvrh|j	| }||}|d u rX||j	| }| |j_|jd || q,|jjd u rh| |j_|jd q,t| j|	D ]4}|j| }||}|d u r||}| |j_|jd || qo|jjd u r| |j_|jd qot  d S )Nr   )DistributedOperator)DistributedTensorr&   r   )Zstatic.dist_opr]   Zstatic.dist_tensorr^   rQ   rR   rS   rT   r+   rU   rV   r   rX   r9   r'   rW   Zget_dist_tensor_for_programZ	dist_attrr   Zmark_annotatedZadd_dist_tensor_for_programr7   rY   Zget_dist_op_for_programZadd_dist_op_for_programr   )r;   exc_type	exc_valueexc_tracebackr]   r^   rZ   r[   Znew_var_namesZnew_op_sizer'   Zdefault_dist_ctxnameZtensorZdist_tensoridxopZdist_opr   r   r   __exit__   sH   






zProcessMesh.__exit__c                 C   s<   t | |v r|t |  S tt| j| j}||t | < |S r   )idr   r)   r*   r<   r=   )r;   memoZnew_process_meshr   r   r   __deepcopy__  s
   zProcessMesh.__deepcopy__c                 C   s4   t |ttjfs
dS | j|jks| j|jkrdS dS )NFT)r   r   r   r   r   r;   otherr   r   r   __eq__  s
   zProcessMesh.__eq__c                 C   s   |  | S r   )rk   ri   r   r   r   __ne__  s   zProcessMesh.__ne__c                 C   s   d | j| j| j}|S )Nz%shape {}, process_ids {}, dim_nams {})formatr   r   r=   )r;   r"   r   r   r   __str__"  s   zProcessMesh.__str__)NNNN)__name__
__module____qualname____doc__r8   propertyr<   r=   r   rI   rK   rP   r\   re   rh   rk   rl   rn   r   r   r   r   r   G   s$    
B


	)r   c                 C   s@   | sdS dd }d}| D ]}|||\}}|s dS qt |S )zCCompute the compatible process mesh given a list of process meshes.Nc                 S   s   | d u rd|fS |d u rd| fS | |krd| fS | j |j kr0t| jt|jkr,d| fS d|fS t| j }t|j }||rCd|fS ||rLd| fS dS )NT)FN)r   r   r   r3   issubset)Zpm1Zpm2Zprocess_set1Zprocess_set2r   r   r   '_compute_compatible_process_mesh_of_two.  s"   



zPcompute_compatible_process_mesh.<locals>._compute_compatible_process_mesh_of_two)r4   r5   )Zprocess_mesh_listru   Zcompatible_resultr   
compatibler   r   r   compute_compatible_process_mesh)  s   
rw   c                 C   sL   d}t  }| D ]}|durt |j}||}qt|dkr$tt|}|S )zMerge a list of process meshes.Nr   )r3   r   unionr   r   r+   )Zprocess_meshesZmerged_process_meshZmerged_process_idsr   r   r   r   r   merge_process_meshesL  s   

ry   )r4   numpyr)   rQ   Zpaddle.frameworkr   r
   r   r   r   r   r   r   r   r   r   rw   ry   r   r   r   r   <module>   s"    c#