Repository navigation
Size the memory of empty DLPack tensors on oneAPI devices as one element - #3203
Open
antonwolfy wants to merge 2 commits into
Open
antonwolfy wants to merge 2 commits into
antonwolfy wants to merge 2 commits into
Conversation
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.
antonwolfy
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
November 26, 2026 17:33
antonwolfy
added a commit
that referenced
this pull request
Nov 26, 2026
CuPy fixed the memory size of a tensor imported from DLPack, which added up the shape extents instead of multiplying them, and added a test for it. This mirrors the test in `core_tests/test_dlpack.py`. The new `TestDLTensorMemory::test_memory_size` checks `out.data.size`, since dpnp's `data` is the USM memory itself, against `max(nbytes, itemsize)`, since dpnp allocates at least one element for an empty array. dpnp multiplies the extents, but its empty case `(0, 3)` fails until #3203 is merged, because the import sized an empty tensor by its nonzero dimensions.
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3203/index.html |
Contributor
|
Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_58 ran successfully. |
Collaborator
vlad-perevezentsev
approved these changes
Nov 27, 2026
vlad-perevezentsev
left a comment
Contributor
There was a problem hiding this comment.
LGTM
Thank you @antonwolfy for sizing empty DLPack imports as one element
This branch has not been deployed
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 the memory size that
dpnp.tensor.from_dlpackanddpnp.from_dlpackgive an empty DLPack tensor with more than one dimension on a oneAPI device.from_dlpack_capsulesizes 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 asnelements. 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)ofint32and 8 GiB for(0, 2**31). With a data pointer, the imported memory claimed more than the producer allocated: 4 MiB for an emptydpnp.tensorarray 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, asusm_ndarraydoes 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.tensorarray 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.