Skip to content

Commit

Permalink
Load the onnxruntime shared lib in python (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
baijumeswani authored Aug 5, 2024
1 parent 03594a1 commit 7e29b42
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/linux-cpu-arm64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ jobs:
mkdir -p ort/lib
mv microsoft.ml.onnxruntime/**/build/native/include ort/
mv microsoft.ml.onnxruntime/**/runtimes/linux-arm64/native/* ort/lib/
ort_version=$(echo ${{ env.ORT_NIGHTLY_VERSION }} | cut -d- -f1-1)
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.$ort_version
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.1
- name: Download Docker Image
run: |
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/linux-cpu-x64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ jobs:
mkdir -p ort/lib
mv microsoft.ml.onnxruntime/${{ env.ORT_NIGHTLY_VERSION }}/build/native/include ort/
mv microsoft.ml.onnxruntime/${{ env.ORT_NIGHTLY_VERSION }}/runtimes/linux-x64/native/* ort/lib/
ort_version=$(echo ${{ env.ORT_NIGHTLY_VERSION }} | cut -d- -f1-1)
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.$ort_version
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.1
- name: Build with CMake and GCC
run: |
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/linux-gpu-x64-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ jobs:
mkdir -p ort/lib
mv microsoft.ml.onnxruntime.gpu.linux/${{ env.ORT_NIGHTLY_VERSION }}/buildTransitive/native/include ort/
mv microsoft.ml.onnxruntime.gpu.linux/${{ env.ORT_NIGHTLY_VERSION }}/runtimes/linux-x64/native/* ort/lib/
ort_version=$(echo ${{ env.ORT_NIGHTLY_VERSION }} | cut -d- -f1-1)
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.$ort_version
cp ort/lib/libonnxruntime.so ort/lib/libonnxruntime.so.1
- name: Get Docker Image
Expand Down
27 changes: 1 addition & 26 deletions src/models/onnxruntime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,32 +213,7 @@ inline void InitApi() {

#if !defined(__ANDROID__)
if (ort_lib_handle == nullptr) {
const std::array<std::string, 4> target_libraries = {
std::string("libonnxruntime.so"),
std::string("libonnxruntime.so.1.18.0"),
std::string("libonnxruntime.so.1.19.0"),
std::string("libonnxruntime.so.1.20.0")};

// Search parent directory
std::string current_module_dir = GetCurrentModuleDir();
for (const std::string& lib_name : target_libraries) {
std::string pip_path{current_module_dir + "/" + lib_name};
ort_lib_handle = LoadDynamicLibraryIfExists(pip_path);
if (ort_lib_handle != nullptr) {
break;
}
}

if (ort_lib_handle == nullptr) {
// Search for pip installation
for (const std::string& lib_name : target_libraries) {
std::string pip_path{current_module_dir + "/../onnxruntime/capi/" + lib_name};
ort_lib_handle = LoadDynamicLibraryIfExists(pip_path);
if (ort_lib_handle != nullptr) {
break;
}
}
}
ort_lib_handle = LoadDynamicLibraryIfExists("libonnxruntime.so.1");
}
#endif

Expand Down
3 changes: 0 additions & 3 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ if(NOT (CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "Linu
target_link_libraries(python PRIVATE ${ONNXRUNTIME_LIB})
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_property(TARGET python APPEND_STRING PROPERTY LINK_FLAGS " -Xlinker -rpath=\\$ORIGIN/../onnxruntime/capi/")
endif()
set_target_properties(python PROPERTIES OUTPUT_NAME "onnxruntime_genai")

if(CMAKE_GENERATOR_TOOLSET MATCHES "Visual Studio")
Expand Down
27 changes: 25 additions & 2 deletions src/python/py/_dll_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ def _is_windows():
return sys.platform.startswith("win")


def _is_linux():
return sys.platform.startswith("linux")


def add_onnxruntime_dependency():
"""Add the onnxruntime DLL directory to the DLL search path.
"""Add the onnxruntime shared library dependency.
This function is a no-op on non-Windows platforms.
On Windows, this function adds the onnxruntime DLL directory to the DLL search path.
On Linux, this function loads the onnxruntime shared library and its dependencies
so that they can be found by the dynamic linker.
"""
if _is_windows():
import importlib.util
Expand All @@ -20,6 +26,23 @@ def add_onnxruntime_dependency():
raise ImportError("Could not find the onnxruntime package.")
ort_package_path = ort_package.submodule_search_locations[0]
os.add_dll_directory(os.path.join(ort_package_path, "capi"))
elif _is_linux():
import importlib.util
import ctypes
import glob

ort_package = importlib.util.find_spec("onnxruntime")
if not ort_package:
raise ImportError("Could not find the onnxruntime package.")

# Load the onnxruntime shared library here since we can find the path in python with ease.
# This avoids needing to know the exact path of the shared library from native code.
ort_package_path = ort_package.submodule_search_locations[0]
ort_lib_path = glob.glob(os.path.join(ort_package_path, "capi", "libonnxruntime.so*"))
if not ort_lib_path:
raise ImportError("Could not find the onnxruntime shared library.")

_ = ctypes.CDLL(ort_lib_path[0])


def add_cuda_dependency():
Expand Down

0 comments on commit 7e29b42

Please sign in to comment.