Skip to content

Commit edacadc

Browse files
authored
Fix _llm_runner.so RPATH for pip wheel installs (#17772)
Summary from executorch.extension.llm.runner import TextLLMRunner fails on pip wheel installs because _llm_runner.so cannot find _portable_lib.so or its transitive libtorch*.so dependencies at runtime. Root cause — two issues: 1. Missing torch preload: __init__.py imported torch after _llm_runner, so libtorch*.so shared libs weren't loaded when the dynamic linker tried to resolve _llm_runner.so's dependencies. The existing portable_lib.py already solves this correctly by importing torch first. 2. Incomplete RPATH + wrong RPATH property: The RPATH on _llm_runner.so only included ../../pybindings (to find _portable_lib.so) but was missing ../../../../torch/lib (to find libtorch*.so). More critically, only INSTALL_RPATH was set — but pip install never runs cmake --install; it copies .so files directly from the build directory, so only BUILD_RPATH takes effect. The portable_lib target already handles this correctly by setting both properties. Fixes: - extension/llm/runner/__init__.py: Move import torch before the _llm_runner import to preload libtorch shared libs. - extension/llm/runner/CMakeLists.txt: Add torch/lib to the RPATH, and set both BUILD_RPATH and INSTALL_RPATH so the paths are embedded regardless of whether the .so is copied from the build tree or installed via CMake. Test plan - Build from source with EXECUTORCH_BUILD_PYBIND=ON - Remove build directories (pip-out/, cmake-out/) to simulate a clean pip install environment - Verify RPATH with otool -l _llm_runner*.so | grep -A2 RPATH (macOS) — confirms @loader_path/../../pybindings and @loader_path/../../../../torch/lib are present - Verify from executorch.extension.llm.runner import TextLLMRunner succeeds
1 parent cfb8383 commit edacadc

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

extension/llm/runner/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ if(EXECUTORCH_BUILD_PYBIND)
123123
CXX_STANDARD 20
124124
)
125125
if(APPLE)
126-
set(RPATH "@loader_path/../../pybindings")
126+
set(RPATH
127+
"@loader_path/../../pybindings;@loader_path/../../../../torch/lib"
128+
)
127129
else()
128-
set(RPATH "$ORIGIN/../../pybindings")
130+
set(RPATH "$ORIGIN/../../pybindings:$ORIGIN/../../../../torch/lib")
129131
endif()
130-
set_target_properties(_llm_runner PROPERTIES INSTALL_RPATH ${RPATH})
132+
set_target_properties(
133+
_llm_runner PROPERTIES BUILD_RPATH "${RPATH}" INSTALL_RPATH "${RPATH}"
134+
)
131135
# Add include directories
132136
target_include_directories(
133137
_llm_runner PRIVATE ${_common_include_directories} ${TORCH_INCLUDE_DIRS}

extension/llm/runner/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
enabling processing of mixed inputs (text, images, audio) and text generation.
1212
"""
1313

14+
import torch # preload libtorch shared libs for _llm_runner
15+
1416
try:
1517
# Import shared components from the compiled C++ extension
1618
from executorch.extension.llm.runner._llm_runner import ( # noqa: F401
@@ -35,7 +37,6 @@
3537
import logging
3638
from typing import Callable, List, Optional, Union
3739

38-
import torch
3940
from transformers.feature_extraction_utils import BatchFeature
4041

4142

0 commit comments

Comments
 (0)