Skip to content

Commit e87af4c

Browse files
committed
Add cpu_thread setting logic to xnn_executor_runner
ghstack-source-id: f1452c802d39347ab465bc8fdf285814cb5deead ghstack-comment-id: 2695791073 Pull Request resolved: #8902
1 parent 9c3f962 commit e87af4c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

backends/xnnpack/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
142142
xnn_executor_runner gflags optimized_native_cpu_ops_lib ${xnn_executor_runner_libs}
143143
)
144144
target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})
145+
if(EXECUTORCH_BUILD_PTHREADPOOL)
146+
target_link_libraries(xnn_executor_runner extension_threadpool pthreadpool)
147+
target_compile_definitions(xnn_executor_runner PRIVATE ET_USE_THREADPOOL)
148+
endif()
145149
endif()
146150

147151
install(

examples/portable/executor_runner/executor_runner.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
#include <executorch/devtools/etdump/etdump_flatcc.h>
3636
#endif // ET_EVENT_TRACER_ENABLED
3737

38+
#if defined(ET_USE_THREADPOOL)
39+
#include <executorch/extension/threadpool/cpuinfo_utils.h>
40+
#include <executorch/extension/threadpool/threadpool.h>
41+
#endif
42+
3843
static uint8_t method_allocator_pool[4 * 1024U * 1024U]; // 4 MB
3944

4045
static uint8_t temp_allocator_pool[1024U * 1024U];
@@ -47,6 +52,10 @@ DEFINE_uint32(num_executions, 1, "Number of times to run the model.");
4752
#ifdef ET_EVENT_TRACER_ENABLED
4853
DEFINE_string(etdump_path, "model.etdump", "Write ETDump data to this path.");
4954
#endif // ET_EVENT_TRACER_ENABLED
55+
DEFINE_int32(
56+
cpu_threads,
57+
-1,
58+
"Number of CPU threads for inference. Defaults to -1, which implies we'll use a heuristic to derive the # of performant cores for a specific device.");
5059

5160
using executorch::extension::FileDataLoader;
5261
using executorch::runtime::Error;
@@ -124,6 +133,18 @@ int main(int argc, char** argv) {
124133
return 1;
125134
}
126135

136+
auto cpu_threads = FLAGS_cpu_threads;
137+
#if defined(ET_USE_THREADPOOL)
138+
uint32_t num_performant_cores = cpu_threads == -1
139+
? ::executorch::extension::cpuinfo::get_num_performant_cores()
140+
: static_cast<uint32_t>(cpu_threads);
141+
ET_LOG(
142+
Info, "Resetting threadpool with num threads = %d", num_performant_cores);
143+
if (num_performant_cores > 0) {
144+
::executorch::extension::threadpool::get_threadpool()
145+
->_unsafe_reset_threadpool(num_performant_cores);
146+
}
147+
#endif // ET_USE_THREADPOOL
127148
// Create a loader to get the data of the program file. There are other
128149
// DataLoaders that use mmap() or point to data that's already in memory, and
129150
// users can create their own DataLoaders to load from arbitrary sources.

0 commit comments

Comments
 (0)