Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions onnxruntime/python/onnxruntime_pybind_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading