Skip to content

Accept NULL data pointers in DLPack tensors on oneAPI devices - #3197

Merged
antonwolfy merged 3 commits into
masterfrom
dlpack-null-data
Nov 24, 2026
Merged

antonwolfy merged 3 commits into
masterfrom
dlpack-null-data

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • 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?

`from_dlpack_capsule` allocated the buffer of a `kDLOneAPI` tensor whose data pointer is NULL with `dpmem.MemoryUSMDevice(sz, q)`, but `MemoryUSMDevice` takes the queue only as a keyword, so the import raised `TypeError`. 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.
@antonwolfy antonwolfy self-assigned this Nov 23, 2026
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.553% (+0.008%) from 84.545% — dlpack-null-data into master

Base automatically changed from fix-dlpack-bugs to master November 23, 2026 15:38

@ndgrigorian ndgrigorian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This LGTM after a rebase, thank you @antonwolfy

@antonwolfy
antonwolfy merged commit e3697fb into master Nov 24, 2026
45 checks passed
@antonwolfy
antonwolfy deleted the dlpack-null-data branch November 24, 2026 08:35
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
…ent (#3203)

This PR fixes the memory size that `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` give an empty DLPack tensor with more than one
dimension on a oneAPI device.

`from_dlpack_capsule` sizes the memory of a tensor by the span from its
lowest to its highest element offset. A dimension of length 0 added
nothing to that span, but the other dimensions still did, so an empty
tensor of shape `(0, n)` was sized as `n` elements. With a NULL data
pointer, which DLPack asks for in a tensor of size zero and which #3197
made importable, that size is what gets allocated for the result: 4 MiB
for shape `(0, 2**20)` of `int32` and 8 GiB for `(0, 2**31)`. With a
data pointer, the imported memory claimed more than the producer
allocated: 4 MiB for an empty `dpnp.tensor` array of shape `(0, 2**20)`,
whose buffer is 4 bytes. A tensor with a dimension of length 0 now spans
no elements and gets one element, as `usm_ndarray` does for an empty
shape.

Two tests are added. Importing an empty tensor with a NULL data pointer
must allocate one element, and importing an empty `dpnp.tensor` array
must not claim more memory than the array's buffer. Without the fix, all
five cases fail.

#3183 changes the same loop to accept NULL strides, so whichever of the
two PRs lands second needs a small rebase.

- [x] Have you provided a meaningful PR description?
- [x] 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?
- [ ] Have you added documentation for your changes, if necessary?
- [x] Have you added your changes to the changelog?
github-actions Bot added a commit that referenced this pull request Nov 27, 2026
…ent (#3203)

This PR fixes the memory size that `dpnp.tensor.from_dlpack` and
`dpnp.from_dlpack` give an empty DLPack tensor with more than one
dimension on a oneAPI device.

`from_dlpack_capsule` sizes the memory of a tensor by the span from its
lowest to its highest element offset. A dimension of length 0 added
nothing to that span, but the other dimensions still did, so an empty
tensor of shape `(0, n)` was sized as `n` elements. With a NULL data
pointer, which DLPack asks for in a tensor of size zero and which #3197
made importable, that size is what gets allocated for the result: 4 MiB
for shape `(0, 2**20)` of `int32` and 8 GiB for `(0, 2**31)`. With a
data pointer, the imported memory claimed more than the producer
allocated: 4 MiB for an empty `dpnp.tensor` array of shape `(0, 2**20)`,
whose buffer is 4 bytes. A tensor with a dimension of length 0 now spans
no elements and gets one element, as `usm_ndarray` does for an empty
shape.

Two tests are added. Importing an empty tensor with a NULL data pointer
must allocate one element, and importing an empty `dpnp.tensor` array
must not claim more memory than the array's buffer. Without the fix, all
five cases fail.

#3183 changes the same loop to accept NULL strides, so whichever of the
two PRs lands second needs a small rebase.

- [x] Have you provided a meaningful PR description?
- [x] 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?
- [ ] Have you added documentation for your changes, if necessary?
- [x] Have you added your changes to the changelog?
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.

3 participants