Skip to content

Commit 077f24c

Browse files
pytorchbotdbort
andauthored
Migrate extension/threadpool to new namespace (#5849)
Migrate extension/threadpool to new namespace (#5825) Summary: Pull Request resolved: #5825 Migrate these headers under the new ::executorch::extension:: namespace. Add temporary aliases from the old ::torch::executorch namespace so we can migrate users incrementally. Note that this code incorrectly used `::torch::executorch` instead of `::torch::executor`, so the aliases use the old namespace. Also, now that we guarantee C++17, we can start using single-line namespace definition syntax. While I'm here, fix some comments to use Doxygen format, and reword a bit. Reviewed By: mergennachin Differential Revision: D63782662 fbshipit-source-id: 673e82269f65a368396929f6c06eb2cdd0903134 (cherry picked from commit 436afce) Co-authored-by: Dave Bort <[email protected]>
1 parent fd88913 commit 077f24c

File tree

7 files changed

+103
-89
lines changed

7 files changed

+103
-89
lines changed

extension/threadpool/cpuinfo_utils.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <executorch/runtime/platform/assert.h>
9+
#include <executorch/extension/threadpool/cpuinfo_utils.h>
10+
1011
#include <fstream>
1112
#include <mutex>
1213
#include <string>
1314
#include <vector>
1415

15-
#include "cpuinfo_utils.h"
16+
#include <executorch/runtime/platform/assert.h>
1617

17-
namespace torch {
18-
namespace executorch {
19-
namespace cpuinfo {
18+
namespace executorch::extension::cpuinfo {
2019

2120
// Ignore revisions (last digit (4 LSBs))
2221
#define CPUINFO_ARM_MIDR_CORTEX_A520 UINT32_C(0x410FD800)
@@ -171,6 +170,4 @@ uint32_t get_num_performant_cores() {
171170
}
172171
}
173172

174-
} // namespace cpuinfo
175-
} // namespace executorch
176-
} // namespace torch
173+
} // namespace executorch::extension::cpuinfo

extension/threadpool/cpuinfo_utils.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
#include <cpuinfo.h>
1212

13-
namespace torch {
14-
namespace executorch {
15-
namespace cpuinfo {
13+
namespace executorch::extension::cpuinfo {
1614

1715
uint32_t get_num_performant_cores();
1816

19-
} // namespace cpuinfo
20-
} // namespace executorch
21-
} // namespace torch
17+
} // namespace executorch::extension::cpuinfo
18+
19+
namespace torch::executorch::cpuinfo { // DEPRECATED
20+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
21+
// to the new `::executorch` namespaces. Note that threadpool incorrectly used
22+
// the namespace `torch::executorch` instead of `torch::executor`.
23+
using ::executorch::extension::cpuinfo::get_num_performant_cores; // DEPRECATED
24+
} // namespace torch::executorch::cpuinfo

extension/threadpool/test/threadpool_test.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
#include <gtest/gtest.h>
9+
#include <executorch/extension/threadpool/threadpool.h>
10+
1011
#include <mutex>
1112
#include <numeric>
1213
#include <random>
1314

14-
#include <executorch/extension/threadpool/threadpool.h>
1515
#include <executorch/extension/threadpool/threadpool_guard.h>
1616

17+
#include <gtest/gtest.h>
18+
1719
using namespace ::testing;
1820

1921
namespace {
@@ -63,7 +65,7 @@ void run_lambda_with_size(
6365
size_t grain_size) {
6466
size_t num_grains = div_round_up(range, grain_size);
6567

66-
auto threadpool = torch::executorch::threadpool::get_threadpool();
68+
auto threadpool = ::executorch::extension::threadpool::get_threadpool();
6769
threadpool->run(f, range);
6870
}
6971
} // namespace
@@ -82,7 +84,7 @@ TEST(ThreadPoolTest, ParallelAdd) {
8284
}
8385
};
8486

85-
auto threadpool = torch::executorch::threadpool::get_threadpool();
87+
auto threadpool = ::executorch::extension::threadpool::get_threadpool();
8688
EXPECT_GT(threadpool->get_thread_count(), 1);
8789

8890
generate_add_test_inputs(a, b, c_ref, c, vector_size);
@@ -125,7 +127,7 @@ TEST(ThreadPoolTest, ParallelReduce) {
125127
}
126128
};
127129

128-
auto threadpool = torch::executorch::threadpool::get_threadpool();
130+
auto threadpool = ::executorch::extension::threadpool::get_threadpool();
129131
EXPECT_GT(threadpool->get_thread_count(), 1);
130132

131133
generate_reduce_test_inputs(a, c_ref, vector_size);
@@ -142,46 +144,50 @@ TEST(ThreadPoolTest, ParallelReduce) {
142144
// Copied from
143145
// caffe2/aten/src/ATen/test/test_thread_pool_guard.cp
144146
TEST(TestNoThreadPoolGuard, TestThreadPoolGuard) {
145-
auto threadpool_ptr = torch::executorch::threadpool::get_pthreadpool();
147+
auto threadpool_ptr = ::executorch::extension::threadpool::get_pthreadpool();
146148

147149
ASSERT_NE(threadpool_ptr, nullptr);
148150
{
149-
torch::executorch::threadpool::NoThreadPoolGuard g1;
150-
auto threadpool_ptr1 = torch::executorch::threadpool::get_pthreadpool();
151+
::executorch::extension::threadpool::NoThreadPoolGuard g1;
152+
auto threadpool_ptr1 =
153+
::executorch::extension::threadpool::get_pthreadpool();
151154
ASSERT_EQ(threadpool_ptr1, nullptr);
152155

153156
{
154-
torch::executorch::threadpool::NoThreadPoolGuard g2;
155-
auto threadpool_ptr2 = torch::executorch::threadpool::get_pthreadpool();
157+
::executorch::extension::threadpool::NoThreadPoolGuard g2;
158+
auto threadpool_ptr2 =
159+
::executorch::extension::threadpool::get_pthreadpool();
156160
ASSERT_EQ(threadpool_ptr2, nullptr);
157161
}
158162

159163
// Guard should restore prev value (nullptr)
160-
auto threadpool_ptr3 = torch::executorch::threadpool::get_pthreadpool();
164+
auto threadpool_ptr3 =
165+
::executorch::extension::threadpool::get_pthreadpool();
161166
ASSERT_EQ(threadpool_ptr3, nullptr);
162167
}
163168

164169
// Guard should restore prev value (pthreadpool_)
165-
auto threadpool_ptr4 = torch::executorch::threadpool::get_pthreadpool();
170+
auto threadpool_ptr4 = ::executorch::extension::threadpool::get_pthreadpool();
166171
ASSERT_NE(threadpool_ptr4, nullptr);
167172
ASSERT_EQ(threadpool_ptr4, threadpool_ptr);
168173
}
169174

170175
TEST(TestNoThreadPoolGuard, TestRunWithGuard) {
171176
const std::vector<int64_t> array = {1, 2, 3};
172177

173-
auto pool = torch::executorch::threadpool::get_threadpool();
178+
auto pool = ::executorch::extension::threadpool::get_threadpool();
174179
int64_t inner = 0;
175180
{
176181
// Run on same thread
177-
torch::executorch::threadpool::NoThreadPoolGuard g1;
182+
::executorch::extension::threadpool::NoThreadPoolGuard g1;
178183
auto fn = [&array, &inner](const size_t task_id) {
179184
inner += array[task_id];
180185
};
181186
pool->run(fn, 3);
182187

183188
// confirm the guard is on
184-
auto threadpool_ptr = torch::executorch::threadpool::get_pthreadpool();
189+
auto threadpool_ptr =
190+
::executorch::extension::threadpool::get_pthreadpool();
185191
ASSERT_EQ(threadpool_ptr, nullptr);
186192
}
187193
ASSERT_EQ(inner, 6);

extension/threadpool/threadpool.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
*/
88

99
#include <executorch/extension/threadpool/threadpool.h>
10+
11+
#include <algorithm>
12+
#include <atomic>
13+
#include <memory>
14+
1015
#include <executorch/extension/threadpool/threadpool_guard.h>
1116
#include <executorch/runtime/platform/assert.h>
12-
#include <algorithm>
1317

1418
#include <cpuinfo.h>
1519

16-
#include <atomic>
17-
#include <memory>
18-
19-
namespace torch {
20-
namespace executorch {
21-
namespace threadpool {
20+
namespace executorch::extension::threadpool {
2221

2322
#if !(defined(WIN32))
2423
namespace {
@@ -138,6 +137,4 @@ pthreadpool_t get_pthreadpool() {
138137
return threadpool->threadpool_.get();
139138
}
140139

141-
} // namespace threadpool
142-
} // namespace executorch
143-
} // namespace torch
140+
} // namespace executorch::extension::threadpool

extension/threadpool/threadpool.h

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
#pragma once
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
28

3-
#include <pthreadpool.h>
9+
#pragma once
410

5-
// @nolint PATTERNLINT Ok to use stdlib for this optional library
611
#include <functional>
7-
// @nolint PATTERNLINT Ok to use stdlib for this optional library
812
#include <memory>
9-
// @nolint PATTERNLINT Ok to use stdlib for this optional library
1013
#include <mutex>
1114

12-
namespace torch {
13-
namespace executorch {
14-
namespace threadpool {
15+
#include <pthreadpool.h>
16+
17+
namespace executorch::extension::threadpool {
1518

1619
class ThreadPool final {
1720
public:
@@ -26,55 +29,64 @@ class ThreadPool final {
2629
ThreadPool& operator=(const ThreadPool&) = delete;
2730

2831
// Make threadpool non-movable.
29-
// For now this is non-movable, but if we want to have clients
30-
// such as say torch::executorch::Executor, to be able to own
31-
// threadpool, then we will have to make this movable.
3232
ThreadPool(ThreadPool&&) = delete;
3333
ThreadPool& operator=(ThreadPool&&) = delete;
3434

3535
size_t get_thread_count() const;
3636

37-
/*
38-
* Resets the threadpool by creating a new threadpool with requested # of
39-
* threads. This is not a thread safe call. When calling this method, threads
40-
* of the threadpool might be doing some work. Some other code may also be
41-
* holding on to the threadpool pointer, that is no longer valid. This is a
42-
* private API, which will later be replaced by something that allows creating
43-
* of threadpool with requested size and use such a threadpool with backend
44-
* delegates, custom ops or optimized lib.
37+
/**
38+
* INTERNAL: Resets the threadpool by creating a new threadpool with requested
39+
* # of threads. This is not a thread safe call. When calling this method,
40+
* threads of the threadpool might be doing some work. Some other code may
41+
* also be holding on to the threadpool pointer, that is no longer valid. This
42+
* is a private API, which will later be replaced by something that allows
43+
* creating of threadpool with requested size and use such a threadpool with
44+
* backend delegates, custom ops or optimized lib.
4545
*/
46+
[[deprecated("This API is experimental and may change without notice.")]]
4647
bool _unsafe_reset_threadpool(uint32_t num_threads);
4748

48-
// Run, in parallel, function fn(task_id) over task_id in range [0, range).
49-
// This function is blocking. All input is processed by the time it returns.
50-
// NoThreadPoolGuard (see threadpool_guard.h) can used to disable
51-
// use of multiple threads with the scope of the guard
52-
// When NoThreadPoolGuard is not used all calls to run method are serialized.
49+
/**
50+
* Run, in parallel, function fn(task_id) over task_id in range [0, range).
51+
* This function is blocking. All input is processed by the time it returns.
52+
* NoThreadPoolGuard (see threadpool_guard.h) can used to disable use of
53+
* multiple threads with the scope of the guard When NoThreadPoolGuard is not
54+
* used all calls to run method are serialized.
55+
*/
5356
void run(const std::function<void(size_t)>& fn, size_t range);
5457

5558
private:
5659
friend pthreadpool_t get_pthreadpool();
5760

5861
private:
59-
// This mutex is used inside get_thread_count API but it is not
60-
// really needed. Since data members of ThreadPool objects are not
61-
// really mutable.
62-
// Figure out if we will allow set_num_threads API, in which mutex
63-
// will be useful. Otherwise remove it.
64-
// TODO(kimishpatel)
62+
// This mutex is used inside get_thread_count API but it is not really needed
63+
// since data members of ThreadPool objects are not really mutable.
64+
// TODO(kimishpatel): Figure out if we will allow set_num_threads API, in
65+
// which case this mutex will be useful. Otherwise remove it.
6566
mutable std::mutex mutex_;
6667
std::unique_ptr<pthreadpool, decltype(&pthreadpool_destroy)> threadpool_;
6768
};
6869

69-
// Return a singleton instance of ThreadPool for ATen/TH multithreading.
70+
/**
71+
* Returns the singleton instance of ThreadPool for ATen/TH multithreading.
72+
*/
7073
ThreadPool* get_threadpool();
7174

72-
// Exposes the underlying implementation of ThreadPool.
73-
// Only for use in external libraries so as to unify threading across
74-
// internal (i.e. ATen, etc.) and external (e.g. NNPACK, QNNPACK, XNNPACK)
75-
// use cases.
75+
/**
76+
* Returns the underlying pthreadpool instance used by the implementation of
77+
* ThreadPool returned by `get_threadpool()`. Only for use in external libraries
78+
* so as to unify threading across internal (i.e. ATen, etc.) and external (e.g.
79+
* NNPACK, QNNPACK, XNNPACK) use cases.
80+
*/
7681
pthreadpool_t get_pthreadpool();
7782

78-
} // namespace threadpool
79-
} // namespace executorch
80-
} // namespace torch
83+
} // namespace executorch::extension::threadpool
84+
85+
namespace torch::executorch::threadpool { // DEPRECATED
86+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
87+
// to the new `::executorch` namespaces. Note that threadpool incorrectly used
88+
// the namespace `torch::executorch` instead of `torch::executor`.
89+
using ::executorch::extension::threadpool::get_pthreadpool; // DEPRECATED
90+
using ::executorch::extension::threadpool::get_threadpool; // DEPRECATED
91+
using ::executorch::extension::threadpool::ThreadPool; // DEPRECATED
92+
} // namespace torch::executorch::threadpool

extension/threadpool/threadpool_guard.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
#include <executorch/extension/threadpool/threadpool_guard.h>
1010

11-
namespace torch {
12-
namespace executorch {
13-
namespace threadpool {
11+
namespace executorch::extension::threadpool {
1412

1513
thread_local bool NoThreadPoolGuard_enabled = false;
1614

@@ -22,6 +20,4 @@ void NoThreadPoolGuard::set_enabled(bool enabled) {
2220
NoThreadPoolGuard_enabled = enabled;
2321
}
2422

25-
} // namespace threadpool
26-
} // namespace executorch
27-
} // namespace torch
23+
} // namespace executorch::extension::threadpool

extension/threadpool/threadpool_guard.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
#pragma once
1010

11-
namespace torch {
12-
namespace executorch {
13-
namespace threadpool {
11+
namespace executorch::extension::threadpool {
1412

1513
// A RAII, thread local (!) guard that enables or disables guard upon
1614
// construction, and sets it back to the original value upon destruction.
@@ -29,6 +27,11 @@ struct NoThreadPoolGuard {
2927
const bool prev_mode_;
3028
};
3129

32-
} // namespace threadpool
33-
} // namespace executorch
34-
} // namespace torch
30+
} // namespace executorch::extension::threadpool
31+
32+
namespace torch::executorch::threadpool { // DEPRECATED
33+
// TODO(T197294990): Remove these deprecated aliases once all users have moved
34+
// to the new `::executorch` namespaces. Note that threadpool incorrectly used
35+
// the namespace `torch::executorch` instead of `torch::executor`.
36+
using ::executorch::extension::threadpool::NoThreadPoolGuard; // DEPRECATED
37+
} // namespace torch::executorch::threadpool

0 commit comments

Comments
 (0)