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:
baKon authored and StillDeadcode committed 2026-10-08 17:16:10 +02:00
1 parent a97985e7c2
commit 6b4a67f52e
7 files changed
+282 -41

No files matched your search

+15 -7
View File
@@ -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
// 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
// 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,
long peer_flags0, long peer_flags1,
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);
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
// one-shot (r4d_ar_twoshot_Nrank_exact.hip): shard s, a contiguous 1/ws of the message, is
// reduced by rank s (fp32, ascending rank order), then gathered. Sends 2*(ws-1)/ws*N 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
// the caller falls back:
// one-shot (r4d_ar_twoshot_Nrank_exact.hip): shard s, a contiguous run of the message's 16-byte
// words, is reduced by rank s (fp32, ascending rank order), then gathered. Sends 2*(ws-1)/ws*N
// bytes per rank in two hops. Covers the sizes where the one-shot's (ws-1)*N send is too heavy.
// Shards are as even as the word count allows (the first n16 % ws take one more word), so a
// 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
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,
long peer_flags0, long peer_flags1, long peer_flags2,
long my_scratch, long my_flags, long seq_ctrs,