From e4960df51afbdbe541ec4909cfc262291ac6d965 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Tue, 10 Jun 2025 18:17:20 -0700 Subject: [PATCH 1/3] [llm] Fix llama/README.md This PR simplifies README.md's cmake command by using preset. --- examples/models/llama/README.md | 22 +++------------------- extension/llm/runner/CMakeLists.txt | 2 ++ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/examples/models/llama/README.md b/examples/models/llama/README.md index 38eb195fcd4..c6f0350fff7 100644 --- a/examples/models/llama/README.md +++ b/examples/models/llama/README.md @@ -269,18 +269,7 @@ You can export and run the original Llama 3 8B instruct model. 1. Build executorch with optimized CPU performance as follows. Build options available [here](https://github.com/pytorch/executorch/blob/main/CMakeLists.txt#L59). ``` - cmake -DPYTHON_EXECUTABLE=python \ - -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DEXECUTORCH_ENABLE_LOGGING=1 \ - -DCMAKE_BUILD_TYPE=Release \ - -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ - -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ - -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \ - -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ - -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ - -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ - -Bcmake-out . + cmake --preset llm -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=cmake-out cmake --build cmake-out -j16 --target install --config Release ``` @@ -288,14 +277,9 @@ Note for Mac users: There's a known linking issue with Xcode 15.1. Refer to the 2. Build llama runner. ``` - cmake -DPYTHON_EXECUTABLE=python \ - -DCMAKE_INSTALL_PREFIX=cmake-out \ + cmake -DCMAKE_INSTALL_PREFIX=cmake-out \ + -DBUILD_TESTING=OFF \ -DCMAKE_BUILD_TYPE=Release \ - -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ - -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ - -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ - -DSUPPORT_REGEX_LOOKAHEAD=ON -Bcmake-out/examples/models/llama \ examples/models/llama diff --git a/extension/llm/runner/CMakeLists.txt b/extension/llm/runner/CMakeLists.txt index 05f47e55c74..cade4b51a63 100644 --- a/extension/llm/runner/CMakeLists.txt +++ b/extension/llm/runner/CMakeLists.txt @@ -44,6 +44,8 @@ target_include_directories( add_library(extension_llm_runner STATIC ${_extension_llm_runner__srcs}) # add tokenizers +set(SUPPORT_REGEX_LOOKAHEAD ON) +set(PCRE2_STATIC_PIC ON) add_subdirectory( ${EXECUTORCH_ROOT}/extension/llm/tokenizers ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/tokenizers From a63046ac5293919a7419bc9ef8a7837f9efef812 Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Wed, 11 Jun 2025 09:16:04 -0700 Subject: [PATCH 2/3] Remove PIC --- extension/llm/runner/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/extension/llm/runner/CMakeLists.txt b/extension/llm/runner/CMakeLists.txt index cade4b51a63..798f0f14085 100644 --- a/extension/llm/runner/CMakeLists.txt +++ b/extension/llm/runner/CMakeLists.txt @@ -45,7 +45,6 @@ add_library(extension_llm_runner STATIC ${_extension_llm_runner__srcs}) # add tokenizers set(SUPPORT_REGEX_LOOKAHEAD ON) -set(PCRE2_STATIC_PIC ON) add_subdirectory( ${EXECUTORCH_ROOT}/extension/llm/tokenizers ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/tokenizers From cf99195eff2a10a67e5a902d770edc88a9bab26b Mon Sep 17 00:00:00 2001 From: Mengwei Liu Date: Wed, 11 Jun 2025 09:52:44 -0700 Subject: [PATCH 3/3] Add -fPIC --- .ci/scripts/test_llama.sh | 3 +-- extension/llm/runner/CMakeLists.txt | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.ci/scripts/test_llama.sh b/.ci/scripts/test_llama.sh index 88fedabba27..9f183528719 100644 --- a/.ci/scripts/test_llama.sh +++ b/.ci/scripts/test_llama.sh @@ -156,8 +156,7 @@ cmake_install_executorch_libraries() { -DCMAKE_INSTALL_PREFIX=cmake-out \ -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \ -DEXECUTORCH_BUILD_QNN="$QNN" \ - -DQNN_SDK_ROOT="$QNN_SDK_ROOT" \ - -Bcmake-out . + -DQNN_SDK_ROOT="$QNN_SDK_ROOT" cmake --build cmake-out -j9 --target install --config "$CMAKE_BUILD_TYPE" } diff --git a/extension/llm/runner/CMakeLists.txt b/extension/llm/runner/CMakeLists.txt index 798f0f14085..7f3f8ad1519 100644 --- a/extension/llm/runner/CMakeLists.txt +++ b/extension/llm/runner/CMakeLists.txt @@ -45,6 +45,9 @@ add_library(extension_llm_runner STATIC ${_extension_llm_runner__srcs}) # add tokenizers set(SUPPORT_REGEX_LOOKAHEAD ON) +# llama/runner/CMakeLists.txt builds a shared library libllama_runner.so that +# transitively depends on tokenizers. Need to build tokenizers with -fPIC. +set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_subdirectory( ${EXECUTORCH_ROOT}/extension/llm/tokenizers ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/tokenizers @@ -55,8 +58,9 @@ set(runner_deps executorch_core extension_module extension_tensor tokenizers) target_link_libraries(extension_llm_runner PUBLIC ${runner_deps}) target_include_directories( - extension_llm_runner INTERFACE ${_common_include_directories} - ${EXECUTORCH_ROOT}/extension/llm/tokenizers/include + extension_llm_runner + INTERFACE ${_common_include_directories} + ${EXECUTORCH_ROOT}/extension/llm/tokenizers/include ) if(BUILD_TESTING)