diff --git a/.github/workflows/linux-cpu-arm64-build.yml b/.github/workflows/linux-cpu-arm64-build.yml index 26b749c5e..5d8fe12b4 100644 --- a/.github/workflows/linux-cpu-arm64-build.yml +++ b/.github/workflows/linux-cpu-arm64-build.yml @@ -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: | diff --git a/.github/workflows/linux-cpu-x64-build.yml b/.github/workflows/linux-cpu-x64-build.yml index 7b7e08c7b..924604930 100644 --- a/.github/workflows/linux-cpu-x64-build.yml +++ b/.github/workflows/linux-cpu-x64-build.yml @@ -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: | diff --git a/.github/workflows/linux-gpu-x64-build.yml b/.github/workflows/linux-gpu-x64-build.yml index 3006b750f..c7d7efd7f 100644 --- a/.github/workflows/linux-gpu-x64-build.yml +++ b/.github/workflows/linux-gpu-x64-build.yml @@ -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 diff --git a/src/models/onnxruntime_api.h b/src/models/onnxruntime_api.h index 2b7f3371e..e89613b66 100644 --- a/src/models/onnxruntime_api.h +++ b/src/models/onnxruntime_api.h @@ -213,32 +213,7 @@ inline void InitApi() { #if !defined(__ANDROID__) if (ort_lib_handle == nullptr) { - const std::array 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 diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index 189f4135c..6472fe3a8 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -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") diff --git a/src/python/py/_dll_directory.py b/src/python/py/_dll_directory.py index 9479003ff..d8fe005cd 100644 --- a/src/python/py/_dll_directory.py +++ b/src/python/py/_dll_directory.py @@ -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 @@ -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():