o
    Y۷ik                     @   s  d dl Zd dlmZmZmZmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZ d dlZdd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Z d#d$ Z!d%d& Z"d'd( Z#d)d* Z$d+d, Z%d-d. Z&d/d0 Z'd1d2 Z(d3d4 Z)d5d6 Z*d7d8 Z+d9d: Z,d;d< Z-d=d> Z.d?d@ Z/dAdB Z0dCdD Z1dEdF Z2dGdH Z3dIdJ Z4dKdL Z5dMdN Z6dOdP Z7dQdR Z8dSdT Z9dUdV Z:G dWdX dXeZ;e<dYkre=  dS dS )Z    N)skip_unless_cc_53unittestCUDATestCaseskip_on_cudasim)numpy_support)cudafloat32float64int32	vectorizevoidint64c                 C       t d}t| | ||< d S N   )r   gridmathacosABi r   W/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/cuda/tests/cudapy/test_math.py	math_acos      
r   c                 C   r   r   )r   r   r   asinr   r   r   r   	math_asin   r   r   c                 C   r   r   )r   r   r   atanr   r   r   r   	math_atan   r   r   c                 C   r   r   )r   r   r   acoshr   r   r   r   
math_acosh   r   r!   c                 C   r   r   )r   r   r   asinhr   r   r   r   
math_asinh   r   r#   c                 C   r   r   )r   r   r   atanhr   r   r   r   
math_atanh$   r   r%   c                 C   r   r   )r   r   r   cosr   r   r   r   math_cos)   r   r'   c                 C   r   r   )r   r   r   sinr   r   r   r   math_sin.   r   r)   c                 C   r   r   )r   r   r   tanr   r   r   r   math_tan3   r   r+   c                 C   r   r   )r   r   r   coshr   r   r   r   	math_cosh8   r   r-   c                 C   r   r   )r   r   r   sinhr   r   r   r   	math_sinh=   r   r/   c                 C   r   r   )r   r   r   tanhr   r   r   r   	math_tanhB   r   r1   c                 C   &   t d}t| | || ||< d S r   )r   r   r   atan2r   r   Cr   r   r   r   
math_atan2G      
r6   c                 C   r   r   )r   r   r   expr   r   r   r   math_expL   r   r9   c                 C   r   r   )r   r   r   erfr   r   r   r   math_erfQ   r   r;   c                 C   r   r   )r   r   r   erfcr   r   r   r   	math_erfcV   r   r=   c                 C   r   r   )r   r   r   expm1r   r   r   r   
math_expm1[   r   r?   c                 C   r   r   )r   r   r   fabsr   r   r   r   	math_fabs`   r   rA   c                 C   r   r   )r   r   r   gammar   r   r   r   
math_gammae   r   rC   c                 C   r   r   )r   r   r   lgammar   r   r   r   math_lgammaj   r   rE   c                 C   r   r   )r   r   r   logr   r   r   r   math_logo   r   rG   c                 C   r   r   )r   r   r   log2r   r   r   r   	math_log2t   r   rI   c                 C   r   r   )r   r   r   log10r   r   r   r   
math_log10y   r   rK   c                 C   r   r   )r   r   r   log1pr   r   r   r   
math_log1p~   r   rM   c                 C   r2   r   )r   r   r   	remainderr4   r   r   r   math_remainder   r7   rO   c                 C   r   r   )r   r   r   sqrtr   r   r   r   	math_sqrt   r   rQ   c                 C   r2   r   )r   r   r   hypotr4   r   r   r   
math_hypot   r7   rS   c                 C   r2   r   )r   r   r   powr4   r   r   r   math_pow   r7   rU   c                 C   r   r   )r   r   r   ceilr   r   r   r   	math_ceil   r   rW   c                 C   r   r   )r   r   r   floorr   r   r   r   
math_floor   r   rY   c                 C   r2   r   )r   r   r   copysignr4   r   r   r   math_copysign   r7   r[   c                 C   r2   r   )r   r   r   fmodr4   r   r   r   	math_fmod   r7   r]   c                 C   s(   t d}t| | \||< ||< d S r   )r   r   r   modfr4   r   r   r   	math_modf   s   
r_   c                 C   r   r   )r   r   r   isnanr   r   r   r   
math_isnan   r   ra   c                 C   r   r   )r   r   r   isinfr   r   r   r   
math_isinf   r   rc   c                 C   r   r   )r   r   r   isfiniter   r   r   r   math_isfinite   r   re   c                 C   r   r   )r   r   r   degreesr   r   r   r   math_degrees   r   rg   c                 C   r   r   )r   r   r   radiansr   r   r   r   math_radians   r   ri   c                 C   r   r   )r   r   r   truncr   r   r   r   
math_trunc   r   rk   c                 C   s"   t d}| | ||  ||< d S r   r   r   r4   r   r   r   math_pow_binop      
rm   c                 C   s"   t d}| | ||  ||< d S r   rl   r4   r   r   r   math_mod_binop   rn   ro   c                   @   sN  e Zd ZdddZdddZdddZdd
dZdddZdd Zdd Z	dd Z
dd ZdddZdddZdddZdddZdd  Zdd!d"Zdd#d$Zdd%d&Zdd'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zed9d: Zed;ed<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1d`da Z2edbdcdd Z3dedf Z4dgdh Z5didj Z6dkdl Z7dmdn Z8dodp Z9dqdr Z:dsdt Z;edudvdw Z<dxdy Z=dzd{ Z>d|d} Z?d~d Z@dd ZAdd ZBdd ZCdd ZDdd ZEdS )TestCudaMathr   r   c                 C      |  ||tjtj|| d S N)unary_templatenpfloat16selffuncnpfuncstartstopr   r   r   unary_template_float16      z#TestCudaMath.unary_template_float16c                 C   rq   rr   rs   rt   r   rv   r   r   r   unary_template_float32   r}   z#TestCudaMath.unary_template_float32c                 C   rq   rr   rs   rt   r	   rv   r   r   r   unary_template_float64   r}   z#TestCudaMath.unary_template_float642   c                 C      |  ||tjtj|| d S rr   )rs   rt   r   r	   rv   r   r   r   unary_template_int64   r}   z!TestCudaMath.unary_template_int64c                 C   r   rr   )rs   rt   uint64r	   rv   r   r   r   unary_template_uint64   r}   z"TestCudaMath.unary_template_uint64c                 C   s   d}t ||||}t ||}	t|d d d }
t|d d d }t|
|f|}|d|f ||	 |t jkrBd}n
|t j	krJd}nd}t j
j|||	|d d S )Nr   r   gvIh%<=ư>gMbP?rtol)rt   linspaceastype
empty_liker   
from_dtyper   jitr	   r   testingassert_allclose)rw   rx   ry   npdtype	nprestyperz   r{   nelemr   r   arytyperestypecfuncr   r   r   r   rs      s   

zTestCudaMath.unary_templatec           
   	   C   s   t |}|jd }t jd||jdd|jt jt jg|d}t j|t jd}t	
|d d d td d d f|}	|	d|jf || t j||| |	d|jf | | t j|||  d S )N   g        g      ?g      ?dtyper   )rt   finfotinyarraymaxinfnanr   r
   r   r   sizer   assert_array_equal)
rw   rx   ry   r   npmtypefidenormr   r   r   r   r   r   unary_bool_special_values   s   

&z&TestCudaMath.unary_bool_special_valuesc                 C      |  ||tjt d S rr   )r   rt   r   rw   rx   ry   r   r   r   !unary_bool_special_values_float32     z.TestCudaMath.unary_bool_special_values_float32c                 C   r   rr   )r   rt   r	   r   r   r   r   !unary_bool_special_values_float64  r   z.TestCudaMath.unary_bool_special_values_float64c                 C   rq   rr   r~   rv   r   r   r   unary_bool_template_float32  r}   z(TestCudaMath.unary_bool_template_float32c                 C   rq   rr   r   rv   r   r   r   unary_bool_template_float64  r}   z(TestCudaMath.unary_bool_template_float641   c                 C   rq   rr   )rs   rt   r
   rv   r   r   r   unary_bool_template_int32  r}   z&TestCudaMath.unary_bool_template_int32c                 C   rq   rr   )rs   rt   r   rv   r   r   r   unary_bool_template_int64  r}   z&TestCudaMath.unary_bool_template_int64c                 C   s   d}t ||||}t j|jt jd}	|d d d }
td d d }t|
|f|}|d|f ||	 t j	|||	 d S )Nr   r   r   )
rt   r   r   emptyshaper
   r   r   r   r   )rw   rx   ry   r   r   rz   r{   r   r   r   iarytypeoarytyper   r   r   r   unary_bool_template  s   z TestCudaMath.unary_bool_templatec                 C   rq   rr   )binary_templatert   r   rv   r   r   r   binary_template_float32'  r}   z$TestCudaMath.binary_template_float32c                 C   rq   rr   )r   rt   r	   rv   r   r   r   binary_template_float64*  r}   z$TestCudaMath.binary_template_float64c                 C   r   rr   )r   rt   r   r	   rv   r   r   r   binary_template_int64-  r}   z"TestCudaMath.binary_template_int64c                 C   r   rr   )r   rt   r   r	   rv   r   r   r   binary_template_uint640  r}   z#TestCudaMath.binary_template_uint64c                 C   s   d}t ||||}t ||}	t|d d d }
t|d d d }t|
|
|f|}|d|f |||	 t j	||||	 d S )Nr   r   )
rt   r   r   r   r   r   r   r   r   r   )rw   rx   ry   r   r   rz   r{   r   r   r   r   r   r   r   r   r   r   3  s   zTestCudaMath.binary_templatec                 C   H   |  ttj | ttj | jttjddd | jttjddd d S Nr   rz   r{   )r   r   rt   arccosr   r   r   rw   r   r   r   test_math_acos@     zTestCudaMath.test_math_acosc                 C   r   r   )r   r   rt   arcsinr   r   r   r   r   r   r   test_math_asinK  r   zTestCudaMath.test_math_asinc                 C   <   |  ttj | ttj | ttj | ttj d S rr   )r   r   rt   arctanr   r   r   r   r   r   r   test_math_atanV     zTestCudaMath.test_math_atanc                 C   T   | j ttjddd | jttjddd | jttjddd | jttjddd d S )Nr      r   )r   r!   rt   arccoshr   r   r   r   r   r   r   test_math_acosh_     zTestCudaMath.test_math_acoshc                 C   r   rr   )r   r#   rt   arcsinhr   r   r   r   r   r   r   test_math_asinhh  r   zTestCudaMath.test_math_asinhc                 C   r   )Nr   g?r   )r   r%   rt   arctanhr   r   r   r   r   r   r   test_math_atanhq  r   zTestCudaMath.test_math_atanhc                 C   r   rr   )r   r'   rt   r&   r   r   r   r   r   r   r   test_math_cosz  r   zTestCudaMath.test_math_cosc                 C   s   |  ttj |  ttj |  ttj | j ttj	dd | j t
tjdd | j ttjdd | j ttjdd |  ttj |  ttj |  ttj d S )Nr   rz   )r|   r)   rt   r(   r'   r&   r9   r8   rG   rF   rI   rH   rK   rJ   rA   r@   rQ   rP   rW   rV   rY   rX   r   r   r   r   test_math_fp16  s   zTestCudaMath.test_math_fp16z(numpy does not support trunc for float16c                 C      |  ttj d S rr   )r|   rk   rt   rj   r   r   r   r   test_math_fp16_trunc  s   z!TestCudaMath.test_math_fp16_truncc                 C   r   rr   )r   r)   rt   r(   r   r   r   r   r   r   r   test_math_sin  r   zTestCudaMath.test_math_sinc                 C   r   rr   )r   r+   rt   r*   r   r   r   r   r   r   r   test_math_tan  r   zTestCudaMath.test_math_tanc                 C   r   rr   )r   r-   rt   r,   r   r   r   r   r   r   r   test_math_cosh  r   zTestCudaMath.test_math_coshc                 C   r   rr   )r   r/   rt   r.   r   r   r   r   r   r   r   test_math_sinh  r   zTestCudaMath.test_math_sinhc                 C   r   rr   )r   r1   rt   r0   r   r   r   r   r   r   r   test_math_tanh  r   zTestCudaMath.test_math_tanhc                 C   r   rr   )r   r6   rt   arctan2r   r   r   r   r   r   r   test_math_atan2  r   zTestCudaMath.test_math_atan2c                 C   @   t dd }| t| | t| | t| | t| d S )Nc                 S   
   t | S rr   )r   r:   xr   r   r   ufunc     
z)TestCudaMath.test_math_erf.<locals>.ufunc)r   r   r;   r   r   r   rw   r   r   r   r   test_math_erf     
zTestCudaMath.test_math_erfc                 C   r   )Nc                 S   r   rr   )r   r<   r   r   r   r   r     r   z*TestCudaMath.test_math_erfc.<locals>.ufunc)r   r   r=   r   r   r   r   r   r   r   test_math_erfc  r   zTestCudaMath.test_math_erfcc                 C   r   rr   )r   r9   rt   r8   r   r   r   r   r   r   r   test_math_exp  r   zTestCudaMath.test_math_expc                 C   r   rr   )r   r?   rt   r>   r   r   r   r   r   r   r   test_math_expm1  r   zTestCudaMath.test_math_expm1c                 C   L   | j ttjdd | jttjdd | jttjdd | jttjdd d S Nr   r   )r   rA   rt   r@   r   r   r   r   r   r   r   test_math_fabs     zTestCudaMath.test_math_fabsc                 C   P   t dd }| jt|dd | jt|dd | jt|dd | jt|dd d S )Nc                 S   r   rr   )r   rB   r   r   r   r   r     r   z+TestCudaMath.test_math_gamma.<locals>.ufunc皙?r   r   )r   r   rC   r   r   r   r   r   r   r   test_math_gamma     
zTestCudaMath.test_math_gammac                 C   r   )Nc                 S   r   rr   )r   rD   r   r   r   r   r     r   z,TestCudaMath.test_math_lgamma.<locals>.ufuncr   r   r   )r   r   rE   r   r   r   r   r   r   r   test_math_lgamma
  r   zTestCudaMath.test_math_lgammac                 C   r   Nr   r   )r   rG   rt   rF   r   r   r   r   r   r   r   test_math_log  r   zTestCudaMath.test_math_logc                 C   r   r   )r   rI   rt   rH   r   r   r   r   r   r   r   test_math_log2  r   zTestCudaMath.test_math_log2c                 C   r   r   )r   rK   rt   rJ   r   r   r   r   r   r   r   test_math_log10(  r   zTestCudaMath.test_math_log10c                 C   r   rr   )r   rM   rt   rL   r   r   r   r   r   r   r   test_math_log1p1  r   zTestCudaMath.test_math_log1pc                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S )Ngdy=r   r   )r   rO   rt   rN   r   r   r   r   r   r   r   test_math_remainder:  r   z TestCudaMath.test_math_remainderz3math.remainder(0, 0) raises a ValueError on CUDASimc                 C   sZ   t ttd d d ttdd }tdtj}|d |dd | t|d  d S )Nr   c                 S   s   t ||| d< d S )Nr   )r   rN   )rr   yr   r   r   test_0_0B  s   z6TestCudaMath.test_math_remainder_0_0.<locals>.test_0_0)r   r   r   )	r   r   r   r	   r   rt   zeros
assertTruer`   )rw   r   r   r   r   r   test_math_remainder_0_0@  s
   
z$TestCudaMath.test_math_remainder_0_0c                 C   r   rr   )r   rQ   rt   rP   r   r   r   r   r   r   r   test_math_sqrtL  r   zTestCudaMath.test_math_sqrtc                 C   r   rr   )r   rS   rt   rR   r   r   r   r   r   r   r   test_math_hypotU  r   zTestCudaMath.test_math_hypotc           
      C   s   d}t dd||}t j|t jd}t |}t|d d d }t	|td d d |ft
}|d|f ||| t |}tt|D ]}	t||	 ||	 ||	< qGt jjt ||||dd d S )Nr   r      r   r   r   r   )rt   r   r   aranger
   r   r   r   r   r   rU   rangelenr   rT   r   r   power)
rw   r   r   r   r   r5   r   r   Crefr   r   r   r   pow_template_int32^  s   

$zTestCudaMath.pow_template_int32c                 C   s8   |  ttj | ttj | tj | tj d S rr   )r   rU   rt   r  r   r  r   r	   r   r   r   r   test_math_powo  s   zTestCudaMath.test_math_powc                 C       |  ttj | ttj d S rr   )r   rm   rt   r  r   r   r   r   r   test_math_pow_binopx     z TestCudaMath.test_math_pow_binopc                 C   r   rr   )r   rW   rt   rV   r   r   r   r   r   r   r   test_math_ceil  r   zTestCudaMath.test_math_ceilc                 C   r   rr   )r   rY   rt   rX   r   r   r   r   r   r   r   test_math_floor  r   zTestCudaMath.test_math_floorc                 C   r   rr   )r   rk   rt   rj   r   r   r   r   test_math_trunc  s   zTestCudaMath.test_math_truncz%trunc only supported on NumPy float64c                 C   s.   |  ttj | ttj | ttj d S rr   )r   rk   rt   rj   r   r   r   r   r   r   test_math_trunc_non_float64  s   z(TestCudaMath.test_math_trunc_non_float64c                 C   (   | j ttjdd | jttjdd d S r   )r   r[   rt   rZ   r   r   r   r   r   test_math_copysign     zTestCudaMath.test_math_copysignc                    s   fdd} fdd}d}  d |tdd|tjtd d  d	 W d    n1 s/w   Y    d
 |ttjtj gtjtd d  d	 W d    n1 sXw   Y    d |tjtd d  d	 W d    n1 sxw   Y    d |tdd|tjtd d  d	 W d    n1 sw   Y    d |ttjtj gtjtd d  d	 W d    n1 sw   Y    d |tjtd d  d	 W d    d S 1 sw   Y  d S )Nc                    sv   t jt jg| d}t |}t |}t|||ft}|dt|f |||  t 	|  t 	| d S )Nr   r   )
rt   r   r   
zeros_liker   r   r_   r  r   r`   )r   r   r   r   r5   r   r   r   r   modf_template_nan  s   

z6TestCudaMath.test_math_modf.<locals>.modf_template_nanc                    s   |  |} t| }t| }t|||ft}|dt| f | || t| \}} t	||  t	|| d S r   )
r   rt   r  r   r   r_   r  r^   r   array_equal)r   r   r   r   r5   r   DEr   r   r   modf_template_compare  s   


z:TestCudaMath.test_math_modf.<locals>.modf_template_comparer   zfloat32 modf on simple floatr   
   )r   r   zfloat32 modf on +- infinityzfloat32 modf on nanzfloat64 modf on simple floatzfloat64 modf on +- infinityzfloat64 modf on nan)subTestrt   r   r   r   r   r	   )rw   r  r  r   r   r   r   test_math_modf  s:   	




"zTestCudaMath.test_math_modfc                 C   r  r   )r   r]   rt   r\   r   r   r   r   r   test_math_fmod  r  zTestCudaMath.test_math_fmodc                 C   r  r   )r   ro   rt   r\   r   r   r   r   r   test_math_mod_binop  r  z TestCudaMath.test_math_mod_binopc                 C   X   |  ttj | ttj | ttj | ttj | ttj | ttj d S rr   )	r   ra   rt   r`   r   r   r   r   r   r   r   r   r   test_math_isnan     zTestCudaMath.test_math_isnanc                 C   r  rr   )	r   rc   rt   rb   r   r   r   r   r   r   r   r   r   test_math_isinf  r  zTestCudaMath.test_math_isinfc                 C   r  rr   )	r   re   rt   rd   r   r   r   r   r   r   r   r   r   test_math_isfinite  r  zTestCudaMath.test_math_isfinitec                 C   r  rr   )r   rg   rt   rf   r   r   r   r   r   test_math_degrees  r
  zTestCudaMath.test_math_degreesc                 C   r  rr   )r   ri   rt   rh   r   r   r   r   r   test_math_radians  r
  zTestCudaMath.test_math_radiansN)r   r   )r   r   )r   r   )F__name__
__module____qualname__r|   r   r   r   r   rs   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   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  r  r  r  r  r  r   r!  r"  r#  r   r   r   r   rp      s    













				
													
				
,rp   __main__)>numpyrt   numba.cuda.testingr   r   r   r   numba.npr   numbar   r   r	   r
   r   r   r   r   r   r   r   r!   r#   r%   r'   r)   r+   r-   r/   r1   r6   r9   r;   r=   r?   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   rg   ri   rk   rm   ro   rp   r$  mainr   r   r   r   <module>   sl    $    =