o
    8ήc,                     @   s   d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ eeZdZdi dddfdd	Zd
d Z		dddZdd Zi ddfddZdd ZdS )z%
Define @jit and related decorators.
    N)DeprecationErrorNumbaDeprecationWarning)stencil)config	extendingsigutilsregistryz`Deprecated keyword argument `{0}`. Signatures should be passed as the first positional argument.Fc                 K   s   d|v rt tdd|v rt td|ddr&|ddr&tdd|v r0|d |d< |dd	}||d
< | du rCd}d}nt| trMd}| }nt	| rXd}| g}n| }d}i }	|durf||	d< t
|f||||d|	}
|dur{|
|S |
S )a  
    This decorator is used to compile a Python function into native code.

    Args
    -----
    signature_or_function:
        The (optional) signature or list of signatures to be compiled.
        If not passed, required signatures will be compiled when the
        decorated function is called, depending on the argument values.
        As a convenience, you can directly pass the function to be compiled
        instead.

    locals: dict
        Mapping of local variable names to Numba types. Used to override the
        types deduced by Numba's type inference engine.

    pipeline_class: type numba.compiler.CompilerBase
            The compiler pipeline type for customizing the compilation stages.

    options:
        For a cpu target, valid options are:
            nopython: bool
                Set to True to disable the use of PyObjects and Python API
                calls. The default behavior is to allow the use of PyObjects
                and Python API. Default value is False.

            forceobj: bool
                Set to True to force the use of PyObjects for every value.
                Default value is False.

            looplift: bool
                Set to True to enable jitting loops in nopython mode while
                leaving surrounding code in object mode. This allows functions
                to allocate NumPy arrays and use Python objects, while the
                tight loops in the function can still be compiled in nopython
                mode. Any arrays that the tight loop uses should be created
                before the loop is entered. Default value is True.

            error_model: str
                The error-model affects divide-by-zero behavior.
                Valid values are 'python' and 'numpy'. The 'python' model
                raises exception.  The 'numpy' model sets the result to
                *+/-inf* or *nan*. Default value is 'python'.

            inline: str or callable
                The inline option will determine whether a function is inlined
                at into its caller if called. String options are 'never'
                (default) which will never inline, and 'always', which will
                always inline. If a callable is provided it will be called with
                the call expression node that is requesting inlining, the
                caller's IR and callee's IR as arguments, it is expected to
                return Truthy as to whether to inline.
                NOTE: This inlining is performed at the Numba IR level and is in
                no way related to LLVM inlining.

            boundscheck: bool or None
                Set to True to enable bounds checking for array indices. Out
                of bounds accesses will raise IndexError. The default is to
                not do bounds checking. If False, bounds checking is disabled,
                out of bounds accesses can produce garbage results or segfaults.
                However, enabling bounds checking will slow down typical
                functions, so it is recommended to only use this flag for
                debugging. You can also set the NUMBA_BOUNDSCHECK environment
                variable to 0 or 1 to globally override this flag. The default
                value is None, which under normal execution equates to False,
                but if debug is set to True then bounds checking will be
                enabled.

    Returns
    --------
    A callable usable as a compiled function.  Actual compiling will be
    done lazily if no explicit signatures are passed.

    Examples
    --------
    The function can be used in the following ways:

    1) jit(signatures, **targetoptions) -> jit(function)

        Equivalent to:

            d = dispatcher(function, targetoptions)
            for signature in signatures:
                d.compile(signature)

        Create a dispatcher object for a python function.  Then, compile
        the function with the given signature(s).

        Example:

            @jit("int32(int32, int32)")
            def foo(x, y):
                return x + y

            @jit(["int32(int32, int32)", "float32(float32, float32)"])
            def bar(x, y):
                return x + y

    2) jit(function, **targetoptions) -> dispatcher

        Create a dispatcher function object that specializes at call site.

        Examples:

            @jit
            def foo(x, y):
                return x + y

            @jit(nopython=True)
            def bar(x, y):
                return x + y

    argtypesrestypenopythonFforceobjz1Only one of 'nopython' or 'forceobj' can be True._targettarget_backendcpuboundscheckNpipeline_class)localstargetcachetargetoptions)r   _msg_deprecated_signature_argformatget
ValueErrorpop
isinstancelistr   is_signature_jit)signature_or_functionr   r   r   r   optionsr   pyfuncsigsdispatcher_argswrapper r%   </tmp/pip-target-vg8gfxp4/lib/python/numba/core/decorators.pyjit   s@   s

r'   c                    s0   ddl m} | fdd}|S )Nr   )resolve_dispatcher_from_strc                    s   t | rtd|  d|  dt| stdt|  dtjr1dkr1ddlm	} |
| S tjr:d	ks:| S d| d
} rK|  d uryddlm} || D ]}|| q]|  W d    |S 1 stw   Y  |S )Nz9A jit decorator was called on an already jitted function z=.  If trying to access the original python function, use the z.py_func attribute.z1The decorated object is not a function (got type z).cudar   )r)   npyufunc)py_funcr   r   )	typeinferr%   )r   	is_jitted	TypeErrorinspect
isfunctiontyper   ENABLE_CUDASIMnumbar)   r'   DISABLE_JITenable_caching
numba.corer,   register_dispatchercompiledisable_compile)funcr)   dispr,   sigr   
dispatcherr#   r   r"   r   r   r%   r&   r$      sH   




z_jit.<locals>.wrapper)numba.core.target_extensionr(   )r"   r   r   r   r   r#   r(   r$   r%   r=   r&   r      s   "r   c              	   K   sD   i }|dur
||d< t ddi d||dd|}| dur || S |S )a  
    This decorator allows flexible type-based compilation
    of a jitted function.  It works as `@jit`, except that the decorated
    function is called at compile-time with the *types* of the arguments
    and should return an implementation function for those types.
    Nr   r   	generated)r"   r   r   r   r   	impl_kindr%   )r   )functionr   r   r    r#   r$   r%   r%   r&   generated_jit   s   rC   c                  O   sJ   d|v r
t dt d|v rt dt |d= |ddi t| i |S )zr
    Equivalent to jit(nopython=True)

    See documentation for jit function/decorator for full description.
    r   z'nopython is set for njit and is ignoredr   z'forceobj is set for njit and is ignoredT)warningswarnRuntimeWarningupdater'   )argskwsr%   r%   r&   njit   s   rJ   c                    s"   t  fdd}|S )z
    This decorator is used to compile a Python function into a C callback
    usable with foreign C libraries.

    Usage::
        @cfunc("float64(float64, float64)", nopython=True, cache=True)
        def add(a, b):
            return a + b

    c                    sP   ddl m} i }d ur|d< || fd|} r"|  |  |S )Nr   )CFuncr   )r   r    )numba.core.ccallbackrK   r5   r8   )r:   rK   additional_argsresr   r   r    r   r<   r%   r&   r$     s   zcfunc.<locals>.wrapper)r   normalize_signature)r<   r   r   r   r    r$   r%   rO   r&   cfunc  s   
rQ   c                  K   sv   t  d }t |d }|j D ]&\}}t |r8t ||kr8td||j	|  t
|fi | |j|< qdS )a   Automatically ``jit``-wraps functions defined in a Python module

    Note that ``jit_module`` should only be called at the end of the module to
    be jitted. In addition, only functions which are defined in the module
    ``jit_module`` is called from are considered for automatic jit-wrapping.
    See the Numba documentation for more information about what can/cannot be
    jitted.

    :param kwargs: Keyword arguments to pass to ``jit`` such as ``nopython``
                   or ``error_model``.

       r   zCAuto decorating function {} from module {} with jit and options: {}N)r/   stack	getmodule__dict__itemsr0   _loggerdebugr   __name__r'   )kwargsframemodulenameobjr%   r%   r&   
jit_module   s   r_   )NFN)__doc__sysrD   r/   loggingnumba.core.errorsr   r   numba.stencils.stencilr   r6   r   r   r   r   	getLoggerrY   rW   r   r'   r   rC   rJ   rQ   r_   r%   r%   r%   r&   <module>   s*    

 *
