diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e14b0d2df6..08ddc567b3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi * 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 a 0-d or non-C-contiguous host array to a SYCL device, unless its items are one byte [#3207](https://github.com/IntelPython/dpnp/pull/3207) +* 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 diff --git a/dpnp/tensor/_dlpack.pyx b/dpnp/tensor/_dlpack.pyx index 6f27a561f20..31a68774628 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 bb5a49b3708..7339248e6c5 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_dlpack.py +++ b/dpnp/tests/tensor/test_usm_ndarray_dlpack.py @@ -946,6 +946,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"