Skip to content

Fix copying empty host arrays to a device in from_dlpack - #3208

Merged
antonwolfy merged 4 commits into
masterfrom
dlpack-size0-host-copy
Dec 4, 2026
Merged

antonwolfy merged 4 commits into
masterfrom
dlpack-size0-host-copy

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes a ValueError in dpnp.from_dlpack and dpnp.tensor.from_dlpack when copying an empty host array to a SYCL device.

When the producer cannot export to the requested oneAPI device, as NumPy cannot, from_dlpack imports the array on the host and copies it to the device with _to_usm_ary_from_host_blob. For an empty array, that helper asked MemoryUSMDevice for 0 bytes, which raises ValueError: Number of bytes of request allocation must be positive., and copy_from_host rejects an empty buffer as well. The helper now returns an empty usm_ndarray allocated on the target queue, as dpnp.tensor.empty does, and copies nothing.

test_copy_via_host_size_0 copies 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 the ValueError.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

`_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.
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3+81.g3d46ba263 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

coveralls commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

No base build to compare — dlpack-size0-host-copy into master

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

antonwolfy and others added 2 commits December 4, 2026 12:13
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.
@antonwolfy
antonwolfy merged commit bfad54b into master Dec 4, 2026
42 checks passed
@antonwolfy
antonwolfy deleted the dlpack-size0-host-copy branch December 4, 2026 19:49
github-actions Bot added a commit that referenced this pull request Dec 4, 2026
This PR fixes a `ValueError` in `dpnp.from_dlpack` and
`dpnp.tensor.from_dlpack` when copying an empty host array to a SYCL
device.

When the producer cannot export to the requested oneAPI device, as NumPy
cannot, `from_dlpack` imports the array on the host and copies it to the
device with `_to_usm_ary_from_host_blob`. For an empty array, that
helper asked `MemoryUSMDevice` for 0 bytes, which raises `ValueError:
Number of bytes of request allocation must be positive.`, and
`copy_from_host` rejects an empty buffer as well. The helper now returns
an empty `usm_ndarray` allocated on the target queue, as
`dpnp.tensor.empty` does, and copies nothing.

`test_copy_via_host_size_0` copies 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 the `ValueError`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants