Skip to content

Commit 117d28b

Browse files
committed
[GR-65718] Fix torch on 3.12
PullRequest: graalpython/3834
2 parents 5e84626 + 243e152 commit 117d28b

File tree

5 files changed

+116
-24
lines changed

5 files changed

+116
-24
lines changed

graalpython/com.oracle.graal.python.cext/src/gcmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
static inline int
4444
call_traverse(traverseproc traverse, PyObject *op, visitproc visit, void *arg)
4545
{
46+
if (points_to_py_handle_space(op)) {
47+
// Ignore managed handles. Some builtin types don't (yet) create native objects when subclassed in native,
48+
// so they can end up with native traverse. Example is pybind11_static_property that subclasses builtin property
49+
return 0;
50+
}
4651
if (!traverse) {
4752
PyTruffle_Log(PY_TRUFFLE_LOG_FINE,
4853
"type '%.100s' is a GC type but tp_traverse is NULL",

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
package com.oracle.graal.python.builtins.modules;
2727

28+
import static com.oracle.graal.python.nodes.BuiltinNames.T_ENVIRON;
2829
import static com.oracle.graal.python.nodes.BuiltinNames.T_NT;
2930
import static com.oracle.graal.python.nodes.BuiltinNames.T_POSIX;
3031
import static com.oracle.graal.python.nodes.StringLiterals.T_DOT;
@@ -252,7 +253,7 @@ public void initialize(Python3Core core) {
252253
}
253254
PythonLanguage language = core.getLanguage();
254255
addBuiltinConstant("_have_functions", PFactory.createList(language, haveFunctions.toArray()));
255-
addBuiltinConstant("environ", PFactory.createDict(language));
256+
addBuiltinConstant(T_ENVIRON, PFactory.createDict(language));
256257

257258
LinkedHashMap<String, Object> sysconfigNames = new LinkedHashMap<>();
258259
for (IntConstant name : PosixConstants.sysconfigNames) {
@@ -313,16 +314,12 @@ public void postInitialize(Python3Core core) {
313314
// we don't want subprocesses to pick it up
314315
continue;
315316
}
316-
Object key, val;
317-
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
318-
if (entry.getKey().startsWith("=")) {
319-
// Hidden variable, shouldn't be visible to python
320-
continue;
321-
}
322-
key = toTruffleStringUncached(entry.getKey());
323-
} else {
324-
key = PFactory.createBytes(language, entry.getKey().getBytes());
317+
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32 && entry.getKey().startsWith("=")) {
318+
// Hidden variable, shouldn't be visible to python
319+
continue;
325320
}
321+
Object key = toEnv(language, entry.getKey());
322+
Object val = toEnv(language, entry.getValue());
326323
if (pyenvLauncherKey.equals(entry.getKey())) {
327324
// On Mac, the CPython launcher uses this env variable to specify the real Python
328325
// executable. It will be honored by packages like "site". So, if it is set, we
@@ -334,31 +331,24 @@ public void postInitialize(Python3Core core) {
334331
posixLib.setenv(posixSupport, k, v, true);
335332
} catch (PosixException ignored) {
336333
}
337-
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
338-
val = value;
339-
} else {
340-
val = PFactory.createBytes(language, value.toJavaStringUncached().getBytes());
341-
}
342-
} else {
343-
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
344-
val = toTruffleStringUncached(entry.getValue());
345-
} else {
346-
val = PFactory.createBytes(language, (entry.getValue().getBytes()));
347-
}
334+
val = toEnv(language, value);
348335
}
349336
environ.setItem(key, val);
350337
}
351338
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
352339
// XXX: Until we fix pip
353-
environ.setItem(toTruffleStringUncached("PIP_NO_CACHE_DIR"), toTruffleStringUncached("0"));
340+
environ.setItem(toEnv(language, "PIP_NO_CACHE_DIR"), toEnv(language, "0"));
354341
}
342+
// XXX: Until a pyo3 version that doesn't have a different maximum version for GraalPy than
343+
// CPython gets widespread
344+
environ.setItem(toEnv(language, "UNSAFE_PYO3_SKIP_VERSION_CHECK"), toEnv(language, "1"));
355345
PythonModule posix;
356346
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
357347
posix = core.lookupBuiltinModule(T_NT);
358348
} else {
359349
posix = core.lookupBuiltinModule(T_POSIX);
360350
}
361-
Object environAttr = posix.getAttribute(tsLiteral("environ"));
351+
Object environAttr = posix.getAttribute(T_ENVIRON);
362352
((PDict) environAttr).setDictStorage(environ.getDictStorage());
363353

364354
if (posixLib.getBackend(posixSupport).toJavaStringUncached().equals("java")) {
@@ -370,6 +360,22 @@ public void postInitialize(Python3Core core) {
370360
}
371361
}
372362

363+
private static Object toEnv(PythonLanguage language, String value) {
364+
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
365+
return toTruffleStringUncached(value);
366+
} else {
367+
return PFactory.createBytes(language, value.getBytes());
368+
}
369+
}
370+
371+
private static Object toEnv(PythonLanguage language, TruffleString value) {
372+
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
373+
return value;
374+
} else {
375+
return PFactory.createBytes(language, value.toJavaStringUncached().getBytes());
376+
}
377+
}
378+
373379
@Builtin(name = "putenv", minNumOfPositionalArgs = 2, parameterNames = {"name", "value"})
374380
@ArgumentClinic(name = "name", conversionClass = FsConverterNode.class)
375381
@ArgumentClinic(name = "value", conversionClass = FsConverterNode.class)

graalpython/lib-graalpython/patches/torch-2.4.1.patch

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,46 @@ index 7c9b4be00..b8edbcfda 100644
645645
}
646646

647647
ExtraState* init_and_set_extra_state(PyCodeObject* code) {
648+
diff --git a/torch/csrc/dynamo/guards.cpp b/torch/csrc/dynamo/guards.cpp
649+
index b7fde50a9..712e3613b 100644
650+
--- a/torch/csrc/dynamo/guards.cpp
651+
+++ b/torch/csrc/dynamo/guards.cpp
652+
@@ -26,7 +26,7 @@
653+
// https://github.com/python/cpython/blob/9afc6d102d16080535325f645849cd84eb04d57d/Objects/tupleobject.c#L1058-L1062
654+
// To handle this, we manually copy the struct here and manually cast it to this
655+
// new struct. From 3.12, the struct is included in the header file.
656+
-#if IS_PYTHON_3_12_PLUS
657+
+#if 0 // GraalPy change
658+
659+
#define Py_BUILD_CORE
660+
// Bring _PyTupleIterObject from the header file
661+
@@ -596,7 +596,7 @@ static PyObject* check_obj_id(PyObject* dummy, PyObject* args) {
662+
}
663+
}
664+
665+
-#if IS_PYTHON_3_12_PLUS
666+
+#if 0 // GraalPy change
667+
668+
static std::unordered_map<PyObject*, uint64_t> dict_version_map;
669+
static int dict_version_watcher_id;
670+
@@ -617,7 +617,7 @@ static int dict_version_watch_callback(
671+
#endif
672+
673+
static uint64_t get_dict_version_unchecked(PyObject* dict) {
674+
-#if IS_PYTHON_3_12_PLUS
675+
+#if 0 // GraalPy change
676+
677+
if (PyDict_Watch(dict_version_watcher_id, dict)) {
678+
throw std::runtime_error("failed to add version watcher to dict!");
679+
@@ -4013,7 +4013,7 @@ PyObject* torch_c_dynamo_guards_init() {
680+
"install_no_tensor_aliasing_guard", install_no_tensor_aliasing_guard);
681+
682+
// initialize dict_version_map watcher for 3.12
683+
-#if IS_PYTHON_3_12_PLUS
684+
+#if 0 // GraalPy change
685+
686+
dict_version_watcher_id = PyDict_AddWatcher(dict_version_watch_callback);
687+
if (dict_version_watcher_id == -1) {
648688
diff --git a/torch/csrc/jit/python/python_tracer.cpp b/torch/csrc/jit/python/python_tracer.cpp
649689
index 92e6e2d3a..4d2ec0bfe 100644
650690
--- a/torch/csrc/jit/python/python_tracer.cpp

graalpython/lib-graalpython/patches/torch-2.7.0.patch

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,46 @@ index b839fb26f..b7853029f 100644
750750
}
751751

752752
#else
753+
diff --git a/torch/csrc/dynamo/guards.cpp b/torch/csrc/dynamo/guards.cpp
754+
index 60fb339d1..28d07734a 100644
755+
--- a/torch/csrc/dynamo/guards.cpp
756+
+++ b/torch/csrc/dynamo/guards.cpp
757+
@@ -42,7 +42,7 @@
758+
// To handle the older python versions, we manually copy the struct here and
759+
// manually cast it to this new struct. For newer versions, the struct is
760+
// included in the header file.
761+
-#if IS_PYTHON_3_12_PLUS
762+
+#if 0 // GraalPy change
763+
764+
#define Py_BUILD_CORE
765+
#include <internal/pycore_range.h> // _PyRangeIterObject
766+
@@ -707,7 +707,7 @@ static PyObject* check_obj_id(PyObject* dummy, PyObject* args) {
767+
}
768+
}
769+
770+
-#if IS_PYTHON_3_12_PLUS
771+
+#if 0 // GraalPy change
772+
773+
static std::unordered_map<PyObject*, uint64_t> dict_version_map;
774+
static int dict_version_watcher_id;
775+
@@ -728,7 +728,7 @@ static int dict_version_watch_callback(
776+
#endif
777+
778+
static uint64_t get_dict_version_unchecked(PyObject* dict) {
779+
-#if IS_PYTHON_3_12_PLUS
780+
+#if 0 // GraalPy change
781+
782+
if (PyDict_Watch(dict_version_watcher_id, dict)) {
783+
throw std::runtime_error("failed to add version watcher to dict!");
784+
@@ -6090,7 +6090,7 @@ PyObject* torch_c_dynamo_guards_init() {
785+
py_m.def("profile_guard_manager", profile_guard_manager);
786+
787+
// initialize dict_version_map watcher for 3.12
788+
-#if IS_PYTHON_3_12_PLUS
789+
+#if 0 // GraalPy change
790+
791+
dict_version_watcher_id = PyDict_AddWatcher(dict_version_watch_callback);
792+
if (dict_version_watcher_id == -1) {
753793
diff --git a/torch/csrc/jit/python/python_tracer.cpp b/torch/csrc/jit/python/python_tracer.cpp
754794
index 876186743..041348257 100644
755795
--- a/torch/csrc/jit/python/python_tracer.cpp

graalpython/lib-python/3/test/test_subprocess.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,8 @@ def is_env_var_to_ignore(n):
832832
n == 'PWD' or n == 'SHLVL' or # Graalpython bash launcher
833833
n.startswith('JAVA_MAIN_CLASS') or # JVM on MacOS
834834
n == 'LD_LIBRARY_PATH' or n == 'DYLD_LIBRARY_PATH' or # Added by graalpy launcher
835-
n == 'LC_CTYPE') # Locale coercion triggered
835+
n == 'LC_CTYPE', # Locale coercion triggered
836+
n == 'UNSAFE_PYO3_SKIP_VERSION_CHECK') # GraalPy change
836837

837838
with subprocess.Popen([sys.executable, "-c",
838839
'import os; print(list(os.environ.keys()))'],

0 commit comments

Comments
 (0)