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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
#include "coll_custom_medium_all_reduce_mesh_executor.h"
namespace hccl
{
CollCustomMediumAllReduceMeshExecutor::CollCustomMediumAllReduceMeshExecutor(const HcclDispatcher dispatcher,
std::unique_ptr<TopoMatcher> &topoMatcher)
: CollAllReduceExecutor(dispatcher, topoMatcher)
{
CCLMemSlice_ = false;
DMAReduceFlag_ = true;
}
// Calculate the amount of scratch memory to request
HcclResult CollCustomMediumAllReduceMeshExecutor::CalcScratchMemSize(u64 &scratchMemSize)
{
// We don't need to use Scratch memory
scratchMemSize = 0U;
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][CalcScratchMemSize] scratchMemSize: %u",
scratchMemSize);
return HCCL_SUCCESS;
}
// Calculate the number of streams to be requested
HcclResult CollCustomMediumAllReduceMeshExecutor::CalcStreamNum(u32 &streamNum)
{
// One stream is required for each remote rank
u32 totalStreamNum = topoAttr_.deviceNumPerAggregation;
streamNum = totalStreamNum - 1U;
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][CalcStreamNum] streamNum: %u", streamNum);
return HCCL_SUCCESS;
}
// Calculate the number of Notify to be requested
HcclResult CollCustomMediumAllReduceMeshExecutor::CalcNotifyNum(u32 streamNum, u32 ¬ifyNum)
{
notifyNum = 2U * streamNum;
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][CalcNotifyNum] notifyNum: %u", notifyNum);
return HCCL_SUCCESS;
}
// Set up the level-0 mesh topology required for the AllReduce operation
HcclResult CollCustomMediumAllReduceMeshExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport> &opTransport)
{
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][CalcNotifyNum]");
// Define the source and destination memory types for communication
TransportMemType inputType = TransportMemType::CCL_INPUT;
TransportMemType outputType = TransportMemType::CCL_OUTPUT;
// Construct a mesh topology for level 0
CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
commParaLevel0.meshSinglePlane = true;
// Compute and populate the transport plan for level-0 communication domain
CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
return HCCL_SUCCESS;
}
// Calculate the number of iterations for loop processing
u64 CollCustomMediumAllReduceMeshExecutor::CalcLoopMaxCount(const u64 cclBuffSize, const u32 unitSize)
{
u64 maxCountPerLoop = cclBuffSize / unitSize;
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][CalcLoopMaxCount] maxCountPerLoop: %u",
maxCountPerLoop);
return maxCountPerLoop;
}
// Process data for a single iteration of the AllReduce algorithm execution
HcclResult CollCustomMediumAllReduceMeshExecutor::KernelRun(const OpParam ¶m, ExecMem &execMem)
{
// Get sub-communication domain information for level 0
CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
// Dertermine the information for the topology
u32 rankSize = level0CommInfo.localRankSize;
u32 rankId = level0CommInfo.localRank;
// Perform AllReduce
if (IsHealthyRank(rankId))
{
HealthyPerformAllReduce(param, execMem);
}
else
{
UnhealthyPerformAllReduce(param, execMem);
}
// Resolving unhealthy ranks
if (IsProxyRank(rankId))
{
ProxyPerformSend(param, execMem);
}
if (!IsHealthyRank(rankId))
{
UnhealthyPerformReceive(param, execMem);
}
HCCL_WARNING("[HCCLContest][CollCustomMediumAllReduceMeshExecutor][KernelRun] localRank: %u, localRankSize: %u",
level0CommInfo.localRank, level0CommInfo.localRankSize);
return HCCL_SUCCESS;
}
// The healthy rank pulls and reduces data from each rank
HcclResult CollCustomMediumAllReduceMeshExecutor::HealthyPerformAllReduce(const OpParam ¶m, ExecMem &execMem)
{
// Retrieve the sub-communication domain information and the master stream
CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
// Dertermine the information for the topology and the data
u32 rankSize = level0CommInfo.localRankSize;
u32 rankId = level0CommInfo.localRank;
u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
u64 chunkSize = execMem.count * unitSize;
// Copy data from user input to CCL_Out
DeviceMem src = DeviceMem::create(execMem.inputPtr, chunkSize);
DeviceMem dst = DeviceMem::create(execMem.outputMem.ptr(), chunkSize);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, masterStream));
// Copy data from user input to user output
src = DeviceMem::create(execMem.inputPtr, chunkSize);
dst = DeviceMem::create(execMem.outputPtr, chunkSize);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, masterStream));
// Post notify signals from master stream to all slave streams
CHK_RET(MainPostToSlaves(param, execMem));
// Slave streams wait for master's notification
CHK_RET(SlavesWaitForMain(param, execMem));
for (u32 round = 1; round < rankSize; round++)
{
// Get the slave stream assigned for communication with dstRank
u32 dstRank = (round + rankId) % rankSize;
Stream &subStream = algResResp_->slaveStreams[round - 1];
// Only receive data from unhealthy ranks, other ranks not only receive but also send
if (!IsHealthyRank(dstRank))
{
// Wait for notification from the remote rank
CHK_RET(level0CommInfo.links[dstRank]->RxAck(subStream));
}
else
{
// Notify remote rank data has been ready
CHK_RET(level0CommInfo.links[dstRank]->TxAck(subStream));
// Wait for notification from the remote rank
CHK_RET(level0CommInfo.links[dstRank]->RxAck(subStream));
}
// Source address on the remote rank's CCL_Out (remote)
void *srcRemoteMemPtr = nullptr;
CHK_RET(level0CommInfo.links[dstRank]->GetRemoteMem(UserMemType::OUTPUT_MEM, &srcRemoteMemPtr));
DeviceMem srcRemote = DeviceMem::create(static_cast<char *>(srcRemoteMemPtr), chunkSize);
// Destination address on the local rank's user output (local)
void *dstLocalMemPtr = static_cast<char *>(execMem.outputPtr);
DeviceMem dstLocal = DeviceMem::create(static_cast<char *>(dstLocalMemPtr), chunkSize);
// Perform HcclD2DMemcpyAsync: Copy and reduce data from remote CCL_Out to local user output
CHK_RET(HcclReduceAsync(dispatcher_, static_cast<void *>(srcRemote.ptr()), chunkSize / unitSize,
param.DataDes.dataType, param.reduceType, subStream,
static_cast<void *>(dstLocal.ptr()),
level0CommInfo.links[dstRank]->GetRemoteRank(),
level0CommInfo.links[dstRank]->GetLinkType(),
INLINE_REDUCE_BIT));
// Only receive data from unhealthy ranks, other ranks not only receive but also send
if (!IsHealthyRank(dstRank))
{
// Notify data transfer and operations are completed
CHK_RET(level0CommInfo.links[dstRank]->TxDataSignal(subStream));
}
else
{
// Notify the remote rank that data transfer and operations have completed
CHK_RET(level0CommInfo.links[dstRank]->TxDataSignal(subStream));
// Wait for data transfer and operations has been completed
CHK_RET(level0CommInfo.links[dstRank]->RxDataSignal(subStream));
}
}
// Slave streams notify the master stream that their tasks are completed
CHK_RET(SlavesPostToMain(param, execMem));
// The master stream waits for all slave streams to complete their tasks
CHK_RET(MainWaitForSlaves(param, execMem));
return HCCL_SUCCESS;
}
// Unhealthy ranks only need to prepare data
HcclResult CollCustomMediumAllReduceMeshExecutor::UnhealthyPerformAllReduce(const OpParam ¶m, ExecMem &execMem)
{
// Retrieve the sub-communication domain information and the master stream
CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
// Dertermine the information for the topology and the data
u32 rankSize = level0CommInfo.localRankSize;
u32 rankId = level0CommInfo.localRank;
u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
u64 chunkSize = execMem.count * unitSize;
// Copy data from user input to CCL_Out
DeviceMem src = DeviceMem::create(execMem.inputPtr, chunkSize);
DeviceMem dst = DeviceMem::create(execMem.outputMem.ptr(), chunkSize);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, masterStream));
// Post notify signals from master stream to all slave streams
CHK_RET(MainPostToSlaves(param, execMem));
// Slave streams wait for master's notification
CHK_RET(SlavesWaitForMain(param, execMem));
for (u32 round = 1; round < rankSize; round++)
{
// Get the slave stream assigned for communication with dstRank
u32 dstRank = (round + rankId) % rankSize;
Stream &subStream = algResResp_->slaveStreams[round - 1];
// Skipping unhealthy ranks
if (!IsHealthyRank(dstRank))
{
// Slave stream do empty job
CHK_RET(SlaveExecEmptyTask(param, execMem, round - 1));
continue;
}
// Notify remote rank data has been ready
CHK_RET(level0CommInfo.links[dstRank]->TxAck(subStream));
// Ensure data transfer and operations are completed
CHK_RET(level0CommInfo.links[dstRank]->RxDataSignal(subStream));
}
// Slave streams notify the master stream that their tasks are completed
CHK_RET(SlavesPostToMain(param, execMem));
// The master stream waits for all slave streams to complete their tasks
CHK_RET(MainWaitForSlaves(param, execMem));
return HCCL_SUCCESS;
}
// The proxy rank passes the result to the unhealthy rank
HcclResult CollCustomMediumAllReduceMeshExecutor::ProxyPerformSend(const OpParam ¶m, ExecMem &execMem)
{
// Retrieve the sub-communication domain information and the master stream
CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
// Dertermine the information for the topology and the data
u32 rankSize = level0CommInfo.localRankSize;
u32 rankId = level0CommInfo.localRank;
u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
u64 chunkSize = execMem.count * unitSize;
// Each proxy rank is responsible for only one unhealthy rank
u32 dstRank = 0;
dstRank = (rankId == PROXY_RANK_X) ? BROKEN_RANK_X : dstRank;
dstRank = (rankId == PROXY_RANK_Y) ? BROKEN_RANK_Y : dstRank;
// Notify remote rank data has been ready
CHK_RET(level0CommInfo.links[dstRank]->RxAck(masterStream));
// Destination address on the remote rank's CCL_Out (remote)
void *dstRemoteMemPtr = nullptr;
CHK_RET(level0CommInfo.links[dstRank]->GetRemoteMem(UserMemType::OUTPUT_MEM, &dstRemoteMemPtr));
DeviceMem dstRemote = DeviceMem::create(static_cast<char *>(dstRemoteMemPtr), chunkSize);
// Source address on the local rank's user output (local)
void *srcLocalMemPtr = static_cast<char *>(execMem.outputPtr);
DeviceMem dstLocal = DeviceMem::create(static_cast<char *>(srcLocalMemPtr), chunkSize);
// Perform HcclD2DMemcpyAsync: Copy data from local user output to remote CCL_Out
HcclD2DMemcpyAsync(dispatcher_, dstRemote, dstLocal, masterStream);
// Communication post-synchronization: Ensure data transfer and operations are completed
CHK_RET(level0CommInfo.links[dstRank]->TxDataSignal(masterStream));
return HCCL_SUCCESS;
}
// Unhealthy ranks receive the last results
HcclResult CollCustomMediumAllReduceMeshExecutor::UnhealthyPerformReceive(const OpParam ¶m, ExecMem &execMem)
{
// Retrieve the sub-communication domain information and the master stream
CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
// Dertermine the information for the topology and the data
u32 rankSize = level0CommInfo.localRankSize;
u32 rankId = level0CommInfo.localRank;
u32 unitSize = SIZE_TABLE[param.DataDes.dataType];
u64 chunkSize = execMem.count * unitSize;
// Each proxy rank is responsible for only one unhealthy rank
u32 srcRank = 0;
srcRank = (rankId == BROKEN_RANK_X) ? PROXY_RANK_X : srcRank;
srcRank = (rankId == BROKEN_RANK_Y) ? PROXY_RANK_Y : srcRank;
// Notify remote rank data has been ready
CHK_RET(level0CommInfo.links[srcRank]->TxAck(masterStream));
// Ensure data transfer and operations are completed
CHK_RET(level0CommInfo.links[srcRank]->RxDataSignal(masterStream));
// Copy data from CCL_Out to user input
DeviceMem dst = DeviceMem::create(execMem.outputPtr, chunkSize);
DeviceMem src = DeviceMem::create(execMem.outputMem.ptr(), chunkSize);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dst, src, masterStream));
return HCCL_SUCCESS;
}
// Determine if a rank is healthy
bool CollCustomMediumAllReduceMeshExecutor::IsHealthyRank(u32 rankId)
{
return rankId != BROKEN_RANK_X && rankId != BROKEN_RANK_Y;
}
// Determine if a rank is proxy
bool CollCustomMediumAllReduceMeshExecutor::IsProxyRank(u32 rankId)
{
return rankId == PROXY_RANK_X || rankId == PROXY_RANK_Y;
}
// Post notify signals from master stream to all slave streams
HcclResult CollCustomMediumAllReduceMeshExecutor::MainPostToSlaves(const OpParam ¶m, ExecMem &execMem)
{
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
for (u32 signalIndex = 0; signalIndex < algResResp_->slaveStreams.size(); signalIndex++)
{
CHK_RET(LocalNotify::Post(masterStream, dispatcher_,
algResResp_->notifiesAux[signalIndex], PROF_STAGE_1));
}
return HCCL_SUCCESS;
}
// Slave streams wait for master's notification
HcclResult CollCustomMediumAllReduceMeshExecutor::SlavesWaitForMain(const OpParam ¶m, ExecMem &execMem)
{
for (u32 streamIndex = 0; streamIndex < algResResp_->slaveStreams.size(); streamIndex++)
{
CHK_RET(LocalNotify::Wait(algResResp_->slaveStreams[streamIndex], dispatcher_,
algResResp_->notifiesAux[streamIndex], PROF_STAGE_1));
}
return HCCL_SUCCESS;
}
// Slave streams notify the master stream that their tasks are completed
HcclResult CollCustomMediumAllReduceMeshExecutor::SlavesPostToMain(const OpParam ¶m, ExecMem &execMem)
{
for (u32 streamIndex = 0; streamIndex < algResResp_->slaveStreams.size(); streamIndex++)
{
CHK_RET(LocalNotify::Post(algResResp_->slaveStreams[streamIndex], dispatcher_,
algResResp_->notifiesMain[streamIndex], PROF_STAGE_1));
}
return HCCL_SUCCESS;
}
// The master stream waits for all slave streams to complete their tasks
HcclResult CollCustomMediumAllReduceMeshExecutor::MainWaitForSlaves(const OpParam ¶m, ExecMem &execMem)
{
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
for (u32 signalIndex = 0; signalIndex < algResResp_->slaveStreams.size(); signalIndex++)
{
CHK_RET(LocalNotify::Wait(masterStream, dispatcher_,
algResResp_->notifiesMain[signalIndex], PROF_STAGE_1));
}
return HCCL_SUCCESS;
}
// The master stream executes an empty task to ensure synchronization
HcclResult CollCustomMediumAllReduceMeshExecutor::MainExecEmptyTask(const OpParam ¶m, ExecMem &execMem)
{
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
DeviceMem srcTmp = DeviceMem::create(execMem.inputPtr, 0);
DeviceMem dstTmp = DeviceMem::create(execMem.outputPtr, 0);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstTmp, srcTmp, masterStream));
return HCCL_SUCCESS;
}
// The slave stream execute an empty task to ensure synchronization
HcclResult CollCustomMediumAllReduceMeshExecutor::SlaveExecEmptyTask(const OpParam ¶m, ExecMem &execMem, u32 streamIndex)
{
hccl::Stream &masterStream = const_cast<hccl::Stream &>(param.stream);
DeviceMem srcTmp = DeviceMem::create(execMem.inputPtr, 0);
DeviceMem dstTmp = DeviceMem::create(execMem.outputPtr, 0);
CHK_RET(HcclD2DMemcpyAsync(dispatcher_, dstTmp, srcTmp, algResResp_->slaveStreams[streamIndex]));
return HCCL_SUCCESS;
}
REGISTER_EXEC("CustomMediumAllReduceMeshExecutor", CustomMediumAllReduceMesh, CollCustomMediumAllReduceMeshExecutor);
} // namespace hccl
|