Skip to content

Commit fae83c3

Browse files
[#6102][fix] support non-system python installation (#7763)
Signed-off-by: Yuan Tong <[email protected]>
1 parent d650320 commit fae83c3

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

cpp/tensorrt_llm/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ if(TARGET ${NIXL_WRAPPER_TARGET})
294294
endif()
295295

296296
if(NOT WIN32)
297-
set_target_properties(${SHARED_TARGET} PROPERTIES BUILD_RPATH "$ORIGIN")
297+
# Load libraries at $PREFIX/lib from
298+
# $PREFIX/lib/python3.12/site-packages/tensorrt_llm/libs
299+
set_target_properties(${SHARED_TARGET}
300+
PROPERTIES BUILD_RPATH "$ORIGIN;$ORIGIN/../../../..")
298301
endif()
299302

300303
if(BUILD_PYT)

cpp/tensorrt_llm/nanobind/CMakeLists.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ endif()
4141
target_link_libraries(
4242
${TRTLLM_NB_MODULE}
4343
PUBLIC ${SHARED_TARGET}
44-
${UNDEFINED_FLAG}
45-
${NO_AS_NEEDED_FLAG}
4644
${Python3_LIBRARIES}
4745
${TORCH_LIBRARIES}
4846
torch_python
@@ -54,10 +52,14 @@ target_compile_definitions(
5452
PYBIND11_DETAILED_ERROR_MESSAGES=1)
5553

5654
if(NOT WIN32)
57-
set_target_properties(
58-
${TRTLLM_NB_MODULE}
59-
PROPERTIES
60-
LINK_FLAGS
61-
"-Wl,-rpath,'$ORIGIN/libs' -Wl,-rpath,'$ORIGIN/../nvidia/nccl/lib' ${AS_NEEDED_FLAG} ${UNDEFINED_FLAG}"
55+
set(TRTLLM_NB_MODULE_RPATH_LIST
56+
"$ORIGIN/libs" # TRTLLM libraries
57+
"$ORIGIN/../../.." # Shared libraries under $PREFIX/lib
58+
"$ORIGIN/../nvidia/nccl/lib" # NCCL libraries
6259
)
60+
set_target_properties(${TRTLLM_NB_MODULE}
61+
PROPERTIES BUILD_RPATH "${TRTLLM_NB_MODULE_RPATH_LIST}")
62+
set_target_properties(
63+
${TRTLLM_NB_MODULE} PROPERTIES LINK_FLAGS
64+
"${AS_NEEDED_FLAG} ${UNDEFINED_FLAG}")
6365
endif()

cpp/tensorrt_llm/pybind/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ endif()
4343
target_link_libraries(
4444
${TRTLLM_PYBIND_MODULE}
4545
PUBLIC ${SHARED_TARGET}
46-
${UNDEFINED_FLAG}
47-
${NO_AS_NEEDED_FLAG}
4846
${Python3_LIBRARIES}
4947
${TORCH_LIBRARIES}
5048
torch_python
@@ -56,10 +54,15 @@ target_compile_definitions(
5654
PYBIND11_DETAILED_ERROR_MESSAGES=1)
5755

5856
if(NOT WIN32)
59-
set_target_properties(
60-
${TRTLLM_PYBIND_MODULE}
61-
PROPERTIES
62-
LINK_FLAGS
63-
"-Wl,-rpath,'$ORIGIN/libs' -Wl,-rpath,'$ORIGIN/../nvidia/nccl/lib' ${AS_NEEDED_FLAG} ${UNDEFINED_FLAG}"
57+
set(TRTLLM_PYBIND_MODULE_RPATH_LIST
58+
"$ORIGIN/libs" # TRTLLM libraries
59+
"$ORIGIN/../../.." # Shared libraries under $PREFIX/lib
60+
"$ORIGIN/../nvidia/nccl/lib" # NCCL libraries
6461
)
62+
set_target_properties(
63+
${TRTLLM_PYBIND_MODULE} PROPERTIES BUILD_RPATH
64+
"${TRTLLM_PYBIND_MODULE_RPATH_LIST}")
65+
set_target_properties(
66+
${TRTLLM_PYBIND_MODULE} PROPERTIES LINK_FLAGS
67+
"${AS_NEEDED_FLAG} ${UNDEFINED_FLAG}")
6568
endif()

tensorrt_llm/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ def _add_trt_llm_dll_directory():
2727

2828
_add_trt_llm_dll_directory()
2929

30+
31+
def _preload_python_lib():
32+
"""
33+
Preload Python library.
34+
35+
On Linux, the python executable links to libpython statically,
36+
so the dynamic library `libpython3.x.so` is not loaded.
37+
When using virtual environment on top of non-system Python installation,
38+
our libraries installed under `$VENV_PREFIX/lib/python3.x/site-packages/`
39+
have difficulties loading `$PREFIX/lib/libpython3.x.so.1.0` on their own,
40+
since venv does not symlink `libpython3.x.so` into `$VENV_PREFIX/lib/`,
41+
and the relative path from `$VENV_PREFIX` to `$PREFIX` is arbitrary.
42+
43+
We preload the libraries here since the Python executable under `$PREFIX/bin`
44+
can easily find the library.
45+
"""
46+
import platform
47+
on_linux = platform.system() == "Linux"
48+
if on_linux:
49+
import sys
50+
from ctypes import cdll
51+
v_major, v_minor, *_ = sys.version_info
52+
pythonlib = f'libpython{v_major}.{v_minor}.so'
53+
_ = cdll.LoadLibrary(pythonlib + '.1.0')
54+
_ = cdll.LoadLibrary(pythonlib)
55+
56+
57+
_preload_python_lib()
58+
3059
import sys
3160

3261
# Need to import torch before tensorrt_llm library, otherwise some shared binary files

0 commit comments

Comments
 (0)