Skip to content

Commit 0945b42

Browse files
committed
Add patch for pybind11-2.10.1
1 parent 6f5142a commit 0945b42

File tree

3 files changed

+273
-11
lines changed

3 files changed

+273
-11
lines changed

graalpython/lib-graalpython/patches/onnxruntime/onnxruntime-1.17.1.patch

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
2-
index 94d650f..62ffee7 100644
2+
index 94d650f..175a6b8 100644
33
--- a/cmake/CMakeLists.txt
44
+++ b/cmake/CMakeLists.txt
55
@@ -54,6 +54,8 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
66
enable_testing()
77
include(Dart)
88

9-
+set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wno-error=array-bounds -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict -Wno-error=unused-but-set-variable -Wno-error=overloaded-virtual")
9+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=array-bounds -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict -Wno-error=unused-but-set-variable -Wno-error=overloaded-virtual")
1010
+
1111
if (NOT CMAKE_BUILD_TYPE)
1212
message(STATUS "Build type not set - using RelWithDebInfo")
1313
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose build type: Debug Release RelWithDebInfo MinSizeRel." FORCE)
1414
diff --git a/onnxruntime_build_backend.py b/onnxruntime_build_backend.py
1515
new file mode 100644
16-
index 0000000..dac866e
16+
index 0000000..33fc235
1717
--- /dev/null
1818
+++ b/onnxruntime_build_backend.py
19-
@@ -0,0 +1,52 @@
19+
@@ -0,0 +1,59 @@
2020
+import os
2121
+import re
2222
+import sys
@@ -45,26 +45,33 @@ index 0000000..dac866e
4545
+
4646
+def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
4747
+ wheel_directory = Path(wheel_directory).absolute()
48-
+ builddir = Path('build/Release')
48+
+ build_type = 'Release'
49+
+ build_dir = Path(f'build/{build_type}')
4950
+ parallel = os.environ.get('CMAKE_BUILD_PARALLEL_LEVEL', os.cpu_count())
5051
+ build_cmd = [
5152
+ sys.executable,
5253
+ 'tools/ci_build/build.py',
5354
+ '--build_dir', 'build',
54-
+ '--build_wheel',
5555
+ '--skip_submodule_sync',
5656
+ '--skip_tests',
57-
+ '--config', 'Release',
57+
+ '--config', build_type,
5858
+ '--enable_pybind',
5959
+ '--parallel', str(parallel),
6060
+ ]
61-
+ subprocess.check_call(build_cmd)
6261
+ if sys.implementation.name == 'graalpy':
6362
+ # The cmake build downloads a bunch of sources that need to be patched
64-
+ subprocess.check_call([sys.executable, '-m', 'autopatch_capi', '.'])
65-
+ # Rerun to rebuild the patched sources
6663
+ subprocess.check_call(build_cmd)
67-
+ wheels = list((builddir / 'dist').glob('*.whl'))
64+
+ marker = build_dir / 'graalpy-patched-marker'
65+
+ if not marker.exists():
66+
+ subprocess.check_call([sys.executable, '-m', 'autopatch_capi', '.'])
67+
+ pybind11_dir = build_dir / '_deps/pybind11_project-src'
68+
+ patches_dir = Path(__graalpython__.core_home) / 'patches'
69+
+ with open(patches_dir / 'pybind11' / 'pybind11-2.10.1.patch') as f:
70+
+ subprocess.check_call(['patch', '-p2', '-f'], stdin=f, cwd=pybind11_dir)
71+
+ with open(marker, 'w') as f:
72+
+ pass
73+
+ subprocess.check_call([*build_cmd, '--build_wheel'])
74+
+ wheels = list((build_dir / 'dist').glob('*.whl'))
6875
+ assert len(wheels) == 1, f"Expected 1 wheel, found {len(wheels)}"
6976
+ wheel = wheels[0]
7077
+ shutil.copyfile(wheel, wheel_directory / wheel.name)

graalpython/lib-graalpython/patches/pybind11/metadata.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
version = '== 2.10.4'
33
patch = 'pybind11-2.10.4.patch'
44

5+
[[rules]]
6+
# Note: This patch file is also used directly outside of pip during onnxruntime build
7+
version = '== 2.10.1'
8+
patch = 'pybind11-2.10.1.patch'
9+
510
[[rules]]
611
version = '== 2.10.0'
712
patch = 'pybind11-2.10.0.patch'
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
diff --git a/pybind11/include/pybind11/cast.h b/pybind11/include/pybind11/cast.h
2+
index 6f0a75a..173a3d9 100644
3+
--- a/pybind11/include/pybind11/cast.h
4+
+++ b/pybind11/include/pybind11/cast.h
5+
@@ -132,7 +132,7 @@ public:
6+
return false;
7+
}
8+
9+
-#if !defined(PYPY_VERSION)
10+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
11+
auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
12+
#else
13+
// In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
14+
@@ -328,7 +328,7 @@ public:
15+
if (src.is_none()) {
16+
res = 0; // None is implicitly converted to False
17+
}
18+
-#if defined(PYPY_VERSION)
19+
+#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
20+
// On PyPy, check that "__bool__" attr exists
21+
else if (hasattr(src, PYBIND11_BOOL_ATTR)) {
22+
res = PyObject_IsTrue(src.ptr());
23+
@@ -445,7 +445,7 @@ struct string_caster {
24+
25+
private:
26+
static handle decode_utfN(const char *buffer, ssize_t nbytes) {
27+
-#if !defined(PYPY_VERSION)
28+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
29+
return UTF_N == 8 ? PyUnicode_DecodeUTF8(buffer, nbytes, nullptr)
30+
: UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr)
31+
: PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr);
32+
diff --git a/pybind11/include/pybind11/detail/class.h b/pybind11/include/pybind11/detail/class.h
33+
index 528e716..352f686 100644
34+
--- a/pybind11/include/pybind11/detail/class.h
35+
+++ b/pybind11/include/pybind11/detail/class.h
36+
@@ -26,7 +26,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
37+
#endif
38+
39+
inline std::string get_fully_qualified_tp_name(PyTypeObject *type) {
40+
-#if !defined(PYPY_VERSION)
41+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
42+
return type->tp_name;
43+
#else
44+
auto module_name = handle((PyObject *) type).attr("__module__").cast<std::string>();
45+
@@ -42,7 +42,7 @@ inline PyTypeObject *type_incref(PyTypeObject *type) {
46+
return type;
47+
}
48+
49+
-#if !defined(PYPY_VERSION)
50+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
51+
52+
/// `pybind11_static_property.__get__()`: Always pass the class instead of the instance.
53+
extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) {
54+
@@ -149,7 +149,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyOb
55+
&& (PyObject_IsInstance(value, static_prop) == 0);
56+
if (call_descr_set) {
57+
// Call `static_property.__set__()` instead of replacing the `static_property`.
58+
-#if !defined(PYPY_VERSION)
59+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
60+
return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
61+
#else
62+
if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
63+
@@ -347,7 +347,7 @@ inline bool deregister_instance(instance *self, void *valptr, const type_info *t
64+
/// for holding C++ objects and holders. Allocation is done lazily (the first time the instance is
65+
/// cast to a reference or pointer), and initialization is done by an `__init__` function.
66+
inline PyObject *make_new_instance(PyTypeObject *type) {
67+
-#if defined(PYPY_VERSION)
68+
+#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
69+
// PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first
70+
// inherited object is a plain Python type (i.e. not derived from an extension type). Fix it.
71+
ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
72+
@@ -638,7 +638,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
73+
}
74+
75+
const auto *full_name = c_str(
76+
-#if !defined(PYPY_VERSION)
77+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
78+
module_ ? str(module_).cast<std::string>() + "." + rec.name :
79+
#endif
80+
rec.name);
81+
diff --git a/pybind11/include/pybind11/detail/common.h b/pybind11/include/pybind11/detail/common.h
82+
index f34fd27..f1681b7 100644
83+
--- a/pybind11/include/pybind11/detail/common.h
84+
+++ b/pybind11/include/pybind11/detail/common.h
85+
@@ -233,6 +233,9 @@
86+
#if defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
87+
# define PYBIND11_SIMPLE_GIL_MANAGEMENT
88+
#endif
89+
+#ifdef GRAALVM_PYTHON
90+
+# define PYBIND11_SIMPLE_GIL_MANAGEMENT
91+
+#endif
92+
93+
#if defined(_MSC_VER)
94+
# if defined(PYBIND11_DEBUG_MARKER)
95+
diff --git a/pybind11/include/pybind11/detail/internals.h b/pybind11/include/pybind11/detail/internals.h
96+
index 7de7794..146b084 100644
97+
--- a/pybind11/include/pybind11/detail/internals.h
98+
+++ b/pybind11/include/pybind11/detail/internals.h
99+
@@ -174,7 +174,7 @@ struct internals {
100+
PyTypeObject *static_property_type;
101+
PyTypeObject *default_metaclass;
102+
PyObject *instance_base;
103+
-#if defined(WITH_THREAD)
104+
+#if defined(WITH_THREAD) && !defined(GRAALVM_PYTHON)
105+
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
106+
PYBIND11_TLS_KEY_INIT(tstate)
107+
# if PYBIND11_INTERNALS_VERSION > 4
108+
@@ -278,7 +278,7 @@ struct type_info {
109+
#endif
110+
111+
#ifndef PYBIND11_INTERNALS_KIND
112+
-# if defined(WITH_THREAD)
113+
+# if defined(WITH_THREAD) && !defined(GRAALVM_PYTHON)
114+
# define PYBIND11_INTERNALS_KIND ""
115+
# else
116+
# define PYBIND11_INTERNALS_KIND "_without_thread"
117+
@@ -454,7 +454,7 @@ PYBIND11_NOINLINE internals &get_internals() {
118+
}
119+
auto *&internals_ptr = *internals_pp;
120+
internals_ptr = new internals();
121+
-#if defined(WITH_THREAD)
122+
+#if defined(WITH_THREAD) && !defined(GRAALVM_PYTHON)
123+
124+
# if PY_VERSION_HEX < 0x03090000
125+
PyEval_InitThreads();
126+
@@ -491,7 +491,7 @@ PYBIND11_NOINLINE internals &get_internals() {
127+
struct local_internals {
128+
type_map<type_info *> registered_types_cpp;
129+
std::forward_list<ExceptionTranslator> registered_exception_translators;
130+
-#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4
131+
+#if defined(WITH_THREAD) && !defined(GRAALVM_PYTHON) && PYBIND11_INTERNALS_VERSION == 4
132+
133+
// For ABI compatibility, we can't store the loader_life_support TLS key in
134+
// the `internals` struct directly. Instead, we store it in `shared_data` and
135+
diff --git a/pybind11/include/pybind11/detail/type_caster_base.h b/pybind11/include/pybind11/detail/type_caster_base.h
136+
index 21f69c2..0b853f0 100644
137+
--- a/pybind11/include/pybind11/detail/type_caster_base.h
138+
+++ b/pybind11/include/pybind11/detail/type_caster_base.h
139+
@@ -36,7 +36,7 @@ private:
140+
loader_life_support *parent = nullptr;
141+
std::unordered_set<PyObject *> keep_alive;
142+
143+
-#if defined(WITH_THREAD)
144+
+#if defined(WITH_THREAD) && !defined(GRAALVM_PYTHON)
145+
// Store stack pointer in thread-local storage.
146+
static PYBIND11_TLS_KEY_REF get_stack_tls_key() {
147+
# if PYBIND11_INTERNALS_VERSION == 4
148+
@@ -55,7 +55,7 @@ private:
149+
// Use single global variable for stack.
150+
static loader_life_support **get_stack_pp() {
151+
static loader_life_support *global_stack = nullptr;
152+
- return global_stack;
153+
+ return &global_stack;
154+
}
155+
static loader_life_support *get_stack_top() { return *get_stack_pp(); }
156+
static void set_stack_top(loader_life_support *value) { *get_stack_pp() = value; }
157+
@@ -484,7 +484,7 @@ PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_i
158+
}
159+
160+
inline PyThreadState *get_thread_state_unchecked() {
161+
-#if defined(PYPY_VERSION)
162+
+#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
163+
return PyThreadState_GET();
164+
#else
165+
return _PyThreadState_UncheckedGet();
166+
diff --git a/pybind11/include/pybind11/eval.h b/pybind11/include/pybind11/eval.h
167+
index bd5f981..c25f036 100644
168+
--- a/pybind11/include/pybind11/eval.h
169+
+++ b/pybind11/include/pybind11/eval.h
170+
@@ -94,7 +94,7 @@ void exec(const char (&s)[N], object global = globals(), object local = object()
171+
eval<eval_statements>(s, std::move(global), std::move(local));
172+
}
173+
174+
-#if defined(PYPY_VERSION)
175+
+#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
176+
template <eval_mode mode = eval_statements>
177+
object eval_file(str, object, object) {
178+
pybind11_fail("eval_file not supported in PyPy3. Use eval");
179+
diff --git a/pybind11/include/pybind11/gil.h b/pybind11/include/pybind11/gil.h
180+
index cb0028d..6435226 100644
181+
--- a/pybind11/include/pybind11/gil.h
182+
+++ b/pybind11/include/pybind11/gil.h
183+
@@ -173,8 +173,8 @@ public:
184+
if (disassoc) {
185+
// Python >= 3.7 can remove this, it's an int before 3.7
186+
// NOLINTNEXTLINE(readability-qualified-auto)
187+
- auto key = detail::get_internals().tstate;
188+
- PYBIND11_TLS_REPLACE_VALUE(key, tstate);
189+
+ // auto key = detail::get_internals().tstate;
190+
+ // PYBIND11_TLS_REPLACE_VALUE(key, tstate);
191+
}
192+
}
193+
194+
diff --git a/pybind11/include/pybind11/pybind11.h b/pybind11/include/pybind11/pybind11.h
195+
index 42d432a..2e12a93 100644
196+
--- a/pybind11/include/pybind11/pybind11.h
197+
+++ b/pybind11/include/pybind11/pybind11.h
198+
@@ -537,7 +537,7 @@ protected:
199+
chain_start = rec;
200+
rec->next = chain;
201+
auto rec_capsule
202+
- = reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self);
203+
+ = reinterpret_borrow<capsule>(PyCFunction_GetSelf(m_ptr));
204+
rec_capsule.set_pointer(unique_rec.release());
205+
guarded_strdup.release();
206+
} else {
207+
@@ -2657,7 +2657,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
208+
209+
/* Don't call dispatch code if invoked from overridden function.
210+
Unfortunately this doesn't work on PyPy. */
211+
-#if !defined(PYPY_VERSION)
212+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
213+
# if PY_VERSION_HEX >= 0x03090000
214+
PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
215+
if (frame != nullptr) {
216+
diff --git a/pybind11/include/pybind11/pytypes.h b/pybind11/include/pybind11/pytypes.h
217+
index d21fc89..70cf72a 100644
218+
--- a/pybind11/include/pybind11/pytypes.h
219+
+++ b/pybind11/include/pybind11/pytypes.h
220+
@@ -534,7 +534,7 @@ struct error_fetch_and_normalize {
221+
222+
bool have_trace = false;
223+
if (m_trace) {
224+
-#if !defined(PYPY_VERSION)
225+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
226+
auto *tb = reinterpret_cast<PyTracebackObject *>(m_trace.ptr());
227+
228+
// Get the deepest trace possible.
229+
@@ -1208,7 +1208,7 @@ private:
230+
};
231+
PYBIND11_NAMESPACE_END(iterator_policies)
232+
233+
-#if !defined(PYPY_VERSION)
234+
+#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
235+
using tuple_iterator = generic_iterator<iterator_policies::sequence_fast_readonly>;
236+
using list_iterator = generic_iterator<iterator_policies::sequence_fast_readonly>;
237+
#else
238+
diff --git a/pybind11/include/pybind11/stl/filesystem.h b/pybind11/include/pybind11/stl/filesystem.h
239+
index e26f421..a221955 100644
240+
--- a/pybind11/include/pybind11/stl/filesystem.h
241+
+++ b/pybind11/include/pybind11/stl/filesystem.h
242+
@@ -40,7 +40,7 @@ struct path_caster {
243+
244+
private:
245+
static PyObject *unicode_from_fs_native(const std::string &w) {
246+
-# if !defined(PYPY_VERSION)
247+
+# if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON)
248+
return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size()));
249+
# else
250+
// PyPy mistakenly declares the first parameter as non-const.

0 commit comments

Comments
 (0)