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
@@ -127,6 +127,12 @@ radiance --model qwen3.8-next-flash-fp8-iq4r-moe.rad --tp 3 --tp-wire exact --ma
|
|||||||
--num-speculative-tokens 3 --max-num-batched-tokens 2048
|
--num-speculative-tokens 3 --max-num-batched-tokens 2048
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The exact all-reduce at three ranks is the wide family's one-shot up to 1 MiB a message (every
|
||||||
|
decode step) and its two-shot above it (every prefill chunk): reduce-scatter + all-gather, which
|
||||||
|
sends 4/3 of the message per rank instead of twice it. A message rarely divides into thirds -- a
|
||||||
|
2048-wide bf16 row is 256 16-byte words -- so the first `words % 3` shards take one word more.
|
||||||
|
Both rows fold in fp32 in ascending rank order, so the band edge between them changes no bits.
|
||||||
|
|
||||||
The log says what the geometry did:
|
The log says what the geometry did:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
+15
-7
@@ -332,7 +332,8 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo
|
|||||||
// [block][source rank]; seq_ctrs is 2 * max_blocks words, a handshake and a slot-parity counter
|
// [block][source rank]; seq_ctrs is 2 * max_blocks words, a handshake and a slot-parity counter
|
||||||
// per block of capacity (r4d_ar_seq.h).
|
// per block of capacity (r4d_ar_seq.h).
|
||||||
// Three ranks: the same kernel instantiated at WS=3 (two peers). Nothing in the wide family is
|
// Three ranks: the same kernel instantiated at WS=3 (two peers). Nothing in the wide family is
|
||||||
// power-of-two; the two-shot cannot serve 3 because it shards the message `world` ways.
|
// power-of-two; the two-shot serves 3 too, with shards one word uneven where the message does not
|
||||||
|
// divide (r4d_ar_twoshot_3rank_exact, below).
|
||||||
void r4d_ar_oneshot_3rank_exact(long peer_scratch0, long peer_scratch1,
|
void r4d_ar_oneshot_3rank_exact(long peer_scratch0, long peer_scratch1,
|
||||||
long peer_flags0, long peer_flags1,
|
long peer_flags0, long peer_flags1,
|
||||||
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
|
long my_scratch, long my_flags, long seq_ctrs, long slot_stride16,
|
||||||
@@ -391,14 +392,21 @@ void r4d_ar_gather_hc_write_4rank(long peer_scratch0, long peer_scratch1, long p
|
|||||||
int r4d_ar_gather_inv(long inv, long sorted, long T, long stream);
|
int r4d_ar_gather_inv(long inv, long sorted, long T, long stream);
|
||||||
void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers);
|
void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers);
|
||||||
|
|
||||||
// ---- all-reduce: two-shot, 4 or 8 ranks over P2P -------------------------------------------
|
// ---- all-reduce: two-shot, 3, 4 or 8 ranks over P2P ----------------------------------------
|
||||||
// Reduce-scatter + all-gather on the same buffer trio and peer-argument order as the wide
|
// Reduce-scatter + all-gather on the same buffer trio and peer-argument order as the wide
|
||||||
// one-shot (r4d_ar_twoshot_Nrank_exact.hip): shard s, a contiguous 1/ws of the message, is
|
// one-shot (r4d_ar_twoshot_Nrank_exact.hip): shard s, a contiguous run of the message's 16-byte
|
||||||
// reduced by rank s (fp32, ascending rank order), then gathered. Sends 2*(ws-1)/ws*N bytes
|
// words, is reduced by rank s (fp32, ascending rank order), then gathered. Sends 2*(ws-1)/ws*N
|
||||||
// per rank in two hops. Covers the sizes where the one-shot's (ws-1)*N send is too heavy.
|
// bytes per rank in two hops. Covers the sizes where the one-shot's (ws-1)*N send is too heavy.
|
||||||
// Regions A and B sit at 0 and shard16, so the slot stride must hold 2*shard16. Above this bound
|
// Shards are as even as the word count allows (the first n16 % ws take one more word), so a
|
||||||
// the caller falls back:
|
// message need not divide by the rank count -- at three it almost never does. Regions A and B sit
|
||||||
|
// at 0 and the longest shard, so the slot stride must hold twice that. Above this bound the caller
|
||||||
|
// falls back:
|
||||||
enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 20971520 }; // a 4096-token hidden-5120 prefill chunk
|
enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 20971520 }; // a 4096-token hidden-5120 prefill chunk
|
||||||
|
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);
|
||||||
void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
|
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 peer_flags0, long peer_flags1, long peer_flags2,
|
||||||
long my_scratch, long my_flags, long seq_ctrs,
|
long my_scratch, long my_flags, long seq_ctrs,
|
||||||
|
|||||||
@@ -870,6 +870,33 @@ extern "C" int r4d_rad_ar_oneshot_8rank_exact(const RadArgs* a, RadStream s) {
|
|||||||
* the thread that already read it, later in its own program order, and no peer reads this rank's
|
* the thread that already read it, later in its own program order, and no peer reads this rank's
|
||||||
* input -- peers read their own scratch. The selector hands the two-shot the messages above the
|
* input -- peers read their own scratch. The selector hands the two-shot the messages above the
|
||||||
* one-shot's size bound (r4d_rows.cpp, cArTsExact4). */
|
* one-shot's size bound (r4d_rows.cpp, cArTsExact4). */
|
||||||
|
/* AT THREE RANKS THE SHARDS ARE UNEVEN, so there is no divisibility to check: a 2048-wide bf16
|
||||||
|
* row is 256 words and a step's message is 256 * rows of them, a multiple of 3 only one step in
|
||||||
|
* three. The kernel gives the first (words % 3) shards one word more, the slot holds two of the
|
||||||
|
* longest, and the fold is the one-shot's -- fp32, ascending rank order -- so the two rows that
|
||||||
|
* share this op produce the same bits and the band edge between them moves nothing a request
|
||||||
|
* could see. */
|
||||||
|
extern "C" int r4d_rad_ar_twoshot_3rank_exact(const RadArgs* a, RadStream s) {
|
||||||
|
ArCall c;
|
||||||
|
int rc = ar_setup(a, &c, 1);
|
||||||
|
if (rc != RAD_OK) return rc;
|
||||||
|
if (c.in->world != 3) return RAD_E_SHAPE;
|
||||||
|
if (c.numel > R4D_AR_TWOSHOT_WIDE_MAX_ELEMS) return RAD_E_SHAPE;
|
||||||
|
const long words16 = c.numel * c.in->esize / 16;
|
||||||
|
if (words16 < c.in->world) return RAD_E_SHAPE;
|
||||||
|
const long sh_max = (words16 + c.in->world - 1) / c.in->world;
|
||||||
|
if (2 * sh_max * 16 > c.in->slot_bytes) return RAD_E_SCRATCH;
|
||||||
|
const ArInst* in = c.in;
|
||||||
|
R4D_TRY("ar_twoshot_3rank_exact",
|
||||||
|
r4d_ar_twoshot_3rank_exact(in->peer_scratch[0], in->peer_scratch[1],
|
||||||
|
in->peer_flags[0], in->peer_flags[1],
|
||||||
|
in->my_scratch, in->my_flags, in->my_seq, in->slot16,
|
||||||
|
c.inp, c.out, c.numel, in->dtype, in->rank,
|
||||||
|
(long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq,
|
||||||
|
c.pub));
|
||||||
|
return RAD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" int r4d_rad_ar_twoshot_4rank_exact(const RadArgs* a, RadStream s) {
|
extern "C" int r4d_rad_ar_twoshot_4rank_exact(const RadArgs* a, RadStream s) {
|
||||||
ArCall c;
|
ArCall c;
|
||||||
int rc = ar_setup(a, &c, 1);
|
int rc = ar_setup(a, &c, 1);
|
||||||
|
|||||||
@@ -1,13 +1,20 @@
|
|||||||
// r4d_ar_twoshot_Nrank_exact.hip: r4d_ar_twoshot_4rank_exact and r4d_ar_twoshot_8rank_exact
|
// r4d_ar_twoshot_Nrank_exact.hip: r4d_ar_twoshot_{3,4,8}rank_exact -- reduce-scatter +
|
||||||
// reduce-scatter + all-gather at 4 or 8 ranks, exact bf16/fp16/fp32 wire. Per-block pipelined
|
// all-gather at 3, 4 or 8 ranks, exact bf16/fp16/fp32 wire. Per-block pipelined handshake
|
||||||
// handshake instead of a grid arrival election. Sends 2*(ws-1)/ws*N bytes per rank in two hops,
|
// instead of a grid arrival election. Sends 2*(ws-1)/ws*N bytes per rank in two hops, so it
|
||||||
// so it covers the sizes where the one-shot's (ws-1)*N send is too heavy.
|
// 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
|
// Shard s = a contiguous run of the message's 16-byte words, owned by rank s. Phase 1: push my
|
||||||
// each peer-owned shard into the owner's slot[me] region A, flag 2s-1; reduce my shard fp32 in
|
// contribution to each peer-owned shard into the owner's slot[me] region A, flag 2s-1; reduce my
|
||||||
// ascending rank order (own contribution from the input). Phase 2: reduced words go to my out
|
// shard fp32 in ascending rank order (own contribution from the input). Phase 2: reduced words go
|
||||||
// AND every peer's slot[me] region B, flag 2s; assemble out from region B. Region A at 0, B at
|
// to my out AND every peer's slot[me] region B, flag 2s; assemble out from region B. Region A at
|
||||||
// shard16; the slot stride must hold 2*shard16.
|
// 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
|
// 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
|
// 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(
|
__global__ void r4d_ar_twoshot_wide_exact_kernel(
|
||||||
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
const int4* __restrict__ in4, R4DArWidePeers<int4, WS - 1> peers,
|
||||||
const int4* __restrict__ my_base, unsigned int* __restrict__ my_flags,
|
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) {
|
int slot_stride16, int rank, int drain, int acq, int pub) {
|
||||||
constexpr int NPEERS = WS - 1;
|
constexpr int NPEERS = WS - 1;
|
||||||
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
|
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 unsigned int s = s_seq;
|
||||||
const int parity = (int)s_slot;
|
const int parity = (int)s_slot;
|
||||||
|
|
||||||
const int w = (shard16 + nb - 1) / nb; // block b owns words [w0,w1) of the SHARD space
|
// shard s = words [s*base + min(s, rem), +base + (s < rem)); sh_max is the longest one
|
||||||
const int w0 = b * w;
|
const int base = n16 / WS, rem = n16 % WS;
|
||||||
int w1 = w0 + w; if (w1 > shard16) w1 = shard16;
|
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];
|
int4* pa[NPEERS];
|
||||||
#pragma unroll
|
#pragma unroll
|
||||||
for (int p = 0; p < NPEERS; ++p)
|
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
|
// ---- phase 1 push: my contribution to each peer-owned shard -> owner's region A
|
||||||
for (int p = 0; p < NPEERS; ++p) {
|
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];
|
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);
|
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];
|
const int4* src[WS];
|
||||||
#pragma unroll
|
#pragma unroll
|
||||||
for (int r = 0; r < WS; ++r)
|
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);
|
: (my_base + ((size_t)parity * WS + r) * (size_t)slot_stride16);
|
||||||
int4* my_out = out4 + (size_t)rank * shard16;
|
int4* my_out = out4 + (size_t)R4D_TS_OFF(rank);
|
||||||
for (int k = w0 + tid; k < w1; k += nt) {
|
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];
|
float acc[LANES];
|
||||||
#pragma unroll
|
#pragma unroll
|
||||||
for (int j = 0; j < LANES; ++j) acc[j] = 0.0f;
|
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]);
|
for (int j = 0; j < LANES; ++j) r4d_ar_from_f(acc[j], po[j]);
|
||||||
my_out[k] = o;
|
my_out[k] = o;
|
||||||
#pragma unroll
|
#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);
|
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
|
||||||
|
|
||||||
// ---- assemble: every peer-owned shard from my region B copies
|
// ---- assemble: every peer-owned shard from my region B copies
|
||||||
for (int p = 0; p < NPEERS; ++p) {
|
for (int p = 0; p < NPEERS; ++p) {
|
||||||
const int owner = peers.rank[p];
|
const int owner = peers.rank[p];
|
||||||
const int4* srcB = my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride16 + shard16;
|
const int4* srcB = my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride16 + sh_max;
|
||||||
int4* dst = out4 + (size_t)owner * shard16;
|
int4* dst = out4 + (size_t)R4D_TS_OFF(owner);
|
||||||
for (int k = w0 + tid; k < w1; k += nt) dst[k] = srcB[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] = srcB[k];
|
||||||
}
|
}
|
||||||
|
#undef R4D_TS_OFF
|
||||||
|
#undef R4D_TS_LEN
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- host side ---
|
// --- 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) +
|
std::to_string(R4D_AR_TWOSHOT_WIDE_MAX_ELEMS) +
|
||||||
" elems); use the fallback path");
|
" elems); use the fallback path");
|
||||||
const long n16 = nbytes / 16;
|
const long n16 = nbytes / 16;
|
||||||
if (n16 % WS != 0)
|
if (n16 < WS)
|
||||||
throw std::runtime_error("all_reduce: message (" + std::to_string(n16) +
|
throw std::runtime_error("all_reduce: message (" + std::to_string(n16) +
|
||||||
" 16B words) does not shard evenly over " + std::to_string(WS) +
|
" 16B words) is shorter than one word a rank");
|
||||||
" ranks");
|
// the longest shard (the kernel's uneven split); regions A and B each hold one
|
||||||
const long shard16 = n16 / WS;
|
const long sh_max = (n16 + WS - 1) / WS;
|
||||||
if (2 * shard16 > (long)slot_stride16)
|
if (2 * sh_max > (long)slot_stride16)
|
||||||
throw std::runtime_error("all_reduce: shard (" + std::to_string(shard16) +
|
throw std::runtime_error("all_reduce: shard (" + std::to_string(sh_max) +
|
||||||
" 16B words) needs 2x inside the per-source slot stride (" +
|
" 16B words) needs 2x inside the per-source slot stride (" +
|
||||||
std::to_string(slot_stride16) + ")");
|
std::to_string(slot_stride16) + ")");
|
||||||
|
|
||||||
int nb = (int)nblocks;
|
int nb = (int)nblocks;
|
||||||
if (nb < 1) nb = R4D_AR_NB_DESIGN;
|
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;
|
if (nb > R4D_AR_WIDE_MAX_BLOCKS) nb = R4D_AR_WIDE_MAX_BLOCKS;
|
||||||
int nt = (int)nthreads;
|
int nt = (int)nthreads;
|
||||||
if (nt < 64) nt = 64;
|
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) \
|
#define R4D_AR_TS_LAUNCH(T, LN) \
|
||||||
r4d_ar_twoshot_wide_exact_kernel<T, LN, WS><<<nb, nt, 0, st>>>( \
|
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, \
|
(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)
|
(int)rank, (int)drain, (int)acq, (int)pub)
|
||||||
if (dtype == 0) R4D_AR_TS_LAUNCH(__hip_bfloat16, 8);
|
if (dtype == 0) R4D_AR_TS_LAUNCH(__hip_bfloat16, 8);
|
||||||
else if (dtype == 1) R4D_AR_TS_LAUNCH(__half, 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
|
#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,
|
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 peer_flags0, long peer_flags1, long peer_flags2,
|
||||||
long my_scratch, long my_flags, long seq_ctrs,
|
long my_scratch, long my_flags, long seq_ctrs,
|
||||||
|
|||||||
@@ -185,6 +185,7 @@ int r4d_rad_ar_oneshot_3rank_exact(const RadArgs*, RadStream);
|
|||||||
int r4d_rad_ar_gather_3rank (const RadArgs*, RadStream);
|
int r4d_rad_ar_gather_3rank (const RadArgs*, RadStream);
|
||||||
int r4d_rad_ar_oneshot_4rank_exact(const RadArgs*, RadStream);
|
int r4d_rad_ar_oneshot_4rank_exact(const RadArgs*, RadStream);
|
||||||
int r4d_rad_ar_oneshot_8rank_exact(const RadArgs*, RadStream);
|
int r4d_rad_ar_oneshot_8rank_exact(const RadArgs*, RadStream);
|
||||||
|
int r4d_rad_ar_twoshot_3rank_exact(const RadArgs*, RadStream);
|
||||||
int r4d_rad_ar_twoshot_4rank_exact(const RadArgs*, RadStream);
|
int r4d_rad_ar_twoshot_4rank_exact(const RadArgs*, RadStream);
|
||||||
int r4d_rad_ar_twoshot_8rank_exact(const RadArgs*, RadStream);
|
int r4d_rad_ar_twoshot_8rank_exact(const RadArgs*, RadStream);
|
||||||
int r4d_rad_ar_twoshot_4rank_ti8 (const RadArgs*, RadStream);
|
int r4d_rad_ar_twoshot_4rank_ti8 (const RadArgs*, RadStream);
|
||||||
|
|||||||
+24
-2
@@ -2668,6 +2668,13 @@ static const RadConstraint cArExact8[] = {
|
|||||||
C_EQ("world_size", 8), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
C_EQ("world_size", 8), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||||
C_P2P,
|
C_P2P,
|
||||||
};
|
};
|
||||||
|
/* No DIV: the three-rank two-shot shards unevenly (r4d_rad_ar_twoshot_3rank_exact), so the only
|
||||||
|
* shape it needs is the 16-byte push ar_setup already checks. */
|
||||||
|
static const RadConstraint cArTsExact3[] = {
|
||||||
|
C_EQ("world_size", 3), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||||
|
C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1), C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS),
|
||||||
|
C_P2P,
|
||||||
|
};
|
||||||
static const RadConstraint cArTsExact4[] = {
|
static const RadConstraint cArTsExact4[] = {
|
||||||
C_EQ("world_size", 4), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
C_EQ("world_size", 4), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||||
C_DIV("numel", 64), C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1),
|
C_DIV("numel", 64), C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1),
|
||||||
@@ -4345,6 +4352,20 @@ static const RadKernelInfo kKernels[] = {
|
|||||||
|
|
||||||
/* ---------------------------------------------------------------------------- all-reduce ----
|
/* ---------------------------------------------------------------------------- all-reduce ----
|
||||||
* The two-shot rows outrank the one-shots, for the reason the constraint block above states. */
|
* The two-shot rows outrank the one-shots, for the reason the constraint block above states. */
|
||||||
|
{ "ar_twoshot_3rank_exact", "all_reduce", "ar",
|
||||||
|
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, per-block pipelined "
|
||||||
|
"handshake",
|
||||||
|
"exactly 3 ranks, all peer-accessible; messages above "
|
||||||
|
R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements, two hops; shards as even as the word count "
|
||||||
|
"allows (the first words % 3 take one more); shares the wide family's flag/seq/scratch set; in "
|
||||||
|
"place; cudagraph-safe",
|
||||||
|
"bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks "
|
||||||
|
"and to ar_oneshot_3rank_exact",
|
||||||
|
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT,
|
||||||
|
ROW(cArTsExact3), ROW(tAr), nullptr,
|
||||||
|
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr,
|
||||||
|
r4d_shape_all_reduce },
|
||||||
|
|
||||||
{ "ar_twoshot_4rank_exact", "all_reduce", "ar",
|
{ "ar_twoshot_4rank_exact", "all_reduce", "ar",
|
||||||
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, per-block pipelined "
|
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, per-block pipelined "
|
||||||
"handshake",
|
"handshake",
|
||||||
@@ -4435,8 +4456,9 @@ static const RadKernelInfo kKernels[] = {
|
|||||||
r4d_shape_all_gather },
|
r4d_shape_all_gather },
|
||||||
{ "ar_oneshot_3rank_exact", "all_reduce", "ar",
|
{ "ar_oneshot_3rank_exact", "all_reduce", "ar",
|
||||||
"all-reduce (sum), one-shot push over P2P IPC scratch, per-block handshake",
|
"all-reduce (sum), one-shot push over P2P IPC scratch, per-block handshake",
|
||||||
"exactly 3 ranks, all peer-accessible; one scratch slot per (parity, source rank); "
|
"exactly 3 ranks, all peer-accessible; one scratch slot per (parity, source rank); in place; "
|
||||||
"the only wide form at three -- the two-shot shards the message `world` ways; cudagraph-safe",
|
"above " R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements ar_twoshot_3rank_exact outranks it; "
|
||||||
|
"cudagraph-safe",
|
||||||
"bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks",
|
"bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks",
|
||||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT,
|
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT,
|
||||||
ROW(cArExact3), ROW(tAr), nullptr,
|
ROW(cArExact3), ROW(tAr), nullptr,
|
||||||
|
|||||||
+148
-1
@@ -10684,6 +10684,148 @@ static void ar_three_rank(const Plugin& pl, const std::vector<int>& devs, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* THE THREE-RANK ALL-REDUCE ROWS, at the sizes the two-shot is selected for and the ones that do
|
||||||
|
* not divide.
|
||||||
|
*
|
||||||
|
* ar_three_rank's input is a constant per rank, which a shard landing at the wrong offset still
|
||||||
|
* sums correctly. This one gives every element its own value, so a word reduced by the wrong
|
||||||
|
* owner, gathered from the wrong slot or dropped at a short shard's end is a wrong number at a
|
||||||
|
* known index. The sizes walk the uneven split: words % 3 of 0, 1 and 2, from a 16-word message
|
||||||
|
* to the row's 20971520-element ceiling, in place (how every block issues it) and once out of
|
||||||
|
* place, back to back on ONE instance so the slot parity and the changing block count are
|
||||||
|
* exercised the way a step's mixed messages exercise them. The oracle is the fold the kernel
|
||||||
|
* claims -- fp32 in ascending rank order, rounded once -- so it is bit for bit, and so it is also
|
||||||
|
* the one-shot's answer: the band edge between the two rows changes no bits. */
|
||||||
|
struct ArPattern {
|
||||||
|
const RadKernelInfo* row;
|
||||||
|
int rank, dev;
|
||||||
|
const char* dtype; /* "bf16" or "fp32" */
|
||||||
|
std::vector<int64_t> sizes;
|
||||||
|
int out_of_place = -1; /* index into sizes, or -1 */
|
||||||
|
void* inst = nullptr;
|
||||||
|
int rc_init = RAD_E_STATE, rc_launch = RAD_OK;
|
||||||
|
std::vector<std::vector<float>> out;
|
||||||
|
};
|
||||||
|
|
||||||
|
static float ar_pattern_value(int rank, int64_t i) {
|
||||||
|
/* Integers up to 254 over 64: exact in bf16 (8 significant bits), and three of them need ten,
|
||||||
|
* so the bf16 sum is rounded and the rounding is checked too. */
|
||||||
|
return (float)((int)((i * 37 + (int64_t)rank * 101) % 509) - 254) / 64.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ar_pattern_worker(ArPattern* t) {
|
||||||
|
if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; }
|
||||||
|
const bool bf = !std::strcmp(t->dtype, "bf16");
|
||||||
|
const int esz = bf ? 2 : 4;
|
||||||
|
int64_t most = 0;
|
||||||
|
for (int64_t n : t->sizes) most = n > most ? n : most;
|
||||||
|
std::vector<RadParam> ps = {
|
||||||
|
PI("world_size", 3), PI("numel", most), PS("dtype", t->dtype), PI("exact", 1),
|
||||||
|
};
|
||||||
|
t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 3, &t->inst);
|
||||||
|
if (t->rc_init != RAD_OK) return;
|
||||||
|
void* in = nullptr;
|
||||||
|
void* out = nullptr;
|
||||||
|
if (hipMalloc(&in, (size_t)most * esz) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||||
|
if (hipMalloc(&out, (size_t)most * esz) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
|
||||||
|
std::vector<uint8_t> h((size_t)most * esz);
|
||||||
|
for (size_t k = 0; k < t->sizes.size(); ++k) {
|
||||||
|
const int64_t n = t->sizes[k];
|
||||||
|
for (int64_t i = 0; i < n; ++i) {
|
||||||
|
const float v = ar_pattern_value(t->rank, i);
|
||||||
|
if (bf) { const uint16_t b = h_to_bf16(v); std::memcpy(&h[(size_t)i * 2], &b, 2); }
|
||||||
|
else std::memcpy(&h[(size_t)i * 4], &v, 4);
|
||||||
|
}
|
||||||
|
st_memcpy(in, h.data(), (size_t)n * esz, hipMemcpyHostToDevice);
|
||||||
|
const bool oop = (int)k == t->out_of_place;
|
||||||
|
if (oop) st_memset(out, 0, (size_t)n * esz);
|
||||||
|
const uint32_t dt = bf ? RAD_BF16 : RAD_F32;
|
||||||
|
RadTensor ts[2] = { T(in, dt, { n }), T(out, dt, { n }) };
|
||||||
|
RadArgs a;
|
||||||
|
std::memset(&a, 0, sizeof(a));
|
||||||
|
a.t = ts; a.n_t = oop ? 2 : 1;
|
||||||
|
a.p = ps.data(); a.n_p = (int)ps.size();
|
||||||
|
a.instance = t->inst;
|
||||||
|
a.rank = t->rank;
|
||||||
|
a.world_size = 3;
|
||||||
|
const int rc = t->row->launch(&a, nullptr);
|
||||||
|
st_stream_sync(nullptr);
|
||||||
|
if (rc != RAD_OK && t->rc_launch == RAD_OK) t->rc_launch = rc;
|
||||||
|
st_memcpy(h.data(), oop ? out : in, (size_t)n * esz, hipMemcpyDeviceToHost);
|
||||||
|
std::vector<float> got((size_t)n);
|
||||||
|
for (int64_t i = 0; i < n; ++i) {
|
||||||
|
if (bf) { uint16_t b; std::memcpy(&b, &h[(size_t)i * 2], 2); got[(size_t)i] = h_bf16(b); }
|
||||||
|
else std::memcpy(&got[(size_t)i], &h[(size_t)i * 4], 4);
|
||||||
|
}
|
||||||
|
t->out.push_back(std::move(got));
|
||||||
|
}
|
||||||
|
st_free(in);
|
||||||
|
st_free(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ar_three_rank_pattern(const Plugin& pl, const std::vector<int>& devs, const char* name) {
|
||||||
|
const RadKernelInfo* row = find(pl, name);
|
||||||
|
if (!row) { ok(name, false, "row absent"); return; }
|
||||||
|
if (devs.size() < 3) { skip(name, "fewer than three gfx1201 cards"); return; }
|
||||||
|
struct Run { const char* dtype; std::vector<int64_t> sizes; int oop; };
|
||||||
|
const std::vector<Run> runs = {
|
||||||
|
/* bf16, the residual: words = numel / 8. 2048 x 2048 rows is words % 3 == 2, x 2049 is
|
||||||
|
* 0, x 2050 is 1; 128 is 16 words (1), 24008 is 3001 (1); 20971520 the ceiling (1). */
|
||||||
|
{ "bf16", { 4194304, 128, 4196352, 24008, 4198400, 20971520, 4194304 }, 2 },
|
||||||
|
/* fp32: words = numel / 4. 8196 -> 2049 (0), 4100 -> 1025 (2), 1048580 -> 262145 (2). */
|
||||||
|
{ "fp32", { 8196, 4100, 1048580 }, 0 },
|
||||||
|
};
|
||||||
|
for (const Run& run : runs) {
|
||||||
|
ArPattern t[3];
|
||||||
|
for (int r = 0; r < 3; ++r) {
|
||||||
|
t[r].row = row; t[r].rank = r; t[r].dev = devs[(size_t)r];
|
||||||
|
t[r].dtype = run.dtype; t[r].sizes = run.sizes; t[r].out_of_place = run.oop;
|
||||||
|
}
|
||||||
|
std::thread a(ar_pattern_worker, &t[0]), b(ar_pattern_worker, &t[1]), c(ar_pattern_worker, &t[2]);
|
||||||
|
a.join(); b.join(); c.join();
|
||||||
|
const std::string what = std::string(name) + " " + run.dtype;
|
||||||
|
const bool up = t[0].rc_init == RAD_OK && t[1].rc_init == RAD_OK && t[2].rc_init == RAD_OK;
|
||||||
|
ok((what + " init (peer rendezvous)").c_str(), up,
|
||||||
|
"rc " + std::to_string(t[0].rc_init) + "/" + std::to_string(t[1].rc_init) + "/" +
|
||||||
|
std::to_string(t[2].rc_init));
|
||||||
|
if (!up) return;
|
||||||
|
const bool ran = t[0].rc_launch == RAD_OK && t[1].rc_launch == RAD_OK &&
|
||||||
|
t[2].rc_launch == RAD_OK;
|
||||||
|
ok((what + " launches").c_str(), ran,
|
||||||
|
"rc " + std::to_string(t[0].rc_launch) + "/" + std::to_string(t[1].rc_launch) + "/" +
|
||||||
|
std::to_string(t[2].rc_launch));
|
||||||
|
if (ran) {
|
||||||
|
const bool bf = !std::strcmp(run.dtype, "bf16");
|
||||||
|
for (size_t k = 0; k < run.sizes.size(); ++k) {
|
||||||
|
const int64_t n = run.sizes[k];
|
||||||
|
int64_t bad = 0, first = -1;
|
||||||
|
for (int64_t i = 0; i < n; ++i) {
|
||||||
|
float acc = 0.0f;
|
||||||
|
for (int r = 0; r < 3; ++r) {
|
||||||
|
const float v = ar_pattern_value(r, i);
|
||||||
|
acc += bf ? h_bf16(h_to_bf16(v)) : v;
|
||||||
|
}
|
||||||
|
const float want = bf ? h_bf16(h_to_bf16(acc)) : acc;
|
||||||
|
for (int r = 0; r < 3; ++r)
|
||||||
|
if (t[r].out.size() <= k || t[r].out[k][(size_t)i] != want) {
|
||||||
|
if (first < 0) first = i;
|
||||||
|
++bad;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const int64_t words = n * (bf ? 2 : 4) / 16;
|
||||||
|
const std::string label =
|
||||||
|
what + " numel " + std::to_string(n) + " (" + std::to_string(words) +
|
||||||
|
" words, % 3 = " + std::to_string(words % 3) +
|
||||||
|
((int)k == run.oop ? ", out of place" : ", in place") +
|
||||||
|
") == fp32 ascending-rank fold on every rank, bit for bit";
|
||||||
|
ok(label.c_str(), bad == 0,
|
||||||
|
std::to_string(bad) + " elements wrong, first at " + std::to_string(first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (row->fini) for (int r = 0; r < 3; ++r) if (t[r].inst) row->fini(t[r].inst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void gather_three_rank(const Plugin& pl, const std::vector<int>& devs, const char* name) {
|
static void gather_three_rank(const Plugin& pl, const std::vector<int>& devs, const char* name) {
|
||||||
const RadKernelInfo* row = find(pl, name);
|
const RadKernelInfo* row = find(pl, name);
|
||||||
if (!row) { ok(name, false, "row absent"); return; }
|
if (!row) { ok(name, false, "row absent"); return; }
|
||||||
@@ -12518,7 +12660,7 @@ static void device_phase(const Plugin& pl) {
|
|||||||
/* Every collective has a host twin (r4d_rows.cpp): the same kernel with its mailbox in pinned
|
/* Every collective has a host twin (r4d_rows.cpp): the same kernel with its mailbox in pinned
|
||||||
* host memory. A case is the pair's, so the twin is held to exactly the original's checks. */
|
* host memory. A case is the pair's, so the twin is held to exactly the original's checks. */
|
||||||
for (const char* base : { "ar_oneshot_2rank_exact", "ar_gather_2rank", "ar_oneshot_3rank_exact",
|
for (const char* base : { "ar_oneshot_2rank_exact", "ar_gather_2rank", "ar_oneshot_3rank_exact",
|
||||||
"ar_gather_3rank" }) {
|
"ar_twoshot_3rank_exact", "ar_gather_3rank" }) {
|
||||||
for (const std::string& n : { std::string(base), std::string(base) + "_host" }) {
|
for (const std::string& n : { std::string(base), std::string(base) + "_host" }) {
|
||||||
if (!sel(n.c_str())) continue;
|
if (!sel(n.c_str())) continue;
|
||||||
const bool three = std::strstr(base, "3rank") != nullptr;
|
const bool three = std::strstr(base, "3rank") != nullptr;
|
||||||
@@ -12534,8 +12676,13 @@ static void device_phase(const Plugin& pl) {
|
|||||||
* ar_oneshot_3rank_exact` reproduces one of them alone. */
|
* ar_oneshot_3rank_exact` reproduces one of them alone. */
|
||||||
} else if (three && std::strstr(base, "gather")) {
|
} else if (three && std::strstr(base, "gather")) {
|
||||||
gather_three_rank(pl, devs, n.c_str());
|
gather_three_rank(pl, devs, n.c_str());
|
||||||
|
} else if (three && std::strstr(base, "twoshot")) {
|
||||||
|
ar_three_rank_pattern(pl, devs, n.c_str());
|
||||||
} else {
|
} else {
|
||||||
ar_three_rank(pl, devs, n.c_str());
|
ar_three_rank(pl, devs, n.c_str());
|
||||||
|
/* The one-shot through the same per-element oracle: both rows of the op agree
|
||||||
|
* with it, so they agree with each other at every size either could be given. */
|
||||||
|
ar_three_rank_pattern(pl, devs, n.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user