-
Notifications
You must be signed in to change notification settings - Fork 493
【KernelGen】Add nonzero_numpy operator #1755
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
Merged
w1120029931-bit
merged 1 commit into
flagos-ai:master
from
Schopenhauer-loves-Hegel:auto-gen/nonzero_numpy
May 11, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import pytest | ||
| import torch | ||
|
|
||
| from . import base, consts | ||
|
|
||
|
|
||
| @pytest.mark.nonzero_numpy | ||
| def test_nonzero_numpy(): | ||
| bench = base.GenericBenchmark2DOnly( | ||
| input_fn=base.unary_input_fn, | ||
| op_name="nonzero_numpy", | ||
| torch_op=torch.ops.aten.nonzero_numpy, | ||
| dtypes=consts.FLOAT_DTYPES + consts.INT_DTYPES + consts.BOOL_DTYPES, | ||
| ) | ||
| bench.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import logging | ||
|
|
||
| from flag_gems.ops.nonzero import nonzero | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def nonzero_numpy(inp): | ||
| """ | ||
| Returns a tuple of 1D tensors, one for each dimension of the input, | ||
| containing the indices of the non-zero elements in that dimension. | ||
|
|
||
| This is equivalent to torch.nonzero(...).T or numpy.nonzero() behavior. | ||
| """ | ||
| logger.debug("GEMS NONZERO_NUMPY") | ||
|
|
||
| # Use the existing nonzero implementation which returns shape [N, ndim] | ||
| out = nonzero(inp, as_tuple=False) | ||
|
|
||
| # Unbind along dim=1 to get ndim tensors of shape [N] | ||
| # Convert to list since aten::nonzero_numpy returns Tensor[] | ||
| return list(out.unbind(dim=1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import pytest | ||
| import torch | ||
|
|
||
| import flag_gems | ||
|
|
||
| from .accuracy_utils import ( | ||
| BOOL_TYPES, | ||
| FLOAT_DTYPES, | ||
| INT_DTYPES, | ||
| REDUCTION_SHAPES, | ||
| gems_assert_equal, | ||
| to_reference, | ||
| ) | ||
|
|
||
| NONZERO_SHAPES = REDUCTION_SHAPES | ||
|
|
||
|
|
||
| @pytest.mark.nonzero_numpy | ||
| @pytest.mark.parametrize("shape", NONZERO_SHAPES) | ||
| @pytest.mark.parametrize("dtype", FLOAT_DTYPES + INT_DTYPES + BOOL_TYPES) | ||
| def test_nonzero_numpy(shape, dtype): | ||
| if dtype == torch.bool: | ||
| inp = torch.randint(0, 2, shape, dtype=torch.int, device=flag_gems.device).to( | ||
| dtype | ||
| ) | ||
| elif dtype in INT_DTYPES: | ||
| inp = torch.randint(-3, 3, shape, device=flag_gems.device).to(dtype) | ||
| else: | ||
| inp = torch.randn(shape, dtype=dtype, device=flag_gems.device) | ||
| ref_inp = to_reference(inp, False) | ||
|
|
||
| ref_out = torch.ops.aten.nonzero_numpy(ref_inp) | ||
| with flag_gems.use_gems(): | ||
| res_out = torch.ops.aten.nonzero_numpy(inp) | ||
|
|
||
| assert len(res_out) == len(ref_out), "Number of output tensors should match" | ||
| for res_t, ref_t in zip(res_out, ref_out): | ||
| gems_assert_equal(res_t, ref_t) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.