Skip to content

Commit 1f7f01e

Browse files
kimishpatelmalfet
authored andcommitted
Enable android runner with custom sdpa op (#363)
Summary: This diff - refactors install_et.sh into a bunch of utils - Uses those utils in build_android.sh to minimize duplication. - Makes sure taht we are building with custom sdpa op Test Plan: Model export python export.py --quant '{"linear:a8w4dq" : {"groupsize": 256}}' --checkpoint-path /home/kimishpatel/models/llama2/stories/stories110M.pt --params-path /home/kimishpatel/models/llama2/stories/params.json --output-pte-path /tmp/stories110m_a8w4dq.pte python utils/tokenizer.py --tokenizer-model=/tmp/tokenizer.model linux: ./scripts/install_et.sh rm -rf build/cmake-out/ cmake -S ./runner-et -B build/cmake-out -G Ninja cmake --build ./build/cmake-out ./build/cmake-out/runner_et /tmp/stories110m_a8w4dq.pte -z /tmp/tokenizer.bin -t 0 -n 120 android: ./runner-et/build_android.sh adb push ./build/cmake-out-android/runner_et /data/local/tmp/ adb push /tmp/stories110m_a8w4dq.pte /data/local/tmp/ adb push /tmp/tokenizer.bin /data/local/tmp/ adb shell "cd /data/local/tmp && ./runner_et ./stories110m_a8w4dq.pte -z ./tokenizer.bin -t 0 -n 120" Will add build commands to ci in the next PR Reviewers: Subscribers: Tasks: Tags:
1 parent ee280db commit 1f7f01e

File tree

5 files changed

+158
-77
lines changed

5 files changed

+158
-77
lines changed

export_et_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def forward(self, x, freqs_cis, mask, input_pos=None):
8080

8181

8282
def replace_attention_with_custom_sdpa_attention(module: nn.Module):
83+
from executorch.examples.models.llama2.custom_ops import sdpa_with_kv_cache # noqa
8384
for name, child in module.named_children():
8485
if isinstance(child, Attention):
8586
setattr(module, name, CustomSDPAAttention(child))

runner-et/CMakeLists.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ target_link_libraries(
4141
run PRIVATE
4242
executorch
4343
extension_module
44-
${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/lib/libextension_data_loader.a # This one gets installed in build directory by ExecuTorch
44+
${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src/executorch/${CMAKE_OUT_DIR}/extension/data_loader/libextension_data_loader.a # This one does not get installed by ExecuTorch
4545
optimized_kernels
4646
portable_kernels
4747
cpublas
@@ -58,5 +58,32 @@ target_link_options_shared_lib(xnnpack_backend)
5858
target_link_options_shared_lib(XNNPACK)
5959
target_link_options_shared_lib(pthreadpool)
6060
target_link_options_shared_lib(cpuinfo)
61-
target_link_options_shared_lib(executorch)
62-
target_link_libraries(run PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops.a>")
61+
# Not clear why linking executorch as whole-archive outside android/apple is leading
62+
# to double registration. Most likely because of linkage issues.
63+
# Will figure this out later. Until then use this.
64+
if(ANDROID OR APPLE)
65+
target_link_options_shared_lib(executorch)
66+
endif()
67+
target_link_libraries(runner_et PRIVATE
68+
"$<LINK_LIBRARY:WHOLE_ARCHIVE,${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops.a>")
69+
70+
# This one is needed for cpuinfo where it uses android specific log lib
71+
if(ANDROID)
72+
target_link_libraries(runner_et PRIVATE log)
73+
endif()
74+
75+
# Adding target_link_options_shared_lib as commented out below leads to this:
76+
#
77+
# CMake Error at Utils.cmake:22 (target_link_options):
78+
# Cannot specify link options for target
79+
# "/Users/scroy/etorch/torchchat/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a"
80+
# which is not built by this project.
81+
# Call Stack (most recent call first):
82+
# Utils.cmake:30 (macos_kernel_link_options)
83+
# CMakeLists.txt:41 (target_link_options_shared_lib)
84+
#
85+
#target_link_options_shared_lib("${TORCHCHAT_ROOT}/et-build/src/executorch/${CMAKE_OUT_DIR}/examples/models/llama2/custom_ops/libcustom_ops_lib.a") # This one does not get installed by ExecuTorch
86+
87+
# This works on mac, but appears to run into issues on linux
88+
# It is needed to solve:
89+
# E 00:00:00.055965 executorch:method.cpp:536] Missing operator: [8] llama::sdpa_with_kv_cache.out

runner-et/build_android.sh

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

88
set -ex
99

10+
source "$(dirname "${BASH_SOURCE[0]}")/../scripts/install_utils.sh"
11+
1012
if ["${ANDROID_NDK}" == ""]; then
1113
echo "Please set ANDROID_NDK enviornment variable."
1214
echo "For example it can be /Users/guest/Desktop/android-ndk-r26."
@@ -17,45 +19,24 @@ else
1719
fi
1820

1921
export CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
20-
export DANDROID_ABI=arm64-v8a
21-
export DANDROID_PLATFORM=android-23
22+
export ANDROID_ABI=arm64-v8a
23+
export ANDROID_PLATFORM=android-23
2224
export ET_BUILD_DIR="et-build-android"
2325
export CMAKE_OUT_DIR="cmake-out-android"
26+
export EXECUTORCH_BUILD_CUSTOM_OPS_AOT="OFF"
27+
export EXECUTORCH_BUILD_CUSTOM="ON"
28+
export CMAKE_OUT_DIR="cmake-out-android"
2429
# export DCMAKE_INSTALL_PREFIX=cmake-out-android
2530
#
2631

27-
install_executorch() {
28-
echo "Cloning executorch to ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src"
29-
ET_BUILD_DIR="${TORCHCHAT_ROOT}/${ET_BUILD_DIR}"
30-
rm -rf ${ET_BUILD_DIR}
31-
mkdir -p ${ET_BUILD_DIR}/src
32-
pushd ${ET_BUILD_DIR}/src
33-
git clone https://github.com/pytorch/executorch.git
34-
cd executorch
35-
git checkout viable/strict
36-
echo "Install executorch: submodule update"
37-
git submodule sync
38-
git submodule update --init
39-
40-
echo "Applying fixes"
41-
cp ${TORCHCHAT_ROOT}/scripts/fixes_et/module.cpp ${ET_BUILD_DIR}/src/executorch/extension/module/module.cpp # ET uses non-standard C++ that does not compile in GCC
42-
cp ${TORCHCHAT_ROOT}/scripts/fixes_et/managed_tensor.h ${ET_BUILD_DIR}/src/executorch/extension/runner_util/managed_tensor.h # ET is missing headers for vector/memory. This causes downstream issues when building runner-et.
43-
44-
CMAKE_OUT_DIR="cmake-out-android"
45-
echo "Building and installing C++ libraries"
46-
echo "Inside: ${PWD}"
47-
mkdir ${CMAKE_OUT_DIR}
48-
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-23 -DCMAKE_INSTALL_PREFIX=cmake-out-android -DEXECUTORCH_ENABLE_LOGGING=ON -DEXECUTORCH_LOG_LEVEL=Info -DEXECUTORCH_BUILD_OPTIMIZED=ON -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON -DEXECUTORCH_BUILD_XNNPACK=ON -S . -B ${CMAKE_OUT_DIR} -G Ninja
49-
cmake --build ${CMAKE_OUT_DIR}
50-
cmake --install ${CMAKE_OUT_DIR} --prefix ${ET_BUILD_DIR}/install
51-
popd
52-
}
53-
5432
build_runner_et() {
5533
rm -rf build/cmake-out-android
34+
echo "ET BUILD DIR IS ${ET_BUILD_DIR}"
5635
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-23 -S ./runner-et -B build/cmake-out-android -G Ninja
5736
cmake --build build/cmake-out-android/ -j16 --config Release
5837
}
5938

60-
# install_executorch
39+
find_cmake_prefix_path
40+
clone_executorch
41+
install_executorch
6142
build_runner_et

scripts/install_et.sh

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,20 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
set -exu
8+
set -ex
99

10-
install_pip_dependencies() {
11-
echo "Intalling common pip packages"
12-
13-
pip3 install wheel
14-
pip3 install "cmake>=3.19"
15-
pip3 install ninja
16-
pip3 install zstd
17-
pushd ${TORCHCHAT_ROOT}
18-
pip3 install -r ./requirements.txt
19-
popd
20-
}
21-
22-
install_executorch() {
23-
echo "Cloning executorch to ${TORCHCHAT_ROOT}/et-build/src"
24-
rm -rf ${TORCHCHAT_ROOT}/et-build
25-
mkdir -p ${TORCHCHAT_ROOT}/et-build/src
26-
pushd ${TORCHCHAT_ROOT}/et-build/src
27-
git clone https://github.com/pytorch/executorch.git
28-
cd executorch
29-
git checkout viable/strict
30-
echo "Install executorch: submodule update"
31-
git submodule sync
32-
git submodule update --init
33-
34-
echo "Building and installing python libraries"
35-
if [ "${ENABLE_ET_PYBIND}" = false ]; then
36-
echo "Not installing pybind"
37-
bash ./install_requirements.sh
38-
else
39-
echo "Installing pybind"
40-
bash ./install_requirements.sh --pybind xnnpack
41-
fi
42-
pip3 list
43-
44-
echo "Building and installing C++ libraries"
45-
echo "Inside: ${PWD}"
46-
mkdir cmake-out
47-
cmake -DCMAKE_BUILD_TYPE=Release -DEXECUTORCH_ENABLE_LOGGING=ON -DEXECUTORCH_LOG_LEVEL=Info -DEXECUTORCH_BUILD_CUSTOM=ON -DEXECUTORCH_BUILD_OPTIMIZED=ON -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON -DEXECUTORCH_BUILD_XNNPACK=ON -S . -B cmake-out -G Ninja
48-
cmake --build cmake-out
49-
cmake --install cmake-out --prefix ${TORCHCHAT_ROOT}/et-build/install
50-
popd
51-
}
10+
source "$(dirname "${BASH_SOURCE[0]}")/install_utils.sh"
5211

12+
if [ "${ET_BUILD_DIR}" == "" ]; then
13+
ET_BUILD_DIR="et-build"
14+
fi
5315

5416
ENABLE_ET_PYBIND="${1:-true}"
5517

5618
pushd ${TORCHCHAT_ROOT}
19+
find_cmake_prefix_path
5720
install_pip_dependencies
58-
install_executorch $ENABLE_ET_PYBIND
21+
clone_executorch
22+
install_executorch_python_libs $ENABLE_ET_PYBIND
23+
install_executorch
5924
popd

scripts/install_utils.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -ex
9+
10+
install_pip_dependencies() {
11+
echo "Intalling common pip packages"
12+
13+
pip3 install wheel
14+
pip3 install "cmake>=3.19"
15+
pip3 install ninja
16+
pip3 install zstd
17+
pushd ${TORCHCHAT_ROOT}
18+
pip3 install -r ./requirements.txt
19+
popd
20+
}
21+
22+
function find_cmake_prefix_path() {
23+
path=`python -c "from distutils.sysconfig import get_python_lib;print(get_python_lib())"`
24+
MY_CMAKE_PREFIX_PATH=$path
25+
}
26+
27+
clone_executorch() {
28+
echo "Cloning executorch to ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src"
29+
rm -rf ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}
30+
mkdir -p ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src
31+
pushd ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src
32+
git clone https://github.com/pytorch/executorch.git
33+
cd executorch
34+
git checkout viable/strict
35+
echo "Install executorch: submodule update"
36+
git submodule sync
37+
git submodule update --init
38+
39+
echo "Applying fixes"
40+
cp ${TORCHCHAT_ROOT}/scripts/fixes_et/module.cpp ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src/executorch/extension/module/module.cpp # ET uses non-standard C++ that does not compile in GCC
41+
cp ${TORCHCHAT_ROOT}/scripts/fixes_et/managed_tensor.h ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src/executorch/extension/runner_util/managed_tensor.h # ET is missing headers for vector/memory. This causes downstream issues when building runner-et.
42+
popd
43+
}
44+
45+
install_executorch_python_libs() {
46+
if [ ! -d "${TORCHCHAT_ROOT}/${ET_BUILD_DIR}" ]; then
47+
echo "Directory ${TORCHCHAT_ROOT}/${ET_BUILD_DIR} does not exist."
48+
echo "Make sur eyou run clone_executorch"
49+
exit 1
50+
fi
51+
pushd ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src
52+
cd executorch
53+
54+
echo "Building and installing python libraries"
55+
echo "Building and installing python libraries"
56+
if [ "${ENABLE_ET_PYBIND}" = false ]; then
57+
echo "Not installing pybind"
58+
bash ./install_requirements.sh
59+
else
60+
echo "Installing pybind"
61+
bash ./install_requirements.sh --pybind xnnpack
62+
fi
63+
pip3 list
64+
popd
65+
}
66+
67+
install_executorch() {
68+
# AOT lib has to be build for model export
69+
# So by default it is built, and you can explicitly opt-out
70+
EXECUTORCH_BUILD_CUSTOM_OPS_AOT_VAR=OFF
71+
if [ "${EXECUTORCH_BUILD_CUSTOM_OPS_AOT}" == "" ]; then
72+
EXECUTORCH_BUILD_CUSTOM_OPS_AOT_VAR=ON
73+
fi
74+
75+
# but for runner not
76+
EXECUTORCH_BUILD_CUSTOM_VAR=OFF
77+
if [ ! ["${EXECUTORCH_BUILD_CUSTOM}" == ""] ]; then
78+
EXECUTORCH_BUILD_CUSTOM_VAR=ON
79+
fi
80+
echo "${EXECUTORCH_BUILD_CUSTOM_OPS_AOT_VAR}"
81+
echo "${EXECUTORCH_BUILD_CUSTOM_VAR}"
82+
if [ ! -d "${TORCHCHAT_ROOT}/${ET_BUILD_DIR}" ]; then
83+
echo "Directory ${TORCHCHAT_ROOT}/${ET_BUILD_DIR} does not exist."
84+
echo "Make sure you run clone_executorch"
85+
exit 1
86+
fi
87+
pushd ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src
88+
cd executorch
89+
90+
if [ "${CMAKE_OUT_DIR}" == "" ]; then
91+
CMAKE_OUT_DIR="cmake-out"
92+
fi
93+
94+
CROSS_COMPILE_ARGS=""
95+
if [ "${CMAKE_OUT_DIR}" == "cmake-out-android" ]; then
96+
CROSS_COMPILE_ARGS="-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DANDROID_ABI=${ANDROID_ABI} -DANDROID_PLATFORM=${ANDROID_PLATFORM}"
97+
fi
98+
99+
echo "Building and installing C++ libraries"
100+
echo "Inside: ${PWD}"
101+
rm -rf ${CMAKE_OUT_DIR}
102+
mkdir ${CMAKE_OUT_DIR}
103+
cmake -DCMAKE_PREFIX_PATH=${MY_CMAKE_PREFIX_PATH} -DCMAKE_BUILD_TYPE=Release -DEXECUTORCH_ENABLE_LOGGING=ON -DEXECUTORCH_LOG_LEVEL=Info -DEXECUTORCH_BUILD_CUSTOM_OPS_AOT=${EXECUTORCH_BUILD_CUSTOM_OPS_AOT_VAR} -DEXECUTORCH_BUILD_CUSTOM=${EXECUTORCH_BUILD_CUSTOM_VAR} -DEXECUTORCH_BUILD_OPTIMIZED=ON -DEXECUTORCH_BUILD_OPTIMIZED=ON -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON -DEXECUTORCH_BUILD_XNNPACK=ON ${CROSS_COMPILE_ARGS} -S . -B ${CMAKE_OUT_DIR} -G Ninja
104+
cmake --build ${CMAKE_OUT_DIR}
105+
cmake --install ${CMAKE_OUT_DIR} --prefix ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install
106+
popd
107+
}

0 commit comments

Comments
 (0)