Skip to content

Commit

Permalink
Merge pull request #2007 from IntelPython/backport-gh-2003
Browse files Browse the repository at this point in the history
Backport gh-2003
  • Loading branch information
ndgrigorian authored Feb 21, 2025
2 parents 933c642 + 0b284f4 commit 1b319ec
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ A number of adjustments were also made to improve performance of `dpctl` reducti
* Fixed array API namespace inspection utilities showing `complex128` as a valid dtype on devices without double precision and `device` keywords not working with `dpctl.SyclQueue` or filter strings [gh-1979](https://github.com/IntelPython/dpctl/pull/1979)
* Fixed a bug in `test_sycl_device_interface.cpp` which would cause compilation to fail with Clang version 20.0 [gh-1989](https://github.com/IntelPython/dpctl/pull/1989)
* Fixed memory leaks in smart-pointer-managed USM temporaries in synchronizing kernel calls [gh-2002](https://github.com/IntelPython/dpctl/pull/2002)
* `UsmNDArray_MakeSimpleFromPtr` and `UsmNDArray_MakeFromPtr` now raise an error when provided an invalid `typenum` before attempting to create the array [gh-2003](https://github.com/IntelPython/dpctl/pull/2003)

### Maintenance

Expand Down
12 changes: 8 additions & 4 deletions dpctl/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,10 @@ cdef api object UsmNDArray_MakeSimpleFromPtr(
Returns:
Created usm_ndarray instance
"""
cdef size_t itemsize = type_bytesize(typenum)
cdef size_t nbytes = itemsize * nelems
cdef int itemsize = type_bytesize(typenum)
if (itemsize < 1):
raise ValueError("dtype with typenum=" + str(typenum) + " is not supported.")
cdef size_t nbytes = (<size_t> itemsize) * nelems
cdef c_dpmem._Memory mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
ptr, nbytes, QRef, memory_owner=owner
)
Expand Down Expand Up @@ -1817,7 +1819,7 @@ cdef api object UsmNDArray_MakeFromPtr(
Returns:
Created usm_ndarray instance
"""
cdef size_t itemsize = type_bytesize(typenum)
cdef int itemsize = type_bytesize(typenum)
cdef int err = 0
cdef size_t nelems = 1
cdef Py_ssize_t min_disp = 0
Expand All @@ -1830,6 +1832,8 @@ cdef api object UsmNDArray_MakeFromPtr(
cdef object obj_shape
cdef object obj_strides

if (itemsize < 1):
raise ValueError("dtype with typenum=" + str(typenum) + " is not supported.")
if (nd < 0):
raise ValueError("Dimensionality must be non-negative")
if (ptr is NULL or QRef is NULL):
Expand Down Expand Up @@ -1881,7 +1885,7 @@ cdef api object UsmNDArray_MakeFromPtr(
raise ValueError(
"Given shape, strides and offset reference out-of-bound memory"
)
nbytes = itemsize * (offset + max_disp + 1)
nbytes = (<size_t> itemsize) * (offset + max_disp + 1)
mobj = c_dpmem._Memory.create_from_usm_pointer_size_qref(
ptr, nbytes, QRef, memory_owner=owner
)
Expand Down
69 changes: 69 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,75 @@ def test_pyx_capi_make_general():
assert zd_arr._pointer == mat._pointer


def test_pyx_capi_make_fns_invalid_typenum():
q = get_queue_or_skip()
usm_ndarray = dpt.empty(tuple(), dtype="i4", sycl_queue=q)

make_simple_from_ptr = _pyx_capi_fnptr_to_callable(
usm_ndarray,
"UsmNDArray_MakeSimpleFromPtr",
b"PyObject *(size_t, int, DPCTLSyclUSMRef, "
b"DPCTLSyclQueueRef, PyObject *)",
fn_restype=ctypes.py_object,
fn_argtypes=(
ctypes.c_size_t,
ctypes.c_int,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.py_object,
),
)

nelems = 10
dtype = dpt.int64
arr = dpt.arange(nelems, dtype=dtype, sycl_queue=q)

with pytest.raises(ValueError):
make_simple_from_ptr(
ctypes.c_size_t(nelems),
-1,
arr._pointer,
arr.sycl_queue.addressof_ref(),
arr,
)

make_from_ptr = _pyx_capi_fnptr_to_callable(
usm_ndarray,
"UsmNDArray_MakeFromPtr",
b"PyObject *(int, Py_ssize_t const *, int, Py_ssize_t const *, "
b"DPCTLSyclUSMRef, DPCTLSyclQueueRef, Py_ssize_t, PyObject *)",
fn_restype=ctypes.py_object,
fn_argtypes=(
ctypes.c_int,
ctypes.POINTER(ctypes.c_ssize_t),
ctypes.c_int,
ctypes.POINTER(ctypes.c_ssize_t),
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_ssize_t,
ctypes.py_object,
),
)
c_shape = (ctypes.c_ssize_t * 1)(
nelems,
)
c_strides = (ctypes.c_ssize_t * 1)(
1,
)
with pytest.raises(ValueError):
make_from_ptr(
ctypes.c_int(1),
c_shape,
-1,
c_strides,
arr._pointer,
arr.sycl_queue.addressof_ref(),
ctypes.c_ssize_t(0),
arr,
)
del arr


def _pyx_capi_int(X, pyx_capi_name, caps_name=b"int", val_restype=ctypes.c_int):
import sys

Expand Down

0 comments on commit 1b319ec

Please sign in to comment.