Skip to content

Commit 8222c24

Browse files
committed
Add rpath to libcustom_ops_aot_lib.dylib and libquantized_ops_aot_lib.dylib
Summary: As titled. This issue is from https://github.com/pytorch/torchchat/actions/runs/11523122333/job/32080481174?pr=1312 In that job when we try to load `libcustom_ops_aot_lib.dylib` into python, it complains that it can't find `_portable_lib.cpython-310-darwin.so`. This PR is trying to fix it by adding the relative path to `_portable_lib.cpython-310-darwin.so` into `LC_RPATH`. Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 5889cc3 commit 8222c24

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

extension/llm/custom_ops/CMakeLists.txt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ target_include_directories(custom_ops PUBLIC "${_common_include_directories}")
5959
target_include_directories(
6060
custom_ops PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../../../include"
6161
)
62-
target_link_libraries(
63-
custom_ops PUBLIC ${custom_ops_libs} executorch_core
64-
)
62+
target_link_libraries(custom_ops PUBLIC ${custom_ops_libs} executorch_core)
6563

6664
target_compile_options(
6765
custom_ops PUBLIC ${_common_compile_options} -DET_USE_THREADPOOL
@@ -74,7 +72,8 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
7472
find_package(Torch CONFIG REQUIRED)
7573
add_library(
7674
custom_ops_aot_lib SHARED
77-
${_custom_ops__srcs} ${CMAKE_CURRENT_SOURCE_DIR}/op_sdpa_aot.cpp
75+
${_custom_ops__srcs}
76+
${CMAKE_CURRENT_SOURCE_DIR}/op_sdpa_aot.cpp
7877
${CMAKE_CURRENT_SOURCE_DIR}/op_fast_hadamard_transform_aten.cpp
7978
${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop.cpp
8079
${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop_aot.cpp
@@ -110,5 +109,26 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
110109
${_common_compile_options} -DET_USE_THREADPOOL
111110
)
112111

112+
# pip wheels will need to be able to find the dependent libraries. On Linux,
113+
# the .so has non-absolute dependencies on libs like "_portable_lib.so"
114+
# without paths; as long as we `import torch` first, those dependencies will
115+
# work. But Apple dylibs do not support non-absolute dependencies, so we need
116+
# to tell the loader where to look for its libraries. The LC_LOAD_DYLIB
117+
# entries for the portable_lib libraries will look like
118+
# "@rpath/_portable_lib.cpython-310-darwin.so", so we can add an LC_RPATH
119+
# entry to look in a directory relative to the installed location of our
120+
# _portable_lib.so file. To see these LC_* values, run `otool -l
121+
# libcustom_ops_aot_lib.dylib`.
122+
if(APPLE)
123+
set_target_properties(
124+
custom_ops_aot_lib
125+
PROPERTIES # Assume this library will be installed in
126+
# <site-packages>/executorch/extension/llm/custom_ops/, and the
127+
# _portable_lib.so is installed in
128+
# <site-packages>/executorch/extension/pybindings/
129+
BUILD_RPATH "@loader_path/../../pybindings"
130+
INSTALL_RPATH "@loader_path/../../pybindings"
131+
)
132+
endif()
113133
install(TARGETS custom_ops_aot_lib DESTINATION lib)
114134
endif()

kernels/quantized/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ if(NOT CMAKE_GENERATOR STREQUAL "Xcode"
114114
target_link_libraries(
115115
quantized_ops_aot_lib PUBLIC quantized_ops_pybind_lib
116116
)
117+
118+
# pip wheels will need to be able to find the dependent libraries. On
119+
# Linux, the .so has non-absolute dependencies on libs like
120+
# "_portable_lib.so" without paths; as long as we `import torch` first,
121+
# those dependencies will work. But Apple dylibs do not support
122+
# non-absolute dependencies, so we need to tell the loader where to look
123+
# for its libraries. The LC_LOAD_DYLIB entries for the portable_lib
124+
# libraries will look like "@rpath/_portable_lib.cpython-310-darwin.so",
125+
# so we can add an LC_RPATH entry to look in a directory relative to the
126+
# installed location of our _portable_lib.so file. To see these LC_*
127+
# values, run `otool -l libquantized_ops_lib.dylib`.
128+
if(APPLE)
129+
set_target_properties(
130+
quantized_ops_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+
)
138+
endif()
117139
endif()
118140
endif()
119141
endif()

0 commit comments

Comments
 (0)