当我尝试运行此代码时:
from numba import njit, int64
from numba.types import List
@njit(int64(List(int64, True)))
def f(a):
a[0] = 0
return a[0]
@njit(List(int64)())
def g(): return [f([0])]
我得到以下内容。为什么是这样?如何正确识别类型?
Traceback (most recent call last):
File "Temp.py", line 9, in <module>
@njit(List(int64)())
File "numba\decorators.py", line 172, in wrapper
disp.compile(sig)
File "numba\dispatcher.py", line 350, in compile
flags=flags, locals=self.locals)
File "numba\compiler.py", line 644, in compile_extra
return pipeline.compile_extra(func)
File "numba\compiler.py", line 361, in compile_extra
return self.compile_bytecode(bc, func_attr=self.func_attr)
File "numba\compiler.py", line 370, in compile_bytecode
return self._compile_bytecode()
File "numba\compiler.py", line 631, in _compile_bytecode
return pm.run(self.status)
File "numba\compiler.py", line 251, in run
raise patched_exception
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
File "numba\compiler.py", line 243, in run
res = stage()
File "numba\compiler.py", line 458, in stage_nopython_frontend
self.locals)
File "numba\compiler.py", line 759, in type_inference_stage
infer.propagate()
File "numba\typeinfer.py", line 510, in propagate
raise errors[0]
TypingError: Internal error at <numba.typeinfer.CallConstraint object at 0x00000000071C82E8>:
--%<-----------------------------------------------------------------
Traceback (most recent call last):
File "numba\typeinfer.py", line 111, in propagate
constraint(typeinfer)
File "numba\typeinfer.py", line 270, in __call__
self.resolve(typeinfer, typevars, fnty)
File "numba\typeinfer.py", line 297, in resolve
sig = context.resolve_function_type(fnty, pos_args, kw_args)
File "numba\typing\context.py", line 113, in resolve_function_type
return func.get_call_type(self, args, kws)
File "numba\types.py", line 334, in get_call_type
sig.pysig = self.pysig
AttributeError: 'NoneType' object has no attribute 'pysig'
--%<-----------------------------------------------------------------
File "Temp.py", line 10
答案 0 :(得分:1)
将f
的装饰器签名从@njit(int64(List(int64, True)))
更改为@njit(int64(List(int64)))
会让Numba 0.31.0上的内容更有效。
对于f
,a
不是反映列表,因此将该标记设置为True
似乎会导致问题。对于Numba 0.31.0,我收到了不同的错误消息。
总的来说,我会允许Numba对输入和输出进行自己的类型推断。我不确定您使用的是哪个版本,但在最近的版本中,很少有理由在装饰器签名中定义类型。如果您对类型推断器的作用感到好奇,只需使用nb.njit
或nb.jit(nopython=True)
,使用所需的输入调用该函数,然后检查f.inspect_types()
(如果您的jitted函数为{ {1}})。