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
Unused closures in njitted function are wrongly reported as escaping #7254
Comments
I can reproduce this. My intuition is that:
|
Thanks for reproducing @gmarkall If the function is used the error message does dissapear: import numba as nb
@nb.njit
def f(a,x):
def g(b):
return a+b
return g(x)
print(nb.version_info)
f(1,2)
If the captured value is not an argument it still fails: import numpy as np
import numba as nb
@nb.njit
def f(x):
c = np.random.rand(1)
def g(b):
return c+b
return 1
print(nb.version_info)
f(1)
|
I think the message perhaps ought to say "a function that may escape", as that covers the case of it not escaping. As to the above, any closed over variables must be compile time constants, calling |
NOTE: The good first issue task is to alter the error message to read |
I feel I need to clarify that this works(first example here ). So if I'm reading the replies correctly, when the inner function is used, in this case it is inlined, so it's directly referencing the argument variable. When it is not used, it's compiled, and an attempt is made to capture the argument variable, which fails because it is not a constant. The context for leaving the function unused but in the source code is that closures are one way of capturing default arguments, and in long functions with many helpers, I found myself frequently having to comment out helpers. This however, in my specific case, with compiling taking 3mn. I have since moved to using structrefs as a way to capture constant values with minimal overhead. This allows me to split functions without having to pass many arguments around. And the whole algorithm takes 1.5mn to compile, i.e. half of the original original time, and it's easy to incrementally modify it. However, I'm not sure how one could prevent confusion that arises from first seeing no issues because the variable is being inlined. |
Reporting a bug
visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
i.e. it's possible to run as 'python bug.py'.
MVE:
The text was updated successfully, but these errors were encountered: