Skip to content
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
7 changes: 6 additions & 1 deletion dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ cdef class usm_ndarray:
cdef Py_ssize_t _offset = offset
cdef Py_ssize_t ary_min_displacement = 0
cdef Py_ssize_t ary_max_displacement = 0
cdef Py_ssize_t tmp = 0
cdef char * data_ptr = NULL

self._reset()
if (not isinstance(shape, (list, tuple))
and not hasattr(shape, 'tolist')):
raise TypeError("Argument shape must be a list of a tuple.")
try:
tmp = <Py_ssize_t> shape
shape = [shape, ]
except Exception:
raise TypeError("Argument shape must be a list or a tuple.")
nd = len(shape)
typenum = dtype_to_typenum(dtype)
itemsize = type_bytesize(typenum)
Expand Down
11 changes: 11 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
(4, 5),
(2, 5, 2),
(2, 2, 2, 2, 2, 2, 2, 2),
5,
],
)
@pytest.mark.parametrize("usm_type", ["shared", "host", "device"])
Expand Down Expand Up @@ -74,13 +75,23 @@ def test_allocate_usm_ndarray(shape, usm_type):
"f8",
"c8",
"c16",
b"float32",
np.dtype("d"),
np.half,
],
)
def test_dtypes(dtype):
Xusm = dpt.usm_ndarray((1,), dtype=dtype)
assert Xusm.itemsize == np.dtype(dtype).itemsize
expected_fmt = (np.dtype(dtype).str)[1:]
actual_fmt = Xusm.__sycl_usm_array_interface__["typestr"][1:]
assert expected_fmt == actual_fmt


@pytest.mark.parametrize("dtype", ["", ">f4", "invalid", 123])
def test_dtypes_invalid(dtype):
with pytest.raises((TypeError, ValueError)):
dpt.usm_ndarray((1,), dtype=dtype)


def test_properties():
Expand Down