From 4c3f730058e87b0dfe7343d24a863ad95e23a31a Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 30 Nov 2026 13:21:08 +0100 Subject: [PATCH 1/2] Cast fp64 host arrays before copying them to a device without fp64 in `from_dlpack` On a device without fp64 support, `_to_usm_ary_from_host_blob` made the result `float32` or `complex64` for a `float64` or `complex128` host array, but copied the host bytes into it unchanged, so `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` returned wrong values. The host array is now cast to the result's data type before the copy, as `dpnp.tensor.asarray` does. `test_copy_via_host_fp64` copies `float64` and `complex128` host arrays to the device and checks the data type and values of the result. --- dpnp/tensor/_dlpack.pyx | 2 ++ dpnp/tests/tensor/test_usm_ndarray_dlpack.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/dpnp/tensor/_dlpack.pyx b/dpnp/tensor/_dlpack.pyx index 4ee3c5bfd48..4265ee9010d 100644 --- a/dpnp/tensor/_dlpack.pyx +++ b/dpnp/tensor/_dlpack.pyx @@ -952,6 +952,8 @@ cdef usm_ndarray _to_usm_ary_from_host_blob(object host_blob, dev : Device): Xusm_dtype = ( "float32" if dt.char == "d" else "complex64" ) + # the bytes are copied as is, so cast on the host first + np_ary = np_ary.astype(Xusm_dtype) else: Xusm_dtype = dt usm_mem = dpmem.MemoryUSMDevice(np_ary.nbytes, queue=q) diff --git a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py index 83e4ffde142..87fcc99b27e 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py +++ b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py @@ -928,6 +928,23 @@ def test_copy_via_host_gh_1789(): dpt.from_dlpack(x_np, device=(14, 0)) +@pytest.mark.parametrize( + "x_np", + [np.array([1.5, -2.25, 3.0]), np.array([1.5 - 2j, 0.25j])], + ids=["float64", "complex128"], +) +def test_copy_via_host_fp64(x_np): + q = get_queue_or_skip() + + y = dpt.from_dlpack(x_np, device=q.sycl_device) + exp_dt = x_np.dtype + if 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 y.dtype == exp_dt + assert np.array_equal(dpt.asnumpy(y), x_np) + + class LegacyContainer: "Helper class implementing legacy `__dlpack__` protocol" From 5a59b3739cd74e9e6a8d599e889dde543e1e2782 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 30 Nov 2026 13:22:14 +0100 Subject: [PATCH 2/2] Add changelog entry for #3209 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4811a3bc76..267496a9b99 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 wrong values from `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` when copying a `float64` or `complex128` host array to a SYCL device without fp64 support, which copied its bytes unchanged into the `float32` or `complex64` result [#3209](https://github.com/IntelPython/dpnp/pull/3209) ### Security