Skip to content

Commit 0b86691

Browse files
committed
address comments - second round
1 parent 6d3f817 commit 0b86691

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
@@ -581,7 +579,7 @@ def copy(a, order="K", subok=False):
581579
return array(a, order=order, subok=subok, copy=True)
582580

583581

584-
def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
582+
def diag(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
585583
"""
586584
Extract a diagonal or construct a diagonal array.
587585
@@ -594,9 +592,8 @@ def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
594592
595593
Limitations
596594
-----------
597-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
598595
Parameter `k` is only supported as integer data type.
599-
Otherwise the function will be executed sequentially on CPU.
596+
Otherwise ``TypeError`` exception will be raised.
600597
601598
See Also
602599
--------
@@ -629,56 +626,44 @@ def diag(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
629626
630627
"""
631628

632-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
633-
pass
634-
elif not isinstance(k, int):
635-
pass
629+
if not isinstance(k, int):
630+
raise TypeError("An integer is required, but got {}".format(type(k)))
636631
else:
637-
_usm_type = x1.usm_type if usm_type is None else usm_type
638-
_sycl_queue = dpnp.get_normalized_queue_device(
639-
x1, sycl_queue=sycl_queue, device=device
640-
)
641-
x1 = (
642-
x1.to_device(_sycl_queue.sycl_device)
643-
if x1.sycl_queue != _sycl_queue
644-
else x1
632+
v = dpnp.asarray(
633+
v, device=device, usm_type=usm_type, sycl_queue=sycl_queue
645634
)
646635

647636
init0 = max(0, -k)
648637
init1 = max(0, k)
649-
if x1.ndim == 1:
650-
size = x1.shape[0] + abs(k)
638+
if v.ndim == 1:
639+
size = v.shape[0] + abs(k)
651640
m = dpnp.zeros(
652641
(size, size),
653-
dtype=x1.dtype,
654-
usm_type=_usm_type,
655-
sycl_queue=_sycl_queue,
642+
dtype=v.dtype,
643+
usm_type=v.usm_type,
644+
sycl_queue=v.sycl_queue,
656645
)
657-
for i in range(x1.shape[0]):
658-
m[(init0 + i), init1 + i] = x1[i]
646+
for i in range(v.shape[0]):
647+
m[(init0 + i), init1 + i] = v[i]
659648
return m
660-
elif x1.ndim == 2:
661-
size = min(
662-
x1.shape[0], x1.shape[0] + k, x1.shape[1], x1.shape[1] - k
663-
)
649+
elif v.ndim == 2:
650+
size = min(v.shape[0], v.shape[0] + k, v.shape[1], v.shape[1] - k)
664651
if size < 0:
665652
size = 0
666653
m = dpnp.zeros(
667654
(size,),
668-
dtype=x1.dtype,
669-
usm_type=_usm_type,
670-
sycl_queue=_sycl_queue,
655+
dtype=v.dtype,
656+
usm_type=v.usm_type,
657+
sycl_queue=v.sycl_queue,
671658
)
672659
for i in range(size):
673-
m[i] = x1[(init0 + i), init1 + i]
660+
m[i] = v[(init0 + i), init1 + i]
674661
return m
675662
else:
676663
raise ValueError("Input must be a 1-D or 2-D array.")
677664

678-
return call_origin(numpy.diag, x1, k)
679665

680-
681-
def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
666+
def diagflat(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
682667
"""
683668
Create a two-dimensional array with the flattened input as a diagonal.
684669
@@ -697,9 +682,8 @@ def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
697682
698683
Limitations
699684
-----------
700-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
701685
Parameter `k` is only supported as integer data type.
702-
Otherwise the function will be executed sequentially on CPU.
686+
Otherwise ``TypeError`` exception will be raised.
703687
704688
Examples
705689
--------
@@ -719,19 +703,15 @@ def diagflat(x1, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
719703
[0, 0, 0, 0, 0]])
720704
721705
"""
722-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
723-
pass
724-
elif not isinstance(k, int):
725-
pass
706+
707+
if not isinstance(k, int):
708+
raise TypeError("An integer is required, but got {}".format(type(k)))
726709
else:
727-
_usm_type = x1.usm_type if usm_type is None else usm_type
728-
_sycl_queue = dpnp.get_normalized_queue_device(
729-
x1, sycl_queue=sycl_queue, device=device
710+
v = dpnp.asarray(
711+
v, device=device, usm_type=usm_type, sycl_queue=sycl_queue
730712
)
731-
v = dpnp.ravel(x1)
732-
return dpnp.diag(v, k, usm_type=_usm_type, sycl_queue=_sycl_queue)
733-
734-
return call_origin(numpy.diagflat, x1, k)
713+
v = dpnp.ravel(v)
714+
return dpnp.diag(v, k, usm_type=v.usm_type, sycl_queue=v.sycl_queue)
735715

736716

737717
def empty(
@@ -1030,6 +1010,7 @@ def full(
10301010
[10, 10, 10, 10]
10311011
10321012
"""
1013+
10331014
if like is not None:
10341015
pass
10351016
elif order not in ("C", "c", "F", "f", None):
@@ -1871,8 +1852,8 @@ def vander(
18711852
18721853
Limitations
18731854
-----------
1874-
Parameter `x1` is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
1875-
Otherwise the function will be executed sequentially on CPU.
1855+
Parameter `N`, if it is not ``None``, is only supported as integer data type.
1856+
Otherwise ``TypeError`` exception will be raised.
18761857
18771858
Examples
18781859
--------
@@ -1899,44 +1880,34 @@ def vander(
18991880
[ 1, 5, 25, 125]])
19001881
"""
19011882

1902-
if not isinstance(x1, (dpnp.ndarray, dpt.usm_ndarray)):
1903-
pass
1904-
elif N is not None and not isinstance(N, int):
1905-
pass
1883+
x1 = dpnp.asarray(
1884+
x1, device=device, usm_type=usm_type, sycl_queue=sycl_queue
1885+
)
1886+
1887+
if N is not None and not isinstance(N, int):
1888+
raise TypeError("An integer is required, but got {}".format(type(N)))
19061889
elif x1.ndim != 1:
19071890
raise ValueError("x1 must be a one-dimensional array or sequence.")
19081891
else:
19091892
if N is None:
19101893
N = x1.size
19111894

19121895
_dtype = int if x1.dtype == bool else x1.dtype
1913-
_usm_type = x1.usm_type if usm_type is None else usm_type
1914-
_sycl_queue = dpnp.get_normalized_queue_device(
1915-
x1, sycl_queue=sycl_queue, device=device
1916-
)
1917-
x1 = (
1918-
x1.to_device(_sycl_queue.sycl_device)
1919-
if x1.sycl_queue != _sycl_queue
1920-
else x1
1921-
)
1922-
19231896
m = empty(
19241897
(x1.size, N),
19251898
dtype=_dtype,
1926-
usm_type=_usm_type,
1927-
sycl_queue=_sycl_queue,
1899+
usm_type=x1.usm_type,
1900+
sycl_queue=x1.sycl_queue,
19281901
)
19291902
tmp = m[:, ::-1] if not increasing else m
19301903
dpnp.power(
19311904
x1.reshape(-1, 1),
1932-
dpnp.arange(N, dtype=_dtype, sycl_queue=_sycl_queue),
1905+
dpnp.arange(N, dtype=_dtype, sycl_queue=x1.sycl_queue),
19331906
out=tmp,
19341907
)
19351908

19361909
return m
19371910

1938-
return call_origin(numpy.vander, x1, N=N, increasing=increasing)
1939-
19401911

19411912
def zeros(
19421913
shape,

dpnp/dpnp_iface_statistics.py

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

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

744738

745739
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
@@ -155,11 +155,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_s
155155
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok
156156
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides
157157

158-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list
159-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple
160-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list
161-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple
162-
163158
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid0
164159
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid1
165160
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
@@ -109,11 +109,6 @@ tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes
109109
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_3_{order='C', shape=(2, 3)}::test_item
110110
tests/third_party/cupy/core_tests/test_ndarray_conversion.py::TestNdarrayToBytes_param_4_{order='F', shape=(2, 3)}::test_item
111111

112-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_list
113-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_construction_from_tuple
114-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_list
115-
tests/third_party/cupy/creation_tests/test_matrix.py::TestMatrix::test_diag_extraction_from_nested_tuple
116-
117112
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_1darray
118113
tests/third_party/cupy/indexing_tests/test_insert.py::TestFillDiagonal_param_4_{shape=(3, 3), val=(2,), wrap=True}::test_fill_diagonal
119114
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

@@ -458,7 +457,7 @@ def test_triu_size_null(k):
458457
def test_vander(array, dtype, n, increase):
459458
if dtype in [dpnp.complex64, dpnp.complex128] and array == [0, 3, 5]:
460459
pytest.skip(
461-
"dpnp.power(dpnp.array(complex(0,0)), dpnp.array(0)) returns nan+nanj while it should be 1+0j"
460+
"per array API dpnp.power(complex(0,0)), 0) returns nan+nanj while NumPy returns 1+0j"
462461
)
463462
vander_func = lambda xp, x: xp.vander(x, N=n, increasing=increase)
464463

@@ -468,6 +467,16 @@ def test_vander(array, dtype, n, increase):
468467
assert_allclose(vander_func(numpy, a_np), vander_func(dpnp, a_dpnp))
469468

470469

470+
@pytest.mark.parametrize(
471+
"sequence",
472+
[[1, 2, 3, 4], (1, 2, 3, 4)],
473+
ids=["[1, 2, 3, 4]", "(1, 2, 3, 4)"],
474+
)
475+
def test_vander_seq(sequence):
476+
vander_func = lambda xp, x: xp.vander(x)
477+
assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence))
478+
479+
471480
@pytest.mark.parametrize(
472481
"shape",
473482
[(), 0, (0,), (2, 0, 3), (3, 2)],

0 commit comments

Comments
 (0)