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

0 投票
1 回答
41 浏览
提问于 2025-04-14 16:04

我正在用值函数迭代的方法来解决一个复杂的动态规划问题,这个问题有很多状态。我想用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装饰,但仍然收到了同样的错误。

1 个回答

3

我觉得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

撰写回答