Repository navigation
Accept NULL data pointers in DLPack tensors on oneAPI devices - #3197
Merged
Merged
Conversation
`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
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
November 23, 2026 12:55
Contributor
Contributor
|
Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_58 ran successfully. |
Collaborator
ndgrigorian
approved these changes
Nov 23, 2026
ndgrigorian
left a comment
Collaborator
There was a problem hiding this comment.
This LGTM after a rebase, thank you @antonwolfy
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`.
5 of 7 tasks
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a
TypeErrorindpnp.tensor.from_dlpackanddpnp.from_dlpackwhen importing a DLPack tensor on a oneAPI device whose data pointer is NULL.For such a tensor,
from_dlpack_capsuleallocates a new buffer withdpmem.MemoryUSMDevice(sz, q), butMemoryUSMDevicetakes the queue only as a keyword, so the import raisedTypeError: __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_blobin the same file already does.dpnp's own capsules always point to an allocation, so only tensors from other producers reach this. The
kDLCPUbranch goes through NumPy and was not affected.The test builds such tensors with the
_dlpack_ctypeshelper added in #3181, so this PR is stacked on #3181 and targets its branch; it is to be retargeted tomasteronce #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 theTypeError.