o
    ۾i                      @   s   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlmZmZmZ d dlZeG d	d
 d
ZedG dd deZe dkrke!  dS dS )    )List)	dataclassfield)cudafloat32)compile_ptx_for_current_devicecompile_ptx)	cossintanexploglog10log2powtanh)truedivN)CUDATestCaseskip_on_cudasimskip_unless_cc_75c                   @   s|   e Zd ZU eedZee ed< eedZ	ee ed< eedZ
ee ed< eedZee ed< dededefd	d
ZdS )FastMathCriterion)default_factoryfast_expectedfast_unexpectedprec_expectedprec_unexpectedtestfastprecc                    s|   | t fdd| jD  | t fdd| jD  | tfdd| jD  | tfdd| jD  d S )Nc                 3       | ]}| v V  qd S N .0ir   r!   Y/home/ubuntu/.local/lib/python3.10/site-packages/numba/cuda/tests/cudapy/test_fastmath.py	<genexpr>       z*FastMathCriterion.check.<locals>.<genexpr>c                 3       | ]}| vV  qd S r    r!   r"   r%   r!   r&   r'      r(   c                 3   r   r    r!   r"   r   r!   r&   r'      r(   c                 3   r)   r    r!   r"   r*   r!   r&   r'      r(   )
assertTrueallr   r   r   r   )selfr   r   r   r!   )r   r   r&   check   s   "zFastMathCriterion.checkN)__name__
__module____qualname__r   listr   r   str__annotations__r   r   r   r   r.   r!   r!   r!   r&   r      s   
 r   z4Fastmath and PTX inspection not available on cudasimc                   @   s   e Zd Zdd ZdefddZdefddZdd	 Zd
d Zdd Z	e
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ejd d! Zd"S )#TestFastMathOptionc           
      C   sx   t j||dd|}t j||d|}|| |||| t|||dd\}}t|||d\}	}|| ||	 d S )NT)devicefastmathr6   )r   jitr.   inspect_asmr   )
r-   pyfuncsigr6   	criterionfastverprecverfastptx_precptxr!   r!   r&   _test_fast_math_common   s   

z)TestFastMathOption._test_fast_math_commonr=   c                    sP    fdd} fdd}| j |td d d tfd|d | j |tfd|d d S )	Nc                    s    || d< d S Nr   r!   rxopr!   r&   kernel2      z8TestFastMathOption._test_fast_math_unary.<locals>.kernelc                    s    | S r    r!   )rG   rH   r!   r&   device_function5   s   zATestFastMathOption._test_fast_math_unary.<locals>.device_function   Fr6   r=   TrC   r   )r-   rI   r=   rJ   rL   r!   rH   r&   _test_fast_math_unary1   s   

z(TestFastMathOption._test_fast_math_unaryc                    sT    fdd} fdd}| j |td d d ttfd|d | j |ttfd|d d S )	Nc                    s    ||| d< d S rD   r!   rF   rG   yrH   r!   r&   rJ   @   s   z9TestFastMathOption._test_fast_math_binary.<locals>.kernelc                    s
    | |S r    r!   )rG   rR   rH   r!   r&   r6   C   s   
z9TestFastMathOption._test_fast_math_binary.<locals>.devicerM   FrN   TrO   )r-   rI   r=   rJ   r6   r!   rH   r&   _test_fast_math_binary?   s   
z)TestFastMathOption._test_fast_math_binaryc                 C      |  ttdgdgd d S )Ncos.approx.ftz.f32 r   r   )rP   r	   r   r-   r!   r!   r&   	test_cosfN      zTestFastMathOption.test_cosfc                 C   rT   )Nsin.approx.ftz.f32 rV   )rP   r
   r   rW   r!   r!   r&   	test_sinfW   rY   zTestFastMathOption.test_sinfc                 C   s   |  ttg ddgd d S )N)rZ   rU   div.approx.ftz.f32 rZ   rV   )rP   r   r   rW   r!   r!   r&   	test_tanf`   s   zTestFastMathOption.test_tanfc                 C   rT   )Ntanh.approx.f32 rV   )rP   r   r   rW   r!   r!   r&   
test_tanhfj   s   zTestFastMathOption.test_tanhfc                    sL   dd  fdd}|dt dgdgdd |d	t d
dgdgdd d S )Nc                 S   s   t || d< d S rD   )r   rE   r!   r!   r&   tanh_kernelv   rK   z>TestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_kernelc                    sT   t td d d tfd| d\}}t td d d tf| d\}}| || d S )NrM   T)r7   cc)ra   )r   r   r.   )ra   r=   r@   rA   rB   r-   r`   r!   r&   tanh_common_testy   s   

zCTestFastMathOption.test_tanhf_compile_ptx.<locals>.tanh_common_test)      r^   rV   )ra   r=   )rd   r   zex2.approx.ftz.f32 zrcp.approx.ftz.f32 )r   )r-   rc   r!   rb   r&   test_tanhf_compile_ptxu   s   

z)TestFastMathOption.test_tanhf_compile_ptxc                 C   rT   )Nzfma.rn.f32 )r   r   )rP   r   r   rW   r!   r!   r&   	test_expf   rY   zTestFastMathOption.test_expfc                 C      |  ttddgdgd d S )Nlg2.approx.ftz.f32 
0f3F317218rV   )rP   r   r   rW   r!   r!   r&   	test_logf      zTestFastMathOption.test_logfc                 C   rh   )Nri   
0f3E9A209BrV   )rP   r   r   rW   r!   r!   r&   test_log10f   rl   zTestFastMathOption.test_log10fc                 C   rT   Nri   rV   )rP   r   r   rW   r!   r!   r&   
test_log2f      zTestFastMathOption.test_log2fc                 C   rT   ro   )rS   r   r   rW   r!   r!   r&   	test_powf   rq   zTestFastMathOption.test_powfc              	   C   s$   |  ttdgdgdgdgd d S )Nr\   
div.rn.f32)r   r   r   r   )rS   r   r   rW   r!   r!   r&   	test_divf   s   zTestFastMathOption.test_divfc                 C   s   |  d dd }td d d ttf}tj|ddd|}tj|dd|}d}tj|tjd	}| t |d|f |d
d W d    n1 sJw   Y  z|d|f |d
d W d S  tyk   | d Y d S w )Nz!Exceptions not supported with LTOc                 S   s   || | d< d S rD   r!   rQ   r!   r!   r&   f10   rK   z3TestFastMathOption.test_divf_exception.<locals>.f10rM   T)r7   debug)rv   
   )dtypeg      $@g        z5Divide in fastmath should not throw ZeroDivisionError)	skip_if_ltor   r   r9   npemptyassertRaisesZeroDivisionErrorfail)r-   ru   r<   r>   r?   nelemaryr!   r!   r&   test_divf_exception   s   
z&TestFastMathOption.test_divf_exceptionc                    s   t jddddd   fdd}td d d tf}t j|dd	|}t ||}| d
|| | d|| | d
|| | d|| d S )Nzfloat32(float32, float32)Tr8   c                 S   s   | | S r    r!   )abr!   r!   r&   foo   s   z@TestFastMathOption.test_device_fastmath_propagation.<locals>.fooc                    s*   t d}|| jk r ||| |< d S d S )NrM   )r   gridsize)arrvalr$   r   r!   r&   bar   s   

z@TestFastMathOption.test_device_fastmath_propagation.<locals>.barrM   )r7   zdiv.approx.f32rs   zdiv.full.f32)r   r9   r   assertInr:   assertNotIn)r-   r   r<   r>   r?   r!   r   r&    test_device_fastmath_propagation   s   
z3TestFastMathOption.test_device_fastmath_propagationN)r/   r0   r1   rC   r   rP   rS   rX   r[   r]   r   r_   rf   rg   rk   rn   rp   rr   rt   r   unittestexpectedFailurer   r!   r!   r!   r&   r5      s&    		


			
r5   __main__)"typingr   dataclassesr   r   numbar   r   numba.cuda.compilerr   r   mathr	   r
   r   r   r   r   r   r   r   operatorr   numpyrz   numba.cuda.testingr   r   r   r   r   r5   r/   mainr!   r!   r!   r&   <module>   s"    , X