Skip to content

dtype keyword of usm_ndarray now supports np.double and other types #526

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 1 commit into from
Aug 16, 2021
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
33 changes: 22 additions & 11 deletions dpctl/tensor/_types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,33 @@ cdef int typenum_from_format(str s) except *:
raise TypeError("Format '" + s + "' can only have native byteorder.")
return dt.num

cdef int descr_to_typenum(object dtype):
"Returns typenum for argumentd dtype that has attribute descr, assumed numpy.dtype"
obj = getattr(dtype, 'descr')
if (not isinstance(obj, list) or len(obj) != 1):
return -1
obj = obj[0]
if (not isinstance(obj, tuple) or len(obj) != 2 or obj[0]):
return -1
obj = obj[1]
if not isinstance(obj, str):
return -1
return typenum_from_format(obj)


cdef int dtype_to_typenum(dtype) except *:
if isinstance(dtype, str):
return typenum_from_format(dtype)
elif isinstance(dtype, bytes):
return typenum_from_format(dtype.decode("UTF-8"))
elif hasattr(dtype, 'descr'):
obj = getattr(dtype, 'descr')
if (not isinstance(obj, list) or len(obj) != 1):
return -1
obj = obj[0]
if (not isinstance(obj, tuple) or len(obj) != 2 or obj[0]):
return -1
obj = obj[1]
if not isinstance(obj, str):
return -1
return typenum_from_format(obj)
return descr_to_typenum(dtype)
else:
return -1
try:
dt = np.dtype(dtype)
if hasattr(dt, 'descr'):
return descr_to_typenum(dt)
else:
return -1
except Exception:
return -1
1 change: 1 addition & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def test_allocate_usm_ndarray(shape, usm_type):
"c8",
"c16",
np.dtype("d"),
np.half,
],
)
def test_dtypes(dtype):
Expand Down