-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support kDLCPU
DLDeviceType for from_dlpack
and __dlpack__
#1781
Conversation
Leverages NumPy to create an array with Python interpreter/host-accessible memory, which is required by the 2023.12 array API specification
Refactors `from_dlpack_versioned_capsule` into `from_dlpack_capsule` `_numpy_array_interface_from_dl_tensor` now takes a pointer to a DLTensor to guarantee no copy is made
When using cimport with Numpy, a warning for a deprecated C-API is thrown NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION is now defined for targets in `build_dpctl_ext` to address this warning
Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞 |
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_239 ran successfully. |
Instead cdef from `numpy/npy_no_deprecated_api.h`
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_240 ran successfully. |
_is_host_cpu(dl_device) checks if user request export for host CPU device. Recognized inputs are (1, 0) (_usmarray.DLDeviceType.kDLCPU, 0) ("kDLCPU", 0) Add test to exercise __dlpack__ with non-default dl_device keyword arguments
Introduce _is_host_cpu utility predicate used in __dlpack__
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_244 ran successfully. |
…tensor if NumPy array is C-contiguous
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_248 ran successfully. |
5847b70
to
1544e9b
Compare
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_249 ran successfully. |
Avoids possible out-of-bounds access by short-circuiting the if statement
250f9a9
to
70c6772
Compare
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_261 ran successfully. |
Passing the used Python capsule to `_numpy_array_interface_wrapper` would cause premature deallocation of the newly created DLPack owner object, causing the dl_tensor to be prematurely deleted Now pass the new owner object instead
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_268 ran successfully. |
I think we must raise an exception on attempt to import
I also think we ought to make |
Actually, we must support importing dlpack capsules with |
In working on this, I realized, a page with |
…ants Expose `DLDeviceType` enumerator in `dpctl.tensor`
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_281 ran successfully. |
Simplifies check for device_type = kDLCPU and device_id = 0
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_287 ran successfully. |
Better fits Cython style by using array-index-access to explicitly dereference pointer rather than relying on Cython to implicitly dereference it in code generation
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_292 ran successfully. |
For arr that supports DLPack, version (1, 0), or legacy, support ``` from_dlpack(arr, device=target_dev) ``` where target_dev is `(kDLCPU, 0)` for transfer to host, or a value recognized by device keywords in dpctl.tensor for other functions, or `(kDLOneAPI, dev_id)`. To support transfer via host, `arr` must support `__dlpack__(max_version=(1,0), dl_device=(1, 0))`. For array objects with legacy `__dlpack__` support only, supported inputs are those residing on kDLCPU device, or those from kDLOneAPI device only. --- This is a combination of 17 commits squashed into one: Combine two validation checks into one, improving coverage Only fall-back to __dlpack__() if requested device does not change Simplify branching, only fall-back to no-arg call to __dlpack__ is dl_device is None or same as reported for the input Changed from_dlpack to copy via host is needed This enables dpt.from_dlpack(numpy_array, device="opencl:cpu") Add a test to exercise copy via host Handle possibilities for TypeError and BufferError These may be hard to test Change exception raised by __dlpack__ if dl_device is unsupported It used to raise NotImplementedError, not raises BufferError Add case of dlpack test to expand coverage Removed comment, add NotImplementedError to the except clause To ensure same validation across branches, compute host_blob by roundtripping it through dlpack Test from_dlpack on numpy input with strides not multiple of elementsize Refined from_dlpack docstrings, reorged impl of from_dlpack Used try/except/else/finally to avoid raising an exception when another one is in flight (confusing UX). device keyword is only allowed to be (kDLCPU, 0) or (kDLOneAPI, num). Device keyword value is used to create output array, rather than device_id deduced from it. Adjusted test per change in implementation Expand applicability of fall-back behavior When `from_dlpack(arr, device=dev)` is called, for `arr` object that supports legacy DLPack interface (max_version, dl_device, copy are not supported), we now support arr being device on host, that is (kDLCPU, 0), and (kDLOneAPI, different_device_id). Support for this last case is being added in this commit, as per review comment. Add symmetric support for containers with legacy DLPack support For legacy containers, support device=(kDLCPU, 0) as well as oneAPI device. Add tests for importing generic legacy and generic modern containers Fix typos in comments Add test for legacy container holding numpy's array.
Enhance from_dlpack to support imported kDLCPU data to kDLOneAPI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @ndgrigorian for working on this. It looks good to me, so I am approving.
I am going to ping @seberg to bring to his attention code for producing/consuming DLManagedTensorVersioned
capsule in dpctl/tensor/_dlpack.pyx
.
We merge the PR though and address his feedback, if any, later.
Array API standard conformance tests for dpctl=0.18.0dev0=py310ha798474_303 ran successfully. |
This PR proposes supporting the
kDLCPU
DLDeviceType as a targeted device infrom_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 thatfrom_dlpack(x_np)
for some NumPy arrayx_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
intofrom_dlpack_capsule
.