[dynamo] support Python 3.13t (#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. 70b6cd4e11/torch/__init__.py (L2535) and 70b6cd4e11/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: https://github.com/pytorch/pytorch/pull/149549
Approved by: https://github.com/jansel
This commit is contained in:
William Wen 2025-03-19 13:02:08 -07:00 committed by PyTorch MergeBot
parent 970ac2d907
commit a66a9581da
5 changed files with 21 additions and 8 deletions

View File

@ -2532,9 +2532,14 @@ def compile(
_C._log_api_usage_once("torch.compile")
if sys.version_info >= (3, 14):
raise RuntimeError("torch.compile is not supported on Python 3.14+")
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1 and sys.version_info < (
3,
13,
3,
):
raise RuntimeError(
"torch.compile is not supported on Python built with GIL disabled"
"torch.compile is not supported on Python < 3.13.3 built with GIL disabled. "
"Please use Python 3.13.3+."
)
# Decorator mode

View File

@ -896,9 +896,14 @@ class _NullDecorator(contextlib.nullcontext): # type: ignore[type-arg]
def check_if_dynamo_supported():
if sys.version_info >= (3, 14):
raise RuntimeError("Python 3.14+ not yet supported for torch.compile")
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1 and sys.version_info < (
3,
13,
3,
):
raise RuntimeError(
"torch.compile is not supported on Python built with GIL disabled"
"torch.compile is not supported on Python < 3.13.3 built with GIL disabled. "
"Please use Python 3.13.3+."
)

View File

@ -2283,7 +2283,7 @@ class CppPythonBindingsCodeCache(CppCodeCache):
return NULL;
}}
#ifdef Py_GIL_DISABLED
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
#endif
return module;
}}

View File

@ -589,7 +589,10 @@ static PyObject* set_skip_guard_eval_unsafe(
}
static PyObject* get_eval_frame_callback_py(PyObject* dummy, PyObject* args) {
return eval_frame_callback_get();
// New reference
PyObject* callback = eval_frame_callback_get();
Py_INCREF(callback);
return callback;
}
static PyObject* reset_code(PyObject* dummy, PyObject* code) {

View File

@ -940,13 +940,13 @@ std::string get_exception_message() {
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
PyObject* exc_message_pyobj = PyObject_Str(pvalue);
const char* exc_message = PyUnicode_AsUTF8(exc_message_pyobj);
std::string exc_message = PyUnicode_AsUTF8(exc_message_pyobj);
Py_DECREF(exc_message_pyobj);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
return std::string(exc_message);
return exc_message;
}
bool is_immutable_object(py::handle example_value) {