
    ^j1                        d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlmZm	Z	m
Z
mZmZ ddlmZ ej                  	 ddej                   dedej"                  d	ej                   d
ej                   f
d       Zej                  	 	 ddej                   dedej"                  dej&                  d	ej                   dee   d
ej                   fd       Zej                  	 	 ddej                   dedej"                  deej&                     dee   d	ej                   d
ej                   fd       Zej                  	 	 	 	 	 ddej,                  ej                   z  dej.                  dej0                  e   deej"                     deej&                     dee   d	ej                   dee   d
ej                   fd       Zej                  	 	 	 	 	 ddej,                  dej0                  e   deej"                     deej&                     dee   dee   ded
e
e
eej,                     gfd       Zej                  	 d dej"                  dededed
ej"                  f
d       Zy)!    N)CallableOptional)Int32Int64Float32Boolean
const_expr   )utilsvalopreduction_bufferinit_valreturnc                    t         j                  j                         t         j                  j                         }}t        j                  |j
                  d         }||z  ||z  }}|dk(  r| |||f<   t         j                  j                          |}	||k  r|||f   }	t         j                  j                  |	|      S )zDreduction_buffer has shape (num_warps / warp_per_row, warps_per_row)r
   r   )cutearchlane_idxwarp_idxsizeshapebarrierwarp_reduction)
r   r   r   r   r   r   warps_per_rowrow_idxcol_idxblock_reduce_vals
             e/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_vendor/quack/reduce.pyblock_reducer      s    
 ++-tyy/A/A/ChHII.44Q78M=0(]2JWG1}-0')*II-+GX,=>99##$4b99    mbar_ptrphasec           	         t         j                  j                         }t         j                  j                         t         j                  j	                         }}|j
                  \  }	\  }
}||
z  ||
z  }}|dk(  rit         j                  j                         5  |	|
z  }t         j                  j                  |||z  |j                  j                  z  dz         ddd       ||k  r1t        j                  | t        j                  ||||ff      ||       t         j                  j                  |||nd       |}t        j                  |
|z  t         j                  j                        }t!        j"                  |      D ]L  }||t         j                  j                  z  z   }|t        j$                  |dg      k  s? |||||f         }N t         j                  j'                  ||      S # 1 sw Y   xY w)zRreduction_buffer has shape (num_warps / warps_per_row, (warps_per_row, cluster_n))r      Npeer_cta_rank_in_clusterr"   r
   mode)r   r   block_idx_in_clusterr   r   r   	elect_onembarrier_arrive_and_expect_txelement_typewidthr   store_shared_remoteelem_pointermbarrier_waitceil_div	WARP_SIZEcutlassrange_constexprr   r   )r   r   r   r!   r   r"   cta_rank_in_clusterr   r   rows_per_blockr   	cluster_nr   r   	num_warpsr   num_iteriidxs                      r   cluster_reducer=      s    ))88:++-tyy/A/A/ChH1A1G1G.N.]I=0(]2JWG1}YY  " 	&6III33I%(8(E(E(K(KKqP	 )!!/'GEX;Y1Z[%-		
 	IIHU5FEAN}}]Y6		8K8KLH$$X. TTYY0000+1#66!"24DWc\4RST 99##$4b99)	 	s   AG77Hc                 \    t        |du       rt        | |||      S t        | |||||      S )zPPerform either block or cluster reduction based on whether mbar_ptr is provided.N)r   r"   r   )r	   r   r=   )r   r   r   r!   r"   r   s         r   block_or_cluster_reducer@   E   s:     (d"#C%5IIc2'7Yabbr    xthreads_per_rowhook_fnc                 ^   t        t        | t        j                              r| j	                  ||d      }n| }t        j
                  j                  t        j                  t        j
                  j                  t        | j                  t        k(        rt        j                  j                  nt        t        j
                  j                  t         t        j
                  j"                  t        j$                  i|   }	t        j                  j'                  ||	t!        |t        j                  j(                              }t        |du      r |        t        |du      rF|j*                  d   \  }
}|dk(  s	|J d       t        |
dkD  xs |dkD        rt-        ||	||||      }|S )zXreduction_buffer must have shape (num_warps / warps_per_row, (warps_per_row, cluster_n))r   r   reduction_profilethreads_in_groupNr
   /mbar_ptr must be provided for cluster reductionr?   )r	   
isinstancer   	TensorSSAreduceReductionOpADDoperatoraddMAXdtyper   r   fmaxmaxMINminMULmulr   r3   r   r@   )rA   r   rB   r   r!   r"   r   rC   r   warp_opr   r8   s               r   
row_reducerZ   U   sh    *Q/0hhrHhBhll
177g;M0NdiinnTWchll	
 	
G ))
"
"_dii.A.AB # C
 '%&	"$./#3#9#9!#< yA~!5 	
=	
5 ma'89q=9)W.PXC Jr    return_exp_xc           	         | j                   t        k(  sJ d       	 t        j                  j	                  | j                  t        j                  j                  t        j                   d      t        j                  j                  t        |t        j                  j                              }t        j                  t        j                        }t        j                  j                  | |z  ||z  z
  d      }	t        j                  j	                  |	j                  t        j                  j                   dd      t"        j$                  t        |t        j                  j                              }
t'        |d u      r |        t'        |d u      r|j(                  \  }\  }}|dk(  s	|J d	       t'        |dkD  xs |dkD        r|j*                  t,        k(  sJ d
       t        j                  j/                         t        j                  j1                         }}||z  ||z  }}t'        |d u       r6|dk(  rt3        j4                  ||
      |||f<   t        j                  j7                          t        j                   }d}
||k  rt3        j8                  |||f         \  }}
t        j                  j	                  |t        j                  j                        }|
t        j                  j;                  ||z
  d      z  }
t        j                  j	                  |
t"        j$                        }
t'        |      r'|	t        j                  j;                  ||z
  d      z  }	|}n9t        j                  j=                         }|dk(  rit        j                  j?                         5  ||z  }t        j                  jA                  |||z  |j*                  jB                  z  dz         d d d        ||k  rEt3        jD                  t3        j4                  ||
      t3        jF                  ||||ff      ||       t        j                  jI                  |||nd       t        jJ                  ||z  t        j                  j                        }t        jL                  |t              }|jO                  t        j                          t        jL                  |t              }|jO                  d       tQ        jR                  |      D ]a  }||t        j                  j                  z  z   }|t        jT                  |dg      k  s?t3        j8                  |||f         \  ||<   ||<   c |jW                         j                  t        j                  j                  t        j                   d      }t        j                  j	                  |t        j                  j                        }d}
tQ        jR                  |      D ]2  }|
||   t        j                  j;                  ||   |z
  d      z  z  }
4 t        j                  j	                  |
t"        j$                        }
t'        |      r'|	t        j                  j;                  ||z
  d      z  }	|}||
t'        |      r|	fS d fS # 1 sw Y   xY w)Nzx must be of type Float32r   rE   rG   T)fastmath        r
   rI   z+reduction_buffer must be of type cute.Int64r$   r%   r'   r(   ),rR   r   r   r   r   rL   rM   rQ   infrS   rV   r3   mathlog2eexp2rN   rO   rP   r	   r   r-   r   r   r   r   f32x2_to_i64r   i64_to_f32x2expr*   r+   r,   r.   r/   r0   r1   r2   make_rmem_tensorfillr4   r5   r   load)rA   rB   r   r!   rC   r"   r[   max_xlog2_eexp_x	sum_exp_xr7   r   r8   r   r   r   r   max_x_single_warpmax_x_finalr6   r9   r:   sum_exp_x_single_warpr;   r<   s                             r   online_softmax_reducerq   ~   s    77g:::eII$$	!!%%PQR		_dii.A.AB % E
 YYtvvFIINN1v:84NHE		((T%%))C1M_dii.A.AB ) I
 '%&	"$./5E5K5K22	A~!5 	
=	
5 ma'89q=9#00E9 =9 "&!3!3!5tyy7I7I7KhH'=8(]:RWG(d*+q=9>9K9KES\9]$Wg%56		!!#%,[[L!	m+383E3E(():;40%y #ii667H$))..YTYY]]+<{+JUY]ZZ	 II44YM	l+TYY]]5;+>]NNE#&*ii&D&D&F#q=,,. $2]$B			??$%	14D4Q4Q4W4WW[\\ i'--**5)<**,wBU8V.W !19 		''ARXY'Z==)BDIIDWDWX$($9$9(G$L!!&&|4(,(=(=h(P%%**3/ 00: A"Q)<)<%<<CTYY'7qcBBINI[I[,Wc\:JF)!,.CA.F 0446==$$((GKK<ST >  #ii66{DIINNS	 00: A!6q!9DIIMM)!,{:T =J = " I !II44YM	l+TYY]]5;+>]NNE#)z,'?eJJTJJQ s   7A[[Xelem_per_lanesubwarp_size	warp_sizec           
      @   |dk\  r)|dk  r$|dt        t        j                  |            z  k(  sJ |dk  r,||z  dk(  r$|dt        t        j                  |            z  k(  sJ t        j                  j                         |z  }t        j                  | t        j                  |            } t        j                  | dg      }|d|z  k  sJ t        j                  t        t        j                  |            t        t        j                  ||z              d      D ]f  }t        j                  t        j                  |       d      D ]7  }t        j                  j                  | |   d|z  |z        }| |   |z   | |<   9 h t        j                  t        t        j                  t        j                  | dg                  dz
  dd      D ]  }	d|	z  }
t        j                  |
d      D ]  }| d	|f   }| d	||
z   f   }t        ||
z         }t        j                  t        j                  |      d      D ]P  }||   ||   }}|r|n|||<   |r|n|||<   t        j                  j                  ||   |
|z        }||   |z   ||<   R   | d
   S )aX  
    For warp reduction, we use Swap Shuffle
    The normal way to reduction among threads:
    use shuffle to let *** the first half of threads *** have *** whole data *** from the second half of threads.
    After each step of reduction, a half of threads won't work in the following steps.
    That is, as the reduction progresses, the efficiency of shuffle & reduction instructions gradually change from 1/2, 1/4 to 1/32 (the worst case).
    To overcome this shortcoming, for a NxN matrix to be reduced among N threads as a 1XN vectors,
    we use swap & shuffle aiming to let *** each half of threads *** have *** a half of data *** from the other half of threads.
    After reduction, each half of threads should deal with a (N/2)x(N/2) sub-matrix independently in the following step.
    We can recursively do this until the problem size is 1.
    r
       r   r(   T)unroll_full)offsetN)Nr   )intr`   ra   r   r   r   logical_dividemake_layoutr   r4   rangeshuffle_sync_bflyr5   r   )rr   rs   rt   ru   r   numvecr;   vshfl_vallogmmrfrg_Afrg_Bshould_swaploweruppers                    r   sum_swap_shuffler      s     	BATYY|%<!===	>
 	R$)c$))I"6777	8 yy!!#|3HAt//>?AYYqs#FR<'''' ]]DIIfDIIi<.G$H IW[ # tyy|> 	#Ayy221Q4a<@W2XHQ4(?AaD	## ''DIIdii6L,M(NQR(RTVXZ[ /Iqd3 	/AdAgJEdAEkNE%hl33K]]499U#3F 	/$Qxqu$/5Ua$/5Ua  9966uQxLHX6Y 8h.a	/	//" W:r    )r^   )r^   N)Nr^   )NNNr^   N)NNNNF)r
   r
   rw   )r`   rO   typingr   r   r4   cutlass.cuter   r   r   r   r   r	    r   jitNumericTensorr   Pointerr=   r@   rK   rM   	Constexprr{   rZ   boolrq   r    r    r   <module>r      sE     %   > >  ]`:	:#:7;{{:NRll:	\\: 
:   !!":	":": kk": ll	":
 ll": E?": 
\\": 
":J  " c	cc kkc t||$	c
 E?c llc 
\\c 
c 
 /3'+! "&%~~$%% &&s+% t{{+	%
 t||$% E?% ll% h% 
\\% 
%P  /3'+"&!_K~~_K&&s+_K t{{+_K t||$	_K
 h_K E?_K _K w01_K 
_KD TV5{{5#&5:=5NQ5	[[5 
5r    