U
    ¿|eõ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_lockÚnopythonÚforceobjÚboundscheckÚfastmathÚ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_loopliftÚnrt)ÚdefaultÚ	debuginfor   r   )	Úis_setr"   r#   Úinherit_if_not_setr   ÚDEBUGINFO_DEFAULTr&   r   Úenable_pyobject_looplift)ÚselfÚflagsÚoptions© r.   úX/var/www/website-v5/atlas_env/lib/python3.8/site-packages/numba/np/ufunc/ufuncbuilder.pyÚfinalize$   s    



zUFuncTargetOptions.finalizeN)Ú__name__Ú
__module__Ú__qualname__r0   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/   r7   ;   s    zUFuncTarget.__init__c                 C   s   t jS ©N)r   Útyping_contextr8   r.   r.   r/   r<   >   s    zUFuncTarget.typing_contextc                 C   s   t jS r;   )r   Útarget_contextr8   r.   r.   r/   r=   B   s    zUFuncTarget.target_context)
r1   r2   r3   r!   r-   r7   Úpropertyr<   r=   Ú__classcell__r.   r.   r9   r/   r4   8   s   
r4   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 r;   )Úpy_funcr   Ú
UniqueDictÚ	overloadsÚtargetoptionsÚlocalsr   Úcache)r+   rA   rE   rD   r.   r.   r/   r7   P   s
    
zUFuncDispatcher.__init__c                 C   s   t | j| j| jd�S )ú4
        NOTE: part of ReduceMixin protocol
        )ÚpyfuncrE   rD   )ÚdictrA   rE   rD   r8   r.   r.   r/   Ú_reduce_statesW   s
    ýzUFuncDispatcher._reduce_statesc                 C   s   | |||d�S )rG   )rA   rE   rD   r.   )ÚclsrH   rE   rD   r.   r.   r/   Ú_rebuilda   s    zUFuncDispatcher._rebuildc                 C   s   t | jƒ| _d S r;   )r   rA   rF   r8   r.   r.   r/   Úenable_cachingh   s    zUFuncDispatcher.enable_cachingc                 K   s`   | j  ¡ }| |¡ | j ¡ }| |¡ t ¡ }| jj ||¡ d|_	d|_
d|_|  |||¡S )NTÚnumpyF)rE   ÚcopyÚupdaterD   r   ÚFlagsÚtargetdescrr-   Úparse_as_flagsÚno_cpython_wrapperÚerror_modelr#   Ú_compile_core)r+   ÚsigrE   rD   ÚlocsÚ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 r;   )Ú	ExceptionrC   ÚgetÚ	signature)Úexists©Úcresr+   r.   r/   Ústore_overloads_on_success†   s    
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)ÚargsÚreturn_typer,   rE   )rR   r<   r=   r   r   r   ÚConfigStackÚenterrO   rF   Úload_overloadr   Únormalize_signaturer   Úcompile_extrarA   Úsave_overload)	r+   rW   r,   rE   Ú	typingctxÚ	targetctxra   rb   rc   r.   r_   r/   rV   ~   s(    (  ýzUFuncDispatcher._compile_coreN)r1   r2   r3   Ú__doc__Úufunc_targetrR   r7   rJ   ÚclassmethodrL   rM   rZ   rV   r.   r.   r.   r/   r@   J   s   

r@   Únpyufuncc                 C   s&   | j |f|Ž}t |¡\}}|||fS r;   )rZ   r   rg   )Únb_funcrD   rW   r`   rb   rc   r.   r.   r/   Ú_compile_element_wise_function­   s    rq   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Ú	TypeErrorr]   rc   r   ÚpyobjectÚAssertionError)r`   rb   rc   r.   r.   r/   Ú_finalize_ufunc_signatureµ   s    
rv   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>)r=   ÚlibraryÚfndescÚllvm_func_namer   r   rr   Úget_pointer_to_functionÚnamerb   Úappendr   rc   rw   Úenvironment)r`   r]   Úctxr|   ÚfnameÚinfoÚptrÚ	dtypenumsr.   r.   r/   Ú!_build_element_wise_ufunc_wrapperÅ   s    
 ÿrˆ   )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
    rŽ   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 )NrD   )ÚhasattrrD   rp   rq   Ú_finalize_signatureÚ_sigsr�   Ú_cres)r+   rW   rD   r`   rb   rc   r.   r.   r/   Úaddý   s    
  ÿ

z_BaseUFuncBuilder.addc                 C   s   dS )zI
        Disable the compilation of new signatures at call time.
        Nr.   r8   r.   r.   r/   Údisable_compile	  s    z!_BaseUFuncBuilder.disable_compile)N)r1   r2   r3   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 )Nro   ©Ú_targetrF   )	r   rA   rŽ   r�   r•   r   rp   r™   rš   )r+   rA   r�   rF   rD   r.   r.   r/   r7     s    
ÿþþzUFuncBuilder.__init__c                 C   s   t |||ƒS )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )rv   ©r+   r`   rb   rc   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 definitionr‰   )r   rp   rs   r™   rš   Úbuildr�   Úintr|   ÚlenÚinspectÚgetfullargspecrA   rb   r]   r   Úfromfuncr1   rl   r�   )r+   Ú	dtypelistZptrlistÚ	keepaliver`   rW   r‡   r†   ÚenvZdatlistÚargspecZinctZoutctr5   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().
        )rˆ   )r+   r`   r]   r.   r.   r/   r¢   K  s    zUFuncBuilder.build)r1   r2   r3   r7   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 )Nro   rž   c                    s   g | ]}ˆ |ƒ‘qS r.   r.   rx   ©Útransform_argr.   r/   r{   c  s     z*GUFuncBuilder.__init__.<locals>.<listcomp>)rA   rŽ   r�   r•   r   rp   r]   r   ÚsinÚsoutrD   rF   r™   rš   Ú_get_transform_argÚtupler    )r+   rA   r]   r�   rF   rD   r    r.   r®   r/   r7   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)rr   r]   rc   r   Úvoidrs   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¡   )rp   rs   r™   rš   r¢   r�   r£   r|   r¤   r°   r±   r   r§   rA   r1   rl   r�   r]   r    )r+   Z	type_listZ	func_listr©   rW   r`   r‡   r†   rª   ZdatalistÚninÚnoutr5   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)rF   Ú
is_parfors)r]   r   rA   r°   r±   rF   rª   r|   r   r€   rb   Ú
isinstancer   ÚArrayÚdtyper�   r   rw   )	r+   r`   r]   r…   rª   r†   r‡   rz   Útyr.   r.   r/   r¢   ‹  s$        þ
zGUFuncBuilder.build)r1   r2   r3   r7   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.   )ry   Ú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£   r‹   r3   ÚRuntimeError)r½   Úmsg©rb   Z
pos_by_argrA   r.   r/   r¯   ¨  s    

z)_get_transform_arg.<locals>.transform_arg)r¥   r¦   rb   Ú	enumerate)rA   r¯   r.   rÁ   r/   r²   £  s    r²   )?r¥   r’   Ú
contextlibr   Ú
numba.corer   r   Únumba.core.decoratorsr   Únumba.core.descriptorsr   Únumba.core.extendingr   Únumba.core.errorsr   Únumba.core.optionsr	   r
   Únumba.core.registryr   Únumba.core.target_extensionr   r   r   r   r   r   r   Únumba.np.numpy_supportr   Únumba.np.ufuncr   Znumba.np.ufunc.sigparser   Únumba.np.ufunc.wrappersr   r   Únumba.core.cachingr   r   Únumba.core.compiler_lockr   Ú_options_mixinr!   r4   rm   ÚReduceMixinr@   rq   rv   rˆ   ÚPyUFunc_ZeroÚPyUFunc_OneÚPyUFunc_NoneÚPyUFunc_ReorderableNonerŠ   rŽ   r•   Úobjectr–   r�   r­   r²   r.   r.   r.   r/   Ú<module>   sZ   ú
^ü
BQ