Skip to content

Commit 4953fc1

Browse files
committed
Android LLAMA demo app build rule
* Unfortunately, we need to build from ExecuTorch root for Android example, because we need Android toolchain and we can't find required packages like gflags, until we have a better solution. * Add the CMake rule for Android LLAMA JNI code. * extension_module depends on in its source.
1 parent 9cb0be6 commit 4953fc1

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ else()
206206
set(CMAKE_TOOLCHAIN_IOS OFF)
207207
endif()
208208

209+
# Detect if an Android toolchain is set.
210+
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*android\.toolchain\.cmake$")
211+
set(CMAKE_TOOLCHAIN_ANDROID ON)
212+
else()
213+
set(CMAKE_TOOLCHAIN_ANDROID OFF)
214+
endif()
215+
209216
# EXECUTORCH_BUILD_HOST_TARGETS: Option to control the building of host-only
210217
# tools like `flatc`, along with example executables like `executor_runner` and
211218
# libraries that it uses, like `gflags`. Disabling this can be helpful when
@@ -328,11 +335,6 @@ if(EXECUTORCH_BUILD_GTESTS)
328335
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/googletest)
329336
endif()
330337

331-
option(EXECUTORCH_BUILD_ANDROID_JNI "Build Android JNI" OFF)
332-
if(EXECUTORCH_BUILD_ANDROID_JNI)
333-
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/android)
334-
endif()
335-
336338
if(EXECUTORCH_BUILD_SDK)
337339
set(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER
338340
ON
@@ -365,6 +367,12 @@ if(EXECUTORCH_BUILD_VULKAN)
365367
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan)
366368
endif()
367369

370+
option(EXECUTORCH_BUILD_ANDROID_JNI "Build Android JNI" OFF)
371+
if(EXECUTORCH_BUILD_ANDROID_JNI)
372+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/models/llama2/runner)
373+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/android)
374+
endif()
375+
368376
option(EXECUTORCH_BUILD_QNN "Build the backends/qualcomm directory" OFF)
369377
if(EXECUTORCH_BUILD_QNN)
370378
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/qualcomm)

build/cmake_deps.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ excludes = [
246246
]
247247
deps = [
248248
"executorch",
249+
"extension_data_loader",
249250
"extension_module",
250251
"portable_kernels",
251252
"quantized_kernels",
252253
"xnnpack_backend",
253254
]
254-
# ---------------------------------- LLama start ----------------------------------
255+
# ---------------------------------- LLama end ----------------------------------

examples/models/llama2/runner/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
# It should also be cmake-lint clean.
1717
#
1818

19+
if(NOT EXECUTORCH_ROOT)
20+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
21+
endif()
22+
1923
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
2024
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
2125
#
@@ -34,7 +38,14 @@ list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
3438
target_include_directories(extension_module
3539
INTERFACE ${_common_include_directories})
3640

37-
add_library(llama_runner SHARED ${_llama_runner__srcs})
41+
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID)
42+
# Building a share library on iOS requires code signing
43+
# On Android we see duplicated registration when using shared lib
44+
add_library(llama_runner STATIC ${_llama_runner__srcs})
45+
else()
46+
add_library(llama_runner SHARED ${_llama_runner__srcs})
47+
endif()
48+
3849
target_link_libraries(
3950
llama_runner PUBLIC executorch portable_kernels extension_module
4051
extension_data_loader)

extension/android/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ add_subdirectory(
1818
${EXECUTORCH_ROOT}/examples/third-party/fbjni
1919
${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni)
2020

21-
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
21+
if(CMAKE_TOOLCHAIN_ANDROID)
2222
add_library(executorch_jni SHARED jni/jni_layer.cpp)
2323
target_link_libraries(executorch_jni extension_data_loader
24-
extension_module xnn_executor_runner_lib fbjni)
24+
extension_module fbjni)
2525
if(EXECUTORCH_BUILD_QNN)
2626
target_link_libraries(executorch_jni qnn_executorch_backend)
2727
endif()
2828
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
29+
30+
add_library(executorch_llama_jni SHARED jni/jni_layer_llama.cpp)
31+
target_link_libraries(executorch_llama_jni fbjni llama_runner portable_ops_lib)
32+
target_compile_options(executorch_llama_jni PUBLIC ${_common_compile_options})
2933
endif()

extension/android/jni/jni_layer_llama.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@
2323
#include <fbjni/ByteBuffer.h>
2424
#include <fbjni/fbjni.h>
2525

26+
#ifdef __ANDROID__
27+
#include <android/log.h>
28+
29+
// For Android, write to logcat
30+
void et_pal_emit_log_message(
31+
et_timestamp_t timestamp,
32+
et_pal_log_level_t level,
33+
const char* filename,
34+
const char* function,
35+
size_t line,
36+
const char* message,
37+
size_t length) {
38+
int android_log_level = ANDROID_LOG_UNKNOWN;
39+
if (level == 'D') {
40+
android_log_level = ANDROID_LOG_DEBUG;
41+
} else if (level == 'I') {
42+
android_log_level = ANDROID_LOG_INFO;
43+
} else if (level == 'E') {
44+
android_log_level = ANDROID_LOG_ERROR;
45+
} else if (level == 'F') {
46+
android_log_level = ANDROID_LOG_FATAL;
47+
}
48+
49+
__android_log_print(android_log_level, "LLAMA", "%s", message);
50+
}
51+
#endif
52+
2653
using namespace torch::executor;
2754

2855
namespace executorch_jni {

extension/module/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ if(NOT EXECUTORCH_ROOT)
1717
endif()
1818

1919
list(TRANSFORM _extension_module__srcs PREPEND "${EXECUTORCH_ROOT}/")
20-
if(CMAKE_TOOLCHAIN_IOS)
20+
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID)
2121
# Building a share library on iOS requires code signing
22+
# On Android we see duplicated registration when using shared lib
2223
add_library(extension_module STATIC ${_extension_module__srcs})
2324
else()
2425
add_library(extension_module SHARED ${_extension_module__srcs})
2526
endif()
26-
target_link_libraries(extension_module PRIVATE executorch)
27+
target_link_libraries(extension_module PRIVATE executorch extension_data_loader)
2728
target_include_directories(extension_module PUBLIC ${EXECUTORCH_ROOT}/..)
2829
target_compile_options(extension_module PUBLIC -Wno-deprecated-declarations
2930
-fPIC)

0 commit comments

Comments
 (0)