Archived
libr4d: a three-rank two-shot all-reduce, with shards one word uneven
At three ranks the exact all-reduce was the one-shot at every size: the two-shot required the message to shard evenly, and a 2048-wide bf16 row is 256 16-byte words, a multiple of 3 only one step in three. Every prefill chunk therefore sent twice its message per rank where the two-shot sends 4/3. The two-shot kernel now splits n16 words into shards of n16/ws, the first n16 % ws one word longer, with region B at the longest shard. At 4 and 8 ranks every message the entries accept divides, so nothing there changes. ar_twoshot_3rank_exact takes the band above R4D_AR_WIDE_ONESHOT_MAX_ELEMS the way the 4- and 8-rank rows do. Its fold is the one-shot's -- fp32, ascending rank order -- so the band edge between the two rows changes no bits. r4d_selftest runs both three-rank rows through a per-element oracle (bf16 and fp32, words % 3 of 0, 1 and 2, 16 words to the 20971520-element ceiling, in and out of place, back to back on one instance).
This commit is contained in:
1 parent
a97985e7c2
commit
6b4a67f52e
7 files changed
+282
-41
No files matched your search
@@ -1,13 +1,20 @@
|
||||
// r4d_ar_twoshot_Nrank_exact.hip: r4d_ar_twoshot_4rank_exact and r4d_ar_twoshot_8rank_exact
|
||||
// reduce-scatter + all-gather at 4 or 8 ranks, exact bf16/fp16/fp32 wire. Per-block pipelined
|
||||
// handshake instead of a grid arrival election. Sends 2*(ws-1)/ws*N bytes per rank in two hops,
|
||||
// so it covers the sizes where the one-shot's (ws-1)*N send is too heavy.
|
||||
// r4d_ar_twoshot_Nrank_exact.hip: r4d_ar_twoshot_{3,4,8}rank_exact -- reduce-scatter +
|
||||
// all-gather at 3, 4 or 8 ranks, exact bf16/fp16/fp32 wire. Per-block pipelined handshake
|
||||
// instead of a grid arrival election. Sends 2*(ws-1)/ws*N bytes per rank in two hops, so it
|
||||
// covers the sizes where the one-shot's (ws-1)*N send is too heavy.
|
||||
//
|
||||
// Shard s = contiguous 1/ws of the message, owned by rank s. Phase 1: push my contribution to
|
||||
// each peer-owned shard into the owner's slot[me] region A, flag 2s-1; reduce my shard fp32 in
|
||||
// ascending rank order (own contribution from the input). Phase 2: reduced words go to my out
|
||||
// AND every peer's slot[me] region B, flag 2s; assemble out from region B. Region A at 0, B at
|
||||
// shard16; the slot stride must hold 2*shard16.
|
||||
// Shard s = a contiguous run of the message's 16-byte words, owned by rank s. Phase 1: push my
|
||||
// contribution to each peer-owned shard into the owner's slot[me] region A, flag 2s-1; reduce my
|
||||
// shard fp32 in ascending rank order (own contribution from the input). Phase 2: reduced words go
|
||||
// to my out AND every peer's slot[me] region B, flag 2s; assemble out from region B. Region A at
|
||||
// 0, B at sh_max; the slot stride must hold 2*sh_max.
|
||||
//
|
||||
// UNEVEN SHARDS. A message of n16 words splits into base = n16/ws words a shard, and the first
|
||||
// n16 % ws shards take one more: shard s starts at s*base + min(s, rem). At 4 and 8 ranks every
|
||||
// residual the engine issues divides evenly and this is the plain 1/ws split. At 3 it almost never
|
||||
// does -- a 2048-wide bf16 row is 256 words, and 256 * rows is a multiple of 3 only when rows is --
|
||||
// so the split has to tolerate it rather than refuse it. The sum per word is the same fp32 fold in
|
||||
// ascending rank order whichever rank owns it, so the bits match the one-shot's.
|
||||
//
|
||||
// SHARES the wide family's flag/seq/scratch set: every wide kernel publishes in the 2*seq flag
|
||||
// space (this one adds the 2s-1 half-step) and advances the per-block counter once per AR, so
|
||||
@@ -22,7 +29,7 @@ template <typename T, int LANES, int WS>
|
||||
__global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
||||
const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags,
|
||||
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, int shard16,
|
||||
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, int n16,
|
||||
int slot_stride16, int rank, int drain, int acq, int pub) {
|
||||
constexpr int NPEERS = WS - 1;
|
||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
||||
@@ -31,11 +38,17 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||
const unsigned int s = s_seq;
|
||||
const int parity = (int)s_slot;
|
||||
|
||||
const int w = (shard16 + nb - 1) / nb; // block b owns words [w0,w1) of the SHARD space
|
||||
const int w0 = b * w;
|
||||
int w1 = w0 + w; if (w1 > shard16) w1 = shard16;
|
||||
// shard s = words [s*base + min(s, rem), +base + (s < rem)); sh_max is the longest one
|
||||
const int base = n16 / WS, rem = n16 % WS;
|
||||
const int sh_max = base + (rem ? 1 : 0);
|
||||
#define R4D_TS_OFF(s) ((s) * base + ((s) < rem ? (s) : rem))
|
||||
#define R4D_TS_LEN(s) (base + ((s) < rem ? 1 : 0))
|
||||
|
||||
// region A of my slot in every peer's scratch (B sits at +shard16)
|
||||
const int w = (sh_max + nb - 1) / nb; // block b owns words [w0,w1) of the SHARD space
|
||||
const int w0 = b * w;
|
||||
int w1 = w0 + w; if (w1 > sh_max) w1 = sh_max;
|
||||
|
||||
// region A of my slot in every peer's scratch (B sits at +sh_max)
|
||||
int4* pa[NPEERS];
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p)
|
||||
@@ -43,9 +56,11 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||
|
||||
// ---- phase 1 push: my contribution to each peer-owned shard -> owner's region A
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int4* src = in4 + (size_t)peers.rank[p] * shard16;
|
||||
const int owner = peers.rank[p];
|
||||
const int4* src = in4 + (size_t)R4D_TS_OFF(owner);
|
||||
int4* dst = pa[p];
|
||||
for (int k = w0 + tid; k < w1; k += nt) dst[k] = src[k];
|
||||
const int lim = w1 < R4D_TS_LEN(owner) ? w1 : R4D_TS_LEN(owner);
|
||||
for (int k = w0 + tid; k < lim; k += nt) dst[k] = src[k];
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s - 1u, drain, acq, pub);
|
||||
|
||||
@@ -54,10 +69,11 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||
const int4* src[WS];
|
||||
#pragma unroll
|
||||
for (int r = 0; r < WS; ++r)
|
||||
src[r] = (r == rank) ? (in4 + (size_t)rank * shard16)
|
||||
src[r] = (r == rank) ? (in4 + (size_t)R4D_TS_OFF(rank))
|
||||
: (my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16);
|
||||
int4* my_out = out4 + (size_t)rank * shard16;
|
||||
for (int k = w0 + tid; k < w1; k += nt) {
|
||||
int4* my_out = out4 + (size_t)R4D_TS_OFF(rank);
|
||||
const int my_lim = w1 < R4D_TS_LEN(rank) ? w1 : R4D_TS_LEN(rank);
|
||||
for (int k = w0 + tid; k < my_lim; k += nt) {
|
||||
float acc[LANES];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < LANES; ++j) acc[j] = 0.0f;
|
||||
@@ -74,17 +90,20 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||
for (int j = 0; j < LANES; ++j) r4d_ar_from_f(acc[j], po[j]);
|
||||
my_out[k] = o;
|
||||
#pragma unroll
|
||||
for (int p = 0; p < NPEERS; ++p) pa[p][shard16 + k] = o; // region B
|
||||
for (int p = 0; p < NPEERS; ++p) pa[p][sh_max + k] = o; // region B
|
||||
}
|
||||
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
|
||||
|
||||
// ---- assemble: every peer-owned shard from my region B copies
|
||||
for (int p = 0; p < NPEERS; ++p) {
|
||||
const int owner = peers.rank[p];
|
||||
const int4* srcB = my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride16 + shard16;
|
||||
int4* dst = out4 + (size_t)owner * shard16;
|
||||
for (int k = w0 + tid; k < w1; k += nt) dst[k] = srcB[k];
|
||||
const int4* srcB = my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride16 + sh_max;
|
||||
int4* dst = out4 + (size_t)R4D_TS_OFF(owner);
|
||||
const int lim = w1 < R4D_TS_LEN(owner) ? w1 : R4D_TS_LEN(owner);
|
||||
for (int k = w0 + tid; k < lim; k += nt) dst[k] = srcB[k];
|
||||
}
|
||||
#undef R4D_TS_OFF
|
||||
#undef R4D_TS_LEN
|
||||
}
|
||||
|
||||
// --- host side ---
|
||||
@@ -107,19 +126,19 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
|
||||
std::to_string(R4D_AR_TWOSHOT_WIDE_MAX_ELEMS) +
|
||||
" elems); use the fallback path");
|
||||
const long n16 = nbytes / 16;
|
||||
if (n16 % WS != 0)
|
||||
if (n16 < WS)
|
||||
throw std::runtime_error("all_reduce: message (" + std::to_string(n16) +
|
||||
" 16B words) does not shard evenly over " + std::to_string(WS) +
|
||||
" ranks");
|
||||
const long shard16 = n16 / WS;
|
||||
if (2 * shard16 > (long)slot_stride16)
|
||||
throw std::runtime_error("all_reduce: shard (" + std::to_string(shard16) +
|
||||
" 16B words) is shorter than one word a rank");
|
||||
// the longest shard (the kernel's uneven split); regions A and B each hold one
|
||||
const long sh_max = (n16 + WS - 1) / WS;
|
||||
if (2 * sh_max > (long)slot_stride16)
|
||||
throw std::runtime_error("all_reduce: shard (" + std::to_string(sh_max) +
|
||||
" 16B words) needs 2x inside the per-source slot stride (" +
|
||||
std::to_string(slot_stride16) + ")");
|
||||
|
||||
int nb = (int)nblocks;
|
||||
if (nb < 1) nb = R4D_AR_NB_DESIGN;
|
||||
if (nb > shard16) nb = (int)shard16;
|
||||
if (nb > sh_max) nb = (int)sh_max;
|
||||
if (nb > R4D_AR_WIDE_MAX_BLOCKS) nb = R4D_AR_WIDE_MAX_BLOCKS;
|
||||
int nt = (int)nthreads;
|
||||
if (nt < 64) nt = 64;
|
||||
@@ -153,7 +172,7 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
|
||||
#define R4D_AR_TS_LAUNCH(T, LN) \
|
||||
r4d_ar_twoshot_wide_exact_kernel<T, LN, WS><<<nb, nt, 0, st>>>( \
|
||||
(const int4*)inp, peers, (const int4*)my_scratch, (unsigned int*)my_flags, \
|
||||
(int4*)out, (unsigned int*)seq_ctrs, (int)shard16, (int)slot_stride16, \
|
||||
(int4*)out, (unsigned int*)seq_ctrs, (int)n16, (int)slot_stride16, \
|
||||
(int)rank, (int)drain, (int)acq, (int)pub)
|
||||
if (dtype == 0) R4D_AR_TS_LAUNCH(__hip_bfloat16, 8);
|
||||
else if (dtype == 1) R4D_AR_TS_LAUNCH(__half, 8);
|
||||
@@ -162,6 +181,17 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
|
||||
#undef R4D_AR_TS_LAUNCH
|
||||
}
|
||||
|
||||
void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long peer_flags0,
|
||||
long peer_flags1, long my_scratch, long my_flags, long seq_ctrs,
|
||||
long slot_stride16, long inp, long out, long n_elem, long dtype,
|
||||
long rank, long stream, long nblocks, long nthreads, long drain,
|
||||
long acq, long pub) {
|
||||
const long ps[2] = {peer_scratch0, peer_scratch1};
|
||||
const long pf[2] = {peer_flags0, peer_flags1};
|
||||
ar_twoshot_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
|
||||
dtype, rank, stream, nblocks, nthreads, drain, acq, pub);
|
||||
}
|
||||
|
||||
void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
||||
long peer_flags0, long peer_flags1, long peer_flags2,
|
||||
long my_scratch, long my_flags, long seq_ctrs,
|
||||
|
||||
Reference in new issue
Block a user