U
    hâËdõ7  ã                   @   sÊ  d dl Z d dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z#m$Z$ d dl%m&Z&m'Z' d dl(m)Z) eddddddƒZ*G dd„ de*eƒZ+G dd„ de
ƒZ,e,ƒ Z-G dd„ dej.ƒZ/e/eed < dd „ Z0d!d"„ Z1d#d$„ Z2ej3ej4ej5ej6d%œZ7d&d'„ Z8ed(d)„ ƒZ9G d*d+„ d+e:ƒZ;G d,d-„ d-e;ƒZ<G d.d/„ d/e;ƒZ=d0d1„ Z>dS )2é    N)Úcontextmanager)ÚconfigÚtargetconfig)Újit)ÚTargetDescriptor)Ú	is_jitted)ÚNumbaDeprecationWarning)ÚTargetOptionsÚinclude_default_options)Ú
cpu_target)Údispatcher_registryÚtarget_registry)ÚutilsÚtypesÚ	serializeÚcompilerÚsigutils)Úas_dtype)Ú	_internal)Úparse_signature)Úbuild_ufunc_wrapperÚbuild_gufunc_wrapper)ÚFunctionCacheÚ	NullCache)Úglobal_compiler_lockZnopythonZforceobjÚboundscheckÚfastmathZtarget_backendÚwritable_argsc                   @   s   e Zd Zdd„ ZdS )ÚUFuncTargetOptionsc                 C   sf   |  d¡sd|_|  d¡s d|_|jddd� |  d¡s@tj|_|  d¡sR|j|_d|_| d¡ d S )	NÚenable_pyobjectTÚenable_loopliftZnrt)ÚdefaultÚ	debuginfor   r   )	Úis_setr   r    Zinherit_if_not_setr   ZDEBUGINFO_DEFAULTr"   r   Zenable_pyobject_looplift)ÚselfÚflagsÚoptions© r'   úT/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/np/ufunc/ufuncbuilder.pyÚfinalize$   s    



zUFuncTargetOptions.finalizeN)Ú__name__Ú
__module__Ú__qualname__r)   r'   r'   r'   r(   r   "   s   r   c                       s8   e Zd ZeZ‡ fdd„Zedd„ ƒZedd„ ƒZ‡  Z	S )ÚUFuncTargetc                    s   t ƒ  d¡ d S )NÚufunc)ÚsuperÚ__init__©r$   ©Ú	__class__r'   r(   r0   ;   s    zUFuncTarget.__init__c                 C   s   t jS ©N)r   Útyping_contextr1   r'   r'   r(   r5   >   s    zUFuncTarget.typing_contextc                 C   s   t jS r4   )r   Útarget_contextr1   r'   r'   r(   r6   B   s    zUFuncTarget.target_context)
r*   r+   r,   r   r&   r0   Úpropertyr5   r6   Ú__classcell__r'   r'   r2   r(   r-   8   s   
r-   c                   @   sR   e Zd ZdZeZi i fdd„Zdd„ Zedd„ ƒZ	dd	„ Z
i fd
d„Zdd„ ZdS )ÚUFuncDispatcherzK
    An object handling compilation of various signatures for a ufunc.
    c                 C   s(   || _ t ¡ | _|| _|| _tƒ | _d S r4   )Úpy_funcr   Z
UniqueDictÚ	overloadsÚtargetoptionsÚlocalsr   Úcache)r$   r:   r=   r<   r'   r'   r(   r0   P   s
    
zUFuncDispatcher.__init__c                 C   s   t | j| j| jd�S )ú4
        NOTE: part of ReduceMixin protocol
        )Úpyfuncr=   r<   )Údictr:   r=   r<   r1   r'   r'   r(   Ú_reduce_statesW   s
    ýzUFuncDispatcher._reduce_statesc                 C   s   | |||d�S )r?   )r:   r=   r<   r'   )Úclsr@   r=   r<   r'   r'   r(   Ú_rebuilda   s    zUFuncDispatcher._rebuildc                 C   s   t | jƒ| _d S r4   )r   r:   r>   r1   r'   r'   r(   Úenable_cachingh   s    zUFuncDispatcher.enable_cachingc                 K   s`   | j  ¡ }| |¡ | j ¡ }| |¡ t ¡ }| jj ||¡ d|_	d|_
d|_|  |||¡S )NTÚnumpyF)r=   ÚcopyÚupdater<   r   ÚFlagsÚtargetdescrr&   Zparse_as_flagsZno_cpython_wrapperZerror_modelr    Ú_compile_core)r$   Úsigr=   r<   ZlocsZtoptr%   r'   r'   r(   Úcompilek   s    



zUFuncDispatcher.compilec           	         sþ   ˆj j}ˆj j}t‡ ‡fdd„ƒ}t�Î t ¡  | ¡ ¡�° |ƒ �ž ˆj	 
||¡‰ ˆ dk	r‚ˆ W  5 Q R £ W  5 Q R £ W  5 Q R £ S t |¡\}}tj||ˆj||||d�‰ ˆj	 |ˆ ¡ ˆ W  5 Q R £ W  5 Q R £ W  5 Q R £ S Q R X W 5 Q R X W 5 Q R X dS )z•
        Trigger the compiler on the core function or load a previously
        compiled version from the cache.  Returns the CompileResult.
        c                  3   sH   z
d V  W n t k
r    ‚ Y n$X ˆj ˆ j¡} | d krDˆ ˆjˆ j< d S r4   )Ú	Exceptionr;   ÚgetÚ	signature)Úexists©Úcresr$   r'   r(   Ústore_overloads_on_success†   s    
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)ÚargsÚreturn_typer%   r=   )rJ   r5   r6   r   r   r   ZConfigStackZenterrG   r>   Zload_overloadr   Únormalize_signaturer   Zcompile_extrar:   Zsave_overload)	r$   rL   r%   r=   Z	typingctxZ	targetctxrT   rU   rV   r'   rR   r(   rK   ~   s(    (  ýzUFuncDispatcher._compile_coreN)r*   r+   r,   Ú__doc__Úufunc_targetrJ   r0   rB   ÚclassmethodrD   rE   rM   rK   r'   r'   r'   r(   r9   J   s   

r9   Únpyufuncc                 C   s&   | j |f|Ž}t |¡\}}|||fS r4   )rM   r   rW   )Únb_funcr<   rL   rS   rU   rV   r'   r'   r(   Ú_compile_element_wise_function­   s    r]   c                 C   s6   |dkr | j rtdƒ‚n| jj}|tjks.t‚||Ž S )z¼Given a compilation result, argument types, and a return type,
    build a valid Numba signature after validating that it doesn't
    violate the constraints for the compilation mode.
    Nz-return type must be specified for object mode)Ú
objectmodeÚ	TypeErrorrP   rV   r   ZpyobjectÚAssertionError)rS   rU   rV   r'   r'   r(   Ú_finalize_ufunc_signatureµ   s    
ra   c              	   C   st   | j }| j}| jj}t�( t||||| j| ƒ}|j |j¡}W 5 Q R X dd„ |j	D ƒ}| 
t|jƒj¡ ||| jfS )z�Build a wrapper for the ufunc loop entry point given by the
    compilation result object, using the element-wise signature.
    c                 S   s   g | ]}t |ƒj‘qS r'   )r   Únum©Ú.0Úar'   r'   r(   Ú
<listcomp>Ò   s     z5_build_element_wise_ufunc_wrapper.<locals>.<listcomp>)r6   ÚlibraryZfndescZllvm_func_namer   r   r^   Úget_pointer_to_functionÚnamerU   Úappendr   rV   rb   Úenvironment)rS   rP   Úctxrg   ÚfnameÚinfoÚptrÚ	dtypenumsr'   r'   r(   Ú!_build_element_wise_ufunc_wrapperÅ   s    
 ÿrq   )r   é   NZreorderablec                 C   s4   zt |  } W n" tk
r.   td| f ƒ‚Y nX | S )z]
    Parse an identity value and return the corresponding low-level value
    for Numpy.
    zInvalid identity value %r)Ú_identitiesÚKeyErrorÚ
ValueError)Úidentityr'   r'   r(   Úparse_identityß   s
    rw   c                	   c   s.   t  ¡ � t jdtdd� dV  W 5 Q R X dS )z÷This suppresses the NumbaDeprecationWarning that occurs through the use
    of `jit` without the `nopython` kwarg. This use of `jit` occurs in a few
    places in the `{g,}ufunc` mechanism in Numba, predominantly to wrap the
    "kernel" function.Úignorez3.*The 'nopython' keyword argument was not supplied*)ÚcategoryÚmessageN)ÚwarningsÚcatch_warningsÚfilterwarningsr   r'   r'   r'   r(   Ú3_suppress_deprecation_warning_nopython_not_suppliedë   s    
þr~   c                   @   s   e Zd Zddd„Zdd„ ZdS )Ú_BaseUFuncBuilderNc                 C   sV   t | dƒr| j}n| jj}t| j||ƒ\}}}|  |||¡}| j |¡ || j|< |S )Nr<   )Úhasattrr<   r\   r]   Ú_finalize_signatureÚ_sigsrj   Ú_cres)r$   rL   r<   rS   rU   rV   r'   r'   r(   Úaddý   s    
  ÿ

z_BaseUFuncBuilder.addc                 C   s   dS )zI
        Disable the compilation of new signatures at call time.
        Nr'   r1   r'   r'   r(   Údisable_compile	  s    z!_BaseUFuncBuilder.disable_compile)N)r*   r+   r,   r„   r…   r'   r'   r'   r(   r   û   s   
r   c                   @   s4   e Zd Zddi fdd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚUFuncBuilderNFc              	   C   sZ   t |ƒr|j}|| _t|ƒ| _tƒ �  tf d|dœ|—Ž|ƒ| _W 5 Q R X g | _i | _d S )Nr[   ©Ú_targetr>   )	r   r:   rw   rv   r~   r   r\   r‚   rƒ   )r$   r:   rv   r>   r<   r'   r'   r(   r0     s    
ÿþþzUFuncBuilder.__init__c                 C   s   t |||ƒS )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )ra   ©r$   rS   rU   rV   r'   r'   r(   r�     s    z UFuncBuilder._finalize_signaturec                 C   sð   t �â g }g }| jstdƒ‚g }d }| jD ]H}| j| }|  ||¡\}}}| |¡ | t|ƒ¡ | |j|f¡ q*d gt	|ƒ }	|d kr¢t
 | j¡}
t	|
jƒ}nt	|jjƒ}d}t | jj| jj|||||	|| j¡	}|W  5 Q R £ S Q R X d S )NúNo definitionrr   )r   r\   r_   r‚   rƒ   Úbuildrj   Úintrg   ÚlenÚinspectÚgetfullargspecr:   rU   rP   r   Úfromfuncr*   rX   rv   )r$   Z	dtypelistZptrlistÚ	keepaliverS   rL   rp   ro   ÚenvZdatlistZargspecZinctZoutctr.   r'   r'   r(   Úbuild_ufunc$  s>    


      ýzUFuncBuilder.build_ufuncc                 C   s
   t ||ƒS )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )rq   )r$   rS   rP   r'   r'   r(   r‹   K  s    zUFuncBuilder.build)r*   r+   r,   r0   r�   r“   r‹   r'   r'   r'   r(   r†     s   'r†   c                   @   s:   e Zd Zddi dfdd„Zdd„ Zedd	„ ƒZd
d„ ZdS )ÚGUFuncBuilderNFr'   c              	      s†   || _ t|ƒ| _tƒ � td|d�|ƒ| _W 5 Q R X || _t|ƒ\| _| _	|| _
|| _g | _i | _t|ƒ‰ t‡ fdd„|D ƒƒ| _d S )Nr[   r‡   c                    s   g | ]}ˆ |ƒ‘qS r'   r'   rc   ©Útransform_argr'   r(   rf   c  s     z*GUFuncBuilder.__init__.<locals>.<listcomp>)r:   rw   rv   r~   r   r\   rP   r   ÚsinÚsoutr<   r>   r‚   rƒ   Ú_get_transform_argÚtupler   )r$   r:   rP   rv   r>   r<   r   r'   r•   r(   r0   U  s    
zGUFuncBuilder.__init__c                 C   s2   |j s|jjtjkrtdƒ‚|d kr*tj}||Ž S )Nz(gufunc kernel must have void return type)r^   rP   rV   r   Úvoidr_   r‰   r'   r'   r(   r�   e  s
    z!GUFuncBuilder._finalize_signaturec                 C   sº   g }g }| j stdƒ‚g }| jD ]F}| j| }|  |¡\}}}| |¡ | t|ƒ¡ | |j|f¡ q d gt|ƒ }	t| j	ƒ}
t| j
ƒ}t | jj| jj|||
||	|| j| j| j¡}|S )NrŠ   )r\   r_   r‚   rƒ   r‹   rj   rŒ   rg   r�   r—   r˜   r   r�   r:   r*   rX   rv   rP   r   )r$   Z	type_listZ	func_listr‘   rL   rS   rp   ro   r’   ZdatalistZninZnoutr.   r'   r'   r(   r“   n  s8    




        ýzGUFuncBuilder.build_ufuncc           	      C   sx   |j }t| j|| j| j| jdd�}|j}|j |j	¡}g }|j
D ],}t|tjƒrX|j}n|}| t|ƒj¡ q@|||fS )zJ
        Returns (dtype numbers, function ptr, EnvironmentObject)
        F)r>   Z
is_parfors)rP   r   r:   r—   r˜   r>   r’   rg   rh   ri   rU   Ú
isinstancer   ZArrayZdtyperj   r   rb   )	r$   rS   rP   rn   r’   ro   rp   re   Útyr'   r'   r(   r‹   ‹  s$        þ
zGUFuncBuilder.build)r*   r+   r,   r0   r�   r   r“   r‹   r'   r'   r'   r(   r”   R  s    ÿ
	
r”   c                    s2   t  ˆ¡j‰ dd„ tˆ ƒD ƒ‰‡ ‡‡fdd„}|S )z-Return function that transform arg into indexc                 S   s   i | ]\}}||“qS r'   r'   )rd   ÚiÚargr'   r'   r(   Ú
<dictcomp>¦  s      z&_get_transform_arg.<locals>.<dictcomp>c              	      sR   t | tƒr| S z
ˆ|  W S  tk
rL   d| › dˆ › dˆj› �}t|ƒ‚Y nX d S )NzSpecified writable arg z not found in arg list z for function )rœ   rŒ   rt   r,   ÚRuntimeError)rŸ   Úmsg©rU   Z
pos_by_argr:   r'   r(   r–   ¨  s    

z)_get_transform_arg.<locals>.transform_arg)rŽ   r�   rU   Ú	enumerate)r:   r–   r'   r£   r(   r™   £  s    r™   )?rŽ   r{   Ú
contextlibr   Z
numba.corer   r   Znumba.core.decoratorsr   Znumba.core.descriptorsr   Znumba.core.extendingr   Znumba.core.errorsr   Znumba.core.optionsr	   r
   Znumba.core.registryr   Znumba.core.target_extensionr   r   r   r   r   r   r   Znumba.np.numpy_supportr   Znumba.np.ufuncr   Znumba.np.ufunc.sigparser   Znumba.np.ufunc.wrappersr   r   Znumba.core.cachingr   r   Znumba.core.compiler_lockr   Z_options_mixinr   r-   rY   ZReduceMixinr9   r]   ra   rq   ZPyUFunc_ZeroZPyUFunc_OneZPyUFunc_NoneZPyUFunc_ReorderableNoners   rw   r~   Úobjectr   r†   r”   r™   r'   r'   r'   r(   Ú<module>   sZ   ú
^ü
BQ