Skip to content

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

Merged
ndgrigorian merged 3 commits into
masterfrom
fix-dlpack-bugs
Nov 23, 2026
Merged

ndgrigorian merged 3 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?

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

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

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

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

@antonwolfy antonwolfy left a comment

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.

Thank you @ndgrigorian, LGTM

@ndgrigorian
ndgrigorian merged commit b7db962 into master Nov 23, 2026
46 checks passed
@ndgrigorian
ndgrigorian deleted the fix-dlpack-bugs branch November 23, 2026 15:38
github-actions Bot added a commit that referenced this pull request Nov 23, 2026
Fix read-only 0-d exports and NULL deleters in DLPack
antonwolfy added a commit that referenced this pull request Nov 24, 2026
This PR fixes a `TypeError` in `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` when importing a DLPack tensor on a oneAPI device
whose data pointer is NULL.

For such a tensor, `from_dlpack_capsule` allocates a new buffer with
`dpmem.MemoryUSMDevice(sz, q)`, but `MemoryUSMDevice` takes the queue
only as a keyword, so the import raised `TypeError: __cinit__() takes
exactly 1 positional argument (2 given)`. DLPack asks for a NULL data
pointer in a tensor of size zero, so importing an empty tensor from a
producer that follows it failed. The queue is now passed by keyword, as
`_to_usm_ary_from_host_blob` in the same file already does.

dpnp's own capsules always point to an allocation, so only tensors from
other producers reach this. The `kDLCPU` branch goes through NumPy and
was not affected.

The test builds such tensors with the `_dlpack_ctypes` helper added in
#3181, so this PR is stacked on #3181 and targets its branch; it is to
be retargeted to `master` once #3181 is merged. The test checks that the
import returns an empty array on the producer's device and that the
deleter is called once. Without the fix, both cases of the new test fail
with the `TypeError`.
github-actions Bot added a commit that referenced this pull request Nov 24, 2026
This PR fixes a `TypeError` in `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` when importing a DLPack tensor on a oneAPI device
whose data pointer is NULL.

For such a tensor, `from_dlpack_capsule` allocates a new buffer with
`dpmem.MemoryUSMDevice(sz, q)`, but `MemoryUSMDevice` takes the queue
only as a keyword, so the import raised `TypeError: __cinit__() takes
exactly 1 positional argument (2 given)`. DLPack asks for a NULL data
pointer in a tensor of size zero, so importing an empty tensor from a
producer that follows it failed. The queue is now passed by keyword, as
`_to_usm_ary_from_host_blob` in the same file already does.

dpnp's own capsules always point to an allocation, so only tensors from
other producers reach this. The `kDLCPU` branch goes through NumPy and
was not affected.

The test builds such tensors with the `_dlpack_ctypes` helper added in
#3181, so this PR is stacked on #3181 and targets its branch; it is to
be retargeted to `master` once #3181 is merged. The test checks that the
import returns an empty array on the producer's device and that the
deleter is called once. Without the fix, both cases of the new test fail
with the `TypeError`.
antonwolfy added a commit that referenced this pull request Nov 27, 2026
This PR fixes a segfault in `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` when importing a DLPack tensor on a oneAPI device
with NULL strides.

To find the extent of the memory a `kDLOneAPI` tensor views,
`from_dlpack_capsule` read `strides[i]` for every dimension without
checking for NULL. DLPack before v1.2 allows `strides` to be NULL to
mean a C-contiguous tensor, and the `kDLCPU` branch already handles
that. The extent is now computed from C-contiguous strides when
`strides` is NULL. The resulting array was already built with
C-contiguous strides in that case.

dpnp's own capsules always set strides, so only tensors from other
producers, through a legacy `DLManagedTensor` or a versioned tensor
older than 1.2, reach this.

The test builds such tensors with the `_dlpack_ctypes` helper added in
#3181, so this PR is stacked on #3181 and targets its branch; it is to
be retargeted to `master` once #3181 is merged. Without the fix, every
case of the new test segfaults, and computing the extent with a stride
of 1 instead of the C-contiguous strides fails the 2-D case. 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.
github-actions Bot added a commit that referenced this pull request Nov 28, 2026
This PR fixes a segfault in `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` when importing a DLPack tensor on a oneAPI device
with NULL strides.

To find the extent of the memory a `kDLOneAPI` tensor views,
`from_dlpack_capsule` read `strides[i]` for every dimension without
checking for NULL. DLPack before v1.2 allows `strides` to be NULL to
mean a C-contiguous tensor, and the `kDLCPU` branch already handles
that. The extent is now computed from C-contiguous strides when
`strides` is NULL. The resulting array was already built with
C-contiguous strides in that case.

dpnp's own capsules always set strides, so only tensors from other
producers, through a legacy `DLManagedTensor` or a versioned tensor
older than 1.2, reach this.

The test builds such tensors with the `_dlpack_ctypes` helper added in
#3181, so this PR is stacked on #3181 and targets its branch; it is to
be retargeted to `master` once #3181 is merged. Without the fix, every
case of the new test segfaults, and computing the extent with a stride
of 1 instead of the C-contiguous strides fails the 2-D case. 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.
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