Support `kDLCPU` DLDeviceType for `from_dlpack` and `__dlpack__`
This PR proposes supporting the kDLCPU DLDeviceType as a targeted device in from_dlpack and __dlpack__.
This is accomplished by __dlpack__ by copying the array data to the host, wrapping it in a NumPy array, and then returning a capsule for this NumPy array.
In from_dlpack, a NumPy array is also returned for devices with __dlpack_device__= (DLDeviceType.kDLCPU, 0). This means that from_dlpack(x_np) for some NumPy array x_np is effectively a no-op with extra validation steps returning a new Python object wrapping the same memory with zero-copy.
New utility functions numpy_to_dlpack_versioned_capsule, _numpy_array_interface_from_dl_tensor, and the class _numpy_array_interface_wrapper are introduced to enable the new functionality.
This PR also refactors from_dlpack_versioned_capsule into from_dlpack_capsule.
- [x] Have you provided a meaningful PR description?
- [ ] 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?
- [x] If this PR is a work in progress, are you opening the PR as a draft?
Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. :crossed_fingers:
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_239 ran successfully. Passed: 894 Failed: 1 Skipped: 119
coverage: 87.883% (-0.05%) from 87.93% when pulling 9a17afcaf0097e5d3a9bd2d2e3e86f3dcc13bfe9 on feature/dlpack-kdlcpu-support into ff0d4ea434b50ac865c12400c64e10975fdc3e69 on master.
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_240 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_244 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_248 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_249 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_261 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_268 ran successfully. Passed: 894 Failed: 1 Skipped: 119
I think we must raise an exception on attempt to import np.ndarray instance to device=(kDLoneAPI, num), or support it properly. At the moment, we silently return value the np.ndarray back:
In [1]: import dpctl.tensor as dpt, numpy as np
In [2]: x = dpt.ones((2, 3))
In [3]: y = dpt.from_dlpack(x, device=(1,0))
In [4]: type(y)
Out[4]: numpy.ndarray
In [5]: z = dpt.from_dlpack(y)
In [6]: type(z)
Out[6]: numpy.ndarray
In [7]: dpt.from_dlpack(z, device=x.__dlpack_device__())
Out[7]:
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)
In [8]: type(_)
Out[8]: numpy.ndarray
In [9]: x.__dlpack_device__()
Out[9]: (<DLDeviceType.kDLOneAPI: 14>, 2)
I also think we ought to make DLDeviceType enum documented and accessible in a public namespace. I think dpctl.tensor is appropriate.
Actually, we must support importing dlpack capsules with kDLCPU to allow copying from an arbitrary importer (that supports exporting to "kDLCPU") via host.
I also think we ought to make
DLDeviceTypeenum documented and accessible in a public namespace. I thinkdpctl.tensoris appropriate.
In working on this, I realized, a page with DLDeviceType enum documented would be the perfect place for other constants (dpt.pi, dpt.e, etc.)
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_281 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_287 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_292 ran successfully. Passed: 894 Failed: 1 Skipped: 119
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_303 ran successfully. Passed: 895 Failed: 0 Skipped: 119