Skip to content

ENH: Set DLPack tensor shape and strides to NULL iff ndim == 0.#29872

Merged
seberg merged 1 commit intonumpy:mainfrom
hpkfft:dlpack
Oct 9, 2025
Merged

ENH: Set DLPack tensor shape and strides to NULL iff ndim == 0.#29872
seberg merged 1 commit intonumpy:mainfrom
hpkfft:dlpack

Conversation

@hpkfft
Copy link
Contributor

@hpkfft hpkfft commented Oct 3, 2025

DLPack allows, but does not require, DLTensor.strides to be NULL if PyArray_IS_C_CONTIGUOUS.
Previously, in this case, NumPy set the strides pointer to NULL.
Now, NumPy will supply a valid pointer to correctly initialized strides when ndim > 0.

This change can improve performance for downstream users. For example, nanobind can be used to import a NumPy array using the DLPack protocol to make the array available to C++ code. If DLTensor.strides is NULL, nanobind allocates and initializes a strides array. This redundant work is now avoided since NumPy provides a pointer to the strides, which it allocated and initialized anyway.

If ndim == 0, this PR sets both shape and strides to NULL.
Previously, strides was was set to NULL (since PyArray_IS_C_CONTIGUOUS), but shape pointed past the end of allocated memory. For security and debugability, NULL is preferred.

@seberg
Copy link
Member

seberg commented Oct 6, 2025

I don't care either way about NULLing, the important change here is: dmlc/dlpack#177

This is useful for you and since it was brought up by Tianqi, I guess we might as well. I'll note that NULL is actually convenient for consumers who expect a contiguous array, since they can skip any further checks.

Anyway, looks fine, a small test would be nice, though. But I'll wait for the decision there, at least a bit, so ping again if we forget about it.

@seberg
Copy link
Member

seberg commented Oct 9, 2025

OK, I'll just put it in, thanks @hpkfft. Basically, I am doubting the discussion there changes. And even if it does, this doesn't actually hurt, since I doubt other exporters add this logic to try and be helpful (it just doesn't help either).

@seberg seberg merged commit 5fe21fb into numpy:main Oct 9, 2025
78 checks passed
@hpkfft hpkfft deleted the dlpack branch October 12, 2025 19:44
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Oct 13, 2025
…numpy#29872)

This assumes a slight shift in the dlpack interpretation:

dmlc/dlpack#177

---

DLPack allows, but does not require, DLTensor.strides to be NULL if PyArray_IS_C_CONTIGUOUS.
Previously, in this case, NumPy set the strides pointer to NULL.
Now, NumPy will supply a valid pointer to correctly initialized strides when ndim > 0.

This change can improve performance for downstream users. For example, nanobind can be used to import a NumPy array using the DLPack protocol to make the array available to C++ code. If DLTensor.strides is NULL, nanobind allocates and initializes a strides array. This redundant work is now avoided since NumPy provides a pointer to the strides, which it allocated and initialized anyway.

If ndim == 0, this PR sets both shape and strides to NULL.
Previously, strides was was set to NULL (since PyArray_IS_C_CONTIGUOUS), but shape pointed past the end of allocated memory. For security and debugability, NULL is preferred.
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
…numpy#29872)

This assumes a slight shift in the dlpack interpretation:

dmlc/dlpack#177

---

DLPack allows, but does not require, DLTensor.strides to be NULL if PyArray_IS_C_CONTIGUOUS.
Previously, in this case, NumPy set the strides pointer to NULL.
Now, NumPy will supply a valid pointer to correctly initialized strides when ndim > 0.

This change can improve performance for downstream users. For example, nanobind can be used to import a NumPy array using the DLPack protocol to make the array available to C++ code. If DLTensor.strides is NULL, nanobind allocates and initializes a strides array. This redundant work is now avoided since NumPy provides a pointer to the strides, which it allocated and initialized anyway.

If ndim == 0, this PR sets both shape and strides to NULL.
Previously, strides was was set to NULL (since PyArray_IS_C_CONTIGUOUS), but shape pointed past the end of allocated memory. For security and debugability, NULL is preferred.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants