Skip to content

Commit a66a958

Browse files
williamwen42pytorchmergebot
authored andcommitted
[dynamo] support Python 3.13t (pytorch#149549)
A few bug fixes to get Dynamo mostly working with 3.13 nogil. Dynamo encounters internal CPython assert errors in older versions of 3.13. The fix has been landed on [CPython's 3.13 branch](https://github.com/python/cpython/tree/3.13) and will be included in 3.13.3 (https://peps.python.org/pep-0719/ - april 8). If you wish to try `torch.compile` on the latest 3.13 branch, you can comment out the error checking (i.e. https://github.com/pytorch/pytorch/blob/70b6cd4e11fe61f8f9d6229b6da510c4d91a992b/torch/__init__.py#L2535 and https://github.com/pytorch/pytorch/blob/70b6cd4e11fe61f8f9d6229b6da510c4d91a992b/torch/_dynamo/eval_frame.py#L899). We will work on getting PyTorch CI up for Dynamo/dynamo-wrapped/inductor once 3.13.3 is available. Pull Request resolved: pytorch#149549 Approved by: https://github.com/jansel
1 parent 970ac2d commit a66a958

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

torch/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,9 +2532,14 @@ def foo(x):
25322532
_C._log_api_usage_once("torch.compile")
25332533
if sys.version_info >= (3, 14):
25342534
raise RuntimeError("torch.compile is not supported on Python 3.14+")
2535-
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
2535+
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1 and sys.version_info < (
2536+
3,
2537+
13,
2538+
3,
2539+
):
25362540
raise RuntimeError(
2537-
"torch.compile is not supported on Python built with GIL disabled"
2541+
"torch.compile is not supported on Python < 3.13.3 built with GIL disabled. "
2542+
"Please use Python 3.13.3+."
25382543
)
25392544

25402545
# Decorator mode

torch/_dynamo/eval_frame.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,14 @@ def __call__(self, fn):
896896
def check_if_dynamo_supported():
897897
if sys.version_info >= (3, 14):
898898
raise RuntimeError("Python 3.14+ not yet supported for torch.compile")
899-
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
899+
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1 and sys.version_info < (
900+
3,
901+
13,
902+
3,
903+
):
900904
raise RuntimeError(
901-
"torch.compile is not supported on Python built with GIL disabled"
905+
"torch.compile is not supported on Python < 3.13.3 built with GIL disabled. "
906+
"Please use Python 3.13.3+."
902907
)
903908

904909

torch/_inductor/codecache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,7 @@ class CppPythonBindingsCodeCache(CppCodeCache):
22832283
return NULL;
22842284
}}
22852285
#ifdef Py_GIL_DISABLED
2286-
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
2286+
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
22872287
#endif
22882288
return module;
22892289
}}

torch/csrc/dynamo/eval_frame.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,10 @@ static PyObject* set_skip_guard_eval_unsafe(
589589
}
590590

591591
static PyObject* get_eval_frame_callback_py(PyObject* dummy, PyObject* args) {
592-
return eval_frame_callback_get();
592+
// New reference
593+
PyObject* callback = eval_frame_callback_get();
594+
Py_INCREF(callback);
595+
return callback;
593596
}
594597

595598
static PyObject* reset_code(PyObject* dummy, PyObject* code) {

torch/csrc/dynamo/guards.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,13 +940,13 @@ std::string get_exception_message() {
940940
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
941941

942942
PyObject* exc_message_pyobj = PyObject_Str(pvalue);
943-
const char* exc_message = PyUnicode_AsUTF8(exc_message_pyobj);
943+
std::string exc_message = PyUnicode_AsUTF8(exc_message_pyobj);
944944

945945
Py_DECREF(exc_message_pyobj);
946946
Py_XDECREF(ptype);
947947
Py_XDECREF(pvalue);
948948
Py_XDECREF(ptraceback);
949-
return std::string(exc_message);
949+
return exc_message;
950950
}
951951

952952
bool is_immutable_object(py::handle example_value) {

0 commit comments

Comments
 (0)