jit - 尽管没有在 numba 中使用 nopython,但“在 nopython 模式管道中失败”错误

问题描述 投票:0回答:1
我正在使用值函数迭代来解决具有多种状态的复杂动态规划问题。我想使用 numba/jit 来加速我的代码(并最终并行化 for 循环)。当我在代码中使用 @jit 装饰器时,尽管我没有调用 nopython 模式,但我还是收到了 nopython 错误。

@jit def vfi(cm, λ=1): vf_new = np.zeros_like(cm.vf) k_prime = np.zeros_like(cm.k_opt) for k_i in range(cm.k_grid_size): cm.set_k(cm.kgrid[k_i]) for b_i in range(cm.cb_grid_size): cm.set_b(cm.cb_mesh[0][b_i]) for s_i in range(2): cm.set_s(s_i) b_prime = cm.b_prime(cm.kgrid) vf_interp = RegularGridInterpolator((cm.kgrid, cm.cb_mesh[0],cm.sgrid), cm.vf) objective = cm.F - cm.T(cm.kgrid) - cm.C(cm.kgrid) + cm.β*cm.p*np.array([vf_interp(x) for x in zip(cm.kgrid,b_prime,np.zeros_like(b_prime))]) + cm.β*(1-cm.p)*np.array([vf_interp(x) for x in zip(cm.kgrid,b_prime,np.ones_like(b_prime))]) vf_new[k_i,b_i,s_i] = np.max(objective) k_prime[k_i,b_i,s_i] = np.argmax(objective) error = np.max(np.abs(cm.vf - vf_new)) cm.vf = cm.vf + λ*(vf_new-cm.vf) cm.k_opt = k_prime return error qe.util.tic() cm = CarrybacksModel() error = 10000000 itern=0 tol = 1e-5 while error>tol: error = vfi(cm) itern+=1 print(f"Iteration number {itern}, error = {error}.") print(f"Completed in {itern} iterations.") qe.util.toc()
返回以下错误:

> --------------------------------------------------------------------------- TypingError Traceback (most recent call last) Cell In[57], line 7 5 tol = 1e-5 6 while error>tol: ----> 7 error = vfi(cm) 8 itern+=1 9 print(f"Iteration number {itern}, error = {error}.") File ~\miniconda3\Lib\site-packages\numba\core\dispatcher.py:468, 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) --> 468 error_rewrite(e, 'typing') 469 except errors.UnsupportedError as e: 470 # Something unsupported is present in the user code, add help info 471 error_rewrite(e, 'unsupported_error') File ~\miniconda3\Lib\site-packages\numba\core\dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.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) Untyped global name 'RegularGridInterpolator': Cannot determine Numba type of <class 'type'> File "..\..\..\..\AppData\Local\Temp\ipykernel_16688\910653365.py", line 12: <source missing, REPL/exec in use?> This error may have been caused by the following argument(s): - argument 0: Cannot determine Numba type of <class '__main__.CarrybacksModel'>
我知道我不能使用 njit/nopython 模式,因为我正在定义自己的类,并且 scipy 的 RegularGridInterpolator 似乎不兼容。然而,我认为通过使用 @jit 装饰器而不是 @njit 那么 nopython 永远不应该被调用。为什么我收到 nopython 错误?

如果有必要,我愿意使用不同的函数而不是 RegularGridInterpolator - 我自己编写了一个函数并用 @jit 装饰它,但仍然收到相同的错误。

python scipy dynamic-programming numba jit
1个回答
0
投票
我认为 nopython 模式是默认的。

来自 numba github 存储库中的

decorators.py

if nopython is False: msg = ("The keyword argument 'nopython=False' was supplied. From " "Numba 0.59.0 the default is True and supplying this argument " "has no effect.")
我认为您正在寻找的强制 python 模式的选项称为 

forceobj

© www.soinside.com 2019 - 2024. All rights reserved.