Skip to content

Commit e37af23

Browse files
authored
Avoid nested thread-pool locking in parallel_for (pytorch#22663)
Summary: Using Kleidi can invoke `parallel_for` inside an existing pool task. Calculating the inner chunk size queries the pool's thread count and relocks the mutex held by the outer call, hanging real model inference even with one thread. Honor `NoThreadPoolGuard` before querying the pool. Run nested nonempty ranges directly and preserve the outer thread number used for per-thread scratch storage. Cover nested work, thread-number preservation, and empty ranges in both source mirrors. AI-assisted: Codex. Reviewed By: digantdesai Differential Revision: D119433132 Pull Request resolved: pytorch#22663
1 parent d2f2f5f commit e37af23

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎extension/threadpool/test/thread_parallel_test.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ TEST_P(ParallelTest, TestAllInvoked) {
7070
}
7171
}
7272

73+
TEST_P(ParallelTest, NestedCallsPreserveOuterThreadNumber) {
74+
std::array<std::array<int, 3>, 10> visits{};
75+
EXPECT_TRUE(parallel_for(0, 10, 1, [&](int64_t begin, int64_t end) {
76+
const auto outer_thread = executorch::extension::get_thread_num();
77+
for (int64_t row = begin; row < end; ++row) {
78+
EXPECT_TRUE(
79+
parallel_for(2, 5, 1, [&](int64_t inner_begin, int64_t inner_end) {
80+
EXPECT_EQ(executorch::extension::get_thread_num(), outer_thread);
81+
for (int64_t column = inner_begin; column < inner_end; ++column) {
82+
++visits[row][column - 2];
83+
}
84+
}));
85+
EXPECT_TRUE(parallel_for(2, 2, 1, [&](int64_t, int64_t) {
86+
ADD_FAILURE() << "Empty nested range invoked its callback";
87+
}));
88+
EXPECT_EQ(executorch::extension::get_thread_num(), outer_thread);
89+
}
90+
}));
91+
for (const auto& row : visits) {
92+
for (const auto count : row) {
93+
EXPECT_EQ(count, 1);
94+
}
95+
}
96+
}
97+
7398
TEST_P(ParallelTest, TestAllInvokedWithMutex) {
7499
EXPECT_TRUE(parallel_for(0, 10, 1, [this](int64_t begin, int64_t end) {
75100
this->RunExclusiveTask(begin, end);

‎extension/threadpool/thread_parallel.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <tuple>
1212

1313
#include <executorch/extension/threadpool/threadpool.h>
14+
#include <executorch/extension/threadpool/threadpool_guard.h>
1415
#include <executorch/runtime/core/error.h>
1516
#include <executorch/runtime/kernel/thread_parallel_interface.h>
1617
#include <executorch/runtime/platform/assert.h>
@@ -61,6 +62,13 @@ bool parallel_for(
6162
begin,
6263
end);
6364
ET_CHECK_OR_RETURN_FALSE(grain_size > 0, "grain_size = %" PRId64, grain_size);
65+
if (NoThreadPoolGuard::is_enabled()) {
66+
// Querying the pool's size would lock the mutex held by the outer call.
67+
if (begin < end) {
68+
f(begin, end);
69+
}
70+
return true;
71+
}
6472
int64_t num_tasks = 0, chunk_size = 0;
6573
std::tie(num_tasks, chunk_size) =
6674
calc_num_tasks_and_chunk_size(begin, end, grain_size);

0 commit comments

Comments
 (0)