Repository navigation
Fix read-only 0-d exports and NULL deleters in DLPack - #3181
ndgrigorian wants to merge 2 commits into
Conversation
`to_dlpack_versioned_capsule` read the array flags only inside the `nd > 0` branch, so a writable 0-d array was exported with `DLPACK_FLAG_BITMASK_READ_ONLY` set. `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` request a versioned capsule, so they returned a read-only view of a writable 0-d array. DLPack allows the deleter of `DLManagedTensor` and `DLManagedTensorVersioned` to be NULL when the producer has nothing to free. The owners that keep an imported tensor alive called it unconditionally, so freeing the imported array segfaulted. The tests build the NULL-deleter tensors with ctypes, through a new `_dlpack_ctypes` test helper.
There was a problem hiding this comment.
🟡 Changes recommended
The device tests can fail intermittently because the raw DLPack capsule lacks synchronization with the asynchronous fill; wait on the producing queue before passing the pointer to the capsule.
1 open issue
What changed in this PR
This PR fixes DLPack interoperability in dpnp.tensor, preserving writable scalar exports and safely releasing imported tensors with NULL deleters.
Changes:
- Reads array flags for versioned exports of 0-d arrays.
- Guards calls to legacy and versioned tensor deleters.
- Adds regression tests, ctypes-based test helpers, and changelog entries.
| File | Description |
|---|---|
dpnp/tests/tensor/test_usm_ndarray_dlpack.py |
Tests scalar writability and NULL-deleter imports. |
dpnp/tests/tensor/helper/_dlpack_ctypes.py |
Adds ctypes helpers for constructing test DLPack capsules. |
dpnp/tensor/_dlpack.pyx |
Fixes scalar export flags and guards NULL deleters. |
CHANGELOG.md |
Documents both DLPack fixes. |
Review effort: Balanced
| def test_from_dlpack_capsule_null_deleter(versioned, on_device): | ||
| if on_device: | ||
| q = get_queue_or_skip() | ||
| x = dpt.arange(4, dtype="i4", sycl_queue=q) |
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3181/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev1=np2py314h8d9cdd5_6 ran successfully. |
| assert not y.flags.writable | ||
|
|
||
|
|
||
| def test_from_dlpack_0d_view_is_writable(): |
There was a problem hiding this comment.
Nit: The CHANGELOG also names dpnp.from_dlpack, but only dpnp.tensor is tested. test_ndim0 in dpnp/tests/test_dlpack.py checks only the value; one more line there would cover the dpnp side and fails without this fix:
def test_ndim0(self):
x = dpnp.array(1.0)
y = dpnp.from_dlpack(x)
assert_array_equal(x, y)
assert y.flags.writableThere was a problem hiding this comment.
I think, as an exception, it's fine to leave that only on dpnp.tensor only tests, because the dpnp specific test would not cover something special here.

This PR fixes two bugs in the DLPack implementation of
dpnp.tensor, found while adding the DLPack C exchange API.to_dlpack_versioned_capsuleread the array flags only for arrays withndim > 0, so a writable 0-d array was exported withDLPACK_FLAG_BITMASK_READ_ONLYset.dpnp.from_dlpackanddpnp.tensor.from_dlpackrequest a versioned capsule, sodpnp.from_dlpack(dpnp.asarray(5))returned a read-only view of a writable array. The flags are now read for every array.DLPack allows the
deleterofDLManagedTensorandDLManagedTensorVersionedto be NULL when the producer has nothing to free, but the owners that keep an imported tensor alive called it unconditionally, so freeing an array imported from such a tensor segfaulted. A NULL deleter is now skipped. dpnp's own capsules always set a deleter, so only tensors from other producers reach this.The tests build the NULL-deleter tensors with ctypes, through a new
_dlpack_ctypestest helper. Without the fix, the 0-d tests fail and the NULL-deleter tests segfault. The DLPack tests indpnp/tests/tensor/test_usm_ndarray_dlpack.pyanddpnp/tests/test_dlpack.pypass on a Level Zero iGPU and the OpenCL CPU device, and the fulldpnp.tests.tensorsuite on the iGPU.