-
Notifications
You must be signed in to change notification settings - Fork 146
Add numba overload for Nonzero #1289
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 3 commits
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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
from pytensor.scalar.basic import ScalarType | ||
from pytensor.scalar.math import Softplus | ||
from pytensor.sparse import SparseTensorType | ||
from pytensor.tensor.basic import Nonzero | ||
from pytensor.tensor.blas import BatchedDot | ||
from pytensor.tensor.math import Dot | ||
from pytensor.tensor.shape import Reshape, Shape, Shape_i, SpecifyShape | ||
|
@@ -744,3 +745,22 @@ def ifelse(cond, *args): | |
return res[0] | ||
|
||
return ifelse | ||
|
||
|
||
@numba_funcify.register(Nonzero) | ||
def numba_funcify_Nonzero(op, node, **kwargs): | ||
a = node.inputs[0] | ||
|
||
if a.ndim == 0: | ||
raise ValueError("Nonzero only supports non-scalar arrays.") | ||
|
||
@numba_njit | ||
def nonzero(a): | ||
if a.ndim == 1: | ||
indices = np.where(a != 0)[0] | ||
return indices.astype(np.int64) | ||
|
||
|
||
result_tuple = np.nonzero(a) | ||
return list(result_tuple) | ||
jessegrabowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return nonzero |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This input validation is done by the
nonzero
Op itself, there's no need to repeat it here.