diff --git a/README.md b/README.md index 2ad063a9..9f21a500 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,8 @@ At `--tp 4` the gated residual's write carries the exact all-reduce, with the Mo of it, on decode and verify steps; a prefill chunk runs the unfused chain. That chain's exact all-reduce is one-shot up to 1 MiB a message and two-shot above that, which sends half the bytes on a prefill chunk; both produce the same bits. The row above runs exact. -`--tp-wire wht6` at four ranks selects a different lossy wire, tiered int8 over the two-shot, -above `--tp-wire-min-kb`. Eight ranks have no lossy wire, so `--tp 8 --tp-wire wht6` stops at +`--tp-wire wht6` at three and four ranks selects a different lossy wire, tiered int8 over the +two-shot, above `--tp-wire-min-kb`. Eight ranks have no lossy wire, so `--tp 8 --tp-wire wht6` stops at startup naming the all-reduce. Four ranks is a capacity configuration; its per-step rate against `--tp 2` on the same cards has not been measured. The tiering flags are the tp2 recipe carried over. Above two ranks each rank still owns a **slice of diff --git a/core/config.cpp b/core/config.cpp index 6997a40f..4b7be5d6 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -46,8 +46,8 @@ static const Flag g_flags[] = { { "--tp", "N", "tensor-parallel ranks" }, { "--tp-wire", "MODE", "cross-rank all-reduce payload: exact (default) or wht6, a\n" - "lossy wire: rotated 6-bit at 2 ranks, tiered int8 at 4,\n" - "none at 8" }, + "lossy wire: rotated 6-bit at 2 ranks, tiered int8 at 3\n" + "and 4, none at 8" }, { "--tp-wire-min-kb", "N", "smallest all-reduce, in KiB, the lossy wire is used for" }, { "--p2p", "MODE", "how the collectives reach the other cards: auto (default)\n" "asks the driver, on requires peer access and stops without\n" diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 10c1caf0..d8ae745c 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -956,7 +956,7 @@ int Engine::declare() { const int64_t exact_rows = (cfg_.tp_wire_min_kb * 1024 + row_bytes - 1) / row_bytes - 1; RAD_WARN("tp wire: LOSSY (--tp-wire wht6) at %lld KiB a message and above. A cross-rank " "all-reduce of that size carries a quantised payload instead of bf16 (libr4d's " - "is Walsh-Hadamard-rotated 6-bit at two ranks and tiered int8 at four), so this " + "is Walsh-Hadamard-rotated 6-bit at two ranks and tiered int8 at three and four), so this " "process does not compute the same function the exact wire does. Steps of at " "most %lld rows are exact: single-sequence decode, and a " "decode step until sequences x (1 + speculative depth) passes that; prefill and " diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 1366b6fb..7e4097bb 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -791,8 +791,9 @@ which is why it is a named flag and not a heuristic. `--tp-wire-min-kb` is the floor below which messages are still served exactly — 128 KiB leaves single-sequence decode exact and compresses prefill, which is where the win is. -At four ranks `wht6` selects a different lossy wire with the same floor: tiered int8 over a -two-shot all-reduce, both hops quantised. Eight ranks have no lossy wire, and `--tp 8 --tp-wire +At three and four ranks `wht6` selects a different lossy wire with the same floor: tiered int8 +over a two-shot all-reduce, both hops quantised (at three the shards are whole 32-element groups, +one group uneven where the message does not divide). Eight ranks have no lossy wire, and `--tp 8 --tp-wire wht6` stops at startup naming the all-reduce it found no kernel for. The exact wire at four and eight ranks is one-shot up to 1 MiB a message and two-shot above it, which sends 2·(ranks−1)/ranks of the message per rank instead of (ranks−1) times it; the two produce the same diff --git a/docs/TP3.md b/docs/TP3.md index 9d9baf18..2f9daa89 100644 --- a/docs/TP3.md +++ b/docs/TP3.md @@ -132,6 +132,8 @@ decode step) and its two-shot above it (every prefill chunk): reduce-scatter + a sends 4/3 of the message per rank instead of twice it. A message rarely divides into thirds -- a 2048-wide bf16 row is 256 16-byte words -- so the first `words % 3` shards take one word more. Both rows fold in fp32 in ascending rank order, so the band edge between them changes no bits. +`--tp-wire wht6` at three ranks is the four-rank tiered-int8 wire over the same uneven two-shot, +above `--tp-wire-min-kb`; under it, the exact one-shot. The log says what the geometry did: diff --git a/libr4d/r4d.h b/libr4d/r4d.h index ffeac74d..b497ab09 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -422,12 +422,19 @@ void r4d_ar_twoshot_8rank_exact(long peer_scratch0, long peer_scratch1, long pee long out, long n_elem, long dtype, long rank, long stream, long nblocks, long nthreads, long drain, long acq, long pub); // The tiered int8 wire on both of the two-shot's hops (r4d_ar_twoshot_Nrank_ti8.hip; codec -// in r4d_ar_ti8.h), 4 ranks only. The wire: int8 groups of 32 with an fp16 scale and a +// in r4d_ar_ti8.h), 3 or 4 ranks. The wire: int8 groups of 32 with an fp16 scale and a // per-group record (T1 plain, T2 top-1-exact, T3 wht32) 38 bytes/group, deterministic // tiering, bit-identical across ranks. Same peer-argument order and flag layout as the exact // pair; slot stride in bytes (the wire is not 16B-aligned), holding two wire images; the -// encoder writes its own slot too (no loc_pack). numel must tile 32-elem groups over the ws -// shards (numel % 128). +// encoder writes its own slot too (no loc_pack). numel must be whole 32-elem groups; the +// groups split over the ranks as evenly as they divide (the first ng % ws shards one group +// longer) and every slot is laid out for the longest shard. The 4-rank entry still asks for +// numel % 128, the even split. +void r4d_ar_twoshot_3rank_ti8(long peer_scratch0, long peer_scratch1, long peer_flags0, + long peer_flags1, long my_scratch, long my_flags, long seq_ctrs, + long slot_stride_bytes, 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_twoshot_4rank_ti8(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, diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 87c15e8d..2e52c93c 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -953,6 +953,43 @@ extern "C" int r4d_rad_ar_twoshot_8rank_exact(const RadArgs* a, RadStream s) { * four-rank crossover has been measured to set another. In place is safe for the exact two-shot's * reason: a thread owns the same 8 elements of a shard in all three phases, reads them from the * input before it writes them, and no peer reads this rank's input. */ +/* The same wire at three ranks, over the uneven two-shot (r4d_ar_twoshot_Nrank_ti8.hip): the only + * lossy row at three, so as at four a lossy request resolves it at every size and a message + * under `min_bytes` -- or one that is not whole 32-element groups -- goes to the exact one-shot. + * A 2048-wide row is 64 groups, so the residual is always whole groups; what it rarely is, is a + * multiple of three groups, and the kernel's split does not need it to be. */ +extern "C" int r4d_rad_ar_twoshot_3rank_ti8(const RadArgs* a, RadStream s) { + ArCall c; + int rc = ar_setup(a, &c, 1); + if (rc != RAD_OK) return rc; + if (c.in->world != 3) return RAD_E_SHAPE; + if (c.in->dtype == 2) return RAD_E_DTYPE; + + int group = 0, group_bytes = 0; + r4d_ar_ti8_dims(&group, &group_bytes); + if (group <= 0) return RAD_E_DEVICE; + const long long floor_b = r4d_pgeti_or(a->p, a->n_p, "min_bytes", R4D_AR_WHT6_MIN_BYTES); + const long n_groups = c.numel / group; + if ((long long)c.numel * c.in->esize < floor_b || c.numel % group || n_groups < c.in->world) + return r4d_rad_ar_oneshot_3rank_exact(a, s); + /* The slot holds two wire images of the LONGEST shard. */ + const long sg = (n_groups + c.in->world - 1) / c.in->world; + const long wire = (sg * group_bytes + 15) / 16 * 16; + if (2 * wire > c.in->slot_bytes) return RAD_E_SCRATCH; + /* nthreads must be a multiple of the encoder's group width -- a throw in the entry point. */ + if (c.nthreads % 32) return RAD_E_INVAL; + + const ArInst* in = c.in; + R4D_TRY("ar_twoshot_3rank_ti8", + r4d_ar_twoshot_3rank_ti8(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->slot_bytes, + 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_twoshot_4rank_ti8(const RadArgs* a, RadStream s) { ArCall c; int rc = ar_setup(a, &c, 1); diff --git a/libr4d/r4d_ar_twoshot_Nrank_ti8.hip b/libr4d/r4d_ar_twoshot_Nrank_ti8.hip index 783d7876..16b1b574 100644 --- a/libr4d/r4d_ar_twoshot_Nrank_ti8.hip +++ b/libr4d/r4d_ar_twoshot_Nrank_ti8.hip @@ -1,5 +1,5 @@ -// r4d_ar_twoshot_Nrank_ti8.hip: r4d_ar_twoshot_4rank_ti8 -- reduce-scatter + all-gather with -// the tiered-int8 wire on both hops, at 4 ranks. Codec (tiers 1/2/3) from r4d_ar_ti8.h, +// r4d_ar_twoshot_Nrank_ti8.hip: r4d_ar_twoshot_{3,4}rank_ti8 -- reduce-scatter + all-gather with +// the tiered-int8 wire on both hops, at 3 or 4 ranks. Codec (tiers 1/2/3) from r4d_ar_ti8.h, // transport from r4d_ar_twoshot_Nrank_exact.hip. The second hop costs 38/64 of its exact // bytes. Own contribution reads exact from the input at fold time. // @@ -10,6 +10,13 @@ // // Slot: region A = ti8 wire (38*sg bytes, codes | rec | scales), region B at align16(A end) // a second wire image of the same layout; the stride must hold both. +// +// UNEVEN SHARDS, in whole groups (the exact two-shot's rule one level up): ng = numel/32 groups +// split ng/ws a shard and the first ng % ws take one more, so shard s starts at group +// s*base + min(s, rem). Every slot is laid out for the LONGEST shard (sg_max), whichever shard it +// carries, so a writer and a reader never have to agree on anything but sg_max. At 4 ranks the +// entry still requires numel to tile 32 x 4 and this is the even split it always was; at 3 a +// 2048-wide row is 64 groups and 64 * rows divides by 3 one step in three. #include "r4d.h" #include "r4d_ar_ti8.h" #include "r4d_ar_wide.h" @@ -20,7 +27,7 @@ template __global__ void r4d_ar_twoshot_wide_ti8_kernel( const T* __restrict__ in, R4DArWidePeers peers, char* __restrict__ my_base, unsigned int* __restrict__ my_flags, T* __restrict__ out, - unsigned int* __restrict__ seq_ctrs, int shard_elems, long slot_stride_bytes, + unsigned int* __restrict__ seq_ctrs, int n_groups, long slot_stride_bytes, long b_off_bytes, int rank, int drain, int acq, int pub) { constexpr int NPEERS = WS - 1; const int b = blockIdx.x, nb = gridDim.x, tid = threadIdx.x, nt = blockDim.x; @@ -29,7 +36,11 @@ __global__ void r4d_ar_twoshot_wide_ti8_kernel( const unsigned int s = s_seq; const int parity = (int)s_slot; - const int sg = shard_elems / TI8_GROUP; // groups per shard + // shard s = groups [s*gbase + min(s, grem), +gbase + (s < grem)); sg is the longest + const int gbase = n_groups / WS, grem = n_groups % WS; + const int sg = gbase + (grem ? 1 : 0); +#define R4D_TI8_OFF(s) ((size_t)((s) * gbase + ((s) < grem ? (s) : grem)) * TI8_GROUP) +#define R4D_TI8_LEN(s) (gbase + ((s) < grem ? 1 : 0)) const int rec_off = TI8_REC_OFF * sg, sc_off = TI8_SC_OFF * sg; // wire layout: codes | rec | scales const int gpb = (sg + nb - 1) / nb; // block b owns shard groups [gA, gB) const int gA = b * gpb; @@ -43,8 +54,9 @@ __global__ void r4d_ar_twoshot_wide_ti8_kernel( // ---- phase 1: encode my contribution to each peer-owned shard into its owner's // region A. My own shard's contribution stays exact, read from the input at fold time. for (int p = 0; p < NPEERS; ++p) { - const T* src = in + (size_t)peers.rank[p] * shard_elems; - for (int k = 4 * gA + tid; k < 4 * gB; k += nt) { + const T* src = in + R4D_TI8_OFF(peers.rank[p]); + const int gE = gB < R4D_TI8_LEN(peers.rank[p]) ? gB : R4D_TI8_LEN(peers.rank[p]); + for (int k = 4 * gA + tid; k < 4 * gE; k += nt) { const int lig = k & 3; const int g = k >> 2; uint2 codes; @@ -63,9 +75,10 @@ __global__ void r4d_ar_twoshot_wide_ti8_kernel( // ---- reduce my shard: own contribution exact from the input, peers decoded from // region A, fp32 fold in ascending rank order; the reduced group is re-encoded and its // wire words go to every peer's region B, the owner keeping their decode. - const T* my_in = in + (size_t)rank * shard_elems; - T* my_out = out + (size_t)rank * shard_elems; - for (int k = 4 * gA + tid; k < 4 * gB; k += nt) { + const T* my_in = in + R4D_TI8_OFF(rank); + T* my_out = out + R4D_TI8_OFF(rank); + const int gM = gB < R4D_TI8_LEN(rank) ? gB : R4D_TI8_LEN(rank); + for (int k = 4 * gA + tid; k < 4 * gM; k += nt) { const int lig = k & 3; const int g = k >> 2; float acc[TI8_EPL]; @@ -111,8 +124,9 @@ __global__ void r4d_ar_twoshot_wide_ti8_kernel( const int owner = peers.rank[p]; const char* baseB = my_base + ((size_t)parity * WS + owner) * (size_t)slot_stride_bytes + b_off_bytes; - T* dst = out + (size_t)owner * shard_elems; - for (int k = 4 * gA + tid; k < 4 * gB; k += nt) { + T* dst = out + R4D_TI8_OFF(owner); + const int gE = gB < R4D_TI8_LEN(owner) ? gB : R4D_TI8_LEN(owner); + for (int k = 4 * gA + tid; k < 4 * gE; k += nt) { const int lig = k & 3; const int g = k >> 2; const uint2 c = ((const uint2*)baseB)[k]; @@ -123,6 +137,8 @@ __global__ void r4d_ar_twoshot_wide_ti8_kernel( ti8_store8(dst + k * TI8_EPL, v); } } +#undef R4D_TI8_OFF +#undef R4D_TI8_LEN } // ---------------------------------------------------------------------------------- host side @@ -138,11 +154,13 @@ static void ar_twoshot_ti8_launch(const long* peer_scratch, const long* peer_fla " out of range for the " + std::to_string(WS) + " ranks this entry point is compiled for"); if (dtype != 0 && dtype != 1) throw std::runtime_error("ti8 ar: only bf16/fp16 payloads"); - if (n_elem % ((long)TI8_GROUP * WS) != 0) - throw std::runtime_error("ti8 ar: n_elem must tile " + std::to_string(TI8_GROUP) + - "-elem groups over " + std::to_string(WS) + " shards"); - const int shard_elems = (int)(n_elem / WS); - const int sg = shard_elems / TI8_GROUP; + if (n_elem % TI8_GROUP != 0) + throw std::runtime_error("ti8 ar: n_elem must be whole " + std::to_string(TI8_GROUP) + + "-elem groups"); + const long n_groups = n_elem / TI8_GROUP; + if (n_groups < WS) + throw std::runtime_error("ti8 ar: fewer groups than ranks"); + const int sg = (int)((n_groups + WS - 1) / WS); // the longest shard, the slot layout's size const long b_off = ((long)TI8_GROUP_BYTES * sg + 15) / 16 * 16; // one wire image, 16B-aligned if (2 * b_off > slot_stride_bytes) throw std::runtime_error("ti8 ar: slot stride " + std::to_string(slot_stride_bytes) + @@ -194,13 +212,25 @@ static void ar_twoshot_ti8_launch(const long* peer_scratch, const long* peer_fla #define R4D_AR_TS8_LAUNCH(T) \ r4d_ar_twoshot_wide_ti8_kernel<<>>( \ (const T*)inp, peers, (char*)my_scratch, (unsigned int*)my_flags, (T*)out, \ - (unsigned int*)seq_ctrs, shard_elems, (long)slot_stride_bytes, b_off, (int)rank, \ + (unsigned int*)seq_ctrs, (int)n_groups, (long)slot_stride_bytes, b_off, (int)rank, \ (int)drain, (int)acq, (int)pub) if (dtype == 0) R4D_AR_TS8_LAUNCH(__hip_bfloat16); else R4D_AR_TS8_LAUNCH(__half); #undef R4D_AR_TS8_LAUNCH } +void r4d_ar_twoshot_3rank_ti8(long peer_scratch0, long peer_scratch1, long peer_flags0, + long peer_flags1, long my_scratch, long my_flags, long seq_ctrs, + long slot_stride_bytes, 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_twoshot_ti8_launch<3>(ps, pf, my_scratch, my_flags, seq_ctrs, slot_stride_bytes, inp, + out, n_elem, dtype, rank, stream, nblocks, nthreads, drain, + acq, pub); +} + void r4d_ar_twoshot_4rank_ti8(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, diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 51d74906..b139a532 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -188,6 +188,7 @@ int r4d_rad_ar_oneshot_8rank_exact(const RadArgs*, RadStream); int r4d_rad_ar_twoshot_3rank_exact(const RadArgs*, RadStream); int r4d_rad_ar_twoshot_4rank_exact(const RadArgs*, RadStream); int r4d_rad_ar_twoshot_8rank_exact(const RadArgs*, RadStream); +int r4d_rad_ar_twoshot_3rank_ti8 (const RadArgs*, RadStream); int r4d_rad_ar_twoshot_4rank_ti8 (const RadArgs*, RadStream); /* The fused all-reduce + norm shares the IPC set, so it shares init/fini. */ int r4d_rad_ar_ln_init(const RadParam*, int, int rank, int world, void** out); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index f3727701..ffbd2715 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2691,6 +2691,11 @@ static const RadConstraint cArWht6[] = { C_EQ("world_size", 2), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 64), C_P2P, }; +/* DIV 32, not 96: the three-rank split is uneven in whole groups (r4d_rad_ar_twoshot_3rank_ti8). */ +static const RadConstraint cArTi8Ts3[] = { + C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32), + C_P2P, +}; static const RadConstraint cArTi8Ts4[] = { C_EQ("world_size", 4), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 128), C_P2P, @@ -4484,6 +4489,18 @@ static const RadKernelInfo kKernels[] = { r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_oneshot_8rank_exact, nullptr, nullptr, nullptr, r4d_shape_all_reduce }, +{ "ar_twoshot_3rank_ti8", "all_reduce", "ar", + "all-reduce (sum), reduce-scatter + all-gather; both hops ride the tiered-int8 wire (double " + "quant, the owner storing the decode of its own words) -- lossy", + "exactly 3 ranks, all peer-accessible; groups of 32, split over the shards as evenly as they " + "divide (the first groups % 3 take one more); under min_bytes the exact one-shot; cudagraph-safe", + "bf16 / fp16 payload, int8 + fp16 scale + u32 record per group on the wire, fp32 accumulate in " + "ascending rank order, bit-identical across ranks", + RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT, + ROW(cArTi8Ts3), ROW(tAr), nullptr, + r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr, + r4d_shape_all_reduce }, + { "ar_twoshot_4rank_ti8", "all_reduce", "ar", "all-reduce (sum), reduce-scatter + all-gather; both hops ride the tiered-int8 wire (double " "quant, the owner storing the decode of its own words) -- lossy", diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index e60ebbb4..2cf5af7c 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -10826,6 +10826,127 @@ static void ar_three_rank_pattern(const Plugin& pl, const std::vector& devs } } +/* THE THREE-RANK TIERED-INT8 WIRE, held to what a lossy wire can be held to. + * + * Exactly: every rank ends with the same bytes (the owner stores the decode of the words it + * published, so the wire is bit-identical across ranks by construction), and a message the wire + * declines -- under `min_bytes`, or not whole 32-element groups -- is the exact one-shot's fold, + * bit for bit. Statistically: the rest is within a quantisation error of the fp32 sum. The input is + * the per-element pattern plus a large outlier every 997 elements on rank 1 (the record's tier 2 + * keeps a dominant element exact), and the sizes walk groups % 3 of 0, 1 and 2 to the 20971520 + * ceiling, so a group reduced by the wrong owner or read from the wrong slot is an error of the + * order of the values themselves -- a relative RMS near 1, not the few tenths of a percent an int8 + * wire costs. */ +struct ArTi8 { + const RadKernelInfo* row; + int rank, dev; + std::vector sizes; + void* inst = nullptr; + int rc_init = RAD_E_STATE, rc_launch = RAD_OK; + std::vector> out; +}; + +static float ar_ti8_value(int rank, int64_t i) { + if (rank == 1 && i % 997 == 13) return 900.0f; /* exact in bf16 */ + return ar_pattern_value(rank, i); +} + +static void ar_ti8_worker(ArTi8* t) { + if (st_set_device(t->dev) != hipSuccess) { t->rc_init = RAD_E_DEVICE; return; } + int64_t most = 0; + for (int64_t n : t->sizes) most = n > most ? n : most; + std::vector ps = { + PI("world_size", 3), PI("numel", most), PS("dtype", "bf16"), PI("exact", 0), + PI("min_bytes", 131072), + }; + t->rc_init = t->row->init(ps.data(), (int)ps.size(), t->rank, 3, &t->inst); + if (t->rc_init != RAD_OK) return; + void* buf = nullptr; + if (hipMalloc(&buf, (size_t)most * 2) != hipSuccess) { t->rc_launch = RAD_E_NOMEM; return; } + std::vector h((size_t)most); + for (int64_t n : t->sizes) { + for (int64_t i = 0; i < n; ++i) h[(size_t)i] = h_to_bf16(ar_ti8_value(t->rank, i)); + st_memcpy(buf, h.data(), (size_t)n * 2, hipMemcpyHostToDevice); + RadTensor ts[1] = { T(buf, RAD_BF16, { n }) }; + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.t = ts; a.n_t = 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; + std::vector got((size_t)n); + st_memcpy(got.data(), buf, (size_t)n * 2, hipMemcpyDeviceToHost); + t->out.push_back(std::move(got)); + } + st_free(buf); +} + +static void ar_three_rank_ti8(const Plugin& pl, const std::vector& 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; } + /* 32768 is under the 128 KiB floor and 65544 is not whole groups: both exact. The rest are + * lossy: 65536 sits ON the floor (2048 groups, % 3 = 2), then 2048 x 2048 / 2049 / 2050 rows + * (groups % 3 = 2, 0, 1) and the ceiling (655360 groups, % 3 = 1), and 2048 rows again. */ + const std::vector sizes = { 4194304, 32768, 65536, 4196352, 65544, 4198400, 20971520, + 4194304 }; + ArTi8 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].sizes = sizes; } + std::thread a(ar_ti8_worker, &t[0]), b(ar_ti8_worker, &t[1]), c(ar_ti8_worker, &t[2]); + a.join(); b.join(); c.join(); + const std::string nm(name); + const bool up = t[0].rc_init == RAD_OK && t[1].rc_init == RAD_OK && t[2].rc_init == RAD_OK; + ok((nm + " 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((nm + " 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) { + for (size_t k = 0; k < sizes.size(); ++k) { + const int64_t n = sizes[k]; + const bool lossy = n * 2 >= 131072 && n % 32 == 0; + int64_t diff_ranks = 0, inexact = 0; + double se = 0.0, sx = 0.0, worst = 0.0; + for (int64_t i = 0; i < n; ++i) { + const uint16_t g0 = t[0].out[k][(size_t)i]; + if (t[1].out[k][(size_t)i] != g0 || t[2].out[k][(size_t)i] != g0) ++diff_ranks; + float acc = 0.0f; + for (int r = 0; r < 3; ++r) acc += h_bf16(h_to_bf16(ar_ti8_value(r, i))); + const double e = (double)h_bf16(g0) - (double)acc; + if (!lossy && g0 != h_to_bf16(acc)) ++inexact; + se += e * e; sx += (double)acc * acc; + const double rel = std::fabs(e) / (std::fabs((double)acc) + 1.0); + if (rel > worst) worst = rel; + } + const double rrms = sx > 0 ? std::sqrt(se / sx) : 0.0; + const std::string what = nm + " numel " + std::to_string(n) + " (" + + std::to_string(n / 32) + " groups, % 3 = " + + std::to_string((n / 32) % 3) + ")"; + ok((what + ": every rank holds the same bytes").c_str(), diff_ranks == 0, + std::to_string(diff_ranks) + " elements differ between ranks"); + if (!lossy) { + ok((what + ": declined by the wire, so the exact fold, bit for bit").c_str(), + inexact == 0, std::to_string(inexact) + " elements not the exact fold"); + } else { + char d[160]; + std::snprintf(d, sizeof(d), "relative RMS %.4f%%, worst |err|/(|sum|+1) %.4f", + rrms * 100.0, worst); + std::printf(" %s: %s\n", what.c_str(), d); + ok((what + ": within the int8 wire's error of the fp32 sum (relative RMS < 1%)").c_str(), + rrms < 0.01, d); + } + } + } + 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& devs, const char* name) { const RadKernelInfo* row = find(pl, name); if (!row) { ok(name, false, "row absent"); return; } @@ -12660,7 +12781,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_twoshot_3rank_exact", "ar_gather_3rank" }) { + "ar_twoshot_3rank_exact", "ar_twoshot_3rank_ti8", "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; @@ -12676,6 +12797,8 @@ 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, "ti8")) { + ar_three_rank_ti8(pl, devs, n.c_str()); } else if (three && std::strstr(base, "twoshot")) { ar_three_rank_pattern(pl, devs, n.c_str()); } else {