Skip to content

Commit 23ef028

Browse files
author
Github Executorch
committed
Reuse GELU implementation from PyTorch core
Pull Request resolved: #7041 kernels/optimized doesn't need to support embedded systems, so it can just take a header-only dep on PyTorch. Note that, because we will pick up Sleef internally and ignore it externally thanks to ATen vec, this PR gets to enable optimized GELU in OSS. Testing: CI to make sure this doesn't break mobile build modes; happy to take advice on anything not currently covered that might break. ghstack-source-id: 265149952 @exported-using-ghexport Differential Revision: [D66335522](https://our.internmc.facebook.com/intern/diff/D66335522/)
1 parent 34f3ab0 commit 23ef028

File tree

17 files changed

+102
-53
lines changed

17 files changed

+102
-53
lines changed

.ci/scripts/build_llama_android.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ set -exu
1010
# shellcheck source=/dev/null
1111
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
1212

13+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
14+
PYTHON_EXECUTABLE=python3
15+
fi
16+
which "${PYTHON_EXECUTABLE}"
17+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
18+
1319
install_executorch_and_backend_lib() {
1420
echo "Installing executorch and xnnpack backend"
1521
clean_executorch_install_folders
@@ -22,6 +28,7 @@ install_executorch_and_backend_lib() {
2228
-DANDROID_ABI="${ANDROID_ABI}" \
2329
-DCMAKE_INSTALL_PREFIX=cmake-android-out \
2430
-DCMAKE_BUILD_TYPE=Release \
31+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
2532
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
2633
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
2734
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
@@ -47,6 +54,7 @@ build_llama_runner() {
4754
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
4855
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
4956
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
57+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
5058
-Bcmake-android-out/examples/models/llama examples/models/llama
5159

5260
cmake --build cmake-android-out/examples/models/llama -j4 --config Release

.ci/scripts/test_llama.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ cmake_install_executorch_libraries() {
154154
rm -rf cmake-out
155155
retry cmake \
156156
-DCMAKE_INSTALL_PREFIX=cmake-out \
157+
-DCMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" \
157158
-DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
158159
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
159160
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \

.ci/scripts/test_llava.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ fi
3030
NPROC=8
3131
if hash nproc &> /dev/null; then NPROC=$(nproc); fi
3232

33+
python_lib=$($PYTHON_EXECUTABLE -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
34+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
3335
EXECUTORCH_COMMON_CMAKE_ARGS=" \
3436
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
35-
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
37+
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
3638
-DEXECUTORCH_ENABLE_LOGGING=ON \
3739
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
3840
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
@@ -46,6 +48,7 @@ EXECUTORCH_COMMON_CMAKE_ARGS=" \
4648
cmake_install_executorch_libraries() {
4749
cmake \
4850
${EXECUTORCH_COMMON_CMAKE_ARGS} \
51+
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \
4952
-B${BUILD_DIR} .
5053

5154
cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE}
@@ -56,6 +59,7 @@ cmake_install_executorch_libraries_for_android() {
5659
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
5760
-DANDROID_ABI=arm64-v8a \
5861
${EXECUTORCH_COMMON_CMAKE_ARGS} \
62+
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \
5963
-B${BUILD_DIR} .
6064

6165
cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE}
@@ -76,7 +80,7 @@ cmake_build_llava_runner() {
7680

7781
cmake \
7882
${LLAVA_COMMON_CMAKE_ARGS} \
79-
-DCMAKE_PREFIX_PATH="$python_lib" \
83+
-DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \
8084
-B${BUILD_DIR}/${dir} \
8185
${dir}
8286

@@ -92,7 +96,7 @@ cmake_build_llava_runner_for_android() {
9296
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
9397
-DANDROID_ABI=arm64-v8a \
9498
${LLAVA_COMMON_CMAKE_ARGS} \
95-
-DCMAKE_PREFIX_PATH="$python_lib" \
99+
-DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \
96100
-DLLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE=ON \
97101
-B${BUILD_DIR}/${dir} \
98102
${dir}

.ci/scripts/test_model.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ prepare_artifacts_upload() {
5050

5151
build_cmake_executor_runner() {
5252
echo "Building executor_runner"
53+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
5354
rm -rf ${CMAKE_OUTPUT_DIR}
5455
cmake -DCMAKE_BUILD_TYPE=Debug \
5556
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
5657
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
58+
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
5759
-B${CMAKE_OUTPUT_DIR} .
5860

5961
cmake --build ${CMAKE_OUTPUT_DIR} -j4 --config Debug
@@ -98,8 +100,7 @@ test_model() {
98100

99101
build_cmake_xnn_executor_runner() {
100102
echo "Building xnn_executor_runner"
101-
SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
102-
CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch"
103+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
103104

104105
(rm -rf ${CMAKE_OUTPUT_DIR} \
105106
&& mkdir ${CMAKE_OUTPUT_DIR} \

.ci/scripts/test_phi_3_mini.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ NPROC=8
2222
if hash nproc &> /dev/null; then NPROC=$(nproc); fi
2323

2424
cmake_install_executorch_libraries() {
25+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
2526
cmake -DPYTHON_EXECUTABLE=python \
2627
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
28+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
2729
-DEXECUTORCH_ENABLE_LOGGING=1 \
2830
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
2931
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
@@ -39,8 +41,10 @@ cmake_install_executorch_libraries() {
3941
}
4042

4143
cmake_build_phi_3_mini() {
44+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
4245
cmake -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
4346
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
47+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
4448
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
4549
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
4650
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \

.ci/scripts/utils.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ cmake_install_executorch_lib() {
136136
clean_executorch_install_folders
137137
retry cmake -DBUCK2="$BUCK" \
138138
-DCMAKE_INSTALL_PREFIX=cmake-out \
139+
-DCMAKE_PREFIX_PATH="$($PYTHON_EXECUTABLE -c 'import torch as _; print(_.__path__[0])')" \
139140
-DCMAKE_BUILD_TYPE=Release \
140141
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
141142
-Bcmake-out .

.github/workflows/pull.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ jobs:
147147
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
148148
conda activate "${CONDA_ENV}"
149149
150+
source .ci/scripts/utils.sh
151+
install_executorch "use-pt-pinned-commit"
150152
BUILD_TOOL="cmake"
151153
PYTHON_EXECUTABLE=python \
152154
bash .ci/scripts/build_llama_android.sh "${BUILD_TOOL}"

.github/workflows/trunk.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ jobs:
394394
rm -rf cmake-out
395395
cmake \
396396
-DCMAKE_INSTALL_PREFIX=cmake-out \
397+
-DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \
397398
-DCMAKE_BUILD_TYPE=Release \
398399
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
399400
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
@@ -411,6 +412,7 @@ jobs:
411412
cmake \
412413
-DCMAKE_INSTALL_PREFIX=cmake-out \
413414
-DCMAKE_BUILD_TYPE=Release \
415+
-DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \
414416
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
415417
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
416418
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \

build/Utils.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,20 @@ function(resolve_python_executable)
321321
)
322322
endif()
323323
endfunction()
324+
325+
# find_package(Torch CONFIG REQUIRED) replacement for targets that
326+
# have a header-only Torch dependency. Because find_package sets
327+
# variables in the parent scope, we use a macro to preserve this
328+
# rather than maintaining our own list of those variables.
329+
macro(find_package_torch_headers)
330+
# We cannot simply use CMAKE_FIND_ROOT_PATH_BOTH, because that does
331+
# not propagate into TorchConfig.cmake.
332+
foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE)
333+
set(OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} ${CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}})
334+
set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} BOTH)
335+
endforeach()
336+
find_package(Torch CONFIG REQUIRED)
337+
foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE)
338+
set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} ${OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}})
339+
endforeach()
340+
endmacro()

build/build_android_llm_demo.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
set -ex
99

10+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
11+
PYTHON_EXECUTABLE=python3
12+
fi
13+
which "${PYTHON_EXECUTABLE}"
14+
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
15+
1016
build_jar() {
1117
pushd extension/android
1218
./gradlew build
@@ -36,6 +42,7 @@ build_android_native_library() {
3642
fi
3743

3844
cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
45+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
3946
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
4047
-DANDROID_ABI="${ANDROID_ABI}" \
4148
-DANDROID_PLATFORM=android-26 \
@@ -69,6 +76,7 @@ build_android_native_library() {
6976
-DANDROID_ABI="${ANDROID_ABI}" \
7077
-DANDROID_PLATFORM=android-26 \
7178
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
79+
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
7280
-DEXECUTORCH_ENABLE_LOGGING=ON \
7381
-DEXECUTORCH_LOG_LEVEL=Info \
7482
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \

0 commit comments

Comments
 (0)