Skip to content

Commit 97ef47c

Browse files
authored
Merge f73bcf3 into 6cc2348
2 parents 6cc2348 + f73bcf3 commit 97ef47c

File tree

12 files changed

+62
-28
lines changed

12 files changed

+62
-28
lines changed

dpnp/dpnp_array.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ def __and__(self, other):
185185
"""Return ``self&value``."""
186186
return dpnp.bitwise_and(self, other)
187187

188-
# '__array__',
188+
def __array__(self, dtype=None, /, *, copy=None):
189+
raise TypeError(
190+
"Implicit conversion to a NumPy array is not allowed. "
191+
"Please use `.asnumpy()` to construct a NumPy array explicitly."
192+
)
193+
189194
# '__array_finalize__',
190195
# '__array_function__',
191196
# '__array_interface__',

dpnp/dpnp_utils/dpnp_utils_pad.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ def _as_pairs(x, ndim, as_index=False):
7474
x = round(x)
7575
return ((x, x),) * ndim
7676

77-
x = numpy.array(x)
77+
# explicitly cast input "x" to NumPy array
78+
if dpnp.is_supported_array_type(x):
79+
x = dpnp.asnumpy(x)
80+
else:
81+
x = numpy.array(x)
82+
7883
if as_index:
7984
x = numpy.asarray(numpy.round(x), dtype=numpy.intp)
8085

dpnp/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from . import testing
44

55
numpy.testing.assert_allclose = testing.assert_allclose
6+
numpy.testing.assert_almost_equal = testing.assert_almost_equal
67
numpy.testing.assert_array_equal = testing.assert_array_equal
78
numpy.testing.assert_equal = testing.assert_equal

dpnp/tests/helper.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ def get_all_dtypes(
161161
return dtypes
162162

163163

164+
def get_array(xp, a):
165+
"""
166+
Cast input array `a` to a type supported by `xp` initerface.
167+
168+
Implicit conversion of either DPNP or DPCTL array to a NumPy array is not
169+
allowed. Input array has to be explicitly casted with `asnumpy` function.
170+
171+
"""
172+
173+
if xp is numpy and dpnp.is_supported_array_type(a):
174+
return dpnp.asnumpy(a)
175+
return a
176+
177+
164178
def generate_random_numpy_array(
165179
shape,
166180
dtype=None,

dpnp/tests/test_arraycreation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .helper import (
1818
assert_dtype_allclose,
1919
get_all_dtypes,
20+
get_array,
2021
)
2122
from .third_party.cupy import testing
2223

@@ -768,7 +769,7 @@ def test_space_numpy_dtype(func, start_dtype, stop_dtype):
768769
],
769770
)
770771
def test_linspace_arrays(start, stop):
771-
func = lambda xp: xp.linspace(start, stop, 10)
772+
func = lambda xp: xp.linspace(get_array(xp, start), get_array(xp, stop), 10)
772773
assert func(numpy).shape == func(dpnp).shape
773774

774775

dpnp/tests/test_indexing.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import dpnp
1818
from dpnp.dpnp_array import dpnp_array
1919

20-
from .helper import get_all_dtypes, get_integer_dtypes, has_support_aspect64
20+
from .helper import (
21+
get_all_dtypes,
22+
get_array,
23+
get_integer_dtypes,
24+
has_support_aspect64,
25+
)
2126
from .third_party.cupy import testing
2227

2328

@@ -441,16 +446,15 @@ class TestPut:
441446
)
442447
@pytest.mark.parametrize("ind_dt", get_all_dtypes(no_none=True))
443448
@pytest.mark.parametrize(
444-
"vals",
449+
"ivals",
445450
[0, [1, 2], (2, 2), dpnp.array([1, 2])],
446451
ids=["0", "[1, 2]", "(2, 2)", "dpnp.array([1,2])"],
447452
)
448453
@pytest.mark.parametrize("mode", ["clip", "wrap"])
449-
def test_input_1d(self, a_dt, indices, ind_dt, vals, mode):
454+
def test_input_1d(self, a_dt, indices, ind_dt, ivals, mode):
450455
a = numpy.array([-2, -1, 0, 1, 2], dtype=a_dt)
451-
b = numpy.copy(a)
452-
ia = dpnp.array(a)
453-
ib = dpnp.array(b)
456+
b, vals = numpy.copy(a), get_array(numpy, ivals)
457+
ia, ib = dpnp.array(a), dpnp.array(b)
454458

455459
ind = numpy.array(indices, dtype=ind_dt)
456460
if ind_dt == dpnp.bool and ind.all():
@@ -459,18 +463,18 @@ def test_input_1d(self, a_dt, indices, ind_dt, vals, mode):
459463

460464
if numpy.can_cast(ind_dt, numpy.intp, casting="safe"):
461465
numpy.put(a, ind, vals, mode=mode)
462-
dpnp.put(ia, iind, vals, mode=mode)
466+
dpnp.put(ia, iind, ivals, mode=mode)
463467
assert_array_equal(ia, a)
464468

465469
b.put(ind, vals, mode=mode)
466-
ib.put(iind, vals, mode=mode)
470+
ib.put(iind, ivals, mode=mode)
467471
assert_array_equal(ib, b)
468472
else:
469473
assert_raises(TypeError, numpy.put, a, ind, vals, mode=mode)
470-
assert_raises(TypeError, dpnp.put, ia, iind, vals, mode=mode)
474+
assert_raises(TypeError, dpnp.put, ia, iind, ivals, mode=mode)
471475

472476
assert_raises(TypeError, b.put, ind, vals, mode=mode)
473-
assert_raises(TypeError, ib.put, iind, vals, mode=mode)
477+
assert_raises(TypeError, ib.put, iind, ivals, mode=mode)
474478

475479
@pytest.mark.parametrize("a_dt", get_all_dtypes(no_none=True))
476480
@pytest.mark.parametrize(
@@ -637,7 +641,7 @@ def test_values(self, arr_dt, idx_dt, ndim, values):
637641
ia, iind = dpnp.array(a), dpnp.array(ind)
638642

639643
for axis in range(ndim):
640-
numpy.put_along_axis(a, ind, values, axis)
644+
numpy.put_along_axis(a, ind, get_array(numpy, values), axis)
641645
dpnp.put_along_axis(ia, iind, values, axis)
642646
assert_array_equal(ia, a)
643647

dpnp/tests/test_linalg.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ def test_matrix_rank(self, data, dtype):
19351935

19361936
np_rank = numpy.linalg.matrix_rank(a)
19371937
dp_rank = dpnp.linalg.matrix_rank(a_dp)
1938-
assert np_rank == dp_rank
1938+
assert dp_rank.asnumpy() == np_rank
19391939

19401940
@pytest.mark.parametrize("dtype", get_all_dtypes())
19411941
@pytest.mark.parametrize(
@@ -1953,7 +1953,7 @@ def test_matrix_rank_hermitian(self, data, dtype):
19531953

19541954
np_rank = numpy.linalg.matrix_rank(a, hermitian=True)
19551955
dp_rank = dpnp.linalg.matrix_rank(a_dp, hermitian=True)
1956-
assert np_rank == dp_rank
1956+
assert dp_rank.asnumpy() == np_rank
19571957

19581958
@pytest.mark.parametrize(
19591959
"high_tol, low_tol",
@@ -1986,15 +1986,15 @@ def test_matrix_rank_tolerance(self, high_tol, low_tol):
19861986
dp_rank_high_tol = dpnp.linalg.matrix_rank(
19871987
a_dp, hermitian=True, tol=dp_high_tol
19881988
)
1989-
assert np_rank_high_tol == dp_rank_high_tol
1989+
assert dp_rank_high_tol.asnumpy() == np_rank_high_tol
19901990

19911991
np_rank_low_tol = numpy.linalg.matrix_rank(
19921992
a, hermitian=True, tol=low_tol
19931993
)
19941994
dp_rank_low_tol = dpnp.linalg.matrix_rank(
19951995
a_dp, hermitian=True, tol=dp_low_tol
19961996
)
1997-
assert np_rank_low_tol == dp_rank_low_tol
1997+
assert dp_rank_low_tol.asnumpy() == np_rank_low_tol
19981998

19991999
# rtol kwarg was added in numpy 2.0
20002000
@testing.with_requires("numpy>=2.0")
@@ -2789,15 +2789,14 @@ def check_decomposition(
27892789
for i in range(min(dp_a.shape[-2], dp_a.shape[-1])):
27902790
dpnp_diag_s[..., i, i] = dp_s[..., i]
27912791
reconstructed = dpnp.dot(dp_u, dpnp.dot(dpnp_diag_s, dp_vt))
2792-
# TODO: use assert dpnp.allclose() inside check_decomposition()
2793-
# when it will support complex dtypes
2794-
assert_allclose(dp_a, reconstructed, rtol=tol, atol=1e-4)
2792+
2793+
assert dpnp.allclose(dp_a, reconstructed, rtol=tol, atol=1e-4)
27952794

27962795
assert_allclose(dp_s, np_s, rtol=tol, atol=1e-03)
27972796

27982797
if compute_vt:
27992798
for i in range(min(dp_a.shape[-2], dp_a.shape[-1])):
2800-
if np_u[..., 0, i] * dp_u[..., 0, i] < 0:
2799+
if np_u[..., 0, i] * dpnp.asnumpy(dp_u[..., 0, i]) < 0:
28012800
np_u[..., :, i] = -np_u[..., :, i]
28022801
np_vt[..., i, :] = -np_vt[..., i, :]
28032802
for i in range(numpy.count_nonzero(np_s > tol)):

dpnp/tests/testing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .array import (
22
assert_allclose,
3+
assert_almost_equal,
34
assert_array_equal,
45
assert_equal,
56
)

dpnp/tests/testing/array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from dpnp.dpnp_utils import convert_item
3030

3131
assert_allclose_orig = numpy.testing.assert_allclose
32+
assert_almost_equal_orig = numpy.testing.assert_almost_equal
3233
assert_array_equal_orig = numpy.testing.assert_array_equal
3334
assert_equal_orig = numpy.testing.assert_equal
3435

@@ -44,6 +45,10 @@ def assert_allclose(result, expected, *args, **kwargs):
4445
_assert(assert_allclose_orig, result, expected, *args, **kwargs)
4546

4647

48+
def assert_almost_equal(result, expected, *args, **kwargs):
49+
_assert(assert_almost_equal_orig, result, expected, *args, **kwargs)
50+
51+
4752
def assert_array_equal(result, expected, *args, **kwargs):
4853
_assert(assert_array_equal_orig, result, expected, *args, **kwargs)
4954

dpnp/tests/third_party/cupy/core_tests/test_ndarray.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ def test_format(self, xp):
691691
return format(x, ".2f")
692692

693693

694-
@pytest.mark.skip("implicit conversation to numpy does not raise an exception")
695694
class TestNdarrayImplicitConversion(unittest.TestCase):
696695

697696
def test_array(self):

dpnp/tests/third_party/cupy/random_tests/test_permutations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class TestPermutationSoundness(unittest.TestCase):
129129

130130
def setUp(self):
131131
a = cupy.random.permutation(self.num)
132-
self.a = a
132+
self.a = a.asnumpy()
133133

134134
# Test soundness
135135

dpnp/tests/third_party/cupy/random_tests/test_sample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def test_bound_float2(self):
8989
def test_goodness_of_fit(self):
9090
mx = 5
9191
trial = 100
92-
vals = [random.randint(mx) for _ in range(trial)]
92+
vals = [random.randint(mx).asnumpy() for _ in range(trial)]
9393
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
9494
expected = numpy.array([float(trial) / mx] * mx)
9595
assert _hypothesis.chi_square_test(counts, expected)
9696

9797
@_condition.repeat(3, 10)
9898
def test_goodness_of_fit_2(self):
9999
mx = 5
100-
vals = random.randint(mx, size=(5, 20))
100+
vals = random.randint(mx, size=(5, 20)).asnumpy()
101101
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
102102
expected = numpy.array([float(vals.size) / mx] * mx)
103103
assert _hypothesis.chi_square_test(counts, expected)
@@ -191,15 +191,15 @@ def test_bound_2(self):
191191
def test_goodness_of_fit(self):
192192
mx = 5
193193
trial = 100
194-
vals = [random.randint(0, mx) for _ in range(trial)]
194+
vals = [random.randint(0, mx).asnumpy() for _ in range(trial)]
195195
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
196196
expected = numpy.array([float(trial) / mx] * mx)
197197
assert _hypothesis.chi_square_test(counts, expected)
198198

199199
@_condition.repeat(3, 10)
200200
def test_goodness_of_fit_2(self):
201201
mx = 5
202-
vals = random.randint(0, mx, (5, 20))
202+
vals = random.randint(0, mx, (5, 20)).asnumpy()
203203
counts = numpy.histogram(vals, bins=numpy.arange(mx + 1))[0]
204204
expected = numpy.array([float(vals.size) / mx] * mx)
205205
assert _hypothesis.chi_square_test(counts, expected)

0 commit comments

Comments
 (0)