diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 22cc056476..bbed346163 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -1853,7 +1853,7 @@ if (onnxruntime_ENABLE_EXTERNAL_CUSTOM_OP_SCHEMAS) message(FATAL_ERROR "External custom operator schemas is supported only with --enable-training option") endif() - add_custom_target(install_protobuf ALL DEPENDS protobuf::libprotobuf protobuf::protoc) + add_custom_target(install_protobuf ALL DEPENDS ${PROTOBUF_LIB} protobuf::protoc) add_custom_command( TARGET install_protobuf COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR} -P cmake_install.cmake diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index 098502a36f..452fb8a09a 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -240,6 +240,18 @@ else() ) endif() +# Generate _pybind_state.py from _pybind_state.py.in replacing macros with either setdlopenflags or "" +if (onnxruntime_ENABLE_EXTERNAL_CUSTOM_OP_SCHEMAS) + set(ONNXRUNTIME_SETDLOPENFLAGS_GLOBAL "sys.setdlopenflags(os.RTLD_GLOBAL|os.RTLD_NOW|os.RTLD_DEEPBIND)") + set(ONNXRUNTIME_SETDLOPENFLAGS_LOCAL "sys.setdlopenflags(os.RTLD_LOCAL|os.RTLD_NOW|os.RTLD_DEEPBIND)") +else() + set(ONNXRUNTIME_SETDLOPENFLAGS_GLOBAL "") + set(ONNXRUNTIME_SETDLOPENFLAGS_LOCAL "") +endif() + +configure_file(${ONNXRUNTIME_ROOT}/python/_pybind_state.py.in + ${CMAKE_BINARY_DIR}/onnxruntime/capi/_pybind_state.py) + if (onnxruntime_ENABLE_TRAINING) file(GLOB onnxruntime_python_capi_training_srcs CONFIGURE_DEPENDS "${ORTTRAINING_SOURCE_DIR}/python/deprecated/*.py" @@ -384,6 +396,9 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_python_srcs} $/onnxruntime/capi/ + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${CMAKE_BINARY_DIR}/onnxruntime/capi/_pybind_state.py + $/onnxruntime/capi/ COMMAND ${CMAKE_COMMAND} -E copy ${onnxruntime_python_capi_training_srcs} $/onnxruntime/capi/training/ diff --git a/onnxruntime/python/_pybind_state.py b/onnxruntime/python/_pybind_state.py.in similarity index 90% rename from onnxruntime/python/_pybind_state.py rename to onnxruntime/python/_pybind_state.py.in index 2b32641050..4fa859a559 100644 --- a/onnxruntime/python/_pybind_state.py +++ b/onnxruntime/python/_pybind_state.py.in @@ -7,6 +7,7 @@ Ensure that dependencies are available and then load the extension module. """ import os import platform +import sys from . import _ld_preload # noqa: F401 @@ -17,5 +18,6 @@ if platform.system() == "Windows": if not os.path.isfile("C:\\Windows\\System32\\vcruntime140_1.dll"): raise ImportError( "Microsoft Visual C++ Redistributable for Visual Studio 2019 not installed on the machine.") - +@ONNXRUNTIME_SETDLOPENFLAGS_GLOBAL@ from .onnxruntime_pybind11_state import * # noqa +@ONNXRUNTIME_SETDLOPENFLAGS_LOCAL@ diff --git a/orttraining/orttraining/test/external_custom_ops/test.py b/orttraining/orttraining/test/external_custom_ops/test.py index 1d74a46c45..e147442521 100644 --- a/orttraining/orttraining/test/external_custom_ops/test.py +++ b/orttraining/orttraining/test/external_custom_ops/test.py @@ -8,10 +8,8 @@ import numpy as np # Expose available (onnx::* and protobuf::*) symbols from onnxruntime to resolve references in # the custom ops shared library. Deepbind flag is required to avoid conflicts with other # instances of onnx/protobuf libraries. -sys.setdlopenflags(os.RTLD_GLOBAL|os.RTLD_NOW|os.RTLD_DEEPBIND) import onnxruntime # Restore dlopen flags. -sys.setdlopenflags(os.RTLD_LOCAL|os.RTLD_NOW|os.RTLD_DEEPBIND) import orttraining_external_custom_ops so = onnxruntime.SessionOptions() sess = onnxruntime.InferenceSession("testdata/model.onnx", so)