Skip to content

Address NumPy 1.25 deprecation warnings #1368

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
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
25 changes: 13 additions & 12 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ cdef class InternalUSMArrayError(Exception):
pass


cdef object _as_zero_dim_ndarray(object usm_ary):
"Convert size-1 array to NumPy 0d array"
mem_view = dpmem.as_usm_memory(usm_ary)
host_buf = mem_view.copy_to_host()
view = host_buf.view(usm_ary.dtype)
view.shape = tuple()
return view


cdef class usm_ndarray:
""" usm_ndarray(shape, dtype=None, strides=None, buffer="device", \
offset=0, order="C", buffer_ctor_kwargs=dict(), \
Expand Down Expand Up @@ -840,9 +849,7 @@ cdef class usm_ndarray:

def __bool__(self):
if self.size == 1:
mem_view = dpmem.as_usm_memory(self)
host_buf = mem_view.copy_to_host()
view = host_buf.view(self.dtype)
view = _as_zero_dim_ndarray(self)
return view.__bool__()

if self.size == 0:
Expand All @@ -857,9 +864,7 @@ cdef class usm_ndarray:

def __float__(self):
if self.size == 1:
mem_view = dpmem.as_usm_memory(self)
host_buf = mem_view.copy_to_host()
view = host_buf.view(self.dtype)
view = _as_zero_dim_ndarray(self)
return view.__float__()

raise ValueError(
Expand All @@ -868,9 +873,7 @@ cdef class usm_ndarray:

def __complex__(self):
if self.size == 1:
mem_view = dpmem.as_usm_memory(self)
host_buf = mem_view.copy_to_host()
view = host_buf.view(self.dtype)
view = _as_zero_dim_ndarray(self)
return view.__complex__()

raise ValueError(
Expand All @@ -879,9 +882,7 @@ cdef class usm_ndarray:

def __int__(self):
if self.size == 1:
mem_view = dpmem.as_usm_memory(self)
host_buf = mem_view.copy_to_host()
view = host_buf.view(self.dtype)
view = _as_zero_dim_ndarray(self)
return view.__int__()

raise ValueError(
Expand Down
10 changes: 6 additions & 4 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ def test_copy_scalar_with_func(func, shape, dtype):
X = dpt.usm_ndarray(shape, dtype=dtype)
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
Y = np.arange(1, X.size + 1, dtype=dtype).reshape(shape)
X.usm_data.copy_from_host(Y.reshape(-1).view("|u1"))
Y = np.arange(1, X.size + 1, dtype=dtype)
X.usm_data.copy_from_host(Y.view("|u1"))
Y.shape = tuple()
assert func(X) == func(Y)


Expand All @@ -254,8 +255,9 @@ def test_copy_scalar_with_method(method, shape, dtype):
X = dpt.usm_ndarray(shape, dtype=dtype)
except dpctl.SyclDeviceCreationError:
pytest.skip("No SYCL devices available")
Y = np.arange(1, X.size + 1, dtype=dtype).reshape(shape)
X.usm_data.copy_from_host(Y.reshape(-1).view("|u1"))
Y = np.arange(1, X.size + 1, dtype=dtype)
X.usm_data.copy_from_host(Y.view("|u1"))
Y.shape = tuple()
assert getattr(X, method)() == getattr(Y, method)()


Expand Down