a
    .;bC1                     @   s  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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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 Z+dd Z,d d! Z-ej.ej/ej0ej1d"Z2d#d$ Z3G d%d& d&e4Z5G d'd( d(e5Z6G d)d* d*e5Z7dS )+    N)contextmanager)configtargetconfig)jit)TargetDescriptor)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_backendc                   @   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   )	Zis_setr   r   Zinherit_if_not_setr   ZDEBUGINFO_DEFAULTr   r   Zenable_pyobject_looplift)selfflagsoptions r#   :lib/python3.9/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$   r,   7   s    zUFuncTarget.__init__c                 C   s   t jS N)r	   typing_contextr-   r#   r#   r$   r1   :   s    zUFuncTarget.typing_contextc                 C   s   t jS r0   )r	   target_contextr-   r#   r#   r$   r2   >   s    zUFuncTarget.target_context)
r&   r'   r(   r   r"   r,   propertyr1   r2   __classcell__r#   r#   r.   r$   r)   4   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 r0   )py_funcr   Z
UniqueDict	overloadstargetoptionslocalsr   cache)r    r6   r9   r8   r#   r#   r$   r,   L   s
    
zUFuncDispatcher.__init__c                 C   s   t | j| j| jdS )4
        NOTE: part of ReduceMixin protocol
        )pyfuncr9   r8   )dictr6   r9   r8   r-   r#   r#   r$   _reduce_statesS   s
    zUFuncDispatcher._reduce_statesc                 C   s   | |||dS )r;   )r6   r9   r8   r#   )clsr<   r9   r8   r#   r#   r$   _rebuild]   s    zUFuncDispatcher._rebuildc                 C   s   t | j| _d S r0   )r   r6   r:   r-   r#   r#   r$   enable_cachingd   s    zUFuncDispatcher.enable_cachingc                 K   s`   | j  }|| | j }|| t }| jj|| d|_	d|_
d|_| |||S )NTZnumpyF)r9   copyupdater8   r   ZFlagstargetdescrr"   Zparse_as_flagsZno_cpython_wrapperZerror_modelr   _compile_core)r    sigr9   r8   ZlocsZtoptr!   r#   r#   r$   compileg   s    



zUFuncDispatcher.compilec           	         sD  j j}j j}t fdd}t t |  |  j	
||  dur W  d   W  d   W  d   S t|\}}tj||j||||d j	|   W  d   W  d   W  d   S 1  s0    Y  W d   n1 s0    Y  W d   n1 s60    Y  dS )z
        Trigger the compiler on the core function or load a previously
        compiled version from the cache.  Returns the CompileResult.
        c                  3   sF   z
d V  W n t y    Y n$0 j j} | d u rB j j< d S r0   )	Exceptionr7   get	signature)existscresr    r#   r$   store_overloads_on_success   s    
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)argsreturn_typer!   r9   )rD   r1   r2   r   r   r   ZConfigStackZenterrB   r:   Zload_overloadr   normalize_signaturer   Zcompile_extrar6   Zsave_overload)	r    rF   r!   r9   Z	typingctxZ	targetctxrN   rO   rP   r#   rL   r$   rE   z   s$    .zUFuncDispatcher._compile_coreN)r&   r'   r(   __doc__ufunc_targetrD   r,   r>   classmethodr@   rA   rG   rE   r#   r#   r#   r$   r5   F   s   

r5   npyufuncc                 C   s*   | j |fi |}t|\}}|||fS r0   )rG   r   rQ   )nb_funcr8   rF   rM   rO   rP   r#   r#   r$   _compile_element_wise_function   s    rW   c                 C   s6   |du r | j rtdn| jj}|tjks.J || S )zGiven 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	TypeErrorrJ   rP   r   Zpyobject)rM   rO   rP   r#   r#   r$   _finalize_ufunc_signature   s    
rZ   c                 C   s   | j }| j}| jj}t2 t||||| j| }|j|j}W d   n1 sP0    Y  dd |j	D }|
t|jj ||| jfS )zBuild a wrapper for the ufunc loop entry point given by the
    compilation result object, using the element-wise signature.
    Nc                 S   s   g | ]}t |jqS r#   )r   num).0ar#   r#   r$   
<listcomp>       z5_build_element_wise_ufunc_wrapper.<locals>.<listcomp>)r2   libraryZfndescZllvm_func_namer   r   rX   get_pointer_to_functionnamerO   appendr   rP   r[   Zenvironment)rM   rJ   ctxr`   fnameinfoptr	dtypenumsr#   r#   r$   !_build_element_wise_ufunc_wrapper   s    
,ri   )r      NZreorderablec                 C   s2   zt |  } W n  ty,   td| f Y n0 | 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
    ro   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 )Nr8   )hasattrr8   rV   rW   _finalize_signature_sigsrc   _cres)r    rF   r8   rM   rO   rP   r#   r#   r$   add   s    


z_BaseUFuncBuilder.addc                 C   s   dS )zI
        Disable the compilation of new signatures at call time.
        Nr#   r-   r#   r#   r$   disable_compile   s    z!_BaseUFuncBuilder.disable_compile)N)r&   r'   r(   ru   rv   r#   r#   r#   r$   rp      s   
rp   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   s:   || _ t|| _tf d|d||| _g | _i | _d S NrU   )Z_targetr:   )r6   ro   rn   r   rV   rs   rt   )r    r6   rn   r:   r8   r#   r#   r$   r,      s    
zUFuncBuilder.__init__c                 C   s   t |||S )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )rZ   r    rM   rO   rP   r#   r#   r$   rr   	  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 u rt
| j}
t	|
j}nt	|jj}d}t| jj| jj|||||	|| j	}|W  d    S 1 s0    Y  d S )NNo definitionrj   )r   rV   rY   rs   rt   buildrc   intr`   leninspectZgetfullargspecr6   rO   rJ   r   fromfuncr&   rR   rn   )r    Z	dtypelistZptrlist	keepaliverM   rF   rh   rg   envZdatlistZargspecZinctZoutctr*   r#   r#   r$   build_ufunc  s2    



zUFuncBuilder.build_ufuncc                 C   s
   t ||S )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )ri   )r    rM   rJ   r#   r#   r$   r{   6  s    zUFuncBuilder.build)r&   r'   r(   r,   rr   r   r{   r#   r#   r#   r$   rw      s   	'rw   c                   @   s8   e Zd Zddi fddZdd Zedd Zd	d
 ZdS )GUFuncBuilderNFc                 C   sT   || _ t|| _td|d|| _|| _t|\| _| _|| _	|| _
g | _i | _d S rx   )r6   ro   rn   r   rV   rJ   r   sinsoutr8   r:   rs   rt   )r    r6   rJ   rn   r:   r8   r#   r#   r$   r,   @  s    
zGUFuncBuilder.__init__c                 C   s2   |j s|jjtjkrtd|d u r*tj}|| S )Nz(gufunc kernel must have void return type)rX   rJ   rP   r   ZvoidrY   ry   r#   r#   r$   rr   L  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
}|S )Nrz   )rV   rY   rs   rt   r{   rc   r|   r`   r}   r   r   r   r   r6   r&   rR   rn   rJ   )r    Z	type_listZ	func_listr   rF   rM   rh   rg   r   ZdatalistZninZnoutr*   r#   r#   r$   r   U  s(    






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)rJ   r   r6   r   r   r:   r   r`   ra   rb   rO   
isinstancer   ZArrayZdtyperc   r   r[   )	r    rM   rJ   rf   r   rg   rh   r]   Ztyr#   r#   r$   r{   r  s    
zGUFuncBuilder.build)r&   r'   r(   r,   rr   r   r   r{   r#   r#   r#   r$   r   =  s   
	
r   )8r~   
contextlibr   Z
numba.corer   r   Znumba.core.decoratorsr   Znumba.core.descriptorsr   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)   rS   ZReduceMixinr5   rW   rZ   ri   ZPyUFunc_ZeroZPyUFunc_OneZPyUFunc_NoneZPyUFunc_ReorderableNonerk   ro   objectrp   rw   r   r#   r#   r#   r$   <module>   sL   	^?