Skip to content

Size the memory of empty DLPack tensors on oneAPI devices as one element - #3203

Open
antonwolfy wants to merge 2 commits into
masterfrom
fix-dlpack-empty-tensor-extent
Open

antonwolfy wants to merge 2 commits into
masterfrom
fix-dlpack-empty-tensor-extent

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.

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

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 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.
@antonwolfy antonwolfy self-assigned this Nov 26, 2026
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Nov 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

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

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.555% (+0.002%) from 84.553% — fix-dlpack-empty-tensor-extent into master

@vlad-perevezentsev vlad-perevezentsev 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.

LGTM
Thank you @antonwolfy for sizing empty DLPack imports as one element

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.

3 participants