Skip to content

Fix copying 0-d and non-contiguous host arrays to a device in from_dlpack - #3207

Merged
antonwolfy merged 2 commits into
masterfrom
dlpack-0d-host-round-trip
Nov 30, 2026
Merged

antonwolfy merged 2 commits into
masterfrom
dlpack-0d-host-round-trip

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 a 0-d or non-C-contiguous 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. That helper took a uint8 view of the host array before flattening it, and NumPy refuses a view to a smaller itemsize on a 0-d array (Changing the dtype of a 0d array is only supported if the itemsize is unchanged) or on an array whose last axis is not contiguous (To change to a dtype of a different size, the last axis must be contiguous). Only arrays with one-byte items, such as bool, got through. The array is now made C-contiguous, and at least 1-d, before the view.

The 0-d case is the round trip scikit-learn makes for Array API inputs: it moves an array to the host with numpy.from_dlpack(x, device="cpu") and back with xp.from_dlpack(y, device=device). The first direction already worked.

test_ndim0_host_round_trip makes both calls for a 0-d array of every dtype, and test_copy_via_host_layouts copies 0-d, strided, reversed and F-contiguous host arrays to the device with dpnp.tensor.from_dlpack. Without the fix, every case of both tests fails except bool.

  • 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?

…lpack`

`_to_usm_ary_from_host_blob` took a `uint8` view of the host array before flattening it, and NumPy refuses a view to a smaller itemsize on a 0-d array or on an array whose last axis is not contiguous. So `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` raised `ValueError` when they copied such a host array to a SYCL device, unless its items are one byte. The array is now made C-contiguous and at least 1-d before the view.

`test_ndim0_host_round_trip` moves a 0-d array of every dtype to the host with `numpy.from_dlpack(x, device="cpu")` and back with `dpnp.from_dlpack(y, device=x.device)`. `test_copy_via_host_layouts` copies 0-d, strided, reversed and F-contiguous host arrays 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

Copy link
Copy Markdown
Contributor

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

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.559% (+0.003%) from 84.556% — dlpack-0d-host-round-trip into master

@antonwolfy
antonwolfy merged commit 4c83417 into master Nov 30, 2026
45 checks passed
@antonwolfy
antonwolfy deleted the dlpack-0d-host-round-trip branch November 30, 2026 17:08
github-actions Bot added a commit that referenced this pull request Nov 30, 2026
…lpack` (#3207)

This PR fixes a `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.

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`. That helper took a `uint8`
view of the host array before flattening it, and NumPy refuses a view to
a smaller itemsize on a 0-d array (`Changing the dtype of a 0d array is
only supported if the itemsize is unchanged`) or on an array whose last
axis is not contiguous (`To change to a dtype of a different size, the
last axis must be contiguous`). Only arrays with one-byte items, such as
`bool`, got through. The array is now made C-contiguous, and at least
1-d, before the view.

The 0-d case is the round trip scikit-learn makes for Array API inputs:
it moves an array to the host with `numpy.from_dlpack(x, device="cpu")`
and back with `xp.from_dlpack(y, device=device)`. The first direction
already worked.

`test_ndim0_host_round_trip` makes both calls for a 0-d array of every
dtype, and `test_copy_via_host_layouts` copies 0-d, strided, reversed
and F-contiguous host arrays to the device with
`dpnp.tensor.from_dlpack`. Without the fix, every case of both tests
fails except `bool`.

- [x] Have you provided a meaningful PR description?
- [x] Have you added a test, reproducer or referred to an issue with a
reproducer?
- [x] Have you tested your changes locally for CPU and GPU devices?
- [x] 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?
- [x] Have you added your changes to the changelog?
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.

3 participants