-
Notifications
You must be signed in to change notification settings - Fork 975
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
187 lines (159 loc) · 6.52 KB
/
CMakeLists.txt
File metadata and controls
187 lines (159 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
cmake_minimum_required(VERSION 3.24) # 3.24 is required for WHOLE_ARCHIVE
project(executorch_jni)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
if(NOT ANDROID)
message(FATAL_ERROR "This directory is for Android build only")
endif()
set(EXECUTORCH_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../..")
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
if(NOT ANDROID_PLATFORM)
set(ANDROID_PLATFORM android-30)
endif()
# We need to download fbjni library from maven, and use its "prefab" library
# and headers, and link executorch library against that fbjni library.
# We don't know which NDK is used to compile fbjni, and we need to link our
# executorch library to the version which Android APK links against for runtime
# to ensure the libc++ dependencies are consistent.
# WARNING #
# Users need to use the SAME fbjni version here and in app gradle dependency
# for runtime compatibility!
if(NOT FBJNI_VERSION)
set(FBJNI_VERSION 0.5.1)
endif()
set(FBJNI_AAR_URL https://repo1.maven.org/maven2/com/facebook/fbjni/fbjni/${FBJNI_VERSION}/fbjni-${FBJNI_VERSION}.aar)
set(FBJNI_DOWNLOAD_PATH ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/fbjni.aar)
if(NOT EXISTS "${FBJNI_DOWNLOAD_PATH}")
file(DOWNLOAD "${FBJNI_AAR_URL}" "${FBJNI_DOWNLOAD_PATH}")
endif()
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
COMMAND unzip -o ${FBJNI_DOWNLOAD_PATH} -d ${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni
DEPENDS "${FBJNI_DOWNLOAD_PATH}"
)
add_custom_target(
fbjni_prefab
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/" "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
add_library(fbjni SHARED IMPORTED)
add_dependencies(fbjni fbjni_prefab)
set_target_properties(fbjni PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/libs/android.${ANDROID_ABI}/libfbjni.so"
)
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../lib/cmake/ExecuTorch)
find_package(executorch CONFIG REQUIRED)
target_link_options_shared_lib(executorch)
add_library(executorch_jni SHARED jni/jni_layer.cpp jni/log.cpp)
set(link_libraries)
list(
APPEND
link_libraries
executorch
extension_data_loader
extension_module
extension_runner_util
extension_tensor
extension_threadpool
fbjni
)
if(TARGET optimized_native_cpu_ops_lib)
list(
APPEND
link_libraries
optimized_native_cpu_ops_lib
optimized_kernels
portable_kernels
cpublas
eigen_blas
)
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
else()
list(APPEND link_libraries portable_ops_lib portable_kernels)
target_link_options_shared_lib(portable_ops_lib)
endif()
if(TARGET quantized_kernels)
list(APPEND link_libraries quantized_kernels quantized_ops_lib)
target_link_options_shared_lib(quantized_ops_lib)
endif()
if(TARGET qnn_executorch_backend)
list(APPEND link_libraries qnn_executorch_backend)
endif()
if(TARGET xnnpack_backend)
target_link_options_shared_lib(xnnpack_backend)
list(APPEND link_libraries xnnpack_backend XNNPACK pthreadpool cpuinfo microkernels-prod)
endif()
if(TARGET vulkan_backend)
target_link_options_shared_lib(vulkan_backend)
list(APPEND link_libraries vulkan_backend)
endif()
if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
list(APPEND link_libraries $<LINK_LIBRARY:WHOLE_ARCHIVE,custom_ops>)
endif()
if(TARGET pthreadpool)
target_include_directories(
executorch_jni
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include
)
target_include_directories(
executorch_jni
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include
)
endif()
if(EXECUTORCH_JNI_CUSTOM_LIBRARY)
list(APPEND link_libraries ${EXECUTORCH_JNI_CUSTOM_LIBRARY})
target_link_libraries(
executorch_jni -Wl,--whole-archive ${EXECUTORCH_JNI_CUSTOM_LIBRARY}
-Wl,--no-whole-archive
)
endif()
if(EXECUTORCH_BUILD_LLAMA_JNI)
target_sources(executorch_jni PRIVATE jni/jni_layer_llama.cpp jni/log.cpp)
list(APPEND link_libraries llama_runner llava_runner)
target_compile_definitions(executorch_jni PUBLIC EXECUTORCH_BUILD_LLAMA_JNI=1)
add_subdirectory(
${EXECUTORCH_ROOT}/examples/models/llava/runner
${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llava/runner
)
add_subdirectory(
${EXECUTORCH_ROOT}/examples/models/llama/runner
${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama/runner
)
if(NEURON_BUFFER_ALLOCATOR_LIB)
target_sources(
executorch_jni PRIVATE
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/mtk_llama_runner.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaModelChunk.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/LlamaRuntime.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/ModelChunk.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/MultiModelLoader.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/mask_builder.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/rotary_embedding.cpp
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner/llm_helper/token_embedding.cpp
)
target_include_directories(
executorch_jni PRIVATE
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/
${EXECUTORCH_ROOT}/examples/mediatek/executor_runner/llama_runner
)
add_library(libneuron_buffer_allocator SHARED IMPORTED)
set_property(TARGET libneuron_buffer_allocator PROPERTY IMPORTED_LOCATION ${NEURON_BUFFER_ALLOCATOR_LIB})
list(APPEND link_libraries neuron_backend libneuron_buffer_allocator)
target_compile_definitions(executorch_jni PRIVATE EXECUTORCH_BUILD_MEDIATEK=1)
endif()
endif()
target_include_directories(
executorch_jni PRIVATE ${_common_include_directories}
"${CMAKE_CURRENT_BINARY_DIR}/third-party/fbjni/prefab/modules/fbjni/include/"
)
target_compile_options(executorch_jni PUBLIC ${_common_compile_options})
target_link_libraries(executorch_jni ${link_libraries} log)