Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG, ENH: np._from_dlpack: export correct device information #21138

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions numpy/core/src/multiarray/dlpack.c
Expand Up @@ -88,6 +88,12 @@ array_get_dl_device(PyArrayObject *self) {
ret.device_type = kDLCPU;
ret.device_id = 0;
PyObject *base = PyArray_BASE(self);

// walk the bases (see gh-20340)
while (base != NULL && PyArray_Check(base)) {
base = PyArray_BASE((PyArrayObject *)base);
}

// The outer if is due to the fact that NumPy arrays are on the CPU
// by default (if not created from DLPack).
if (PyCapsule_IsValid(base, NPY_DLPACK_INTERNAL_CAPSULE_NAME)) {
Expand Down
5 changes: 4 additions & 1 deletion numpy/core/tests/test_dlpack.py
Expand Up @@ -91,7 +91,10 @@ def test_higher_dims(self, ndim):
def test_dlpack_device(self):
x = np.arange(5)
assert x.__dlpack_device__() == (1, 0)
assert np._from_dlpack(x).__dlpack_device__() == (1, 0)
y = np._from_dlpack(x)
assert y.__dlpack_device__() == (1, 0)
z = y[::2]
assert z.__dlpack_device__() == (1, 0)

def dlpack_deleter_exception(self):
x = np.arange(5)
Expand Down