Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MAINT: from_dlpack thread safety fixes (#28883)#28889

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
charris merged 1 commit intonumpy:maintenance/2.2.xfromcharris:backport-28883
May 2, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletionsnumpy/_core/src/multiarray/dlpack.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -504,36 +504,12 @@ from_dlpack(PyObject *NPY_UNUSED(self),
return NULL;
}

/* Prepare the arguments to call objects __dlpack__() method */
static PyObject *call_kwnames = NULL;
static PyObject *dl_cpu_device_tuple = NULL;
static PyObject *max_version = NULL;

if (call_kwnames == NULL) {
call_kwnames = Py_BuildValue("(sss)", "dl_device", "copy", "max_version");
if (call_kwnames == NULL) {
return NULL;
}
}
if (dl_cpu_device_tuple == NULL) {
dl_cpu_device_tuple = Py_BuildValue("(i,i)", 1, 0);
if (dl_cpu_device_tuple == NULL) {
return NULL;
}
}
if (max_version == NULL) {
max_version = Py_BuildValue("(i,i)", 1, 0);
if (max_version == NULL) {
return NULL;
}
}

/*
* Prepare arguments for the full call. We always forward copy and pass
* our max_version. `device` is always passed as `None`, but if the user
* provided a device, we will replace it with the "cpu": (1, 0).
*/
PyObject *call_args[] = {obj, Py_None, copy,max_version};
PyObject *call_args[] = {obj, Py_None, copy,npy_static_pydata.dl_max_version};
Py_ssize_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;

/* If device is passed it must be "cpu" and replace it with (1, 0) */
Expand All@@ -544,12 +520,13 @@ from_dlpack(PyObject *NPY_UNUSED(self),
return NULL;
}
assert(device_request == NPY_DEVICE_CPU);
call_args[1] = dl_cpu_device_tuple;
call_args[1] =npy_static_pydata.dl_cpu_device_tuple;
}


PyObject *capsule = PyObject_VectorcallMethod(
npy_interned_str.__dlpack__, call_args, nargsf, call_kwnames);
npy_interned_str.__dlpack__, call_args, nargsf,
npy_static_pydata.dl_call_kwnames);
if (capsule == NULL) {
/*
* TODO: This path should be deprecated in NumPy 2.1. Once deprecated
Expand Down
16 changes: 16 additions & 0 deletionsnumpy/_core/src/multiarray/npy_static_data.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -184,6 +184,22 @@ initialize_static_globals(void)
return -1;
}

npy_static_pydata.dl_call_kwnames =
Py_BuildValue("(sss)", "dl_device", "copy", "max_version");
if (npy_static_pydata.dl_call_kwnames == NULL) {
return -1;
}

npy_static_pydata.dl_cpu_device_tuple = Py_BuildValue("(i,i)", 1, 0);
if (npy_static_pydata.dl_cpu_device_tuple == NULL) {
return -1;
}

npy_static_pydata.dl_max_version = Py_BuildValue("(i,i)", 1, 0);
if (npy_static_pydata.dl_max_version == NULL) {
return -1;
}

/*
* Initialize contents of npy_static_cdata struct
*
Expand Down
7 changes: 7 additions & 0 deletionsnumpy/_core/src/multiarray/npy_static_data.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -138,6 +138,13 @@ typedef struct npy_static_pydata_struct {
PyObject *GenericToVoidMethod;
PyObject *ObjectToGenericMethod;
PyObject *GenericToObjectMethod;

/*
* Used in from_dlpack
*/
PyObject *dl_call_kwnames;
PyObject *dl_cpu_device_tuple;
PyObject *dl_max_version;
} npy_static_pydata_struct;


Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp