libr4d: the eight-rank two-shot in pieces, both hops on the link at once, by message size

Each block splits its words (in shard space) into K runs. Step j pushes run j+1's first hop and
reduces and pushes run j's second, then handshakes, so a rank with a short shard sends and
receives at once instead of one direction a hop. A launch takes K+1 handshakes instead of two.
The flag a (block, source) publishes is a monotone counter, so one handshake vouches for every
earlier one, and r4d_ar_seq_take reserves the K steps at once (`steps`, 1 for every other kernel):
one-shots and gathers on the same instance stay in the family's flag space. The regions stay at 0
and the longest shard, so the scratch is as before; every word is touched by the same thread in
the same three steps, so the fold, the bits and the in-place form are unchanged. The launcher
clamps K to the words a block holds, and K 1 is its own instantiation without the loop.

  16 MiB, bf16, warm   K 1      K 2    K 4    K 8
  even                 2841 us  2806   2667   2470
  4,1,0,4,0,1,4,4      2730     2126   1798   1636
  4,2,1,4,1,2,4,4      2495     2121   1940   1922

4,1,0,4,0,1,4,4 in 8 pieces is 42% under the even split. Fitting measured = f x computed + h x
(K+1) at K 4 and 8 gives f = 1.21 and h about 5 us a handshake: what separates 1636 us from the
1.17 ms the Gen4 links need is their efficiency, not the handshakes.

A decode-sized message loses with either:

  numel      even K 1   4,1,0,4,0,1,4,4: K 1 / K 2 / K 4 / K 8
  196608     73 us      85 / 76 / 81 / 113
  524288     214        207 / 166 / 169 / 198
  2097152    699        711 / 581 / 531 / 527
  8388608    2841       2730 / 2126 / 1798 / 1636

so below RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS and RADIANCE_AR8_PIECES_MIN_ELEMS (262144 elements
each) the message runs even and in one piece, on the kernel as it was. Above them, without
RADIANCE_AR8_PIECES, K follows the size (r4d_ar8_auto_pieces): 2 under 1048576 elements, 4 under
4194304, 8 above, where the crossings above lie. RADIANCE_AR8_PIECES (1 to 16) fixes K over the
floor, and a launch's `pieces` parameter (the selftest's) overrides that. Weights and K are a
function of the message size and the instance (ar8_weights_for, ar8_pieces_for), so a replay of
the same size decides the same; the slot is sized for weights only when the largest message is
over the weight floor. Every rank compares both variables and both floors at init.

r4d_selftest, with both floors at 0: the weighted and even sets at K 1, 2 and 4 (1- and 9-word
messages cross the launcher's clamp), and one instance whose K changes from launch to launch (1,
4, 2, 8, 16, 3, 1), which a wrong flag reservation hangs or races. At the default floors: the size
rule over 131072 to 4194304 elements, K 4 on both sides of the piece floor, a 131073-word message
over the weight floor (served only weighted) and a 9-word one under it (refused, and served at a
floor of 64). 192 of 192 on the cards. --perfar8 names the floors and times the two-shot as the
server runs it and at K 1, 2, 4 and 8.

GLM-5.3-Flash at TP8 (an out-of-tree plugin) on these eight cards with 4,1,0,4,0,1,4,4, against
the same build without in the same hour: a 20K-token prefill 3450 tok/s against 2945, 8K 3372
against 2920; eight agent streams 457 tok/s against 458, one 141 against 139. K 8 at every size
instead gave the same prefill (3461) and 436 at eight streams, 113 at one.

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 4777be8654
commit adc7f37a70
7 files changed
+391 -105

No files matched your search

+107 -61
View File
@@ -18,10 +18,15 @@
// 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.
//
// At 8 ranks the message may also run in pieces (RADIANCE_AR8_PIECES, by default a count from
// the message size, r4d_ar_shards.h): each block's words split into `pieces` runs pipelined so
// piece j+1's phase 1 shares the link with piece j's phase 2. A rank with a short shard then
// sends and receives at once instead of one direction per phase.
//
// 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
// can carry any mix of one-shot and two-shot launches. The slot stride must hold the largest
// requirement in the mix.
// space plus the 2s-1 half-step (with pieces: pieces+1 values from 2s-1 on, after reserving one
// step a piece), so one buffer set can carry any mix of one-shot and two-shot launches. The slot
// stride must hold the largest requirement in the mix.
#include "r4d.h"
#include "r4d_ar_wide.h"
#include "r4d_ar_shards.h"
@@ -34,19 +39,22 @@ struct R4DArShardOff {
int sh_max;
};
template <typename T, int LANES, int WS>
// PIECED false fixes pieces at 1, so the unpipelined launch keeps the straight-line kernel.
template <typename T, int LANES, int WS, bool PIECED>
__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, R4DArShardOff<WS> sh,
int slot_stride16, int rank, int drain, int acq, int pub,
long long* gate, unsigned int* gate_ctr) {
long long* gate, unsigned int* gate_ctr, int pieces_arg) {
constexpr int NPEERS = WS - 1;
const int pieces = PIECED ? pieces_arg : 1;
const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
__shared__ unsigned int s_seq, s_slot;
const long long gate_word = r4d_ar_link_word(gate, (unsigned int)tid);
r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot); // device-resident, replay-safe
const unsigned int s = s_seq;
// device-resident, replay-safe; `pieces` steps reserve the 2*pieces flag values this launch uses
r4d_ar_seq_take(seq_ctrs, R4D_AR_WIDE_MAX_BLOCKS, s_seq, s_slot, (unsigned int)pieces);
const unsigned int v0 = 2u * s_seq - 1u;
const int parity = (int)s_slot;
const int sh_max = sh.sh_max;
@@ -56,6 +64,7 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
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;
const int pw = (w + pieces - 1) / pieces; // piece j of the block: [w0 + j*pw, +pw) within w1
// region A of my slot in every peer's scratch (B sits at +sh_max)
int4* pa[NPEERS];
@@ -63,60 +72,83 @@ __global__ void r4d_ar_twoshot_wide_exact_kernel(
for (int p = 0; p < NPEERS; ++p)
pa[p] = peers.scratch[p] + ((size_t)parity * WS + rank) * (size_t)slot_stride16;
// ---- phase 1 push: my contribution to each peer-owned shard -> owner's region A
for (int p = 0; p < NPEERS; ++p) {
const int owner = peers.rank[p];
const int4* src = in4 + (size_t)R4D_TS_OFF(owner);
int4* dst = pa[p];
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);
// ---- reduce my shard (fp32, ascending rank, own contribution from the input), and phase 2
// push: the reduced word goes to my out and every peer's region B in the same pass
const int4* src[WS];
#pragma unroll
for (int r = 0; r < WS; ++r)
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)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;
#pragma unroll
for (int r = 0; r < WS; ++r) {
const int4 v = src[r][k];
const T* pv = reinterpret_cast<const T*>(&v);
#pragma unroll
for (int j = 0; j < LANES; ++j) acc[j] += r4d_ar_to_f(pv[j]);
}
int4 o;
T* po = reinterpret_cast<T*>(&o);
#pragma unroll
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][sh_max + k] = o; // region B
}
r4d_ar_handshake<WS>(peers, my_flags, b, rank, 2u * s, drain, acq, pub);
/* The hold ends after the second handshake, not the first: the reduce above is fused with the
* phase-2 push (every reduced word goes to each peer's region B), so at the first handshake the
* link still carries this launch's phase-2 bytes. Here every phase-1 and phase-2 remote store
* has retired behind its flag store and every peer's pushes to this rank have been waited on;
* only the local assemble is left (r4d_ar_seq.h). */
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
// ---- 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 + 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];
// phase 1: my contribution to each peer-owned shard -> owner's region A
auto push1 = [&](int j) {
const int p0 = w0 + j * pw, p1 = p0 + pw < w1 ? p0 + pw : w1;
for (int p = 0; p < NPEERS; ++p) {
const int owner = peers.rank[p];
const int4* s1 = in4 + (size_t)R4D_TS_OFF(owner);
int4* dst = pa[p];
const int lim = p1 < R4D_TS_LEN(owner) ? p1 : R4D_TS_LEN(owner);
for (int k = p0 + tid; k < lim; k += nt) dst[k] = s1[k];
}
};
// reduce my shard (fp32, ascending rank, own contribution from the input) fused with the phase-2
// push: the reduced word goes to my out and every peer's region B in the same pass
auto reduce2 = [&](int j) {
const int p0 = w0 + j * pw, p1 = p0 + pw < w1 ? p0 + pw : w1;
const int my_lim = p1 < R4D_TS_LEN(rank) ? p1 : R4D_TS_LEN(rank);
for (int k = p0 + tid; k < my_lim; k += nt) {
float acc[LANES];
#pragma unroll
for (int l = 0; l < LANES; ++l) acc[l] = 0.0f;
#pragma unroll
for (int r = 0; r < WS; ++r) {
const int4 v = src[r][k];
const T* pv = reinterpret_cast<const T*>(&v);
#pragma unroll
for (int l = 0; l < LANES; ++l) acc[l] += r4d_ar_to_f(pv[l]);
}
int4 o;
T* po = reinterpret_cast<T*>(&o);
#pragma unroll
for (int l = 0; l < LANES; ++l) r4d_ar_from_f(acc[l], po[l]);
my_out[k] = o;
#pragma unroll
for (int p = 0; p < NPEERS; ++p) pa[p][sh_max + k] = o; // region B
}
};
// every peer-owned shard from my region B copies
auto assemble = [&](int j) {
const int p0 = w0 + j * pw, p1 = p0 + pw < w1 ? p0 + pw : w1;
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 + sh_max;
int4* dst = out4 + (size_t)R4D_TS_OFF(owner);
const int lim = p1 < R4D_TS_LEN(owner) ? p1 : R4D_TS_LEN(owner);
for (int k = p0 + tid; k < lim; k += nt) dst[k] = srcB[k];
}
};
/* Pieces run as a pipeline so both directions of a link carry bytes at once: step j pushes
* piece j+1's phase 1 and piece j's phase 2. One flag word per (block, source) is a monotone
* counter, so a step's single handshake (v0 + 1 + j) also vouches for every earlier one. A rank
* owning little sends nearly all its bytes in phase 1 and receives them in phase 2; without the
* overlap one direction idles in each phase. With one piece the sequence is push, flag 2s-1,
* reduce, flag 2s, assemble. A word is touched by the same thread in all three steps, which keeps
* the in-place form safe. */
push1(0);
r4d_ar_handshake<WS>(peers, my_flags, b, rank, v0, drain, acq, pub);
for (int j = 0; j < pieces; ++j) {
if (j + 1 < pieces) push1(j + 1);
reduce2(j);
if (j > 0) assemble(j - 1);
r4d_ar_handshake<WS>(peers, my_flags, b, rank, v0 + 1u + (unsigned int)j, drain, acq, pub);
}
/* The hold ends after the last handshake, not the first: the reduce is fused with the phase-2
* push (every reduced word goes to each peer's region B), so until then the link still carries
* this launch's phase-2 bytes. Here every phase-1 and phase-2 remote store has retired behind
* its flag store and every peer's pushes to this rank have been waited on; only the local
* assemble of the last piece is left (r4d_ar_seq.h). */
r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid);
assemble(pieces - 1);
#undef R4D_TS_OFF
#undef R4D_TS_LEN
}
@@ -127,7 +159,8 @@ 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, const int* shard_weights) {
long gate, long gate_ctr, const int* shard_weights,
long pieces) {
constexpr int NPEERS = WS - 1;
if (rank < 0 || rank >= WS)
throw std::runtime_error("all_reduce: rank " + std::to_string(rank) +
@@ -173,6 +206,12 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
int nt = (int)nthreads;
if (nt < 64) nt = 64;
if (nt > 1024) nt = 1024;
if (pieces < 1 || pieces > R4D_AR_TS_MAX_PIECES)
throw std::runtime_error("all_reduce: pieces " + std::to_string(pieces) + " outside 1.." +
std::to_string((int)R4D_AR_TS_MAX_PIECES));
// a block's share of the longest shard bounds the pieces worth a handshake each
const long words_a_block = (sh_max + nb - 1) / nb;
const int np = (int)(pieces < words_a_block ? pieces : words_a_block);
R4DArWidePeers<int4, NPEERS> peers{};
for (int i = 0; i < NPEERS; ++i) {
@@ -198,17 +237,23 @@ static void ar_twoshot_launch(const long* peer_scratch, const long* peer_flags,
}
hipStream_t st = (hipStream_t)stream_i;
#define R4D_AR_TS_LAUNCH(T, LN) \
r4d_ar_twoshot_wide_exact_kernel<T, LN, WS><<<nb, nt, 0, st>>>( \
#define R4D_AR_TS_LAUNCH_P(T, LN, P) \
r4d_ar_twoshot_wide_exact_kernel<T, LN, WS, P><<<nb, nt, 0, st>>>( \
(const int4*)inp, peers, (const int4*)my_scratch, (unsigned int*)my_flags, \
(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)
(unsigned int*)gate_ctr, np)
#define R4D_AR_TS_LAUNCH(T, LN) \
do { \
if (np > 1) R4D_AR_TS_LAUNCH_P(T, LN, true); \
else R4D_AR_TS_LAUNCH_P(T, LN, false); \
} while (0)
if (dtype == 0) R4D_AR_TS_LAUNCH(__hip_bfloat16, 8);
else if (dtype == 1) R4D_AR_TS_LAUNCH(__half, 8);
else if (dtype == 2) R4D_AR_TS_LAUNCH(float, 4);
else throw std::runtime_error("all_reduce: bad dtype");
#undef R4D_AR_TS_LAUNCH
#undef R4D_AR_TS_LAUNCH_P
}
void r4d_ar_twoshot_3rank_exact(long peer_scratch0, long peer_scratch1, long peer_flags0,
@@ -220,7 +265,8 @@ 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, nullptr);
dtype, rank, stream, nblocks, nthreads, drain, acq, pub, 0L, 0L, nullptr,
1L);
}
void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
@@ -233,7 +279,7 @@ void r4d_ar_twoshot_4rank_exact(long peer_scratch0, long peer_scratch1, long pee
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,
nullptr);
nullptr, 1L);
}
void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2,
@@ -244,7 +290,7 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee
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 int* shard_weights) {
const int* shard_weights, long pieces) {
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,
@@ -252,5 +298,5 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee
/* 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,
shard_weights);
shard_weights, pieces);
}