ENH: Set DLPack tensor shape and strides to NULL iff ndim == 0.#29872
Merged
seberg merged 1 commit intonumpy:mainfrom Oct 9, 2025
Merged
ENH: Set DLPack tensor shape and strides to NULL iff ndim == 0.#29872seberg merged 1 commit intonumpy:mainfrom
shape and strides to NULL iff ndim == 0.#29872seberg merged 1 commit intonumpy:mainfrom
Conversation
Member
|
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 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. |
Member
|
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). |
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.
8 tasks
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.
2 tasks
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.
DLPack allows, but does not require,
DLTensor.stridesto beNULLifPyArray_IS_C_CONTIGUOUS.Previously, in this case, NumPy set the
stridespointer toNULL.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.stridesisNULL, nanobind allocates and initializes astridesarray. This redundant work is now avoided since NumPy provides a pointer to thestrides, which it allocated and initialized anyway.If
ndim == 0, this PR sets bothshapeandstridestoNULL.Previously,
strideswas was set toNULL(sincePyArray_IS_C_CONTIGUOUS), butshapepointed past the end of allocated memory. For security and debugability,NULLis preferred.