o
    0 i7                     @   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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=d/d0 Z>dS )1    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fastmath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s)|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'   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/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   :      zUFuncTarget.__init__c                 C      t jS N)r   typing_contextr1   r'   r'   r(   r7   =      zUFuncTarget.typing_contextc                 C   r5   r6   )r   target_contextr1   r'   r'   r(   r9   A   r8   zUFuncTarget.target_context)
r*   r+   r,   r   r&   r0   propertyr7   r9   __classcell__r'   r'   r2   r(   r-   7   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 r6   )py_funcr   Z
UniqueDict	overloadstargetoptionslocalsr   cache)r$   r=   r@   r?   r'   r'   r(   r0   O   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_statesV   s
   zUFuncDispatcher._reduce_statesc                 C   s   | |||dS )rB   )r=   r@   r?   r'   )clsrC   r@   r?   r'   r'   r(   _rebuild`   s   zUFuncDispatcher._rebuildc                 C   s   t | j| _d S r6   )r   r=   rA   r1   r'   r'   r(   enable_cachingg   r4   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?   locsZtoptr%   r'   r'   r(   compilej   s   



zUFuncDispatcher.compilec           	         sN  j j}j j}t fdd}t t | h | U j	
||  durD W  d   W  d   W  d   S t|\}}tj||j||||d j	|   W  d   W  d   W  d   S 1 syw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   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   sH    zd V  W n t y    w j j} | d u r" j j< d S d S r6   )	Exceptionr>   get	signature)existscresr$   r'   r(   store_overloads_on_success   s   
zAUFuncDispatcher._compile_core.<locals>.store_overloads_on_successN)argsreturn_typer%   r@   )rM   r7   r9   r   r   r   ZConfigStackZenterrJ   rA   Zload_overloadr   normalize_signaturer   Zcompile_extrar=   Zsave_overload)	r$   rO   r%   r@   Z	typingctxZ	targetctxrX   rY   rZ   r'   rV   r(   rN   }   s:   	"zUFuncDispatcher._compile_coreN)r*   r+   r,   __doc__ufunc_targetrM   r0   rE   classmethodrG   rH   rQ   rN   r'   r'   r'   r(   r<   I   s    

r<   npyufuncc                 C   s*   | j |fi |}t|\}}|||fS r6   )rQ   r   r[   )nb_funcr?   rO   rW   rY   rZ   r'   r'   r(   _compile_element_wise_function   s   
ra   c                 C   s4   |du r| j rtd| 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	TypeErrorrT   rZ   r   Zpyobject)rW   rY   rZ   r'   r'   r(   _finalize_ufunc_signature   s   rd   c                 C   s   | j }| j}| jj}t t||||| j| }|j|j}W d   n1 s(w   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>)r9   libraryZfndescZllvm_func_namer   r   rb   get_pointer_to_functionnamerY   appendr   rZ   re   environment)rW   rT   ctxrk   fnameinfoptr	dtypenumsr'   r'   r(   !_build_element_wise_ufunc_wrapper   s   
ru   )r      NZreorderablec                 C   s,   zt |  } W | S  ty   td| f w )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   sF    t   t jdtdd dV  W d   dS 1 sw   Y  dS )zThis 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`   ra   _finalize_signature_sigsrn   _cres)r$   rO   r?   rW   rY   rZ   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_compiler6   )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   sn   t |r|j}|| _t|| _t  tdd|d||| _W d    n1 s*w   Y  g | _i | _d S )Nr_   _targetrA   r'   )	r   r=   r{   rz   r   r   r`   r   r   )r$   r=   rz   rA   r?   r'   r'   r(   r0     s   

zUFuncBuilder.__init__c                 C   s   t |||S )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )rd   r$   rW   rY   rZ   r'   r'   r(   r     s   z UFuncBuilder._finalize_signaturec                 C   s   t r g }g }| jstdg }d }| jD ]$}| j| }| ||\}}}|| |t| ||j|f qd gt	| }	|d u rQt
| j}
t	|
j}nt	|jj}d}t| jj| jj|||||	|| j	}|W  d    S 1 sww   Y  d S )NNo definitionrv   )r   r`   rc   r   r   buildrn   intrk   leninspectgetfullargspecr=   rY   rT   r   fromfuncr*   r\   rz   )r$   Z	dtypelistZptrlist	keepaliverW   rO   rt   rs   envZdatlistZargspecZinctZoutctr.   r'   r'   r(   build_ufunc#  s4   



$zUFuncBuilder.build_ufuncc                 C   s
   t ||S )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )ru   )r$   rW   rT   r'   r'   r(   r   J  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 d    n1 sw   Y  || _t|\| _| _	|| _
|| _g | _i | _t| t fdd|D | _d S )Nr_   r   c                    s   g | ]} |qS r'   r'   rf   transform_argr'   r(   ri   b  s    z*GUFuncBuilder.__init__.<locals>.<listcomp>)r=   r{   rz   r   r   r`   rT   r   sinsoutr?   rA   r   r   _get_transform_argtupler   )r$   r=   rT   rz   rA   r?   r   r'   r   r(   r0   T  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)rb   rT   rZ   r   voidrc   r   r'   r'   r(   r   d  s
   z!GUFuncBuilder._finalize_signaturec                 C   s   g }g }| j stdg }| jD ]#}| j| }| |\}}}|| |t| ||j|f qd gt| }	t| j	}
t| j
}t| jj| jj|||
||	|| j| j| j}|S )Nr   )r`   rc   r   r   r   rn   r   rk   r   r   r   r   r   r=   r*   r\   rz   rT   r   )r$   Z	type_listZ	func_listr   rO   rW   rt   rs   r   ZdatalistZninZnoutr.   r'   r'   r(   r   m  s(   





zGUFuncBuilder.build_ufuncc           	      C   sx   |j }t| j|| j| j| jdd}|j}|j|j	}g }|j
D ]}t|tjr,|j}n|}|t|j q |||fS )zJ
        Returns (dtype numbers, function ptr, EnvironmentObject)
        F)rA   Z
is_parfors)rT   r   r=   r   r   rA   r   rk   rl   rm   rY   
isinstancer   ZArrayZdtypern   r   re   )	r$   rW   rT   rr   r   rs   rt   rh   tyr'   r'   r(   r     s   

zGUFuncBuilder.build)r*   r+   r,   r0   r   r   r   r   r'   r'   r'   r(   r   Q  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'   )rg   iargr'   r'   r(   
<dictcomp>  rj   z&_get_transform_arg.<locals>.<dictcomp>c              	      sH   t | tr| S z|  W S  ty#   d|  d  dj }t|w )NzSpecified writable arg z not found in arg list z for function )r   r   rx   r,   RuntimeError)r   msgrY   Z
pos_by_argr=   r'   r(   r     s   

z)_get_transform_arg.<locals>.transform_arg)r   r   rY   	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-   r]   ZReduceMixinr<   ra   rd   ru   ZPyUFunc_ZeroZPyUFunc_OneZPyUFunc_NoneZPyUFunc_ReorderableNonerw   r{   r   objectr   r   r   r   r'   r'   r'   r(   <module>   sZ   	^
BQ