Skip to content

Commit

Permalink
[Android] Use prebuilts; add instructions for using them (pytorch#469)
Browse files Browse the repository at this point in the history
* [Android] Use prebuilts; add instructions for using them

* Use persistent S3
  • Loading branch information
kirklandsign authored and malfet committed Jul 17, 2024
1 parent c750f24 commit 183d9bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
43 changes: 42 additions & 1 deletion docs/Android.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Executing LLM models on Android

## Option 1: Use ExecuTorch LLAMA Demo App

Check out the [tutorial on how to build an Android app running your
PyTorch models with
ExecuTorch](https://pytorch.org/executorch/main/llm/llama-demo-android.html),
Expand All @@ -9,4 +11,43 @@ and give your torchchat models a spin.

Detailed step by step in conjunction with ET Android build, to run on
simulator for Android. `scripts/android_example.sh` for running a
model on an Android simulator (on Mac)
model on an Android simulator (on Mac)

## Option 2: Integrate the Java API with your own app

We provide a Java library for you to integrate LLM runner to your own app.
See [this file](https://github.com/pytorch/executorch/blob/main/extension/android/src/main/java/org/pytorch/executorch/LlamaModule.java)
for Java APIs.

To add the Java library to your app, use helper functions `download_jar_library`
and `download_jni_library` from `scripts/android_example.sh` to download the
prebuilt libraries.

```bash
# my_build_script.sh
source scripts/android_example.sh
download_jar_library
download_jni_library
```

In your app working directory (for example executorch/examples/demo-apps/android/LlamaDemo),
copy the jar to your app libs:
```bash
mkdir -p app/libs
cp ${TORCHCHAT_ROOT}/build/android/executorch.jar app/libs/executorch.jar
```

In your Java app, add the jar file path to your gradle build rule.
```
# app/build.grardle.kts
dependencies {
implementation(files("libs/executorch.jar"))
}
```

Then copy the corresponding JNI library to your app:

```bash
mkdir -p app/src/main/jniLibs/arm64-v8a
cp ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so app/src/main/jniLibs/arm64-v8a
```
37 changes: 31 additions & 6 deletions scripts/android_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

set -eu
set -eux

cd ${TORCHCHAT_ROOT}
echo "Inside: $TORCHCHAT_ROOT"
Expand All @@ -25,6 +25,10 @@ else
exit -1
fi

LLAMA_JNI_ARM64_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/arm64-v8a/libexecutorch_llama_jni.so"
LLAMA_JNI_X86_64_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/x86_64/libexecutorch_llama_jni.so"
LLAMA_JAR_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/executorch.jar"

mkdir -p ${TORCHCHAT_ROOT}/build/android

setup_java() {
Expand Down Expand Up @@ -64,30 +68,50 @@ setup_android_sdk_manager() {
setup_android_sdk() {
sdkmanager "platforms;android-34"
sdkmanager "platform-tools"
sdkmanager "emulator"
sdkmanager "system-images;android-34;google_apis;${ANDROID_ABI}"
}

setup_android_ndk() {
sdkmanager "ndk;25.0.8775105"
export ANDROID_NDK="$ANDROID_HOME/ndk/25.0.8775105"
}

download_jar_library() {
mkdir -p ${TORCHCHAT_ROOT}/build/android
curl "${LLAMA_JAR_URL}" -o ${TORCHCHAT_ROOT}/build/android/executorch.jar
}

download_jni_library() {
mkdir -p ${TORCHCHAT_ROOT}/build/android/arm64-v8a
mkdir -p ${TORCHCHAT_ROOT}/build/android/x86_64
if [ ! -f ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so ]; then
curl "${LLAMA_JNI_ARM64_URL}" -o ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so
fi
if [ ! -f ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so ]; then
curl "${LLAMA_JNI_X86_64_URL}" -o ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so
fi
}

build_app() {
pushd build/src/executorch/examples/demo-apps/android/LlamaDemo
./gradlew :app:setup
mkdir -p app/src/main/jniLibs/arm64-v8a
mkdir -p app/src/main/jniLibs/x86_64
cp ${TORCHCHAT_ROOT}/build/android/arm64-v8a/libexecutorch_llama_jni.so app/src/main/jniLibs/arm64-v8a
cp ${TORCHCHAT_ROOT}/build/android/x86_64/libexecutorch_llama_jni.so app/src/main/jniLibs/x86_64
./gradlew :app:build
popd
}

setup_avd() {
sdkmanager "emulator"
sdkmanager "system-images;android-34;google_apis;${ANDROID_ABI}"

avdmanager create avd --name "torchchat" --package "system-images;android-34;google_apis;${ANDROID_ABI}"
sdk/emulator/emulator @torchchat &
}

push_files_to_android() {
adb wait-for-device
adb shell mkdir /data/local/tmp/llama
adb shell mkdir -p /data/local/tmp/llama
adb push stories15M.pte /data/local/tmp/llama
adb push checkpoints/stories15M/tokenizer.bin /data/local/tmp/llama
adb install -t build/src/executorch/examples/demo-apps/android/LlamaDemo/app/build/outputs/apk/debug/app-debug.apk
Expand All @@ -98,7 +122,8 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
setup_android_sdk_manager
setup_android_sdk
setup_android_ndk
build_app
setup_avd
download_jni_library
build_app
push_files_to_android
fi

0 comments on commit 183d9bd

Please sign in to comment.