Skip to content

Commit 92faa20

Browse files
authored
Enable third party tests for dpnp.ndarray (#1964)
* Enable all supported use cases and address missing one * Use dpnp_array._create_from_usm_ndarray() within dpnp container * Allow shape setter to work with integer scalar * Applied pre-commit hook * Removed commented leftovers
1 parent 7948a0a commit 92faa20

File tree

8 files changed

+63
-59
lines changed

8 files changed

+63
-59
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ repos:
9696
[
9797
"-rn", # Only display messages
9898
"-sn", # Don't display the score
99+
"--disable=c-extension-no-member",
99100
"--disable=import-error",
100101
"--disable=redefined-builtin",
101102
"--disable=unused-wildcard-import"

dpnp/dpnp_array.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,30 @@ def __init__(
7070
usm_type="device",
7171
sycl_queue=None,
7272
):
73+
if order is None:
74+
order = "C"
75+
7376
if buffer is not None:
74-
if not isinstance(buffer, dpt.usm_ndarray):
75-
raise TypeError(
76-
"Expected dpctl.tensor.usm_ndarray, got {}"
77-
"".format(type(buffer))
78-
)
79-
if buffer.shape != shape:
80-
raise ValueError(
81-
"Expected buffer.shape={}, got {}"
82-
"".format(shape, buffer.shape)
83-
)
84-
self._array_obj = dpt.asarray(buffer, copy=False, order=order)
77+
buffer = dpnp.get_usm_ndarray(buffer)
78+
79+
if dtype is None:
80+
dtype = buffer.dtype
8581
else:
86-
sycl_queue_normalized = dpnp.get_normalized_queue_device(
87-
device=device, sycl_queue=sycl_queue
88-
)
89-
self._array_obj = dpt.usm_ndarray(
90-
shape,
91-
dtype=dtype,
92-
strides=strides,
93-
buffer=usm_type,
94-
offset=offset,
95-
order=order,
96-
buffer_ctor_kwargs={"queue": sycl_queue_normalized},
97-
)
82+
buffer = usm_type
83+
84+
sycl_queue_normalized = dpnp.get_normalized_queue_device(
85+
device=device, sycl_queue=sycl_queue
86+
)
87+
88+
self._array_obj = dpt.usm_ndarray(
89+
shape,
90+
dtype=dtype,
91+
strides=strides,
92+
buffer=buffer,
93+
offset=offset,
94+
order=order,
95+
buffer_ctor_kwargs={"queue": sycl_queue_normalized},
96+
)
9897

9998
@property
10099
def __sycl_usm_array_interface__(self):
@@ -457,6 +456,8 @@ def __setitem__(self, key, val):
457456
# '__setstate__',
458457
# '__sizeof__',
459458

459+
__slots__ = ("_array_obj",)
460+
460461
def __str__(self):
461462
"""Return ``str(self)``."""
462463
return self._array_obj.__str__()
@@ -1275,6 +1276,9 @@ def shape(self, newshape):
12751276
12761277
"""
12771278

1279+
if not isinstance(newshape, (list, tuple)):
1280+
newshape = (newshape,)
1281+
12781282
self._array_obj.shape = newshape
12791283

12801284
@property

dpnp/dpnp_container.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def arange(
7979
usm_type=usm_type,
8080
sycl_queue=sycl_queue_normalized,
8181
)
82-
83-
return dpnp_array(array_obj.shape, buffer=array_obj)
82+
return dpnp_array._create_from_usm_ndarray(array_obj)
8483

8584

8685
def asarray(
@@ -132,7 +131,7 @@ def asarray(
132131
if array_obj is x1_obj and isinstance(x1, dpnp_array):
133132
return x1
134133

135-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
134+
return dpnp_array._create_from_usm_ndarray(array_obj)
136135

137136

138137
def copy(x1, /, *, order="K"):
@@ -141,7 +140,7 @@ def copy(x1, /, *, order="K"):
141140
order = "K"
142141

143142
array_obj = dpt.copy(dpnp.get_usm_ndarray(x1), order=order)
144-
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
143+
return dpnp_array._create_from_usm_ndarray(array_obj)
145144

146145

147146
def empty(
@@ -169,7 +168,7 @@ def empty(
169168
usm_type=usm_type,
170169
sycl_queue=sycl_queue_normalized,
171170
)
172-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
171+
return dpnp_array._create_from_usm_ndarray(array_obj)
173172

174173

175174
def eye(
@@ -202,7 +201,7 @@ def eye(
202201
usm_type=usm_type,
203202
sycl_queue=sycl_queue_normalized,
204203
)
205-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
204+
return dpnp_array._create_from_usm_ndarray(array_obj)
206205

207206

208207
def full(
@@ -236,7 +235,7 @@ def full(
236235
usm_type=usm_type,
237236
sycl_queue=sycl_queue_normalized,
238237
)
239-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
238+
return dpnp_array._create_from_usm_ndarray(array_obj)
240239

241240

242241
def ones(
@@ -264,19 +263,19 @@ def ones(
264263
usm_type=usm_type,
265264
sycl_queue=sycl_queue_normalized,
266265
)
267-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
266+
return dpnp_array._create_from_usm_ndarray(array_obj)
268267

269268

270269
def tril(x1, /, *, k=0):
271270
"""Creates `dpnp_array` as lower triangular part of an input array."""
272271
array_obj = dpt.tril(dpnp.get_usm_ndarray(x1), k=k)
273-
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
272+
return dpnp_array._create_from_usm_ndarray(array_obj)
274273

275274

276275
def triu(x1, /, *, k=0):
277276
"""Creates `dpnp_array` as upper triangular part of an input array."""
278277
array_obj = dpt.triu(dpnp.get_usm_ndarray(x1), k=k)
279-
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
278+
return dpnp_array._create_from_usm_ndarray(array_obj)
280279

281280

282281
def zeros(
@@ -304,4 +303,4 @@ def zeros(
304303
usm_type=usm_type,
305304
sycl_queue=sycl_queue_normalized,
306305
)
307-
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
306+
return dpnp_array._create_from_usm_ndarray(array_obj)

dpnp/dpnp_iface_bitwise.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"""
3939

4040
# pylint: disable=protected-access
41-
# pylint: disable=c-extension-no-member
4241

4342

4443
import dpctl.tensor._tensor_elementwise_impl as ti

dpnp/dpnp_iface_logic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"""
4141

4242
# pylint: disable=protected-access
43-
# pylint: disable=c-extension-no-member
4443
# pylint: disable=duplicate-code
4544
# pylint: disable=no-name-in-module
4645

dpnp/dpnp_iface_mathematical.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"""
3939

4040
# pylint: disable=protected-access
41-
# pylint: disable=c-extension-no-member
4241
# pylint: disable=duplicate-code
4342
# pylint: disable=no-name-in-module
4443

dpnp/fft/dpnp_utils_fft.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"""
3535

3636
# pylint: disable=protected-access
37-
# pylint: disable=c-extension-no-member
3837
# pylint: disable=no-name-in-module
3938

4039
import dpctl

tests/third_party/cupy/core_tests/test_ndarray.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,50 @@ def test_shape_not_integer(self):
4242
with pytest.raises(TypeError):
4343
xp.ndarray((1.0,))
4444

45-
@pytest.mark.skip("passing buffer as dpnp array is not supported")
4645
def test_shape_int_with_strides(self):
4746
dummy = cupy.ndarray(3)
4847
a = cupy.ndarray(3, strides=(0,), buffer=dummy)
4948
assert a.shape == (3,)
5049
assert a.strides == (0,)
5150

52-
@pytest.mark.skip("passing buffer as dpnp array is not supported")
5351
def test_memptr(self):
5452
a = cupy.arange(6).astype(numpy.float32).reshape((2, 3))
5553
memptr = a
5654

57-
b = cupy.ndarray((2, 3), numpy.float32, buffer=memptr)
55+
b = cupy.ndarray((2, 3), numpy.float32, memptr)
5856
testing.assert_array_equal(a, b)
5957

6058
b += 1
6159
testing.assert_array_equal(a, b)
6260

63-
@pytest.mark.skip("self-overlapping strides are not supported")
61+
@pytest.mark.skip(
62+
"dpctl-1765: might lead to race condition (no plan to support that)"
63+
)
6464
def test_memptr_with_strides(self):
6565
buf = cupy.ndarray(20, numpy.uint8)
6666
memptr = buf
6767

6868
# self-overlapping strides
69-
a = cupy.ndarray((2, 3), numpy.float32, buffer=memptr, strides=(8, 4))
70-
assert a.strides == (8, 4)
69+
a = cupy.ndarray((2, 3), numpy.float32, memptr, strides=(2, 1))
70+
assert a.strides == (2, 1)
7171

7272
a[:] = 1
7373
a[0, 2] = 4
7474
assert float(a[1, 0]) == 4
7575

76-
@pytest.mark.skip("no exception raised by dpctl")
76+
@pytest.mark.skip(
77+
"dpctl-1766: no exception raised by dpctl (no plan to support that)"
78+
)
7779
def test_strides_without_memptr(self):
7880
for xp in (numpy, cupy):
7981
with pytest.raises(ValueError):
8082
xp.ndarray((2, 3), numpy.float32, strides=(20, 4))
8183

82-
@pytest.mark.skip("passing buffer as dpnp array is not supported")
8384
def test_strides_is_given_and_order_is_ignored(self):
8485
buf = cupy.ndarray(20, numpy.uint8)
8586
a = cupy.ndarray((2, 3), numpy.float32, buf, strides=(2, 1), order="C")
8687
assert a.strides == (2, 1)
8788

88-
@pytest.mark.skip("dpctl-1724 issue")
8989
@testing.with_requires("numpy>=1.19")
9090
def test_strides_is_given_but_order_is_invalid(self):
9191
for xp in (numpy, cupy):
@@ -102,7 +102,6 @@ def test_order(self):
102102
assert a.flags.f_contiguous
103103
assert not a.flags.c_contiguous
104104

105-
@pytest.mark.skip("passing 'None' into order arguments is not supported")
106105
def test_order_none(self):
107106
shape = (2, 3, 4)
108107
a = cupy.ndarray(shape, order=None)
@@ -113,7 +112,6 @@ def test_order_none(self):
113112
i * a.itemsize == j for i, j in zip(a.strides, a_cpu.strides)
114113
)
115114

116-
@pytest.mark.skip("__slots__ is not supported")
117115
def test_slots(self):
118116
# Test for #7883.
119117
a = cupy.ndarray((2, 3))
@@ -130,7 +128,7 @@ class UserNdarray(cupy.ndarray):
130128
@testing.parameterize(
131129
*testing.product(
132130
{
133-
"shape": [(), (1,), (1, 2), (1, 2, 3)],
131+
"shape": [(), (1,), (2, 3), (1, 2, 3)],
134132
"order": ["C", "F"],
135133
"dtype": [
136134
numpy.uint8, # itemsize=1
@@ -139,13 +137,15 @@ class UserNdarray(cupy.ndarray):
139137
}
140138
)
141139
)
142-
@pytest.mark.skip("strides may vary")
143140
class TestNdarrayInitStrides(unittest.TestCase):
144141
# Check the strides given shape, itemsize and order.
145142
@testing.numpy_cupy_equal()
146143
def test_strides(self, xp):
147144
arr = xp.ndarray(self.shape, dtype=self.dtype, order=self.order)
148-
return (arr.strides, arr.flags.c_contiguous, arr.flags.f_contiguous)
145+
strides = arr.strides
146+
if xp is cupy:
147+
strides = tuple(i * arr.itemsize for i in strides)
148+
return (strides, arr.flags.c_contiguous, arr.flags.f_contiguous)
149149

150150

151151
class TestNdarrayInitRaise(unittest.TestCase):
@@ -210,8 +210,8 @@ def test_deepcopy_multi_device(self):
210210
"""
211211

212212

213-
@pytest.mark.skip()
214213
class TestNdarrayCopy:
214+
@pytest.mark.skip("SAT-7167: add device keyword")
215215
@testing.multi_gpu(2)
216216
@testing.for_orders("CFA")
217217
def test_copy_multi_device_non_contiguous(self, order):
@@ -221,14 +221,16 @@ def test_copy_multi_device_non_contiguous(self, order):
221221
assert arr2.device == dev1
222222
testing.assert_array_equal(arr, arr2)
223223

224+
@pytest.mark.skip("SAT-7167: add device keyword")
224225
@testing.multi_gpu(2)
225226
def test_copy_multi_device_non_contiguous_K(self):
226-
arr = _core.ndarray((20,))[::2]
227-
with cuda.Device(1):
228-
with pytest.raises(NotImplementedError):
229-
arr.copy("K")
227+
arr = cupy.ndarray((20,))[::2]
228+
dev1 = dpctl.SyclDevice()
229+
with pytest.raises(NotImplementedError):
230+
arr.copy("K", device=dev1)
230231

231232
# See cupy/cupy#5004
233+
@pytest.mark.skip("RawKernel() is not supported")
232234
@testing.multi_gpu(2)
233235
def test_copy_multi_device_with_stream(self):
234236
# Kernel that takes long enough then finally writes values.
@@ -256,14 +258,16 @@ def test_copy_multi_device_with_stream(self):
256258
)
257259

258260

259-
@pytest.mark.skip()
260261
class TestNdarrayShape(unittest.TestCase):
261262
@testing.numpy_cupy_array_equal()
262263
def test_shape_set(self, xp):
263264
arr = xp.ndarray((2, 3))
264265
arr.shape = (3, 2)
265266
return xp.array(arr.shape)
266267

268+
@pytest.mark.skip(
269+
"dpctl-1699: shape setter does not work with negative shape"
270+
)
267271
@testing.numpy_cupy_array_equal()
268272
def test_shape_set_infer(self, xp):
269273
arr = xp.ndarray((2, 3))
@@ -682,7 +686,7 @@ def __array_finalize__(self, obj):
682686
self.info = getattr(obj, "info", None)
683687

684688

685-
@pytest.mark.skip("explicit constructor call is not supported")
689+
@pytest.mark.skip("SAT-7168: explicit constructor call is not supported")
686690
class TestNdarraySubclass:
687691
def test_explicit_constructor_call(self):
688692
a = C([0, 1, 2, 3], info="information")

0 commit comments

Comments
 (0)