This repository was archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtorch_ucc.hpp
334 lines (276 loc) · 10.1 KB
/
torch_ucc.hpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* Copyright (C) Mellanox Technologies Ltd. 2020-2021. ALL RIGHTS RESERVED.
* See file LICENSE for terms.
*/
#pragma once
#include "torch_ucc_comm.hpp"
#include <torch/python.h>
#include <exception>
#include <memory>
#include <mutex>
#include <queue>
#include <vector>
#include <pybind11/chrono.h>
#include <c10d/ProcessGroup.hpp>
#include <c10d/Store.hpp>
#include <c10d/Types.hpp>
#include <c10d/Utils.hpp>
#ifdef USE_CUDA
#include <ATen/cuda/CUDAEvent.h>
#include <c10/cuda/CUDAStream.h>
#endif
namespace c10d {
#define TORCH_UCC_DEVICE_NOT_SET -2
#define TORCH_UCX_MAKE_P2P_TAG(_tag, _rank, _comm) \
((((uint64_t)(_tag)) << TORCH_UCX_TAG_BITS_OFFSET) | \
(((uint64_t)(_rank)) << TORCH_UCX_RANK_BITS_OFFSET) | \
(((uint64_t)(_comm)) << TORCH_UCX_COMM_BITS_OFFSET))
#define TORCH_UCX_MAKE_OOB_TAG(_tag, _rank, _comm) \
((((uint64_t)(_tag)) << TORCH_UCX_OOB_BITS_OFFSET) | \
(((uint64_t)(_rank)) << TORCH_UCX_RANK_BITS_OFFSET) | \
(((uint64_t)(_rank)) << TORCH_UCX_COMM_BITS_OFFSET))
#define TORCH_UCX_MAKE_SEND_TAG(_ucp_tag, _tag, _rank, _comm) \
do { \
(_ucp_tag) = TORCH_UCX_MAKE_P2P_TAG((_tag), (_rank), (_comm)); \
} while (0)
#define TORCH_UCX_ANY_SOURCE (TORCH_UCX_MAX_RANK - 1)
#define TORCH_UCX_ANY_SOURCE_MASK (~TORCH_UCX_RANK_MASK)
#define TORCH_UCX_SPECIFIC_SOURCE_MASK ((uint64_t)-1)
#define TORCH_UCX_MAKE_RECV_TAG(_ucp_tag, _ucp_tag_mask, _tag, _rank, _comm) \
do { \
(_ucp_tag) = TORCH_UCX_MAKE_P2P_TAG((_tag), (_rank), (_comm)); \
if ((_rank) == TORCH_UCX_ANY_SOURCE) { \
(_ucp_tag_mask) = TORCH_UCX_ANY_SOURCE_MASK; \
} else { \
(_ucp_tag_mask) = TORCH_UCX_SPECIFIC_SOURCE_MASK; \
} \
} while (0)
#define TORCH_UCX_MAKE_OOB_SEND_TAG(_ucp_tag, _tag, _rank, _comm) \
do { \
(_ucp_tag) = TORCH_UCX_MAKE_OOB_TAG((_tag), (_rank), (_comm)); \
} while (0)
#define TORCH_UCX_MAKE_OOB_RECV_TAG( \
_ucp_tag, _ucp_tag_mask, _tag, _rank, _comm) \
do { \
(_ucp_tag) = TORCH_UCX_MAKE_OOB_TAG((_tag), (_rank), (_comm)); \
(_ucp_tag_mask) = (uint64_t)-1; \
} while (0)
enum torch_ucx_tag_type_t { TORCH_UCX_P2P_TAG, TORCH_UCX_OOB_TAG };
struct event_pool_t {
#ifdef USE_CUDA
std::queue<std::unique_ptr<at::cuda::CUDAEvent>> event_pool;
#endif
std::mutex event_pool_mutex;
};
class CommPG;
class ProcessGroupUCC : public ProcessGroup {
public:
class WorkData {
public:
std::vector<at::Tensor> src;
std::vector<at::Tensor> dst;
WorkData() {}
virtual ~WorkData() = default;
};
class AlltoallWorkData : public WorkData {
public:
AlltoallWorkData(int size)
: send_lengths(size),
send_offsets(size),
recv_lengths(size),
recv_offsets(size) {}
std::vector<uint32_t> send_lengths;
std::vector<uint32_t> send_offsets;
std::vector<uint32_t> recv_lengths;
std::vector<uint32_t> recv_offsets;
};
class AllgatherWorkData : public WorkData {
public:
AllgatherWorkData(int size)
: recv_lengths(size),
recv_offsets(size) {}
std::vector<uint64_t> recv_lengths;
std::vector<uint64_t> recv_offsets;
};
class WorkUCC : public ProcessGroup::Work {
friend class ProcessGroupUCC;
friend class CommPG;
public:
WorkUCC(
OpType opType,
ucc_status_t status,
ucc_coll_req_h request,
ucc_ee_h ee,
CommBase* comm)
: ProcessGroup::Work(-1, opType),
status_(status),
request_(request),
comm_(comm) {}
~WorkUCC();
bool isCompleted() override;
bool isSuccess() const override;
bool wait(std::chrono::milliseconds timeout = kUnsetTimeout) override;
void finalize();
std::unique_ptr<WorkData> data;
#ifdef USE_CUDA
std::unique_ptr<at::cuda::CUDAEvent> fence = nullptr;
event_pool_t* ep = nullptr;
#endif
protected:
ucc_status_t status_;
ucc_coll_req_h request_;
CommBase* comm_;
};
explicit ProcessGroupUCC(
const c10::intrusive_ptr<Store>& store,
int rank = -1,
int size = -1);
void initComm(c10::Device dev);
~ProcessGroupUCC() override;
c10::intrusive_ptr<ProcessGroup::Work> collective_post(
OpType opType,
ucc_coll_args_t& coll,
std::unique_ptr<ProcessGroupUCC::WorkData> data,
c10::Device dev);
c10::intrusive_ptr<ProcessGroup::Work> broadcast(
std::vector<at::Tensor>& data,
const BroadcastOptions& opts = BroadcastOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> allreduce(
std::vector<at::Tensor>& tensors,
const AllreduceOptions& opts = AllreduceOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> allreduce_coalesced(
std::vector<at::Tensor>& tensors,
const AllreduceCoalescedOptions& opts =
AllreduceCoalescedOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> reduce(
std::vector<at::Tensor>& tensors,
const ReduceOptions& opts = ReduceOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> allgather(
std::vector<std::vector<at::Tensor>>& outputTensors,
std::vector<at::Tensor>& inputTensors,
const AllgatherOptions& opts = AllgatherOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> allgather_base(
at::Tensor& outputBuffer,
at::Tensor& inputBuffer,
const AllgatherOptions& opts = AllgatherOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> barrier(
const BarrierOptions& opts = BarrierOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> gather(
std::vector<std::vector<at::Tensor>>& outputTensors,
std::vector<at::Tensor>& inputTensors,
const GatherOptions& opts = GatherOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> scatter(
std::vector<at::Tensor>& outputTensors,
std::vector<std::vector<at::Tensor>>& inputTensors,
const ScatterOptions& opts = ScatterOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> reduce_scatter(
std::vector<at::Tensor>& outputTensors,
std::vector<std::vector<at::Tensor>>& inputTensors,
const ReduceScatterOptions& opts = ReduceScatterOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> alltoall_base(
at::Tensor& outputTensor,
at::Tensor& inputTensor,
std::vector<int64_t>& outputSplitSizes,
std::vector<int64_t>& inputSplitSizes,
const AllToAllOptions& opts = AllToAllOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> alltoall(
std::vector<at::Tensor>& outputTensors,
std::vector<at::Tensor>& inputTensors,
const AllToAllOptions& opts = AllToAllOptions()) override;
c10::intrusive_ptr<ProcessGroup::Work> send(
std::vector<at::Tensor>& tensors,
int dstRank,
int tag) override;
c10::intrusive_ptr<ProcessGroup::Work> recv(
std::vector<at::Tensor>& tensors,
int srcRank,
int tag) override;
c10::intrusive_ptr<ProcessGroup::Work> recvAnysource(
std::vector<at::Tensor>& tensors,
int tag) override;
static c10::intrusive_ptr<ProcessGroup> createProcessGroupUCC(
const c10::intrusive_ptr<::c10d::Store>& store,
int rank,
int size,
const std::chrono::duration<float>& timeout);
static void ProcessGroupUCCConstructor() __attribute__((constructor)) {
py::object module = py::module::import("torch.distributed");
py::object register_backend =
module.attr("Backend").attr("register_backend");
register_backend("ucc", py::cpp_function(createProcessGroupUCC));
}
protected:
torch_ucc_oob_coll_info_t oob;
std::shared_ptr<CommPG> comm;
uint32_t comm_id;
std::vector<ucp_ep_h> eps;
ucc_team_h team;
ucc_ee_h cuda_ee;
#ifdef USE_CUDA
std::unique_ptr<at::cuda::CUDAStream> stream = nullptr;
event_pool_t ep;
#endif
};
class CommPG {
CommUCX ucx_comm;
CommUCC ucc_comm;
c10::DeviceIndex device_index;
std::mutex mutex;
std::thread progress_thread;
std::condition_variable queue_produce_cv;
std::condition_variable queue_consume_cv;
std::deque<c10::intrusive_ptr<ProcessGroupUCC::WorkUCC>> progress_queue;
bool stop_progress_loop;
public:
c10::DeviceIndex cuda_device_index;
CommPG(torch_ucc_oob_coll_info_t* oob_info,
c10::Device dev);
~CommPG();
void ucx_connect_eps(
std::vector<ucp_ep_h>& eps,
torch_ucc_oob_coll_info_t* oob);
void ucx_disconnect_eps(
std::vector<ucp_ep_h>& eps,
torch_ucc_oob_coll_info_t* oob);
void ucc_create_team(
ucc_team_h& team,
torch_ucc_oob_coll_info_t* oob_info);
void ucc_destroy_team(ucc_team_h& team);
c10::intrusive_ptr<ProcessGroup::Work> enqueue_p2p(
OpType opType,
ucc_coll_req_h request);
#ifdef USE_CUDA
c10::intrusive_ptr<ProcessGroupUCC::WorkUCC> enqueue_cuda_collective(
OpType opType,
ucc_coll_args_t& coll,
std::unique_ptr<ProcessGroupUCC::WorkData> data,
ucc_team_h& team,
ucc_ee_h ee,
std::unique_ptr<at::cuda::CUDAEvent> cuda_ev,
const at::cuda::CUDAStream& stream,
event_pool_t* ep);
#endif
c10::intrusive_ptr<ProcessGroupUCC::WorkUCC> enqueue_collective(
OpType opType,
ucc_coll_args_t& coll,
std::unique_ptr<ProcessGroupUCC::WorkData> data,
ucc_team_h& team);
static std::shared_ptr<CommPG> get_comm(
uint32_t& id,
c10::Device dev,
torch_ucc_oob_coll_info_t *oob);
void progress_loop();
ucc_coll_req_h send_nb(
ucp_ep_h ep,
void* data,
ucs_memory_type_t mtype,
size_t size,
ucp_tag_t ucp_tag);
ucc_coll_req_h recv_nb(
void* data,
ucs_memory_type_t mtype,
size_t size,
ucp_tag_t ucp_tag,
ucp_tag_t ucp_tag_mask);
};
} // namespace c10d