Skip to content

Commit 1eb6681

Browse files
kimishpatelmalfet
authored andcommitted
Add android build for runner-et (#274)
Summary: This enables us to benchmark models on android Test Plan: export ANDROID_NDK="path-to" ./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_fp32.pte -z tokenizer.bin -n 200 -t 0" produced text Once upon a time, there was a little girl named Lily. She loved to play outside in the sunshine. Reviewers: Subscribers: Tasks: Tags:
1 parent 309798d commit 1eb6681

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

runner-et/CMakeLists.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
cmake_minimum_required(VERSION 3.24)
22
set(CMAKE_CXX_STANDARD 17)
33

4+
IF(DEFINED ENV{ET_BUILD_DIR})
5+
set(ET_BUILD_DIR $ENV{ET_BUILD_DIR})
6+
ELSE()
7+
set(ET_BUILD_DIR "et-build")
8+
ENDIF()
9+
10+
MESSAGE(STATUS "Using ET BUILD DIR: --[${ET_BUILD_DIR}]--")
11+
12+
IF(DEFINED ENV{CMAKE_OUT_DIR})
13+
set(CMAKE_OUT_DIR $ENV{CMAKE_OUT_DIR})
14+
ELSE()
15+
set(CMAKE_OUT_DIR "cmake-out")
16+
ENDIF()
17+
18+
MESSAGE(STATUS "Using ET BUILD DIR: --[${ET_BUILD_DIR}]--")
19+
420
project(Torchchat)
521

622
include(CMakePrintHelpers)
723
include(Utils.cmake)
824
set(TORCHCHAT_ROOT $ENV{TORCHCHAT_ROOT})
925
cmake_print_variables(TORCHCHAT_ROOT)
1026

11-
find_package(executorch CONFIG REQUIRED PATHS ${TORCHCHAT_ROOT}/et-build/install/lib/cmake/ExecuTorch)
12-
set(_common_include_directories ${TORCHCHAT_ROOT}/et-build/src)
27+
MESSAGE(STATUS "Looking for excutorch in ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install/lib/cmake/ExecuTorch")
28+
set(executorch_DIR ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install/lib/cmake/ExecuTorch)
29+
find_package(executorch CONFIG REQUIRED PATHS ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/install/lib/cmake/ExecuTorch)
30+
MESSAGE(STATUS "Found executorch cmake config.")
31+
32+
set(_common_include_directories ${TORCHCHAT_ROOT}/${ET_BUILD_DIR}/src)
1333
cmake_print_variables(_common_include_directories)
1434

1535
target_include_directories(executorch INTERFACE ${_common_include_directories}) # Ideally ExecuTorch installation process would do this
@@ -20,7 +40,7 @@ target_link_libraries(
2040
runner_et PRIVATE
2141
executorch
2242
extension_module
23-
${TORCHCHAT_ROOT}/et-build/src/executorch/cmake-out/extension/data_loader/libextension_data_loader.a # This one does not get installed by ExecuTorch
43+
${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
2444
optimized_kernels
2545
portable_kernels
2646
cpublas
@@ -32,6 +52,7 @@ target_link_libraries(
3252
pthreadpool
3353
cpuinfo
3454
)
55+
target_link_options_shared_lib(executorch)
3556
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
3657
target_link_options_shared_lib(xnnpack_backend)
3758
target_link_options_shared_lib(XNNPACK)

runner-et/build_android.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
if ["${ANDROID_NDK}" == ""]; then
11+
echo "Please set ANDROID_NDK enviornment variable."
12+
echo "For example it can be /Users/guest/Desktop/android-ndk-r26."
13+
echo "You can download NDK from android website"
14+
exit 1
15+
else
16+
echo "ANDROID_NDK set to ${ANDROID_NDK}"
17+
fi
18+
19+
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 ET_BUILD_DIR="et-build-android"
23+
export CMAKE_OUT_DIR="cmake-out-android"
24+
# export DCMAKE_INSTALL_PREFIX=cmake-out-android
25+
#
26+
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+
54+
build_runner_et() {
55+
rm -rf build/cmake-out-android
56+
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
57+
cmake --build build/cmake-out-android/ -j16 --config Release
58+
}
59+
60+
# install_executorch
61+
build_runner_et

0 commit comments

Comments
 (0)