
    rh?                       d dl mZ ddlmZ ddlmZ ddlmZ dDdZdDdZej                  ed	               Z
ej                  e ej                  d
      d                      Zej                  e ej                  d      dEd                     Zej                  edFd              Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zej                  e ej,                  ddd      dGd                     Zej                  e ej,                  dd      dHd                     Zed         Zed!        Zed"        Zed#        Zej                  e ej,                  d$dd      dGd%                     Zej                  e ej,                  d&d      dHd'                     Zed(        ZdId*Z ej                  e ej,                  d+d),      dJdKd-                     Z!ed.        Z"ej                  e ej,                  d/      dLd0                     Z#ed1        Z$ej                  e ej,                  d2      dFd3                     Z%ej                  e ejL                  d4d),      dMdKd5                     Z'ed6        Z(ej                  e ejL                  d7      dNd8                     Z)edOd9       Z*edDd:       Z+edPd;       Z,edQd<       Z-eddej\                  fdRd=       Z/edej\                  fdSd>       Z0edTdUd?       Z1edej\                  fdSd@       Z2dA Z3ej                  edTdB              Z4edC        Z5y)V    )annotations   )jit   )core)mathc                    d}t        j                  |       j                  }|dkD  r|dz  }|dz  }|dkD  rt        j                  |      S )Nr   r   )r   	constexprvalue)ilog2ns      k/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/triton/language/standard.py_log2r   
   sN    DqA
a%	a	 a% >>$    c                d    | j                   }t        j                  ||dz
  z  dk(  xr |dk7        S Nr   r   )r   r   r
   )r   r   s     r   _is_power_of_twor      s0    	A>>1A;1,7a88r   c                    | |z   dz
  |z  S )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                :    ddt        j                  |        z   z  S )Nr   )r   exp)r   s    r   r   r   +   s     DHHaRL !!r   softmaxNc                    |d}n|}| t        | ||      z
  }t        j                  |      }t        |||      }t        j                  |||      S )Nr   	keep_dims)maxr   r   sumfdiv)r   dimr    ieee_rounding_dimznumdens           r   r   r   2   sT     { "	C49--A
((1+C
c49
-C99S#}--r   c                H    t        j                  | | j                  g|      S )zn
    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     <<AGG9+>>r   c                    | |z  |z   }||z  }||z  }||z  }t        j                  ||z
  |      }||z  }|||z  z   }	||z  }
|	|
fS )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   sm    , 
VaB voGW}HvE\\&5.&1F	gBBKE&LE%<r   c                0    t        j                  | d|      S )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    t        | j                  | j                        S )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                    |r| |k(  xr ||k  }nd}| |kD  xs |}t        j                  || |      }t        j                  |||      }||fS NFr   where)	value1index1value2index2tie_break_lefttiegtv_reti_rets	            r   _argmax_combinerR      sY    26F?	&	CBJJr66*EJJr66*E%<r   c                     t        | |||d      S NTrR   rI   rJ   rK   rL   s       r   _argmax_combine_tie_break_leftrW          66664@@r   c                     t        | |||d      S rF   rU   rV   s       r   _argmax_combine_tie_break_fastrZ          66665AAr   c                .    t        j                  | |      S N)r   maximumabs     r   _elementwise_maxrb          <<1r   r^   return_indicesreturn_indices_tie_break_left)return_indices_argtie_break_argc                z   t        j                  |       } |r<|rt        j                  | |t        |      S t        j                  | |t        |      S t        j
                  | j                  j                        t        j
                  d      k  rt        j
                  | j                  j                               r | j                  t         j                        } n@| j                  j                         sJ d       | j                  t         j                        } t        j                  | |t        |      S 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(,,UD:Xdmnn,,UD:Xdmnn>>%++889DNN2<NN~~ekk5578.{{))+Q-QQ+,{{5$(8INNr   zmaximum indexrM   )rg   c                ,    t        | |d||      \  }}|S NT)rd   re   r    )r!   rC   ru   rM   r    _rets         r   argmaxr{      s!     5$tSamvwHQJr   c                    |r| |k(  xr ||k  }nd}| |k  xs |}t        j                  || |      }t        j                  |||      }||fS rF   rG   )	rI   rJ   rK   rL   rM   rN   lt	value_ret	index_rets	            r   _argmin_combiner      sZ    26F?	&	CB

2vv.I

2vv.Iir   c                     t        | |||d      S rT   r   rV   s       r   _argmin_combine_tie_break_leftr      rX   r   c                     t        | |||d      S rF   r   rV   s       r   _argmin_combine_tie_break_fastr      r[   r   c                .    t        j                  | |      S r]   r0   r_   s     r   _elementwise_minr      rc   r   r1   c                T   t        j                  |       } |r<|rt        j                  | |t        |      S t        j                  | |t        |      S t        j
                  | j                  j                        dk  rt        j
                  | j                  j                               r | j                  t         j                        } n@| j                  j                         sJ d       | j                  t         j                        } t        j                  | |t        |      S ri   )r   rk   rl   r   r   r
   r@   rm   rn   ro   rp   rq   rr   rs   r   rt   s        r   minr      s    
 --e4E(,,UD:Xdmnn,,UD:Xdmnn>>%++889B>~~ekk5578.{{))+Q-QQ+,{{5$(8INNr   zminimum indexc                ,    t        | |d||      \  }}|S rw   )r   rx   s         r   argminr      s!     TQ_ktuFAsJr   c                    | |z   S r]   r   r_   s     r   _sum_combiner         q5Lr   r@   c                   t        j                  |      }||S d }| j                         r%| j                  dk  rt         j                  }|S d }|S | j                         r!| j                  dk  rt         j                  nd }|S )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                    t        | j                  |      }|| j                  |      } t        j                  | |t
        |      S )Nr   )r   r@   ro   r   rs   r   )rC   ru   r    r@   r   s        r   r"   r"     s=    
 !0U CI#;;udLIFFr   c                    | |z  S r]   r   r_   s     r   _xor_combiner   %  r   r   zxor sumc                    t        j                  | j                  j                  j	                         d       t        j
                  | |t        |      S )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;;udLIFFr   c                    | |z  S r]   r   )r   ys     r   _or_combiner   8  r   r   	reduce_ofc                    t        j                  | j                  j                  j	                         d       t        j
                  | |t        |      S )Nz%reduce_of only supported for integersr   )r   r   r   r   rq   rs   r   r   s      r   	reduce_orr   =  s=     	uzz((//13Z[;;udK9EEr   cumsumc                    t        j                  |       } t        | j                  |      }|| j	                  |      } t        j
                  | |t        |      S r]   )r   rk   r   r@   ro   associative_scanr   )rC   ru   reverser@   r   s        r   r   r   H  sO     --e4E /U CI#  lGDDr   c                    | |z  S r]   r   r_   s     r   _prod_combiner   Z  r   r   cumprodc                d    t        j                  |       } t        j                  | |t        |      S r]   )r   rk   r   r   )rC   ru   r   s      r   r   r   _  s+    
 --e4E  mWEEr   c                    t        j                  dd      }t        j                  |dg| |z
  dz
  z  dgz   dg|z  z         }|S )Nr   r   r   )r   aranger,   )n_dimsr2   ars      r   
_indicatorr   k  sH    	Q	B	b1#!a0A36!q@	ABIr   c                j   t        | j                        }t        j                  | j                  j
                  d      }| j                  |d      }|t        ||dz
  |z
  d      z  }|j                  | j                  d      }t        ||      }t        j                  | |kD  ||z  k7  ||       }	|	S )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	gb&1*q.$/	/B
aggt$A &!$H **a!e11a
8CJr   c                    |dk(  r t        t        | j                        |      }n|}t        j                  |      D ]  }t        | ||dz
  |z
        }  | S )zb
    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     sY     z%.%0u% 6auqy1}56Hr   c                    t        j                  | dgt        | j                        z        }t	        |||      }t        j                  || j
                        } | S )Nr   )r   r,   r   r-   r   r?   )r   r   r   r   hs        r   _bitonic_merger     sH    QeAGGn,-A E51AQ AHr   c                   |t        | j                        dz
  n|}t        j                  |t        | j                        dz
  k(  d       t	        | j                  |         }||n
t	        |      }t	        | j
                        }t        j                  | dg|z        }t        j                  d|dz         D ]  }	t        ||	|	|k  rdn|      } t        j                  |dz   |dz         D ]d  }	|r&t        |t	        |j
                        dz
  |z
        n%t        |t	        |j
                        dz
  |z
        }t        |||	|k  rdn|      }f t        j                  || j                  dd d|z  gz         } | S )ai  
    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     sk    03{3qww<!+Dts177|a//1^_!!''$-0E%&YEE!HE"177^F 	Qf%A q%!), K$QE	1zJK
 uqy%!)4 O9CCqww!+e35QV[\]\c\cVdghVhkpVpIr$QAIq:NO
 	Q5z12AHr   c                    t        | ||      S )N)r$   r   r   )r   r$   r   s      r   sortr     s    QCJ77r   c                     t        | ||d      S )NT)r   r$   r   r   )r   r   r$   s      r   topkr     s    Q!66r   c                    |t        | j                        dz
  n|}t        j                  |t        | j                        dz
  k(  d       t	        | j                  d         }t        | |||      S )Nr   r   r   )r   r?   r   r   r   r   )r   r$   r   r&   r   s        r   bitonic_merger     sb     03{3qww<!+Dts177|a//1^_"1772;/F!VZ88r   c                    t        j                  |       } t        j                  |      }| t        |      dz
  } | dk  r| t        |      z  } t        j                  |       S r   )r   r   r   r
   )r$   r?   s     r   _get_flip_dimr     sX    

#
#C
(C%%e,E
{%j1n
Qws5z>>#r   c                   t        j                  t        | j                         |k  xr |t        | j                        k         t	        || j                        }t        j                  t        | j                  |                t        | j                  |         }t        j                  | j                  j                  d      }t        j                  | j                  |d      | j                  d| dg|z  z   | j                  |dz   d z         }t        j                  |      D ]  }|t        |||z   d      z  } t        j                  || j                        j                  | j                  d      } | S )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(agg6D'67!!''$-0E )C)CDQFQTT&$T/$1#+1MPQPWPWX\_`X`XaPb1bcAu% +4!8T**+Q ##AGGT#:AHr   c                    t        j                  | |      }t        |j                        dk(  r|S t        j                  ||j                  dd d|j                  d   z  gz         S )a7  
    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     sY     			!QA
177|q
 ||Aqwws|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   