libr4d: RADIANCE_AR8_SHARD_WEIGHTS=duplex -- shard weights from the both-directions link model, 8 to 1 instead of 4 to 1

The eight-rank two-shot in pieces runs both hops on a link at once, so each direction of rank r's
link carries (1 + 6 x_r) N for a shard fraction x_r: N - x_r N out in the reduce-scatter and
7 x_r N out in the all-gather, the same in. The slowest rank sets the time. The quadratic rule
(round(4 (rate / max)^2)) gives four Gen5 x16 and four Gen5 x8 cards 4 and 1: x8 carries 1.3 N at
28.6 GB/s (763 us for 16 MiB) while x16 carries 2.2 N at 57.1 (647 us), so the x16 links idle
15% of the time.

r4d_ar_weights_duplex equalises the ranks instead: x_r = (T B_r - 1) / 6 where positive, the x
summing to 1, filled from the fastest rank down. On that board it gives x16 2/9 and x8 1/36,
weights 64 and 8, and both kinds of card carry the same time's bytes: 7/6 N / 28.6 GB/s, 685 us
for 16 MiB, 10% under the quadratic rule's 763. On the earlier board with two Gen4 x8 links it
gives 64,9,0,64,0,9,64,64: the Gen4 cards nothing, as the measured best 4,1,0,4,0,1,4,4 did, the
Gen5 x8 ones half of what it gave them.

It is a new value of the variable, not a new default: unset and "auto" keep the quadratic rule,
so nothing runs differently until a measurement on the board says so. Which rank owns a word
changes only the link it crosses, not its fold, so the bits are those of every other table.
--perfar8 reads the variable the same way. r4d_ar_links_test checks both boards, equal links and
that every rank with a shard finishes together.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Fable 5.1 authored and StillDeadcode committed 2026-10-22 11:54:56 +02:00
1 parent 91200c900b
commit 5b9bc13e5f
5 files changed
+95 -16

No files matched your search

+3
View File
@@ -1169,6 +1169,9 @@ message takes 1360 µs.
The links are read once, at startup: a link that drops to a lower speed while the server runs
keeps its weight until the next start. `RADIANCE_AR8_SHARD_WEIGHTS` overrides them: `off` keeps
the shards even, eight comma-separated integers set them, unset or `auto` reads the links.
`duplex` reads the same links and weights them so that every link's busier direction carries the
same time's bytes when both directions run at once (`r4d_ar_weights_duplex`): x16 64 and x8 8 on
the repaired board, whose link time it puts 10% under the `auto` weights', a Gen4 x8 card nothing.
`RADIANCE_AR8_PIECES` (1 to 16) fixes the piece count, and `RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS`
and `RADIANCE_AR8_PIECES_MIN_ELEMS` move the two sizes they start at. Every rank must read the
same values; where they differ, every rank stops at startup.
+16 -9
View File
@@ -151,18 +151,21 @@ long long pgeti_hi(const RadParam* p, int n_p, const char* k, long long dflt) {
long align16(long v) { return (v + 15) & ~15L; }
/* RADIANCE_AR8_SHARD_WEIGHTS, read at every eight-rank build so a process can change it between
* instances: unset or "auto" derives the weights from the links, "off" keeps the shards even,
* eight integers set them. Anything else is "off", with one warning. */
enum { AR8_EVEN = 0, AR8_SET = 1, AR8_AUTO = 2 };
* instances: unset or "auto" derives the weights from the links by r4d_ar_weights_from_rates,
* "duplex" from the same links by r4d_ar_weights_duplex, "off" keeps the shards even, eight
* integers set them. Anything else is "off", with one warning. */
enum { AR8_EVEN = 0, AR8_SET = 1, AR8_AUTO = 2, AR8_DUPLEX = 3 };
int ar8_weights_mode(int* w) {
const char* e = std::getenv(R4D_AR8_SHARD_WEIGHTS_ENV);
if (!e || !*e || !std::strcmp(e, "auto")) return AR8_AUTO;
if (!std::strcmp(e, "duplex")) return AR8_DUPLEX;
if (!std::strcmp(e, "off")) return AR8_EVEN;
if (r4d_ar_shard_weights_parse(e, 8, w)) return AR8_SET;
static std::once_flag said;
std::call_once(said, [e] {
std::fprintf(stderr, "[r4d] %s=\"%s\" ignored (needs auto, off or eight non-negative "
"integers, comma-separated, sum > 0); eight-rank two-shot shards stay even\n",
"integers, comma-separated, sum > 0, or duplex); eight-rank two-shot shards "
"stay even\n",
R4D_AR8_SHARD_WEIGHTS_ENV, e);
});
return AR8_EVEN;
@@ -237,7 +240,8 @@ int ar8_settle(ArInst* in, int rank, int world) {
me.pieces = ar8_pieces();
me.weights_min = ar8_min_elems(R4D_AR8_SHARD_WEIGHTS_MIN_ENV, &said_w);
me.pieces_min = ar8_min_elems(R4D_AR8_PIECES_MIN_ENV, &said_p);
if (me.mode == AR8_AUTO && !ar8_own_link(&me.link)) me.link = R4DArLink();
if ((me.mode == AR8_AUTO || me.mode == AR8_DUPLEX) && !ar8_own_link(&me.link))
me.link = R4DArLink();
{
std::lock_guard<std::mutex> lk(g_rv.m);
g_rv.ar8[rank] = me;
@@ -267,10 +271,12 @@ int ar8_settle(ArInst* in, int rank, int world) {
"%d,%d,%d,%d,%d,%d,%d,%d (%s)\n", w[0], w[1], w[2], w[3], w[4], w[5],
w[6], w[7], R4D_AR8_SHARD_WEIGHTS_ENV);
});
} else if (me.mode == AR8_AUTO) {
} else if (me.mode == AR8_AUTO || me.mode == AR8_DUPLEX) {
double gbs[R4D_MAX_RANKS] = { 0 };
for (int r = 0; r < world; ++r) gbs[r] = r4d_ar_link_gbs(all[r].link);
if (!r4d_ar_weights_from_rates(gbs, world, w)) {
const bool known = me.mode == AR8_DUPLEX ? r4d_ar_weights_duplex(gbs, world, w)
: r4d_ar_weights_from_rates(gbs, world, w);
if (!known) {
std::call_once(said_unknown, [all, world] {
std::string who;
for (int r = 0; r < world; ++r)
@@ -280,7 +286,8 @@ int ar8_settle(ArInst* in, int rank, int world) {
});
return RAD_OK;
}
std::call_once(said_links, [all, world, &w] {
const bool duplex = me.mode == AR8_DUPLEX;
std::call_once(said_links, [all, world, duplex, &w] {
std::string l;
for (int r = 0; r < world; ++r) {
char one[96];
@@ -290,7 +297,7 @@ int ar8_settle(ArInst* in, int rank, int world) {
l += one;
}
std::fprintf(stderr, "[r4d] eight-rank two-shot shard weights from the slowest PCIe "
"hop of each card:%s\n", l.c_str());
"hop of each card%s:%s\n", duplex ? " (duplex)" : "", l.c_str());
});
bool same = true;
for (int r = 1; r < world; ++r) same = same && w[r] == w[0];
+41
View File
@@ -83,3 +83,44 @@ inline bool r4d_ar_weights_from_rates(const double* gbs, int ws, int* w) {
}
return true;
}
// RADIANCE_AR8_SHARD_WEIGHTS=duplex: the weights that minimise the two-shot's link time when both
// directions of a link run at once (the pieced kernel's aim). Rank r with shard fraction x_r sends
// (1 - x_r) of the message in the first hop and (ws - 1) x_r in the second, and receives the same,
// so each direction carries (1 + (ws - 2) x_r) N at rate B_r. The slowest rank sets the time T, so
// the best table makes every rank with a shard finish together: x_r = (T B_r - 1) / (ws - 2) where
// that is positive, 0 elsewhere, with the x summing to 1 (water filling from the fastest rank
// down). Four ranks at B and four at B / 2 give x of 2/9 and 1/36, 8 to 1 where the quadratic rule
// above gives 4 to 1, and a time 10% lower (7/6 N / B / 2 against 1.3 N / B / 2). Scaled so the
// largest weight is 64. False when a rank's rate is unknown.
inline bool r4d_ar_weights_duplex(const double* gbs, int ws, int* w) {
if (ws < 3 || ws > 16) return false;
int order[16];
for (int r = 0; r < ws; ++r) {
if (!(gbs[r] > 0.0)) return false;
order[r] = r;
}
for (int i = 1; i < ws; ++i)
for (int j = i; j > 0 && gbs[order[j]] > gbs[order[j - 1]]; --j) {
const int t = order[j]; order[j] = order[j - 1]; order[j - 1] = t;
}
// With the k fastest ranks holding shards, sum (T B_r - 1) = ws - 2 gives T; the set is right
// when the slowest of them still gets a positive share and the next one would not.
double sum = 0.0, t = 0.0;
int k = 0;
for (int n = 1; n <= ws; ++n) {
sum += gbs[order[n - 1]];
const double tn = (double)(ws - 2 + n) / sum;
if (tn * gbs[order[n - 1]] <= 1.0) break;
k = n;
t = tn;
}
double x[16] = { 0 }, top = 0.0;
for (int i = 0; i < k; ++i) {
const int r = order[i];
x[r] = (t * gbs[r] - 1.0) / (double)(ws - 2);
if (x[r] > top) top = x[r];
}
for (int r = 0; r < ws; ++r) w[r] = (int)std::lround(64.0 * x[r] / top);
return true;
}
+5 -4
View File
@@ -22581,7 +22581,8 @@ static void ar8_print_timeline(const std::vector<Ar8Perf>& t, size_t cfg, double
* `how` names the source and, for "auto", every rank's link. */
static bool ar8_weights_as_plugin(const std::vector<int>& devs, int* w, std::string* how) {
const char* e = std::getenv(R4D_AR8_SHARD_WEIGHTS_ENV);
if (e && *e && std::strcmp(e, "auto")) {
const bool duplex = e && !std::strcmp(e, "duplex");
if (e && *e && std::strcmp(e, "auto") && !duplex) {
if (!std::strcmp(e, "off")) { *how = " (off)"; return false; }
if (r4d_ar_shard_weights_parse(e, 8, w)) { *how = " (set)"; return true; }
*how = fmt(" (%s=\"%s\" is invalid)", R4D_AR8_SHARD_WEIGHTS_ENV, e);
@@ -22599,11 +22600,11 @@ static bool ar8_weights_as_plugin(const std::vector<int>& devs, int* w, std::str
gbs[r] = r4d_ar_link_gbs(l);
links += fmt("%s r%d %.1f GT/s x%d", r ? "," : "", r, l.gts, l.width);
}
if (!r4d_ar_weights_from_rates(gbs, 8, w)) {
*how = " (auto: a link is unreadable;" + links + ")";
if (!(duplex ? r4d_ar_weights_duplex(gbs, 8, w) : r4d_ar_weights_from_rates(gbs, 8, w))) {
*how = fmt(" (%s: a link is unreadable;", duplex ? "duplex" : "auto") + links + ")";
return false;
}
*how = " (auto:" + links + ")";
*how = fmt(" (%s:", duplex ? "duplex" : "auto") + links + ")";
for (int r = 1; r < 8; ++r) if (w[r] != w[0]) return true;
return false;
}
+30 -3
View File
@@ -77,8 +77,8 @@ std::string card(const Root& r, int i, const std::string& speed, int width) {
struct Slot { const char* speed; int width; };
/* The weights the eight cards give, or "unknown". */
std::string weights(const Root& r, const Slot* slots) {
/* The weights the eight cards give, or "unknown", by the quadratic rule or the duplex model. */
std::string weights(const Root& r, const Slot* slots, bool duplex = false) {
std::string bdf[8];
for (int i = 0; i < 8; ++i) bdf[i] = card(r, i, slots[i].speed, slots[i].width);
double gbs[8] = { 0 };
@@ -87,7 +87,8 @@ std::string weights(const Root& r, const Slot* slots) {
if (r4d_ar_link_bottleneck(r.dir, bdf[i], &l)) gbs[i] = r4d_ar_link_gbs(l);
}
int w[8];
if (!r4d_ar_weights_from_rates(gbs, 8, w)) return "unknown";
if (!(duplex ? r4d_ar_weights_duplex(gbs, 8, w) : r4d_ar_weights_from_rates(gbs, 8, w)))
return "unknown";
std::string s;
for (int i = 0; i < 8; ++i) s += (i ? "," : "") + std::to_string(w[i]);
return s;
@@ -123,6 +124,32 @@ TEST(repaired_links_give_four_and_one) {
CHECK_EQ(weights(r, s), std::string("4,1,4,4,1,1,4,4"));
}
/* The duplex model on the board after the repair: x16 holds 2/9 of the message and x8 1/36, so
* each direction of every link carries the same time's bytes; on the earlier board the Gen4 x8
* cards hold nothing (they would need T B > N) and Gen5 x8 one seventh of x16. */
TEST(the_duplex_model_balances_both_directions_of_every_link) {
Root r;
const Slot fixed[8] = { { G5, 16 }, { G5, 8 }, { G5, 8 }, { G5, 16 },
{ G5, 8 }, { G5, 8 }, { G5, 16 }, { G5, 16 } };
CHECK_EQ(weights(r, fixed, true), std::string("64,8,8,64,8,8,64,64"));
Root r2;
const Slot older[8] = { { G5, 16 }, { G5, 8 }, { G4, 8 }, { G5, 16 },
{ G4, 8 }, { G5, 8 }, { G5, 16 }, { G5, 16 } };
CHECK_EQ(weights(r2, older, true), std::string("64,9,0,64,0,9,64,64"));
Root r3;
Slot even[8];
for (Slot& x : even) x = { G5, 16 };
CHECK_EQ(weights(r3, even, true), std::string("64,64,64,64,64,64,64,64"));
/* The time each rank's bytes take, (1 + 6 x) / B, is the same for every rank with a shard. */
const double gbs[8] = { 63, 31.5, 31.5, 63, 31.5, 31.5, 63, 63 };
int w[8];
REQUIRE(r4d_ar_weights_duplex(gbs, 8, w));
double sum = 0;
for (int i = 0; i < 8; ++i) sum += w[i];
const double tf = (1 + 6 * w[0] / sum) / gbs[0], ts = (1 + 6 * w[1] / sum) / gbs[1];
CHECK(std::fabs(tf - ts) / tf < 1e-9);
}
TEST(equal_links_give_equal_weights) {
Root r;
Slot s[8];