I am using value function iteration to solve a complex dynamic programming problem with many states. I want to use numba/jit to speed up my code (and eventually parallelize the for loops). When I use the @jit
decorator in my code, I am getting a nopython
error despite me not invoking nopython
mode.
@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()
Returns the following error:
> ---------------------------------------------------------------------------
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'>
I am aware that I cannot use njit/nopython mode since I am defining my own class and scipy's RegularGridInterpolator
doesn't seem to be compatible. However, I thought that by using the @jit
decorator instead of @njit
then nopython
should never be invoked. Why am I receiving a nopython
error?
I am open to using a different function instead of RegularGridInterpolator
if necessary - I coded one up myself and decorated that with @jit
but still received the same error.
I think nopython mode is the default.
From decorators.py in the numba github repo:
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.")
I think the option you are looking for to force the python mode is called forceobj
.