o
    i!C                  	   @   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Zd dl	Z
d dlmZmZ d dl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 d dlZeejdZdd	d
dZd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j0e j0gd-g d.g d/g g g g ej1e j1gej2e j2gej3e j3gg d0Z4g d1Z5d2d3 e46 D Z4d4d5 e46 D Z7d6d5 e46 D Z8dGd8d9Z9d:d; Z:d<d= Z;eG d>d? d?eZ<e<=  d@dA Z>dBdC Z?eG dDdE dEeZ@eAdFkrpeB  dS dS )H    N)chaincombinations)configcpu)prangenjit)compile_isolatedFlags)TestCasetagoverride_env_configz"SVML tests need SVML to be presentZnehalemZhaswellskylake-avx512)          -prefer-256-bit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 r4   d/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/numba/tests/test_svml.py
<dictcomp>T   s   " r6   c                 C   &   g | ]\}}d dd |D v r|qS )z<ufuncc                 S      g | ]}t |d d qS  r   strsplitr1   pr4   r4   r5   
<listcomp>W       <listcomp>.<listcomp>r4   r1   fr3   r4   r4   r5   r@   V       r@   c                 C   r7   )z	<built-inc                 S   r8   r9   r;   r>   r4   r4   r5   r@   Y   rA   rB   r4   rC   r4   r4   r5   r@   X   rE           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]r4   r1   ar4   r4   r5   r@   d       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   rI   )z[i]r4   rJ   r4   r4   r5   r@   k   rL   zP{pad}for i in {mode}({res}.size):
{pad}{pad}{res}[i] += math.{func}({arg_list})
float32	complex64rD   r   r   Z_haz$_$z__svml_%s%d%s,Z__svml_r   int32z%zmmNr4   )join
startswithformatlocalsr   IS_OSX)funcargsresdtypemodevlenfastmathpadarg_listbodyZis_f32rD   r3   Z	prec_suffZscalar_funcZ	svml_funccontainsavoidsr4   r4   r5   func_patterns\   s6   

rf   c                 C   s   |  d| | d| S )z, Returns pretty name for given set of modes _r4   )r]   r^   r_   namer4   r4   r5   usecase_name   s   ri   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)
rM   complexxretz        return retNr4   )ri   rW   rX   setnumpy_funcsother_funcsrV   
differencecomplex_funcs_excluderf   updateexecglobals__doc__)r]   r^   r_   r`   rh   rc   funcsrd   re   rD   bcrK   Zldictr4   r4   r5   combo_svml_usecase   s*   



ry   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okr}   r4   r4   r5   	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 )Nrj   rM   rN   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`   rh   NUMBA_CPU_NAMENUMBA_CPU_FEATURESerror_modelr`   r   zraised while compiling c                    s   g | ]}| vr|qS r4   r4   r1   patternasmr4   r5   r@          zITestSVMLGeneration._inject_test.<locals>.run_template.<locals>.<listcomp>c                    s   g | ]}| v r|qS r4   r4   r   r   r4   r5   r@      r   rO   c                    s$   g | ]} j |rd |vr|qS )")
asm_filtersearch)r1   line)r   r4   r5   r@      s    
zWhile expecting z	 and not z,
it contains:
z
when compiling )
ry   r   vlen2cpuvlen2cpu_featuresr   r   ru   Zinspect_asmrU   r=   )	fnrd   re   Z	jitted_fnmissedfoundr   detailr}   )r   r]   flagsr^   sigr_   r   r5   run_template   sB   

 
z5TestSVMLGeneration._inject_test.<locals>.run_templaterh   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)targetr[      )timeoutzProcess timed out.r   zProcess terminated with signal .zprocess ended unexpectedly)r}   r|   r}   )mpZget_contextQueueProcesstyper   startrU   exitcodeZfailassertEqualget
assertTrue)	selfctxqr?   Zterm_or_timeoutr   outr|   r}   )r   r4   r5   test_runner   s    
z4TestSVMLGeneration._inject_test.<locals>.test_runnerZtest_)rV   numbaZint64staticmethodri   setattrunittest
skipUnless)	r   r]   r^   r_   r   Zskippedr   Zpostfixr   r4   )r   r]   r   r^   r   r   r_   r5   _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 )NFrM   Zusecase)r`   r   rh   TZfastmath_usecase)rR   float64rQ   rT   )rG   rP   r   rM   )Ztest_int32_range4_usecaseZ	important)r   r   dictr   r   r~   )r   Z	flag_listr]   r_   r   r^   nr4   r4   r5   autogenerate   s$   zTestSVMLGeneration.autogenerateN)__name__
__module____qualname__ru   _numba_parallel_test_recompilerU   list
svml_funcsr   classmethodr   r   r   r4   r4   r4   r5   rz      s    
	
@rz   c                 C   s
   t | S N)mathr   )rk   r4   r4   r5   math_sin_scalar  s   
r   c                 C   s6   t j| t jd}t| D ]}tt |||< q|S )Nr]   )npemptyr   rP   r   r   )r   rl   rk   r4   r4   r5   math_sin_loop  s   r   c                       sd   e Zd ZdZdZ f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  ZS )TestSVMLz  Tests SVML behaves as expected Fc                    sB   t  | _d| j_t  | _d| j_td| j_tt| j	|  d S )NT)
r	   r   Znrt	fastflagsr   ZFastMathOptionsr`   superr   __init__)r   r[   	__class__r4   r5   r   "  s   zTestSVML.__init__c                 O   sB   |rJ t dd |D }t||| jd}t||| jd}||fS )Nc                 S   s   g | ]}t |qS r4   )r   Ztypeof)r1   rk   r4   r4   r5   r@   .  s    z$TestSVML.compile.<locals>.<listcomp>)r   )tupler   r   r   )r   rZ   r[   kwargsr   stdfastr4   r4   r5   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 )Nr2   z%Unsupported argument type encountered)
r   
isinstancer   ZndarrayappendcopynumbernumbersNumber
ValueError)r   r[   new_argsrk   r4   r4   r5   	copy_args5  s   zTestSVML.copy_argsc              	   O   s@  | j |g|R  \}}|dd }|dd }|dd}|dd}	|| j|  }
|j| j|  }|j| j|  }tjj||
fi | tjj||
fi | td|A td|	# | j |g|R  \}}|ro| || |rw| || W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )	Nstd_patternfast_patterncpu_namer   cpu_featuresr   r   r   )	r   popr   entry_pointr   testingZassert_almost_equalr   check_svml_presence)r   Zpyfuncr[   r   ZjitstdZjitfastr   r   r   r   Zpy_expectedZjitstd_resultZjitfast_resultr4   r4   r5   checkD  s(   PzTestSVML.checkc                 C   s   |j  }| || d S r   )libraryZget_asm_strZassertIn)r   rZ   r   r   r4   r4   r5   r   f  s   
zTestSVML.check_svml_presencec                 C   s2   t jrdnd}| jtd|d | jtd|d d S )Nz$_sinz$sing      @)r   )r   )r   rY   r   r   )r   patr4   r4   r5   test_scalar_contextj  s   zTestSVML.test_scalar_contextc                 C   s   d}d}| j td||d d S )Nz__svml_sin8_ha,z__svml_sin8,
   )r   r   )r   r   )r   r   r   r4   r4   r5   	test_svmlp  s   zTestSVML.test_svmlc                 C   sP   d}t jtjd|gt jt jd}| \}}|jdkr&td|j| f d S )Na  if 1:
            import os
            import numpy as np
            import math

            def math_sin_loop(n):
                ret = np.empty(n, dtype=np.float64)
                for x in range(n):
                    ret[x] = math.sin(np.float64(x))
                return ret

            def check_no_svml():
                try:
                    # ban the use of SVML
                    os.environ['NUMBA_DISABLE_INTEL_SVML'] = '1'

                    # delay numba imports to account for env change as
                    # numba.__init__ picks up SVML and it is too late by
                    # then to override using `numba.config`
                    import numba
                    from numba import config
                    from numba.core import cpu
                    from numba.tests.support import override_env_config
                    from numba.core.compiler import compile_isolated, Flags

                    # compile for overridden CPU, with and without fastmath
                    with override_env_config('NUMBA_CPU_NAME', 'skylake-avx512'),                          override_env_config('NUMBA_CPU_FEATURES', ''):
                        sig = (numba.int32,)
                        f = Flags()
                        f.nrt = True
                        std = compile_isolated(math_sin_loop, sig, flags=f)
                        f.fastmath = cpu.FastMathOptions(True)
                        fast = compile_isolated(math_sin_loop, sig, flags=f)
                        fns = std, fast

                        # assert no SVML call is present in the asm
                        for fn in fns:
                            asm = fn.library.get_asm_str()
                            assert '__svml_sin' not in asm
                finally:
                    # not really needed as process is separate
                    os.environ['NUMBA_DISABLE_INTEL_SVML'] = '0'
                    config.reload_config()
            check_no_svml()
            z-c)stdoutstderrr   z/process failed with code %s: stderr follows
%s
)	
subprocessPopensys
executablePIPEcommunicate
returncodeAssertionErrordecode)r   codepopenr   errr4   r4   r5   test_svml_disabledw  s   .

zTestSVML.test_svml_disabledc                 C   s<   t dhdddd }|d | d||jd v  d S )	Nr   rM   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_likerP   sizer   r    )r   rk   rl   ir4   r4   r5   impl  s
   
z@TestSVML.test_svml_working_in_non_isolated_context.<locals>.impl   Zintel_svmlccr   )r   r   Zinspect_llvmZ
signatures)r   r   r4   r4   r5   )test_svml_working_in_non_isolated_context  s   
z2TestSVML.test_svml_working_in_non_isolated_context)r   r   r   ru   r   r   r   r   r   r   r   r   r   r   __classcell__r4   r4   r   r5   r     s    
	"8r   __main__)rF   )Cr   rM   r   r   r   	importlibr   r   r   multiprocessingr   	itertoolsr   r   r   Z
numba.corer   r   r   r   Znumba.core.compilerr   r	   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   rq   itemsrn   ro   rf   ri   ry   rz   r   r   r   r   r   mainr4   r4   r4   r5   <module>   s    	
 !"#


,
+e 
 