Skip to content

Commit dc2e02a

Browse files
authored
Add rpath to libcustom_ops_aot_lib.dylib and libquantized_ops_aot_lib… (#6499)
* 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: * Add smoke test * Lint
1 parent cd1a25d commit dc2e02a

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

build/packaging/smoke_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
# will fail and the process will exit.
1616
from executorch.extension.pybindings import portable_lib # usort: skip
1717

18+
# Import custom ops. This requires portable_lib to be loaded first.
19+
from executorch.extension.llm.custom_ops import ( # noqa: F401, F403
20+
sdpa_with_kv_cache,
21+
) # usort: skip
22+
23+
# Import quantized ops. This requires portable_lib to be loaded first.
24+
from executorch.kernels import quantized # usort: skip # noqa: F401, F403
25+
1826
# Import this after importing the ExecuTorch pybindings. If the pybindings
1927
# links against a different torch.so than this uses, there will be a set of
2028
# symbol comflicts; the process will either exit now, or there will be issues
@@ -75,6 +83,15 @@ def main():
7583
assert len(ops) > 0, "Empty operator list"
7684
print(f"Found {len(ops)} operators; first element '{ops[0]}'")
7785

86+
# Make sure custom ops are registered.
87+
assert (
88+
"llama::sdpa_with_kv_cache" in ops
89+
), f"sdpa_with_kv_cache not registered, Got ops: {ops}"
90+
91+
# Make sure quantized ops are registered.
92+
assert (
93+
"quantized_decomposed::add.out" in ops
94+
), f"quantized_decomposed::add.out not registered, Got ops: {ops}"
7895
# Export LinearModel to .pte data.
7996
pte_data: bytes = export_linear_model()
8097

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_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+
)
138+
endif()
117139
endif()
118140
endif()
119141
endif()

0 commit comments

Comments
 (0)