
    Ϧi5                       S SK Jr  SSKJr  SSKJr  SSKJr  S9S jrS9S jr\R                  \S	 5       5       r
\R                  \\R                  " S
5      S 5       5       5       r\R                  \\R                  " S5      S:S j5       5       5       r\R                  \S 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S;S j5       5       5       r\R                  \\R,                  " SSS9S<S j5       5       5       r\S  5       r\S! 5       r\S" 5       r\S# 5       r\R                  \\R,                  " S$SSS9S;S% j5       5       5       r\R                  \\R,                  " S&SS9S<S' j5       5       5       r\S( 5       r\R                  \\R,                  " S)5      S=S* j5       5       5       r \S+ 5       r!\R                  \RD                  \R,                  " S,5      S>S- j5       5       5       r#\R                  \\RH                  " S.5      S?S/ j5       5       5       r%\S0 5       r&\R                  \\RH                  " S15      S?S2 j5       5       5       r'\S@S3 j5       r(\SAS4 j5       r)\R                  \S\RT                  4SBS5 jj5       5       r+S6 r,\R                  \SCS7 j5       5       r-\S8 5       r.g)D    )annotations   )jit   )core)mathc                z    SnU R                   nUS:  a  US-  nUS-  nUS:  a  M  [        R                  " U5      $ )Nr   r   valuer   	constexpr)ilog2ns      W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/triton/language/standard.py_log2r   
   sC    D	A
a%	a	 a% >>$    c                n    U R                   n[        R                  " XS-
  -  S:H  =(       a    US:g  5      $ )Nr   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   softmaxc                    U [        U S5      -
  n[        R                  " U5      n[        US5      n[        R                  " X4U5      $ )Nr   )maxr   r   sumfdiv)r   ieee_roundingznumdens        r   r   r   2   s<     	
C1IA
((1+C
c1+C99S}--r   c                B    [         R                  " X R                  /SS9$ )z^
Returns a contiguous flattened view of :code:`x`.

:param x: the input tensor
:type x: Block
T)can_reorder)r   reshapenumelr   s    r   ravelr+   <   s     <<GG9$77r   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   	swizzle2dr9   H   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   zerosr>   p   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
)r>   r<   r=   )inputs    r   
zeros_likerA   }   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_combinerO      sM    26?		CBJJr6*EJJr6*E<r   c                    [        XX#S5      $ NTrO   rF   rG   rH   rI   s       r   _argmax_combine_tie_break_leftrT          664@@r   c                    [        XX#S5      $ rC   rR   rS   s       r   _argmax_combine_tie_break_fastrW          665AAr   c                .    [         R                  " X5      $ N)r   maximumabs     r   _elementwise_maxr_          <<r   r[   return_indicesreturn_indices_tie_break_left)return_indices_argtie_break_argNc                   [         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$ N	keep_dims    z"Expecting input to be integer type)r   _promote_bfloat16_to_float32_reduce_with_indicesrT   rW   r   r=   primitive_bitwidthis_floatingtofloat32is_intint32reducer_   r@   axisra   rb   rh   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 indexrJ   )rd   c                     [        XSX#S9u  pEU$ NT)ra   rb   rh   )r    r@   rt   rJ   rh   _rets         r   argmaxrz      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$ rC   rD   )	rF   rG   rH   rI   rJ   rK   lt	value_ret	index_rets	            r   _argmin_combiner      sN    26?		CB

2v.I

2v.Ir   c                    [        XX#S5      $ rQ   r   rS   s       r   _argmin_combine_tie_break_leftr      rU   r   c                    [        XX#S5      $ rC   r   rS   s       r   _argmin_combine_tie_break_fastr      rX   r   c                .    [         R                  " X5      $ rZ   r-   r\   s     r   _elementwise_minr      r`   r   r.   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$ rf   )r   rj   rk   r   r   r   r=   rl   rm   rn   ro   rp   rq   rr   r   rs   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$ rv   )r   rw   s         r   argminr      s     TQ_uFAJr   c                
    X-   $ rZ   r   r\   s     r   _sum_combiner      	    5Lr   r!   c                b    [         R                  " U 5      n [         R                  " X[        US9$ )Nrg   )r   rj   rr   r   )r@   rt   rh   s      r   r!   r!     s'     --e4E;;uLIFFr   c                
    X-  $ rZ   r   r\   s     r   _xor_combiner     r   r   zxor sumc           	         U R                   R                  nUR                  5       (       d  [        S5      e[        R
                  " XS9n [        R                  " X[        X#US9$ )Nz#xor_sum only supported for integers)_builder)rh   r   
_generator)typescalarrp   
ValueErrorr   rj   rr   r   )r@   rt   rh   r   r   	scalar_tys         r   xor_sumr     sT     

!!I>??--eGE;;uLIeoppr   cumsumc                f    [         R                  " U 5      n [         R                  " X[        U5      $ rZ   )r   rj   associative_scanr   r@   rt   reverses      r   r   r   %  s)    
 --e4E  lGDDr   c                
    X-  $ rZ   r   r\   s     r   _prod_combiner   1  r   r   cumprodc                f    [         R                  " U 5      n [         R                  " X[        U5      $ rZ   )r   rj   r   r   r   s      r   r   r   6  s)    
 --e4E  mWEEr   c                   U R                   U-	  nUSU-  -  SSX2-
  S-
  -  /n[        R                  " X5      n[        R                  " SS5      S S S 2S 4   n[        R                  " [        USU-
  -  S5      S S 2S S S 24   U5      R                  UR                  5      n[        R                  " [        Xg-  S5      S S 2S S S 24   U5      R                  UR                  5      n	[        R                  " XR                  5      n[        R                  " XR                  5      n	[        R                  " U R                  R                  SS9n
UR                  U
SS9nU	R                  U
SS9nU R                  U
SS9nU[        R                  " X:  U:g  X-  [        U5      5      -  nUR                  U R                  SS9$ )Nr   r   r   T)bitwidthsigned)bitcast)r*   r   r)   arangebroadcast_tor!   rn   r=   r<   get_int_dtyperl   rE   rA   )r   flipr   n_dimsn_outerr<   ymaskleftrightidtypeileftirightixry   s                  r   _compare_and_swapr   B  sy   gg/G$q!t^QFJN0CDEQA;;q!T1d]+DSa$h3AtQJ?GJJ177SDc!(A.q$z:EBEEaggNE<<gg&DLL(E)C)CDQFGGFDG)EXXfdX+F	
fd	#B
tzz4<D0%.*R.Q
QC66!''46((r   c                   U R                   U-	  n[        R                  " X:*  5        US:X  ah  USUS-
  U-
  -  -  SSU-  /n[        R                  " [        R                  " [        R
                  " SS5      SSS2S4   U5      U R                  5      nOUn[        R                  " U5       H  n[        XXsU-
  -   U5      n M     U $ )zR
order_type 0 == ascending
order_type 1 == descending
order_type 2 == alternating
r   r   r   N)	r*   r   static_assertr)   r   r   r<   static_ranger   )r   stageorderr   r   r<   r   r   s           r   _bitonic_merger   V  s      gg/Gu' z!(1vzE/A+B!BAq%x P||D--dkk!Q.?a.NPUVXYX_X_`u%aqUN';VD &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[        R
                  " SUS-   5       H  n[        XXT:  a  SOUU5      n M     U $ )a  
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 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
r   z+only minor dimension is currently supportedr   )lenr<   r   r   r   r   r   )r   dim
descending_dimr   r   s         r   sortr   o  s     03{3qww<!+Dts177|a//1^_"1774=1Fq&1*-1ajj&I .Hr   c                    [         R                  " U 5      n [         R                  " U5      nU c  [        U5      S-
  n U [        U5      S-
  :X  d   S5       e[         R                  " U 5      $ )Nr   z2Currently only support flipping the last dimension)r   _unwrap_if_constexprr   r   )r   r<   s     r   _get_flip_dimr     s_    

#
#C
(C%%e,E
{%j1n#e*q. V"VV >>#r   c           	        [         R                  " [        U R                  [	        XR                  5         5      5        [         R                  " [        U R
                  5      5        [        U R
                  5      n[        U R
                  5      [        U R                  [	        XR                  5         5      -
  n[         R                  " U S/U-  5      n[         R                  " XC5      n[         R                  " SS5      SS2S4   S[         R                  " SS5      -
  :H  n[         R                  " X25       H]  nUn[         R                  " SUS-   5       H*  nX:w  d  M
  XS-   :w  d  M  [         R                  " Xx5      nM,     [        XG-  US-   SS9nM_     [         R                  " X@R                  5      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 (currently only final dimension supported)
:type dim: int
r   r   Nr   Trg   )r   r   r   r<   r   r*   r   r)   expand_dimsr   r   r!   )	r   r   stepsstartr   r   r   flip2r/   s	            r   r   r     sO    	'c770K(LMN'01 "!''NE!!''NU177=gg;V3W-XXEQe$A"AKK1ag&!dkk!Q.?*??Du,""1eai0Av!1u*((2 1 	1q5D1 - 	Q 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]   r^   cs      r   
interleaver     sU     			!A
177|q
 ||Awws|q1772;.??@@r   )r   core.constexpr)F)NFTF)TFrC   )NFNN)r   F)r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   rZ   )/
__future__r   runtime.jitr    r   r   r   r   _tensor_member_fnr   _add_math_1arg_docstrr   r   r+   r9   r>   rA   rO   rT   rW   r_   _add_reduction_docstrr    rz   r   r   r   r   r   r   r   r!   r   builtinr   _add_scan_docstrr   r   r   r   r   CONSTEXPR_0r   r   r   r   r   r   r   <module>r      sP   "   
 9 	   	  I&" '  " I&. '  . 8  8 $ $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  
   E"G #  G
   I&q '  q x E !  E   y!F "  F ) )&  0 "&TEUEU   0   8 A Ar   