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

+8 -6
View File
@@ -382,11 +382,13 @@ void r4d_ar_wide_dims(int* max_blocks, int* nb_design, int* max_peers);
// count allows (the first n16 % ws take one more word), so the message need not divide by the
// rank count. At 8 ranks `shard_weights` (8 ints, or null for the even split) sizes the shards per
// rank instead (r4d_ar_shards.h); every rank must pass the same weights. Regions A and B sit at 0
// and the longest shard, so the slot stride must hold twice that. Above the bound below the
// caller falls back to the one-shot; it only guards the kernel's int word offsets (2^28 elements
// is 2^26 words at fp32). A lower bound would send large prefill chunks (e.g. 8192 tokens at
// Gemma 4's 2816 width, 23M-46M elements) to the one-shot.
enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 268435456 };
// and the longest shard, so the slot stride must hold twice that. `pieces` (8 ranks, 1 to
// R4D_AR_TS_MAX_PIECES, the same on every rank) pipelines each block's words in that many runs so
// phase 1 of one run and phase 2 of the previous share the link; 1 is the unpipelined kernel.
// Above the bound below the caller falls back to the one-shot; it only guards the kernel's int
// word offsets (2^28 elements is 2^26 words at fp32). A lower bound would send large prefill
// chunks (e.g. 8192 tokens at Gemma 4's 2816 width, 23M-46M elements) to the one-shot.
enum { R4D_AR_TWOSHOT_WIDE_MAX_ELEMS = 268435456, R4D_AR_TS_MAX_PIECES = 16 };
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,
@@ -406,7 +408,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);
// The tiered int8 wire on both two-shot hops (r4d_ar_twoshot_Nrank_ti8.hip; codec in
// r4d_ar_ti8.h), 3 or 4 ranks. Int8 groups of 32 with an fp16 scale and a per-group record
// (T1 plain, T2 top-1-exact, T3 wht32), 38 bytes/group; tiering is deterministic and
+81 -8
View File
@@ -34,6 +34,8 @@
static_assert(R4D_AR_TWOSHOT_WIDE_MAX_ELEMS == 268435456,
"r4d_rows.cpp restates this bound; libr4d changed it");
static_assert(R4D_MAX_RANKS == 8, "the 8-rank entry point names exactly seven peers");
static_assert((long long)R4D_AR8_MIN_ELEMS_MAX == (long long)R4D_AR_TWOSHOT_WIDE_MAX_ELEMS,
"r4d_ar_shards.h restates the two-shot band's top");
/* ------------------------------------------------------------------ throw conversion
*
@@ -70,6 +72,9 @@ struct Rendezvous {
long seq [R4D_MAX_RANKS] = { 0 };
int dev [R4D_MAX_RANKS] = { 0 };
int shard_w[R4D_MAX_RANKS][R4D_MAX_RANKS] = { { 0 } };
int pieces [R4D_MAX_RANKS] = { 0 };
long long weights_min[R4D_MAX_RANKS] = { 0 };
long long pieces_min [R4D_MAX_RANKS] = { 0 };
int failed = 0;
};
@@ -117,6 +122,12 @@ struct ArInst {
* scratch was sized for them and a recorded launch keeps its table. */
int shard_weighted = 0;
int shard_w[R4D_MAX_RANKS] = { 0 };
/* Its piece count (RADIANCE_AR8_PIECES), 0 for the size rule (r4d_ar8_auto_pieces); a launch's
* `pieces` parameter overrides it. */
int pieces = 0;
/* Messages under these sizes (elements) run even and in one piece, whatever is configured. */
long long weights_min = R4D_AR8_MIN_ELEMS_DEFAULT;
long long pieces_min = R4D_AR8_MIN_ELEMS_DEFAULT;
};
/* A parameter that may be declared as a range: init gets one band's frozen geometry, and buffers
@@ -156,6 +167,49 @@ bool ar8_shard_weights(int* w) {
return true;
}
/* RADIANCE_AR8_PIECES and the two size floors, read like the weights. */
int ar8_pieces() {
const char* e = std::getenv(R4D_AR8_PIECES_ENV);
if (!e || !*e) return 0;
long long v = 0;
if (!r4d_ar_parse_count(e, R4D_AR_TS_MAX_PIECES, &v) || v < 1) {
static std::once_flag said;
std::call_once(said, [e] {
std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs 1..%d); the eight-rank "
"two-shot takes its piece count from the message size\n",
R4D_AR8_PIECES_ENV, e, (int)R4D_AR_TS_MAX_PIECES);
});
return 0;
}
return (int)v;
}
long long ar8_min_elems(const char* name, std::once_flag* said) {
const char* e = std::getenv(name);
if (!e || !*e) return R4D_AR8_MIN_ELEMS_DEFAULT;
long long v = 0;
if (!r4d_ar_parse_count(e, R4D_AR8_MIN_ELEMS_MAX, &v)) {
std::call_once(*said, [name, e] {
std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs 0..%d elements); using %d\n",
name, e, (int)R4D_AR8_MIN_ELEMS_MAX,
(int)R4D_AR8_MIN_ELEMS_DEFAULT);
});
return R4D_AR8_MIN_ELEMS_DEFAULT;
}
return v;
}
/* The eight-rank two-shot's choice for one message, a function of its size and the instance
* alone: a recorded launch keeps the table and piece count it was recorded with, and replays of
* the same size agree with it. */
const int* ar8_weights_for(const ArInst* in, long numel) {
return in->shard_weighted && numel >= in->weights_min ? in->shard_w : nullptr;
}
long long ar8_pieces_for(const ArInst* in, long numel, long long asked) {
if (numel < in->pieces_min) return 1;
return asked > 0 ? asked : r4d_ar8_auto_pieces(numel);
}
/* Sequence counter allocation in words: 2 * nbw (r4d_ar_seq.h), then r4d_ar_link_landed's block
* counter, padded to a cache line. */
long kSeqWords(long nbw) { return 2L * nbw + 32; }
@@ -184,11 +238,18 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code,
in->max_numel = (long)max_numel;
in->slot_bytes = align16((long)max_numel * in->esize);
in->slot16 = in->slot_bytes / 16;
if (world == 8) {
static std::once_flag said_w, said_p;
in->pieces = ar8_pieces();
in->weights_min = ar8_min_elems(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, &said_w);
in->pieces_min = ar8_min_elems(R4D_AR8_PIECES_MIN_ENV, &said_p);
}
if (world == 8 && ar8_shard_weights(in->shard_w)) {
in->shard_weighted = 1;
const long need16 =
2 * r4d_ar_shard_max_bound((long)max_numel * in->esize / 16, 8, in->shard_w);
if (need16 > in->slot16) {
/* Sized only if some message this instance carries is weighted. */
if (max_numel >= in->weights_min && need16 > in->slot16) {
in->slot16 = need16;
in->slot_bytes = need16 * 16;
}
@@ -265,16 +326,24 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code,
g_rv.dev[rank] = dev;
for (int i = 0; i < R4D_MAX_RANKS; ++i)
g_rv.shard_w[rank][i] = in->shard_weighted ? in->shard_w[i] : -1;
g_rv.pieces[rank] = in->pieces;
g_rv.weights_min[rank] = in->weights_min;
g_rv.pieces_min[rank] = in->pieces_min;
}
int rc = rv_barrier(world);
if (rc != RAD_OK) return rc;
/* Every rank must cut the message at the same words, or an owner reduces words its peers
* pushed elsewhere. All ranks compare the same table, so all refuse together. */
* pushed elsewhere, and take the same handshake steps, or the flags drift apart. All ranks
* compare the same values, so all refuse together. */
for (int r = 0; r < world; ++r)
if (std::memcmp(g_rv.shard_w[r], g_rv.shard_w[rank], sizeof(g_rv.shard_w[rank]))) {
std::fprintf(stderr, "[r4d] ranks %d and %d read different %s\n", rank, r,
R4D_AR8_SHARD_WEIGHTS_ENV);
if (std::memcmp(g_rv.shard_w[r], g_rv.shard_w[rank], sizeof(g_rv.shard_w[rank])) ||
g_rv.pieces[r] != g_rv.pieces[rank] ||
g_rv.weights_min[r] != g_rv.weights_min[rank] ||
g_rv.pieces_min[r] != g_rv.pieces_min[rank]) {
std::fprintf(stderr, "[r4d] ranks %d and %d read different eight-rank two-shot "
"settings (%s, %s and their _MIN_ELEMS)\n", rank, r,
R4D_AR8_SHARD_WEIGHTS_ENV, R4D_AR8_PIECES_ENV);
return RAD_E_STATE;
}
@@ -546,7 +615,7 @@ static int ar_setup(const RadArgs* a, ArCall* c, int shard_nb) {
const long words16 = (long)n * in->esize / 16;
long basis = shard_nb ? words16 / in->world : words16;
if (shard_nb && in->shard_weighted) {
if (shard_nb && ar8_weights_for(in, n)) {
long off[R4D_MAX_RANKS + 1];
basis = r4d_ar_shard_table(words16, in->world, in->shard_w, off);
}
@@ -898,13 +967,17 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) {
if (c.in->world != 8) return RAD_E_SHAPE;
if (c.numel > R4D_AR_TWOSHOT_WIDE_MAX_ELEMS) return RAD_E_SHAPE;
const ArInst* in = c.in;
const int* weights = in->shard_weighted ? in->shard_w : nullptr;
const int* weights = ar8_weights_for(in, c.numel);
/* Even shards keep the four-rank rule (whole words a rank); weighted ones are uneven anyway. */
const long words16 = c.numel * in->esize / 16;
if (!weights && words16 % in->world) return RAD_E_SHAPE;
long off[R4D_MAX_RANKS + 1];
const long sh_max = r4d_ar_shard_table(words16, in->world, weights, off);
if (2 * sh_max * 16 > in->slot_bytes) return RAD_E_SCRATCH;
/* Pieces only move the handshakes; the regions stay at 0 and sh_max, so scratch is as above. */
const long long asked = r4d_pgeti_or(a->p, a->n_p, "pieces", in->pieces);
if (asked < 0 || asked > R4D_AR_TS_MAX_PIECES) return RAD_E_INVAL;
const long long pieces = ar8_pieces_for(in, c.numel, asked);
R4D_TRY("ar_twoshot_8rank_exact",
r4d_ar_twoshot_8rank_exact(in->peer_scratch[0], in->peer_scratch[1], in->peer_scratch[2],
in->peer_scratch[3], in->peer_scratch[4], in->peer_scratch[5],
@@ -915,7 +988,7 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) {
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, weights));
c.pub, weights, (long)pieces));
return RAD_OK;
}
+7 -3
View File
@@ -33,17 +33,21 @@
// Sets `s_seq` to this block's handshake value and `s_slot` to the launch's scratch slot (0 or 1),
// then barriers so every thread can read both. `nbw` is the capacity the counters were allocated
// for, and gridDim.x must not exceed it.
// for, and gridDim.x must not exceed it. `steps` handshake values are taken at once (s_seq is the
// first): a kernel that handshakes more than twice per launch reserves the flag values it
// publishes, so every kernel on the instance stays in the same monotone flag space. All ranks must
// take the same number for the same block.
//
// The barrier fences LDS only, since it publishes just the two LDS words. A full __syncthreads
// would wait for the outstanding parity atomics and invalidate the vector caches, putting that
// traffic on the path to the first push.
__device__ __forceinline__ void r4d_ar_seq_take(unsigned int* __restrict__ ctrs, unsigned int nbw,
unsigned int& s_seq, unsigned int& s_slot) {
unsigned int& s_seq, unsigned int& s_slot,
unsigned int steps = 1u) {
const unsigned int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x;
unsigned int* const par = ctrs + nbw;
const unsigned int pt = nt > 32u ? 32u : 0u; // first lane of the second wave, if there is one
if (tid == 0) s_seq = atomicAdd(&ctrs[b], 1u) + 1u;
if (tid == 0) s_seq = atomicAdd(&ctrs[b], steps) + 1u;
if (tid == pt) s_slot = (atomicAdd(&par[b], 1u) + 1u) & 1u;
for (unsigned int j = b + (tid + 1u) * nb; j < nbw; j += nt * nb) atomicAdd(&par[j], 1u);
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "workgroup", "local");
+36 -1
View File
@@ -12,7 +12,42 @@
#include <cstdlib>
#define R4D_AR8_SHARD_WEIGHTS_ENV "RADIANCE_AR8_SHARD_WEIGHTS"
enum { R4D_AR_SHARD_WEIGHT_MAX = 65535 };
#define R4D_AR8_PIECES_ENV "RADIANCE_AR8_PIECES"
// Below these message sizes (elements) the weights and the pieces do not apply: a decode-sized
// message pays more for the extra handshakes and the uneven split than its link time saves
// (measured on the eight R9700: 196608 elements run fastest even and in one piece, 524288 already
// gain from weights and two pieces).
#define R4D_AR8_SHARD_WEIGHTS_MIN_ENV "RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS"
#define R4D_AR8_PIECES_MIN_ENV "RADIANCE_AR8_PIECES_MIN_ELEMS"
enum {
R4D_AR_SHARD_WEIGHT_MAX = 65535,
R4D_AR8_MIN_ELEMS_DEFAULT = 262144,
R4D_AR8_MIN_ELEMS_MAX = 268435456, // the two-shot band's top (r4d.h)
};
// The piece count when RADIANCE_AR8_PIECES is unset, by message size in elements: more pieces
// shorten the one-way first and last steps ((K+1)/K of the link time) but add a handshake each,
// which only a large message amortises (measured at 4,1,0,4,0,1,4,4: K2 best at 524288, K4 at
// 2097152, K8 at 8388608).
inline int r4d_ar8_auto_pieces(long long numel) {
if (numel < 262144) return 1;
if (numel < 1048576) return 2;
if (numel < 4194304) return 4;
return 8;
}
// A non-negative decimal integer of at most `max`, nothing else.
inline bool r4d_ar_parse_count(const char* s, long long max, long long* out) {
if (!s || *s < '0' || *s > '9') return false;
long long v = 0;
while (*s >= '0' && *s <= '9') {
v = v * 10 + (*s++ - '0');
if (v > max) return false;
}
if (*s) return false;
*out = v;
return true;
}
// `ws` non-negative integers separated by commas, each at most R4D_AR_SHARD_WEIGHT_MAX, summing
// to more than zero; nothing else (no blanks, signs or trailing text).
+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);
}
+2 -1
View File
@@ -1,7 +1,8 @@
#pragma once
// r4d_ar_wide.h: shared transport of the wide (ws>2) all-reduce family. Every wide kernel
// (one-shot exact, two-shot exact, two-shot ti8) publishes in the 2*seq flag space (the two-shots
// add the 2s-1 half-step), advances seq_ctrs[b] once per AR, and lays scratch out as 2*ws slots of
// add the 2s-1 half-step), advances seq_ctrs[b] once per AR (a two-shot in pieces by its piece
// count, reserving the extra flag values it publishes), and lays scratch out as 2*ws slots of
// one stride (parity x source rank). The parity is the launch's, from the parity counters in
// r4d_ar_seq.h, so seq_ctrs is 2 * R4D_AR_WIDE_MAX_BLOCKS words. The constants sizing the shared
// flag/seq/scratch set are defined only here so they cannot drift apart.
+150 -25
View File
@@ -16179,6 +16179,7 @@ struct ArPattern {
const char* dtype = nullptr; /* "bf16" or "fp32" */
std::vector<int64_t> sizes;
int out_of_place = -1; /* index into sizes, or -1 */
std::vector<int> pieces; /* per size: a launch `pieces` parameter, 0 or absent none */
void* inst = nullptr;
int rc_init = RAD_E_STATE, rc_launch = RAD_OK;
std::vector<std::vector<float>> out;
@@ -16221,7 +16222,9 @@ static void ar_pattern_worker(ArPattern* t) {
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();
std::vector<RadParam> lp = ps;
if (k < t->pieces.size() && t->pieces[k] > 0) lp.push_back(PI("pieces", t->pieces[k]));
a.p = lp.data(); a.n_p = (int)lp.size();
a.instance = t->inst;
a.rank = t->rank;
a.world_size = t->world;
@@ -16257,7 +16260,10 @@ struct ScopedEnv {
ScopedEnv& operator=(const ScopedEnv&) = delete;
};
struct ArPatternRun { const char* dtype; std::vector<int64_t> sizes; int oop; };
struct ArPatternRun {
const char* dtype; std::vector<int64_t> sizes; int oop;
std::vector<int> pieces = {}; /* per size, as ArPattern::pieces */
};
/* One run on a fresh instance per rank, every size checked against the fold. False when init
* failed, so the caller stops instead of reporting every later run as failed too. */
@@ -16270,6 +16276,7 @@ static bool ar_pattern_check(const RadKernelInfo* row, const std::vector<int>& d
t[(size_t)r].world = world;
t[(size_t)r].dtype = run.dtype; t[(size_t)r].sizes = run.sizes;
t[(size_t)r].out_of_place = run.oop;
t[(size_t)r].pieces = run.pieces;
th.emplace_back(ar_pattern_worker, &t[(size_t)r]);
}
for (std::thread& x : th) x.join();
@@ -16307,6 +16314,8 @@ static bool ar_pattern_check(const RadKernelInfo* row, const std::vector<int>& d
" words, % " + std::to_string(world) + " = " +
std::to_string(words % world) +
((int)k == run.oop ? ", out of place" : ", in place") +
(k < run.pieces.size() && run.pieces[k] > 0
? ", pieces " + std::to_string(run.pieces[k]) : std::string()) +
") == 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));
@@ -16392,32 +16401,112 @@ static void ar_wide_pattern(const Plugin& pl, const std::vector<int>& devs, cons
}
}
/* The eight-rank two-shot with per-rank shard weights (RADIANCE_AR8_SHARD_WEIGHTS): shards of
* unequal length, empty ones (weight 0: the rank only pushes, gathers and handshakes), a single
* owner (its two regions outgrow a message, so init must grow the slot), and messages shorter than
* one word a rank, which the even split refuses. The fold is the same whoever owns a word, so the
* oracle is unchanged. */
/* The eight-rank two-shot with per-rank shard weights (RADIANCE_AR8_SHARD_WEIGHTS) and in
* pieces (RADIANCE_AR8_PIECES): shards of unequal length, empty ones (weight 0: the rank only
* pushes, gathers and handshakes), a single owner (its two regions outgrow a message, so init
* must grow the slot), and messages shorter than one word a rank, which the even split refuses --
* each at one, two and four pieces. Messages of a few words have fewer words a block than pieces,
* so the launcher's clamp is crossed too. A last run changes the piece count from launch to
* launch on one instance: a launch reserves one handshake step a piece, so a wrong reservation
* leaves a peer waiting on a flag value it already passed or running ahead into a slot still
* being read. These runs drop both size floors to 0 so small messages are weighted and pieced
* too; the last runs check the floors themselves. The fold is the same whoever owns a word, so
* the oracle is unchanged. */
static void ar_wide_weighted(const Plugin& pl, const std::vector<int>& devs, const char* name) {
constexpr int W = 8;
const RadKernelInfo* row = find(pl, name);
if (!row) { ok(name, false, "row absent"); return; }
if ((int)devs.size() < W) { skip(name, "needs 8 gfx1201 cards"); return; }
/* bf16: 72 elements are 9 words, 8 are one; 8256 is 1032 words, 4196352 is 524544. */
const std::vector<ArPatternRun> runs = {
/* bf16: 72 elements are 9 words, 8 are one; 8256 is 1032 words, 4196352 is 524544. Without
* weights the words must divide by eight. */
const std::vector<ArPatternRun> uneven = {
{ "bf16", { 4194304, 72, 8, 128, 8256, 4196352, 4194304 }, 1 },
{ "fp32", { 36, 1048580 }, 0 },
};
for (const char* w : { "0,3,1,0,5,1,2,4", "1,0,0,0,0,0,0,0", "4,1,0,4,0,1,4,4" }) {
ScopedEnv env(R4D_AR8_SHARD_WEIGHTS_ENV, w);
for (const ArPatternRun& run : runs)
if (!ar_pattern_check(row, devs, std::string(name) + " weights " + w, W, run)) return;
const std::vector<ArPatternRun> even = {
{ "bf16", { 4194304, 128, 8256, 4196352, 4194304 }, 1 },
{ "fp32", { 8256, 1048576 }, 0 },
};
{
ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, "0"), floor_k(R4D_AR8_PIECES_MIN_ENV, "0");
for (const char* k : { "1", "2", "4" }) {
ScopedEnv env_k(R4D_AR8_PIECES_ENV, k);
for (const char* w : { "", "0,3,1,0,5,1,2,4", "1,0,0,0,0,0,0,0", "4,1,0,4,0,1,4,4" }) {
ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, *w ? w : nullptr);
const std::string label =
std::string(name) + " pieces " + k +
(*w ? std::string(" weights ") + w : std::string(" even"));
for (const ArPatternRun& run : *w ? uneven : even)
if (!ar_pattern_check(row, devs, label, W, run)) return;
}
}
{
ScopedEnv env_k(R4D_AR8_PIECES_ENV, nullptr);
ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, "4,1,0,4,0,1,4,4");
const ArPatternRun mixed = {
"bf16", { 4194304, 4194304, 8256, 4196352, 72, 4194304, 4194304 }, 3,
{ 1, 4, 2, 8, 16, 3, 1 } };
ar_pattern_check(row, devs,
std::string(name) + " weights 4,1,0,4,0,1,4,4, pieces per launch", W,
mixed);
}
}
/* The floors at their defaults (R4D_AR8_MIN_ELEMS_DEFAULT elements) with weights, first at the
* size rule's piece counts (131072 runs one piece, 262144 two, 1048576 four, 4194304 eight),
* then at four pieces set. Which side of the weight floor a message fell on shows in what the
* entry accepts: 1048584 elements are 131073 words, over the floor and not divisible by eight,
* so only a weighted split serves it; 72 (9 words) is under it, so the even rule refuses it,
* unless the floor is lowered under it. The piece floor and rule change no bits and no rc;
* the sizes on both sides are checked for bits. */
ScopedEnv env_w(R4D_AR8_SHARD_WEIGHTS_ENV, "4,1,0,4,0,1,4,4");
ScopedEnv floor_k(R4D_AR8_PIECES_MIN_ENV, nullptr);
{
ScopedEnv env_k(R4D_AR8_PIECES_ENV, nullptr);
ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, nullptr);
const ArPatternRun rule = {
"bf16", { 131072, 262144, 1048576, 4194304, 8256, 4196352 }, 1 };
if (!ar_pattern_check(row, devs, std::string(name) + " weights 4,1,0,4,0,1,4,4, size rule",
W, rule))
return;
}
ScopedEnv env_k(R4D_AR8_PIECES_ENV, "4");
for (const char* fw : { (const char*)nullptr, "64" }) {
ScopedEnv floor_w(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, fw);
const std::string at = std::string(name) + " weights 4,1,0,4,0,1,4,4, pieces 4, floor " +
(fw ? fw : "default");
if (!fw) {
const ArPatternRun sides = { "bf16", { 1048584, 131072, 524288, 4194304, 8256 }, 0 };
if (!ar_pattern_check(row, devs, at, W, sides)) return;
}
std::vector<ArPattern> t((size_t)W);
std::vector<std::thread> th;
for (int r = 0; r < W; ++r) {
t[(size_t)r].row = row; t[(size_t)r].rank = r; t[(size_t)r].dev = devs[(size_t)r];
t[(size_t)r].world = W; t[(size_t)r].dtype = "bf16";
t[(size_t)r].sizes = { 524288, 72 }; t[(size_t)r].out_of_place = 1;
th.emplace_back(ar_pattern_worker, &t[(size_t)r]);
}
for (std::thread& x : th) x.join();
const int want = fw ? RAD_OK : RAD_E_SHAPE;
int as_wanted = 0;
for (const ArPattern& x : t) {
if (x.rc_init == RAD_OK && x.rc_launch == want) ++as_wanted;
if (x.inst) row->fini(x.inst);
}
ok((at + (fw ? ": a 9-word message (over the floor) is served"
: ": a 9-word message (under the floor) is refused, RAD_E_SHAPE")).c_str(),
as_wanted == W,
std::to_string(as_wanted) + " of 8 ranks as expected, rc " +
std::to_string(t[0].rc_launch));
}
}
/* The eight-rank exact all-reduce timed, one thread per card as the engine runs it: both rows
* (one-shot, two-shot) at every message size given, each at the block rule and at fixed block
* counts, out of place on the per-element pattern and checked bit for bit against the fp32
* ascending-rank fold on every rank.
* (one-shot; two-shot without a `pieces` parameter -- RADIANCE_AR8_PIECES or the size rule --
* and at 1, 2, 4 and 8 pieces, K1 the unpipelined kernel; under the piece floor every row runs
* K1, and the launcher clamps K to the words a block holds) at every message size given, each at
* the block rule and at fixed block counts, out of place on the per-element pattern and checked
* bit for bit against the fp32 ascending-rank fold on every rank.
*
* Warm, every rank launches `iters` times back to back after as many warm-up launches, and the
* period is the span over iters (the handshake keeps ranks in lockstep after the first). Cold
@@ -16447,6 +16536,7 @@ struct Ar8Perf {
const char* dtype = "bf16";
std::vector<int64_t> sizes;
std::vector<const RadKernelInfo*> rows;
std::vector<int> ks; /* per row: the `pieces` parameter, 0 or -1 none */
std::vector<int> nbs;
StBarrier* bar = nullptr;
int rc_init = RAD_OK;
@@ -16508,6 +16598,7 @@ static void ar8_perf_worker(Ar8Perf* t) {
for (int nb : t->nbs) {
std::vector<RadParam> ps = ip;
if (nb > 0) ps.push_back(PI("nblocks", nb));
if (t->ks[j] > 0) ps.push_back(PI("pieces", t->ks[j]));
RadTensor ts[2] = { T(in, dt, { n }), T(out, dt, { n }) };
RadArgs a;
std::memset(&a, 0, sizeof(a));
@@ -16610,10 +16701,16 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty
if ((int)devs.size() < W) { std::printf("--perfar8: needs 8 gfx1201 cards, found %zu\n",
devs.size()); return; }
std::vector<const RadKernelInfo*> rows;
std::vector<int> ks;
/* The two-shot once a piece count; its instances share one key, so they are one buffer set. */
for (const char* name : { "ar_oneshot_8rank_exact", "ar_twoshot_8rank_exact" }) {
const RadKernelInfo* r = find(pl, name);
if (!r) { std::printf("--perfar8: row %s absent\n", name); return; }
rows.push_back(r);
const bool two = !std::strcmp(name, "ar_twoshot_8rank_exact");
for (int k : two ? std::vector<int>{ -1, 1, 2, 4, 8 } : std::vector<int>{ 0 }) {
rows.push_back(r);
ks.push_back(k);
}
}
const std::vector<int> nbs = { 0, 4, 8, 16, 32, 64 };
std::printf("eight-rank exact all-reduce, %s, %d launches a point, %s\n", dtype, iters,
@@ -16631,13 +16728,33 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty
: wenv && *wenv ? fmt("even (%s=\"%s\" is invalid)", R4D_AR8_SHARD_WEIGHTS_ENV,
wenv).c_str()
: "even");
/* The floors as the plugin reads them; under them a message runs even and in one piece, the
* `pieces` parameter included. */
auto floor_of = [](const char* name) {
long long v = R4D_AR8_MIN_ELEMS_DEFAULT;
const char* e = std::getenv(name);
if (e && *e && !r4d_ar_parse_count(e, R4D_AR8_MIN_ELEMS_MAX, &v))
v = R4D_AR8_MIN_ELEMS_DEFAULT;
return (int64_t)v;
};
const int64_t wmin = floor_of(R4D_AR8_SHARD_WEIGHTS_MIN_ENV);
const int64_t kmin = floor_of(R4D_AR8_PIECES_MIN_ENV);
std::printf("two-shot floors: weights from %lld elements (%s), pieces from %lld (%s)\n",
(long long)wmin, R4D_AR8_SHARD_WEIGHTS_MIN_ENV, (long long)kmin,
R4D_AR8_PIECES_MIN_ENV);
long long kenv = 0;
const char* kenv_s = std::getenv(R4D_AR8_PIECES_ENV);
if (!kenv_s || !*kenv_s || !r4d_ar_parse_count(kenv_s, 16, &kenv) || kenv < 1) kenv = 0;
std::printf("two-shot pieces without a parameter (row \"two-shot set\"): %s\n",
kenv ? fmt("%lld (%s)", kenv, R4D_AR8_PIECES_ENV).c_str()
: "from the message size (1 / 2 / 4 / 8 from 0 / 262144 / 1048576 / 4194304)");
StBarrier bar(W);
std::vector<Ar8Perf> t((size_t)W);
std::vector<std::thread> th;
for (int r = 0; r < W; ++r) {
Ar8Perf& x = t[(size_t)r];
x.rank = r; x.dev = devs[(size_t)r]; x.iters = iters; x.dtype = dtype;
x.sizes = sizes; x.rows = rows; x.nbs = nbs; x.bar = &bar;
x.sizes = sizes; x.rows = rows; x.ks = ks; x.nbs = nbs; x.bar = &bar;
th.emplace_back(ar8_perf_worker, &x);
}
for (std::thread& x : th) x.join();
@@ -16658,25 +16775,33 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty
std::printf(" numel %lld (%.0f KiB, %lld 16-byte words)\n", (long long)n, bytes / 1024.0,
(long long)n16);
long off[W + 1];
const int64_t sh_max = r4d_ar_shard_table((long)n16, W, weighted ? wts : nullptr, off);
const bool wtd = weighted && n >= wmin;
const int64_t sh_max = r4d_ar_shard_table((long)n16, W, wtd ? wts : nullptr, off);
std::string shards;
for (int r = 0; r < W; ++r) shards += fmt(" %ld", off[r + 1] - off[r]);
std::printf(" two-shot shard words by rank:%s\n", shards.c_str());
for (size_t j = 0; j < rows.size(); ++j) {
const bool two = j == 1;
const bool two = ks[j] != 0;
/* The piece count the shim runs (before the launcher's words-a-block clamp). */
const int keff = n < kmin ? 1 : ks[j] > 0 ? ks[j]
: kenv ? (int)kenv : r4d_ar8_auto_pieces(n);
const std::string rname = !two ? std::string("one-shot")
: ks[j] < 0 ? fmt("two-shot set K%d", keff)
: keff != ks[j] ? fmt("two-shot K%d>%d", ks[j], keff)
: fmt("two-shot K%d", ks[j]);
const double sent = two ? 2.0 * (W - 1) / W * bytes : (double)(W - 1) * bytes;
for (int nb : nbs) {
const Ar8Perf& r0 = t[0];
/* The entry's nb_for, to name the rule's count: 1400 words a block, floor 4,
* ceiling 16, the two-shot over its longest shard with ceiling 8 at eight ranks. */
const int64_t basis = two ? (weighted ? sh_max : n16 / W) : n16;
const int64_t basis = two ? (wtd ? sh_max : n16 / W) : n16;
const int64_t rule =
std::min<int64_t>(std::max<int64_t>(basis / 1400, 4), two ? 8 : 16);
const std::string nbs_ =
nb ? std::to_string(nb) : fmt("rule %lld", (long long)rule);
if (r0.rc[cfg] != RAD_OK) {
std::printf(" %-9s nb %-7s rc=%d (not served)\n",
two ? "two-shot" : "one-shot", nbs_.c_str(), r0.rc[cfg]);
std::printf(" %-16s nb %-7s rc=%d (not served)\n", rname.c_str(),
nbs_.c_str(), r0.rc[cfg]);
++cfg;
continue;
}
@@ -16699,8 +16824,8 @@ static void ar8_perf(const Plugin& pl, int64_t numel, int iters, const char* dty
}
us = median(lo) * 1000.0; us_hi = median(hi) * 1000.0;
}
std::printf(" %-9s nb %-7s %8.1f us%s %6.2f GB/s payload %6.2f GB/s sent a "
"rank %s\n", two ? "two-shot" : "one-shot", nbs_.c_str(), us,
std::printf(" %-16s nb %-7s %8.1f us%s %6.2f GB/s payload %6.2f GB/s sent a "
"rank %s\n", rname.c_str(), nbs_.c_str(), us,
g_cold > 0 ? fmt(" (skew %.1f)", us_hi).c_str() : "",
us > 0 ? bytes / (us * 1e3) : 0.0, us > 0 ? sent / (us * 1e3) : 0.0,
bad ? fmt("%lld elements differ from the fold", (long long)bad).c_str()