Repository navigation
Fix copying empty host arrays to a device in from_dlpack - #3208
antonwolfy wants to merge 4 commits into
Conversation
`_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.
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3208/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev3+81.g3d46ba263 ran successfully. |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
Empty host arrays of every shape, dtype and order now come to the device, and the new test fails without the fix. One nit below. It needs a rebase on master: it conflicts in CHANGELOG.md.
| @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") |
There was a problem hiding this comment.
Nit: The test uses only i4, so the dtype=Xusm_dtype argument of the new branch is never checked: with dtype=dt there it still passes. Could it also take f8 and c16, with the expected dtype computed as in test_copy_via_host_fp64? Then a device without fp64 checks the fallback for empty arrays too.
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.
This PR fixes a
ValueErrorindpnp.from_dlpackanddpnp.tensor.from_dlpackwhen copying an empty host array to a SYCL device.When the producer cannot export to the requested oneAPI device, as NumPy cannot,
from_dlpackimports the array on the host and copies it to the device with_to_usm_ary_from_host_blob. For an empty array, that helper askedMemoryUSMDevicefor 0 bytes, which raisesValueError: Number of bytes of request allocation must be positive., andcopy_from_hostrejects an empty buffer as well. The helper now returns an emptyusm_ndarrayallocated on the target queue, asdpnp.tensor.emptydoes, and copies nothing.test_copy_via_host_size_0copies empty host arrays of shapes(0,),(0, 3)and(3, 0)to the device and checks the shape, data type, USM type and device of the result. Without the fix, all three cases fail with theValueError.