
    
f7                     b   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e*e          Z+ G d de
          Z, e,            Z- G d dej.                  Z/e/eed         <   d Z0d  Z1d! Z2ej3        ej4        ej5        ej6        d"Z7d# Z8ed$             Z9 G d% d&e:          Z; G d' d(e;          Z< G d) d*e;          Z=d+ Z>dS ),    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                       e Zd Zd ZdS )UFuncTargetOptionsc                 j   |                     d          sd|_        |                     d          sd|_        |                    dd           |                     d          st          j        |_        |                     d          s|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optionss      ;lib/python3.11/site-packages/numba/np/ufunc/ufuncbuilder.pyfinalizezUFuncTargetOptions.finalize$   s    ||-.. 	)$(E!||-.. 	)$(E!   555||K(( 	7$6EO||M** 	0 %E)-&  ,,,,,    N)__name__
__module____qualname__r2    r3   r1   r#   r#   "   s#        - - - - -r3   r#   c                   N     e Zd ZeZ fdZed             Zed             Z xZ	S )UFuncTargetc                 J    t                                          d           d S )Nufunc)super__init__)r.   	__class__s    r1   r=   zUFuncTarget.__init__;   s!    !!!!!r3   c                     t           j        S N)r   typing_contextr.   s    r1   rA   zUFuncTarget.typing_context>       ((r3   c                     t           j        S r@   )r   target_contextrB   s    r1   rE   zUFuncTarget.target_contextB   rC   r3   )
r4   r5   r6   r#   r0   r=   propertyrA   rE   __classcell__)r>   s   @r1   r9   r9   8   ss         G" " " " " ) ) X) ) ) X) ) ) ) )r3   r9   c                   T    e Zd ZdZeZi i fdZd Zed             Z	d Z
i fdZd ZdS )	UFuncDispatcherzK
    An object handling compilation of various signatures for a ufunc.
    c                     || _         t          j                    | _        || _        || _        t                      | _        d S r@   )py_funcr   
UniqueDict	overloadstargetoptionslocalsr   cache)r.   rK   rO   rN   s       r1   r=   zUFuncDispatcher.__init__P   s7    )++*[[


r3   c                 D    t          | j        | j        | j                  S )4
        NOTE: part of ReduceMixin protocol
        )pyfuncrO   rN   )dictrK   rO   rN   rB   s    r1   _reduce_stateszUFuncDispatcher._reduce_statesW   s+     <;,
 
 
 	
r3   c                      | |||          S )rR   )rK   rO   rN   r7   )clsrS   rO   rN   s       r1   _rebuildzUFuncDispatcher._rebuilda   s    
 s6&NNNNr3   c                 8    t          | j                  | _        d S r@   )r   rK   rP   rB   s    r1   enable_cachingzUFuncDispatcher.enable_cachingh   s    "4<00


r3   c                 x   | j                                         }|                    |           | j                                        }|                    |           t	          j                    }| j        j                            ||           d|_	        d|_
        d|_        |                     |||          S )NTnumpyF)rO   copyupdaterN   r   Flagstargetdescrr0   parse_as_flagsno_cpython_wrappererror_modelr&   _compile_core)r.   sigrO   rN   locstoptr/   s          r1   compilezUFuncDispatcher.compilek   s    {!!F!&&((M"""   //t<<<#' # !&!!#uf555r3   c                    	  j         j        } j         j        }t          	 fd            }t          5  t          j                                        |                                          5   |            5   j	        
                    ||          		&	cddd           cddd           cddd           S t          j        |          \  }}t          j        || j        ||||          	 j	                            |	           	cddd           cddd           cddd           S # 1 swxY w Y   	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w 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      K   	 d V  j                             j                  } | j         j        <   d S d S # t          $ r  w xY wr@   )rM   get	signature	Exception)existscresr.   s    r1   store_overloads_on_successzAUFuncDispatcher._compile_core.<locals>.store_overloads_on_success   sn      : ++DN;;>59DN4>222 ">	    s	   = A	N)argsreturn_typer/   rO   )r`   rA   rE   r   r   r   ConfigStackenterr]   rP   load_overloadr   normalize_signaturer   compile_extrarK   save_overload)
r.   re   r/   rO   	typingctx	targetctxrp   rq   rr   ro   s
   `        @r1   rd   zUFuncDispatcher._compile_core~   s   
 $3	$3				: 		: 		: 		: 		: 
		: " 	  	 )++11%**,,??    //11    :33CCCD'#	                          	  	  	  	  	  	  	  	  )1(DS(I(I%D+#1)Y26,T>I8=fN N ND J,,S$777!                          	  	  	  	  	  	  	  	                                               	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 sr   9E%.E9D5E$E%=AD5EE%5D99E<D9=EE%E	E%E	E%%E),E)N)r4   r5   r6   __doc__ufunc_targetr`   r=   rU   classmethodrX   rZ   rh   rd   r7   r3   r1   rI   rI   J   s          K') ! ! ! !
 
 
 O O [O1 1 1 #% 6 6 6 6&'  '  '  '  ' r3   rI   npyufuncc                 V     | j         |fi |}t          j        |          \  }}|||fS r@   )rh   r   rv   )nb_funcrN   re   ro   rq   rr   s         r1   _compile_element_wise_functionr      s?     7?300-00D 4S99D+{""r3   c                 x    |"| 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	TypeErrorrl   rr   r   pyobject)ro   rq   rr   s      r1   _finalize_ufunc_signaturer      sM    
 ? 	5KLLL.4K%.((((;r3   c           	      r   | j         }| j        }| j        j        }t          5  t          ||||| j        |           }|j                            |j                  }ddd           n# 1 swxY w Y   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                 6    g | ]}t          |          j        S r7   )r   num).0as     r1   
<listcomp>z5_build_element_wise_ufunc_wrapper.<locals>.<listcomp>   s     999Q!999r3   )rE   libraryfndescllvm_func_namer   r   r   get_pointer_to_functionnamerq   appendr   rr   r   environment)ro   rl   ctxr   fnameinfoptr	dtypenumss           r1   !_build_element_wise_ufunc_wrapperr      s     
ClGK&E	 > >"7C	#'?D: :l2249==> > > > > > > > > > > > > > >
 :9).999IXi3448999c4+++s   9A''A+.A+)r      Nreorderablec                 d    	 t           |          } n # t          $ r t          d|           w xY w| S )z]
    Parse an identity value and return the corresponding low-level value
    for Numpy.
    zInvalid identity value )_identitiesKeyError
ValueError)identitys    r1   parse_identityr      sM    
Dx( D D DjBCCCDOs    -c               #      K   t          j                    5  t          j        dt          d           dV  ddd           dS # 1 swxY 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	   r7   r3   r1   3_suppress_deprecation_warning_nopython_not_suppliedr      s       
	 	"	"  )@*=	@ 	@ 	@ 	@ 	                 s   !AAAc                       e Zd ZddZd ZdS )_BaseUFuncBuilderNc                     t          | d          r| j        }n| j        j        }t          | j        ||          \  }}}|                     |||          }| j                            |           || j        |<   |S )NrN   )hasattrrN   r   r   _finalize_signature_sigsr   _cres)r.   re   rN   ro   rq   rr   s         r1   addz_BaseUFuncBuilder.add   s    4)) 	7 .MM L6M"@L-#. #.dK&&tT;??
#
3r3   c                     dS )zI
        Disable the compilation of new signatures at call time.
        Nr7   rB   s    r1   disable_compilez!_BaseUFuncBuilder.disable_compile	  s      r3   r@   )r4   r5   r6   r   r   r7   r3   r1   r   r      s7        
 
 
 
    r3   r   c                   .    e Zd Zddi fdZd Zd Zd ZdS )UFuncBuilderNFc                 
   t          |          r|j        }|| _        t          |          | _        t	                      5   t          dd|d||          | _        d d d            n# 1 swxY w Y   g | _        i | _        d S )Nr~   _targetrP   r7   )	r   rK   r   r   r   r   r   r   r   )r.   rK   r   rP   rN   s        r1   r=   zUFuncBuilder.__init__  s    W 	&oG&x00@BB 	9 	903 0z%*0 0!.0 0079 9DL	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 



s    A**A.1A.c                 $    t          |||          S )z^Slated for deprecation, use ufuncbuilder._finalize_ufunc_signature()
        instead.
        )r   r.   ro   rq   rr   s       r1   r   z UFuncBuilder._finalize_signature  s     )t[AAAr3   c                    t           5  g }g }| j        st          d          g }d }| j        D ]|}| j        |         }|                     ||          \  }}}|                    |           |                    t          |                     |                    |j        |f           }d gt          |          z  }	|.t          j        | j                  }
t          |
j                  }nt          |j        j                  }d}t          j        | j        j        | j        j        |||||	|| j        	  	        }|cd d d            S # 1 swxY w Y   d S )NNo definitionr   )r   r   r   r   r   buildr   intr   leninspectgetfullargspecrK   rq   rl   r   fromfuncr4   r{   r   )r.   	dtypelistptrlist	keepalivero   re   r   r   envdatlistargspecinctoutctr;   s                 r1   build_ufunczUFuncBuilder.build_ufunc$  s   ! $	 $	IG< 1000 IDz 6 6z#&*jjs&;&;#	3  +++s3xx(((  $,!45555fs7||+G|!0>>7<((4>.//E &%t|';D%4= E I$	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	 $	s   D7EEEc                 "    t          ||          S )z^Slated for deprecation, use
        ufuncbuilder._build_element_wise_ufunc_wrapper().
        )r   )r.   ro   rl   s      r1   r   zUFuncBuilder.buildK  s     1yAAAr3   )r4   r5   r6   r=   r   r   r   r7   r3   r1   r   r     se        )-U" 
 
 
 
B B B% % %NB B B B Br3   r   c                   @    e Zd Zddi dfdZd Zed             Zd ZdS )GUFuncBuilderNFr7   c                    || _         t          |          | _        t                      5   t	          d|          |          | _        d d d            n# 1 swxY w Y   || _        t          |          \  | _        | _	        || _
        || _        g | _        i | _        t          |          t          fd|D                       | _        d S )Nr~   r   c                 &    g | ]} |          S r7   r7   )r   r   transform_args     r1   r   z*GUFuncBuilder.__init__.<locals>.<listcomp>c  s#    #L#L#LMM!$4$4#L#L#Lr3   )rK   r   r   r   r   r   rl   r   sinsoutrN   rP   r   r   _get_transform_argtupler!   )r.   rK   rl   r   rP   rN   r!   r   s          @r1   r=   zGUFuncBuilder.__init__U  s   &x00@BB 	I 	I?3z???HHDL	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I"-i88$)*


*733"#L#L#L#Lm#L#L#LMMs    AAAc                     |j         s)|j        j        t          j        k    rt          d          |t          j        } || S )Nz(gufunc kernel must have void return type)r   rl   rr   r   voidr   r   s       r1   r   z!GUFuncBuilder._finalize_signaturee  sG     	H4>#=#K#KFGGG*K{D!!r3   c                 <   g }g }| j         st          d          g }| j        D ]{}| j        |         }|                     |          \  }}}|                    |           |                    t          |                     |                    |j        |f           |d gt          |          z  }	t          | j	                  }
t          | j
                  }t          j        | j        j        | j        j        |||
||	|| j        | j        | j                  }|S )Nr   )r   r   r   r   r   r   r   r   r   r   r   r   r   rK   r4   r{   r   rl   r!   )r.   	type_list	func_listr   re   ro   r   r   r   datalistninnoutr;   s                r1   r   zGUFuncBuilder.build_ufuncn  s   		| 	-O,,, 	: 	2 	2C:c?D"&**T"2"2IsCY'''SXX&&&dlC011116C	NN*$(mm49~~ "L!4<#7y#tXt}dnd6H
 

 r3   c                 f   |j         }t          | j        || j        | j        | j        d          }|j        }|j                            |j	                  }g }|j
        D ]M}t          |t          j                  r|j        }n|}|                    t!          |          j                   N|||fS )zJ
        Returns (dtype numbers, function ptr, EnvironmentObject)
        F)rP   
is_parfors)rl   r   rK   r   r   rP   r   r   r   r   rq   
isinstancer   Arraydtyper   r   r   )	r.   ro   rl   r   r   r   r   r   tys	            r1   r   zGUFuncBuilder.build  s    
 N	#L$$)*
 
 

 hl2249==	 	/ 	/A!U[)) WXb\\-....#s""r3   )r4   r5   r6   r=   r   r   r   r   r7   r3   r1   r   r   R  sr         59!N N N N " " "   8# # # # #r3   r   c                 ~     t          j                   j        d t                    D              fd}|S )z-Return function that transform arg into indexc                     i | ]\  }}||	S r7   r7   )r   iargs      r1   
<dictcomp>z&_get_transform_arg.<locals>.<dictcomp>  s    777VQ#q777r3   c                     t          | t                    r| S 	 |          S # t          $ r  d|  d dj         }t	          |          w xY w)NzSpecified writable arg z not found in arg list z for function )r   r   r   r6   RuntimeError)r   msgrq   
pos_by_argrK   s     r1   r   z)_get_transform_arg.<locals>.transform_arg  s    c3 	J	$c?" 	$ 	$ 	$AS A AA A*1*>A ACs###	$s	   " *A)r   r   rq   	enumerate)rK   r   rq   r   s   ` @@r1   r   r     s\    !'**/D77y777J	$ 	$ 	$ 	$ 	$ 	$ 	$ r3   )?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   numba.np.ufunc.sigparser   numba.np.ufunc.wrappersr   r   numba.core.cachingr   r   numba.core.compiler_lockr   _options_mixinr#   r9   r|   ReduceMixinrI   r   r   r   PyUFunc_ZeroPyUFunc_OnePyUFunc_NonePyUFunc_ReorderableNoner   r   r   objectr   r   r   r   r7   r3   r1   <module>r     sj     % % % % % % + + + + + + + + % % % % % % 3 3 3 3 3 3 * * * * * * 5 5 5 5 5 5 E E E E E E E E * * * * * * L L L L L L L L B B B B B B B B B B B B B B + + + + + + $ $ $ $ $ $ 3 3 3 3 3 3 M M M M M M M M 7 7 7 7 7 7 7 7 9 9 9 9 9 9 )( - - - - - - - -,) ) ) ) )" ) ) ) {}}[  [  [  [  [ i+ [  [  [ | 4C OJ/ 0
# # #   , , ,& 

 4	 	 	 	 
 
 
       *?B ?B ?B ?B ?B$ ?B ?B ?BDN# N# N# N# N#% N# N# N#b    r3   