-
Notifications
You must be signed in to change notification settings - Fork 131
Fix numba dispatch not returning arrays or wrong dtypes #1406
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
Changes from all commits
1de6bae
4e42b84
e7f4cf9
70fb7cf
2672b45
4a25c9c
614ffdd
bff6d08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ def numba_funcify_Det(op, node, **kwargs): | |
|
||
@numba_basic.numba_njit(inline="always") | ||
def det(x): | ||
return numba_basic.direct_cast(np.linalg.det(inputs_cast(x)), out_dtype) | ||
return np.array(np.linalg.det(inputs_cast(x))).astype(out_dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider adding a comment explaining why wrapping the output with np.array is necessary to ensure consistency in array outputs, which aids in maintainability. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
return det | ||
|
||
|
@@ -68,8 +68,8 @@ def numba_funcify_SLogDet(op, node, **kwargs): | |
def slogdet(x): | ||
sign, det = np.linalg.slogdet(inputs_cast(x)) | ||
return ( | ||
numba_basic.direct_cast(sign, out_dtype_1), | ||
numba_basic.direct_cast(det, out_dtype_2), | ||
np.array(sign).astype(out_dtype_1), | ||
np.array(det).astype(out_dtype_2), | ||
) | ||
|
||
return slogdet | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ | |
numba = pytest.importorskip("numba") | ||
|
||
import pytensor.scalar as ps | ||
import pytensor.scalar.math as psm | ||
import pytensor.tensor as pt | ||
import pytensor.tensor.math as ptm | ||
from pytensor import config, shared | ||
|
@@ -260,9 +259,12 @@ def compare_numba_and_py( | |
if assert_fn is None: | ||
|
||
def assert_fn(x, y): | ||
return np.testing.assert_allclose(x, y, rtol=1e-4) and compare_shape_dtype( | ||
x, y | ||
) | ||
np.testing.assert_allclose(x, y, rtol=1e-4, strict=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strict=True covers the shape/dtype mismatch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old logic with return x and y, didn't trigger y (the |
||
# Make sure we don't have one input be a np.ndarray while the other is not | ||
if isinstance(x, np.ndarray): | ||
assert isinstance(y, np.ndarray), "y is not a NumPy array, but x is" | ||
else: | ||
assert not isinstance(y, np.ndarray), "y is a NumPy array, but x is not" | ||
|
||
if any( | ||
inp.owner is not None | ||
|
@@ -295,8 +297,8 @@ def assert_fn(x, y): | |
test_inputs_copy = (inp.copy() for inp in test_inputs) if inplace else test_inputs | ||
numba_res = pytensor_numba_fn(*test_inputs_copy) | ||
if isinstance(graph_outputs, tuple | list): | ||
for j, p in zip(numba_res, py_res, strict=True): | ||
assert_fn(j, p) | ||
for numba_res_i, python_res_i in zip(numba_res, py_res, strict=True): | ||
assert_fn(numba_res_i, python_res_i) | ||
else: | ||
assert_fn(numba_res, py_res) | ||
|
||
|
@@ -640,48 +642,6 @@ def test_Dot(x, y, exc): | |
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"x, exc", | ||
[ | ||
( | ||
(ps.float64(), np.array(0.0, dtype="float64")), | ||
None, | ||
), | ||
( | ||
(ps.float64(), np.array(-32.0, dtype="float64")), | ||
None, | ||
), | ||
( | ||
(ps.float64(), np.array(-40.0, dtype="float64")), | ||
None, | ||
), | ||
( | ||
(ps.float64(), np.array(32.0, dtype="float64")), | ||
None, | ||
), | ||
( | ||
(ps.float64(), np.array(40.0, dtype="float64")), | ||
None, | ||
), | ||
( | ||
(ps.int64(), np.array(32, dtype="int64")), | ||
None, | ||
), | ||
], | ||
) | ||
def test_Softplus(x, exc): | ||
x, x_test_value = x | ||
g = psm.Softplus(ps.upgrade_to_float)(x) | ||
|
||
cm = contextlib.suppress() if exc is None else pytest.warns(exc) | ||
with cm: | ||
compare_numba_and_py( | ||
[x], | ||
[g], | ||
[x_test_value], | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"x, y, exc", | ||
[ | ||
|
Uh oh!
There was an error while loading. Please reload this page.