Skip to content

Commit ac9c121

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Fix demo app workflow and tutorial (#1979)
Summary: Now we use sources from extension/android, and we need to update the instructions for Android demo app. Note: We have two ad-hoc solutions 1. For Java, so far we are copying sources; we should use package instead. 2. For JNI, we are building from top-level CMakeLists.txt; we should build from extension/android and `find_package()` for deps. We will address these two issues in follow-up PRs. Pull Request resolved: #1979 Reviewed By: mcr229 Differential Revision: D53791261 Pulled By: kirklandsign fbshipit-source-id: 73e48f1cc3c444426b8b3252fc48762aa182cb98
1 parent 75aa0b4 commit ac9c121

File tree

13 files changed

+90
-1866
lines changed

13 files changed

+90
-1866
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
305305
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
306306
endif()
307307

308-
# Add Android demo app JNI subdirectory
309-
if(EXECUTORCH_BUILD_ANDROID_DEMO_APP_JNI)
310-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/demo-apps/android/jni)
308+
# Add Android JNI subdirectory
309+
if(EXECUTORCH_BUILD_ANDROID_JNI)
310+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/android)
311311
endif()
312312

313313
if(EXECUTORCH_BUILD_SDK)

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,40 @@ cp deeplab_v3/dlv3_qnn.pte examples/demo-apps/android/ExecuTorchDemo/app/src/mai
6262

6363
We build the required ExecuTorch runtime library to run the model.
6464

65+
#### Java helper classes
66+
67+
Note: This is an ad-hoc solution. We will publish a formal Java package when it is ready. However, for now we need to copy sources from extension/android/src/main/java/org/pytorch/executorch.
68+
69+
```
70+
mkdir -p examples/demo-apps/android/ExecuTorchDemo/app/src/main/java/com/example/executorchdemo/executor
71+
cp extension/android/src/main/java/org/pytorch/executorch/*.java examples/demo-apps/android/ExecuTorchDemo/app/src/main/java/com/example/executorchdemo/executor
72+
```
73+
6574
#### XNNPACK
6675

6776
1. Configure the CMake target for the library with XNNPACK backend:
6877

6978
```bash
7079
export ANDROID_NDK=<path-to-android-ndk>
80+
export BUCK2=/tmp/buck2 # Or your buck path
7181

7282
rm -rf cmake-out && mkdir cmake-out && cd cmake-out
73-
cmake .. \
74-
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
75-
-DANDROID_ABI=arm64-v8a \
76-
-DBUCK2=/tmp/buck2 \
77-
-DEXECUTORCH_BUILD_ANDROID_DEMO_APP_JNI=ON \
78-
-DEXECUTORCH_BUILD_XNNPACK=ON \
79-
-DEXECUTORCH_BUILD_FLATC=OFF \
80-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
83+
cmake .. -DCMAKE_INSTALL_PREFIX=cmake-out \
84+
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
85+
-DANDROID_ABI=arm64-v8a \
86+
-DBUCK2=$BUCK \
87+
-DEXECUTORCH_BUILD_XNNPACK=ON \
88+
-DEXECUTORCH_BUILD_FLATC=OFF \
89+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
90+
-DFLATC_EXECUTABLE=$FLATC_EXECUTABLE \
91+
-DEXECUTORCH_BUILD_ANDROID_JNI=ON \
92+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
93+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
8194
```
8295

83-
When we set `EXECUTORCH_BUILD_XNNPACK=ON`, we will build the target [`xnn_executor_runner_lib`](https://github.com/pytorch/executorch/blob/main/backends/xnnpack/CMakeLists.txt) which in turn is linked into libexecutorchdemo via [CMake](https://github.com/pytorch/executorch/blob/main/examples/demo-apps/android/jni/CMakeLists.txt).
96+
When we set `EXECUTORCH_BUILD_XNNPACK=ON`, we will build the target [`xnn_executor_runner_lib`](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).
8497

85-
`libexecutorchdemo.so` wraps up the required XNNPACK Backend runtime library from `xnn_executor_runner_lib`, and adds an additional JNI layer using fbjni. This is later exposed to Java app.
98+
`libexecutorch_jni.so` wraps up the required XNNPACK Backend runtime library from `xnn_executor_runner_lib`, and adds an additional JNI layer using fbjni. This is later exposed to Java app.
8699

87100
2. Build the libraries:
88101

@@ -110,7 +123,7 @@ cmake .. \
110123
-DQNN_SDK_ROOT=$QNN_SDK \
111124
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
112125
```
113-
Similar to the XNNPACK library, with this setup, we compile `libexecutorchdemo.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.
126+
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.
114127

115128
`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)`.
116129

@@ -131,8 +144,8 @@ mkdir -p ../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64
131144
Copy the core libraries:
132145

133146
```bash
134-
cp ./examples/demo-apps/android/jni/libexecutorchdemo.so \
135-
../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
147+
cp ./examples/demo-apps/android/jni/libexecutorch_jni.so \
148+
../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/libexecutorch.so
136149
```
137150

138151
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.
@@ -153,8 +166,8 @@ cp ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtp.so ${QNN_SDK_ROOT}/lib/aarch64-
153166
Copy the core libraries:
154167

155168
```bash
156-
cp ./examples/demo-apps/android/jni/libexecutorchdemo.so \
157-
../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
169+
cp ./examples/demo-apps/android/jni/libexecutorch_jni.so \
170+
../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/libexecutorch.so
158171
```
159172

160173
## Running the App

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

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

0 commit comments

Comments
 (0)