o
    0 in=                  	   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZ d dlmZmZmZ d dlZeejdZdddd	Zd
d
dd	ZdddZi deje jgdeje jgdg deje jgdeje jgde jgde jgde jgde jgdg de j gde j!gdg dg dg dg dg i dej"e j"gde j#gd e j$gd!g d"g d#g d$g d%ej%e j%gd&g d'g d(g d)g d*ej&e j&gd+ej'e j'gd,g d-g d.g g g g ej(e j(gej)e j)gej*e j*gg d/Z+g d0Z,d1d2 e+- D Z+d3d4 e+- D Z.d5d4 e+- D Z/dFd7d8Z0d9d: Z1d;d< Z2eG d=d> d>eZ3e34  d?d@ Z5dAdB Z6eG dCdD dDeZ7e8dEkrWe9  dS dS )G    N)njitprange)config)TestCasetagoverride_env_configz"SVML tests need SVML to be presentZnehalemZhaswellzskylake-avx512)          z-prefer-256-bit)NUMBA_CPU_NAMENUMBA_CPU_FEATURESsincospowexplogacosacoshasinasinhatan2atanatanhZcbrtZcdfnormZ
cdfnorminvceilZcosdcosherferfcZerfcinvZerfinvZexp10Zexp2expm1floorfmodhypotZinvsqrtlog10log1plog2logbZ	nearbyint)ZrintroundZsindsinhtantanhtrunc)r(   r"   r   r#   r)   r   c                 C   s"   i | ]\}}t |d kr||qS )r   )len).0kv r/   a/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/numba/tests/test_svml.py
<dictcomp>U   s   " r1   c                 C   &   g | ]\}}d dd |D v r|qS )z<ufuncc                 S      g | ]}t |d d qS  r   strsplitr,   pr/   r/   r0   
<listcomp>X       <listcomp>.<listcomp>r/   r,   fr.   r/   r/   r0   r;   W       r;   c                 C   r2   )z	<built-inc                 S   r3   r4   r6   r9   r/   r/   r0   r;   Z   r<   r=   r/   r>   r/   r/   r0   r;   Y   r@           c                 C   s\  |dkrd dd |D }d||| |f }	n:|dkr4d||| d |f }	|	|dr0d	| nd
7 }	n|dks>|dks>J d dd |D }djdi t }	|dkpX|dk}
|
r_| d n| }|
rg|d n|}|rmdnd}tjrvd| nd| }d|||f }|dkr|g}d|g}n|g}g }|dkr|
s|dkr|dd||d |f g7 }|	||fS )z
    For a given function and its usage modes,
    returns python code and assembly patterns it should and should not generate
    scalar,c                 S      g | ]}|d  qS )z[0]r/   r,   ar/   r/   r0   r;   e       z!func_patterns.<locals>.<listcomp>z%s%s[0] += math.%s(%s)
numpyz%s%s += np.%s(%s)intz.astype(np.%s)

ranger   c                 S   rD   )z[i]r/   rE   r/   r/   r0   r;   l   rG   zP{pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
float32	complex64r?   r   r   Z_haz$_$z__svml_%s%d%s,Z__svml_r
   int32z%zmmNr/   )join
startswithformatlocalsr   IS_OSX)funcargsresdtypemodevlenfastmathpadarg_listbodyZis_f32r?   r.   Z	prec_suffZscalar_funcZ	svml_funccontainsavoidsr/   r/   r0   func_patterns]   s6   

ra   c                 C   s   |  d| | d| S )z, Returns pretty name for given set of modes _r/   )rX   rY   rZ   namer/   r/   r0   usecase_name   s   rd   c              	   C   s   t | |||}djdi t }t|dkrtnt}| dr$|t}t }t }|D ]}	t	|	dgd| |||\}
}}|
| ||
7 }|
| q,|d7 }i }t|t | ||| _|| ||fS )	z? Combine multiple function calls under single umbrella usecase z\def {name}(n):
        x   = np.empty(n*8, dtype=np.{dtype})
        ret = np.empty_like(x)
rH   complexxretz        return retNr/   )rd   rR   rS   setnumpy_funcsother_funcsrQ   
differencecomplex_funcs_excludera   updateexecglobals__doc__)rX   rY   rZ   r[   rc   r^   funcsr_   r`   r?   bcrF   Zldictr/   r/   r0   combo_svml_usecase   s*   



rt   c                   @   sR   e Zd ZdZdZeddgee	 Z
edd Zedd Zed	d
 ZdS )TestSVMLGenerationz; Tests all SVML-generating functions produce desired calls F|z\$[a-z_]\w+,c                 C   sL   t | |}z| \}}W n ty   t }d}Y nw |||d d S )NF)statusmsg)getattr	Exception	traceback
format_excput)clstestnameZoutqueuemethodokrx   r/   r/   r0   	mp_runner   s   
zTestSVMLGeneration.mp_runnerc           	         s    drdkrd S  dodk}tjft fdd}td }d| t | t| d	fd
d}t d| | d S )Nre   rH   rI   r   c            	   	      s,  t d d \} }}tdt 9 tdt " ztd d d| }W n
   td| j W d    n1 sCw   Y  W d    n1 sRw   Y  |  fdd	|D } fd
d	|D }| os| }dfdd	 	dD }d| d| d| d| j }||fS )Nr[   rc   r   r   error_modelr[   r   zraised while compiling c                    s   g | ]}| vr|qS r/   r/   r,   patternasmr/   r0   r;          zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>c                    s   g | ]}| v r|qS r/   r/   r   r   r/   r0   r;      r   rJ   c                    s$   g | ]} j |rd |vr|qS )")
asm_filtersearch)r,   line)r~   r/   r0   r;      s    
zWhile expecting z	 and not z,
it contains:
z
when compiling )
rt   r   vlen2cpuvlen2cpu_featuresr   rz   rp   Zinspect_asmrP   r8   )	fnr_   r`   Z	jitted_fnmissedfoundr   detailrx   )r~   rX   flagsrY   sigrZ   r   r0   run_template   sB   

 
z5TestSVMLGeneration._inject_test.<locals>.run_templaterc   Zrun_zNot implementedc           	         s   t d}| }|jt| j |gd}|  |jdd}|j}|d u r>|d u r0| 	d n|dk r>| 	d|  d | j
|dd	d
 | }|d }|d }| j||d
 d S )NZspawn)targetrV      )timeoutzProcess timed out.r   zProcess terminated with signal .zprocess ended unexpectedly)rx   rw   rx   )mpZget_contextQueueProcesstyper   startrP   exitcodeZfailassertEqualget
assertTrue)	selfctxqr:   Zterm_or_timeoutr   outrw   rx   )r   r/   r0   test_runner   s    
z4TestSVMLGeneration._inject_test.<locals>.test_runnerZtest_)rQ   numbaZint64staticmethodrd   setattrunittest
skipUnless)	r~   rX   rY   rZ   r   Zskippedr   Zpostfixr   r/   )r~   rX   r   rY   r   r   rZ   r0   _inject_test   s   
zTestSVMLGeneration._inject_testc                 C   s|   ddddddddg}dD ]}t D ]}|D ]}dD ]}| |||t| qqqqd	D ]}t| |td
t| | q,d S )NFrH   Zusecase)r[   r   rc   TZfastmath_usecase)rM   float64rL   rO   )rB   rK   r   rH   )Ztest_int32_range4_usecaseZ	important)r   r   dictr   r   ry   )r~   Z	flag_listrX   rZ   r   rY   nr/   r/   r0   autogenerate   s$   zTestSVMLGeneration.autogenerateN)__name__
__module____qualname__rp   _numba_parallel_test_recompilerP   list
svml_funcsr   classmethodr   r   r   r/   r/   r/   r0   ru      s    
	
@ru   c                 C   s
   t | S N)mathr   )rf   r/   r/   r0   math_sin_scalar  s   
r   c                 C   6   t j| t jd}t| D ]}tt |||< q|S NrX   npemptyr   rK   r   r   r   rg   rf   r/   r/   r0   math_sin_loop     r   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZddddZ	dd Z
ejeddd Zdd Zejeddd Zdd Zejddieddd Zdd Zd S )!TestSVMLz  Tests SVML behaves as expected Fc                 O   sJ   |rJ t dd |D }t||}t|dd|}|j| |j| fS )Nc                 S   s   g | ]}t |qS r/   )r   Ztypeof)r,   rf   r/   r/   r0   r;   %  s    z$TestSVML.compile.<locals>.<listcomp>Tr[   )tupler   	overloads)r   rU   rV   kwargsr   stdfastr/   r/   r0   r   #  s
   zTestSVML.compilec                 G   sx   |st  S g }|D ].}t|tjr||d q	t|tjr(||  q	t|tjr4|| q	t	dt |S )Nr-   z%Unsupported argument type encountered)
r   
isinstancer   ZndarrayappendcopynumbernumbersNumber
ValueError)r   rV   new_argsrf   r/   r/   r0   	copy_args,  s   zTestSVML.copy_argsc           	      O   st   | j |g|R  \}}|| j|  }|j| j|  }|j| j|  }tjj||fi | tjj||fi | d S r   )r   r   entry_pointr   testingZassert_almost_equal)	r   pyfuncrV   r   jitstdjitfastZpy_expectedZjitstd_resultZjitfast_resultr/   r/   r0   check_result;  s   zTestSVML.check_resultc                 O   sV   | dd }| dd }| j|g|R  \}}|r| || |r)| || d S d S )Nstd_patternfast_pattern)popr   check_svml_presence)r   r   rV   r   r   r   r   r   r/   r/   r0   	check_asmK  s   zTestSVML.check_asmbothwhatc                O   sd   |dv sJ |dks|dkr| j |g|R i | |dks"|dkr0| j|g|R i | d S d S )N)r   resultr   r   r   r   )r   r   )r   r   r   rV   r   r/   r/   r0   checkW  s   zTestSVML.checkc                 C   s   |j  }| || d S r   )libraryget_asm_strZassertIn)r   rU   r   r   r/   r/   r0   r   ^  s   
zTestSVML.check_svml_presence)envvarsc                 C   s6   t jrdnd}| jtdd|d | jtdd|d d S )Nz$_sinz$sin      @r   )r   r   )r   r   )r   rT   r   r   )r   patr/   r/   r0   test_scalar_context_asmb  s   z TestSVML.test_scalar_context_asmc                 C      | j tddd d S )Nr   r   r   )r   r   r   r/   r/   r0   test_scalar_context_resulti     z#TestSVML.test_scalar_context_resultc                 C   s    d}d}| j tdd||d d S )Nz__svml_sin8_ha,z__svml_sin8,
   r   )r   r   r   r   r   )r   r   r   r/   r/   r0   test_svml_asmm  s
   
zTestSVML.test_svml_asmc                 C   r   )Nr   r   r   r   r   r/   r/   r0   test_svml_resultv  r   zTestSVML.test_svml_resultZNUMBA_DISABLE_INTEL_SVML1c                 C   sd   dd }t jf}t||}t|dd|}|j| |j| f}|D ]}|j }| d| q"d S )Nc                 S   r   r   r   r   r/   r/   r0   r   ~  r   z2TestSVML.test_svml_disabled.<locals>.math_sin_loopTr   Z
__svml_sin)r   rO   r   r   r   r   ZassertNotIn)r   r   r   r   r   fnsr   r   r/   r/   r0   test_svml_disabledz  s   
zTestSVML.test_svml_disabledc                 C   s<   t dhdddd }|d | d||jd v  d S )	Nr   rH   r   c                 S   sL   t j| d t jd}t |}t|jD ]}||  t|| 7  < q|S )Nr
   r   )r   r   r   Z
empty_likerK   sizer   r   )r   rf   rg   ir/   r/   r0   impl  s
   
z@TestSVML.test_svml_working_in_non_isolated_context.<locals>.impl   Zintel_svmlccr   )r   r   Zinspect_llvmZ
signatures)r   r   r/   r/   r0   )test_svml_working_in_non_isolated_context  s   
z2TestSVML.test_svml_working_in_non_isolated_contextN)r   r   r   rp   r   r   r   r   r   r   r   r   Zrun_test_in_subprocess_skylake_axv512_envvarsr   r   r   r   r   r   r/   r/   r/   r0   r     s(    	





r   __main__)rA   ):r   rH   r   r   r   r{   multiprocessingr   r   r   r   Z
numba.corer   Znumba.tests.supportr   r   r   r   r   Z
USING_SVMLZ
needs_svmlr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r#   r'   r(   r)   r   rl   itemsri   rj   ra   rd   rt   ru   r   r   r   r   r   mainr/   r/   r/   r0   <module>   s    	
 !"#


,
+e
}