I am currently doing hasattr(func, "inspect_llvm")
to find out is a function is jitted. It works but …
- What is the recommended way to do it?
- And to check if was jitted in
object
ornopython
mode?
I am currently doing hasattr(func, "inspect_llvm")
to find out is a function is jitted. It works but …
object
or nopython
mode?Perhaps you could use the signatures
attribute of the dispatcher:
In [1]: import numba as nb
In [2]: @nb.njit
...: def foo():
...: acc = 0
...: for i in range(100):
...: acc += i
...: return acc
...:
In [3]: foo
Out[3]: CPUDispatcher(<function foo at 0x7fbf7572ab80>)
In [4]: foo.signatures
Out[4]: []
In [5]: foo()
Out[5]: 4950
In [6]: foo.signatures
Out[6]: [()]
Or maybe this is even better: https://github.com/numba/numba/blob/master/numba/core/extending.py#L526
Thanks @esc I was not aware of this. Do you know if there is a way to distinguish between jitted and nijitted functions?
This is now documented: Document numba.extending.is_jitted by stuartarchibald · Pull Request #6472 · numba/numba · GitHub
RE: differentiating between nopython
vs “some sort of jit”, there’s two attributes on the dispatcher object:
signatures
which is a list of all compiled signaturesnopython_signatures
which is a list of nopython
mode compiled signatures.Small demo:
In [1]: from numba import jit
In [2]: @jit
...: def foo(x):
...: if x.ndim == 1:
...: object()
...: return 10
...: else:
...: return 4
...:
In [3]: import numpy as np
In [4]: a = np.array([1])
In [5]: foo(a)
<ipython-input-2-bd094eb34546>:1: NumbaWarning:
Compilation is falling back to object mode WITH looplifting enabled because Function "foo" failed type inference due to: Untyped global name 'object': cannot determine Numba type of <class 'type'>
File "<ipython-input-2-bd094eb34546>", line 4:
def foo(x):
<source elided>
if x.ndim == 1:
object()
^
@jit
<path>/numba/core/object_mode_passes.py:178: NumbaWarning: Function "foo" was compiled in object mode without forceobj=True.
File "<ipython-input-2-bd094eb34546>", line 2:
@jit
def foo(x):
^
state.func_ir.loc))
<path>/numba/core/object_mode_passes.py:188: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
For more information visit http://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
File "<ipython-input-2-bd094eb34546>", line 2:
@jit
def foo(x):
^
state.func_ir.loc))
Out[5]: 10
In [6]: b = np.array([[1]])
In [7]: foo(b)
Out[7]: 4
In [8]: foo.signatures
Out[8]: [(array(int64, 1d, C),), (array(int64, 2d, C),)]
In [9]: foo.nopython_signatures
Out[9]: [(array(int64, 2d, C),) -> Literal[int](4)]