Skip to content

Commit b5d4002

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Android automate cmake build in gradle (#2838)
Summary: Automatically invoke `examples/demo-apps/android/LlamaDemo/setup.sh` in `gradle setup` task. Users doesn't need to run examples/demo-apps/android/LlamaDemo/setup.sh manually. Test (follow https://github.com/pytorch/executorch/blob/ba824577f495f7522cc9be91d38cf8ad187bc841/examples/demo-apps/android/LlamaDemo/README.md): 1. Fetch source ``` git fetch origin pull/2838/head git checkout FETCH_HEAD ``` 2. Push model ``` adb shell mkdir -p /data/local/tmp/llama adb push llama2.pte /data/local/tmp/llama <--- Use your pte file adb push tokenizer.bin /data/local/tmp/llama <--- Use your tokenizer.bin ``` 3. Build JNI ``` export ANDROID_NDK=<path_to_android_ndk> <---- You can download NDK from https://developer.android.com/ndk/downloads export ANDROID_ABI=arm64-v8a pushd examples/demo-apps/android/LlamaDemo ./gradlew :app:setup popd ``` 4. Build app * (Method 1) Open Android Studio and select "Open an existing Android Studio project" to open examples/demo-apps/android/LlamaDemo. Run the app (^R). This builds and launches the app on the phone. * (Method 2) ``` export ANDROID_HOME=--sanitized-- Pull Request resolved: #2838 Reviewed By: cccclai Differential Revision: D55717885 Pulled By: kirklandsign fbshipit-source-id: fc2c47219860c8245a1b7728abee46467c846908
1 parent ea3927a commit b5d4002

File tree

4 files changed

+37
-69
lines changed

4 files changed

+37
-69
lines changed

build/test_android_ci.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ build_executorch() {
2020
rm -rf cmake-out && mkdir cmake-out
2121
ANDROID_NDK=/opt/ndk BUCK2=$(which buck2) FLATC=$(which flatc) ANDROID_ABI=arm64-v8a \
2222
bash examples/demo-apps/android/ExecuTorchDemo/setup.sh
23-
ANDROID_NDK=/opt/ndk BUCK2=$(which buck2) FLATC=$(which flatc) ANDROID_ABI=arm64-v8a \
24-
bash examples/demo-apps/android/LlamaDemo/setup.sh
2523
}
2624

2725
build_android_demo_app() {
@@ -32,6 +30,7 @@ build_android_demo_app() {
3230

3331
build_android_llama_demo_app() {
3432
pushd examples/demo-apps/android/LlamaDemo
33+
ANDROID_NDK=/opt/ndk ANDROID_ABI=arm64-v8a ./gradlew setup
3534
ANDROID_HOME=/opt/android/sdk ./gradlew build
3635
popd
3736
}
Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
1-
# Welcome to the ExecuTorch LLaMA Android Demo App
1+
# Building ExecuTorch LLaMA Android Demo App
22

33
This app demonstrates the use of the LLaMA chat app demonstrating local inference use case with ExecuTorch.
44

5-
::::{grid} 2
6-
:::{grid-item-card} What you will learn
7-
:class-card: card-prerequisites
8-
* How to set up a build target for Android arm64-v8a
9-
* How to build the required ExecuTorch runtime, LLaMA runner, and JNI wrapper for Android
10-
* How to build the app with required JNI library
11-
:::
12-
:::{grid-item-card} Prerequisites
13-
:class-card: card-prerequisites
5+
## Prerequisites
146
* Refer to [Setting up ExecuTorch](https://pytorch.org/executorch/stable/getting-started-setup) to set up the repo and dev environment.
157
* Download and install [Android Studio and SDK](https://developer.android.com/studio).
168
* Supported Host OS: CentOS, macOS Sonoma on Apple Silicon.
17-
:::
18-
::::
199

2010
```{note}
2111
This demo app and tutorial has only been validated with arm64-v8a [ABI](https://developer.android.com/ndk/guides/abis), with NDK 25.
2212
```
2313

2414
## Getting models
25-
Please refer to the LLaMA tutorial to export the model.
15+
Please refer to the [ExecuTorch Llama2 docs](https://github.com/pytorch/executorch/blob/main/examples/models/llama2/README.md) to export the model.
2616

27-
Once you have the model, you need to push it to your device:
17+
After you export the model and generate tokenizer.bin, push them device:
2818
```bash
2919
adb shell mkdir -p /data/local/tmp/llama
30-
adb push model.pte /data/local/tmp/llama
20+
adb push llama2.pte /data/local/tmp/llama
3121
adb push tokenizer.bin /data/local/tmp/llama
3222
```
3323

@@ -38,64 +28,37 @@ The demo app searches in `/data/local/tmp/llama` for .pte and .bin files as LLAM
3828
## Build JNI library
3929
1. Open a terminal window and navigate to the root directory of the `executorch`.
4030
2. Set the following environment variables:
41-
```
31+
```bash
4232
export ANDROID_NDK=<path_to_android_ndk>
4333
export ANDROID_ABI=arm64-v8a
4434
```
45-
3. Create a new directory for the CMake build output:
46-
```
47-
mkdir cmake-out
48-
```
49-
4. Run the following command to configure the CMake build:
50-
```
51-
# Build the core ExecuTorch runtime library
52-
cmake . -DCMAKE_INSTALL_PREFIX=cmake-out \
53-
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
54-
-DANDROID_ABI="${ANDROID_ABI}" \
55-
-DEXECUTORCH_BUILD_XNNPACK=ON \
56-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
57-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
58-
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
59-
-Bcmake-out
60-
61-
cmake --build cmake-out -j16 --target install
62-
63-
# Build the llama2 runner library and custom ops
64-
cmake examples/models/llama2 \
65-
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
66-
-DANDROID_ABI="$ANDROID_ABI" \
67-
-DCMAKE_INSTALL_PREFIX=cmake-out \
68-
-Bcmake-out/examples/models/llama2
69-
70-
cmake --build cmake-out/examples/models/llama2 -j16
71-
72-
# Build the Android JNI library
73-
cmake extension/android \
74-
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
75-
-DANDROID_ABI="${ANDROID_ABI}" \
76-
-DCMAKE_INSTALL_PREFIX=cmake-out \
77-
-DEXECUTORCH_BUILD_LLAMA_JNI=ON \
78-
-Bcmake-out/extension/android
79-
80-
cmake --build cmake-out/extension/android -j16
81-
```
82-
83-
5. Copy the built library to your app:
84-
```
85-
JNI_LIBS_PATH="examples/demo-apps/android/LlamaDemo/app/src/main/jniLibs"
86-
mkdir -p "${JNI_LIBS_PATH}/${ANDROID_ABI}"
87-
cp cmake-out/extension/android/libexecutorch_llama_jni.so "${JNI_LIBS_PATH}/${ANDROID_ABI}/"
35+
3. Run the following command set up the required JNI library:
36+
```bash
37+
pushd examples/demo-apps/android/LlamaDemo
38+
./gradlew :app:setup
39+
popd
8840
```
41+
This is running the shell script [setup.sh](./setup.sh) which configures the required core ExecuTorch, LLAMA2, and Android libraries, builds them, and copy to jniLibs.
8942

9043
## Build Java app
44+
### Alternative 1: Android Studio (Recommended)
9145
1. Open Android Studio and select "Open an existing Android Studio project" to open examples/demo-apps/android/LlamaDemo.
92-
2. Run the app (^R).
46+
2. Run the app (^R). This builds and launches the app on the phone.
47+
48+
### Alternative 2: Command line
49+
Without Android Studio UI, we can run gradle directly to build the app. We need to set up the Android SDK path and invoke gradle.
50+
```bash
51+
export ANDROID_HOME=<path_to_android_sdk_home>
52+
pushd examples/demo-apps/android/LlamaDemo
53+
./gradlew :app:installDebug
54+
popd
55+
```
9356

9457
On the phone or emulator, you can try running the model:
9558
<img src="../_static/img/android_llama_app.png" alt="Android LLaMA App" /><br>
9659

9760
## Takeaways
98-
Through this tutorial we've learnt how to build the ExecuTorch LLAMA library with XNNPACK backend, and expose it to JNI layer to build the Android app.
61+
Through this tutorial we've learnt how to build the ExecuTorch LLAMA library, and expose it to JNI layer to build the Android app.
9962

10063
## Reporting Issues
10164
If you encountered any bugs or issues following this tutorial please file a bug/issue here on [Github](https://github.com/pytorch/executorch/issues/new).

examples/demo-apps/android/LlamaDemo/app/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ dependencies {
6767
debugImplementation("androidx.compose.ui:ui-tooling")
6868
debugImplementation("androidx.compose.ui:ui-test-manifest")
6969
}
70+
71+
tasks.register("setup") {
72+
doFirst {
73+
exec {
74+
commandLine("sh", "examples/demo-apps/android/LlamaDemo/setup.sh")
75+
workingDir("../../../../../")
76+
}
77+
}
78+
}

examples/demo-apps/android/LlamaDemo/setup.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
set -eu
99

10-
CMAKE_OUT=cmake-out
10+
CMAKE_OUT="${CMAKE_OUT:-cmake-out}"
1111
# Note: Set up ANDROID_NDK, ANDROID_ABI, BUCK2, and FLATC
1212
cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
1313
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
1414
-DANDROID_ABI="${ANDROID_ABI}" \
15-
-DBUCK2="${BUCK2}" \
1615
-DEXECUTORCH_BUILD_XNNPACK=ON \
17-
-DEXECUTORCH_BUILD_FLATC=OFF \
1816
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
19-
-DFLATC_EXECUTABLE="${FLATC}" \
2017
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
2118
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
2219
-B"${CMAKE_OUT}"
@@ -28,15 +25,15 @@ else
2825
fi
2926
cmake --build "${CMAKE_OUT}" -j "${CMAKE_JOBS}" --target install
3027

31-
cmake examples/models/llama2 -DBUCK2="${BUCK2}" \
28+
cmake examples/models/llama2 \
3229
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
3330
-DANDROID_ABI="$ANDROID_ABI" \
3431
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
3532
-B"${CMAKE_OUT}"/examples/models/llama2
3633

3734
cmake --build "${CMAKE_OUT}"/examples/models/llama2 -j "${CMAKE_JOBS}"
3835

39-
cmake extension/android -DBUCK2="${BUCK2}" \
36+
cmake extension/android \
4037
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
4138
-DANDROID_ABI="${ANDROID_ABI}" \
4239
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \

0 commit comments

Comments
 (0)