Skip to content

Commit a70f7d7

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Android app integration (#547)
Summary: Pull Request resolved: #547 Test Plan: To repro, use Android Studio to open examples/demo-apps/android/ExecuTorchDemo. Follow https://www.internalfb.com/code/fbsource/[D49790694-V13]/xplat/executorch/examples/demo-apps/android/ExecuTorchDemo/README.md Then ``` cp dl3_xnnpack_fp32.pte ic3_xnnpack_fp32.pte examples/demo-apps/android/ExecuTorchDemo/app/src/main/assets/ ``` Also ``` cp libexecutorchdemo.so examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a/ ``` Differential Revision: D49790694 Pulled By: kirklandsign fbshipit-source-id: 7e8ea425cdff0b9b53610d1d12b52b45b7148a90
1 parent d935d9a commit a70f7d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4194
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@
4040
[submodule "examples/third-party/llama"]
4141
path = examples/third-party/llama
4242
url = https://github.com/facebookresearch/llama.git
43+
[submodule "examples/demo-apps/android/jni/third-party/fbjni"]
44+
path = examples/demo-apps/android/jni/third-party/fbjni
45+
url = https://github.com/facebookincubator/fbjni.git

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
296296
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
297297
endif()
298298

299+
# Add Android demo app JNI subdirectory
300+
if(EXECUTORCH_BUILD_ANDROID_DEMO_APP_JNI)
301+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/demo-apps/android/jni)
302+
endif()
303+
299304
option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER
300305
"Build the extension/data_loader directory" OFF)
301306
if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)

backends/xnnpack/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
100100
add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
101101
target_link_libraries(xnn_executor_runner ${xnn_executor_runner_libs})
102102
target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})
103+
104+
add_library(xnn_executor_runner_lib STATIC ${_xnn_executor_runner__srcs})
105+
target_link_libraries(xnn_executor_runner_lib ${xnn_executor_runner_libs})
106+
target_compile_options(xnn_executor_runner_lib
107+
PUBLIC ${_common_compile_options})
103108
endif()

build/cmake_deps.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ filters = [
7777
".fbs$",
7878
]
7979

80+
[targets.print_evalue]
81+
buck_targets = [
82+
"//extension/evalue_util:print_evalue",
83+
]
84+
filters = [
85+
".cpp$",
86+
]
87+
deps = [
88+
"executorch",
89+
]
90+
8091
[targets.size_test]
8192
buck_targets = [
8293
"//test:size_test",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
.idea
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild
9+
.cxx
10+
local.properties
11+
*.so
12+
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Android Demo App: ExecuTorch Setup
2+
3+
This is forked from [PyTorch android demo app](https://github.com/pytorch/android-demo-app)
4+
5+
This guide explains how to setup ExecuTorch for Android using a demo app. The app employs a DeepLab v3 model for image segmentation tasks and Inception v3 model for image classification tasks. Models are exported to ExecuTorch using XNNPACK FP32 backend.
6+
7+
## Pre-setup
8+
9+
1. Download [Android Studio and SDK](https://developer.android.com/studio).
10+
11+
2. Install buck2 binary (using MacOS Apple Silicon build as example):
12+
```bash
13+
curl -L -O https://github.com/facebook/buck2/releases/download/2023-07-18/buck2-aarch64-apple-darwin.zst
14+
pip3 install zstd
15+
zstd -cdq buck2-aarch64-apple-darwin.zst > /tmp/buck2 && chmod +x /tmp/buck2
16+
```
17+
18+
3. Install and link [Cmake](cmake.org/download) to a system directory or `$PATH`:
19+
20+
```bash
21+
ln -s /Applications/CMake.app/Contents/bin/cmake /usr/bin/cmake
22+
```
23+
24+
4. Clone ExecuTorch repository and update submodules:
25+
26+
```bash
27+
git clone https://github.com/pytorch/executorch.git
28+
cd executorch
29+
git submodule sync
30+
git submodule update --init
31+
```
32+
33+
5. Verify Python 3.10+ (standard since MacOS 13.5) and `pip3` installation:
34+
35+
```bash
36+
which python3 pip
37+
python3 --version
38+
```
39+
40+
6. Install PyTorch dependencies:
41+
42+
```bash
43+
./install_requirements.sh
44+
```
45+
46+
## Flatbuffers Compiler Setup
47+
48+
Run the following in the `flatbuffers` directory:
49+
50+
```bash
51+
cd third-party/flatbuffers
52+
rm -rf cmake-out && mkdir cmake-out && cd cmake-out
53+
cmake .. && cmake --build . --target flatc
54+
cd ../../..
55+
```
56+
57+
## ExecuTorch Configuration
58+
59+
Configure the libraries for Android:
60+
61+
```bash
62+
rm -rf cmake-out && mkdir cmake-out && cd cmake-out
63+
cmake .. \
64+
-DCMAKE_TOOLCHAIN_FILE=/path/to/ndk/build/cmake/android.toolchain.cmake \
65+
-DANDROID_ABI=arm64-v8a \
66+
-DBUCK2=/tmp/buck2 \
67+
-DFLATC_EXECUTABLE=$(realpath ../third-party/flatbuffers/cmake-out/flatc) \
68+
-DEXECUTORCH_BUILD_ANDROID_DEMO_APP_JNI=ON \
69+
-DEXECUTORCH_BUILD_XNNPACK=ON \
70+
-DEXECUTORCH_BUILD_FLATC=OFF \
71+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
72+
```
73+
74+
## Building and Copying Libraries
75+
76+
1. Build the libraries:
77+
78+
```bash
79+
cmake --build .
80+
```
81+
82+
2. Copy the libraries to the appropriate location to link against them:
83+
84+
Navigate to the build artifacts directory:
85+
86+
```bash
87+
mkdir -p ../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
88+
```
89+
90+
Copy the core libraries:
91+
92+
```bash
93+
cp ./examples/demo-apps/android/jni/libexecutorchdemo.so \
94+
../examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs/arm64-v8a
95+
```
96+
97+
Later, this shared library will be loaded by `NativePeer.java` in Java code.
98+
99+
Then return to the `executorch` directory:
100+
101+
```bash
102+
cd ..
103+
```
104+
105+
## Model Download and Bundling
106+
107+
1. Export a DeepLab v3 model and Inception v4 model backed with XNNPACK delegate and bundle it with
108+
the app:
109+
110+
```bash
111+
export FLATC_EXECUTABLE=$(realpath third-party/flatbuffers/cmake-out/flatc)
112+
python3 -m examples.backend.xnnpack_examples --model_name="dl3" --delegate
113+
python3 -m examples.backend.xnnpack_examples --model_name="ic4" --delegate
114+
cp dl3_xnnpack_fp32.pte ic4_xnnpack_fp32.pte examples/android_demo_apps/ExecuTorchDemo/app/src/main/assets/
115+
```
116+
117+
## Final Steps
118+
119+
1. Open the project `examples/android_demo_apps/ExecuTorchDemo` with Android Studio.
120+
121+
2. Run the app (^R)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "com.example.executorchdemo"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
applicationId = "com.example.executorchdemo"
12+
minSdk = 24
13+
targetSdk = 33
14+
versionCode = 1
15+
versionName = "1.0"
16+
17+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables { useSupportLibrary = true }
19+
externalNativeBuild { cmake { cppFlags += "" } }
20+
}
21+
22+
buildTypes {
23+
release {
24+
isMinifyEnabled = false
25+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
26+
}
27+
}
28+
compileOptions {
29+
sourceCompatibility = JavaVersion.VERSION_1_8
30+
targetCompatibility = JavaVersion.VERSION_1_8
31+
}
32+
kotlinOptions { jvmTarget = "1.8" }
33+
buildFeatures { compose = true }
34+
composeOptions { kotlinCompilerExtensionVersion = "1.4.3" }
35+
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
36+
}
37+
38+
dependencies {
39+
implementation("androidx.core:core-ktx:1.9.0")
40+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
41+
implementation("androidx.activity:activity-compose:1.7.0")
42+
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
43+
implementation("androidx.compose.ui:ui")
44+
implementation("androidx.compose.ui:ui-graphics")
45+
implementation("androidx.compose.ui:ui-tooling-preview")
46+
implementation("androidx.compose.material3:material3")
47+
implementation("androidx.appcompat:appcompat:1.6.1")
48+
implementation("androidx.camera:camera-core:1.3.0-rc02")
49+
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")
50+
implementation("com.facebook.soloader:soloader:0.10.5")
51+
implementation("com.facebook.fbjni:fbjni:0.5.1")
52+
testImplementation("junit:junit:4.13.2")
53+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
54+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
55+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
56+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
57+
debugImplementation("androidx.compose.ui:ui-tooling")
58+
debugImplementation("androidx.compose.ui:ui-test-manifest")
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.CAMERA" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
11+
android:fullBackupContent="@xml/backup_rules"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:roundIcon="@mipmap/ic_launcher_round"
15+
android:supportsRtl="true"
16+
android:theme="@style/Theme.ExecuTorchDemo"
17+
android:extractNativeLibs="true"
18+
tools:targetApi="31">
19+
20+
<uses-native-library android:name="libexecutorchdemo.so"
21+
android:required="false"/>
22+
23+
<uses-native-library android:name="libcdsprpc.so"
24+
android:required="false"/>
25+
26+
<uses-native-library android:name="libQnnHtp.so"
27+
android:required="false"/>
28+
29+
<uses-native-library android:name="libQnnHtpV69Skel.so"
30+
android:required="false"/>
31+
32+
<uses-native-library android:name="libQnnHtpV69Stub.so"
33+
android:required="false"/>
34+
35+
<uses-native-library android:name="libQnnSystem.so"
36+
android:required="false"/>
37+
38+
<activity
39+
android:name=".MainActivity"
40+
android:exported="true"
41+
android:label="@string/app_name"
42+
android:theme="@style/Theme.ExecuTorchDemo">
43+
<intent-filter>
44+
<action android:name="android.intent.action.MAIN" />
45+
46+
<category android:name="android.intent.category.LAUNCHER" />
47+
</intent-filter>
48+
</activity>
49+
<activity
50+
android:name=".ClassificationActivity"
51+
android:label="@string/app_name"
52+
android:theme="@style/Theme.ExecuTorchDemo">
53+
</activity>
54+
55+
</application>
56+
57+
</manifest>
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)