Skip to content

Commit c1612c6

Browse files
author
Vahid Tavanashad
committed
address comments
1 parent 03c51af commit c1612c6

File tree

6 files changed

+75
-72
lines changed

6 files changed

+75
-72
lines changed

dpnp/tests/helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def get_float_complex_dtypes(no_float16=True, device=None):
147147
def get_abs_array(data, dtype=None):
148148
if numpy.issubdtype(dtype, numpy.unsignedinteger):
149149
data = numpy.abs(data)
150-
return numpy.array(data, dtype=dtype)
150+
# it is better to use astype with the default casting=unsafe
151+
# otherwise, we need to skip test for cases where overflow occurs
152+
return numpy.array(data).astype(dtype)
151153

152154

153155
def get_all_dtypes(

dpnp/tests/test_linalg.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class TestCholesky:
151151
[[[7, 2], [2, 7]], [[8, 3], [3, 8]]],
152152
],
153153
],
154-
ids=["2D_array", "3D_array", "4D_array"],
154+
ids=["2D", "3D", "4D"],
155155
)
156156
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
157157
def test_cholesky(self, array, dtype):
@@ -171,7 +171,7 @@ def test_cholesky(self, array, dtype):
171171
[[[7, 2], [2, 7]], [[8, 3], [3, 8]]],
172172
],
173173
],
174-
ids=["2D_array", "3D_array", "4D_array"],
174+
ids=["2D", "3D", "4D"],
175175
)
176176
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
177177
def test_cholesky_upper(self, array, dtype):
@@ -191,16 +191,14 @@ def test_cholesky_upper(self, array, dtype):
191191
)
192192
else:
193193
reconstructed = res_reshaped[idx].T @ res_reshaped[idx]
194-
assert_dtype_allclose(
195-
reconstructed, ia_reshaped[idx], check_type=False
196-
)
194+
assert dpnp.allclose(reconstructed, ia_reshaped[idx])
197195
else:
198196
# Reconstruct the matrix using the Cholesky decomposition result
199197
if dpnp.issubdtype(dtype, dpnp.complexfloating):
200198
reconstructed = result.T.conj() @ result
201199
else:
202200
reconstructed = result.T @ result
203-
assert_dtype_allclose(reconstructed, ia, check_type=False)
201+
assert dpnp.allclose(reconstructed, ia)
204202

205203
# upper parameter support will be added in numpy 2.0 version
206204
@testing.with_requires("numpy>=2.0")
@@ -214,7 +212,7 @@ def test_cholesky_upper(self, array, dtype):
214212
[[[7, 2], [2, 7]], [[8, 3], [3, 8]]],
215213
],
216214
],
217-
ids=["2D_array", "3D_array", "4D_array"],
215+
ids=["2D", "3D", "4D"],
218216
)
219217
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
220218
def test_cholesky_upper_numpy(self, array, dtype):
@@ -382,7 +380,7 @@ class TestDet:
382380
[[[1, 3], [3, 1]], [[0, 1], [1, 3]]],
383381
],
384382
],
385-
ids=["2D_array", "3D_array", "4D_array"],
383+
ids=["2D", "3D", "4D"],
386384
)
387385
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
388386
def test_det(self, array, dtype):
@@ -1683,7 +1681,7 @@ class TestInv:
16831681
[[[1, 3], [3, 1]], [[0, 1], [1, 3]]],
16841682
],
16851683
],
1686-
ids=["2D_array", "3D_array", "4D_array"],
1684+
ids=["2D", "3D", "4D"],
16871685
)
16881686
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
16891687
def test_inv(self, array, dtype):
@@ -2851,7 +2849,7 @@ def check_decomposition(
28512849
dpnp_diag_s[..., i, i] = dp_s[..., i]
28522850
reconstructed = dpnp.dot(dp_u, dpnp.dot(dpnp_diag_s, dp_vt))
28532851

2854-
assert_allclose(dp_a, reconstructed, rtol=tol, atol=1e-4)
2852+
assert dpnp.allclose(dp_a, reconstructed, rtol=tol, atol=1e-4)
28552853

28562854
assert_allclose(dp_s, np_s, rtol=tol, atol=1e-03)
28572855

@@ -3034,7 +3032,7 @@ def test_pinv(self, dtype, shape):
30343032
else: # a.ndim > 2
30353033
reconstructed = dpnp.matmul(a_dp, dpnp.matmul(B_dp, a_dp))
30363034

3037-
assert_allclose(reconstructed, a_dp, rtol=tol, atol=tol)
3035+
assert dpnp.allclose(reconstructed, a_dp, rtol=tol, atol=tol)
30383036

30393037
@pytest.mark.parametrize("dtype", get_float_complex_dtypes())
30403038
@pytest.mark.parametrize(
@@ -3054,7 +3052,7 @@ def test_pinv_hermitian(self, dtype, shape):
30543052
tol = self._tol
30553053

30563054
reconstructed = dpnp.dot(dpnp.dot(a_dp, B_dp), a_dp)
3057-
assert_allclose(reconstructed, a_dp, rtol=tol, atol=tol)
3055+
assert dpnp.allclose(reconstructed, a_dp, rtol=tol, atol=tol)
30583056

30593057
# rtol kwarg was added in numpy 2.0
30603058
@testing.with_requires("numpy>=2.0")

dpnp/tests/test_search.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,83 @@
99
generate_random_numpy_array,
1010
get_all_dtypes,
1111
)
12+
from .third_party.cupy import testing
1213

1314

15+
@testing.parameterize(
16+
*testing.product(
17+
{
18+
"func": ("argmax", "argmin"),
19+
}
20+
)
21+
)
1422
class TestArgmaxArgmin:
15-
@pytest.mark.parametrize("func", ["argmax", "argmin"])
1623
@pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2])
1724
@pytest.mark.parametrize("keepdims", [False, True])
1825
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
19-
def test_func(self, func, axis, keepdims, dtype):
26+
def test_func(self, axis, keepdims, dtype):
2027
a = generate_random_numpy_array((4, 4, 6, 8), dtype=dtype)
2128
ia = dpnp.array(a)
2229

23-
expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims)
24-
result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims)
30+
expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims)
31+
result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims)
2532
assert_array_equal(result, expected)
2633

27-
@pytest.mark.parametrize("func", ["argmax", "argmin"])
28-
def test_out(self, func):
34+
def test_out(self):
2935
a = generate_random_numpy_array((2, 2, 3), dtype=numpy.float32)
3036
ia = dpnp.array(a)
3137

3238
# out is dpnp_array
33-
expected = getattr(numpy, func)(a, axis=0)
39+
expected = getattr(numpy, self.func)(a, axis=0)
3440
dpnp_out = dpnp.empty(expected.shape, dtype=expected.dtype)
35-
result = getattr(dpnp, func)(ia, axis=0, out=dpnp_out)
41+
result = getattr(dpnp, self.func)(ia, axis=0, out=dpnp_out)
3642
assert dpnp_out is result
3743
assert_array_equal(result, expected)
3844

3945
# out is usm_ndarray
4046
dpt_out = dpt.empty(expected.shape, dtype=expected.dtype)
41-
result = getattr(dpnp, func)(ia, axis=0, out=dpt_out)
47+
result = getattr(dpnp, self.func)(ia, axis=0, out=dpt_out)
4248
assert dpt_out is result.get_array()
4349
assert_array_equal(result, expected)
4450

4551
# out is a numpy array -> TypeError
4652
result = numpy.empty_like(expected)
4753
with pytest.raises(TypeError):
48-
getattr(dpnp, func)(ia, axis=0, out=result)
54+
getattr(dpnp, self.func)(ia, axis=0, out=result)
4955

5056
# out shape is incorrect -> ValueError
5157
result = dpnp.array(numpy.zeros((2, 2)), dtype=dpnp.intp)
5258
with pytest.raises(ValueError):
53-
getattr(dpnp, func)(ia, axis=0, out=result)
59+
getattr(dpnp, self.func)(ia, axis=0, out=result)
5460

55-
@pytest.mark.parametrize("func", ["argmax", "argmin"])
5661
@pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True))
5762
@pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True))
58-
def test_out_dtype(self, func, arr_dt, out_dt):
63+
def test_out_dtype(self, arr_dt, out_dt):
5964
a = generate_random_numpy_array((2, 2, 3), dtype=arr_dt)
6065
out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt)
6166
ia, iout = dpnp.array(a), dpnp.array(out)
6267

6368
if numpy.can_cast(out.dtype, numpy.intp, casting="safe"):
64-
result = getattr(dpnp, func)(ia, out=iout, axis=1)
65-
expected = getattr(numpy, func)(a, out=out, axis=1)
69+
result = getattr(dpnp, self.func)(ia, out=iout, axis=1)
70+
expected = getattr(numpy, self.func)(a, out=out, axis=1)
6671
assert_array_equal(result, expected)
6772
assert result is iout
6873
else:
69-
assert_raises(TypeError, getattr(numpy, func), a, out=out, axis=1)
70-
assert_raises(TypeError, getattr(dpnp, func), ia, out=iout, axis=1)
74+
assert_raises(
75+
TypeError, getattr(numpy, self.func), a, out=out, axis=1
76+
)
77+
assert_raises(
78+
TypeError, getattr(dpnp, self.func), ia, out=iout, axis=1
79+
)
7180

72-
@pytest.mark.parametrize("func", ["argmax", "argmin"])
7381
@pytest.mark.parametrize("axis", [None, 0, 1, -1])
7482
@pytest.mark.parametrize("keepdims", [False, True])
75-
def test_ndarray(self, func, axis, keepdims):
83+
def test_ndarray(self, axis, keepdims):
7684
a = generate_random_numpy_array((4, 6, 8), dtype=numpy.float32)
7785
ia = dpnp.array(a)
7886

79-
expected = getattr(a, func)(axis=axis, keepdims=keepdims)
80-
result = getattr(ia, func)(axis=axis, keepdims=keepdims)
87+
expected = getattr(a, self.func)(axis=axis, keepdims=keepdims)
88+
result = getattr(ia, self.func)(axis=axis, keepdims=keepdims)
8189
assert_array_equal(result, expected)
8290

8391

dpnp/tests/test_statistics.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -600,84 +600,87 @@ def test_true_rowvar(self):
600600
assert_allclose(result, expected)
601601

602602

603+
@testing.parameterize(
604+
*testing.product(
605+
{
606+
"func": ("max", "min"),
607+
}
608+
)
609+
)
603610
class TestMaxMin:
604-
@pytest.mark.parametrize("func", ["max", "min"])
605611
@pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2, (1, 2), (0, -2)])
606612
@pytest.mark.parametrize("keepdims", [False, True])
607613
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
608-
def test_func(self, func, axis, keepdims, dtype):
614+
def test_func(self, axis, keepdims, dtype):
609615
a = numpy.arange(768, dtype=dtype).reshape((4, 4, 6, 8))
610616
ia = dpnp.array(a)
611617

612-
expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims)
613-
result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims)
618+
expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims)
619+
result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims)
614620
assert_dtype_allclose(result, expected)
615621

616-
@pytest.mark.parametrize("func", ["max", "min"])
617622
@pytest.mark.parametrize("axis", [None, 0, 1, -1])
618623
@pytest.mark.parametrize("keepdims", [False, True])
619-
def test_bool(self, func, axis, keepdims):
624+
def test_bool(self, axis, keepdims):
620625
a = numpy.arange(2, dtype=numpy.bool_)
621626
a = numpy.tile(a, (2, 2))
622627
ia = dpnp.array(a)
623628

624-
expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims)
625-
result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims)
629+
expected = getattr(numpy, self.func)(a, axis=axis, keepdims=keepdims)
630+
result = getattr(dpnp, self.func)(ia, axis=axis, keepdims=keepdims)
626631
assert_dtype_allclose(result, expected)
627632

628-
@pytest.mark.parametrize("func", ["max", "min"])
629-
def test_out(self, func):
633+
def test_out(self):
630634
a = numpy.arange(12, dtype=numpy.float32).reshape((2, 2, 3))
631635
ia = dpnp.array(a)
632636

633637
# out is dpnp_array
634-
expected = getattr(numpy, func)(a, axis=0)
638+
expected = getattr(numpy, self.func)(a, axis=0)
635639
dpnp_out = dpnp.empty(expected.shape, dtype=expected.dtype)
636-
result = getattr(dpnp, func)(ia, axis=0, out=dpnp_out)
640+
result = getattr(dpnp, self.func)(ia, axis=0, out=dpnp_out)
637641
assert dpnp_out is result
638642
assert_allclose(result, expected)
639643

640644
# out is usm_ndarray
641645
dpt_out = dpt.empty(expected.shape, dtype=expected.dtype)
642-
result = getattr(dpnp, func)(ia, axis=0, out=dpt_out)
646+
result = getattr(dpnp, self.func)(ia, axis=0, out=dpt_out)
643647
assert dpt_out is result.get_array()
644648
assert_allclose(result, expected)
645649

646650
# output is numpy array -> Error
647651
result = numpy.empty_like(expected)
648652
with pytest.raises(TypeError):
649-
getattr(dpnp, func)(ia, axis=0, out=result)
653+
getattr(dpnp, self.func)(ia, axis=0, out=result)
650654

651655
# output has incorrect shape -> Error
652656
result = dpnp.array(numpy.zeros((4, 2)))
653657
with pytest.raises(ValueError):
654-
getattr(dpnp, func)(ia, axis=0, out=result)
658+
getattr(dpnp, self.func)(ia, axis=0, out=result)
655659

656660
@pytest.mark.usefixtures("suppress_complex_warning")
657-
@pytest.mark.parametrize("func", ["max", "min"])
658661
@pytest.mark.parametrize("arr_dt", get_all_dtypes(no_none=True))
659662
@pytest.mark.parametrize("out_dt", get_all_dtypes(no_none=True))
660-
def test_out_dtype(self, func, arr_dt, out_dt):
663+
def test_out_dtype(self, arr_dt, out_dt):
664+
# if out_dt is unsigned, input cannot be signed otherwise overflow occurs
661665
low = 0 if dpnp.issubdtype(out_dt, dpnp.unsignedinteger) else -10
662666
a = generate_random_numpy_array((2, 2, 3), dtype=arr_dt, low=low)
663667
out = numpy.zeros_like(a, shape=(2, 3), dtype=out_dt)
664668
ia, iout = dpnp.array(a), dpnp.array(out)
665669

666-
result = getattr(dpnp, func)(ia, out=iout, axis=1)
667-
expected = getattr(numpy, func)(a, out=out, axis=1)
670+
result = getattr(dpnp, self.func)(ia, out=iout, axis=1)
671+
expected = getattr(numpy, self.func)(a, out=out, axis=1)
668672
assert_dtype_allclose(result, expected)
669673
assert result is iout
670674

671-
@pytest.mark.parametrize("func", ["max", "min"])
672-
def test_error(self, func):
675+
def test_error(self):
673676
ia = dpnp.arange(5)
674677
# where is not supported
675678
with pytest.raises(NotImplementedError):
676-
getattr(dpnp, func)(ia, where=False)
679+
getattr(dpnp, self.func)(ia, where=False)
677680

678681
# initial is not supported
679682
with pytest.raises(NotImplementedError):
680-
getattr(dpnp, func)(ia, initial=6)
683+
getattr(dpnp, self.func)(ia, initial=6)
681684

682685

683686
class TestMean:

dpnp/tests/test_sum.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,7 @@ def test_sum(shape, dtype_in, dtype_out, transpose, keepdims, order):
5151
axes.append(tuple(axes_range))
5252

5353
for axis in axes:
54-
if (
55-
numpy.issubdtype(dtype_out, numpy.bool_)
56-
and numpy.issubdtype(dtype_in, numpy.signedinteger)
57-
and not a_np.sum(axis=axis).all()
58-
):
59-
# TODO: remove workaround when dpctl-issue#1944 is resolved
60-
a = a.astype(dpnp.bool)
61-
dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims)
62-
else:
63-
dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims)
54+
dpnp_res = a.sum(axis=axis, dtype=dtype_out, keepdims=keepdims)
6455
numpy_res = a_np.sum(axis=axis, dtype=dtype_out, keepdims=keepdims)
6556
assert_dtype_allclose(dpnp_res, numpy_res, factor=16)
6657

@@ -74,7 +65,9 @@ def test_sum_empty_out(dtype):
7465
assert_array_equal(out, numpy.array(0, dtype=dtype))
7566

7667

77-
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True, no_bool=True))
68+
@pytest.mark.parametrize(
69+
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
70+
)
7871
@pytest.mark.parametrize("axis", [None, 0, 1, 2, 3])
7972
def test_sum_empty(dtype, axis):
8073
a = numpy.empty((1, 2, 0, 4), dtype=dtype)

dpnp/tests/test_umath.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,11 @@ def test_values(self, dt):
468468
assert_dtype_allclose(result, expected)
469469

470470
@pytest.mark.parametrize(
471-
"dt",
472-
[dpnp.bool, dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64],
471+
"dt", get_all_dtypes(no_none=True, no_complex=True)
473472
)
474473
def test_range(self, dt):
475-
a = numpy.array([1000000, -1000000, 1000200, -1000200], dtype=dt)
476-
b = numpy.array([1000200, -1000200, 1000000, -1000000], dtype=dt)
474+
a = get_abs_array([1000000, -1000000, 1000200, -1000200], dtype=dt)
475+
b = get_abs_array([1000200, -1000200, 1000000, -1000000], dtype=dt)
477476
ia, ib = dpnp.array(a), dpnp.array(b)
478477

479478
result = dpnp.logaddexp2(ia, ib)

0 commit comments

Comments
 (0)