
    wg                        d dl mZ d dl mZ ddlmZ ddlmZ  ej                  d      Zdee	   defd	Z
d
e	dej                  egef   fdZd
e	dej                  egef   fdZd
e	dej                  egef   fdZej                   e
g d       ed      d5d                     Zej                   e
ddg       ed      ej"                  d5d                            Zej                   e
ddg       ed      ej"                  d5d                            Zej                   e
ddg       ed      ej"                  d5d                            Zej                   e
ddg       ed      ej"                  d5d                            Zej                   e
ddg       ed      ej"                  d5d                            Zej                   e
ddg       ed      ej"                  d5d                             Zej                   e
ddg       ed!      ej"                  d5d"                            Zej                   e
dg       ed#      ej"                  d5d$                            Zej                   e
ddg       ed%      ej"                  d5d&                            Zej                   ed'      ej"                  d5d(                     Zej                   ed)      d6d*              Zej                   e
dg       ed+      d5d,                     Zej                   e
ddg       ed-      ej"                  d5d.                            Zej                   e
ddg       ed/      ej"                  d5d0                            Zej                   e
ddg       ed1      ej"                  d5d2                            Z ej                   ed3      d5d4              Z!y)7   )core)semantic    wraps)ListTdtypesreturnc                       fd}|S )al  
    We're following libdevice's convention to check accepted data types for math functions.
    It is not a good practice to support all data types as accelerators/GPUs don't support
    many float16 and bfloat16 math operations.
    We should let the users know that they are using and invoke explicit cast to convert
    the data type to the supported one.
    c                 2     t                fd       }|S )Nc                  j   t        |       t        |j                               z   }|D cg c]  }t        |t        j                        s|! c}D ]S  }|j
                  j                  j                  vs&t        d d|j
                  j                  j                           | i |S c c}w )NzExpected dtype z	 but got )	listvalues
isinstancer   tensortypescalarname
ValueError)argskwargsall_argsaargr
   fns        Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/triton/language/math.pycheckz,_check_dtype.<locals>.wrapper.<locals>.check   s     DzD$99H#+Jaz!T[[/IJ `88??''v5$vhiH\H\G]%^__` t&v&& Ks   B0B0r   )r   r   r
   s   ` r   wrapperz_check_dtype.<locals>.wrapper   s     	r	' 
	'      )r
   r   s   ` r   _check_dtyper"   	   s     Nr    r   c                 ,     dt         dt         f fd}|S )Nfuncr   c                 :    d}|j                        | _        | S )Nzk
    Computes the element-wise {name} of :code:`x`.

    :param x: the input values
    :type x: Block
    r   format__doc__r$   docstrr   s     r   
_decoratorz)_add_math_1arg_docstr.<locals>._decorator$   s#     }}$}/r    r	   r   r,   s   ` r   _add_math_1arg_docstrr/   "   s     q  r    c                 ,     dt         dt         f fd}|S )Nr$   r   c                 :    d}|j                        | _        | S )Nz
    Computes the element-wise {name} of :code:`x` and :code:`y`.

    :param x: the input values
    :type x: Block
    :param y: the input values
    :type y: Block
    r&   r'   r*   s     r   r,   z)_add_math_2arg_docstr.<locals>._decorator3   s#     }}$}/r    r-   r.   s   ` r   _add_math_2arg_docstrr2   1   s    
 
q 
 r    c                 ,     dt         dt         f fd}|S )Nr$   r   c                 :    d}|j                        | _        | S )Nz
    Computes the element-wise {name} of :code:`x`, :code:`y`, and :code:`z`.

    :param x: the input values
    :type x: Block
    :param y: the input values
    :type y: Block
    :param z: the input values
    :type z: Block
    r&   r'   r*   s     r   r,   z)_add_math_3arg_docstr.<locals>._decoratorD   s#    	 }}$}/r    r-   r.   s   ` r   _add_math_3arg_docstrr5   B   s     q  r    )int32int64uint32uint64)r
   z-most significant N bits of the 2N-bit productNc                    t        j                  | |      } t        j                  ||      }t        j                  | ||      \  } }t        j                  |j	                  | j
                  |j
                        | j                        S N)r   
_to_tensorbinary_op_type_legalizationr   create_umulhihandler   xy_builders      r   umulhirD   U   sf     	8$A8$A++Aq(;DAq;;x--ahhA166JJr    fp32fp64exponentialc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   
create_expr?   r   rA   rC   s     r   exprK   _   8    
 	8$A;;x**1884aff==r    zexponential (base 2)c                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_exp2r?   r   rJ   s     r   exp2rO   h   8    
 	8$A;;x++AHH5qvv>>r    znatural logarithmc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   
create_logr?   r   rJ   s     r   logrS   q   rL   r    zlogarithm (base 2)c                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_log2r?   r   rJ   s     r   log2rV   z   rP   r    cosinec                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   
create_cosr?   r   rJ   s     r   cosrZ      rL   r    sinec                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   
create_sinr?   r   rJ   s     r   sinr^      rL   r    zfast square rootc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_sqrtr?   r   rJ   s     r   sqrtra      rP   r    z)precise square root (rounding to nearest)c                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_precise_sqrtr?   r   rJ   s     r   sqrt_rnrd      s8    
 	8$A;;x33AHH=qvvFFr    zinverse square rootc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_rsqrtr?   r   rJ   s     r   rsqrtrg      8    
 	8$A;;x,,QXX6??r    zabsolute valuec                    t        j                  | |      } | j                  }|j                         rut        j                  | j
                  dt         j                  |      }t        j                  |j                  | j                  |j                        | j                        S |j                         r9t        j                  |j                  | j                        | j                        S |j                         r9t        j                  |j                  | j                        | j                        S |j                         r| S J d|        )N   )rC   zUnexpected dtype )r   r<   dtypeis_fp8e4b15fullshapeint8r   
create_andr?   r   is_floatingcreate_fabsis_int_signedcreate_iabsis_int_unsigned)rA   rC   rk   masks       r   absrw      s     	8$AGGEyy$		HE{{8..qxxEqvvNN				{{8//9166BB				{{8//9166BB				 1)%11ur    zfast divisionc                     t        j                  |      }t        j                  | |      } t        j                  ||      }t        j                  | |||      S r;   )r   _constexpr_to_valuer<   r   fdiv)rA   rB   ieee_roundingrC   s       r   rz   rz      sI     ,,];M8$A8$A==A}h77r    z&precise division (rounding to nearest)c                    t        j                  | |      } t        j                  ||      }t        j                  | ||      \  } }t        j                  |j	                  | j
                  |j
                        | j                        S r;   )r   r<   r=   r   create_precise_divfr?   r   r@   s      r   div_rnr~      sf     	8$A8$A++Aq(;DAq;;x33AHHahhGPPr    zerror functionc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   
create_erfr?   r   rJ   s     r   erfr      rL   r    floorc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_floorr?   r   rJ   s     r   r   r      rh   r    ceilc                     t        j                  | |      } t        j                  |j                  | j                        | j
                        S r;   )r   r<   r   create_ceilr?   r   rJ   s     r   r   r      rP   r    zfused multiply-addc                    t        j                  | |      } t        j                  ||      }t        j                  ||      }t        j                  | ||      \  } }t        j                  || |      \  }} t        j                  |||      \  }}t        j                  |j	                  | j
                  |j
                  |j
                        | j                        S r;   )r   r<   r=   r   
create_fmar?   r   )rA   rB   zrC   s       r   fmar      s     	8$A8$A8$A++Aq(;DAq++Aq(;DAq++Aq(;DAq;;x**188QXXqxxH!&&QQr    r;   )FN)" r   r   	functoolsr   typingr   TypeVarr	   strr"   Callabler/   r2   r5   builtinrD   _tensor_member_fnrK   rO   rS   rV   rZ   r^   ra   rd   rg   rw   rz   r~   r   r   r   r   r!   r    r   <module>r      s      DLLc q 2 qc1f(=  qc1f(= " qc1f(= & ;<FGK H = K ff%&}%>  & ' >
 ff%&-.?  / ' ?
 ff%&*+>  , ' >
 ff%&+,?  - ' ?
 ff%&x >  ! ' >
 ff%&v>   ' >
 ff%&)*?  + ' ?
 fXBCG  D  G
 ff%&,-@  . ' @
 '(2  ) 2  '8 ( 8 fX?@Q A  Q ff%&'(>  ) ' >
 ff%&w@    ' @
 ff%&v?   ' ?
 +,R - Rr    