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

+148 -1
View File
@@ -10684,6 +10684,148 @@ static void ar_three_rank(const Plugin& pl, const std::vector<int>& devs, const
}
}
/* THE THREE-RANK ALL-REDUCE ROWS, at the sizes the two-shot is selected for and the ones that do
* not divide.
*
* ar_three_rank's input is a constant per rank, which a shard landing at the wrong offset still
* sums correctly. This one gives every element its own value, so a word reduced by the wrong
* owner, gathered from the wrong slot or dropped at a short shard's end is a wrong number at a
* known index. The sizes walk the uneven split: words % 3 of 0, 1 and 2, from a 16-word message
* to the row's 20971520-element ceiling, in place (how every block issues it) and once out of
* place, back to back on ONE instance so the slot parity and the changing block count are
* exercised the way a step's mixed messages exercise them. The oracle is the fold the kernel
* claims -- fp32 in ascending rank order, rounded once -- so it is bit for bit, and so it is also
* the one-shot's answer: the band edge between the two rows changes no bits. */
struct ArPattern {
const RadKernelInfo* row;
int rank, dev;
const char* dtype; /* "bf16" or "fp32" */
std::vector<int64_t> sizes;
int out_of_place = -1; /* index into sizes, or -1 */
void* inst = nullptr;
int rc_init = RAD_E_STATE, rc_launch = RAD_OK;
std::vector<std::vector<float>> out;
};
static float ar_pattern_value(int rank, int64_t i) {
/* Integers up to 254 over 64: exact in bf16 (8 significant bits), and three of them need ten,
* so the bf16 sum is rounded and the rounding is checked too. */
return (float)((int)((i * 37 + (int64_t)rank * 101) % 509) - 254) / 64.0f;
}
static void ar_pattern_worker(ArPattern* t) {
if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; }
const bool bf = !std::strcmp(t->dtype, "bf16");
const int esz = bf ? 2 : 4;
int64_t most = 0;
for (int64_t n : t->sizes) most = n > most ? n : most;
std::vector<RadParam> ps = {
PI("world_size", 3), PI("numel", most), PS("dtype", t->dtype), PI("exact", 1),
};
t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 3, &t->inst);
if (t->rc_init != RAD_OK) return;
void* in = nullptr;
void* out = nullptr;
if (hipMalloc(&in, (size_t)most * esz) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
if (hipMalloc(&out, (size_t)most * esz) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; }
std::vector<uint8_t> h((size_t)most * esz);
for (size_t k = 0; k < t->sizes.size(); ++k) {
const int64_t n = t->sizes[k];
for (int64_t i = 0; i < n; ++i) {
const float v = ar_pattern_value(t->rank, i);
if (bf) { const uint16_t b = h_to_bf16(v); std::memcpy(&h[(size_t)i * 2], &b, 2); }
else std::memcpy(&h[(size_t)i * 4], &v, 4);
}
st_memcpy(in, h.data(), (size_t)n * esz, hipMemcpyHostToDevice);
const bool oop = (int)k == t->out_of_place;
if (oop) st_memset(out, 0, (size_t)n * esz);
const uint32_t dt = bf ? RAD_BF16 : RAD_F32;
RadTensor ts[2] = { T(in, dt, { n }), T(out, dt, { n }) };
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();
a.instance = t->inst;
a.rank = t->rank;
a.world_size = 3;
const int rc = t->row->launch(&a, nullptr);
st_stream_sync(nullptr);
if (rc != RAD_OK && t->rc_launch == RAD_OK) t->rc_launch = rc;
st_memcpy(h.data(), oop ? out : in, (size_t)n * esz, hipMemcpyDeviceToHost);
std::vector<float> got((size_t)n);
for (int64_t i = 0; i < n; ++i) {
if (bf) { uint16_t b; std::memcpy(&b, &h[(size_t)i * 2], 2); got[(size_t)i] = h_bf16(b); }
else std::memcpy(&got[(size_t)i], &h[(size_t)i * 4], 4);
}
t->out.push_back(std::move(got));
}
st_free(in);
st_free(out);
}
static void ar_three_rank_pattern(const Plugin& pl, const std::vector<int>& devs, const char* name) {
const RadKernelInfo* row = find(pl, name);
if (!row) { ok(name, false, "row absent"); return; }
if (devs.size() < 3) { skip(name, "fewer than three gfx1201 cards"); return; }
struct Run { const char* dtype; std::vector<int64_t> sizes; int oop; };
const std::vector<Run> runs = {
/* bf16, the residual: words = numel / 8. 2048 x 2048 rows is words % 3 == 2, x 2049 is
* 0, x 2050 is 1; 128 is 16 words (1), 24008 is 3001 (1); 20971520 the ceiling (1). */
{ "bf16", { 4194304, 128, 4196352, 24008, 4198400, 20971520, 4194304 }, 2 },
/* fp32: words = numel / 4. 8196 -> 2049 (0), 4100 -> 1025 (2), 1048580 -> 262145 (2). */
{ "fp32", { 8196, 4100, 1048580 }, 0 },
};
for (const Run& run : runs) {
ArPattern t[3];
for (int r = 0; r < 3; ++r) {
t[r].row = row; t[r].rank = r; t[r].dev = devs[(size_t)r];
t[r].dtype = run.dtype; t[r].sizes = run.sizes; t[r].out_of_place = run.oop;
}
std::thread a(ar_pattern_worker, &t[0]), b(ar_pattern_worker, &t[1]), c(ar_pattern_worker, &t[2]);
a.join(); b.join(); c.join();
const std::string what = std::string(name) + " " + run.dtype;
const bool up = t[0].rc_init == RAD_OK && t[1].rc_init == RAD_OK && t[2].rc_init == RAD_OK;
ok((what + " init (peer rendezvous)").c_str(), up,
"rc " + std::to_string(t[0].rc_init) + "/" + std::to_string(t[1].rc_init) + "/" +
std::to_string(t[2].rc_init));
if (!up) return;
const bool ran = t[0].rc_launch == RAD_OK && t[1].rc_launch == RAD_OK &&
t[2].rc_launch == RAD_OK;
ok((what + " launches").c_str(), ran,
"rc " + std::to_string(t[0].rc_launch) + "/" + std::to_string(t[1].rc_launch) + "/" +
std::to_string(t[2].rc_launch));
if (ran) {
const bool bf = !std::strcmp(run.dtype, "bf16");
for (size_t k = 0; k < run.sizes.size(); ++k) {
const int64_t n = run.sizes[k];
int64_t bad = 0, first = -1;
for (int64_t i = 0; i < n; ++i) {
float acc = 0.0f;
for (int r = 0; r < 3; ++r) {
const float v = ar_pattern_value(r, i);
acc += bf ? h_bf16(h_to_bf16(v)) : v;
}
const float want = bf ? h_bf16(h_to_bf16(acc)) : acc;
for (int r = 0; r < 3; ++r)
if (t[r].out.size() <= k || t[r].out[k][(size_t)i] != want) {
if (first < 0) first = i;
++bad;
}
}
const int64_t words = n * (bf ? 2 : 4) / 16;
const std::string label =
what + " numel " + std::to_string(n) + " (" + std::to_string(words) +
" words, % 3 = " + std::to_string(words % 3) +
((int)k == run.oop ? ", out of place" : ", in place") +
") == 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));
}
}
if (row->fini) for (int r = 0; r < 3; ++r) if (t[r].inst) row->fini(t[r].inst);
}
}
static void gather_three_rank(const Plugin& pl, const std::vector<int>& devs, const char* name) {
const RadKernelInfo* row = find(pl, name);
if (!row) { ok(name, false, "row absent"); return; }
@@ -12518,7 +12660,7 @@ static void device_phase(const Plugin& pl) {
/* Every collective has a host twin (r4d_rows.cpp): the same kernel with its mailbox in pinned
* host memory. A case is the pair's, so the twin is held to exactly the original's checks. */
for (const char* base : { "ar_oneshot_2rank_exact", "ar_gather_2rank", "ar_oneshot_3rank_exact",
"ar_gather_3rank" }) {
"ar_twoshot_3rank_exact", "ar_gather_3rank" }) {
for (const std::string& n : { std::string(base), std::string(base) + "_host" }) {
if (!sel(n.c_str())) continue;
const bool three = std::strstr(base, "3rank") != nullptr;
@@ -12534,8 +12676,13 @@ static void device_phase(const Plugin& pl) {
* ar_oneshot_3rank_exact` reproduces one of them alone. */
} else if (three && std::strstr(base, "gather")) {
gather_three_rank(pl, devs, n.c_str());
} else if (three && std::strstr(base, "twoshot")) {
ar_three_rank_pattern(pl, devs, n.c_str());
} else {
ar_three_rank(pl, devs, n.c_str());
/* The one-shot through the same per-element oracle: both rows of the op agree
* with it, so they agree with each other at every size either could be given. */
ar_three_rank_pattern(pl, devs, n.c_str());
}
}
}