-
Notifications
You must be signed in to change notification settings - Fork 0
/
allreduce.hpp
515 lines (452 loc) · 18 KB
/
allreduce.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#pragma once
#include <sycl/sycl.hpp>
#include <gen_visa_templates.hpp>
#include "transmit.hpp"
template <typename T,
int NRanks,
template <typename, int> class Proto,
template <typename, int, template <typename, int> class, int> class Transmit,
int SubGroupSize = 16>
struct AllReduce : public Transmit<T, NRanks, Proto, SubGroupSize> {
using Super = Transmit<T, NRanks, Proto, SubGroupSize>;
using message_t = typename Super::message_t;
constexpr static int unroll = 1 /*NPeers < 4 ? 4 : 2*/;
constexpr static int wireCapacity = Super::wireCapacity;
constexpr static int wireTransSize = Super::wireTransSize;
AllReduce(
T* input, size_t nelems, int rank, uint32_t seqNo,
T* scatterBuf, T* gatherBuf,
T* const peerBuf0[], T* const peerBuf1[]
#if defined(__enable_sycl_stream__)
, sycl::stream cout
#endif
)
: Transmit<T, NRanks, Proto, SubGroupSize>(
input, scatterBuf, gatherBuf, peerBuf0, peerBuf1,
calcWorkSize(input, nelems * sizeof(T)),
rank, seqNo
#if defined(__enable_sycl_stream__)
, cout
#endif
), workSize(calcWorkSize(input, nelems * sizeof(T)))
#if defined(__enable_sycl_stream__)
, cout(cout)
#endif
{}
static int scatterVerify(
uint32_t* host, int rank, uint32_t flag, size_t nWorkElemsInInt
);
static int stage2Verify(
T* host, int rank, uint32_t flag, size_t nWorkElemsInInt
);
sycl::nd_range<1> getLaunchParam(uint32_t& updateSeqNo) const {
constexpr uint32_t nThreads = 64; /* TODO: get EU/thread config */
constexpr size_t maxSS = 64;
int w = NRanks;
size_t wirePerSS = nThreads / w;
size_t nWire = divUp(workSize, wireCapacity);
size_t nSS = divUp(nWire, wirePerSS);
auto actualSS = std::min(nSS, maxSS);
auto nSteps = divUp(nWire, actualSS * wirePerSS);
updateSeqNo += nSteps;
//
// XXX: we over updated sequence number. Should be nSteps / nSlot
// No harm, but not nice.
//
return sycl::nd_range<1>(
actualSS * wirePerSS * w * SubGroupSize,
wirePerSS * w * SubGroupSize
);
}
static void launch(
T* input, T* ipcbuf0, T* ipcbuf1, T* const peerbuf0[], T* const peerbuf1[],
size_t nelems, int rank, uint32_t& step, sycl::queue queue) {
AllReduce offload(input, nelems, rank, step, ipcbuf0, ipcbuf1, peerbuf0, peerbuf1);
queue.submit([&](sycl::handler &cgh) {
cgh.parallel_for(
offload.getLaunchParam(step), offload
);
});
}
//
// Found this analogy fascinating:
//
// Let correspond sub-group to wire, sequential guaranteed.
// Bundle sub-groups(wires) into group(cable).
//
// Total cables will deliver the full capacity of single loop.
//
void operator() [[sycl::reqd_sub_group_size(SubGroupSize)]] (
sycl::nd_item<1> pos
) const {
auto nWires = pos.get_global_range(0) / SubGroupSize;
auto wireId_x = pos.get_global_id(0) / SubGroupSize / NRanks;
auto loopSize = nWires / NRanks * wireCapacity;
for (size_t gOff = 0, tOff = 0;
gOff < workSize; gOff += loopSize, ++ tOff) {
auto wireOff = wireId_x * wireCapacity + gOff;
#if defined(__enable_sycl_stream__)
auto local_id = pos.get_sub_group().get_local_id()[0];
if (local_id == 0 && groupId == 0)
cout<<"["<<groupId<<", "<<subGroupId
<<"] loopSize:"<<loopSize
<<", wireOff:"<<wireOff<<"; "
<<", transOff:"<<transOff<<"; "<<sycl::endl;
#endif
ssize_t workLeft = workSize - wireOff;
if (workLeft > 0)
const_cast<AllReduce *>(this)-> template run<unroll>(wireOff, tOff, workLeft);
}
}
private:
// TODO: buffer plan and start point calc
static size_t calcWorkSize(T* input, size_t size) {
// Input must be message size align
if ((uintptr_t)input % sizeof(message_t) != 0)
throw std::logic_error("We only support aligned pointer for now");
auto nChunks = NRanks;
auto octSize = divUp(size, sizeof(message_t));
auto chunkSize = divUp(octSize, nChunks);
if (octSize * sizeof(message_t) != size || chunkSize * sizeof(message_t) * nChunks > size)
throw std::logic_error("We don't support non-even divide yet");
// TODO: Production logic needs every rank chunk
return chunkSize * sizeof(message_t);
}
ssize_t workSize;
#if defined(__enable_sycl_stream__)
sycl::stream cout;
#endif
};
template <typename T,
int NRanks,
template <typename, int, int> class Transmit,
int SubGroupSize = 16>
struct bisectAllReduce : public Transmit<T, NRanks, SubGroupSize> {
using Super = Transmit<T, NRanks, SubGroupSize>;
using message_t = typename Super::message_t;
constexpr static int unroll = 1 /*NPeers < 4 ? 4 : 2*/;
constexpr static int wireCapacity = Super::wireCapacity;
constexpr static int wireTransSize = Super::wireTransSize;
bisectAllReduce(
T* input, size_t nelems, int rank, uint32_t seqNo,
T* scatterBuf, T* gatherBuf, T* const peerBuf0[], T* const peerBuf1[]
#if defined(__enable_sycl_stream__)
, sycl::stream cout
#endif
) :
Super(input, scatterBuf, gatherBuf, peerBuf0, peerBuf1,
calcWorkSize(input, nelems * sizeof(T)),
divUp(calcWorkSize(input, nelems * sizeof(T)), wireCapacity)
* wireTransSize, rank, seqNo
#if defined(__enable_sycl_stream__)
, cout
#endif
), workSize(calcWorkSize(input, nelems * sizeof(T)))
#if defined(__enable_sycl_stream__)
, cout(cout)
#endif
{}
static int stage1Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage2Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage3Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage4Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage5Verify(T* host, int rank, uint32_t flag, size_t nelems);
void operator() [[sycl::reqd_sub_group_size(SubGroupSize)]] (
sycl::nd_item<1> pos
) const {
auto groupRange = pos.get_group_range()[0];
int subGroupRange = pos.get_sub_group().get_group_range()[0];
auto cableCapacity = wireCapacity * subGroupRange;
auto cableTSize = wireTransSize * subGroupRange;
auto groupId = pos.get_group().get_group_id()[0];
auto subGroupId = pos.get_sub_group().get_group_id()[0];
auto loopSize = groupRange * cableCapacity;
auto loopTSize = groupRange * cableTSize;
for (size_t gOff = 0, tOff = 0;
gOff < workSize; gOff += loopSize, tOff += loopTSize) {
auto wireOff = groupId * cableCapacity + subGroupId * wireCapacity + gOff;
auto transOff = groupId * cableTSize + subGroupId * wireTransSize + tOff;
ssize_t workLeft = workSize - wireOff;
#if defined(__enable_sycl_stream__)
auto local_id = pos.get_sub_group().get_local_id()[0];
if (local_id == 0 && groupId == 0)
cout<<"["<<groupId<<", "<<subGroupId
<<"] loopSize:"<<loopSize
<<", wireOff:"<<wireOff
<<", transOff:"<<transOff
<<", workLeft:"<<workLeft<<sycl::endl;
#endif
if (workLeft > 0) {
const_cast<bisectAllReduce *>(this)->
template scatterFar<unroll>(wireOff, transOff, workLeft);
const_cast<bisectAllReduce *>(this)->
template closePollReduceScatter<unroll>(wireOff, transOff, workLeft);
const_cast<bisectAllReduce *>(this)->
template closePollRecvReduceBcast<unroll>(wireOff, transOff, workLeft);
const_cast<bisectAllReduce *>(this)->
template pollGatherOutputs<unroll>(wireOff, transOff, workLeft);
const_cast<bisectAllReduce *>(this)->
template pollFarGatherOutput<unroll>(wireOff, transOff, workLeft);
}
}
}
private:
// TODO: buffer plan and start point calc
static size_t calcWorkSize(T* input, size_t size) {
// Input must be message size align
if ((uintptr_t)input % sizeof(message_t) != 0)
throw std::logic_error("We only support aligned pointer for now");
auto nChunks = NRanks;
auto octSize = divUp(size, sizeof(message_t));
auto chunkSize = divUp(octSize, nChunks);
if (octSize * sizeof(message_t) != size || chunkSize * sizeof(message_t) * nChunks > size)
throw std::logic_error("We don't support non-even divide yet");
// TODO: Production logic needs every rank chunk
return chunkSize * sizeof(message_t);
}
ssize_t workSize;
#if defined(__enable_sycl_stream__)
sycl::stream cout;
#endif
};
// Best candidate for throughput
template <typename T,
int NRanks,
template <typename, int, int> class Transmit = BisectPTransmit,
int SubGroupSize = 16>
struct bisectPAllReduce : public Transmit<T, NRanks, SubGroupSize> {
using Super = Transmit<T, NRanks, SubGroupSize>;
using message_t = typename Super::message_t;
constexpr static int unroll = 1 /*NPeers < 4 ? 4 : 2*/;
constexpr static int wireCapacity = Super::wireCapacity;
constexpr static int wireTransSize = Super::wireTransSize;
constexpr static int BiNRanks = NRanks/2;
bisectPAllReduce(
T* input, size_t nelems, int rank, uint32_t seqNo,
T* scatterBuf, T* gatherBuf, T* const peerBuf0[], T* const peerBuf1[]
#if defined(__enable_sycl_stream__)
, sycl::stream cout
#endif
) :
Super(input, scatterBuf, gatherBuf, peerBuf0, peerBuf1,
calcWorkSize(input, nelems * sizeof(T)), rank, seqNo
#if defined(__enable_sycl_stream__)
, cout
#endif
), workSize(calcWorkSize(input, nelems * sizeof(T)))
#if defined(__enable_sycl_stream__)
, cout(cout)
#endif
{}
static int stage1Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage2Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage3Verify(T* host, int rank, uint32_t flag, size_t nelems);
static int stage4Verify(T* host, int rank, uint32_t flag, size_t nelems);
sycl::nd_range<1> getLaunchParam(uint32_t& updateSeqNo) const {
constexpr uint32_t nThreads = 64; /* TODO: get EU/thread config */
constexpr size_t maxSS = 64;
int w = NRanks;
size_t wirePerSS = nThreads / w;
size_t nWire = divUp(workSize, wireCapacity);
size_t nSS = divUp(nWire, wirePerSS);
auto actualSS = std::min(nSS, maxSS);
auto nSteps = divUp(nWire, actualSS * wirePerSS);
updateSeqNo += nSteps;
//
// XXX: we over updated sequence number. Should be nSteps / nSlot
// No harm, but not nice.
//
return sycl::nd_range<1>(
actualSS * wirePerSS * w * SubGroupSize,
wirePerSS * w * SubGroupSize
);
}
static void launch(
T* input, T* ipcbuf0, T* ipcbuf1, T* const peerbuf0[], T* const peerbuf1[],
size_t nelems, int rank, uint32_t& step, sycl::queue queue) {
bisectPAllReduce offload(input, nelems, rank, step, ipcbuf0, ipcbuf1, peerbuf0, peerbuf1);
queue.submit([&](sycl::handler &cgh) {
cgh.parallel_for(
offload.getLaunchParam(step), offload
);
});
}
//
// We use linear group configuration because subgroup is linear only
// And we want to control coalescing as much as possible
//
void operator() [[sycl::reqd_sub_group_size(SubGroupSize)]] (
sycl::nd_item<1> pos
) const {
auto nWires = pos.get_global_range(0)/SubGroupSize;
auto loopSize = nWires / BiNRanks * wireCapacity;
auto wireId_x = pos.get_global_id(0)/SubGroupSize/BiNRanks;
for (size_t gOff = 0, tOff = 0;
gOff < workSize; gOff += loopSize, ++ tOff) {
auto wireOff = wireId_x * wireCapacity + gOff;
ssize_t workLeft = workSize - wireOff;
#if defined(__enable_prefetch__)
auto nextOff = wireOff + gOff;
auto nextLeft = workSize - nextOff;
#endif
#if defined(__enable_sycl_stream__)
auto localId = pos.get_sub_group().get_local_id()[0];
if (localId == 0 && groupId == 0)
cout<<"["<<groupId<<", "<<subGroupXId
<<"] loopSize:"<<loopSize
<<", wireOff:"<<wireOff
<<", workLeft:"<<workLeft<<sycl::endl;
#endif
if (workLeft > 0) { // Y parallel equals bisect Ranks
#if defined(__enable_prefetch__)
if (nextLeft > cableTSize)
const_cast<bisectPAllReduce *>(this)-> template PreloadNext<unroll>(nextOff);
#endif
const_cast<bisectPAllReduce *>(this)->
template scatterFar<unroll>(wireOff, tOff, workLeft);
const_cast<bisectPAllReduce *>(this)->
template closeUnifiedPollReduceScatterGather<unroll>(wireOff, tOff, workLeft);
const_cast<bisectPAllReduce *>(this)->
template pollFarGatherOutput<unroll>(wireOff, tOff, workLeft);
}
}
}
private:
// TODO: buffer plan and start point calc
static size_t calcWorkSize(T* input, size_t size) {
// Input must be message size align
if ((uintptr_t)input % sizeof(message_t) != 0)
throw std::logic_error("We only support aligned pointer for now");
auto nChunks = NRanks;
auto octSize = divUp(size, sizeof(message_t));
auto chunkSize = divUp(octSize, nChunks);
if (octSize * sizeof(message_t) != size || chunkSize * sizeof(message_t) * nChunks > size)
throw std::logic_error("We don't support non-even divide yet");
// TODO: Production logic needs every rank chunk
return chunkSize * sizeof(message_t);
}
ssize_t workSize;
#if defined(__enable_sycl_stream__)
sycl::stream cout;
#endif
};
template <typename T,
int NRanks,
template <typename, int, int> class Transmit = BisectPPTransmit,
int SubGroupSize = 16>
struct bisectPPAllReduce : public Transmit<T, NRanks, SubGroupSize> {
using Super = Transmit<T, NRanks, SubGroupSize>;
using message_t = typename Super::message_t;
constexpr static int unroll = 1 /*NPeers < 4 ? 4 : 2*/;
constexpr static int wireCapacity = Super::wireCapacity;
constexpr static int wireTransSize = Super::wireTransSize;
constexpr static int BiNRanks = NRanks/2;
bisectPPAllReduce(
T* input, size_t nelems, int rank, uint32_t seqNo,
T* scatterBuf, T* gatherBuf, T* const peerBuf0[], T* const peerBuf1[]
#if defined(__enable_sycl_stream__)
, sycl::stream cout
#endif
) :
Super(input, scatterBuf, gatherBuf, peerBuf0, peerBuf1,
calcWorkSize(input, nelems * sizeof(T)), rank, seqNo
#if defined(__enable_sycl_stream__)
, cout
#endif
), workSize(calcWorkSize(input, nelems * sizeof(T)))
#if defined(__enable_sycl_stream__)
, cout(cout)
#endif
{}
static int stage4Verify(T* host, int rank, uint32_t flag, size_t nelems);
//
// We use linear group configuration because subgroup is linear only
// And we want to control coalescing as much as possible
//
void operator() [[sycl::reqd_sub_group_size(SubGroupSize)]] (
sycl::nd_item<1> pos
) const {
auto groupRange = pos.get_group_range()[0];
int subGroupXRange = pos.get_sub_group().get_group_range()[0]/BiNRanks/2;
auto cableCapacity = wireCapacity * subGroupXRange;
auto cableTSize = wireTransSize * subGroupXRange;
// 0, 1, 2, 3, 4, 5, 6, 7 work on same offset
auto subGroupXId = pos.get_sub_group().get_group_id()[0] /(BiNRanks *2);
auto subGroupYId = pos.get_sub_group().get_group_id()[0] %(BiNRanks *2);
auto groupId = pos.get_group().get_group_id()[0];
auto loopSize = groupRange * cableCapacity;
auto loopTSize = groupRange * cableTSize;
for (size_t gOff = 0, tOff = 0;
gOff < workSize; gOff += loopSize, tOff += loopTSize) {
auto wireOff = groupId * cableCapacity + subGroupXId * wireCapacity + gOff;
auto transOff = groupId * cableTSize + subGroupXId * wireTransSize + tOff;
ssize_t workLeft = workSize - wireOff;
#if defined(__enable_sycl_stream__)
auto localId = pos.get_sub_group().get_local_id()[0];
if (localId == 0 && groupId == 0)
cout<<"["<<groupId<<", "<<subGroupXId
<<"] loopSize:"<<loopSize
<<", wireOff:"<<wireOff
<<", transOff:"<<transOff
<<", workLeft:"<<workLeft<<sycl::endl;
#endif
if (workLeft > 0) { // Y parallel equals bisect Ranks
if (subGroupYId < 2) {
const_cast<bisectPPAllReduce *>(this)->
template scatterFar<unroll>(wireOff, transOff, workLeft);
} else if (subGroupYId < 4) {
const_cast<bisectPPAllReduce *>(this)->
template pollFarGatherOutput<unroll>(wireOff, transOff, workLeft);
} else if (subGroupYId < 8) {
const_cast<bisectPPAllReduce *>(this)->
template closeUnifiedPollReduceScatterGather<unroll>(wireOff, transOff, workLeft);
}
}
}
}
private:
// TODO: buffer plan and start point calc
static size_t calcWorkSize(T* input, size_t size) {
// Input must be message size align
if ((uintptr_t)input % sizeof(message_t) != 0)
throw std::logic_error("We only support aligned pointer for now");
auto nChunks = NRanks;
auto octSize = divUp(size, sizeof(message_t));
auto chunkSize = divUp(octSize, nChunks);
if (octSize * sizeof(message_t) != size || chunkSize * sizeof(message_t) * nChunks > size)
throw std::logic_error("We don't support non-even divide yet");
// TODO: Production logic needs every rank chunk
return chunkSize * sizeof(message_t);
}
ssize_t workSize;
#if defined(__enable_sycl_stream__)
sycl::stream cout;
#endif
};
template <typename T,
template <typename, int> class Proto,
template <typename, int, template<typename, int> class, int> class Transmit>
sycl::event testTransmit(
sycl::nd_range<1> launchParam,
T* input, T* ipcbuf0, T* ipcbuf1,
T* const peerbuf0[], T* const peerbuf1[], size_t nelems,
int rank, int world, uint32_t step, uint32_t simd, sycl::queue queue
);
template <typename T, template <typename, int, int> class Transmit>
sycl::event testBisectTransmit(
sycl::nd_range<1> launchParam,
T* input, T* ipcbuf0, T* ipcbuf1,
T* const peerbuf0[], T* const peerbuf1[], size_t nelems,
int rank, int world, uint32_t step, uint32_t simd, sycl::queue queue
);
template <typename T>
sycl::event testTransmit(
std::string transmitType,
sycl::nd_range<1> launchParam,
T* input, T* ipcbuf0, T* ipcbuf1,
T* const peerbuf0[], T* const peerbuf1[], size_t nelems,
int rank, int world, uint32_t step, uint32_t subgroup, sycl::queue queue);
template<typename T> int verifyTransmit(
T* host, T* host2,
uint32_t step, int rank, int world, uint32_t simd, size_t nelems
);