Skip to content

Commit 183d9bd

Browse files
kirklandsignmalfet
authored andcommitted
[Android] Use prebuilts; add instructions for using them (#469)
* [Android] Use prebuilts; add instructions for using them * Use persistent S3
1 parent c750f24 commit 183d9bd

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

docs/Android.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Executing LLM models on Android
22

3+
## Option 1: Use ExecuTorch LLAMA Demo App
4+
35
Check out the [tutorial on how to build an Android app running your
46
PyTorch models with
57
ExecuTorch](https://pytorch.org/executorch/main/llm/llama-demo-android.html),
@@ -9,4 +11,43 @@ and give your torchchat models a spin.
911

1012
Detailed step by step in conjunction with ET Android build, to run on
1113
simulator for Android. `scripts/android_example.sh` for running a
12-
model on an Android simulator (on Mac)
14+
model on an Android simulator (on Mac)
15+
16+
## Option 2: Integrate the Java API with your own app
17+
18+
We provide a Java library for you to integrate LLM runner to your own app.
19+
See [this file](https://github.com/pytorch/executorch/blob/main/extension/android/src/main/java/org/pytorch/executorch/LlamaModule.java)
20+
for Java APIs.
21+
22+
To add the Java library to your app, use helper functions `download_jar_library`
23+
and `download_jni_library` from `scripts/android_example.sh` to download the
24+
prebuilt libraries.
25+
26+
```bash
27+
# my_build_script.sh
28+
source scripts/android_example.sh
29+
download_jar_library
30+
download_jni_library
31+
```
32+
33+
In your app working directory (for example executorch/examples/demo-apps/android/LlamaDemo),
34+
copy the jar to your app libs:
35+
```bash
36+
mkdir -p app/libs
37+
cp ${TORCHCHAT_ROOT}/build/android/executorch.jar app/libs/executorch.jar
38+
```
39+
40+
In your Java app, add the jar file path to your gradle build rule.
41+
```
42+
# app/build.grardle.kts
43+
dependencies {
44+
implementation(files("libs/executorch.jar"))
45+
}
46+
```
47+
48+
Then copy the corresponding JNI library to your app:
49+
50+
```bash
51+
mkdir -p app/src/main/jniLibs/arm64-v8a
52+
cp ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so app/src/main/jniLibs/arm64-v8a
53+
```

scripts/android_example.sh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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 -eu
8+
set -eux
99

1010
cd ${TORCHCHAT_ROOT}
1111
echo "Inside: $TORCHCHAT_ROOT"
@@ -25,6 +25,10 @@ else
2525
exit -1
2626
fi
2727

28+
LLAMA_JNI_ARM64_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/arm64-v8a/libexecutorch_llama_jni.so"
29+
LLAMA_JNI_X86_64_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/x86_64/libexecutorch_llama_jni.so"
30+
LLAMA_JAR_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/executorch.jar"
31+
2832
mkdir -p ${TORCHCHAT_ROOT}/build/android
2933

3034
setup_java() {
@@ -64,30 +68,50 @@ setup_android_sdk_manager() {
6468
setup_android_sdk() {
6569
sdkmanager "platforms;android-34"
6670
sdkmanager "platform-tools"
67-
sdkmanager "emulator"
68-
sdkmanager "system-images;android-34;google_apis;${ANDROID_ABI}"
6971
}
7072

7173
setup_android_ndk() {
7274
sdkmanager "ndk;25.0.8775105"
7375
export ANDROID_NDK="$ANDROID_HOME/ndk/25.0.8775105"
7476
}
7577

78+
download_jar_library() {
79+
mkdir -p ${TORCHCHAT_ROOT}/build/android
80+
curl "${LLAMA_JAR_URL}" -o ${TORCHCHAT_ROOT}/build/android/executorch.jar
81+
}
82+
83+
download_jni_library() {
84+
mkdir -p ${TORCHCHAT_ROOT}/build/android/arm64-v8a
85+
mkdir -p ${TORCHCHAT_ROOT}/build/android/x86_64
86+
if [ ! -f ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so ]; then
87+
curl "${LLAMA_JNI_ARM64_URL}" -o ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so
88+
fi
89+
if [ ! -f ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so ]; then
90+
curl "${LLAMA_JNI_X86_64_URL}" -o ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so
91+
fi
92+
}
93+
7694
build_app() {
7795
pushd build/src/executorch/examples/demo-apps/android/LlamaDemo
78-
./gradlew :app:setup
96+
mkdir -p app/src/main/jniLibs/arm64-v8a
97+
mkdir -p app/src/main/jniLibs/x86_64
98+
cp ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so app/src/main/jniLibs/arm64-v8a
99+
cp ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so app/src/main/jniLibs/x86_64
79100
./gradlew :app:build
80101
popd
81102
}
82103

83104
setup_avd() {
105+
sdkmanager "emulator"
106+
sdkmanager "system-images;android-34;google_apis;${ANDROID_ABI}"
107+
84108
avdmanager create avd --name "torchchat" --package "system-images;android-34;google_apis;${ANDROID_ABI}"
85109
sdk/emulator/emulator @torchchat &
86110
}
87111

88112
push_files_to_android() {
89113
adb wait-for-device
90-
adb shell mkdir /data/local/tmp/llama
114+
adb shell mkdir -p /data/local/tmp/llama
91115
adb push stories15M.pte /data/local/tmp/llama
92116
adb push checkpoints/stories15M/tokenizer.bin /data/local/tmp/llama
93117
adb install -t build/src/executorch/examples/demo-apps/android/LlamaDemo/app/build/outputs/apk/debug/app-debug.apk
@@ -98,7 +122,8 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
98122
setup_android_sdk_manager
99123
setup_android_sdk
100124
setup_android_ndk
101-
build_app
102125
setup_avd
126+
download_jni_library
127+
build_app
103128
push_files_to_android
104129
fi

0 commit comments

Comments
 (0)