Skip to content

Commit

Permalink
onnxruntime shared lib inside python package (#21223)
Browse files Browse the repository at this point in the history
  • Loading branch information
baijumeswani authored Jul 2, 2024
1 parent 7df97f1 commit 116398c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
9 changes: 9 additions & 0 deletions cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,15 @@ add_custom_command(
$<TARGET_FILE_DIR:${build_output_target}>
)

if (onnxruntime_BUILD_SHARED_LIB)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:onnxruntime>
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
)
endif()

if (onnxruntime_USE_OPENVINO)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
Expand Down
21 changes: 17 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,13 @@ def finalize_options(self):
"libmklml_gnu.so",
"libiomp5.so",
"mimalloc.so",
"libonnxruntime.so*",
]
dl_libs = ["libonnxruntime_providers_shared.so"]
dl_libs.append(providers_cuda_or_rocm)
dl_libs.append(providers_tensorrt_or_migraphx)
dl_libs.append(providers_cann)
dl_libs.append("libonnxruntime.so*")
# DNNL, TensorRT & OpenVINO EPs are built as shared libs
libs.extend(["libonnxruntime_providers_shared.so"])
libs.extend(["libonnxruntime_providers_dnnl.so"])
Expand All @@ -313,7 +315,12 @@ def finalize_options(self):
if nightly_build:
libs.extend(["libonnxruntime_pywrapper.so"])
elif platform.system() == "Darwin":
libs = ["onnxruntime_pybind11_state.so", "libdnnl.2.dylib", "mimalloc.so"] # TODO add libmklml and libiomp5 later.
libs = [
"onnxruntime_pybind11_state.so",
"libdnnl.2.dylib",
"mimalloc.so",
"libonnxruntime.dylib*",
] # TODO add libmklml and libiomp5 later.
# DNNL & TensorRT EPs are built as shared libs
libs.extend(["libonnxruntime_providers_shared.dylib"])
libs.extend(["libonnxruntime_providers_dnnl.dylib"])
Expand All @@ -323,7 +330,13 @@ def finalize_options(self):
if nightly_build:
libs.extend(["libonnxruntime_pywrapper.dylib"])
else:
libs = ["onnxruntime_pybind11_state.pyd", "dnnl.dll", "mklml.dll", "libiomp5md.dll"]
libs = [
"onnxruntime_pybind11_state.pyd",
"dnnl.dll",
"mklml.dll",
"libiomp5md.dll",
"onnxruntime.dll",
]
# DNNL, TensorRT & OpenVINO EPs are built as shared libs
libs.extend(["onnxruntime_providers_shared.dll"])
libs.extend(["onnxruntime_providers_dnnl.dll"])
Expand Down Expand Up @@ -376,15 +389,15 @@ def finalize_options(self):
dl_libs.append("plugins.xml")
dl_libs.append("usb-ma2x8x.mvcmd")
data = ["capi/libonnxruntime_pywrapper.so"] if nightly_build else []
data += [path.join("capi", x) for x in dl_libs if path.isfile(path.join("onnxruntime", "capi", x))]
data += [path.join("capi", x) for x in dl_libs if glob(path.join("onnxruntime", "capi", x))]
ext_modules = [
Extension(
"onnxruntime.capi.onnxruntime_pybind11_state",
["onnxruntime/capi/onnxruntime_pybind11_state_manylinux1.so"],
),
]
else:
data = [path.join("capi", x) for x in libs if path.isfile(path.join("onnxruntime", "capi", x))]
data = [path.join("capi", x) for x in libs if glob(path.join("onnxruntime", "capi", x))]
ext_modules = []

# Additional examples
Expand Down
11 changes: 10 additions & 1 deletion tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,16 @@ def main():
if args.build_wheel or args.gen_doc or args.use_tvm or args.enable_training:
args.enable_pybind = True

if args.build_csharp or args.build_nuget or args.build_java or args.build_nodejs:
if (
args.build_csharp
or args.build_nuget
or args.build_java
or args.build_nodejs
or (args.enable_pybind and not args.enable_training)
):
# If pyhon bindings are enabled, we embed the shared lib in the python package.
# If training is enabled, we don't embed the shared lib in the python package since training requires
# torch interop.
args.build_shared_lib = True

if args.build_nuget and cross_compiling:
Expand Down

0 comments on commit 116398c

Please sign in to comment.