diff --git a/CHANGELOG.md b/CHANGELOG.md index 24442cb0da7..2c9c5265b26 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 [#3203](https://github.com/IntelPython/dpnp/pull/3203) ### 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")