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
'<' not supported between instances of 'NoneType' and 'int' #4163
Comments
Thanks for the report. Which version of Numba are you using? I think the issue is that the value of |
Thanks for your response. |
Thanks, 0.43+ has new features to deal with a really common problem pattern whereby type inference would fail because it would need to evaluate all branches in code and get stuck trying to resolve one of them that is actually dead, for example from numba import njit
@njit
def foo(a, b=None):
if b is None:
return a
else:
return a + b
print(foo(10)) the new feature detects dead branches and removes them to permit more code to work. The reason for the failure you are seeing is that in your sample code @jit(nopython=True)
def foo(array, a=None, b=None):
if a is None:
a = 0
if b is None or b > array.shape[0]:
b = array.shape[0]
if a < 0 or b < 0:
# raise error
if b < a:
# raise error a few potential fixes, 1) add a guard in branch pruning to abort when a comparison fails 2) full SSA base IR would probably help as the definition would be the phi-of-a and so not considered const-like? 3) thinking about 2) perhaps if there's a redefinition of a variable abort anyway. |
This makes it so that branch pruning for a given input arg is abandoned if an input arg is redefined in the user code as its value potentially becomes non-const (and no const-prop analysis yet to figure it out). Fixes numba#4163
Thank you a lot for clarifying things. What still confuses me is that the following code works:
If I understood you properly, here I would expect the value of |
No problem. What you are seeing is down to the choice of default value for This breaks: from numba import jit
import numpy as np
@jit(nopython=True)
def foo(array, a=None, b=None):
if b is None:
b = array.shape[0]
if b > array.shape[0]:
b = array.shape[0]
if a < 0:
raise ValueError("branch1")
if b < a:
raise ValueError("branch2")
print(foo(np.zeros(10))) but this works: from numba import jit
import numpy as np
@jit(nopython=True)
def foo(array, a=0 b=None): # <--- only change is default, a=0
if b is None:
b = array.shape[0]
if b > array.shape[0]:
b = array.shape[0]
if a < 0:
raise ValueError("branch1")
if b < a:
raise ValueError("branch2")
print(foo(np.zeros(10))) and this is also broken, note the function is the same as the working one, it's just that the input is from numba import jit
import numpy as np
@jit(nopython=True)
def foo(array, a=0, b=None):
if b is None:
b = array.shape[0]
if b > array.shape[0]:
b = array.shape[0]
if a < 0:
raise ValueError("branch1")
if b < a:
raise ValueError("branch2")
print(foo(np.zeros(10), a=None)) # <--- set a=None This all comes down to how pruning is working. There's two types of pruning possible, type based and value based, this problem is impacting type based pruning. The type based pruning works by finding the types of all the arguments and then seeing if any of them are Hope this helps. |
This makes it so that branch pruning for a given input arg is abandoned if an input arg is redefined in the user code as its value potentially becomes non-const (and no const-prop analysis yet to figure it out). Fixes #4163
Thank you for the explanation, very helpful indeed. |
After updating to numba , our code started to get the following error:
where
array
is a numpy array anda
andb
areint
types.This was working fine, however, after we got the new release we are getting the following error:
I could fix it like this:
The text was updated successfully, but these errors were encountered: