Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Fixed `dpnp.partition` to accept NumPy integers and integer arrays as `kth`, and to raise as NumPy does for a boolean or an empty `kth` [#3138](https://github.com/IntelPython/dpnp/pull/3138)
* Fixed `dpnp.nanmedian` raising `TypeError` for an `out` array of a lower precision data type instead of casting to it as `dpnp.median` does [#3138](https://github.com/IntelPython/dpnp/pull/3138)
* Fixed a `TypeError` in `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` when importing a DLPack tensor on a oneAPI device with a NULL data pointer, which DLPack asks for in a tensor of size zero [#3197](https://github.com/IntelPython/dpnp/pull/3197)
* Fixed `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` sizing the memory of an empty DLPack tensor on a oneAPI device by its nonzero dimensions, which allocated that size for a NULL data pointer (8 GiB for shape `(0, 2**31)` of `int32`) and otherwise claimed more memory than the producer allocated [#3203](https://github.com/IntelPython/dpnp/pull/3203)

### Security

Expand Down
5 changes: 5 additions & 0 deletions dpnp/tensor/_dlpack.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,11 @@ cpdef object from_dlpack_capsule(object py_caps):
for i in range(dl_tensor.ndim):
stride_i = dl_tensor.strides[i]
shape_i = dl_tensor.shape[i]
if shape_i == 0:
# an empty tensor spans no elements
offset_min = 0
offset_max = 0
break
if shape_i > 1:
shape_i -= 1
if stride_i > 0:
Expand Down
27 changes: 27 additions & 0 deletions dpnp/tests/tensor/test_usm_ndarray_dlpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ def test_from_dlpack_capsule_null_data(versioned):
assert calls == [1]


@pytest.mark.parametrize(
"shape, strides",
[((0, 3), (3, 1)), ((3, 0), (1, 3)), ((2, 0, 4), (0, 4, 1))],
)
def test_from_dlpack_capsule_null_data_empty_nd(shape, strides):
q = get_queue_or_skip()
device = dpt.empty(0, sycl_queue=q).__dlpack_device__()
mt = make_managed_tensor(
make_dl_tensor(shape, (kDLInt, 32, 1), device, strides=strides)
)

y = _dlp.from_dlpack_capsule(make_dlpack_capsule(mt))
assert y.shape == shape
# the other extents of an empty tensor take no memory
assert y.usm_data.nbytes == y.itemsize


@pytest.mark.parametrize("shape", [(0, 3), (2, 0, 4)])
def test_from_dlpack_empty_nd_memory(shape):
q = get_queue_or_skip()
x = dpt.empty(shape, dtype="i4", sycl_queue=q)

y = dpt.from_dlpack(x)
assert y.shape == shape
assert y.usm_data.nbytes == x.usm_data.nbytes


def test_from_dlpack_kwargs():
try:
x = dpt.arange(100, dtype="i4")
Expand Down
Loading