Skip to content

Commit 6727fea

Browse files
pytorchbotGithub Executorchswolchok
authored
Reuse GELU implementation from PyTorch core (#8322)
* 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: 265190627 @exported-using-ghexport Differential Revision: [D66335522](https://our.internmc.facebook.com/intern/diff/D66335522/) * apply @huydhn's fix to new code * extra tweak from @huydhn --------- Co-authored-by: Github Executorch <github_executorch@arm.com> Co-authored-by: Scott Wolchok <swolchok@meta.com>
1 parent 148832e commit 6727fea

File tree

18 files changed

+105
-54
lines changed

18 files changed

+105
-54
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 \

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ if(BUILD_EXECUTORCH_PORTABLE_OPS)
614614
endif()
615615

616616
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
617+
# find pytorch lib here to make it available to all sub-directories
618+
find_package_torch_headers()
617619
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/optimized)
618620
endif()
619621

build/Utils.cmake

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

0 commit comments

Comments
 (0)