
    h?                       S SK Jr  SSKJr  SSKJr  SSKJr  SDS jrSDS jr\R                  \S	 5       5       r
\R                  \\R                  " S
5      S 5       5       5       r\R                  \\R                  " S5      SES j5       5       5       r\R                  \SFS j5       5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\R                  \\R,                  " SSSS9SGS j5       5       5       r\R                  \\R,                  " SSS9SHS j5       5       5       r\S  5       r\S! 5       r\S" 5       r\S# 5       r\R                  \\R,                  " S$SSS9SGS% j5       5       5       r\R                  \\R,                  " S&SS9SHS' j5       5       5       r\S( 5       rSIS* jr \R                  \\R,                  " S+S)S,9SJSKS- jj5       5       5       r!\S. 5       r"\R                  \\R,                  " S/5      SLS0 j5       5       5       r#\S1 5       r$\R                  \\R,                  " S25      SFS3 j5       5       5       r%\R                  \\RL                  " S4S)S,9SMSKS5 jj5       5       5       r'\S6 5       r(\R                  \\RL                  " S75      SNS8 j5       5       5       r)\SOS9 j5       r*\SDS: j5       r+\SPS; j5       r,\SQS< j5       r-\SS\R\                  4SRS= jj5       r/\S\R\                  4SSS> jj5       r0\STSUS? jj5       r1\S\R\                  4SSS@ jj5       r2SA r3\R                  \STSB j5       5       r4\SC 5       r5g)V    )annotations   )jit   )core)mathc                    Sn[         R                  " U 5      R                  nUS:  a  US-  nUS-  nUS:  a  M  [         R                  " U5      $ )Nr   r   )r   	constexprvalue)ilog2ns      R/var/www/fran/franai/venv/lib/python3.13/site-packages/triton/language/standard.py_log2r   
   sN    DqA
a%	a	 a% >>$    c                n    U R                   n[        R                  " XS-
  -  S:H  =(       a    US:g  5      $ Nr   r   )r   r   r
   )r   r   s     r   _is_power_of_twor      s.    	A>>1A;1,7a88r   c                    X-   S-
  U-  $ )z
Computes the ceiling division of :code:`x` by :code:`div`

:param x: the input number
:type x: Block
:param div: the divisor
:type div: Block
r    )xdivs     r   cdivr      s     GaKCr   sigmoidc                <    SS[         R                  " U * 5      -   -  $ )Nr   )r   exp)r   s    r   r   r   +   s     DHHaRL !!r   softmaxNc                    Uc  SnOUnU [        XUS9-
  n[        R                  " U5      n[        XdUS9n[        R                  " XgU5      $ )Nr   	keep_dims)maxr   r   sumfdiv)r   dimr    ieee_rounding_dimznumdens           r   r   r   2   sN     { "	C9--A
((1+C
c9
-C99S}--r   c                B    [         R                  " X R                  /US9$ )z^
Returns a contiguous flattened view of :code:`x`.

:param x: the input tensor
:type x: Block
)can_reorder)r   reshapenumel)r   r+   s     r   ravelr.   @   s     <<GG9+>>r   c                ~    X-  U-   nXC-  nXV-  nXt-  n[         R                  " X(-
  U5      nXV-  nXU-  -   n	XT-  n
X4$ )a  
Transforms the indices of a row-major `size_i * size_j` matrix into
the indices of a column-major matrix for each group of `size_g` rows.

For example, for :code:`size_i = size_j = 4` and :code:`size_g = 2`, it will
transform ::

    [[0 , 1 , 2 , 3 ],
     [4 , 5 , 6 , 7 ],
     [8 , 9 , 10, 11],
     [12, 13, 14, 15]]

into ::

    [[0, 2,  4 , 6 ],
     [1, 3,  5 , 7 ],
     [8, 10, 12, 14],
     [9, 11, 13, 15]]
r   minimum)r   jsize_isize_jsize_gijsize_gjgroup_idoff_inew_inew_js              r   	swizzle2dr<   L   s[    , 
aB oG}HE\\&.&1F	BKELE<r   c                2    [         R                  " U SU5      $ )a  
Returns a tensor filled with the scalar value 0 for the given :code:`shape` and :code:`dtype`.

:param shape: Shape of the new array, e.g., (8, 16) or (8, )
:type shape: tuple of ints
:param dtype: Data-type of the new array, e.g., :code:`tl.float16`
:type dtype: DType
r   )r   full)shapedtypes     r   zerosrA   t   s     99UAu%%r   c                B    [        U R                  U R                  5      $ )z{
Returns a tensor of zeros with the same shape and type as a given tensor.

:param input: input tensor
:type input: Tensor
)rA   r?   r@   )inputs    r   
zeros_likerD      s     ekk**r   c                    U(       a  X:H  =(       a    X:  nOSnX:  =(       d    Un[         R                  " X`U5      n[         R                  " XaU5      nXx4$ NFr   where)	value1index1value2index2tie_break_lefttiegtv_reti_rets	            r   _argmax_combinerR      sM    26?		CBJJr6*EJJr6*E<r   c                    [        XX#S5      $ NTrR   rI   rJ   rK   rL   s       r   _argmax_combine_tie_break_leftrW          664@@r   c                    [        XX#S5      $ rF   rU   rV   s       r   _argmax_combine_tie_break_fastrZ          665AAr   c                .    [         R                  " X5      $ N)r   maximumabs     r   _elementwise_maxrb          <<r   r^   return_indicesreturn_indices_tie_break_left)return_indices_argtie_break_argc                   [         R                  " U 5      n U(       a;  U(       a  [         R                  " X[        US9$ [         R                  " X[        US9$ [         R
                  " U R                  R                  5      [         R
                  " S5      :  a  [         R
                  " U R                  R                  5       5      (       a   U R                  [         R                  5      n OEU R                  R                  5       (       d   S5       eU R                  [         R                  5      n [         R                  " X[        US9$ Nr       z"Expecting input to be integer type)r   _promote_bfloat16_to_float32_reduce_with_indicesrW   rZ   r
   r@   primitive_bitwidthis_floatingtofloat32is_intint32reducerb   rC   axisrd   re   r    s        r   r!   r!      s    
 --e4E(,,U:Xdmnn,,U:Xdmnn>>%++889DNN2<NN~~ekk55788.{{))++Q-QQ+,{{5(8INNr   zmaximum indexrM   )rg   c                     [        XSX#S9u  pEU$ NT)rd   re   r    )r!   rC   ru   rM   r    _rets         r   argmaxr{      s     5tSawHQJr   c                    U(       a  X:H  =(       a    X:  nOSnX:  =(       d    Un[         R                  " X`U5      n[         R                  " XaU5      nXx4$ rF   rG   )	rI   rJ   rK   rL   rM   rN   lt	value_ret	index_rets	            r   _argmin_combiner      sN    26?		CB

2v.I

2v.Ir   c                    [        XX#S5      $ rT   r   rV   s       r   _argmin_combine_tie_break_leftr      rX   r   c                    [        XX#S5      $ rF   r   rV   s       r   _argmin_combine_tie_break_fastr      r[   r   c                .    [         R                  " X5      $ r]   r0   r_   s     r   _elementwise_minr      rc   r   r1   c                r   [         R                  " U 5      n U(       a;  U(       a  [         R                  " X[        US9$ [         R                  " X[        US9$ [         R
                  " U R                  R                  5      S:  a  [         R
                  " U R                  R                  5       5      (       a   U R                  [         R                  5      n OEU R                  R                  5       (       d   S5       eU R                  [         R                  5      n [         R                  " X[        US9$ ri   )r   rk   rl   r   r   r
   r@   rm   rn   ro   rp   rq   rr   rs   r   rt   s        r   minr      s    
 --e4E(,,U:Xdmnn,,U:Xdmnn>>%++889B>~~ekk55788.{{))++Q-QQ+,{{5(8INNr   zminimum indexc                     [        XSX#S9u  pEU$ rw   )r   rx   s         r   argminr      s     TQ_uFAJr   c                
    X-   $ r]   r   r_   s     r   _sum_combiner     	    5Lr   r@   c                $   [         R                  " U5      nUb  U$ S nU R                  5       (       a&  U R                  S:  a  [         R                  nU$ S nU$ U R                  5       (       a"  U R                  S:  a  [         R                  OS nU$ )Nrj   )r   _unwrap_if_constexpris_int_signedint_bitwidthrr   is_int_unsigneduint32)in_dtyper@   	out_dtypes      r   _pick_sum_dtyper   
  s    %%e,E I"*"7"7""<DJJ	  CG	  
	!	!	#	##+#8#82#=DKK4	r   r"   )	dtype_argc                    [        U R                  U5      nUb  U R                  U5      n [        R                  " X[
        US9$ )Nr   )r   r@   ro   r   rs   r   )rC   ru   r    r@   r   s        r   r"   r"     s;    
 !0U CI#;;uLIFFr   c                
    X-  $ r]   r   r_   s     r   _xor_combiner   %  r   r   zxor sumc                    [         R                  " U R                  R                  R	                  5       S5        [         R
                  " X[        US9$ )Nz#xor_sum only supported for integersr   )r   static_asserttypescalarrq   rs   r   rC   ru   r    s      r   xor_sumr   -  s;     	uzz((//13XY;;uLIFFr   c                
    X-  $ r]   r   )r   ys     r   _or_combiner   8  r   r   	reduce_ofc                    [         R                  " U R                  R                  R	                  5       S5        [         R
                  " X[        US9$ )Nz%reduce_of only supported for integersr   )r   r   r   r   rq   rs   r   r   s      r   	reduce_orr   =  s;     	uzz((//13Z[;;uK9EEr   cumsumc                    [         R                  " U 5      n [        U R                  U5      nUb  U R	                  U5      n [         R
                  " X[        U5      $ r]   )r   rk   r   r@   ro   associative_scanr   )rC   ru   reverser@   r   s        r   r   r   H  sM     --e4E /U CI#  lGDDr   c                
    X-  $ r]   r   r_   s     r   _prod_combiner   Z  r   r   cumprodc                f    [         R                  " U 5      n [         R                  " X[        U5      $ r]   )r   rk   r   r   )rC   ru   r   s      r   r   r   _  s)    
 --e4E  mWEEr   c                    [         R                  " SS5      n[         R                  " US/X-
  S-
  -  S/-   S/U-  -   5      nU$ )Nr   r   r   )r   aranger,   )n_dimsr2   ars      r   
_indicatorr   k  sF    	Q	B	b1#a0A36!q@	ABIr   c                R   [        U R                  5      n[        R                  " U R                  R
                  SS9nU R                  USS9nU[        XSS-
  U-
  S5      -  nUR                  U R                  SS9n[        X25      n[        R                  " X:  X-  :g  Xp5      n	U	$ )NTbitwidthsignedbitcastr   )
r   r-   r   get_int_dtyper@   rm   ro   r   r   rH   )
r   flipr   r   idtypeixiyr   is_rightrz   s
             r   _compare_and_swapr   r  s     #177^F )C)CDQF	
fd	#B	gb1*q.$/	/B
aggt$A &$H **ae11
8CJr   c                    US:X  a   [        [        U R                  5      U5      nOUn[        R                  " U5       H  n[        XUS-
  U-
  5      n M     U $ )zR
order_type 0 == ascending
order_type 1 == descending
order_type 2 == alternating
r   r   )r   r   r-   r   static_ranger   )r   stageorderr   r   s        r   _bitonic_merge_hypercuber     sR     z%.%0u%auqy1}5 &Hr   c                    [         R                  " U S/[        U R                  5      -  5      n[	        XAU5      n[         R                  " X@R
                  5      n U $ )Nr   )r   r,   r   r-   r   r?   )r   r   r   r   hs        r   _bitonic_merger     sD    QeAGGn,-A 51AQ AHr   c                   Uc  [        U R                  5      S-
  OUn[        R                  " U[        U R                  5      S-
  :H  S5        [	        U R                  U   5      nUc  UO
[	        U5      n[	        U R
                  5      n[        R                  " U S/U-  5      n[        R                  " SUS-   5       H  n	[        XX:  a  SOU5      nM     [        R                  " US-   US-   5       Hc  n	U(       a#  [        U[	        UR
                  5      S-
  U-
  S9O"[        U[	        UR
                  5      S-
  U-
  S9n[        XX:  a  SOU5      nMe     [        R                  " XR                  SS SU-  /-   5      n U $ )aA  
Sorts a tensor along a specified dimension.

:param x: The input tensor to be sorted.
:type x: Tensor
:param dim: The dimension along which to sort the tensor. If None, the tensor is sorted along the last dimension. Currently, only sorting along the last dimension is supported.
:type dim: int, optional
:param k: the number of top elements to select. If none, assume k = x.shape[dim]
:type k: int, optional
:param descending: If set to True, the tensor is sorted in descending order. If set to False, the tensor is sorted in ascending order.
:type descending: bool, optional
Nr   +only minor dimension is currently supportedr   )ru   )lenr?   r   r   r   r-   r,   r   r   r!   r   )
r   kr$   
descendingr&   log_nlog_kr   r   r   s
             r   	sort_implr     sS    03{3qww<!+Dts177|a//1^_!!''$-0E%&YEE!HE"177^F 	Qf%A q%!),$Q	1zJ -
 uqy%!)49CCqww!+e35QV[\]\c\cVdghVhkpVpIr$QAIq:N 5
 	Q5z12AHr   c                    [        XUS9$ )N)r$   r   r   )r   r$   r   s      r   sortr     s    QJ77r   c                    [        XUSS9$ )NT)r   r$   r   r   )r   r   r$   s      r   topkr     s    Q66r   c                    Uc  [        U R                  5      S-
  OUn[        R                  " U[        U R                  5      S-
  :H  S5        [	        U R                  S   5      n[        XX$5      $ )Nr   r   r   )r   r?   r   r   r   r   )r   r$   r   r&   r   s        r   bitonic_merger     s^     03{3qww<!+Dts177|a//1^_"1772;/F!Z88r   c                    [         R                  " U 5      n [         R                  " U5      nU c  [        U5      S-
  n U S:  a  U [        U5      -  n [         R                  " U 5      $ r   )r   r   r   r
   )r$   r?   s     r   _get_flip_dimr     sX    

#
#C
(C%%e,E
{%j1n
Qws5z>>#r   c                   [         R                  " [        U R                  5      * U:*  =(       a    U[        U R                  5      :  5        [	        XR                  5      n[         R                  " [        U R                  U   5      5        [        U R                  U   5      n[         R                  " U R                  R                  SS9n[         R                  " U R                  USS9U R                  SU S/U-  -   U R                  US-   S -   5      n[         R                  " U5       H  nU[        XRU-   S5      -  nM     [         R                  " XPR                  5      R                  U R                  SS9n U $ )z
Flips a tensor `x` along the dimension `dim`.

:param x: the first input tensor
:type x: Block
:param dim: the dimension to flip along
:type dim: int
Tr   r   Nr   r   )r   r   r   r?   r   r   r   r   r@   rm   r,   ro   r   r   )r   r$   r&   stepsr   r   r   s          r   r   r     s/    	AGG}+Bc!''l0BC(gg6D'67!!''$-0E )C)CDQFQTT&$T/$1#+1MPQPWPWX\_`X`XaPb1bcAu%!8T** &Q ##AGGT#:AHr   c                    [         R                  " X5      n[        UR                  5      S:X  a  U$ [         R                  " X"R                  SS SUR                  S   -  /-   5      $ )a  
Interleaves the values of two tensors along their last dimension. The two tensors must have the same shape.
Equivalent to `tl.join(a, b).reshape(a.shape[:-1] + [2 * a.shape[-1]])`

:param a: The first input tensor.
:type a: Tensor
:param b: The second input tensor.
:type b: Tensor
r   Nr   )r   joinr   r?   r,   )r`   ra   cs      r   
interleaver     sU     			!A
177|q
 ||Awws|q1772;.??@@r   )r   core.constexpr)NFF)F)NFTF)TF)r   r   r@   r   )NFN)r@   r   rF   )r   FN)r   F)r   r   r2   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   r$   r   r   r   )r$   r   r   r   r]   )r   r   r$   r   )6
__future__r   runtime.jitr    r   r   r   r   _tensor_member_fnr   _add_math_1arg_docstrr   r   r.   r<   rA   rD   rR   rW   rZ   rb   _add_reduction_docstrr!   r{   r   r   r   r   r   r   r   r   r"   r   r   r   r   _add_scan_docstrr   r   r   r   r   r   r   CONSTEXPR_0r   r   r   r   r   r   r   r   r   r   <module>r      s   "   
 9 	   	  I&" '  " I&. '  . ?  ? $ $N 	& 	& + +   A A B B   I:J*IKOK  O" O;KL M       A A B B   I:J*IKOK  O" O;KL M  
   EW5G 6  G   I&G '  G   K(F )  F x73	E 4  	E   y!F "  F    $  *   %)dhdtdt % %P "&TEUEU 8 8 7 7 +/dN^N^ 9 9   . A Ar   