Skip to content
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

Fix usm_ndarray ctor when shape is integral numpy scalar #1467

Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 13 additions & 7 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,19 @@ cdef class usm_ndarray:
cdef bint is_fp16 = False

self._reset()
if (not isinstance(shape, (list, tuple))
and not hasattr(shape, 'tolist')):
try:
<Py_ssize_t> shape
shape = [shape, ]
except Exception:
raise TypeError("Argument shape must be a list or a tuple.")
if not isinstance(shape, (list, tuple)):
if hasattr(shape, 'tolist'):
fn = getattr(shape, 'tolist')
if callable(fn):
shape = shape.tolist()
if not isinstance(shape, (list, tuple)):
try:
<Py_ssize_t> shape
shape = [shape, ]
except Exception:
raise TypeError(
"Argument shape must be a list or a tuple."
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the logic of this exception and message could be improved a bit. For instance:

In [5]: x = dpt.ones(np.prod((2, 3, 4), dtype="f4"), dtype="i8")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/repos/dpctl/dpctl/tensor/_usmarray.pyx:192, in dpctl.tensor._usmarray.usm_ndarray.__cinit__()
    191 try:
--> 192     <Py_ssize_t> shape
    193     shape = [shape, ]

TypeError: 'float' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 x = dpt.ones(np.prod((2, 3, 4), dtype="f4"), dtype="i8")

File ~/repos/dpctl/dpctl/tensor/_ctors.py:968, in ones(shape, dtype, order, device, usm_type, sycl_queue)
    966 sycl_queue = normalize_queue_device(sycl_queue=sycl_queue, device=device)
    967 dtype = _get_dtype(dtype, sycl_queue)
--> 968 res = dpt.usm_ndarray(
    969     shape,
    970     dtype=dtype,
    971     buffer=usm_type,
    972     order=order,
    973     buffer_ctor_kwargs={"queue": sycl_queue},
    974 )
    975 hev, _ = ti._full_usm_ndarray(1, res, sycl_queue)
    976 hev.wait()

File ~/repos/dpctl/dpctl/tensor/_usmarray.pyx:195, in dpctl.tensor._usmarray.usm_ndarray.__cinit__()
    193     shape = [shape, ]
    194 except Exception:
--> 195     raise TypeError(
    196         "Argument shape must be a list or a tuple."
    197     )

TypeError: Argument shape must be a list or a tuple.

It seems a bit misleading at first, because it would work for np.prod((2, 3, 4)).

nd = len(shape)
if dtype is None:
if isinstance(buffer, (dpmem._memory._Memory, usm_ndarray)):
Expand Down
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 @@ -39,6 +39,7 @@
(2, 5, 2),
(2, 2, 2, 2, 2, 2, 2, 2),
5,
np.int32(7),
],
)
@pytest.mark.parametrize("usm_type", ["shared", "host", "device"])
Expand Down
14 changes: 8 additions & 6 deletions dpctl/tests/test_usm_ndarray_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ def test_search_reduction_kernels(arg_dtype):
q = get_queue_or_skip()
skip_if_dtype_not_supported(arg_dtype, q)

x = dpt.ones((24 * 1025), dtype=arg_dtype, sycl_queue=q)
x_shape = (24, 1024)
x_size = np.prod(x_shape)
x = dpt.ones(x_size, dtype=arg_dtype, sycl_queue=q)
idx = randrange(x.size)
idx_tup = np.unravel_index(idx, (24, 1025))
idx_tup = np.unravel_index(idx, x_shape)
x[idx] = 2

m = dpt.argmax(x)
Expand All @@ -194,7 +196,7 @@ def test_search_reduction_kernels(arg_dtype):
m = dpt.argmax(y)
assert m == 2 * idx

x = dpt.reshape(x, (24, 1025))
x = dpt.reshape(x, x_shape)

x[idx_tup[0], :] = 3
m = dpt.argmax(x, axis=0)
Expand All @@ -209,15 +211,15 @@ def test_search_reduction_kernels(arg_dtype):
m = dpt.argmax(x, axis=1)
assert dpt.all(m == idx)

x = dpt.ones((24 * 1025), dtype=arg_dtype, sycl_queue=q)
x = dpt.ones(x_size, dtype=arg_dtype, sycl_queue=q)
idx = randrange(x.size)
idx_tup = np.unravel_index(idx, (24, 1025))
idx_tup = np.unravel_index(idx, x_shape)
x[idx] = 0

m = dpt.argmin(x)
assert m == idx

x = dpt.reshape(x, (24, 1025))
x = dpt.reshape(x, x_shape)

x[idx_tup[0], :] = -1
m = dpt.argmin(x, axis=0)
Expand Down