Skip to content

Commit 5fe8be1

Browse files
committed
[pybind] Fix rpath for custom ops
Summary: This is a followup to #7096. That PR was aiming to rely on `CMAKE_INSTALL_RPATH_USE_LINK_PATH` to set RPATH automatically. However due to the caveat that we are not installing `_portable_lib.so` into the correct path in CMake stage (we move the .so in setup.py after it's installed), we are not able to add the correct RPATH to libcustom_ops_aot_lib.so. This PR still set the INSTALL_RPATH manually and adds TODOs to fix it later. Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 0a12e33 commit 5fe8be1

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

CMakeLists.txt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ if(NOT CMAKE_BUILD_TYPE)
5656
set(CMAKE_BUILD_TYPE Debug)
5757
endif()
5858

59+
# Setup RPATH.
60+
# See https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
61+
# Use separate rpaths during build and install phases
62+
set(CMAKE_SKIP_BUILD_RPATH OFF)
63+
# Don't use the install-rpath during the build phase
64+
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
65+
# Automatically add all linked folders that are NOT in the build directory to
66+
# the rpath (per library?)
67+
# TODO: Doesn't work for us right now because we are not installing .so's into the
68+
# correct locations. For example we have libcustom_ops_aot_lib.so depending on
69+
# _portable_lib.so, which was eventually put under <site-packages>/executorch/extension/pybindings/
70+
# but this rpath is not automatically added because at build time it seems `portable_lib`
71+
# is being built under the same directory, so no extra rpath is being added. To
72+
# properly fix this we need to install `portable_lib` into the correct path.
73+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
5974
# ------------------------------ OPTIONS -------------------------------------
6075
# WARNING: Please don't add example specific options in this CMakeLists.txt.
6176
# Instead please use `find_package(executorch REQUIRED)` in the example
@@ -682,22 +697,6 @@ if(EXECUTORCH_BUILD_PTHREADPOOL
682697
endif()
683698

684699
if(EXECUTORCH_BUILD_PYBIND)
685-
# Setup RPATH.
686-
# See https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
687-
if(APPLE)
688-
set(CMAKE_MACOSX_RPATH ON)
689-
set(_rpath_portable_origin "@loader_path")
690-
else()
691-
set(_rpath_portable_origin $ORIGIN)
692-
endif(APPLE)
693-
# Use separate rpaths during build and install phases
694-
set(CMAKE_SKIP_BUILD_RPATH FALSE)
695-
# Don't use the install-rpath during the build phase
696-
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
697-
set(CMAKE_INSTALL_RPATH "${_rpath_portable_origin}")
698-
# Automatically add all linked folders that are NOT in the build directory to
699-
# the rpath (per library?)
700-
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
701700
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11)
702701

703702
if(NOT EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)

extension/llm/custom_ops/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,21 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
8484
target_include_directories(
8585
custom_ops_aot_lib PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../../../include"
8686
)
87+
# TODO: This only works if we install portable_lib.so to
88+
# <site-packages>/executorch/extension/pybindings/.
89+
if(APPLE)
90+
set(RPATH "@loader_path/../../pybindings")
91+
else()
92+
set(RPATH "$ORIGIN/../../pybindings")
93+
endif()
94+
set_target_properties(custom_ops_aot_lib PROPERTIES INSTALL_RPATH ${RPATH})
8795
if(TARGET portable_lib)
8896
# If we have portable_lib built, custom_ops_aot_lib gives the ability to use
8997
# the ops in PyTorch and ExecuTorch through pybind
9098
target_link_libraries(custom_ops_aot_lib PUBLIC portable_lib)
9199
else()
92100
# If no portable_lib, custom_ops_aot_lib still gives the ability to use the
93-
# ops in PyTorch
101+
# ops in PyTorchk
94102
target_link_libraries(custom_ops_aot_lib PUBLIC executorch_core)
95103
endif()
96104

@@ -109,5 +117,7 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
109117
${_common_compile_options} -DET_USE_THREADPOOL
110118
)
111119

112-
install(TARGETS custom_ops_aot_lib DESTINATION lib)
120+
install(TARGETS custom_ops_aot_lib
121+
LIBRARY DESTINATION executorch/extension/llm/custom_ops
122+
)
113123
endif()

kernels/quantized/CMakeLists.txt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,14 @@ if(NOT CMAKE_GENERATOR STREQUAL "Xcode"
126126
# installed location of our _portable_lib.so file. To see these LC_*
127127
# values, run `otool -l libquantized_ops_lib.dylib`.
128128
if(APPLE)
129-
set_target_properties(
130-
quantized_ops_aot_lib
131-
PROPERTIES # Assume this library will be installed in
132-
# <site-packages>/executorch/kernels/quantized/, and the
133-
# _portable_lib.so is installed in
134-
# <site-packages>/executorch/extension/pybindings/
135-
BUILD_RPATH "@loader_path/../../extensions/pybindings"
136-
INSTALL_RPATH "@loader_path/../../extensions/pybindings"
137-
)
129+
set(RPATH "@loader_path/../../extensions/pybindings")
130+
else()
131+
set(RPATH "$ORIGIN/../../extensions/pybindings")
138132
endif()
133+
set_target_properties(
134+
quantized_ops_aot_lib PROPERTIES BUILD_RPATH ${RPATH} INSTALL_RPATH
135+
${RPATH}
136+
)
139137
endif()
140138
endif()
141139
endif()

0 commit comments

Comments
 (0)