From 8cd66b1fd97d51f006a8ef9083e2648f32ae794e Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 30 Nov 2026 13:15:02 +0100 Subject: [PATCH 1/3] Fix copying empty host arrays to a device in `from_dlpack` `_to_usm_ary_from_host_blob` asked `MemoryUSMDevice` for 0 bytes for an empty host array, which raises `ValueError`, and `copy_from_host` rejects an empty buffer as well. So `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` failed to copy an empty host array to a SYCL device. The helper now returns an empty `usm_ndarray` allocated on the target queue. `test_copy_via_host_size_0` copies empty host arrays of shapes `(0,)`, `(0, 3)` and `(3, 0)` to the device. --- dpnp/tensor/_dlpack.pyx | 8 ++++++++ dpnp/tests/tensor/test_usm_ndarray_dlpack.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/dpnp/tensor/_dlpack.pyx b/dpnp/tensor/_dlpack.pyx index 4ee3c5bfd48..13dc5d17bb2 100644 --- a/dpnp/tensor/_dlpack.pyx +++ b/dpnp/tensor/_dlpack.pyx @@ -954,6 +954,14 @@ cdef usm_ndarray _to_usm_ary_from_host_blob(object host_blob, dev : Device): ) else: Xusm_dtype = dt + if np_ary.size == 0: + # MemoryUSMDevice and copy_from_host reject zero bytes + return usm_ndarray( + np_ary.shape, + dtype=Xusm_dtype, + buffer="device", + buffer_ctor_kwargs={"queue": q}, + ) usm_mem = dpmem.MemoryUSMDevice(np_ary.nbytes, queue=q) usm_ary = usm_ndarray(np_ary.shape, dtype=Xusm_dtype, buffer=usm_mem) usm_mem.copy_from_host(np.reshape(np_ary.view(dtype="u1"), -1)) diff --git a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py index 83e4ffde142..f7f1f7aa043 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py +++ b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py @@ -889,6 +889,19 @@ def test_dlpack_size_0_on_kdlcpu(): assert y.ctypes.data == x_np.ctypes.data +@pytest.mark.parametrize("shape", [(0,), (0, 3), (3, 0)]) +def test_copy_via_host_size_0(shape): + q = get_queue_or_skip() + x_np = np.ones(shape, dtype="i4") + + y = dpt.from_dlpack(x_np, device=q.sycl_device) + assert isinstance(y, dpt.usm_ndarray) + assert y.shape == shape + assert y.dtype == x_np.dtype + assert y.usm_type == "device" + assert y.sycl_device == q.sycl_device + + def test_copy_via_host(): get_queue_or_skip() x = dpt.ones(1, dtype="i4") From da4cc776f607d4aa75750bf60237643f7677a299 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 30 Nov 2026 13:16:07 +0100 Subject: [PATCH 2/3] Add changelog entry for #3208 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4811a3bc76..981eb9fb9b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi * 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) * Fixed a segfault in `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` when importing a DLPack tensor on a oneAPI device with NULL strides, which DLPack before 1.2 allows for a C-contiguous tensor [#3183](https://github.com/IntelPython/dpnp/pull/3183) +* Fixed `ValueError` in `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` when copying an empty host array to a SYCL device [#3208](https://github.com/IntelPython/dpnp/pull/3208) ### Security From 027ba99c02197adc95d3c1704cf0cab95743cd5a Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Fri, 4 Dec 2026 15:20:28 +0100 Subject: [PATCH 3/3] Test empty host copies of float64 and complex128 arrays Parametrize test_copy_via_host_size_0 over i4, f8 and c16 and expect float32 or complex64 on a device without fp64, as test_copy_via_host_fp64 does, so the dtype of the empty result is checked too. --- dpnp/tests/tensor/test_usm_ndarray_dlpack.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py index c3e8623ef8e..d5e5fccf70a 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py +++ b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py @@ -889,15 +889,20 @@ def test_dlpack_size_0_on_kdlcpu(): assert y.ctypes.data == x_np.ctypes.data +@pytest.mark.parametrize("dtype", ["i4", "f8", "c16"]) @pytest.mark.parametrize("shape", [(0,), (0, 3), (3, 0)]) -def test_copy_via_host_size_0(shape): +def test_copy_via_host_size_0(shape, dtype): q = get_queue_or_skip() - x_np = np.ones(shape, dtype="i4") + x_np = np.ones(shape, dtype=dtype) y = dpt.from_dlpack(x_np, device=q.sycl_device) + exp_dt = x_np.dtype + if exp_dt.kind in "fc" and not q.sycl_device.has_aspect_fp64: + # cast on the host, as dpnp.tensor.asarray does + exp_dt = np.float32 if exp_dt.kind == "f" else np.complex64 assert isinstance(y, dpt.usm_ndarray) assert y.shape == shape - assert y.dtype == x_np.dtype + assert y.dtype == exp_dt assert y.usm_type == "device" assert y.sycl_device == q.sycl_device