diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index 1456c8caa8993..59b3d1428e081 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -69,6 +69,20 @@ endif() onnxruntime_add_shared_library_module(onnxruntime_pybind11_state ${onnxruntime_pybind_srcs}) +message(STATUS "Python_EXECUTABLE: ${Python_EXECUTABLE}") + +# Query Py_GIL_DISABLED (PEP 703) +execute_process( + COMMAND "${Python_EXECUTABLE}" -c + "import sysconfig; print(sysconfig.get_config_var('Py_GIL_DISABLED') or '0')" + RESULT_VARIABLE _py_result + OUTPUT_VARIABLE _py_gil_disabled + OUTPUT_STRIP_TRAILING_WHITESPACE +) +if (_py_result EQUAL 0 AND _py_gil_disabled STREQUAL "1") + message(STATUS "Py_GIL_DISABLED=1 detected: Enabling free-threaded support for onnxruntime_pybind11_state") +endif() + if(MSVC) # The following source file is only needed for the EPs that use delayloading. Namely, DML and WebGPU. target_sources(onnxruntime_pybind11_state PRIVATE "${ONNXRUNTIME_ROOT}/core/dll/delay_load_hook.cc") diff --git a/onnxruntime/python/onnxruntime_pybind_module.cc b/onnxruntime/python/onnxruntime_pybind_module.cc index a067f8d548799..5bf16439c0917 100644 --- a/onnxruntime/python/onnxruntime_pybind_module.cc +++ b/onnxruntime/python/onnxruntime_pybind_module.cc @@ -102,7 +102,12 @@ static constexpr bool HAS_COLLECTIVE_OPS = false; void CreateQuantPybindModule(py::module& m); +// Check if we are building with GIL disabled (Free-threaded) +#ifdef Py_GIL_DISABLED +PYBIND11_MODULE(onnxruntime_pybind11_state, m, py::mod_gil_not_used()) { +#else PYBIND11_MODULE(onnxruntime_pybind11_state, m) { +#endif auto st = CreateInferencePybindStateModule(m); if (!st.IsOK()) throw pybind11::import_error(st.ErrorMessage());