Move setdlopenflags calls into _pybind_state.py (#8916)

* Use PROTOBUF_LIB instead of protobuf::libprotbuf

* Moved setdlopenflags to _pybind_state.py

* Copy the generated _pybind_state.py to required location for Windows.
This commit is contained in:
satyajandhyala 2021-09-02 09:54:32 -07:00 committed by GitHub
parent f711d8992a
commit 4570d85f20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 4 deletions

View file

@ -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

View file

@ -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}
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_BINARY_DIR}/onnxruntime/capi/_pybind_state.py
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
COMMAND ${CMAKE_COMMAND} -E copy
${onnxruntime_python_capi_training_srcs}
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/training/

View file

@ -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@

View file

@ -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)