Skip to content

Commit b730124

Browse files
Implement usm_ndarray.__arrray__ property
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.
1 parent 1ced1a7 commit b730124

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

dpctl/tensor/_usmarray.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,15 @@ cdef class usm_ndarray:
15521552
def __repr__(self):
15531553
return usm_ndarray_repr(self)
15541554

1555+
@property
1556+
def __array__(self):
1557+
"NumPy array protocol"
1558+
raise TypeError(
1559+
"Implicit conversion to a NumPy array is not allowed. "
1560+
"Use `dpctl.tensor.asnumpy` to copy data from this "
1561+
"`dpctl.tensor.usm_ndarray` instance to NumPy array"
1562+
)
1563+
15551564

15561565
cdef usm_ndarray _real_view(usm_ndarray ary):
15571566
"""

0 commit comments

Comments
 (0)