我有这个简单的 MWE:
from numba import njit
@njit
def add(a, b):
return a + b
# Now let's inspect the assembly code for the 'add()' function.
for k, v in add.inspect_asm().items():
print(k)
当我运行它时,我没有得到任何输出。检查装配的正确方法是什么?
您需要首先通过调用或指定签名来编译函数以填充
.inspect_asm()
。例如:
from numba import njit
@njit
def add(a, b):
return a + b
# first call add() to compile it
add(1, 2)
print(add.inspect_asm())
打印:
{(int64, int64): '\t.text\n\t.file\t"<string>"\n\t.globl\t_ZN8__main__3addB2v1B38c8tJTIcFKzyF2ILShI4CrgQElQb6HczSBAA_3dExx\n\t.p2align\t4, ...
或:
from numba import njit
# specify the signature first:
@njit("int64(int64, int64)")
def add(a, b):
return a + b
print(add.inspect_asm())
这是因为尚未编译该函数的版本。
@njit
/@jit
或类似函数每次使用新类型的参数调用时都会被编译。
让我们调用该函数几次来编译它的某些版本,我们可以看到我们使用您的代码编译了哪些版本:
from numba import njit
@njit
def add(a, b):
return a + b
add(3,2)
add(3,2+1j)
add(3.0, 2.0)
# Now let's inspect the assembly code for the 'add()' function.
for k, v in add.inspect_asm().items():
print(k)
现在输出将是:
(int64, int64)
(int64, complex128)
(float64, float64)