Skip to content

Commit

Permalink
Implement usm_ndarray.__arrray__ method
Browse files Browse the repository at this point in the history
The property raises TypeError informing user that
implicit conversions from usm_ndarray to numpy.ndarray
are not permitted.

Explicit conversion using `dpctl.tensor.asnumpy` is
advised.

Disallowing implicit conversion prevents `np.asarray(usm_ar)`
from creating an array of 0D usm_ndarray instances, because
using it is very costly due to multitude of short-array transfers
from GPU to host.
  • Loading branch information
oleksandr-pavlyk committed Jan 14, 2025
1 parent 0f3536b commit 81db5e5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,14 @@ cdef class usm_ndarray:
def __repr__(self):
return usm_ndarray_repr(self)

def __array__(self, dtype=None, copy=None):
"NumPy array protocol"
raise TypeError(
"Implicit conversion to a NumPy array is not allowed. "
"Use `dpctl.tensor.asnumpy` to copy data from this "
"`dpctl.tensor.usm_ndarray` instance to NumPy array"
)


cdef usm_ndarray _real_view(usm_ndarray ary):
"""
Expand Down

0 comments on commit 81db5e5

Please sign in to comment.