diff --git a/docs/OPS.md b/docs/OPS.md index 98ca3581..a23f18b4 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -738,6 +738,13 @@ rows start above 524288 elements, which makes that a band edge of the ranged `nu states nothing for it. `hops` is a caller's request for one or the other (1 or 2); libref accepts and ignores it, and no libr4d row reads it. Every all-reduce kernel serves `y` absent, in place. +libr4d serves these at **2, 3, 4 and 8 ranks**. Two ranks have their own kernels (one flag word a +block, the rotated 6-bit wire, the fused norm-and-reduce forms). The wide family is one template on +the world size; 4 and 8 have both hops, 3 has the one-shot only -- the two-shot shards the message +`world` ways and a residual width such as 2560 does not divide by 3 -- and `all_gather` at width +rides the same instance as the wide `all_reduce` (`ar_gather_3rank`, `_4rank`, `_8rank`). The fused forms are declared +only at two ranks by the arch blocks, which fall back to the plain pair at any other width. + `min_bytes` is the byte floor under which a LOSSY row must serve the message exactly anyway, and it is a parameter rather than a constant in the library because it is a property of the link and not of the kernel. Compression only pays once the transfer is bandwidth-bound: a decode step diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 404a4a0e..c9b6da03 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -326,12 +326,29 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo // words parity x source rank, this rank's own slot unused); flags are max_blocks*ws words indexed // [block][source rank]; seq_ctrs is 2 * max_blocks words, a handshake and a slot-parity counter // per block of capacity (r4d_ar_seq.h). +// Three ranks: the same kernel instantiated at WS=3 (two peers). Nothing in the wide family is +// power-of-two; the two-shot cannot serve 3 because it shards the message `world` ways. +void r4d_ar_oneshot_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, long rank, + long stream, long nblocks, long nthreads, long drain, long acq, + long pub); void r4d_ar_oneshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2, long peer_flags0, long peer_flags1, long peer_flags2, 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); +// ---- all-gather at width (r4d_ar_gather_Nrank.hip) ------------------------------------------ +// The 2-rank gather's placement on the wide family's transport: `out` is world * n_elem elements, +// rank r's slice at r * n_elem, or row-interleaved [rows][world][row] when row_elems > 0. Shares +// the wide all-reduce's instance (scratch, [block][source] flags, seq counters). +void r4d_ar_gather_3rank(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, + long rank, long row_elems, long stream, long nblocks, long nthreads, + long drain, long acq, long pub); void r4d_ar_oneshot_8rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2, long peer_scratch3, long peer_scratch4, long peer_scratch5, long peer_scratch6, long peer_flags0, long peer_flags1, diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index dfe374d8..21c64f63 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -551,7 +551,7 @@ extern "C" int r4d_rad_ar_gather_2rank(const RadArgs* a, RadStream s) { return RAD_OK; } -/* ------------------------------------------------------------------ wide (4 / 8 rank) all-gather +/* ------------------------------------------------------------------ wide (3 / 4 / 8 rank) all-gather * The gather the 2-rank one cannot be at width: its handshake is one peer flag per block, so the * wide form takes the wide family's peer arrays and r4d_ar_handshake instead (r4d_ar_wide.h), and * it rides the SAME instance, scratch, flags and counters as the wide one-shots above -- a slot @@ -623,6 +623,21 @@ static int ar_gather_wide_setup(const RadArgs* a, int ws, const ArInst** out_in, return RAD_OK; } +extern "C" int r4d_rad_ar_gather_3rank(const RadArgs* a, RadStream s) { + const ArInst* in = nullptr; + ArCall c; + long long row = 0; + int rc = ar_gather_wide_setup(a, 3, &in, &c, &row); + if (rc != RAD_OK) return rc; + R4D_TRY("ar_gather_3rank", + r4d_ar_gather_3rank(in->peer_scratch[0], in->peer_scratch[1], + in->peer_flags[0], in->peer_flags[1], + in->my_scratch, in->my_flags, in->my_seq, in->slot16, + c.inp, c.out, c.numel, in->dtype, in->rank, (long)row, + (long)(uintptr_t)s, c.nblocks, c.nthreads, c.drain, c.acq, c.pub)); + return RAD_OK; +} + extern "C" int r4d_rad_ar_gather_4rank(const RadArgs* a, RadStream s) { const ArInst* in = nullptr; ArCall c; @@ -737,7 +752,7 @@ extern "C" int r4d_rad_ar_oneshot_2rank_wht6(const RadArgs* a, RadStream s) { return RAD_OK; } -/* ------------------------------------------------------------------ wide (4 / 8 rank) +/* ------------------------------------------------------------------ wide (3 / 4 / 8 rank) * * The peer arguments are ascending global rank with the current rank removed, which is exactly * the order build() filled peer_scratch/peer_flags in. */ @@ -745,6 +760,22 @@ extern "C" int r4d_rad_ar_oneshot_2rank_wht6(const RadArgs* a, RadStream s) { /* The one-shots run in place: the thread that stores word k of the output is the one that read * word k of the input, for its push and again as its own term of the fold, and peers read only * their own scratch (r4d_ar_oneshot_Nrank_exact.hip). */ +extern "C" int r4d_rad_ar_oneshot_3rank_exact(const RadArgs* a, RadStream s) { + ArCall c; + int rc = ar_setup(a, &c, 0); + if (rc != RAD_OK) return rc; + if (c.in->world != 3) return RAD_E_SHAPE; + const ArInst* in = c.in; + R4D_TRY("ar_oneshot_3rank_exact", + r4d_ar_oneshot_3rank_exact(in->peer_scratch[0], in->peer_scratch[1], + in->peer_flags[0], in->peer_flags[1], + 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)); + return RAD_OK; +} + extern "C" int r4d_rad_ar_oneshot_4rank_exact(const RadArgs* a, RadStream s) { ArCall c; int rc = ar_setup(a, &c, 0); diff --git a/libr4d/r4d_ar_gather_Nrank.hip b/libr4d/r4d_ar_gather_Nrank.hip index d89c504e..3a521e5d 100644 --- a/libr4d/r4d_ar_gather_Nrank.hip +++ b/libr4d/r4d_ar_gather_Nrank.hip @@ -1,4 +1,4 @@ -// r4d_ar_gather_Nrank.hip: r4d_ar_gather_4rank, r4d_ar_gather_8rank -- one-shot P2P all-GATHER, +// r4d_ar_gather_Nrank.hip: r4d_ar_gather_3rank, r4d_ar_gather_4rank, r4d_ar_gather_8rank -- one-shot P2P all-GATHER, // 4 or 8 ranks, gfx1201. WS is compile-time; the 2-rank gather keeps its own file, exactly as the // one-shot all-reduce's does. // @@ -34,6 +34,12 @@ // "C" here, at arm's length from the definitions below, so those definitions get C linkage; the // caller is r4d_ar_entry.hip, which owns the instance and repeats the two prototypes at its call // site (r4d.h declares the rest of the family and is not this unit's file). +extern "C" void r4d_ar_gather_3rank(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, long rank, long row_elems, long stream, + long nblocks, long nthreads, long drain, long acq, long pub); extern "C" void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2, long peer_flags0, long peer_flags1, long peer_flags2, long my_scratch, long my_flags, long seq_ctrs, @@ -215,6 +221,19 @@ static void ar_gather_wide_launch(const long* peer_scratch, const long* peer_fla (int)pub); } +/* Three ranks: the same launch with two peers. The vocab-parallel sampler's gather at a world of + * three (Qwen3.8-Flash-Next padded to 36 heads / 3 KV heads, docs/TP3.md) resolves here. */ +void r4d_ar_gather_3rank(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, long rank, long row_elems, long stream, + long nblocks, long nthreads, long drain, long acq, long pub) { + const long ps[2] = {peer_scratch0, peer_scratch1}; + const long pf[2] = {peer_flags0, peer_flags1}; + ar_gather_wide_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem, + dtype, rank, row_elems, stream, nblocks, nthreads, drain, acq, pub); +} + void r4d_ar_gather_4rank(long peer_scratch0, long peer_scratch1, long peer_scratch2, long peer_flags0, long peer_flags1, long peer_flags2, long my_scratch, long my_flags, long seq_ctrs, long slot_stride16, long inp, long out, diff --git a/libr4d/r4d_ar_oneshot_Nrank_exact.hip b/libr4d/r4d_ar_oneshot_Nrank_exact.hip index 944f90f9..c3a06d78 100644 --- a/libr4d/r4d_ar_oneshot_Nrank_exact.hip +++ b/libr4d/r4d_ar_oneshot_Nrank_exact.hip @@ -1,5 +1,5 @@ -// r4d_ar_oneshot_Nrank_exact.hip: r4d_ar_oneshot_4rank_exact, -// r4d_ar_oneshot_8rank_exact -- one-shot P2P push all-reduce, 4 or 8 +// r4d_ar_oneshot_Nrank_exact.hip: r4d_ar_oneshot_3rank_exact, r4d_ar_oneshot_4rank_exact, +// r4d_ar_oneshot_8rank_exact -- one-shot P2P push all-reduce, 3, 4 or 8 // ranks, gfx1201. WS is compile-time; the 2-rank kernel keeps its own file. // // Per-block handshake: block b waits only on the peers' block b, so push @@ -344,6 +344,23 @@ static void ar_wide_launch(const long* peer_scratch, const long* peer_flags, lon #undef R4D_AR_WIDE_LAUNCH } +// Three ranks: the same kernel at WS=3. Nothing in the push, the handshake or the ascending-rank +// reduce assumes a power of two -- the peer bundle is WS-1 wide and the slot/flag layout is +// parity x source rank -- so this is an instantiation, not a variant. It exists because a 3-card +// box is a real configuration (three R9700s) and the two-shot cannot serve it: that one shards the +// message `world` ways and a residual width of 2560 does not divide by 3. +void r4d_ar_oneshot_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, long rank, + long stream, long nblocks, long nthreads, long drain, long acq, + long pub) { + const long ps[2] = {peer_scratch0, peer_scratch1}; + const long pf[2] = {peer_flags0, peer_flags1}; + ar_wide_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride16, inp, out, n_elem, + dtype, rank, stream, nblocks, nthreads, drain, acq, pub); +} + void r4d_ar_oneshot_4rank_exact(long peer_scratch0, long peer_scratch1, long peer_scratch2, long peer_flags0, long peer_flags1, long peer_flags2, long my_scratch, long my_flags, long seq_ctrs, long slot_stride16, diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 5ac110e2..0c66deb5 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -173,6 +173,8 @@ int r4d_rad_ar_gather_2rank (const RadArgs*, RadStream); int r4d_rad_ar_gather_4rank (const RadArgs*, RadStream); int r4d_rad_ar_gather_8rank (const RadArgs*, RadStream); int r4d_rad_ar_oneshot_2rank_wht6 (const RadArgs*, RadStream); +int r4d_rad_ar_oneshot_3rank_exact(const RadArgs*, RadStream); +int r4d_rad_ar_gather_3rank (const RadArgs*, RadStream); int r4d_rad_ar_oneshot_4rank_exact(const RadArgs*, RadStream); int r4d_rad_ar_oneshot_8rank_exact(const RadArgs*, RadStream); int r4d_rad_ar_twoshot_4rank_exact(const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 6beac273..0a94898c 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2432,6 +2432,14 @@ static const RadConstraint cArExact[] = { C_EQ("world_size", 2), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), C_P2P, }; +static const RadConstraint cArGather3[] = { + C_EQ("world_size", 3), C_IN("dtype", "bf16 fp16 f16 fp32 f32"), + C_P2P, +}; +static const RadConstraint cArExact3[] = { + C_EQ("world_size", 3), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), + C_P2P, +}; static const RadConstraint cArExact4[] = { C_EQ("world_size", 4), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"), C_P2P, @@ -4010,6 +4018,26 @@ static const RadKernelInfo kKernels[] = { r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_oneshot_2rank_wht6, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, +{ "ar_gather_3rank", "all_gather", "ar", + "all-gather, one-shot push over P2P IPC scratch, the wide family's per-block handshake", + "exactly 3 ranks, all peer-accessible; y is [3 * numel] with rank r's input at r * numel, or " + "row-interleaved [rows][3][row] when `row` is given; shares the wide family's " + "flag/seq/scratch set; cudagraph-safe", + "any payload -- the bytes are moved, never summed, so the (u32 id, f32 logit) pairs the " + "vocab-parallel sampler gathers survive intact", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT, + ROW(cArGather3), ROW(tAr), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_gather_3rank, nullptr, nullptr, nullptr, + r4d_shape_all_gather }, +{ "ar_oneshot_3rank_exact", "all_reduce", "ar", + "all-reduce (sum), one-shot push over P2P IPC scratch, per-block handshake", + "exactly 3 ranks, all peer-accessible; one scratch slot per (parity, source rank); " + "the only wide form at three -- the two-shot shards the message `world` ways; cudagraph-safe", + "bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT, + ROW(cArExact3), ROW(tAr), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_oneshot_3rank_exact, nullptr, nullptr, nullptr, + r4d_shape_all_reduce }, { "ar_oneshot_4rank_exact", "all_reduce", "ar", "all-reduce (sum), one-shot push over P2P IPC scratch, per-block handshake", "exactly 4 ranks, all peer-accessible; one scratch slot per (parity, source rank); in " diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 745b2846..9f3ab668 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -8820,14 +8820,17 @@ struct ArThread { /* The all-gather row width in ELEMENTS. 0 is the plain concatenation; anything else asks for * the row-interleaved placement the vocab-parallel sampler needs. */ int64_t row_elems = 0; + /* Ranks in the collective. 2 is the pair; 3 exercises the wide family at the one width that + * is not a power of two. */ + int world = 2; }; static void ar_worker(ArThread* t) { if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; } std::vector ps = { - PI("world_size", 2), PI("numel", t->numel), PS("dtype", "fp32"), PI("exact", 1), + PI("world_size", t->world), PI("numel", t->numel), PS("dtype", "fp32"), PI("exact", 1), }; - t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 2, &t->inst); + t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, t->world, &t->inst); if (t->rc_init != RAD_OK) return; void* in = nullptr; @@ -8845,7 +8848,7 @@ static void ar_worker(ArThread* t) { a.p = ps.data(); a.n_p = (int)ps.size(); a.instance = t->inst; a.rank = t->rank; - a.world_size = 2; + a.world_size = t->world; t->rc_launch = t->row->launch(&a, nullptr); st_stream_sync(nullptr); @@ -8905,6 +8908,7 @@ struct ArSweep { void* inst = nullptr; int rc_init = RAD_E_STATE, rc_launch = RAD_OK; int64_t bad = 0; + int world = 2; }; static void ar_sweep_worker(ArSweep* t) { @@ -8912,10 +8916,12 @@ static void ar_sweep_worker(ArSweep* t) { int64_t most = 0; for (int64_t n : kArSweepNumel) most = n > most ? n : most; std::vector ps = { - PI("world_size", 2), PI("numel", most), PS("dtype", "fp32"), PI("exact", 1), + PI("world_size", t->world), PI("numel", most), PS("dtype", "fp32"), PI("exact", 1), }; - t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 2, &t->inst); + t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, t->world, &t->inst); if (t->rc_init != RAD_OK) return; + /* Rank r contributes (r + 1) * (j + 1), so launch j sums to (1 + 2 + .. + world) * (j + 1). */ + const float tri = (float)(t->world * (t->world + 1) / 2); void* in[kArSweepN] = {}; void* out[kArSweepN] = {}; @@ -8939,14 +8945,14 @@ static void ar_sweep_worker(ArSweep* t) { a.p = ps.data(); a.n_p = (int)ps.size(); a.instance = t->inst; a.rank = t->rank; - a.world_size = 2; + a.world_size = t->world; t->rc_launch = t->row->launch(&a, nullptr); } st_stream_sync(nullptr); for (int j = 0; j < kArSweepN; ++j) { std::vector h((size_t)kArSweepNumel[j]); st_memcpy(h.data(), out[j], h.size() * 4, hipMemcpyDeviceToHost); - for (float v : h) if (v != (float)(3 * (j + 1))) ++t->bad; + for (float v : h) if (v != tri * (float)(j + 1)) ++t->bad; st_free(in[j]); st_free(out[j]); } @@ -8984,16 +8990,16 @@ static void ar_two_rank_sweep(const Plugin& pl, const std::vector& devs) { static void gather_worker(ArThread* t) { if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; } std::vector ps = { - PI("world_size", 2), PI("numel", t->numel), PS("dtype", "f32"), + PI("world_size", t->world), PI("numel", t->numel), PS("dtype", "f32"), }; if (t->row_elems > 0) ps.push_back(PI("row", t->row_elems)); - t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 2, &t->inst); + t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, t->world, &t->inst); if (t->rc_init != RAD_OK) return; void* in = nullptr; void* out = nullptr; if (hipMalloc(&in, (size_t)t->numel * 4) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; } - if (hipMalloc(&out, (size_t)t->numel * 8) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; } + if (hipMalloc(&out, (size_t)t->numel * 4 * t->world) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; } std::vector h((size_t)t->numel); for (int64_t i = 0; i < t->numel; i += 2) { @@ -9002,20 +9008,20 @@ static void gather_worker(ArThread* t) { std::memcpy(&h[(size_t)i + 1], &v, 4); } st_memcpy(in, h.data(), h.size() * 4, hipMemcpyHostToDevice); - st_memset(out, 0xAB, (size_t)t->numel * 8); /* so an untouched half is visibly untouched */ + st_memset(out, 0xAB, (size_t)t->numel * 4 * t->world); /* so an untouched slice is visibly untouched */ - RadTensor ts[2] = { T(in, RAD_F32, { t->numel }), T(out, RAD_F32, { t->numel * 2 }) }; + RadTensor ts[2] = { T(in, RAD_F32, { t->numel }), T(out, RAD_F32, { t->numel * t->world }) }; RadArgs a; std::memset(&a, 0, sizeof(a)); a.t = ts; a.n_t = 2; a.p = ps.data(); a.n_p = (int)ps.size(); a.instance = t->inst; a.rank = t->rank; - a.world_size = 2; + a.world_size = t->world; t->rc_launch = t->row->launch(&a, nullptr); st_stream_sync(nullptr); - t->out.assign((size_t)t->numel * 2, 0.0f); + t->out.assign((size_t)t->numel * t->world, 0.0f); st_memcpy(t->out.data(), out, t->out.size() * 4, hipMemcpyDeviceToHost); st_free(in); st_free(out); @@ -9105,6 +9111,151 @@ static void gather_two_rank(const Plugin& pl, const std::vector& devs) { ok("2-rank all-gather fini ran", true); } +/* ------------------------------------------------------------------ the three-rank wide family */ +/* THE ONE WIDTH THAT IS NOT A POWER OF TWO. The wide kernels are templated on the world size and + * shipped at 4 and 8; three cards instantiate the same template at 3, and these are the checks that + * the instantiation, the dispatch rows and the entry points agree: a rendezvous of three, a sum of + * 1 + 2 + 3 on every rank bit for bit, the slot parity surviving a block-count sweep, and the + * gather placing three slices in both of its layouts. Skipped, not failed, on fewer cards. */ +static void ar_three_rank(const Plugin& pl, const std::vector& devs) { + const RadKernelInfo* row = find(pl, "ar_oneshot_3rank_exact"); + if (!row) { ok("ar_oneshot_3rank_exact", false, "row absent"); return; } + if (devs.size() < 3) { skip("ar_oneshot_3rank_exact (3 ranks)", "fewer than three gfx1201 cards"); return; } + const int64_t numel = 8192; + ArThread a{ row, 0, devs[0], numel }, b{ row, 1, devs[1], numel }, c{ row, 2, devs[2], numel }; + a.world = b.world = c.world = 3; + std::thread ta(ar_worker, &a), tb(ar_worker, &b), tc(ar_worker, &c); + ta.join(); + tb.join(); + tc.join(); + ok("3-rank all-reduce init (peer rendezvous)", + a.rc_init == RAD_OK && b.rc_init == RAD_OK && c.rc_init == RAD_OK, + "rc " + std::to_string(a.rc_init) + "/" + std::to_string(b.rc_init) + "/" + + std::to_string(c.rc_init)); + if (a.rc_init != RAD_OK || b.rc_init != RAD_OK || c.rc_init != RAD_OK) return; + ok("3-rank all-reduce launch", + a.rc_launch == RAD_OK && b.rc_launch == RAD_OK && c.rc_launch == RAD_OK, + "rc " + std::to_string(a.rc_launch) + "/" + std::to_string(b.rc_launch) + "/" + + std::to_string(c.rc_launch)); + int64_t bad = 0; + for (int64_t i = 0; i < numel; ++i) { + if (a.out[(size_t)i] != 6.0f) ++bad; + if (b.out[(size_t)i] != 6.0f) ++bad; + if (c.out[(size_t)i] != 6.0f) ++bad; + } + ok("3-rank all-reduce sums 1 + 2 + 3 == 6 on every rank, bit for bit", bad == 0, + std::to_string(bad) + " elements wrong"); + if (row->fini) { row->fini(a.inst); row->fini(b.inst); row->fini(c.inst); } + ok("3-rank all-reduce fini ran", true); + + /* Block counts that change launch to launch on one instance: the wide family takes its slot + * parity from the shared counters exactly as the pair does, and this is the check that it + * does so at a width whose flag array is [block][3]. */ + ArSweep x{ row, 0, devs[0] }, y{ row, 1, devs[1] }, z{ row, 2, devs[2] }; + x.world = y.world = z.world = 3; + std::thread tx(ar_sweep_worker, &x), ty(ar_sweep_worker, &y), tz(ar_sweep_worker, &z); + tx.join(); + ty.join(); + tz.join(); + const bool up = x.rc_init == RAD_OK && y.rc_init == RAD_OK && z.rc_init == RAD_OK && + x.rc_launch == RAD_OK && y.rc_launch == RAD_OK && z.rc_launch == RAD_OK; + ok("3-rank all-reduce, eight launches at changing block counts on one instance", + up && x.bad == 0 && y.bad == 0 && z.bad == 0, + up ? std::to_string(x.bad + y.bad + z.bad) + " elements wrong" + : "rc " + std::to_string(x.rc_init) + "/" + std::to_string(y.rc_init) + "/" + + std::to_string(z.rc_init) + " init, " + std::to_string(x.rc_launch) + "/" + + std::to_string(y.rc_launch) + "/" + std::to_string(z.rc_launch) + " launch"); + if (row->fini) { + if (x.inst) row->fini(x.inst); + if (y.inst) row->fini(y.inst); + if (z.inst) row->fini(z.inst); + } +} + +static void gather_three_rank(const Plugin& pl, const std::vector& devs) { + const RadKernelInfo* row = find(pl, "ar_gather_3rank"); + if (!row) { ok("ar_gather_3rank", false, "row absent"); return; } + if (devs.size() < 3) { skip("ar_gather_3rank (3 ranks)", "fewer than three gfx1201 cards"); return; } + const int64_t numel = 8192; /* the sampler's own message at 4 rows and 1024 candidates */ + const int W = 3; + /* What each rank sent, rebuilt here rather than read back off the device (gather_two_rank). */ + auto sent = [](int rank, int64_t i) -> uint32_t { + if ((i & 1) == 0) return (uint32_t)(rank * 124160 + (int)i + 1); + uint32_t u; const float v = (float)(rank + 1) * 0.5f + (float)(i - 1); + std::memcpy(&u, &v, 4); + return u; + }; + { + ArThread a{ row, 0, devs[0], numel }, b{ row, 1, devs[1], numel }, c{ row, 2, devs[2], numel }; + a.world = b.world = c.world = W; + std::thread ta(gather_worker, &a), tb(gather_worker, &b), tc(gather_worker, &c); + ta.join(); + tb.join(); + tc.join(); + ok("3-rank all-gather init (peer rendezvous)", + a.rc_init == RAD_OK && b.rc_init == RAD_OK && c.rc_init == RAD_OK, + "rc " + std::to_string(a.rc_init) + "/" + std::to_string(b.rc_init) + "/" + + std::to_string(c.rc_init)); + if (a.rc_init != RAD_OK || b.rc_init != RAD_OK || c.rc_init != RAD_OK) return; + ok("3-rank all-gather launch", + a.rc_launch == RAD_OK && b.rc_launch == RAD_OK && c.rc_launch == RAD_OK, + "rc " + std::to_string(a.rc_launch) + "/" + std::to_string(b.rc_launch) + "/" + + std::to_string(c.rc_launch)); + int64_t bad = 0, bad_id = 0; + for (const ArThread* t : { &a, &b, &c }) { + for (int r = 0; r < W; ++r) { + for (int64_t i = 0; i < numel; ++i) { + uint32_t got; + std::memcpy(&got, &t->out[(size_t)(r * numel + i)], 4); + if (got == sent(r, i)) continue; + ++bad; + if ((i & 1) == 0) ++bad_id; + } + } + } + ok("3-rank all-gather places every rank's bytes on every rank, bit for bit", bad == 0, + std::to_string(bad) + " words wrong, " + std::to_string(bad_id) + " of them token ids"); + if (row->fini) { row->fini(a.inst); row->fini(b.inst); row->fini(c.inst); } + } + + /* The row-interleaved placement the sampler asks for: [rows][3][row]. */ + const int64_t rowe = 2048; /* 4 rows of 1024 (id, logit) pairs */ + ArThread c{ row, 0, devs[0], numel }, d{ row, 1, devs[1], numel }, e{ row, 2, devs[2], numel }; + c.world = d.world = e.world = W; + c.row_elems = d.row_elems = e.row_elems = rowe; + std::thread tc(gather_worker, &c), td(gather_worker, &d), te(gather_worker, &e); + tc.join(); + td.join(); + te.join(); + if (c.rc_init != RAD_OK || d.rc_init != RAD_OK || e.rc_init != RAD_OK || + c.rc_launch != RAD_OK || d.rc_launch != RAD_OK || e.rc_launch != RAD_OK) { + ok("3-rank all-gather, row-interleaved", false, + "rc " + std::to_string(c.rc_init) + "/" + std::to_string(d.rc_init) + "/" + + std::to_string(e.rc_init) + " init, " + std::to_string(c.rc_launch) + "/" + + std::to_string(d.rc_launch) + "/" + std::to_string(e.rc_launch) + " launch"); + return; + } + const int64_t nrows = numel / rowe; + int64_t bad = 0, bad_id = 0; + for (const ArThread* t : { &c, &d, &e }) { + for (int64_t m = 0; m < nrows; ++m) { + for (int r = 0; r < W; ++r) { + for (int64_t j = 0; j < rowe; ++j) { + uint32_t got; + std::memcpy(&got, &t->out[(size_t)(m * W * rowe + r * rowe + j)], 4); + if (got == sent(r, m * rowe + j)) continue; + ++bad; + if ((j & 1) == 0) ++bad_id; + } + } + } + } + ok("3-rank all-gather interleaves by row, so one row carries all three ranks", bad == 0, + std::to_string(bad) + " words wrong, " + std::to_string(bad_id) + " of them token ids"); + if (row->fini) { row->fini(c.inst); row->fini(d.inst); row->fini(e.inst); } + ok("3-rank all-gather fini ran", true); +} + /* ------------------------------------------------------------------ the device sweep */ /* NGRAM_IDS' `ahead_ids`: the prompt that follows the step, hashed in the same launch. Step one @@ -10297,6 +10448,17 @@ static void device_phase(const Plugin& pl) { if (!g_case) std::printf("\ncross-rank\n"); gather_two_rank(pl, devs); } + /* The wide family at three. Its own rows, so the sweep names them (a row the sweep never + * mentions is reported as a coverage hole) and so `--case ar_oneshot_3rank_exact` reproduces + * one of them alone. */ + if (sel("ar_oneshot_3rank_exact")) { + if (!g_case) std::printf("\ncross-rank, three\n"); + ar_three_rank(pl, devs); + } + if (sel("ar_gather_3rank")) { + if (!g_case) std::printf("\ncross-rank, three\n"); + gather_three_rank(pl, devs); + } /* The 4- and 8-rank rows cannot be exercised on a two-card box. Reported as skipped rather * than quietly passing: a row that has never been launched is not a row that works. */ for (const char* n : { "ar_oneshot_4rank_exact", "ar_oneshot_8rank_exact",