Skip to content

Fix read-only 0-d exports and NULL deleters in DLPack - #3181

Open
ndgrigorian wants to merge 2 commits into
masterfrom
fix-dlpack-bugs
Open

ndgrigorian wants to merge 2 commits into
masterfrom
fix-dlpack-bugs

Conversation

@ndgrigorian

Copy link
Copy Markdown
Collaborator

This PR fixes two bugs in the DLPack implementation of dpnp.tensor, found while adding the DLPack C exchange API.

to_dlpack_versioned_capsule read the array flags only for arrays with ndim > 0, 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 dpnp.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 deleter of DLManagedTensor and DLManagedTensorVersioned to 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_ctypes test helper. Without the fix, the 0-d tests fail and the NULL-deleter tests segfault. The DLPack tests in dpnp/tests/tensor/test_usm_ndarray_dlpack.py and dpnp/tests/test_dlpack.py pass on a Level Zero iGPU and the OpenCL CPU device, and the full dpnp.tests.tensor suite on the iGPU.

  • 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_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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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)
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.397%. remained the same — fix-dlpack-bugs into master

assert not y.flags.writable


def test_from_dlpack_0d_view_is_writable():

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 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.writable

@antonwolfy antonwolfy Nov 9, 2026 •

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.

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 branch has not been deployed

No deployments
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.

5 participants