
    ȅi5                        S SK r S SKrS SKJs  Jr  S SKJr  S SKJ	r	  SSK
JrJr  SSKJr  \ R                  " \5      rS\R$                  S\R&                  S	\4S
 jrS\R$                  S\R&                  SSS	S4S jrS\R$                  S	\R.                  4S jr\	\\\4      " 5       rS\R.                  S	S4S jrS\R.                  S	\4S jrS\R$                  S\SS4S jrS\R$                  4S jr S r!g)    N)is_symbolic)
OrderedSet   )configir)Vxcomm_buffer_typereturnc                 6   [        U 5      n[        U[        R                  5      (       a  gUR	                  5       n[        U[        R
                  5      (       a  g[        U[        R                  5      (       a  [        UR                  5       5      (       d  gg)zY
Check if an input can be realized as a comm buffer of the specified
`comm_buffer_type`.
TF)		_get_data
isinstancer   Loopsget_output_specCommBufferLayoutFlexibleLayoutr   	get_numel)r	   r
   datalayouts       W/home/james-whalen/.local/lib/python3.13/site-packages/torch/_inductor/comm_lowering.pycan_realize_as_comm_bufferr   7   so     Q<D$!!!!#F&"--..&"++,,[AQ5R5R    
group_name,torch.distributed.distributed_c10d.GroupNamec                    U R                  5         [        U 5      n[        U[        R                  5      (       d   eUR                  5       n[        U[        R                  5      (       a  g[        U[        R                  5      (       d  [        SU S35      e[        UR                  5       5      (       a  [        SU S35      e[        R                  " UUUS9Ul        g)z
Realize an input as a comm buffer of the specified `comm_buffer_type`.

Specifically, this realizes the underlying buffer if it's still unrealized
and changes the layout of the buffer to `ir.CommBufferLayout`.
NzOA buffer can only be realized as a comm buffer if it has `FlexibleLayout` (got ).zGA buffer with symbolic shape cannot be converted to a comm buffer (got )r   r
   r   )realizer   r   r   Bufferr   r   r   AssertionErrorr   r   r   )r	   r
   r   bufferr   s        r   realize_as_comm_bufferr!   M   s     IIKq\Ffbii((((##%F&"--..fb//00))/4
 	

 6##%&&""(-
 	

 '')FMr   c                    [        U R                  [        R                  5      (       aW  U R                  R	                  5       n[        U[        R                  [        R
                  45      (       d   eUR                  $ [        U R                  [        R                  5      (       a  U R                  R                  $ [        SU R                   S35      e)Nz\Expect the data attr of a `TensorBox` to be either an `ir.BaseView` or `ir.StorageBox` (got r   )r   r   r   BaseViewunwrap_view
MutableBox
StorageBoxr   )r	   nodes     r   r   r   s   s    !&&"++&&vv!!#$bmm <====yy	AFFBMM	*	*vv{{889xrC
 	
r   c                 |    [         R                  [        [        R                  5      U R                  5       45        g)z
If a non-blocking collective is lowered as a blocking collective, the wait
node in the original graph becomes useless and we can skip the lowering it.
N)_bufs_to_skip_waitaddidr   graphget_namer	   s    r   mark_as_skip_waitr/      s%    
 BqwwK67r   c                 b    [        [        R                  5      U R                  5       4[        ;   $ N)r+   r   r,   r-   r)   r.   s    r   should_skip_waitr2      s!    qwwK&*<<<r   inp	reduce_opc                 l   SSK Jn  U R                  5       U R                  5       R                  -  n[
        R                  R                  =(       ac    U" U5      =(       aT    [        U [        R                  R                  5      =(       a)    US:H  =(       a    U[
        R                  R                  :*  $ )Nr   )is_symm_mem_enabled_for_groupsum)#torch.distributed._symmetric_memoryr6   r   	get_dtypeitemsizer   _collectiveauto_selectr   r   CommBufferTypeSYMM_MEM#one_shot_all_reduce_threshold_bytes)r3   r4   r   r6   inp_sizes        r   $_should_lower_as_one_shot_all_reducerA      s    
 R}}!9!99H&& 	O)*5	O&sB,=,=,F,FG	O 	O **NNNr   c           	      B   [        U [        R                  R                  U5        [        R
                  " [        R                  R                  [        R                  R                  [        R                  R                  R                  R                  U UU5      5      $ r1   )r!   r   r=   r>   pytreetree_map	TensorBoxcreateFallbackKerneltorchopssymm_memone_shot_all_reducedefault)r3   r4   r   s      r   _one_shot_all_reducerM      sn    3 1 1 : :JG??

  II22::		
 r   c            	      @  ^^^^^^^  [         R                  R                  R                    SSKJmJ	mJ
mJmJm  UUU4S jn [         R                  R                  mU " TR                  5      S[        R                  S[        SS	S
[        R                  4UU4S jj5       nU " TR                   5      S[        R                  S[        SS	S
[        R                  4UU4S jj5       nU " TR"                  5      UU4S j5       nU " TR$                  5      U4S j5       nS
[        R&                  4S jmU " TR(                  5      UU4S j5       nU " TR*                  5      U4S j5       nU " TR,                  5      U4S j5       nU " TR.                  5      UU4S j5       nU " TR0                  5      U4S j5       n	U " TR2                  5      U4S j5       n
U " TR4                  5      UU4S j5       nU " TR6                  5      UU4S j5       nU " TR8                  5      U4S j5       nU " [         R                  R:                  R<                  5      U4S j5       nU " TR>                  5      U4S j5       ng! [         a    [
        R                  S5         gf = f)z,
Register lowerings for the comm subsystem.
zRInductor support for distributed collectives depends on building torch.distributedNr   )add_layout_constraintcloneconstrain_to_fx_stridescopy_register_loweringc                 &   > T" U T5        T" U 5      $ r1    )fnrO   rQ   rS   s    r   register_comm_lowering7register_comm_lowerings.<locals>.register_comm_lowering   s    b"9: $$r   r3   r4   r   r   r   c                   > [        XU5      (       a  [        XU5      $ T" U 5      n [        R                  (       aG  U R	                  5         [
        R                  R                  R                  U R                  5       5        [        R                  R                  U 5      n [        R                  R                  TR                  R                   U UU5        U $ r1   )rA   rM   r    reorder_for_compute_comm_overlapr   r   r,   no_fuse_buffer_namesr*   r-   r   ExternKernelrequire_contiguous_AllReduce_Kernelcreate_inplaceall_reduce_rL   )r3   r4   r   c10drP   s      r   _all_reduce,register_comm_lowerings.<locals>._all_reduce   s     0
KK'
CC Cj22 KKMGG((,,S\\^<oo005 	++$$		
 
r   c                   > [        XU5      (       a   T" U [        XU5      5      n[        U5        U $ [        R                  R                  U 5      n [        R                  R                  TR                  R                  U UU5        U $ r1   )
rA   rM   r/   r   r\   r]   r^   r_   r`   rL   )r3   r4   r   retra   rR   s       r   _all_reduce_-register_comm_lowerings.<locals>._all_reduce_   s     0
KK$SZ@C c"J oo005
++$$		
 
r   c                    > U  Vs/ s H  nT" U5      PM     n n[         R                  R                  TR                  R                  U UU5        U $ s  snf r1   r   _CollectiveKernelr_   all_reduce_coalesced_rL   )inputsr4   r   r3   ra   rP   s       r   _all_reduce_coalesced6register_comm_lowerings.<locals>._all_reduce_coalesced  sS    (./%*/
++&&..		
  0s   Ac                 t   > [         R                  R                  TR                  R                  U UU5        U $ r1   ri   )rl   r4   r   ra   s      r   _all_reduce_coalesced_7register_comm_lowerings.<locals>._all_reduce_coalesced_  s6    
++&&..		
 r   c                     [         R                  R                  " X/UQ76 n[        U[         R                  5      (       d   e[         R
                  R                  U5      $ r1   )r   rj   create_out_of_placer   IRNoderE   rF   )kernelrl   argsr'   s       r   _create_out_of_place5register_comm_lowerings.<locals>._create_out_of_place  sI    ##77NN$		****||""4((r   c                 B   > T" TR                   R                  U UU5      $ r1   )all_gather_into_tensorrL   )r3   
group_sizer   rw   ra   s      r   _all_gather_into_tensor8register_comm_lowerings.<locals>._all_gather_into_tensor  s(    #''//	
 	
r   c           	         > [         R                  " [        R                  R                  [        R
                  R                  TR                  R                  U UU5      5      $ r1   )	rC   rD   r   rE   rF   rj   rs    all_gather_into_tensor_coalescedrL   )rl   r{   r   ra   s      r   !_all_gather_into_tensor_coalescedBregister_comm_lowerings.<locals>._all_gather_into_tensor_coalesced&  sM    LL  4455==	
 	
r   c                r   > [         R                  R                  TR                  R                  U UUUS9  U$ N)out)r   rj   r_   all_gather_into_tensor_outrL   )r3   r{   r   r   ra   s       r   _all_gather_into_tensor_out<register_comm_lowerings.<locals>._all_gather_into_tensor_out2  s>    
++++33 	, 	
 
r   c                 D   > T" TR                   R                  U UUU5      $ r1   )reduce_scatter_tensorrL   )r3   r4   r{   r   rw   ra   s       r   _reduce_scatter_tensor7register_comm_lowerings.<locals>._reduce_scatter_tensor=  s+    #&&..
 	
r   c          	      t   > [         R                  R                  TR                  R                  U UUUUS9  U$ r   )r   rj   r_   reduce_scatter_tensor_outrL   )r3   r4   r{   r   r   ra   s        r   _reduce_scatter_tensor_out;register_comm_lowerings.<locals>._reduce_scatter_tensor_outG  sA    
++**22 	, 	
 
r   c           
         > [         R                  " [        R                  R                  [        R
                  R                  TR                  R                  U UUU5      5      $ r1   )	rC   rD   r   rE   rF   rj   rs   reduce_scatter_tensor_coalescedrL   )rl   r4   r{   r   ra   s       r    _reduce_scatter_tensor_coalescedAregister_comm_lowerings.<locals>._reduce_scatter_tensor_coalescedS  sP    LL  4444<<	
 		
r   c                 D   > T" TR                   R                  U UUU5      $ r1   )all_to_all_singlerL   )r3   output_split_sizesinput_split_sizesr   rw   ra   s       r   _all_to_all_single3register_comm_lowerings.<locals>._all_to_all_single`  s+    #""**
 	
r   c                    > T" U 5      n [         R                  R                  TR                  R                  XU5        U $ r1   r   rj   r_   
broadcast_rL   )r3   srcr   ra   rP   s      r   
_broadcast+register_comm_lowerings.<locals>._broadcastj  s8    Cj
++OO##Sz	
 
r   c                 r   > [         R                  R                  TR                  R                  XU5        U $ r1   r   )r3   r   r   ra   s      r   _broadcast_,register_comm_lowerings.<locals>._broadcast_r  s/    
++OO##Sz	
 
r   c                 t   > T" [         R                  R                  R                  R                  U UUU5      $ r1   )rH   rI   _dtensorshard_dim_alltoallrL   )r3   
gather_dim	shard_dimr   rw   s       r   _shard_dim_alltoall4register_comm_lowerings.<locals>._shard_dim_alltoally  s5    #II1199
 	
r   c                    > [        U 5      (       a  U $ [        R                  R                  TR                  R
                  U 5        U $ r1   )r2   r   _WaitKernelcreate_waitwait_tensorrL   )r3   ra   s    r   _wait_tensor-register_comm_lowerings.<locals>._wait_tensor  s8    C  J
""4#3#3#;#;SA
r   ) rH   rI   _c10d_functional
all_reduceAttributeErrorloginfoloweringrO   rP   rQ   rR   rS   r   rE   strr`   all_reduce_coalescedrk   rt   rz   r   r   r   r   r   r   	broadcastr   r   r   r   )rW   rb   rf   rm   rp   r|   r   r   r   r   r   r   r   r   r   r   rw   rO   ra   rP   rQ   rR   rS   s                   @@@@@@@r   register_comm_loweringsr      s   		""-- % 99%%DDOO,\\ C 
	 -< D,,-\\ C 
	 .0 D556 7 D667 8)ryy )
 D778
 9
 DAAB	
 C	
 D;;< = D667
 8
 D::;	 <	 D@@A

 B

 D223
 4
 DNN+ , DOO, - EII..AAB
 C
 D,,- .]   	
 	s   $I; ;JJ)"loggingrH   torch.utils._pytreeutils_pytreerC   torch._inductor.utilsr   torch.utils._ordered_setr    r   r   virtualizedr   	getLogger__name__r   rE   r=   boolr   r!   rt   r   tupleintr   r)   r/   r2   rA   rM   r   rU   r   r   <module>r      s%     $ $ - /   !T	||')'8'8	,#	||#''# ?# 
	#L
 
")) 
   c3h02 8 8t 8=		 =d =	 ?"
bll 
Yr   