Skip to content

Default to cores/2 threads in JNI layer #6042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extension/android/jni/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@fbsource//tools/build_defs/android:fb_android_cxx_library.bzl", "fb_android_cxx_library")
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
load("@fbsource//xplat/executorch/codegen:codegen.bzl", "executorch_generated_lib")

oncall("executorch")
Expand Down Expand Up @@ -41,6 +42,8 @@ fb_android_cxx_library(
"//xplat/executorch/extension/module:module_static",
"//xplat/executorch/extension/runner_util:inputs_static",
"//xplat/executorch/extension/tensor:tensor_static",
"//xplat/executorch/extension/threadpool:threadpool_static",
third_party_dep("cpuinfo"),
],
)

Expand Down
24 changes: 24 additions & 0 deletions extension/android/jni/jni_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include <executorch/runtime/platform/platform.h>
#include <executorch/runtime/platform/runtime.h>

#ifdef ET_USE_THREADPOOL
#include <cpuinfo.h>
#include <executorch/extension/threadpool/threadpool.h>
#endif

#include <fbjni/ByteBuffer.h>
#include <fbjni/fbjni.h>

Expand Down Expand Up @@ -260,6 +265,25 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
}

module_ = std::make_unique<Module>(modelPath->toStdString(), load_mode);

#ifdef ET_USE_THREADPOOL
// Default to using cores/2 threadpool threads. The long-term plan is to
// improve performant core detection in CPUInfo, but for now we can use
// cores/2 as a sane default.
//
// Based on testing, this is almost universally faster than using all
// cores, as efficiency cores can be quite slow. In extreme cases, using
// all cores can be 10x slower than using cores/2.
//
// TODO Allow overriding this default from Java.
auto threadpool = executorch::extension::threadpool::get_threadpool();
if (threadpool) {
int thread_count = cpuinfo_get_processors_count() / 2;
if (thread_count > 0) {
threadpool->_unsafe_reset_threadpool(thread_count);
}
}
#endif
}

facebook::jni::local_ref<facebook::jni::JArrayClass<JEValue>> forward(
Expand Down
Loading