Enable Free-threaded Python (PEP 703) support for Python 3.13t+ #26786
+19
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR resolves the
RuntimeWarningencountered when importingonnxruntimein free-threaded Python environments (e.g., Python 3.13t, 3.14t).Previously, the module did not explicitly declare that it could run safely without the GIL, causing the interpreter to re-enable the GIL at runtime.
The Warning
Changes
1. Build System (
cmake/onnxruntime_python.cmake)Detection strategy:
Primary: Check
sysconfig.get_config_var('Py_GIL_DISABLED')(PEP 703 standard).Fallback: Inspect ABI flags (
ABIFLAGSorSOABI) for thetsuffix (e.g.,cp313t), handling cases on Windows where config variables may returnNoneor empty strings.Caching: Cache the result as
ORT_PYTHON_FREE_THREADED_DETECTED(BOOL) to avoid re-running detection on every configure.2. C++ Source (
onnxruntime/python/onnxruntime_pybind_module.cc)PYBIND11_MODULEdefinition to conditionally enable the GIL-free slot.py::mod_gil_not_used()(available in pybind11 ≥ 2.13) whenPy_GIL_DISABLEDis defined.Verification
Environment:
Before Change
python3.14t -c "import onnxruntime"Resulted in the
RuntimeWarningshown above.After Change
python3.14t -c "import onnxruntime"Runs silently with no warnings, confirming the module loads successfully with the GIL disabled.
Related Issue