Skip to content

Commit daa217f

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Use threadpool (#2761)
Summary: Pull Request resolved: #2761 Reviewed By: shoumikhin Differential Revision: D55532937 Pulled By: kirklandsign fbshipit-source-id: cd0d8647ffff905d4ecebfad9ba30f55f6801a68
1 parent 6e2705e commit daa217f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-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: 16 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,17 @@ 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+
ET_LOG(Info, "Resetting threadpool to %d threads", num_performant_cores);
105+
torch::executorch::threadpool::get_threadpool()->_unsafe_reset_threadpool(
106+
num_performant_cores);
107+
}
108+
#endif
109+
94110
runner_ = std::make_unique<Runner>(
95111
model_path->toStdString().c_str(),
96112
tokenizer_path->toStdString().c_str(),

0 commit comments

Comments
 (0)