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
gcd, lcm, subtract, sign, and negative don't match numpy behavior with bool inputs #7922
Comments
Two more: >>> @numba.njit
... def subtract(x, y):
... return np.subtract(x, y)
...
>>> subtract(True, True)
0
>>> np.subtract(True, True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead. |
Thanks for the report, I can reproduce. |
Think this might need some logic like: if not set([x.char for x in np_input_types]).intersection(set([x for x in ufunc_inputs])):
continue around here: numba/numba/np/numpy_support.py Lines 521 to 523 in fa3630e
i.e. once a loop has been found, require that at least one of the input types is present in the selected loop version. This may however be too restrictive. |
I am able to reproduce the incorrect behaviour of Numba. In addition, I added your check as follows and I get the following output with diff diff --git a/numba/np/numpy_support.py b/numba/np/numpy_support.py
index eea1b612d..27b1558cd 100644
--- a/numba/np/numpy_support.py
+++ b/numba/np/numpy_support.py
@@ -521,6 +521,13 @@ def ufunc_find_matching_loop(ufunc, arg_types):
if found:
# Found: determine the Numba types for the loop's inputs and
# outputs.
+ np_input_types_set = set([x.char for x in np_input_types])
+ output_types_set = set([x for x in ufunc_inputs])
+ if not np_input_types_set.intersection(output_types_set):
+ print("found but not correct: ", [x.char for x in np_input_types], [x for x in ufu
nc_inputs])
+ continue
+ else:
+ print("found and correct: ", [x.char for x in np_input_types], [x for x in ufunc_i
nputs])
try:
inputs = choose_types(input_types, ufunc_inputs)
outputs = choose_types(output_types, ufunc_outputs)
@@ -534,8 +541,10 @@ def ufunc_find_matching_loop(ufunc, arg_types):
except errors.NumbaNotImplementedError:
# One of the selected dtypes isn't supported by Numba
# (e.g. float16), try other candidates
+ print("errored")
continue
else:
+ print("returning, :", inputs, outputs, candidate)
return UFuncLoopSpec(inputs, outputs, candidate)
return None Output found and correct: ['d', 'd'] ['d', 'd']
returning, : [float64, float64] [float64] dd->d
found and correct: ['d', 'd'] ['d', 'd']
returning, : [float64, float64] [float64] dd->d
found but not correct: ['?'] ['b']
found but not correct: ['?'] ['B']
found but not correct: ['?'] ['h']
found but not correct: ['?'] ['H']
found but not correct: ['?'] ['i']
found but not correct: ['?'] ['I']
found but not correct: ['?'] ['l']
found but not correct: ['?'] ['L']
found but not correct: ['?'] ['q']
found but not correct: ['?'] ['Q']
found but not correct: ['?'] ['f']
found but not correct: ['?'] ['d']
found but not correct: ['?'] ['g']
found but not correct: ['?'] ['F']
found but not correct: ['?'] ['D']
found but not correct: ['?'] ['G']
found but not correct: ['?'] ['b']
found but not correct: ['?'] ['B']
found but not correct: ['?'] ['h']
found but not correct: ['?'] ['H']
found but not correct: ['?'] ['i']
found but not correct: ['?'] ['I']
found but not correct: ['?'] ['l']
found but not correct: ['?'] ['L']
found but not correct: ['?'] ['q']
found but not correct: ['?'] ['Q']
found but not correct: ['?'] ['f']
found but not correct: ['?'] ['d']
found but not correct: ['?'] ['g']
found but not correct: ['?'] ['F']
found but not correct: ['?'] ['D']
found but not correct: ['?'] ['G']
found but not correct: ['?'] ['b']
found but not correct: ['?'] ['B']
found but not correct: ['?'] ['h']
found but not correct: ['?'] ['H']
found but not correct: ['?'] ['i']
found but not correct: ['?'] ['I']
found but not correct: ['?'] ['l']
found but not correct: ['?'] ['L']
found but not correct: ['?'] ['q']
found but not correct: ['?'] ['Q']
found but not correct: ['?'] ['f']
found but not correct: ['?'] ['d']
found but not correct: ['?'] ['g']
found but not correct: ['?'] ['F']
found but not correct: ['?'] ['D']
found but not correct: ['?'] ['G']
found but not correct: ['?'] ['b']
found but not correct: ['?'] ['B']
found but not correct: ['?'] ['h']
found but not correct: ['?'] ['H']
found but not correct: ['?'] ['i']
found but not correct: ['?'] ['I']
found but not correct: ['?'] ['l']
found but not correct: ['?'] ['L']
found but not correct: ['?'] ['q']
found but not correct: ['?'] ['Q']
found but not correct: ['?'] ['f']
found but not correct: ['?'] ['d']
found but not correct: ['?'] ['g']
found but not correct: ['?'] ['F']
found but not correct: ['?'] ['D']
found but not correct: ['?'] ['G']
Traceback (most recent call last):
File "../debug.py", line 320, in <module>
test_np_gcd_lcm_sub()
File "../debug.py", line 310, in test_np_gcd_lcm_sub
print(sign(True))
File "/Users/czgdp1807/numba_project/numba/numba/core/dispatcher.py", line 468, in _compile_for_args
error_rewrite(e, 'typing')
File "/Users/czgdp1807/numba_project/numba/numba/core/dispatcher.py", line 409, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<ufunc 'sign'>) found for signature:
>>> sign(bool)
There are 2 candidate implementations:
- Of which 2 did not match due to:
Overload in function 'Numpy_rules_ufunc.generic': File: numba/core/typing/npydecl.py: Line 98.
With argument(s): '(bool)':
Rejected as the implementation raised a specific error:
TypingError: can't resolve ufunc sign for types [bool]
raised from /Users/czgdp1807/numba_project/numba/numba/core/typing/npydecl.py:107
During: resolving callee type: Function(<ufunc 'sign'>)
During: typing of call at ../debug.py (308)
File "../debug.py", line 308:
def sign(x):
return np.sign(x)
^ I have a couple of questions from the output,
Thanks. |
Reporting a bug
visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
i.e. it's possible to run as 'python bug.py'.
I noticed that numba doesn't match NumPy's behavior for
np.gcd
,np.lcm
, andnp.sign
with bool inputs. Notably, NumPy raises, but numba treats bools as ints:I can get by with the current behavior, but thought you would want to know.
The text was updated successfully, but these errors were encountered: