Skip to content

Commit 338d936

Browse files
kirklandsignZonglin Peng
authored and
Zonglin Peng
committed
[Android demo] Decouple pte file from assets and remove unused
Differential Revision: D70528015 Pull Request resolved: #8906
1 parent 0f48136 commit 338d936

File tree

13 files changed

+48
-1293
lines changed

13 files changed

+48
-1293
lines changed

build/build_android_library.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ collect_artifacts_to_be_uploaded() {
178178
}
179179

180180
main() {
181-
BUILD_AAR_DIR="$(mktemp -d)"
181+
if [[ -z "${BUILD_AAR_DIR:-}" ]]; then
182+
BUILD_AAR_DIR="$(mktemp -d)"
183+
fi
182184
export BUILD_AAR_DIR
183185
if [ -z "$ANDROID_ABIS" ]; then
184186
ANDROID_ABIS=("arm64-v8a" "x86_64")

examples/demo-apps/android/ExecuTorchDemo/README.md

Lines changed: 36 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ We generate the model file for the ExecuTorch runtime in Android Demo App.
3939
For delegating DeepLab v3 to XNNPACK backend, please do the following to export the model:
4040

4141
```bash
42+
cd executorch # go to executorch root
4243
python3 -m examples.xnnpack.aot_compiler --model_name="dl3" --delegate
43-
mkdir -p examples/demo-apps/android/ExecuTorchDemo/app/src/main/assets/
44-
cp dl3_xnnpack_fp32.pte examples/demo-apps/android/ExecuTorchDemo/app/src/main/assets/
44+
```
45+
46+
Then push the pte file to Android device:
47+
48+
```bash
49+
adb push dl3_xnnpack_fp32.pte /data/local/tmp/dl3_xnnpack_fp32.pte
4550
```
4651

4752
For more detailed tutorial of lowering to XNNPACK, please see [XNNPACK backend](backends-xnnpack.md).
@@ -50,135 +55,63 @@ For more detailed tutorial of lowering to XNNPACK, please see [XNNPACK backend](
5055

5156
For delegating to Qualcomm Hexagon NPU, please follow the tutorial [here](backends-qualcomm.md).
5257

53-
After generating the model, copy the model to `assets` directory.
54-
5558
```bash
5659
python -m examples.qualcomm.scripts.deeplab_v3 -b build-android -m SM8450 -s <adb_connected_device_serial>
57-
cp deeplab_v3/dlv3_qnn.pte examples/demo-apps/android/ExecuTorchDemo/app/src/main/assets/
60+
```
61+
62+
Then push the pte file to Android device:
63+
64+
```bash
65+
adb push deeplab_v3/dlv3_qnn.pte /data/local/tmp/dlv3_qnn.pte
5866
```
5967

6068
### Runtime
6169

62-
We build the required ExecuTorch runtime library to run the model.
70+
We build the required ExecuTorch runtime library (AAR) to run the model.
6371

6472
#### XNNPACK
6573

66-
1. Build the CMake target for the library with XNNPACK backend:
67-
6874
```bash
75+
# go to ExecuTorch repo root
6976
export ANDROID_NDK=<path-to-android-ndk>
70-
export ANDROID_ABI=arm64-v8a
77+
export ANDROID_ABIS=arm64-v8a
7178

7279
# Run the following lines from the `executorch/` folder
7380
./install_executorch.sh --clean
74-
mkdir cmake-android-out
75-
76-
# Build the core executorch library
77-
cmake . -DCMAKE_INSTALL_PREFIX=cmake-android-out \
78-
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
79-
-DANDROID_ABI="${ANDROID_ABI}" \
80-
-DEXECUTORCH_BUILD_XNNPACK=ON \
81-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
82-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
83-
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
84-
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
85-
-Bcmake-android-out
86-
87-
cmake --build cmake-android-out -j16 --target install
88-
```
89-
90-
When we set `EXECUTORCH_BUILD_XNNPACK=ON`, we will build the target [`xnnpack_backend`](https://github.com/pytorch/executorch/blob/main/backends/xnnpack/CMakeLists.txt) which in turn is linked into libexecutorch_jni via [CMake](https://github.com/pytorch/executorch/blob/main/examples/demo-apps/android/jni/CMakeLists.txt).
91-
92-
2. Build the Android extension:
93-
94-
```bash
9581

96-
# Build the android extension
97-
cmake extension/android \
98-
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}"/build/cmake/android.toolchain.cmake \
99-
-DANDROID_ABI="${ANDROID_ABI}" \
100-
-DCMAKE_INSTALL_PREFIX=cmake-android-out \
101-
-Bcmake-android-out/extension/android
82+
# Create a new directory `app/libs` for the AAR to live
83+
pushd examples/demo-apps/android/ExecuTorchDemo
84+
mkdir -p app/libs
85+
popd
10286

103-
cmake --build cmake-android-out/extension/android -j16
87+
# Build the AAR. It will include XNNPACK backend by default.
88+
export BUILD_AAR_DIR=$(realpath examples/demo-apps/android/ExecuTorchDemo/app/libs)
89+
sh build/build_android_library.sh
10490
```
10591

106-
`libexecutorch_jni.so` wraps up the required XNNPACK Backend runtime library from `xnnpack_backend`, and adds an additional JNI layer using fbjni. This is later exposed to Java app.
107-
10892
#### Qualcomm Hexagon NPU
10993

110-
1. Build the CMake target for the library with Qualcomm Hexagon NPU (HTP) backend (XNNPACK also included):
111-
11294
```bash
95+
# go to ExecuTorch repo root
11396
export ANDROID_NDK=<path-to-android-ndk>
114-
export ANDROID_ABI=arm64-v8a
115-
export QNN_SDK_ROOT=<path-to-qnn-sdk>
97+
export ANDROID_ABIS=arm64-v8a
98+
export QNN_SDK_ROOT=<path-to-qnn-sdk-root>
11699

100+
# Run the following lines from the `executorch/` folder
117101
./install_executorch.sh --clean
118-
mkdir cmake-android-out
119-
cmake . -DCMAKE_INSTALL_PREFIX=cmake-android-out \
120-
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
121-
-DANDROID_ABI="${ANDROID_ABI}" \
122-
-DEXECUTORCH_BUILD_XNNPACK=ON \
123-
-DEXECUTORCH_BUILD_QNN=ON \
124-
-DQNN_SDK_ROOT="${QNN_SDK_ROOT}" \
125-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
126-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
127-
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
128-
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
129-
-Bcmake-android-out
130-
131-
cmake --build cmake-android-out -j16 --target install
132-
```
133-
Similar to the XNNPACK library, with this setup, we compile `libexecutorch_jni.so` but it adds an additional static library `qnn_executorch_backend` which wraps up Qualcomm HTP runtime library and registers the Qualcomm HTP backend. This is later exposed to Java app.
134-
135-
`qnn_executorch_backend` is built when we turn on CMake option `EXECUTORCH_BUILD_QNN`. It will include the [CMakeLists.txt](https://github.com/pytorch/executorch/blob/main/backends/qualcomm/CMakeLists.txt) from backends/qualcomm where we `add_library(qnn_executorch_backend STATIC)`.
136102

137-
2. Build the Android extension:
103+
# Create a new directory `app/libs` for the AAR to live
104+
pushd examples/demo-apps/android/ExecuTorchDemo
105+
mkdir -p app/libs
106+
popd
138107

139-
```bash
140-
cmake extension/android \
141-
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}"/build/cmake/android.toolchain.cmake \
142-
-DANDROID_ABI="${ANDROID_ABI}" \
143-
-DCMAKE_INSTALL_PREFIX=cmake-android-out \
144-
-Bcmake-android-out/extension/android
145-
146-
cmake --build cmake-android-out/extension/android -j16
147-
```
148-
149-
## Deploying on Device via Demo App
150-
151-
### Steps for Deploying Model via XNNPACK
152-
153-
```bash
154-
mkdir -p examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
155-
cp cmake-android-out/extension/android/libexecutorch_jni.so \
156-
examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/libexecutorch.so
108+
# Build the AAR. It will include XNNPACK backend by default.
109+
export BUILD_AAR_DIR=$(realpath examples/demo-apps/android/ExecuTorchDemo/app/libs)
110+
sh build/build_android_library.sh
157111
```
158112

159-
This allows the Android app to load ExecuTorch runtime with XNNPACK backend as a JNI library. Later, this shared library will be loaded by `NativePeer.java` in Java code.
160-
161-
### Steps for Deploying Model via Qualcomm's AI Engine Direct
162-
163-
```bash
164-
mkdir -p ../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
165-
```
166-
167-
We need to push some additional Qualcomm HTP backend libraries to the app. Please refer to [Qualcomm docs](backends-qualcomm.md) here.
168-
169-
```bash
170-
cp ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtp.so ${QNN_SDK_ROOT}/lib/hexagon-v69/unsigned/libQnnHtpV69Skel.so ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV69Stub.so ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnSystem.so \
171-
examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
172-
```
173-
174-
Copy the core libraries:
175-
176-
```bash
177-
cp cmake-android-out/extension/android/libexecutorch_jni.so \
178-
examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/libexecutorch.so
179-
cp cmake-android-out/lib/libqnn_executorch_backend.so \
180-
examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/libqnn_executorch_backend.so
181-
```
113+
This is very similar to XNNPACK setup, but users now needs to define `QNN_SDK_ROOT` so that
114+
QNN backend is built into the AAR.
182115

183116
## Running the App
184117

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ dependencies {
5757
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")
5858
implementation("com.facebook.soloader:soloader:0.10.5")
5959
implementation("com.facebook.fbjni:fbjni:0.5.1")
60-
implementation("org.pytorch.executorch:executorch") {
61-
exclude("com.facebook.fbjni", "fbjni-java-only")
62-
}
60+
implementation(files("libs/executorch.aar"))
6361
testImplementation("junit:junit:4.13.2")
6462
androidTestImplementation("androidx.test.ext:junit:1.1.5")
6563
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
@@ -72,8 +70,8 @@ dependencies {
7270
tasks.register("setup") {
7371
doFirst {
7472
exec {
75-
commandLine("sh", "examples/demo-apps/android/LlamaDemo/setup.sh")
76-
workingDir("../../../../../")
73+
commandLine("sh", "setup.sh")
74+
workingDir("../")
7775
}
7876
}
7977
}

examples/demo-apps/android/ExecuTorchDemo/app/src/main/BUCK

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ fb_android_resource(
3030
fb_android_library(
3131
name = "app_lib",
3232
srcs = [
33-
"java/com/example/executorchdemo/ClassificationActivity.java",
34-
"java/com/example/executorchdemo/ImageNetClasses.java",
3533
"java/com/example/executorchdemo/MainActivity.java",
3634
"java/com/example/executorchdemo/TensorImageUtils.java",
3735
],
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/demo-apps/android/ExecuTorchDemo/app/src/main/java/com/example/executorchdemo/ClassificationActivity.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)