Skip to content

Commit 48df2fd

Browse files
committed
[Android] Use threadpool
1 parent cd7d5c3 commit 48df2fd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

extension/android/CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ if(EXECUTORCH_BUILD_LLAMA_JNI)
6666
set_property(TARGET custom_ops PROPERTY IMPORTED_LOCATION ${CUSTOM_OPS_PATH})
6767
target_link_options_shared_lib(custom_ops_lib)
6868

69-
add_library(executorch_llama_jni SHARED jni/jni_layer_llama.cpp)
69+
if(TARGET pthreadpool)
70+
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp ../../backends/xnnpack/threadpool/cpuinfo_utils.cpp)
71+
else()
72+
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp)
73+
endif()
74+
add_library(executorch_llama_jni SHARED ${LLAMA_JNI_SRCS})
75+
if(TARGET pthreadpool)
76+
target_compile_definitions(executorch_llama_jni PRIVATE ET_USE_THREADPOOL=1)
77+
target_include_directories(executorch_llama_jni PUBLIC
78+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/cpuinfo/include)
79+
target_include_directories(executorch_llama_jni PUBLIC
80+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/xnnpack/third-party/pthreadpool/include)
81+
endif()
7082
target_include_directories(executorch_llama_jni PRIVATE ${_common_include_directories})
7183
target_link_libraries(executorch_llama_jni ${link_libraries} llama_runner
7284
custom_ops custom_ops_lib cpublas eigen_blas)

extension/android/jni/jni_layer_llama.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include <executorch/runtime/platform/platform.h>
2121
#include <executorch/runtime/platform/runtime.h>
2222

23+
#if defined(ET_USE_THREADPOOL)
24+
#include <executorch/backends/xnnpack/threadpool/cpuinfo_utils.h>
25+
#include <executorch/backends/xnnpack/threadpool/threadpool.h>
26+
#endif
27+
2328
#include <fbjni/ByteBuffer.h>
2429
#include <fbjni/fbjni.h>
2530

@@ -91,6 +96,21 @@ class ExecuTorchLlamaJni
9196
facebook::jni::alias_ref<jstring> model_path,
9297
facebook::jni::alias_ref<jstring> tokenizer_path,
9398
jfloat temperature) {
99+
#if defined(ET_USE_THREADPOOL)
100+
// Reserve 1 thread for the main thread.
101+
uint32_t num_performant_cores =
102+
torch::executorch::cpuinfo::get_num_performant_cores() - 1;
103+
if (num_performant_cores <= 0) {
104+
num_performant_cores = 1;
105+
}
106+
ET_LOG(
107+
Info, "Resetting threadpool with num threads = %d", num_performant_cores);
108+
if (num_performant_cores > 0) {
109+
torch::executorch::threadpool::get_threadpool()->_unsafe_reset_threadpool(
110+
num_performant_cores);
111+
}
112+
#endif
113+
94114
runner_ = std::make_unique<Runner>(
95115
model_path->toStdString().c_str(),
96116
tokenizer_path->toStdString().c_str(),

0 commit comments

Comments
 (0)