Skip to content

Enable third party tests for dpnp.ndarray #1964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ repos:
[
"-rn", # Only display messages
"-sn", # Don't display the score
"--disable=c-extension-no-member",
"--disable=import-error",
"--disable=redefined-builtin",
"--disable=unused-wildcard-import"
Expand Down
50 changes: 27 additions & 23 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,30 @@ def __init__(
usm_type="device",
sycl_queue=None,
):
if order is None:
order = "C"

if buffer is not None:
if not isinstance(buffer, dpt.usm_ndarray):
raise TypeError(
"Expected dpctl.tensor.usm_ndarray, got {}"
"".format(type(buffer))
)
if buffer.shape != shape:
raise ValueError(
"Expected buffer.shape={}, got {}"
"".format(shape, buffer.shape)
)
self._array_obj = dpt.asarray(buffer, copy=False, order=order)
buffer = dpnp.get_usm_ndarray(buffer)

if dtype is None:
dtype = buffer.dtype
else:
sycl_queue_normalized = dpnp.get_normalized_queue_device(
device=device, sycl_queue=sycl_queue
)
self._array_obj = dpt.usm_ndarray(
shape,
dtype=dtype,
strides=strides,
buffer=usm_type,
offset=offset,
order=order,
buffer_ctor_kwargs={"queue": sycl_queue_normalized},
)
buffer = usm_type

sycl_queue_normalized = dpnp.get_normalized_queue_device(
device=device, sycl_queue=sycl_queue
)

self._array_obj = dpt.usm_ndarray(
shape,
dtype=dtype,
strides=strides,
buffer=buffer,
offset=offset,
order=order,
buffer_ctor_kwargs={"queue": sycl_queue_normalized},
)

@property
def __sycl_usm_array_interface__(self):
Expand Down Expand Up @@ -457,6 +456,8 @@ def __setitem__(self, key, val):
# '__setstate__',
# '__sizeof__',

__slots__ = ("_array_obj",)

def __str__(self):
"""Return ``str(self)``."""
return self._array_obj.__str__()
Expand Down Expand Up @@ -1275,6 +1276,9 @@ def shape(self, newshape):

"""

if not isinstance(newshape, (list, tuple)):
newshape = (newshape,)

self._array_obj.shape = newshape

@property
Expand Down
21 changes: 10 additions & 11 deletions dpnp/dpnp_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def arange(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)

return dpnp_array(array_obj.shape, buffer=array_obj)
return dpnp_array._create_from_usm_ndarray(array_obj)


def asarray(
Expand Down Expand Up @@ -132,7 +131,7 @@ def asarray(
if array_obj is x1_obj and isinstance(x1, dpnp_array):
return x1

return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)


def copy(x1, /, *, order="K"):
Expand All @@ -141,7 +140,7 @@ def copy(x1, /, *, order="K"):
order = "K"

array_obj = dpt.copy(dpnp.get_usm_ndarray(x1), order=order)
return dpnp_array(array_obj.shape, buffer=array_obj, order="K")
return dpnp_array._create_from_usm_ndarray(array_obj)


def empty(
Expand Down Expand Up @@ -169,7 +168,7 @@ def empty(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)


def eye(
Expand Down Expand Up @@ -202,7 +201,7 @@ def eye(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)


def full(
Expand Down Expand Up @@ -236,7 +235,7 @@ def full(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)


def ones(
Expand Down Expand Up @@ -264,19 +263,19 @@ def ones(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)


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


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


def zeros(
Expand Down Expand Up @@ -304,4 +303,4 @@ def zeros(
usm_type=usm_type,
sycl_queue=sycl_queue_normalized,
)
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
return dpnp_array._create_from_usm_ndarray(array_obj)
1 change: 0 additions & 1 deletion dpnp/dpnp_iface_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"""

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


import dpctl.tensor._tensor_elementwise_impl as ti
Expand Down
1 change: 0 additions & 1 deletion dpnp/dpnp_iface_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"""

# pylint: disable=protected-access
# pylint: disable=c-extension-no-member
# pylint: disable=duplicate-code
# pylint: disable=no-name-in-module

Expand Down
1 change: 0 additions & 1 deletion dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"""

# pylint: disable=protected-access
# pylint: disable=c-extension-no-member
# pylint: disable=duplicate-code
# pylint: disable=no-name-in-module

Expand Down
1 change: 0 additions & 1 deletion dpnp/fft/dpnp_utils_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"""

# pylint: disable=protected-access
# pylint: disable=c-extension-no-member
# pylint: disable=no-name-in-module

import dpctl
Expand Down
46 changes: 25 additions & 21 deletions tests/third_party/cupy/core_tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,50 @@ def test_shape_not_integer(self):
with pytest.raises(TypeError):
xp.ndarray((1.0,))

@pytest.mark.skip("passing buffer as dpnp array is not supported")
def test_shape_int_with_strides(self):
dummy = cupy.ndarray(3)
a = cupy.ndarray(3, strides=(0,), buffer=dummy)
assert a.shape == (3,)
assert a.strides == (0,)

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

b = cupy.ndarray((2, 3), numpy.float32, buffer=memptr)
b = cupy.ndarray((2, 3), numpy.float32, memptr)
testing.assert_array_equal(a, b)

b += 1
testing.assert_array_equal(a, b)

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

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

a[:] = 1
a[0, 2] = 4
assert float(a[1, 0]) == 4

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

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

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

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

@pytest.mark.skip("__slots__ is not supported")
def test_slots(self):
# Test for #7883.
a = cupy.ndarray((2, 3))
Expand All @@ -130,7 +128,7 @@ class UserNdarray(cupy.ndarray):
@testing.parameterize(
*testing.product(
{
"shape": [(), (1,), (1, 2), (1, 2, 3)],
"shape": [(), (1,), (2, 3), (1, 2, 3)],
"order": ["C", "F"],
"dtype": [
numpy.uint8, # itemsize=1
Expand All @@ -139,13 +137,15 @@ class UserNdarray(cupy.ndarray):
}
)
)
@pytest.mark.skip("strides may vary")
class TestNdarrayInitStrides(unittest.TestCase):
# Check the strides given shape, itemsize and order.
@testing.numpy_cupy_equal()
def test_strides(self, xp):
arr = xp.ndarray(self.shape, dtype=self.dtype, order=self.order)
return (arr.strides, arr.flags.c_contiguous, arr.flags.f_contiguous)
strides = arr.strides
if xp is cupy:
strides = tuple(i * arr.itemsize for i in strides)
return (strides, arr.flags.c_contiguous, arr.flags.f_contiguous)


class TestNdarrayInitRaise(unittest.TestCase):
Expand Down Expand Up @@ -210,8 +210,8 @@ def test_deepcopy_multi_device(self):
"""


@pytest.mark.skip()
class TestNdarrayCopy:
@pytest.mark.skip("SAT-7167: add device keyword")
@testing.multi_gpu(2)
@testing.for_orders("CFA")
def test_copy_multi_device_non_contiguous(self, order):
Expand All @@ -221,14 +221,16 @@ def test_copy_multi_device_non_contiguous(self, order):
assert arr2.device == dev1
testing.assert_array_equal(arr, arr2)

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

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


@pytest.mark.skip()
class TestNdarrayShape(unittest.TestCase):
@testing.numpy_cupy_array_equal()
def test_shape_set(self, xp):
arr = xp.ndarray((2, 3))
arr.shape = (3, 2)
return xp.array(arr.shape)

@pytest.mark.skip(
"dpctl-1699: shape setter does not work with negative shape"
)
@testing.numpy_cupy_array_equal()
def test_shape_set_infer(self, xp):
arr = xp.ndarray((2, 3))
Expand Down Expand Up @@ -682,7 +686,7 @@ def __array_finalize__(self, obj):
self.info = getattr(obj, "info", None)


@pytest.mark.skip("explicit constructor call is not supported")
@pytest.mark.skip("SAT-7168: explicit constructor call is not supported")
class TestNdarraySubclass:
def test_explicit_constructor_call(self):
a = C([0, 1, 2, 3], info="information")
Expand Down
Loading