-
Notifications
You must be signed in to change notification settings - Fork 22
Leverage on dpctl.tensor implementation in dpnp.count_nonzero
#1962
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f633af
Leverage on dpctl.tensor implementation of count_nonzero
antonwolfy b6e8b05
Extend dpnp.get_result_array() to accept dpt.usm_ndarray
antonwolfy 0012e7a
Updated dpnp.mean() per review comment
antonwolfy 1bd08b5
Add more dpnp tests to cover different use cases
antonwolfy d695159
Merge branch 'master' into dpctl-count_nonzero
antonwolfy 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
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
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 |
---|---|---|
@@ -1,30 +1,124 @@ | ||
import numpy | ||
import pytest | ||
from dpctl.tensor._numpy_helper import AxisError | ||
from numpy.testing import ( | ||
assert_allclose, | ||
assert_equal, | ||
assert_raises, | ||
) | ||
|
||
import dpnp | ||
|
||
from .helper import ( | ||
get_all_dtypes, | ||
get_float_dtypes, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) | ||
@pytest.mark.parametrize("size", [2, 4, 8, 16, 3, 9, 27, 81]) | ||
def test_count_nonzero(dtype, size): | ||
if dtype != dpnp.bool: | ||
a = numpy.arange(size, dtype=dtype) | ||
else: | ||
a = numpy.resize(numpy.arange(2, dtype=dtype), size) | ||
class TestCountNonZero: | ||
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True)) | ||
@pytest.mark.parametrize("size", [2, 4, 8, 16, 3, 9, 27, 81]) | ||
def test_basic(self, dtype, size): | ||
if dtype != dpnp.bool: | ||
a = numpy.arange(size, dtype=dtype) | ||
else: | ||
a = numpy.resize(numpy.arange(2, dtype=dtype), size) | ||
|
||
for i in range(int(size / 2)): | ||
a[(i * (int(size / 3) - 1)) % size] = 0 | ||
for i in range(int(size / 2)): | ||
a[(i * (int(size / 3) - 1)) % size] = 0 | ||
|
||
ia = dpnp.array(a) | ||
ia = dpnp.array(a) | ||
|
||
np_res = numpy.count_nonzero(a) | ||
dpnp_res = dpnp.count_nonzero(ia) | ||
result = dpnp.count_nonzero(ia) | ||
expected = numpy.count_nonzero(a) | ||
assert_allclose(result, expected) | ||
|
||
assert_allclose(dpnp_res, np_res) | ||
@pytest.mark.parametrize("data", [[], [0], [1]]) | ||
def test_trivial(self, data): | ||
a = numpy.array(data) | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia) | ||
expected = numpy.count_nonzero(a) | ||
assert_allclose(result, expected) | ||
|
||
@pytest.mark.parametrize("data", [[], [0], [1]]) | ||
def test_trivial_boolean_dtype(self, data): | ||
a = numpy.array(data, dtype="?") | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia) | ||
expected = numpy.count_nonzero(a) | ||
assert_allclose(result, expected) | ||
|
||
@pytest.mark.parametrize("axis", [0, 1]) | ||
def test_axis_basic(self, axis): | ||
a = numpy.array([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia, axis=axis) | ||
expected = numpy.count_nonzero(a, axis=axis) | ||
assert_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("xp", [numpy, dpnp]) | ||
def test_axis_raises(self, xp): | ||
a = xp.array([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) | ||
|
||
assert_raises(ValueError, xp.count_nonzero, a, axis=(1, 1)) | ||
assert_raises(TypeError, xp.count_nonzero, a, axis="foo") | ||
assert_raises(AxisError, xp.count_nonzero, a, axis=3) | ||
|
||
# different exception type in numpy and dpnp | ||
with pytest.raises((ValueError, TypeError)): | ||
xp.count_nonzero(a, axis=xp.array([[1], [2]])) | ||
|
||
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True)) | ||
@pytest.mark.parametrize("axis", [0, 1, (0, 1), None]) | ||
def test_axis_all_dtypes(self, dt, axis): | ||
a = numpy.zeros((3, 3), dtype=dt) | ||
a[0, 0] = a[1, 0] = 1 | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia, axis=axis) | ||
expected = numpy.count_nonzero(a, axis=axis) | ||
assert_equal(result, expected) | ||
|
||
def test_axis_empty(self): | ||
axis = () | ||
a = numpy.array([[0, 0, 1], [1, 0, 1]]) | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia, axis=axis) | ||
expected = numpy.count_nonzero(a, axis=axis) | ||
assert_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("axis", [None, 0, 1]) | ||
def test_keepdims(self, axis): | ||
a = numpy.array([[0, 0, 1, 0], [0, 3, 5, 0], [7, 9, 2, 0]]) | ||
ia = dpnp.array(a) | ||
|
||
result = dpnp.count_nonzero(ia, axis=axis, keepdims=True) | ||
expected = numpy.count_nonzero(a, axis=axis, keepdims=True) | ||
assert_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True)) | ||
def test_out(self, dt): | ||
a = numpy.array([[0, 1, 0], [2, 0, 3]], dtype=dt) | ||
ia = dpnp.array(a) | ||
iout = dpnp.empty_like(ia, shape=ia.shape[1], dtype=dpnp.intp) | ||
|
||
result = dpnp.count_nonzero(ia, axis=0, out=iout) | ||
expected = numpy.count_nonzero(a, axis=0) # no out keyword | ||
assert_equal(result, expected) | ||
assert result is iout | ||
|
||
@pytest.mark.parametrize("dt", get_float_dtypes()) | ||
def test_out_floating_dtype(self, dt): | ||
a = dpnp.array([[0, 1, 0], [2, 0, 3]]) | ||
out = dpnp.empty_like(a, shape=a.shape[1], dtype=dt) | ||
assert_raises(ValueError, dpnp.count_nonzero, a, axis=0, out=out) | ||
|
||
def test_array_method(self): | ||
a = numpy.array([[1, 0, 0], [4, 0, 6]]) | ||
ia = dpnp.array(a) | ||
assert_equal(ia.nonzero(), a.nonzero()) |
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.