From e3802362234fc8eedbcebc57bfe83c8bd2dc6e8d Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Fri, 18 Oct 2024 16:18:48 -0700 Subject: [PATCH 1/2] [pybind] Add back extension suffix for portable lib shared object in wheel Summary: Initially our portable lib prebuilt library is named as: ``` _portable_lib.cpython-310-x86_64-linux-gnu.so ``` Where it includes an `EXT_SUFFIX` `cpython-310-x86_64-linux-gnu` consists of architecture and build OS information. This is enforced by `setuptools` following PEP 3149 and there's no good way to change this behavior. which is easier to be found by `find_package()` macro in CMake. However #5961 is breaking the other prebuilt libraries such as `libcustom_ops_aot_lib.so` which depends on the original `_portable_lib.cpython-310-x86_64-linux-gnu.so` name in its RPATH. This PR is a fix that reverts #5961 and restore the full name during packaging, but try to match the `EXT_SUFFIX` in CMake to be able to find the .so file. Test Plan: ```bash python -c "from executorch.extension.llm.custom_ops import sdpa_with_kv_cache" ``` Does not throw `_portable_lib.cpython-310-x86_64-linux-gnu.so` not found error. Reviewers: Subscribers: Tasks: Tags: --- build/executorch-wheel-config.cmake | 15 +++++++++++++-- setup.py | 4 ---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/build/executorch-wheel-config.cmake b/build/executorch-wheel-config.cmake index 239fff67c1d..5d4b7066144 100644 --- a/build/executorch-wheel-config.cmake +++ b/build/executorch-wheel-config.cmake @@ -21,9 +21,20 @@ # cmake_minimum_required(VERSION 3.19) -# Find prebuilt _portable_lib.so. This file should be installed under +# Find prebuilt _portable_lib..so. This file should be installed under # /executorch/share/cmake -find_library(_portable_lib_LIBRARY _portable_lib.so PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/") + +# Get the Python version and platform information +execute_process( + COMMAND python -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))" + OUTPUT_VARIABLE EXT_SUFFIX + OUTPUT_STRIP_TRAILING_WHITESPACE +) +find_library( + _portable_lib_LIBRARY + NAMES _portable_lib${EXT_SUFFIX} + PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/" +) set(EXECUTORCH_LIBRARIES) set(EXECUTORCH_FOUND OFF) if(_portable_lib_LIBRARY) diff --git a/setup.py b/setup.py index ef027b451c3..ff1afa89bd6 100644 --- a/setup.py +++ b/setup.py @@ -687,10 +687,6 @@ def get_ext_modules() -> List[Extension]: return ext_modules -# Override extension suffix to be ".so", skipping package info such as -# "cpython-311-darwin" -os.environ["SETUPTOOLS_EXT_SUFFIX"] = ".so" - setup( version=Version.string(), # TODO(dbort): Could use py_modules to restrict the set of modules we From 8885cdbb0c3723e4416b0e2992945e03c6a0414d Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Fri, 18 Oct 2024 17:14:16 -0700 Subject: [PATCH 2/2] Address comments Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: --- build/executorch-wheel-config.cmake | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/build/executorch-wheel-config.cmake b/build/executorch-wheel-config.cmake index 5d4b7066144..d4f3e12ecc8 100644 --- a/build/executorch-wheel-config.cmake +++ b/build/executorch-wheel-config.cmake @@ -24,17 +24,38 @@ cmake_minimum_required(VERSION 3.19) # Find prebuilt _portable_lib..so. This file should be installed under # /executorch/share/cmake +# Find python +if(DEFINED ENV{CONDA_DEFAULT_ENV} AND NOT $ENV{CONDA_DEFAULT_ENV} STREQUAL "base") + set(PYTHON_EXECUTABLE + python + ) +else() + set(PYTHON_EXECUTABLE + python3 + ) +endif() + # Get the Python version and platform information execute_process( - COMMAND python -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))" + COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))" OUTPUT_VARIABLE EXT_SUFFIX + RESULT_VARIABLE SYSCONFIG_RESULT + ERROR_VARIABLE SYSCONFIG_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ) + +if(RESULT_VARIABLE EQUAL 0) + message(STATUS "Sysconfig extension suffix: ${EXT_SUFFIX}") +else() + message(FATAL_ERROR "Failed to retrieve sysconfig config var EXT_SUFFIX: ${SYSCONFIG_ERROR}") +endif() + find_library( _portable_lib_LIBRARY NAMES _portable_lib${EXT_SUFFIX} PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/" ) + set(EXECUTORCH_LIBRARIES) set(EXECUTORCH_FOUND OFF) if(_portable_lib_LIBRARY)