Skip to content

Commit eb8eca8

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Android build improvements (#2725)
Summary: Pull Request resolved: #2725 Reviewed By: shoumikhin Differential Revision: D55464604 Pulled By: kirklandsign fbshipit-source-id: 460fe36372bf7ef23f4a7c4ebec849ecf7ee2f16
1 parent 694d841 commit eb8eca8

File tree

6 files changed

+148
-79
lines changed

6 files changed

+148
-79
lines changed

CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,6 @@ if(EXECUTORCH_BUILD_VULKAN)
428428
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan)
429429
endif()
430430

431-
if(EXECUTORCH_BUILD_ANDROID_JNI)
432-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/models/llama2/runner)
433-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/android)
434-
endif()
435-
436431
if(EXECUTORCH_BUILD_QNN)
437432
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/qualcomm)
438433
endif()

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,36 +64,46 @@ We build the required ExecuTorch runtime library to run the model.
6464

6565
#### XNNPACK
6666

67-
1. Configure the CMake target for the library with XNNPACK backend:
67+
1. Build the CMake target for the library with XNNPACK backend:
6868

6969
```bash
7070
export ANDROID_NDK=<path-to-android-ndk>
7171
export BUCK2=/tmp/buck2 # Or your buck path
7272

7373
rm -rf cmake-out && mkdir cmake-out && cd cmake-out
74+
75+
# Build the core executorch library
7476
cmake .. -DCMAKE_INSTALL_PREFIX=cmake-out \
75-
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
76-
-DANDROID_ABI=arm64-v8a \
77-
-DBUCK2=$BUCK \
78-
-DEXECUTORCH_BUILD_XNNPACK=ON \
79-
-DEXECUTORCH_BUILD_FLATC=OFF \
80-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
81-
-DFLATC_EXECUTABLE=$FLATC_EXECUTABLE \
82-
-DEXECUTORCH_BUILD_ANDROID_JNI=ON \
83-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
84-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON
77+
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
78+
-DANDROID_ABI="${ANDROID_ABI}" \
79+
-DBUCK2="${BUCK2}" \
80+
-DEXECUTORCH_BUILD_XNNPACK=ON \
81+
-DEXECUTORCH_BUILD_FLATC=OFF \
82+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
83+
-DFLATC_EXECUTABLE="${FLATC}" \
84+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON
85+
86+
cmake --build . -j16 --target install
8587
```
8688

8789
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).
8890

89-
`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.
90-
91-
2. Build the libraries:
91+
2. Build the Android extension:
9292

9393
```bash
94-
cmake --build . -j16
94+
95+
# Build the android extension
96+
cmake ../extension/android -DBUCK2="${BUCK2}" \
97+
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
98+
-DANDROID_ABI="${ANDROID_ABI}" \
99+
-DCMAKE_INSTALL_PREFIX=cmake-out \
100+
-Bextension/android
101+
102+
cmake --build ./extension/android -j16
95103
```
96104

105+
`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.
106+
97107
#### Qualcomm Hexagon NPU
98108

99109
1. Configure the CMake target for the library with Qualcomm Hexagon NPU (HTP) backend (XNNPACK also included):

examples/demo-apps/android/ExecuTorchDemo/setup.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,32 @@
88

99
set -eu
1010

11-
pushd cmake-out
12-
# Note: Set up ANDROID_NDK, ANDROID_ABI, BUCK2, and FLATC_EXECUTABLE
13-
cmake .. -DCMAKE_INSTALL_PREFIX=cmake-out \
11+
# Note: Set up ANDROID_NDK, ANDROID_ABI, BUCK2, and FLATC
12+
cmake . -DCMAKE_INSTALL_PREFIX=cmake-out \
1413
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
1514
-DANDROID_ABI="${ANDROID_ABI}" \
1615
-DBUCK2="${BUCK2}" \
1716
-DEXECUTORCH_BUILD_XNNPACK=ON \
1817
-DEXECUTORCH_BUILD_FLATC=OFF \
1918
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
2019
-DFLATC_EXECUTABLE="${FLATC}" \
21-
-DEXECUTORCH_BUILD_ANDROID_JNI=ON \
22-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON
20+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
21+
-Bcmake-out
2322

2423
if [ "$(uname)" == "Darwin" ]; then
2524
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
2625
else
2726
CMAKE_JOBS=$(( $(nproc) - 1 ))
2827
fi
29-
cmake --build . -j "${CMAKE_JOBS}"
30-
popd
28+
cmake --build cmake-out -j "${CMAKE_JOBS}" --target install
29+
30+
cmake extension/android -DBUCK2="${BUCK2}" \
31+
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
32+
-DANDROID_ABI="${ANDROID_ABI}" \
33+
-DCMAKE_INSTALL_PREFIX=cmake-out \
34+
-Bcmake-out/extension/android
35+
36+
cmake --build cmake-out/extension/android -j "${CMAKE_JOBS}"
3137

3238
JNI_LIBS_PATH="examples/demo-apps/android/ExecuTorchDemo/app/src/main/jniLibs"
3339
mkdir -p "${JNI_LIBS_PATH}/${ANDROID_ABI}"

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,39 @@ pushd cmake-out
3333
```
3434
4. Run the following command to configure the CMake build:
3535
```
36-
cmake .. -DBUCK2="$BUCK" \
36+
cmake .. -DCMAKE_INSTALL_PREFIX=cmake-out \
37+
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
38+
-DANDROID_ABI="${ANDROID_ABI}" \
39+
-DBUCK2="${BUCK2}" \
40+
-DEXECUTORCH_BUILD_XNNPACK=ON \
41+
-DEXECUTORCH_BUILD_FLATC=OFF \
42+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
43+
-DFLATC_EXECUTABLE="${FLATC}" \
44+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON
45+
46+
cmake --build . -j16 --target install
47+
48+
cmake ../examples/models/llama2 -DBUCK2="$BUCK" \
3749
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
3850
-DANDROID_ABI="$ANDROID_ABI" \
3951
-DCMAKE_INSTALL_PREFIX=cmake-out \
40-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
41-
-DEXECUTORCH_BUILD_FLATC=OFF \
42-
-DFLATC_EXECUTABLE="${FLATC}" \
43-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
44-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
45-
-DEXECUTORCH_BUILD_ANDROID_JNI=ON \
46-
-DEXECUTORCH_BUILD_XNNPACK=ON
47-
```
48-
5. Run the following command to build the JNI library:
49-
```
50-
cmake --build . -j50
52+
-Bexamples/models/llama2
53+
54+
cmake --build examples/models/llama2 -j16
55+
56+
cmake ../extension/android -DBUCK2="${BUCK2}" \
57+
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
58+
-DANDROID_ABI="${ANDROID_ABI}" \
59+
-DCMAKE_INSTALL_PREFIX=cmake-out \
60+
-DEXECUTORCH_BUILD_LLAMA_JNI=ON \
61+
-Bextension/android
62+
63+
cmake --build extension/android -j16
64+
5165
popd
5266
```
53-
6. Copy the built library to your app:
67+
68+
5. Copy the built library to your app:
5469
```
5570
JNI_LIBS_PATH="examples/demo-apps/android/LlamaDemo/app/src/main/jniLibs"
5671
mkdir -p "${JNI_LIBS_PATH}/${ANDROID_ABI}"

examples/demo-apps/android/LlamaDemo/setup.sh

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,46 @@
77

88
set -eu
99

10-
pushd cmake-out
11-
# Note: Set up ANDROID_NDK, ANDROID_ABI, BUCK2, and FLATC_EXECUTABLE
12-
cmake .. -DBUCK2="$BUCK" \
13-
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
14-
-DANDROID_ABI="$ANDROID_ABI" \
15-
-DCMAKE_INSTALL_PREFIX=cmake-out \
16-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
17-
-DEXECUTORCH_BUILD_FLATC=OFF \
18-
-DFLATC_EXECUTABLE="${FLATC}" \
19-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
20-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
21-
-DEXECUTORCH_BUILD_ANDROID_JNI=ON \
22-
-DEXECUTORCH_BUILD_XNNPACK=ON
10+
CMAKE_OUT=cmake-out
11+
# Note: Set up ANDROID_NDK, ANDROID_ABI, BUCK2, and FLATC
12+
cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
13+
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
14+
-DANDROID_ABI="${ANDROID_ABI}" \
15+
-DBUCK2="${BUCK2}" \
16+
-DEXECUTORCH_BUILD_XNNPACK=ON \
17+
-DEXECUTORCH_BUILD_FLATC=OFF \
18+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
19+
-DFLATC_EXECUTABLE="${FLATC}" \
20+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
21+
-B"${CMAKE_OUT}"
2322

2423
if [ "$(uname)" == "Darwin" ]; then
2524
CMAKE_JOBS=$(( $(sysctl -n hw.ncpu) - 1 ))
2625
else
2726
CMAKE_JOBS=$(( $(nproc) - 1 ))
2827
fi
29-
cmake --build . -j "${CMAKE_JOBS}"
30-
popd
28+
cmake --build "${CMAKE_OUT}" -j "${CMAKE_JOBS}" --target install
29+
30+
cmake examples/models/llama2 -DBUCK2="$BUCK" \
31+
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
32+
-DANDROID_ABI="$ANDROID_ABI" \
33+
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
34+
-B"${CMAKE_OUT}"/examples/models/llama2
35+
36+
cmake --build "${CMAKE_OUT}"/examples/models/llama2 -j "${CMAKE_JOBS}"
37+
38+
cmake extension/android -DBUCK2="${BUCK2}" \
39+
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
40+
-DANDROID_ABI="${ANDROID_ABI}" \
41+
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
42+
-DEXECUTORCH_BUILD_LLAMA_JNI=ON \
43+
-B"${CMAKE_OUT}"/extension/android
44+
45+
cmake --build "${CMAKE_OUT}"/extension/android -j "${CMAKE_JOBS}"
3146

3247
JNI_LIBS_PATH="examples/demo-apps/android/LlamaDemo/app/src/main/jniLibs"
3348
mkdir -p "${JNI_LIBS_PATH}/${ANDROID_ABI}"
34-
cp cmake-out/extension/android/libexecutorch_llama_jni.so "${JNI_LIBS_PATH}/${ANDROID_ABI}/"
49+
cp "${CMAKE_OUT}"/extension/android/libexecutorch_llama_jni.so "${JNI_LIBS_PATH}/${ANDROID_ABI}/"
3550

3651
pushd extension/android
3752
./gradlew build

extension/android/CMakeLists.txt

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,65 @@ cmake_minimum_required(VERSION 3.19)
88

99
project(executorch_jni)
1010

11+
if(NOT ANDROID)
12+
message(FATAL_ERROR "This directory is for Android build only")
13+
endif()
14+
1115
set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
1216
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
13-
17+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
1418
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
1519

1620
add_subdirectory(${EXECUTORCH_ROOT}/examples/third-party/fbjni
1721
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni)
1822

19-
if(CMAKE_TOOLCHAIN_ANDROID)
20-
add_library(executorch_jni SHARED jni/jni_layer.cpp)
21-
target_link_libraries(executorch_jni extension_data_loader extension_module
22-
portable_ops_lib fbjni)
23-
if(EXECUTORCH_BUILD_QNN)
24-
target_link_libraries(executorch_jni qnn_executorch_backend)
25-
endif()
26-
if(EXECUTORCH_BUILD_XNNPACK)
27-
target_link_libraries(executorch_jni xnnpack_backend)
28-
endif()
29-
if(EXECUTORCH_BUILD_VULKAN)
30-
target_link_libraries(executorch_jni vulkan_executor_runner_lib)
31-
endif()
32-
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
23+
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch)
24+
find_package(executorch CONFIG REQUIRED)
25+
target_link_options_shared_lib(executorch)
26+
target_link_options_shared_lib(portable_ops_lib)
3327

34-
add_library(executorch_llama_jni SHARED jni/jni_layer_llama.cpp)
35-
target_link_libraries(executorch_llama_jni fbjni llama_runner portable_ops_lib)
36-
if(EXECUTORCH_BUILD_XNNPACK)
37-
target_link_libraries(executorch_llama_jni xnnpack_backend)
38-
endif()
39-
if(EXECUTORCH_BUILD_VULKAN)
40-
target_link_libraries(executorch_llama_jni vulkan_executor_runner_lib)
41-
endif()
28+
set(link_libraries)
29+
list(APPEND link_libraries extension_data_loader extension_module executorch
30+
portable_ops_lib portable_kernels fbjni)
31+
32+
if(TARGET optimized_native_cpu_ops_lib)
33+
list(APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels
34+
cpublas eigen_blas)
35+
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
36+
endif()
37+
if(TARGET qnn_executorch_backend)
38+
list(APPEND link_libraries qnn_executorch_backend)
39+
endif()
40+
if(TARGET xnnpack_backend)
41+
target_link_options_shared_lib(xnnpack_backend)
42+
list(APPEND link_libraries xnnpack_backend XNNPACK pthreadpool cpuinfo)
43+
endif()
44+
if(TARGET vulkan_executor_runner_lib)
45+
list(APPEND link_libraries vulkan_executor_runner_lib)
46+
endif()
47+
48+
add_library(executorch_jni SHARED jni/jni_layer.cpp)
49+
target_link_libraries(executorch_jni ${link_libraries})
50+
target_include_directories(executorch_jni PRIVATE ${_common_include_directories})
51+
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
52+
53+
if(EXECUTORCH_BUILD_LLAMA_JNI)
54+
set(LLAMA_RUNNER_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama2/runner/libllama_runner.a)
55+
add_library(llama_runner STATIC IMPORTED)
56+
set_property(TARGET llama_runner PROPERTY IMPORTED_LOCATION ${LLAMA_RUNNER_PATH})
4257

58+
set(CUSTOM_OPS_LIB_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama2/custom_ops/libcustom_ops_lib.a)
59+
add_library(custom_ops_lib STATIC IMPORTED)
60+
set_property(TARGET custom_ops_lib PROPERTY IMPORTED_LOCATION ${CUSTOM_OPS_LIB_PATH})
61+
62+
set(CUSTOM_OPS_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama2/custom_ops/libcustom_ops.a)
63+
add_library(custom_ops STATIC IMPORTED)
64+
set_property(TARGET custom_ops PROPERTY IMPORTED_LOCATION ${CUSTOM_OPS_PATH})
65+
target_link_options_shared_lib(custom_ops_lib)
66+
67+
add_library(executorch_llama_jni SHARED jni/jni_layer_llama.cpp)
68+
target_include_directories(executorch_llama_jni PRIVATE ${_common_include_directories})
69+
target_link_libraries(executorch_llama_jni ${link_libraries} llama_runner
70+
custom_ops custom_ops_lib cpublas eigen_blas)
4371
target_compile_options(executorch_llama_jni PUBLIC ${_common_compile_options})
4472
endif()

0 commit comments

Comments
 (0)