libr4d: the eight-rank two-shot's shards weighted per rank (RADIANCE_AR8_SHARD_WEIGHTS)

In the two-shot rank r reduces shard r: in the first hop it sends N - len_r and receives
(ws-1)*len_r, in the second the reverse. With even shards every rank moves 1.75 N each way at
eight ranks, so on a board whose links differ the slowest link sets the time. On eight R9700 (four
links at Gen5 x16, two at Gen5 x8 and two that trained at Gen4 x8, 14.3 GB/s one way by
TransferBench) the 16 MiB two-shot, a 2048-token chunk at 4096 wide, takes 2.45 to 3.05 ms over a
day, where the Gen4 links alone need 2.05 ms for their 28 MiB.

RADIANCE_AR8_SHARD_WEIGHTS=w0,...,w7 sizes rank r's shard in proportion to w_r (the floor, then
the few remaining words one each to the weighted ranks in ascending order); a rank of weight 0
owns no shard and only pushes, gathers and handshakes. The shard bounds become a table the kernel
takes by value (r4d_ar_shards.h, host-only, shared by the launcher, the shim and the selftest), so
a recorded launch keeps its table. Equal weights give the even split word for word, and unset the
split is the even one as before; three and four ranks stay unweighted. Which rank reduces a word
changes the link it crosses, not its fold, so every table gives the same bits.

Every rank must cut the message at the same words: init reads the variable on each rank and fails
on all of them if the ranks disagree. A weight over half the sum makes the longest shard's two
regions outgrow a message, and only then does init grow the slot. Weighted shards need not divide
the words evenly, so the even rule (words % 8, else RAD_E_SHAPE) applies without weights only.

Measured with --perfar8 (16 MiB, bf16, warm): even 2.78 to 3.05 ms that day, 4,1,0,4,0,1,4,4 (no
shard on the Gen4 ranks) 2.71, 4,2,1,4,1,2,4,4 2.50, 2,1,1,2,1,1,2,2 2.58. Only 10 to 17%: with
the hops separated by a handshake a rank sends in one and receives in the other, and the larger of
N - len and 7 len a hop is least at len = N/8. The weights pay once the hops overlap.

r4d_selftest: ar_wide_weighted runs 0,3,1,0,5,1,2,4, 1,0,0,0,0,0,0,0 and 4,1,0,4,0,1,4,4 over
uneven and empty shards, a single owner (the slot grows) and messages of 1 and 9 words, which the
even split refuses; the refusal check runs without weights whatever the environment. 55 of 55 on
the cards, every one bit for bit with the fold. --perfar8 prints the weights, every size's shard
lengths and each rank's time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 authored and StillDeadcode committed 2026-10-19 10:38:33 +02:00
1 parent 0eb3d96ce1
commit 4777be8654
5 files changed
+332 -92

No files matched your search

+45 -19
View File
@@ -9,10 +9,14 @@
// 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: n16 words split into base = n16/ws words per shard, and the first n16 % ws shards
// take one more, so shard s starts at s*base + min(s, rem). At 4 and 8 ranks engine residuals
// divide evenly; at 3 they rarely do (a 2048-wide bf16 row is 256 words). Each word is the same
// fp32 fold in ascending rank order whoever owns it, so the bits match the one-shot's.
// The shard table (r4d_ar_shards.h) comes in as a kernel argument. By default it is the even
// split (the first n16 % ws shards one word longer); at 3 ranks engine residuals rarely divide (a
// 2048-wide bf16 row is 256 words). At 8 ranks it may be weighted per link
// (RADIANCE_AR8_SHARD_WEIGHTS); a rank with weight 0 owns nothing and only pushes, gathers and
// handshakes. Over both phases a rank moves N + (ws-2)*len_r each way, but phase 1 only sends
// N - len_r (receiving (ws-1)*len_r) and phase 2 the reverse, so a short shard on a slow link pays
// only as far as the phases overlap on that link (docs/GUIDE.md, 4.7). Each word is the same fp32
// fold in ascending rank order whoever owns it, so the bits match the one-shot's.
//
// Shares the wide family's flag/seq/scratch set (r4d_ar_wide.h): it publishes in the 2*seq flag
// space plus the 2s-1 half-step and advances the per-block counter once per AR, so one buffer set
@@ -20,14 +24,21 @@
// requirement in the mix.
#include "r4d.h"
#include "r4d_ar_wide.h"
#include "r4d_ar_shards.h"
#include <stdexcept>
#include <string>
template <int WS>
struct R4DArShardOff {
int off[WS + 1];
int sh_max;
};
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 n16,
int4* __restrict__ out4, unsigned int* __restrict__ seq_ctrs, R4DArShardOff<WS> sh,
int slot_stride16, int rank, int drain, int acq, int pub,
long long* gate, unsigned int* gate_ctr) {
constexpr int NPEERS = WS - 1;
@@ -38,11 +49,9 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
const unsigned int s = s_seq;
const int parity = (int)s_slot;
// 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))
const int sh_max = sh.sh_max;
#define R4D_TS_OFF(s) (sh.off[s])
#define R4D_TS_LEN(s) (sh.off[(s) + 1] - sh.off[s])
const int w = (sh_max + nb - 1) / nb; // block b owns words [w0,w1) of the SHARD space
const int w0 = b * w;
@@ -118,7 +127,7 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
long my_flags, long seq_ctrs, long slot_stride16, long inp,
long out, long n_elem, long dtype, long rank, long stream_i,
long nblocks, long nthreads, long drain, long acq, long pub,
long gate, long gate_ctr) {
long gate, long gate_ctr, const int* shard_weights) {
constexpr int NPEERS = WS - 1;
if (rank < 0 || rank >= WS)
throw std::runtime_error("all_reduce: rank " + std::to_string(rank) +
@@ -133,11 +142,25 @@ 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)
if (!shard_weights && n16 < WS)
throw std::runtime_error("all_reduce: message (" + std::to_string(n16) +
" 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 (n16 < 1) throw std::runtime_error("all_reduce: empty message");
if (shard_weights) {
long wsum = 0;
for (int s = 0; s < WS; ++s) {
if (shard_weights[s] < 0 || shard_weights[s] > R4D_AR_SHARD_WEIGHT_MAX)
throw std::runtime_error("all_reduce: shard weight out of range");
wsum += shard_weights[s];
}
if (wsum <= 0) throw std::runtime_error("all_reduce: shard weights sum to zero");
}
long off[WS + 1];
const long sh_max = r4d_ar_shard_table(n16, WS, shard_weights, off);
R4DArShardOff<WS> sh{};
for (int s = 0; s <= WS; ++s) sh.off[s] = (int)off[s];
sh.sh_max = (int)sh_max;
// regions A and B each hold the longest shard
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 (" +
@@ -178,7 +201,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)n16, (int)slot_stride16, \
(int4*)out, (unsigned int*)seq_ctrs, sh, (int)slot_stride16, \
(int)rank, (int)drain, (int)acq, (int)pub, (long long*)gate, \
(unsigned int*)gate_ctr)
if (dtype == 0) R4D_AR_TS_LAUNCH(__hip_bfloat16, 8);
@@ -197,7 +220,7 @@ void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long pee
const long pf[2] = {peer_flags0, peer_flags1};
/* No link gate at three ranks: the row answers 1, the engine releases by barrier. */
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, 0L, 0L);
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L, nullptr);
}
void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
@@ -209,7 +232,8 @@ void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long pee
const long ps[3] = {peer_scratch0, peer_scratch1, peer_scratch2};
const long pf[3] = {peer_flags0, peer_flags1, peer_flags2};
ar_twoshot_launch<4>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, gate, gate_ctr);
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, gate, gate_ctr,
nullptr);
}
void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
@@ -219,12 +243,14 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee
long peer_flags5, long peer_flags6, 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) {
long nblocks, long nthreads, long drain, long acq, long pub,
const int* shard_weights) {
const long ps[7] = {peer_scratch0, peer_scratch1, peer_scratch2, peer_scratch3,
peer_scratch4, peer_scratch5, peer_scratch6};
const long pf[7] = {peer_flags0, peer_flags1, peer_flags2, peer_flags3,
peer_flags4, peer_flags5, peer_flags6};
/* No link gate at eight ranks: the row answers 1, the engine releases by barrier. */
ar_twoshot_launch<8>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem,
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L);
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L,
shard_weights);
}