Skip to content

Commit

Permalink
Disable per-session thread pool for web (#18480)
Browse files Browse the repository at this point in the history
### Description
ORT web prefers to use a global thread pool for all inference sessions.
See how OrtCreateSession is implemented in
https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/wasm/api.cc#L183
.

Application code can only the global thread poo. However, internal
testing code still often use per-session threadpool. This pr is to fix
the inconsistency.

### Motivation and Context
Replace PR #18476
  • Loading branch information
snnn authored Jan 11, 2024
1 parent 5678317 commit 053ddfe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion onnxruntime/core/framework/session_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ struct FreeDimensionOverride {
* Configuration information for a session.
*/
struct SessionOptions {
#if defined(__wasm__) && defined(__EMSCRIPTEN_PTHREADS__)
static constexpr bool DEFAULT_USE_PER_SESSION_THREADS = false;
#else
static constexpr bool DEFAULT_USE_PER_SESSION_THREADS = true;
#endif
ExecutionMode execution_mode = ExecutionMode::ORT_SEQUENTIAL;

// set the execution order of the graph
Expand Down Expand Up @@ -129,7 +134,8 @@ struct SessionOptions {

// By default the session uses its own set of threadpools, unless this is set to false.
// Use this in conjunction with the CreateEnvWithGlobalThreadPools API.
bool use_per_session_threads = true;
bool use_per_session_threads = DEFAULT_USE_PER_SESSION_THREADS;

bool thread_pool_allow_spinning = true;

// Deterministic compute is likely not as performant. This option is default to false.
Expand Down
18 changes: 18 additions & 0 deletions onnxruntime/test/framework/inference_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ TEST(InferenceSessionTests, ModelMetadata) {
}
#endif
TEST(InferenceSessionTests, CheckRunLogger) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
SessionOptions so;

so.session_logid = "CheckRunLogger";
Expand Down Expand Up @@ -837,6 +840,9 @@ TEST(InferenceSessionTests, PreAllocateOutputVector) {
}

TEST(InferenceSessionTests, ConfigureVerbosityLevel) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
SessionOptions so;

so.session_logid = "ConfigureVerbosityLevel";
Expand Down Expand Up @@ -2661,6 +2667,9 @@ class InferenceSessionTestSharingAllocator : public InferenceSessionWrapper {

// Ensure sessions use the same allocator. It uses ORT created allocator.
TEST(InferenceSessionTests, AllocatorSharing_EnsureSessionsUseSameOrtCreatedAllocator) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
auto logging_manager = std::make_unique<logging::LoggingManager>(
std::unique_ptr<ISink>(new CLogSink()), logging::Severity::kVERBOSE, false,
LoggingManager::InstanceType::Temporal);
Expand Down Expand Up @@ -2706,6 +2715,9 @@ TEST(InferenceSessionTests, AllocatorSharing_EnsureSessionsUseSameOrtCreatedAllo

// Ensure sessions don't use the same allocator. It uses ORT created allocator.
TEST(InferenceSessionTests, AllocatorSharing_EnsureSessionsDontUseSameOrtCreatedAllocator) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
auto logging_manager = std::make_unique<logging::LoggingManager>(
std::unique_ptr<ISink>(new CLogSink()), logging::Severity::kVERBOSE, false,
LoggingManager::InstanceType::Temporal);
Expand Down Expand Up @@ -2758,6 +2770,9 @@ class InferenceSessionTestSharingInitializer : public InferenceSessionWrapper {
};

TEST(InferenceSessionTests, InitializerSharing_EnsureSessionsUseUserAddedInitializer) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
auto logging_manager = std::make_unique<logging::LoggingManager>(
std::unique_ptr<ISink>(new CLogSink()), logging::Severity::kVERBOSE, false,
LoggingManager::InstanceType::Temporal);
Expand Down Expand Up @@ -2942,6 +2957,9 @@ TEST(InferenceSessionTests, GlobalThreadPoolWithDenormalAsZero) {

// test inter thread pool with setting denormal as zero
TEST(InferenceSessionTests, InterThreadPoolWithDenormalAsZero) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
// test if denormal-as-zero mode is supported
if (!SetDenormalAsZero(false)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ TEST_F(ActivationOpTest, Softplus) {
}

TEST_F(ActivationOpNoInfTest, Softsign) {
if constexpr (!SessionOptions::DEFAULT_USE_PER_SESSION_THREADS) {
GTEST_SKIP() << "Skipping the test";
}
// TODO: Unskip when fixed #41968513
if (DefaultDmlExecutionProvider().get() != nullptr) {
GTEST_SKIP() << "Skipping because of the following error: The difference between expected[i] and output[i] is 1, which exceeds threshold";
Expand Down

0 comments on commit 053ddfe

Please sign in to comment.