Skip to content

Commit c4be394

Browse files
authored
Add proper handling of unput array of usm_ndarray type in dpnp.ix_ (#2047) (#2050)
* Proper handling of input array as dpctl.tensor.usm_ndarray * Update a page for Indexing routines * Move dpnp.ix_ tests to keep lexycographical order * Add tests to cover faulty use case * Added entry to changelog
1 parent e309ebf commit c4be394

File tree

5 files changed

+62
-39
lines changed

5 files changed

+62
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ In addition, this release completes implementation of `dpnp.fft` module and adds
108108
* Resolved an issue with `dpnp.matmul` when an f_contiguous `out` keyword is passed to the the function [#1872](https://github.com/IntelPython/dpnp/pull/1872)
109109
* Resolved a possible race condition in `dpnp.inv` [#1940](https://github.com/IntelPython/dpnp/pull/1940)
110110
* Resolved an issue with failing tests for `dpnp.append` when running on a device without fp64 support [#2034](https://github.com/IntelPython/dpnp/pull/2034)
111+
* Resolved an issue with input array of `usm_ndarray` passed into `dpnp.ix_` [#2047](https://github.com/IntelPython/dpnp/pull/2047)
111112

112113

113114
## [0.15.0] - 05/25/2024

doc/reference/indexing.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
.. _routines.indexing:
2+
.. _arrays.indexing:
23

3-
Array Indexing Routines
4-
=======================
4+
Indexing routines
5+
=================
56

6-
.. https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
7+
.. https://numpy.org/doc/stable/reference/routines.indexing.html
78
89
Generating index arrays
910
-----------------------
10-
11-
1211
.. autosummary::
1312
:toctree: generated/
1413
:nosignatures:

doc/reference/ndarray.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Indexing arrays
3434
Arrays can be indexed using an extended Python slicing syntax,
3535
``array[selection]``.
3636

37-
.. seealso:: :ref:`Array Indexing Routines <routines.indexing>`.
37+
.. seealso:: :ref:`Indexing routines <routines.indexing>`.
3838

3939

4040
Array attributes

dpnp/dpnp_iface_indexing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ def ix_(*args):
934934
and the dimension with the non-unit shape value cycles through all
935935
N dimensions.
936936
937-
Using :obj:`dpnp.ix_` one can quickly construct index arrays that will
937+
Using :obj:`dpnp.ix_` one can quickly construct index arrays that will
938938
index the cross product. ``a[dpnp.ix_([1,3],[2,5])]`` returns the array
939939
``[[a[1,2] a[1,5]], [a[3,2] a[3,5]]]``.
940940
@@ -994,14 +994,15 @@ def ix_(*args):
994994
"""
995995

996996
dpnp.check_supported_arrays_type(*args)
997+
997998
out = []
998999
nd = len(args)
9991000
for k, new in enumerate(args):
10001001
if new.ndim != 1:
10011002
raise ValueError("Cross index must be 1 dimensional")
10021003
if dpnp.issubdtype(new.dtype, dpnp.bool):
10031004
(new,) = dpnp.nonzero(new)
1004-
new = new.reshape((1,) * k + (new.size,) + (1,) * (nd - k - 1))
1005+
new = dpnp.reshape(new, (1,) * k + (new.size,) + (1,) * (nd - k - 1))
10051006
out.append(new)
10061007
return tuple(out)
10071008

tests/test_indexing.py

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22

3+
import dpctl.tensor as dpt
34
import numpy
45
import pytest
56
from dpctl.tensor._numpy_helper import AxisError
@@ -12,6 +13,7 @@
1213
)
1314

1415
import dpnp
16+
from dpnp.dpnp_array import dpnp_array
1517

1618
from .helper import get_all_dtypes, get_integer_dtypes, has_support_aspect64
1719

@@ -272,6 +274,57 @@ def test_indexing_array_negative_strides(self):
272274
assert_array_equal(arr, 10.0)
273275

274276

277+
class TestIx:
278+
@pytest.mark.parametrize(
279+
"x0", [[0, 1], [True, True]], ids=["[0, 1]", "[True, True]"]
280+
)
281+
@pytest.mark.parametrize(
282+
"x1",
283+
[[2, 4], [False, False, True, False, True]],
284+
ids=["[2, 4]", "[False, False, True, False, True]"],
285+
)
286+
def test_ix(self, x0, x1):
287+
expected = dpnp.ix_(dpnp.array(x0), dpnp.array(x1))
288+
result = numpy.ix_(numpy.array(x0), numpy.array(x1))
289+
290+
assert_array_equal(result[0], expected[0])
291+
assert_array_equal(result[1], expected[1])
292+
293+
@pytest.mark.parametrize("dt", [dpnp.intp, dpnp.float32])
294+
def test_ix_empty_out(self, dt):
295+
a = numpy.array([], dtype=dt)
296+
ia = dpnp.array(a)
297+
298+
(result,) = dpnp.ix_(ia)
299+
(expected,) = numpy.ix_(a)
300+
assert_array_equal(result, expected)
301+
assert a.dtype == dt
302+
303+
def test_repeated_input(self):
304+
a = numpy.arange(5)
305+
ia = dpnp.array(a)
306+
307+
result = dpnp.ix_(ia, ia)
308+
expected = numpy.ix_(a, a)
309+
assert_array_equal(result[0], expected[0])
310+
assert_array_equal(result[1], expected[1])
311+
312+
@pytest.mark.parametrize("arr", [[2, 4, 0, 1], [True, False, True, True]])
313+
def test_usm_ndarray_input(self, arr):
314+
a = numpy.array(arr)
315+
ia = dpt.asarray(a)
316+
317+
(result,) = dpnp.ix_(ia)
318+
(expected,) = numpy.ix_(a)
319+
assert_array_equal(result, expected)
320+
assert isinstance(result, dpnp_array)
321+
322+
@pytest.mark.parametrize("xp", [dpnp, numpy])
323+
@pytest.mark.parametrize("shape", [(), (2, 2)])
324+
def test_ix_error(self, xp, shape):
325+
assert_raises(ValueError, xp.ix_, xp.ones(shape))
326+
327+
275328
class TestNonzero:
276329
@pytest.mark.parametrize("list_val", [[], [0], [1]])
277330
def test_trivial(self, list_val):
@@ -1143,37 +1196,6 @@ def test_empty_indices(self):
11431196
)
11441197

11451198

1146-
class TestIx:
1147-
@pytest.mark.parametrize(
1148-
"x0", [[0, 1], [True, True]], ids=["[0, 1]", "[True, True]"]
1149-
)
1150-
@pytest.mark.parametrize(
1151-
"x1",
1152-
[[2, 4], [False, False, True, False, True]],
1153-
ids=["[2, 4]", "[False, False, True, False, True]"],
1154-
)
1155-
def test_ix(self, x0, x1):
1156-
expected = dpnp.ix_(dpnp.array(x0), dpnp.array(x1))
1157-
result = numpy.ix_(numpy.array(x0), numpy.array(x1))
1158-
1159-
assert_array_equal(expected[0], result[0])
1160-
assert_array_equal(expected[1], result[1])
1161-
1162-
def test_ix_empty_out(self):
1163-
(a,) = dpnp.ix_(dpnp.array([], dtype=dpnp.intp))
1164-
assert_equal(a.dtype, dpnp.intp)
1165-
1166-
(a,) = dpnp.ix_(dpnp.array([], dtype=dpnp.float32))
1167-
assert_equal(a.dtype, dpnp.float32)
1168-
1169-
def test_ix_error(self):
1170-
with pytest.raises(ValueError):
1171-
dpnp.ix_(dpnp.ones(()))
1172-
1173-
with pytest.raises(ValueError):
1174-
dpnp.ix_(dpnp.ones((2, 2)))
1175-
1176-
11771199
class TestSelect:
11781200
choices_np = [
11791201
numpy.array([1, 2, 3]),

0 commit comments

Comments
 (0)