-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcommunication.cpp
More file actions
346 lines (315 loc) · 9.51 KB
/
communication.cpp
File metadata and controls
346 lines (315 loc) · 9.51 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
// clang-format off
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-present NVIDIA CORPORATION & AFFILIATES.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
*/
// clang-format on
#include "multidevice/communication.h"
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include "ir/cloner.h"
#include "ir/iostream.h"
namespace nvfuser {
std::ostream& operator<<(std::ostream& os, const CommunicationType& type) {
switch (type) {
case CommunicationType::Gather:
os << "Gather";
break;
case CommunicationType::Allgather:
os << "Allgather";
break;
case CommunicationType::Scatter:
os << "Scatter";
break;
case CommunicationType::Reduce:
os << "Reduce";
break;
case CommunicationType::Allreduce:
os << "Allreduce";
break;
case CommunicationType::ReduceScatter:
os << "ReduceScatter";
break;
case CommunicationType::Broadcast:
os << "Broadcast";
break;
case CommunicationType::SendRecv:
os << "SendRecv";
break;
case CommunicationType::AllToAll:
os << "AllToAll";
break;
}
return os;
}
namespace {
bool hasRoot(CommunicationType type) {
switch (type) {
case CommunicationType::Gather:
case CommunicationType::Scatter:
case CommunicationType::Reduce:
case CommunicationType::Broadcast:
case CommunicationType::SendRecv:
return true;
case CommunicationType::Allgather:
case CommunicationType::Allreduce:
case CommunicationType::ReduceScatter:
case CommunicationType::AllToAll:
return false;
}
std::unreachable();
}
bool isReduction(CommunicationType type) {
switch (type) {
case CommunicationType::Reduce:
case CommunicationType::Allreduce:
case CommunicationType::ReduceScatter:
return true;
case CommunicationType::Gather:
case CommunicationType::Allgather:
case CommunicationType::Scatter:
case CommunicationType::Broadcast:
case CommunicationType::SendRecv:
case CommunicationType::AllToAll:
return false;
default:
NVF_THROW("unrecognized CommunicationType: ", type);
}
}
} // namespace
Communication::Communication(
IrBuilderPasskey passkey,
CommunicationType type,
TensorView* out,
TensorView* in,
Team team,
Val* root,
RedOpType red_op,
CommunicatorBackend backend)
: Expr(passkey) {
NVF_ERROR(
in->getDeviceMesh().size() > 0,
"The input mesh size must be greater than 0.");
NVF_ERROR(
out->getDeviceMesh().size() > 0,
"The output mesh size must be greater than 0.");
addInput(in);
addInput(root);
addOutput(out);
addDataAttribute(type);
addDataAttribute(team);
addDataAttribute(red_op);
addDataAttribute(backend);
validate();
}
Communication::Communication(
IrBuilderPasskey passkey,
CommunicationType type,
TensorView* out,
TensorView* in,
Team team,
DeviceIdxType root,
RedOpType red_op,
CommunicatorBackend backend)
: Communication(
passkey,
type,
out,
in,
team,
IrBuilder::createInContainer<Val>(
passkey.ir_container_,
root,
DataType::Index),
red_op,
backend) {}
void Communication::validate() {
if (root()->isConstScalar() && root()->isIntegralScalar()) {
auto root_val = root()->evaluate().as<int64_t>();
NVF_ERROR(
hasRoot(type()) == (root_val >= 0),
"Root ",
root_val,
" is not expected by CommunicationType ",
type());
}
NVF_ERROR(isReduction(type()) == (reduceOp() != RedOpType::UNUSED));
}
NVFUSER_DEFINE_CLONE_AND_CREATE(Communication)
std::string Communication::toInlineString(const int indent_size) const {
std::stringstream ss;
indent(ss, indent_size) << "Communication " << name() << " ("
<< "type=" << type() << ", " << "team=(" << team()
<< ")";
if (hasRoot(type())) {
ss << ", root=" << root()->toInlineString();
}
if (!inputs().empty()) {
ss << ", input=" << in();
}
if (!outputs().empty()) {
ss << ", output=" << out();
}
ss << ", backend=" << backend();
ss << ")";
return ss.str();
}
std::string Communication::toString(int indent_size) const {
return toInlineString(indent_size) + "\n";
}
std::ostream& operator<<(std::ostream& os, const P2PCommunicationType& type) {
switch (type) {
case P2PCommunicationType::SEND:
os << "send";
break;
case P2PCommunicationType::RECV:
os << "recv";
break;
}
return os;
}
P2PCommunication::P2PCommunication(
IrBuilderPasskey passkey,
P2PCommunicationType type,
TensorView* buffer,
Val* peer,
CommunicatorBackend backend)
: Expr(passkey) {
addInput(buffer);
addDataAttribute(type);
addAttribute(peer);
addDataAttribute(backend);
}
NVFUSER_DEFINE_CLONE_AND_CREATE(P2PCommunication)
std::string P2PCommunication::toInlineString(const int indent_size) const {
std::stringstream ss;
indent(ss, indent_size) << "P2PCommunication " << name() << " ("
<< "type=" << type() << ", " << "buffer=" << buffer()
<< ", " << "peer=" << peer() << ", "
<< "backend=" << backend() << ")";
return ss.str();
}
std::string P2PCommunication::toString(int indent_size) const {
return toInlineString(indent_size) + "\n";
}
MoeDispatch::MoeDispatch(
IrBuilderPasskey passkey,
TensorView* out_x,
TensorView* out_topk_idx,
TensorView* out_topk_weights,
TensorView* out_src_idx,
TensorView* out_n_tokens_to_rank,
TensorView* out_n_tokens_from_rank,
TensorView* in_x,
TensorView* in_topk_idx,
TensorView* in_topk_weights,
int64_t num_experts,
CommunicatorBackend backend)
: Expr(passkey) {
addInput(in_x);
addInput(in_topk_idx);
addInput(in_topk_weights);
addOutput(out_x);
addOutput(out_topk_idx);
addOutput(out_topk_weights);
addOutput(out_src_idx);
addOutput(out_n_tokens_to_rank);
addOutput(out_n_tokens_from_rank);
addDataAttribute(num_experts);
addDataAttribute(backend);
validate();
}
NVFUSER_DEFINE_CLONE_AND_CREATE(MoeDispatch)
std::string MoeDispatch::toInlineString(int indent_size) const {
std::stringstream ss;
indent(ss, indent_size) << "Dispatch " << name() << " ("
<< "num_experts=" << numExperts() << ", "
<< "backend=" << backend() << ", "
<< "in=" << inX() << ", "
<< "topk_idx=" << inTopkIdx() << ", "
<< "topk_weights=" << inTopkWeights() << ", "
<< "out=" << outX() << ")";
return ss.str();
}
std::string MoeDispatch::toString(int indent_size) const {
return toInlineString(indent_size) + "\n";
}
void MoeDispatch::validate() {
NVF_CHECK(numExperts() > 0, "num_experts must be positive.");
NVF_CHECK(inX()->isA<TensorView>(), "in_x must be a TensorView.");
NVF_CHECK(inTopkIdx()->isA<TensorView>(), "topk_idx must be a TensorView.");
NVF_CHECK(
isIntegralType(inTopkIdx()->getDataType()), "topk_idx must be integral.");
NVF_CHECK(
isFloatingPointType(inTopkWeights()->getDataType()),
"topk_weights must be floating point.");
NVF_CHECK(
isIntegralType(outTopkIdx()->getDataType()),
"out_topk_idx must be integral.");
NVF_CHECK(
isFloatingPointType(outTopkWeights()->getDataType()),
"out_topk_weights must be floating point.");
NVF_CHECK(
isIntegralType(outSrcIdx()->getDataType()),
"out_src_idx must be integral.");
NVF_CHECK(
isIntegralType(outTokensToRank()->getDataType()),
"out_n_tokens_to_rank must be integral.");
NVF_CHECK(
isIntegralType(outTokensFromRank()->getDataType()),
"out_n_tokens_from_rank must be integral.");
}
MoeCombine::MoeCombine(
IrBuilderPasskey passkey,
TensorView* out_x,
TensorView* in_x,
TensorView* in_topk_weights,
TensorView* in_src_idx,
TensorView* in_n_tokens_to_rank,
TensorView* in_n_tokens_from_rank,
Val* num_tokens,
CommunicatorBackend backend)
: Expr(passkey) {
addInput(in_x);
addInput(in_topk_weights);
addInput(in_src_idx);
addInput(in_n_tokens_to_rank);
addInput(in_n_tokens_from_rank);
addInput(num_tokens);
addOutput(out_x);
addDataAttribute(backend);
validate();
}
NVFUSER_DEFINE_CLONE_AND_CREATE(MoeCombine)
std::string MoeCombine::toInlineString(int indent_size) const {
std::stringstream ss;
indent(ss, indent_size) << "Combine " << name() << " ("
<< "backend=" << backend() << ", "
<< "in=" << inX() << ", "
<< "topk_weights=" << inTopkWeights() << ", "
<< "src_idx=" << inSrcIdx() << ", "
<< "out=" << outX() << ")";
return ss.str();
}
std::string MoeCombine::toString(int indent_size) const {
return toInlineString(indent_size) + "\n";
}
void MoeCombine::validate() {
NVF_CHECK(inX()->isA<TensorView>(), "in_x must be a TensorView.");
NVF_CHECK(
isFloatingPointType(inTopkWeights()->getDataType()),
"in_topk_weights must be floating point.");
NVF_CHECK(
isIntegralType(inSrcIdx()->getDataType()),
"in_src_idx must be integral.");
NVF_CHECK(
isIntegralType(inTokensToRank()->getDataType()),
"in_n_tokens_to_rank must be integral.");
NVF_CHECK(
isIntegralType(inTokensFromRank()->getDataType()),
"in_n_tokens_from_rank must be integral.");
}
} // namespace nvfuser