New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot compile in nopython mode for a "specialized" function #5817
Comments
I have also tried to rewrite the above in a closure style code (inspired by the docs example https://numba.pydata.org/numba-doc/latest/user/faq.html?highlight=function#can-i-pass-a-function-as-an-argument-to-a-jitted-function, but this fails too: @nb.jit
def complex_func(runtime_param, predef_param):
return np.cos(predef_param) ** runtime_param
def make_simpler_func(complex_func):
PARAM = 5.
@nb.jit(forceobj=True)
def simpler_func(runtime_param):
return complex_func(runtime_param, PARAM)
return simpler_func
@nb.njit
def assemble_matrix(N, simpler_func):
mat = np.zeros((N, N))
mapping = np.arange(N)
for i in range(N):
I = mapping[i]
for j in range(N):
J = mapping[j]
mat[i, j] = simpler_func(I + J)
return mat
def main():
simpler_func = make_simpler_func(complex_func)
assemble_matrix(5, simpler_func) Traceback: ---------------------------------------------------------------------------
TypingError Traceback (most recent call last)
<ipython-input-39-263240bbee7e> in <module>
----> 1 main()
<ipython-input-34-bfc00d884e85> in main()
27 def main():
28 simpler_func = make_simpler_func(complex_func)
---> 29 assemble_matrix(5, simpler_func)
C:\Anaconda3\envs\pyrus_dev\lib\site-packages\numba\core\dispatcher.py in _compile_for_args(self, *args, **kws)
399 e.patch_message(msg)
400
--> 401 error_rewrite(e, 'typing')
402 except errors.UnsupportedError as e:
403 # Something unsupported is present in the user code, add help info
C:\Anaconda3\envs\pyrus_dev\lib\site-packages\numba\core\dispatcher.py in error_rewrite(e, issue_type)
342 raise e
343 else:
--> 344 reraise(type(e), e, None)
345
346 argtypes = []
C:\Anaconda3\envs\pyrus_dev\lib\site-packages\numba\core\utils.py in reraise(tp, value, tb)
78 value = tp()
79 if value.__traceback__ is not tb:
---> 80 raise value.with_traceback(tb)
81 raise value
82
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of type(CPUDispatcher(<function make_simpler_func.<locals>.simpler_func at 0x00000278B1E25318>)) with parameters (int64)
* parameterized
[1] During: resolving callee type: type(CPUDispatcher(<function make_simpler_func.<locals>.simpler_func at 0x00000278B1E25318>))
[2] During: typing of call at <ipython-input-34-bfc00d884e85> (24)
File "<ipython-input-34-bfc00d884e85>", line 24:
def assemble_matrix(N, simpler_func):
<source elided>
J = mapping[j]
mat[i, j] = simpler_func(I + J)
^ |
Can you explain a bit about what prevents you from jitting from numba import jit, njit, objmode
import numpy as np
PARAM = 5.
@njit
def complex_func(runtime_param, predef_param):
return np.cos(predef_param) ** runtime_param
@njit
def simpler_func(runtime_param):
return complex_func(runtime_param, PARAM)
@njit
def assemble_matrix(N):
mat = np.zeros((N, N))
mapping = np.arange(N)
for i in range(N):
I = mapping[i]
for j in range(N):
J = mapping[j]
mat[i, j] = simpler_func(I + J)
return mat
def main():
assemble_matrix(5)
if __name__ == '__main__':
main() ? |
Not a solution, but a clarification as to what may be going wrong here: from numba import jit, njit
@jit(forceobj=True)
def f(x):
return x
@njit
def g(x):
return f(x)
g(1)
Maybe there would be a way to do this by explicitly using the object mode context manager but this may bring with it a hefty performance penalty. |
Hi @gmarkall and @HPLegion
On my machine this does not work, while @gmarkall's original example indeed works fine. Based on your inputs, I think I can now reframe my question as: is it possible to have an njitted function call functions that cannot be njitted? @HPLegion you said it might be possible through the object mode context manager. I'll try looking into it. Thanks for your help! |
@flothesof perhaps take a look at http://numba.pydata.org/numba-doc/latest/user/withobjmode.html#the-objmode-context-manager seem like it would fit your use case? |
Indeed, this might actually allow me to do it "as is". In the case I was working on when I submitted the issue, the blocker really was the scipy.special.factorial2 function. Reimplementing it with numba-accelerated python let me do what I wanted. Given that my problem is solved and a workaround provided for further cases, I'll close the issue now. Feel free to reopen if you feel it's a better choice. Thanks to all for your help. Regards |
Hi,
I am working on a physics problem where I assemble a matrix using an analytic function which has parameters of two kinds:
predef_param
below)runtime_param
below)To make my assembling code more general, I have tried to separate both concerns (ahead of time vs. runtime) so that I can reuse it.
This results in the fact that I need to have a function
simpler_func
with less parameters calling the originalcomplex_func
.I have code that looks approximately like this:
Running the
main
function results in the following error with numba 0.49.1:Is there a way to make this work? What am I doing wrong? Does this have something to do with the fact that functions can't be passed around well?
Thank you in advance for your help.
Florian
The text was updated successfully, but these errors were encountered: