forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessGroupGlooAsyncTest.cpp
275 lines (228 loc) · 8.08 KB
/
ProcessGroupGlooAsyncTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <c10/cuda/CUDAGuard.h>
#include <c10/util/irange.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10d/FileStore.hpp>
#include <c10d/ProcessGroupGloo.hpp>
#include "CUDATest.hpp"
#include "TestUtils.hpp"
#include <gtest/gtest.h>
using namespace c10d::test;
using at::cuda::CUDAStream;
using c10d::ProcessGroup;
template <typename T, typename... Args>
std::vector<T> initialize(const std::string& path, int N, Args&&... args) {
std::vector<T> tests;
for (C10_UNUSED const auto i : c10::irange(N)) {
tests.push_back(std::move(T(path, std::forward<Args>(args)...)));
}
std::vector<std::thread> threads;
for (C10_UNUSED const auto i : c10::irange(N)) {
threads.push_back(std::thread([i, N, &tests] { tests[i].start(i, N); }));
}
for (auto& thread : threads) {
thread.join();
}
return tests;
}
class AsyncTest {
public:
AsyncTest(std::string path) : path_(std::move(path)) {}
AsyncTest(AsyncTest&& other) {
path_ = std::move(other.path_);
pg_ = std::move(other.pg_);
}
::c10d::ProcessGroupGloo& getProcessGroup() {
return *pg_;
}
void start(int rank, int size) {
auto store = c10::make_intrusive<::c10d::FileStore>(path_, size);
// Use tiny timeout to make this test run fast
auto options = ::c10d::ProcessGroupGloo::Options::create();
options->timeout = std::chrono::milliseconds(50);
options->devices.push_back(
::c10d::ProcessGroupGloo::createDeviceForHostname("127.0.0.1"));
pg_ = std::unique_ptr<::c10d::ProcessGroupGloo>(
new ::c10d::ProcessGroupGloo(store, rank, size, options));
}
protected:
std::string path_;
std::unique_ptr<::c10d::ProcessGroupGloo> pg_;
};
class AsyncInputIsOutputTest : public AsyncTest {
public:
AsyncInputIsOutputTest(const std::string& path, int numTensors)
: AsyncTest(path),
numTensors_(numTensors),
numDevices_(cudaNumDevices()) {
// Allocate inputs on available devices in a round robin fashion.
::at::globalContext().lazyInitCUDA();
inputs_.resize(numTensors_);
for (const auto i : c10::irange(numTensors_)) {
inputs_[i] = at::empty(
{16, 16},
at::device(
{at::kCUDA, static_cast<c10::DeviceIndex>(i % numDevices_)}));
}
// Allocate a stream per device.
//
// The "current stream" is set globally per device in THC, so we
// can't make two tensors on the same device use different streams
// and pass this along to the collective (since it uses the THC
// getters to retrieve the current stream).
//
at::cuda::OptionalCUDAGuard deviceGuard;
streams_.reserve(numDevices_);
for (const auto i : c10::irange(numDevices_)) {
deviceGuard.set_index(i);
streams_.push_back(at::cuda::getStreamFromPool());
}
}
void wait(c10::intrusive_ptr<ProcessGroup::Work>& work) {
c10::cuda::CUDAMultiStreamGuard guard(streams_);
work->wait();
}
std::vector<at::Tensor> getCpuTensors(const std::vector<at::Tensor>& gpu_tensors) {
std::vector<at::Tensor> outputs(gpu_tensors.size());
// For the duration of this function, make THC use our streams
c10::cuda::CUDAMultiStreamGuard guard(streams_);
// Copy inputs to outputs
for (unsigned i = 0; i < gpu_tensors.size(); i++) {
outputs[i] = gpu_tensors[i].cpu();
}
return outputs;
}
std::vector<at::Tensor> getTensors() {
return getCpuTensors(inputs_);
}
protected:
const int numTensors_;
const int numDevices_;
std::vector<at::Tensor> inputs_;
std::vector<CUDAStream> streams_;
};
class AsyncAllreduceTest : public AsyncInputIsOutputTest {
public:
AsyncAllreduceTest(const std::string& path, int numTensors)
: AsyncInputIsOutputTest(path, numTensors) {}
c10::intrusive_ptr<c10d::ProcessGroup::Work> run() {
// For the duration of this function, make THC use our streams
c10::cuda::CUDAMultiStreamGuard guard(streams_);
// Launch sleep on every stream
at::cuda::OptionalCUDAGuard deviceGuard;
for (const auto i : c10::irange(numDevices_)) {
deviceGuard.set_index(i);
cudaSleep(streams_[i], 10 * 1000 * 1000);
}
// Launch value initialization for every tensor
for (const auto i : c10::irange(numTensors_)) {
deviceGuard.set_index(i % numDevices_);
inputs_[i].fill_(pg_->getRank() * numTensors_ + i);
}
return pg_->allreduce(inputs_);
}
};
class AsyncBroadcastTest : public AsyncInputIsOutputTest {
public:
AsyncBroadcastTest(const std::string& path, int numTensors)
: AsyncInputIsOutputTest(path, numTensors) {}
c10::intrusive_ptr<c10d::ProcessGroup::Work> run(int rootRank, int rootTensor) {
// For the duration of this function, make THC use our streams
c10::cuda::CUDAMultiStreamGuard guard(streams_);
// Launch sleep on every stream
at::cuda::OptionalCUDAGuard deviceGuard;
for (const auto i : c10::irange(numDevices_)) {
deviceGuard.set_index(i);
cudaSleep(streams_[i], 10 * 1000 * 1000);
}
// Launch value initialization for every tensor
for (const auto i : c10::irange(numTensors_)) {
deviceGuard.set_index(i % numDevices_);
inputs_[i].fill_(pg_->getRank() * numTensors_ + i);
}
::c10d::BroadcastOptions options;
options.rootRank = rootRank;
options.rootTensor = rootTensor;
return pg_->broadcast(inputs_, options);
}
};
void runAsyncAllreduceTest(
const std::string& path,
size_t numProcesses = 4,
size_t numTensors = 2) {
auto tests = initialize<AsyncAllreduceTest>(path, numProcesses, numTensors);
std::vector<c10::intrusive_ptr<c10d::ProcessGroup::Work>> work(numProcesses);
for(const auto i : c10::irange(numProcesses)) {
work[i] = tests[i].run();
}
// Wait for work to complete
for(const auto i : c10::irange(numProcesses)) {
tests[i].wait(work[i]);
}
// Check results
for(const auto i : c10::irange(numProcesses)) {
const auto size = numProcesses * numTensors;
const auto expected = (size * (size - 1)) / 2;
auto tensors = tests[i].getTensors();
auto results = tests[i].getCpuTensors(work[i]->result());
EXPECT_EQ(tensors.size(), results.size());
for(const auto j : c10::irange(tensors.size())) {
auto& tensor = tensors[j];
auto data = tensor.data_ptr<float>();
auto& result_tensor = results[j];
auto result_data = result_tensor.data_ptr<float>();
EXPECT_EQ(tensor.numel(), result_tensor.numel());
for (const auto k : c10::irange(tensor.numel())) {
EXPECT_EQ(data[k], expected);
EXPECT_EQ(result_data[k], expected);
}
}
}
}
void runAsyncBroadcastTest(
const std::string& path,
size_t numProcesses = 4,
size_t numTensors = 1) {
auto tests = initialize<AsyncBroadcastTest>(path, numProcesses, numTensors);
// Try every permutation of root rank and root tensor
for(const auto rootRank : c10::irange(numProcesses)) {
for(const auto rootTensor : c10::irange(numTensors)) {
std::vector<c10::intrusive_ptr<c10d::ProcessGroup::Work>> work(numProcesses);
for(const auto i : c10::irange(numProcesses)) {
work[i] = tests[i].run(rootRank, rootTensor);
}
// Wait for work to complete
for(const auto i : c10::irange(numProcesses)) {
tests[i].wait(work[i]);
}
// Check results
const auto expected = (rootRank * numTensors + rootTensor);
for(const auto i : c10::irange(numProcesses)) {
auto tensors = tests[i].getTensors();
for (const auto & tensor : tensors) {
const auto *const data = tensor.data_ptr<float>();
for (const auto k : c10::irange(tensor.numel())) {
EXPECT_EQ(data[k], expected);
}
}
}
}
}
}
#ifdef USE_CUDA
TEST(ProcessGroupGlooAsyncTest, testAsyncAllreduce) {
if (!at::cuda::is_available()) {
LOG(INFO) << "CUDA not available, skipping testAsyncAllreduce";
return;
}
TemporaryFile file;
runAsyncAllreduceTest(file.path);
}
TEST(ProcessGroupGlooAsyncTest, testAsyncBroadcast) {
if (!at::cuda::is_available()) {
LOG(INFO) << "CUDA not available, skipping testAsyncBroadcast";
return;
}
TemporaryFile file;
runAsyncBroadcastTest(file.path);
}
#endif