Skip to content

Commit 2c4f7ba

Browse files
mergennachinGregoryComer
authored andcommitted
RPATH Fix for portable_lib Python Extension (pytorch#13254)
RPATH Fix for portable_lib Python Extension Problem: The _portable_lib.so Python extension built on CI couldn't find PyTorch libraries when installed locally because it had hardcoded absolute paths from the CI build environment. Error: ImportError: dlopen(.../_portable_lib.cpython-311-darwin.so, 0x0002): Library not loaded: @rpath/libtorch_python.dylib Referenced from: .../executorch/extension/pybindings/_portable_lib.cpython-311-darwin.so Reason: tried: '/Users/runner/work/_temp/.../torch/lib/libtorch_python.dylib' (no such file) Root Cause: The CMake build was linking to PyTorch libraries using absolute paths from the build environment, without setting proper relative RPATHs for runtime library resolution. Solution: Added platform-specific relative RPATH settings to the portable_lib target in /Users/mnachin/executorch/CMakeLists.txt (lines 657-669): - macOS: Uses @loader_path/../../../torch/lib to find PyTorch libraries relative to the .so file location - Linux: Uses $ORIGIN/../../../torch/lib for the same purpose - Sets both BUILD_RPATH and INSTALL_RPATH to ensure consistency Impact: This allows the wheel-packaged _portable_lib.so to find PyTorch libraries regardless of the installation location, fixing the runtime linking issue when using ExecutorTorch wheels built on CI. Note: The same fix may be needed for _training_lib if it experiences similar issues. Test Plan: ``` # Build the wheel locally python setup.py bdist_wheel # create fresh conda env conda create -yn executorch_test_11 python=3.11.0 && conda activate executorch_test_11 # install pip install ./dist/executorch-*.whl # Verify python -c "from executorch.extension.pybindings._portable_lib import _load_for_executorch; print('Success!')" ```
1 parent 0f22062 commit 2c4f7ba

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

.ci/scripts/wheel/test_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class ModelTest:
4141

4242

4343
def run_tests(model_tests: List[ModelTest]) -> None:
44+
# Test that we can import the portable_lib module - verifies RPATH is correct
45+
print("Testing portable_lib import...")
46+
try:
47+
from executorch.extension.pybindings._portable_lib import ( # noqa: F401
48+
_load_for_executorch,
49+
)
50+
51+
print("✓ Successfully imported _load_for_executorch from portable_lib")
52+
except ImportError as e:
53+
print(f"✗ Failed to import portable_lib: {e}")
54+
raise
55+
4456
# Why are we doing this envvar shenanigans? Since we build the testers, which
4557
# uses buck, we cannot run as root. This is a sneaky of getting around that
4658
# test.

CMakeLists.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,23 @@ if(EXECUTORCH_BUILD_PYBIND)
869869
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
870870
target_link_libraries(portable_lib PRIVATE ${_dep_libs})
871871

872-
install(
873-
TARGETS portable_lib
874-
EXPORT ExecuTorchTargets
875-
LIBRARY DESTINATION executorch/extension/pybindings
872+
# Set RPATH to find PyTorch libraries relative to the installation location
873+
# This goes from executorch/extension/pybindings up to site-packages, then to torch/lib
874+
if(APPLE)
875+
set_target_properties(portable_lib PROPERTIES
876+
BUILD_RPATH "@loader_path/../../../torch/lib"
877+
INSTALL_RPATH "@loader_path/../../../torch/lib"
878+
)
879+
else()
880+
set_target_properties(portable_lib PROPERTIES
881+
BUILD_RPATH "$ORIGIN/../../../torch/lib"
882+
INSTALL_RPATH "$ORIGIN/../../../torch/lib"
883+
)
884+
endif()
885+
886+
install(TARGETS portable_lib
887+
EXPORT ExecuTorchTargets
888+
LIBRARY DESTINATION executorch/extension/pybindings
876889
)
877890
endif()
878891

0 commit comments

Comments
 (0)