使用定义函数
*
时会发生此错误。我可以从三个函数定义案例开始,前两个案例通过,第三个案例是对第二个测试案例的较小修改,但失败。也许不支持*,但是这个错误很有趣,想了解原因。
Numba 版本:'0.56.4' Python 版本:'3.9.17'
import numba as nb
def test_1(a, b, c, d, e, f, g):
return a + b + c + d + e + f + g
test_1 = nb.njit(test_1)
def test_2(a, b, c, d, e, f, g):
return test_1(a, b, c, d, e, f, g)
test_2 = nb.njit(test_2)
test_2(1, 2, 3, 4, 5, 6, 7)
import numba as nb
def test_1(a, b, c, *, d, e, f):
return a + b + c + d + e + f
test_1 = nb.njit(test_1)
def test_2(a, b, c, d, e, f, g):
return test_1(a, b, c, d, e, f)
test_2 = nb.njit(test_2)
test_2(1, 2, 3, 4, 5, 6, 7)
import numba as nb
def test_1(a, b, c, *, d, e, f, g):
return a + b + c + d + e + f
test_1 = nb.njit(test_1)
def test_2(a, b, c, d, e, f, g):
return test_1(a, b, c, d, e, f, g)
test_2 = nb.njit(test_2)
test_2(1, 2, 3, 4, 5, 6, 7)
---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
Cell In[10], line 9
7 test_2 = nb.njit(test_2)
8 # test_1(1, 2, 3, 4, 5, 6, 7)
----> 9 test_2(1, 2, 3, 4, 5, 6, 7)
File ~/.cache/pypoetry/virtualenvs/indices-ldm-post-process-danSbNDA-py3.9/lib/python3.9/site-packages/numba/core/dispatcher.py:467, in _DispatcherBase._compile_for_args(self, *args, **kws)
464 msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
465 f"by the following argument(s):\n{args_str}\n")
466 e.patch_message(msg)
--> 467 error_rewrite(e, 'typing')
468 except errors.UnsupportedError as e:
469 # Something unsupported is present in the user code, add help info
470 error_rewrite(e, 'unsupported_error')
File ~/.cache/pypoetry/virtualenvs/indices-ldm-post-process-danSbNDA-py3.9/lib/python3.9/site-packages/numba/core/dispatcher.py:409, in _DispatcherBase._compile_for_args..error_rewrite(e, issue_type)
407 raise e
408 else:
--> 409 raise e.with_traceback(None)
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at .
tuple index out of range
During: resolving callee type: type(CPUDispatcher())
During: typing of call at /tmp/ipykernel_3435313/3644502072.py (6)
Enable logging at debug level for details.
我在 Numba github
提出了一个问题该错误似乎与
*args
函数定义中使用 test_1
语法有关。 Numba 不支持此语法。
要修复该错误,您可以使用
**kwargs
语法。以下是如何修改 test_1
和 test_2
函数的示例:
python
import numba as nb
def test_1(a, b, c, **kwargs):
d, e, f, g = kwargs.values()
return a + b + c + d + e + f + g
test_1 = nb.njit(test_1)
def test_2(a, b, c, d, e, f, g):
return test_1(a, b, c, d=d, e=e, f=f, g=g)
test_2 = nb.njit(test_2)
test_2(1, 2, 3, 4, 5, 6, 7)
此代码应该可以正常运行,没有任何错误。