From 60b142c230ec22b7c3306fb6c9401f9ba5b18e1a Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 26 Nov 2026 18:31:51 +0100 Subject: [PATCH 1/2] Size the memory of empty DLPack tensors on oneAPI devices as one element This PR fixes the memory size that `dpnp.tensor.from_dlpack` and `dpnp.from_dlpack` give an empty DLPack tensor with more than one dimension on a oneAPI device. `from_dlpack_capsule` sizes the memory of a tensor by the span from its lowest to its highest element offset. A dimension of length 0 added nothing to that span, but the other dimensions still did, so an empty tensor of shape `(0, n)` was sized as `n` elements. With a NULL data pointer, which DLPack asks for in a tensor of size zero and which #3197 made importable, that size is what gets allocated for the result: 4 MiB for shape `(0, 2**20)` of `int32` and 8 GiB for `(0, 2**31)`. With a data pointer, the imported memory claimed more than the producer allocated: 4 MiB for an empty `dpnp.tensor` array of shape `(0, 2**20)`, whose buffer is 4 bytes. A tensor with a dimension of length 0 now spans no elements and gets one element, as `usm_ndarray` does for an empty shape. --- CHANGELOG.md | 1 + dpnp/tensor/_dlpack.pyx | 5 ++++ dpnp/tests/tensor/test_usm_ndarray_dlpack.py | 27 ++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24442cb0da7..ad2afc8ffea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 [#XXXX](https://github.com/IntelPython/dpnp/pull/XXXX) ### Security diff --git a/dpnp/tensor/_dlpack.pyx b/dpnp/tensor/_dlpack.pyx index 60acb7408e7..e6806cae66c 100644 --- a/dpnp/tensor/_dlpack.pyx +++ b/dpnp/tensor/_dlpack.pyx @@ -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: diff --git a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py index 9922c984ea9..71d84a7fc08 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py +++ b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py @@ -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") From f3dd4d073697c562ba4dc2fc47a5439e22e6cf00 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Thu, 26 Nov 2026 18:33:49 +0100 Subject: [PATCH 2/2] Link the changelog entry to the PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad2afc8ffea..2c9c5265b26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,7 +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 [#XXXX](https://github.com/IntelPython/dpnp/pull/XXXX) +* 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