嵌套Numba调用中关键字参数超3个时触发Dispatch错误排查
Numba强制关键字参数调用的内部错误分析
环境信息
- Numba版本:0.56.4
- Python版本:3.9.17
测试用例对比
通过测试用例1
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)
通过测试用例2
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)
失败测试用例3
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.
错误原因分析
对比三个测试用例的核心差异:
- 测试用例1中
test_1无强制关键字参数,参数传递逻辑符合常规,Numba编译执行正常。 - 测试用例2中
test_1通过*定义了3个强制关键字参数(d,e,f),test_2以位置参数形式传递这3个参数时,Numba能正常解析参数匹配规则。 - 测试用例3仅将
test_1的强制关键字参数数量增加到4个(d,e,f,g),test_2调用时传递3个位置参数+4个位置参数,触发Numba内部的tuple index out of range索引越界错误。
这是Numba 0.56.4版本的内部bug:在处理带有*分隔的强制关键字参数时,当强制关键字参数数量超过3个,Numba在解析参数匹配逻辑时出现索引越界问题。该问题已在Numba官方仓库提交相关记录。
内容的提问来源于stack exchange,提问作者eroot163pi
相关产品推荐
相关产品推荐

