Skip to content

Commit 4285f71

Browse files
committed
address comments - second round
1 parent 38a8e2a commit 4285f71

File tree

10 files changed

+257
-182
lines changed

10 files changed

+257
-182
lines changed

dpnp/dpnp_container.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def full(
218218
):
219219
"""Validate input parameters before passing them into `dpctl.tensor` module"""
220220
dpu.validate_usm_type(usm_type, allow_none=True)
221+
221222
sycl_queue_normalized = dpnp.get_normalized_queue_device(
222223
fill_value, sycl_queue=sycl_queue, device=device
223224
)

dpnp/dpnp_iface_arraycreation.py

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# cython: language_level=3
2-
# distutils: language = c++
31
# -*- coding: utf-8 -*-
42
# *****************************************************************************
53
# Copyright (c) 2016-2023, Intel Corporation
@@ -575,7 +573,7 @@ def copy(a, order="K", subok=False):
575573
return array(a, order=order, subok=subok, copy=True)
576574

577575

578-
def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
576+
def diag(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
579577
"""
580578
Extract a diagonal or construct a diagonal array.
581579
@@ -588,9 +586,8 @@ def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
588586
589587
Limitations
590588
-----------
591-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
592589
Parameter `k` is only supported as integer data type.
593-
Otherwise the function will be executed sequentially on CPU.
590+
Otherwise ``TypeError`` exception will be raised.
594591
595592
See Also
596593
--------
@@ -623,56 +620,44 @@ def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
623620
624621
"""
625622

626-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
627-
pass
628-
elif not isinstance(k, int):
629-
pass
623+
if not isinstance(k, int):
624+
raise TypeError("An integer is required, but got {}".format(type(k)))
630625
else:
631-
_usm_type = x1.usm_type if usm_type is None else usm_type
632-
_sycl_queue = dpnp.get_normalized_queue_device(
633-
x1, sycl_queue=sycl_queue, device=device
634-
)
635-
x1 = (
636-
x1.to_device(_sycl_queue.sycl_device)
637-
if x1.sycl_queue != _sycl_queue
638-
else x1
626+
v = dpnp.asarray(
627+
v, device=device, usm_type=usm_type, sycl_queue=sycl_queue
639628
)
640629

641630
init0 = max(0, -k)
642631
init1 = max(0, k)
643-
if x1.ndim == 1:
644-
size = x1.shape[0] + abs(k)
632+
if v.ndim == 1:
633+
size = v.shape[0] + abs(k)
645634
m = dpnp.zeros(
646635
(size, size),
647-
dtype=x1.dtype,
648-
usm_type=_usm_type,
649-
sycl_queue=_sycl_queue,
636+
dtype=v.dtype,
637+
usm_type=v.usm_type,
638+
sycl_queue=v.sycl_queue,
650639
)
651-
for i in range(x1.shape[0]):
652-
m[(init0 + i), init1 + i] = x1[i]
640+
for i in range(v.shape[0]):
641+
m[(init0 + i), init1 + i] = v[i]
653642
return m
654-
elif x1.ndim == 2:
655-
size = min(
656-
x1.shape[0], x1.shape[0] + k, x1.shape[1], x1.shape[1] - k
657-
)
643+
elif v.ndim == 2:
644+
size = min(v.shape[0], v.shape[0] + k, v.shape[1], v.shape[1] - k)
658645
if size < 0:
659646
size = 0
660647
m = dpnp.zeros(
661648
(size,),
662-
dtype=x1.dtype,
663-
usm_type=_usm_type,
664-
sycl_queue=_sycl_queue,
649+
dtype=v.dtype,
650+
usm_type=v.usm_type,
651+
sycl_queue=v.sycl_queue,
665652
)
666653
for i in range(size):
667-
m[i] = x1[(init0 + i), init1 + i]
654+
m[i] = v[(init0 + i), init1 + i]
668655
return m
669656
else:
670657
raise ValueError("Input must be a 1-D or 2-D array.")
671658

672-
return call_origin(numpy.diag, x1, k)
673659

674-
675-
def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
660+
def diagflat(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
676661
"""
677662
Create a two-dimensional array with the flattened input as a diagonal.
678663
@@ -691,9 +676,8 @@ def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
691676
692677
Limitations
693678
-----------
694-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
695679
Parameter `k` is only supported as integer data type.
696-
Otherwise the function will be executed sequentially on CPU.
680+
Otherwise ``TypeError`` exception will be raised.
697681
698682
Examples
699683
--------
@@ -713,19 +697,15 @@ def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
713697
[0, 0, 0, 0, 0]])
714698
715699
"""
716-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
717-
pass
718-
elif not isinstance(k, int):
719-
pass
700+
701+
if not isinstance(k, int):
702+
raise TypeError("An integer is required, but got {}".format(type(k)))
720703
else:
721-
_usm_type = x1.usm_type if usm_type is None else usm_type
722-
_sycl_queue = dpnp.get_normalized_queue_device(
723-
x1, sycl_queue=sycl_queue, device=device
704+
v = dpnp.asarray(
705+
v, device=device, usm_type=usm_type, sycl_queue=sycl_queue
724706
)
725-
v = dpnp.ravel(x1)
726-
return dpnp.diag(v, k, usm_type=_usm_type, sycl_queue=_sycl_queue)
727-
728-
return call_origin(numpy.diagflat, x1, k)
707+
v = dpnp.ravel(v)
708+
return dpnp.diag(v, k, usm_type=v.usm_type, sycl_queue=v.sycl_queue)
729709

730710

731711
def empty(
@@ -1024,6 +1004,7 @@ def full(
10241004
[10, 10, 10, 10]
10251005
10261006
"""
1007+
10271008
if like is not None:
10281009
pass
10291010
elif order not in ("C", "c", "F", "f", None):
@@ -1824,8 +1805,8 @@ def vander(
18241805
18251806
Limitations
18261807
-----------
1827-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
1828-
Otherwise the function will be executed sequentially on CPU.
1808+
Parameter `N`, if it is not ``None``, is only supported as integer data type.
1809+
Otherwise ``TypeError`` exception will be raised.
18291810
18301811
Examples
18311812
--------
@@ -1852,44 +1833,34 @@ def vander(
18521833
[ 1, 5, 25, 125]])
18531834
"""
18541835

1855-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
1856-
pass
1857-
elif N is not None and not isinstance(N, int):
1858-
pass
1836+
x1 = dpnp.asarray(
1837+
x1, device=device, usm_type=usm_type, sycl_queue=sycl_queue
1838+
)
1839+
1840+
if N is not None and not isinstance(N, int):
1841+
raise TypeError("An integer is required, but got {}".format(type(N)))
18591842
elif x1.ndim != 1:
18601843
raise ValueError("x1 must be a one-dimensional array or sequence.")
18611844
else:
18621845
if N is None:
18631846
N = x1.size
18641847

18651848
_dtype = int if x1.dtype == bool else x1.dtype
1866-
_usm_type = x1.usm_type if usm_type is None else usm_type
1867-
_sycl_queue = dpnp.get_normalized_queue_device(
1868-
x1, sycl_queue=sycl_queue, device=device
1869-
)
1870-
x1 = (
1871-
x1.to_device(_sycl_queue.sycl_device)
1872-
if x1.sycl_queue != _sycl_queue
1873-
else x1
1874-
)
1875-
18761849
m = empty(
18771850
(x1.size, N),
18781851
dtype=_dtype,
1879-
usm_type=_usm_type,
1880-
sycl_queue=_sycl_queue,
1852+
usm_type=x1.usm_type,
1853+
sycl_queue=x1.sycl_queue,
18811854
)
18821855
tmp = m[:, ::-1] if not increasing else m
18831856
dpnp.power(
18841857
x1.reshape(-1, 1),
1885-
dpnp.arange(N, dtype=_dtype, sycl_queue=_sycl_queue),
1858+
dpnp.arange(N, dtype=_dtype, sycl_queue=x1.sycl_queue),
18861859
out=tmp,
18871860
)
18881861

18891862
return m
18901863

1891-
return call_origin(numpy.vander, x1, N=N, increasing=increasing)
1892-
18931864

18941865
def zeros(
18951866
shape,

dpnp/dpnp_iface_statistics.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,6 @@ def ptp(
712712
Limitations
713713
-----------
714714
Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
715-
Otherwise the function will be executed sequentially on CPU.
716715
717716
Examples
718717
--------
@@ -729,16 +728,11 @@ def ptp(
729728
730729
"""
731730

732-
if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)):
733-
pass
734-
else:
735-
return dpnp.subtract(
736-
dpnp.max(a, axis=axis, keepdims=keepdims),
737-
dpnp.min(a, axis=axis, keepdims=keepdims),
738-
out=out,
739-
)
740-
741-
return call_origin(numpy.ptp, a, axis, out, keepdims)
731+
return dpnp.subtract(
732+
dpnp.max(a, axis=axis, keepdims=keepdims, out=out),
733+
dpnp.min(a, axis=axis, keepdims=keepdims),
734+
out=out,
735+
)
742736

743737

744738
def nanvar(x1, axis=None, dtype=None, out=None, ddof=0, keepdims=False):

tests/skipped_tests.tbl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_s
158158
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok
159159
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides
160160

161-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list
162-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple
163-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list
164-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple
165-
166161
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid0
167162
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid1
168163
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid2

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes
147147
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_3_{order='C', shape=(2, 3)}::test_item
148148
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_4_{order='F', shape=(2, 3)}::test_item
149149

150-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list
151-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple
152-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list
153-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple
154-
155150
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray
156151
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal
157152
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_5_{shape=(3, 3), val=(2,), wrap=False}::test_1darray

tests/test_arraycreation.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,41 +117,40 @@ def test_arange(start, stop, step, dtype):
117117
"[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]",
118118
],
119119
)
120-
def test_diag(v, k):
120+
def test_diag_diagflat(v, k):
121121
a = numpy.array(v)
122122
ia = dpnp.array(a)
123123
expected = numpy.diag(a, k)
124124
result = dpnp.diag(ia, k)
125125
assert_array_equal(expected, result)
126126

127+
expected = numpy.diagflat(a, k)
128+
result = dpnp.diagflat(ia, k)
129+
assert_array_equal(expected, result)
130+
127131

128132
@pytest.mark.parametrize(
129-
"axis",
130-
[None, 0, 1],
131-
ids=["None", "0", "1"],
132-
)
133-
@pytest.mark.parametrize(
134-
"v",
133+
"seq",
135134
[
136-
[[0, 0], [0, 0]],
137-
[[1, 2], [1, 2]],
138-
[[1, 2], [3, 4]],
135+
[0, 1, 2, 3, 4],
136+
(0, 1, 2, 3, 4),
139137
[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
140138
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]],
141139
],
142140
ids=[
143-
"[[0, 0], [0, 0]]",
144-
"[[1, 2], [1, 2]]",
145-
"[[1, 2], [3, 4]]",
141+
"[0, 1, 2, 3, 4]",
142+
"(0, 1, 2, 3, 4)",
146143
"[[0, 1, 2], [3, 4, 5], [6, 7, 8]]",
147144
"[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]",
148145
],
149146
)
150-
def test_ptp(v, axis):
151-
a = numpy.array(v)
152-
ia = dpnp.array(a)
153-
expected = numpy.ptp(a, axis)
154-
result = dpnp.ptp(ia, axis)
147+
def test_diag_diagflat_seq(seq):
148+
expected = numpy.diag(seq)
149+
result = dpnp.diag(seq)
150+
assert_array_equal(expected, result)
151+
152+
expected = numpy.diagflat(seq)
153+
result = dpnp.diagflat(seq)
155154
assert_array_equal(expected, result)
156155

157156

@@ -479,7 +478,7 @@ def test_triu_size_null(k):
479478
def test_vander(array, dtype, n, increase):
480479
if dtype in [dpnp.complex64, dpnp.complex128] and array == [0, 3, 5]:
481480
pytest.skip(
482-
"dpnp.power(dpnp.array(complex(0,0)), dpnp.array(0)) returns nan+nanj while it should be 1+0j"
481+
"per array API dpnp.power(complex(0,0)), 0) returns nan+nanj while NumPy returns 1+0j"
483482
)
484483
vander_func = lambda xp, x: xp.vander(x, N=n, increasing=increase)
485484

@@ -489,6 +488,16 @@ def test_vander(array, dtype, n, increase):
489488
assert_allclose(vander_func(numpy, a_np), vander_func(dpnp, a_dpnp))
490489

491490

491+
@pytest.mark.parametrize(
492+
"sequence",
493+
[[1, 2, 3, 4], (1, 2, 3, 4)],
494+
ids=["[1, 2, 3, 4]", "(1, 2, 3, 4)"],
495+
)
496+
def test_vander_seq(sequence):
497+
vander_func = lambda xp, x: xp.vander(x)
498+
assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence))
499+
500+
492501
@pytest.mark.parametrize(
493502
"shape",
494503
[(), 0, (0,), (2, 0, 3), (3, 2)],

0 commit comments

Comments
 (0)