o
    ZhS.                     @   s  U d dl Z d dlmZ d dlZd dlmZmZmZmZ d dl	m
Z
 ejj ejjZejjZejjZejjZe Zee ed< defddZd5d
dZi ddd ddd ddd ddd ddd ddd ddd ddd ddd dd d d!d"d d#d$d d%d&d d'd(d d)d*d d+d,d Ze sejd-d.Ze D ]\ZZe eed/ qej!j"ej#j"ej$j"ej%j"ej&j"ej'j"ej(j"ej)j"ej*j"ej+j"ej,j"ej-j"ej.j"ej/j"ej0j"ej1j"hZ2eej3j4 ed0< ej5j"ej6j"ej7j"ej8j"ej9j"ej9j"ej:j"ej;j"ej<j"ej=j"ej>j"ej?j"ej7j"ej@j"ej8j"ej!j"ejAj"hZBeej3j4 ed1< ej0j"ej1j"ej:j"hZCeej3j4 ed2< eDeBe2ZEG d3d4 d4ZFdS )6    N)Any)_resolve_process_groupFakeWorkProcessGroupWork)tree_map_onlyused_idsreturnc                  C   s&   	 t dd} | tvrt|  | S q)NT   i ʚ;)randomrandintr   add)Znew_id r   X/var/www/auris/lib/python3.10/site-packages/torch/distributed/_tools/fake_collectives.pygenerate_unique_id   s   
r   Tc                 C   s*   t  }t |_| }|r| d |fS |S )Nr   )r   r   Zseq_idZboxed)argsreturn_first_argZworkZfakework_script_objr   r   r   create_fakework    s   r   
broadcast_c                  G      t | S Nr   r   r   r   r   <lambda>0       r   
allreduce_c                  G   r   r   r   r   r   r   r   r   1   r   
allgather_c                  G   r   r   r   r   r   r   r   r   2   r   _allgather_base_c                  G   r   r   r   r   r   r   r   r   3   r   reduce_scatter_c                  G   r   r   r   r   r   r   r   r   4   r   _reduce_scatter_base_c                  G   r   r   r   r   r   r   r   r   5   r   reduce_c                  G      t | ddS NF)r   r   r   r   r   r   r   6       gather_c                  G   r!   r"   r   r   r   r   r   r   7   r#   scatter_c                  G   r   r   r   r   r   r   r   r   8   r   	alltoall_c                  G   r   r   r   r   r   r   r   r   9   r   alltoall_base_c                  G   r!   r"   r   r   r   r   r   r   :   r#   barrierc                  G   r!   r"   r   r   r   r   r   r   ;   r#   monitored_barrier_c                  G   s   d S r   r   r   r   r   r   r   <   s    sendc                  G   r!   r"   r   r   r   r   r   r   =   r#   recv_c                  G   r!   r"   r   r   r   r   r   r   >   r#   recv_any_source_c                  G   r!   r"   r   r   r   r   r   r   ?   r#   c10dZIMPLZMetanon_functional_collectivesfunctional_collectivessync_opsc                
   @   s8  e Zd Zejjejjejjejjej	jej
jejjhZejjejjejjejjejjejjejjejjhZejjejjejjejjejjejjejjejjejjejjh
Z ej!jej"jej!jej#jej#je$j%jhZ&ejjejjejjejjejjejjejjejjhZ'ejjej	jej
jejjejjejjejjhZ(ejjejjej	jej
jejjejjejjejjejjejjh
Z)ejjejjhZ*ejjejjhZ+ejjejjejjejjejjej!jej!jhZ,ej#jej#je$j%jhZ-ejjej"jhZ.e/de0de1fddZ2e/de3fddZ4e/de1fddZ5e/de6fd	d
Z7dS )CollectiveOpargr	   c                    s.   d dt jddf fdd}tt j||   S )z?Calculate total memory consumed by the tensors in the argument.r   tr	   Nc                    s    |    7  d S r   )untyped_storagenbytes)r3   Ztotal_memoryr   r   	sum_bytes   s   z+CollectiveOp.sum_tensors.<locals>.sum_bytes)torchZTensorr   )r2   r7   r   r6   r   sum_tensors   s   zCollectiveOp.sum_tensorsc                 C   sp   | t jv rt|d S | t jv rt|d S | t jv r#t|d S | t jv r.t|d S td|  dt	 )zKRetrieve the process group for collective operations, except `wait_tensor`.r
         Func  not found in )
r1   PG_ARG_1r   unboxPG_ARG_2PG_ARG_3r   PG_ARG_4	TypeErrorcollective_ops)funcr   r   r   r   get_process_group   s   



zCollectiveOp.get_process_groupc                 C   s>  | t jv rt |d S | t jv rt |d S | t jv r#|  S | t jv r0|d   S | tj	j
kr>|d   S | tjj
krStt |d t |d S | tjj
krjt|d   |d   S | tjj
krx|d   S | t jv rt |S | t jv r|d   |   S td|  dt )zdCompute the communication tensor size, except for `wait_tensor`, `barrier`, and `monitored_barrier`.r   r
   zUnknown function: z in )r1   COMM_TENSOR_ARG_0r9   COMM_TENSOR_ARG_1COMM_TENSOR_ARG_RESr4   r5   "COMM_TENSOR_SINGLE_UNTYPED_STORAGEr-   r   defaultr&   maxr'   _c10d_functionalall_gather_into_tensor_outCOMM_TENSOR_RES_SUMCOMM_TENSOR_ARG_0_AND_RESrC   rD   )rE   resr   kwargsr   r   r   get_comm_tensor_size  s2   






z!CollectiveOp.get_comm_tensor_sizec                 C   s@   | t jv r
t|S | t jv rt|d S td|  dt )Nr
   r<   r=   )r1   WKr   r?   WK_ARG_1rC   rD   )rE   rR   r   r   r   get_work-  s
   


zCollectiveOp.get_workN)8__name__
__module____qualname__r-   r   rL   r   r    r*   r+   r,   r(   r>   r   r   r   r   r$   r%   r&   r'   r@   rN   	broadcast
all_reduceall_reduce_all_reduce_coalescedall_reduce_coalesced_all_gather_into_tensorrO   _c10d_functional_autograd all_gather_into_tensor_coalescedrA   reduce_scatter_tensorreduce_scatter_tensor_coalescedall_to_all_single_dtensorshard_dim_alltoallrB   rV   rU   rH   rI   rJ   rK   rQ   rP   staticmethodr   intr9   r   rF   rT   r   rW   r   r   r   r   r1   ~   s    
r1   )T)Gr   typingr   r8   Ztorch._C._distributed_c10dr   r   r   r   Ztorch.utils._pytreer   distributedZbatch_isend_irecvopsr-   rN   ra   rf   setr   ri   __annotations__r   r   Z_META_FUNCTIONSZ_running_with_deployZlibraryLibraryZlib_implitemsopZ	meta_funcimplr   rL   r   r    r*   r+   r,   r   r   r   r   r$   r%   r&   r'   r(   r)   r.   Z_opsZ
OpOverloadr[   r\   r`   rc   re   Zwait_tensorr]   r^   r_   rO   rb   rd   rg   r/   r0   unionrD   r1   r   r   r   r   <module>   s   
 
		
